'number' => 'ЕРП_РНС_' . date("Y-m-d_H-i") . '_' . $model->id
]);
- if (!WaybillWriteOffsProducts::setData($model, $shiftTransfer)) {
+ WaybillWriteOffsProducts::setData($model, $shiftTransfer);
+
+ if (!WaybillWriteOffsProducts::find()
+ ->andWhere(['waybill_write_offs_id' => $model->id])
+ ->exists()) {
$model->delete();
- } else {
- $summaries = WaybillWriteOffsProducts::find()
- ->andWhere(['waybill_write_offs_id' => $model->id])
- ->select([
- 'total_product_count' => 'sum(product_count)',
- 'total_summ' => 'sum(summ)',
- 'total_summ_self_cost' => 'sum(summ_self_cost)'
- ])
- ->asArray()
- ->one();
+ return;
+ }
+
+ $summaries = WaybillWriteOffsProducts::find()
+ ->andWhere(['waybill_write_offs_id' => $model->id])
+ ->select([
+ 'total_product_count' => 'sum(product_count)',
+ 'total_summ' => 'sum(summ)',
+ 'total_summ_self_cost' => 'sum(summ_self_cost)'
+ ])
+ ->asArray()
+ ->one();
- if ($summaries) {
- $model->updateAttributes([
- 'quantity' => $summaries['total_product_count'],
- 'summ' => $summaries['total_summ'],
- 'summ_self_cost' => $summaries['total_summ_self_cost']
- ]);
+ if ($summaries) {
+ $model->updateAttributes([
+ 'quantity' => $summaries['total_product_count'],
+ 'summ' => $summaries['total_summ'],
+ 'summ_self_cost' => $summaries['total_summ_self_cost']
+ ]);
+ }
}
} catch (\Exception $exception) {
throw new \Exception($exception);