]> gitweb.erp-flowers.ru Git - erp24_rep/yii-erp24/.git/commitdiff
[ERP-121] Вынесение items из накладных в отдельную таблицу incoming_items
authorAlexander Smirnov <aleksandr.smirnov@erp-flowers.ru>
Wed, 31 Jul 2024 07:17:40 +0000 (07:17 +0000)
committerMarina Zozirova <marina.zozirova@erp-flowers.ru>
Wed, 31 Jul 2024 07:17:40 +0000 (07:17 +0000)
erp24/api2/controllers/DataController.php
erp24/controllers/IncomingController.php [new file with mode: 0644]
erp24/migrations/m240729_081512_create_table_incoming_item.php [new file with mode: 0755]
erp24/records/IncomingItems.php [new file with mode: 0644]
erp24/views/incoming/index.php [new file with mode: 0644]

index ff274b4acf53349a2f92b78485c885d550c84592..2d56dd3fd1c08746386a05e094254667264f428d 100644 (file)
@@ -20,6 +20,7 @@ use yii_app\records\CreateChecksBags;
 use yii_app\records\EmployeeOnShift;
 use yii_app\records\ExportImportTable;
 use yii_app\records\Incoming;
+use yii_app\records\IncomingItems;
 use yii_app\records\OrdersAmo;
 use yii_app\records\PaymentTypes;
 use yii_app\records\Prices;
@@ -725,21 +726,46 @@ class DataController extends BaseController {
 
             if (!empty($result['incomings'])) {
                 foreach ($result["incomings"] as $gi => $arr) {
-                    $incoming = new Incoming;
-                    $incoming->id = $arr["id"];
-                    $incoming->store_id = $arr["store_id"];
-                    $incoming->counteragent_id = $arr["counteragent_id"];
-                    $incoming->number = $arr["number"];
-                    $incoming->date = $arr["date"];
-                    $incoming->comment = $arr["comment"] ?? '';
-                    $incoming->items = empty($arr["items"]) ? '[]' : json_encode($arr["items"],JSON_UNESCAPED_UNICODE);
-                    $incoming->is_discrepancies = (int) $arr["is_discrepancies"];
-                    $incoming->supplier_items = empty($arr["supplier_items"]) ? '[]' : json_encode($arr["supplier_items"],JSON_UNESCAPED_UNICODE);
-                    $incoming->payments = empty($arr["payments"]) ? '[]' : json_encode($arr["payments"],JSON_UNESCAPED_UNICODE);
-                    $incoming->summ = $arr["summ"];
-                    $incoming->save();
+                    $incoming = Incoming::find()->where(['id' => $arr['id']])->one();
+                    if (!$incoming) {
+                        $incoming = new Incoming;
+                        $incoming->id = $arr["id"];
+                        $incoming->store_id = $arr["store_id"];
+                        $incoming->counteragent_id = $arr["counteragent_id"];
+                        $incoming->number = $arr["number"];
+                        $incoming->date = $arr["date"];
+                        $incoming->comment = $arr["comment"] ?? '';
+                        $incoming->items = empty($arr["items"]) ? '[]' : json_encode($arr["items"], JSON_UNESCAPED_UNICODE);
+                        $incoming->is_discrepancies = (int)$arr["is_discrepancies"];
+                        $incoming->supplier_items = empty($arr["supplier_items"]) ? '[]' : json_encode($arr["supplier_items"], JSON_UNESCAPED_UNICODE);
+                        $incoming->payments = empty($arr["payments"]) ? '[]' : json_encode($arr["payments"], JSON_UNESCAPED_UNICODE);
+                        $incoming->summ = $arr["summ"];
+                        $incoming->save();
+                    }
                     if ($incoming->getErrors()) {
                         LogService::apiErrorLog(json_encode(["error_id" => 15, "error" => $incoming->getErrors()], JSON_UNESCAPED_UNICODE));
+                    } else {
+                        try {
+                            $json = Json::decode($incoming->items);
+                            IncomingItems::deleteAll(['incoming_id' => $incoming->id]);
+                            foreach ($json as $item) {
+                                $incomingItem = new IncomingItems;
+                                $incomingItem->incoming_id = $incoming->id;
+                                $incomingItem->product_id = $item['product_id'];
+                                $incomingItem->color = $item['color'];
+                                $incomingItem->quantity = $item['quantity'];
+                                $incomingItem->price = $item['price'];
+                                $incomingItem->vat_rate = $item['vat_rate'];
+                                $incomingItem->vat_amount = $item['vat_amount'];
+                                $incomingItem->summ = $item['summ'];
+                                $incomingItem->save();
+                                if ($incomingItem->getErrors()) {
+                                    LogService::apiErrorLog(json_encode(["error_id" => 15.2, "error" => $incomingItem->getErrors()], JSON_UNESCAPED_UNICODE));
+                                }
+                            }
+                        } catch (Exception $jex) {
+                            LogService::apiErrorLog(json_encode(["error_id" => 15.3, "error" => "items has no json format"], JSON_UNESCAPED_UNICODE));
+                        }
                     }
                 }
             }
diff --git a/erp24/controllers/IncomingController.php b/erp24/controllers/IncomingController.php
new file mode 100644 (file)
index 0000000..86b83c2
--- /dev/null
@@ -0,0 +1,55 @@
+<?php
+
+namespace app\controllers;
+
+use Yii;
+use yii\base\DynamicModel;
+use yii\helpers\Json;
+use yii\web\Controller;
+use yii_app\records\Incoming;
+use yii_app\records\IncomingItems;
+
+class IncomingController extends Controller
+{
+    public function actionIndex() {
+
+        $model = DynamicModel::validateData(
+            ['date_start' => date('Y-m-d H:i:s', strtotime('-3 month', time())), 'date_end' => date('Y-m-d H:i:s')],
+            [
+                [['date_start', 'date_end'], 'safe']
+            ]
+        );
+
+        if (Yii::$app->request->isPost && $model->load(Yii::$app->request->post())) {
+            // загрузка и на индекс
+            $incomings = Incoming::find()->where(['between', 'date', $model->date_start, $model->date_end])->all();
+            foreach ($incomings as $incoming) {
+                /** @var $incoming Incoming */
+                try {
+                    $json = Json::decode($incoming->items);
+                    IncomingItems::deleteAll(['incoming_id' => $incoming->id]);
+                    foreach ($json as $item) {
+                        $incomingItem = new IncomingItems;
+                        $incomingItem->incoming_id = $incoming->id;
+                        $incomingItem->product_id = $item['product_id'];
+                        $incomingItem->color = $item['color'];
+                        $incomingItem->quantity = $item['quantity'];
+                        $incomingItem->price = $item['price'];
+                        $incomingItem->vat_rate = $item['vat_rate'];
+                        $incomingItem->vat_amount = $item['vat_amount'];
+                        $incomingItem->summ = $item['summ'];
+                        $incomingItem->save();
+                        if ($incomingItem->getErrors()) {
+                            var_dump($incomingItem->getErrors());
+                        }
+                    }
+                } catch (\Exception $jex) {}
+            }
+        }
+
+        $incomingCount = Incoming::find()->count();
+        $incomingTransferedCount = IncomingItems::find()->select(['MAX(id)'])->groupBy('incoming_id')->count();
+
+        return $this->render('index', compact('incomingCount', 'incomingTransferedCount', 'model'));
+    }
+}
\ No newline at end of file
diff --git a/erp24/migrations/m240729_081512_create_table_incoming_item.php b/erp24/migrations/m240729_081512_create_table_incoming_item.php
new file mode 100755 (executable)
index 0000000..8f18ace
--- /dev/null
@@ -0,0 +1,37 @@
+<?php
+
+use yii\db\Migration;
+
+/**
+ * Class m240729_081512_create_table_incoming_item
+ */
+class m240729_081512_create_table_incoming_item extends Migration
+{
+    const TABLE_NAME = 'erp24.incoming_items';
+
+    /**
+     * {@inheritdoc}
+     */
+    public function safeUp()
+    {
+        $this->createTable(self::TABLE_NAME, [
+            'id' => $this->primaryKey(),
+            'incoming_id' => $this->string(36)->notNull()->comment('ID из таблицы incoming'),
+            'product_id' => $this->string(36)->notNull()->comment('GUID продукта'),
+            'color' => $this->string(36)->null()->comment('Цвет продукта'),
+            'quantity' => $this->integer()->notNull()->comment('Количество элементов продукта'),
+            'price' => $this->float()->notNull()->comment('Цена единицы продукта'),
+            'vat_rate' => $this->string(36)->notNull()->comment('строка описывающая наличие НДС'),
+            'vat_amount' => $this->float()->null()->comment('Величина НДС'),
+            'summ' => $this->float()->notNull()->comment('обычно, это - quantity * price')
+        ]);
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    public function safeDown()
+    {
+        $this->dropTable(self::TABLE_NAME);
+    }
+}
diff --git a/erp24/records/IncomingItems.php b/erp24/records/IncomingItems.php
new file mode 100644 (file)
index 0000000..38b5c4f
--- /dev/null
@@ -0,0 +1,61 @@
+<?php
+
+namespace yii_app\records;
+
+use Yii;
+
+/**
+ * This is the model class for table "incoming_items".
+ *
+ * @property int $id
+ * @property string $incoming_id ID из таблицы incoming
+ * @property string $product_id GUID продукта
+ * @property string|null $color Цвет продукта
+ * @property int $quantity Количество элементов продукта
+ * @property float $price Цена единицы продукта
+ * @property string $vat_rate строка описывающая наличие НДС
+ * @property float|null $vat_amount Величина НДС
+ * @property float $summ обычно, это - quantity * price
+ */
+class IncomingItems extends \yii\db\ActiveRecord
+{
+    /**
+     * {@inheritdoc}
+     */
+    public static function tableName()
+    {
+        return 'incoming_items';
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    public function rules()
+    {
+        return [
+            [['incoming_id', 'product_id', 'quantity', 'price', 'vat_rate', 'summ'], 'required'],
+            [['quantity'], 'default', 'value' => null],
+            [['quantity'], 'integer'],
+            [['price', 'vat_amount', 'summ'], 'number'],
+            [['incoming_id', 'product_id', 'color', 'vat_rate'], 'string', 'max' => 36],
+        ];
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    public function attributeLabels()
+    {
+        return [
+            'id' => 'ID',
+            'incoming_id' => 'Incoming ID',
+            'product_id' => 'Product ID',
+            'color' => 'Color',
+            'quantity' => 'Quantity',
+            'price' => 'Price',
+            'vat_rate' => 'Vat Rate',
+            'vat_amount' => 'Vat Amount',
+            'summ' => 'Summ',
+        ];
+    }
+}
diff --git a/erp24/views/incoming/index.php b/erp24/views/incoming/index.php
new file mode 100644 (file)
index 0000000..c4c7b4c
--- /dev/null
@@ -0,0 +1,47 @@
+<?php
+
+use \dosamigos\datetimepicker\DateTimePicker;
+use \yii\widgets\ActiveForm;
+use \yii\base\DynamicModel;
+
+/** @var $incomingCount int */
+/** @var $incomingTransferedCount int */
+/** @var $model DynamicModel */
+
+
+?>
+
+<div class="incomingIndex m-5">
+
+    Количество накладных: <?= $incomingCount ?> Количество переведённых: <?= $incomingTransferedCount ?>
+
+    <?php $form = ActiveForm::begin(); ?>
+
+    Время трансформации от: <?= $form->field($model, 'date_start')->widget(DateTimePicker::class, [
+        'language' => 'ru',
+        'template' => '{input}',
+        'clientOptions' => [
+            'autoclose' => true,
+            'format' => 'Y-m-d H:i:s',
+            'todayBtn' => true
+        ],
+    ])->label(false) ?>
+
+    до: <?= $form->field($model, 'date_end')->widget(DateTimePicker::class, [
+        'language' => 'ru',
+        'template' => '{input}',
+        'clientOptions' => [
+            'autoclose' => true,
+            'format' => 'Y-m-d H:i:s',
+            'todayBtn' => true
+        ],
+    ])->label(false) ?>
+
+    <div class="form-group">
+        <?= \yii\helpers\Html::submitButton('Перевести', ['class' => 'btn btn-success btn-sm']); ?>
+    </div>
+
+    <?php ActiveForm::end(); ?>
+
+
+</div>