]> gitweb.erp-flowers.ru Git - erp24_rep/yii-erp24/.git/commitdiff
ERP-259 Доработка второго шага передачи смен
authormarina <m.zozirova@gmail.com>
Fri, 6 Dec 2024 08:33:37 +0000 (11:33 +0300)
committermarina <m.zozirova@gmail.com>
Fri, 6 Dec 2024 08:33:37 +0000 (11:33 +0300)
erp24/controllers/ShiftTransferController.php
erp24/records/EqualizationRemains.php
erp24/views/shift-transfer/_replacement.php

index fc4d378b90660f1221ce17149b69329da5dcc8de..c8305838c295a24559cf0a5aad5a4796cd744531 100644 (file)
@@ -101,8 +101,7 @@ class ShiftTransferController extends Controller
                         die;
                     }
                 }
-
-
+                
                 EqualizationRemains::setData($shiftTransfer);
 
                 if ($shiftTransfer->validate()) {
index 218337f319e102d78489f31f11cafc219d1cd2c3..87f68da62b55ab2a851c6aea7fcca5561173ceb0 100644 (file)
@@ -7,6 +7,7 @@ use yii\behaviors\BlameableBehavior;
 use yii\behaviors\TimestampBehavior;
 use yii\data\ActiveDataProvider;
 use yii\db\Expression;
+use yii\helpers\ArrayHelper;
 
 /**
  * This is the model class for table "equalization_remains".
@@ -130,89 +131,63 @@ class EqualizationRemains extends \yii\db\ActiveRecord
 
     public static function setData($shiftTransfer)
     {
+        EqualizationRemains::deleteAll(['shift_id' => $shiftTransfer->id]);
+
         $plus = ShiftRemains::find()
             ->andWhere(['shift_transfer_id' => $shiftTransfer->id])
             ->andWhere(['!=', 'fact_and_1c_diff', 0])
-            ->andWhere(['>', 'fact_and_1c_diff', 0]);
+            ->andWhere(['>', 'fact_and_1c_diff', 0])
+            ->all();
 
-        $minus = ShiftRemains::find()
+        $minus = ArrayHelper::map(ShiftRemains::find()
             ->andWhere(['shift_transfer_id' => $shiftTransfer->id])
             ->andWhere(['!=', 'fact_and_1c_diff', 0])
             ->andWhere(['<', 'fact_and_1c_diff', 0])
-            ->all();
-
-        foreach ($minus as $m) {
-            $replacement = Product1cReplacement::find()
-                ->andWhere(['guid' => $m->product_guid])
-                ->select('guid_replacement')
-                ->scalar();
-            if (!$plus->andWhere(['product_guid' => $replacement])->exists()) {
-                continue;
-            }
+            ->select(['product_guid_id', 'fact_and_1c_diff', 'retail_price', 'self_cost'])
+            ->asArray()
+            ->all(), 'product_guid_id', function ($item) {
+            return ['fact_and_1c_diff' => $item['fact_and_1c_diff'], 'retail_price' => $item['retail_price'], 'self_cost' => $item['self_cost']];
+        });
 
-            $p = $plus->andWhere(['product_guid' => $replacement])->one();
-            $model = new EqualizationRemains();
-            $model->setAttributes([
-                'shift_id' => $shiftTransfer->id,
-                'product_id' => $m->product_guid,
-                'product_count' => min ($m->fact_and_1c_diff,$p->fact_and_1c_diff),
-                'product_price' => $m->retail_price,
-                'product_self_cost' => $m->self_cost,
-                'product_replacement_id' => $p->product_guid,
-                'product_replacement_count' => min ($m->fact_and_1c_diff,$p->fact_and_1c_diff),
-                'product_replacement_price' => $p->retail_price,
-                'product_replacement_self_cost' => $p->self_cost,
-                'balance' => $m->fact_and_1c_diff * ($p->retail_price - $m->retail_price),
-                'balance_self_cost' => $m->fact_and_1c_diff * ($p->self_cost - $m->self_cost)
-            ]);
-            if ($model->validate()) {
-                $model->save();
-            } elseif ($model->getErrors()) {
-                var_dump($model->errors);
-                die();
-            }
-        }
 
-        $plus =  ShiftRemains::find()
-            ->andWhere(['shift_transfer_id' => $shiftTransfer->id])
-            ->andWhere(['!=', 'fact_and_1c_diff', 0])
-            ->andWhere(['>', 'fact_and_1c_diff', 0])
-        ->all();
         foreach ($plus as $p) {
-            if (EqualizationRemains::find()
-                ->andWhere(['shift_id' => $shiftTransfer->id])
-                ->andWhere(['product_replacement_id' => $p->product_guid])
-                ->exists()) {
-                continue;
-            }
-
             $replacement = Product1cReplacement::find()
                 ->andWhere(['guid' => $p->product_guid])
                 ->select('guid_replacement')
-                ->scalar();
+                ->column();
 
-            $model = new EqualizationRemains();
-            $model->setAttributes([
-                'shift_id' => $shiftTransfer->id,
-                'product_id' => $m->product_guid,
-                'product_count' => min ($m->fact_and_1c_diff,$p->fact_and_1c_diff),
-                'product_price' => $m->retail_price,
-                'product_self_cost' => $m->self_cost,
-                'product_replacement_id' => $p->product_guid,
-                'product_replacement_count' => min ($m->fact_and_1c_diff,$p->fact_and_1c_diff),
-                'product_replacement_price' => $p->retail_price,
-                'product_replacement_self_cost' => $p->self_cost,
-                'balance' => $m->fact_and_1c_diff * ($p->retail_price - $m->retail_price),
-                'balance_self_cost' => $m->fact_and_1c_diff * ($p->self_cost - $m->self_cost)
-            ]);
-            if ($model->validate()) {
-                $model->save();
-            } elseif ($model->getErrors()) {
-                var_dump($model->errors);
-                die();
+            $quanity = $p->fact_and_1c_diff;
+
+            $intersection = array_intersect_key($minus, array_flip($replacement));
+
+            foreach ($intersection as $key => $item) {
+                if ($quanity <= 0) {
+                    break;
+                }
+
+                $model = new EqualizationRemains();
+                $model->setAttributes([
+                    'shift_id' => $shiftTransfer->id,
+                    'product_id' => $p->product_guid,
+                    'product_count' => min($quanity, abs($item['fact_and_1c_diff'])),
+                    'product_price' => $p->retail_price,
+                    'product_self_cost' => $p->self_cost,
+                    'product_replacement_id' => $key,
+                    'product_replacement_count' => min($quanity, abs($item['fact_and_1c_diff'])),
+                    'product_replacement_price' => $item['retail_price'],
+                    'product_replacement_self_cost' => $item['self_cost'],
+                    'balance' => min($quanity, abs($item['fact_and_1c_diff'])) * ($p->retail_price - $item['retail_price']),
+                    'balance_self_cost' => min($quanity, abs($item['fact_and_1c_diff'])) * ($p->self_cost - $item['self_cost'])
+                ]);
+                if ($model->validate()) {
+                    $model->save();
+                    $quanity -= min($quanity, abs($item['fact_and_1c_diff']));
+                } elseif ($model->getErrors()) {
+                    var_dump($model->errors);
+                    die();
+                }
             }
         }
-
     }
 
     /**
index e397c15a6998838acf883607e38d88137818b0b1..e18038eb169643cacc79c317f3fbf9c28acba6bf 100644 (file)
@@ -161,7 +161,8 @@ if (in_array($shiftTransfer->status_id, [ShiftTransfer::STATUS_ID_TRANSFER_ACTIO
                 'label' => 'Товар',
                 'value' => function ($model) {
                     return $model->product->name;
-                }
+                },
+                'pageSummary' => 'Итого:',
             ],
             [
                 'attribute' => 'product_price',
@@ -195,14 +196,14 @@ if (in_array($shiftTransfer->status_id, [ShiftTransfer::STATUS_ID_TRANSFER_ACTIO
                 'label' => 'Количество товара',
                 'format' => 'raw',
                 'pageSummary' => true,
-                'footer' => Html::tag('b', $totalBalance < 0 ? 'Недосдача: ' : 'Избыток: ' ) // Выводим итог в футере для данного столбца
+                'footer' => Html::tag('b', $totalBalance < 0 ? 'Недосдача: ' : 'Избыток: ')
             ],
             [
                 'attribute' => 'balance',
                 'label' => 'Разница в сумме',
                 'format' => 'raw',
                 'pageSummary' => true,
-                'footer' => Html::tag('b', number_format($totalBalance, 2, '.', ' ')), // Показываем итог по балансу
+                'footer' => Html::tag('b', number_format($totalBalance, 2, '.', ' ')),
             ],
             [
                 'attribute' => 'balance_self_cost',
@@ -224,7 +225,7 @@ if (in_array($shiftTransfer->status_id, [ShiftTransfer::STATUS_ID_TRANSFER_ACTIO
             ],
         ],
         'showPageSummary' => true,
-        'layout' => '{items}{summary}',
+        'layout' => '{items}',
     ]);
 
     if (