]> gitweb.erp-flowers.ru Git - erp24_rep/yii-erp24/.git/commitdiff
[ERP-331] считывания errors из json ответа
authorAlexander Smirnov <fredeom@mail.ru>
Thu, 20 Mar 2025 14:40:03 +0000 (17:40 +0300)
committerAlexander Smirnov <fredeom@mail.ru>
Thu, 20 Mar 2025 14:40:03 +0000 (17:40 +0300)
erp24/api2/controllers/DataController.php
erp24/migrations/m250320_142705_alter_table_marketplace_orders_add_field_error_text.php [new file with mode: 0755]
erp24/records/MarketplaceOrders.php

index 406efc1b4e837945f71367379bd6964bbb9e1f25..2e1238b19bbc9b8fe07e54f779198ebeea0e2907 100644 (file)
@@ -2230,7 +2230,91 @@ class DataController extends BaseController
 
             if (!empty($result['created_orders'])) {
                 foreach ($result['created_orders'] as $arr) {
-                    MarketplaceOrders::updateAll(['status_1c' => MarketplaceOrders::STATUSES_1C_CREATED_IN_1C], ['guid' => $arr['id']]);
+                    if (!empty($arr["id"])) {
+                        $marketplaceOrders = MarketplaceOrders::find()->where(['guid' => $arr["id"]])->one();
+                    }
+                    if (!empty($marketplaceOrders)) {
+                        /** @var MarketplaceOrders $marketplaceOrders */
+                        if (!empty($arr["errors"]) || !empty($arr["errors_items"])) {
+                            $errorText = '';
+                            if (!empty($arr["errors"])) {
+                                    //"errors": [
+                                    //{
+                                    //"error": "Ошибка преобразования ИД документа",
+                                    //"error_description": "ИД документа 01202307-2705-1039-09bc-100015809667 уже записан в системе",
+                                    //"error_json": [
+                                    //{
+                                    //"field": "id",
+                                    //"error": "Документ с данным ИД уже записан в системе"
+                                    //}
+                                    //
+
+                                foreach ($arr["errors"] as $errorRow) {
+                                    if (!empty($errorRow['error'])) {
+                                        $errorText .= $errorRow['error'];
+
+                                        $errorText .= ' ,' . $errorRow['error_description'];
+                                        if (!empty($errorRow['error_json'])) {
+                                            foreach ($errorRow['error_json'] as $errorJsonRow) {
+                                                $errorText .= ' , поле: ' . $errorJsonRow['field'];
+                                                $errorText .= ' , ошибка: ' . $errorJsonRow['error'];
+                                            }
+                                        }
+                                    }
+
+                                    if (!empty($errorRow["errors_items"])) {
+                                        foreach ($errorRow["errors_items"] as $errorDopRow) {
+                                                //"errors_items": [
+                                                //{
+                                                //"error_dop": "Ошибка проверки количества номенклатуры",
+                                                //"field": "quantity",
+                                                //"product_id": "bdd17587-09d8-11e5-bd74-1c6f659fb563",
+                                                //"error": "У данной позиции запрещён ввод дробного значения!"
+                                                //},
+                                                //
+                                                //
+                                            $productName = $errorDopRow['product_id'];
+
+                                            if (array_key_exists($errorDopRow['product_id'], $productsNames)) {
+                                                $productName = $productsNames[$errorDopRow['product_id']];
+                                            }
+
+                                            $errorText .= ' ' . $errorDopRow['error_dop'];
+                                            $errorText .= ' , поле: ' . $errorDopRow['field'];
+                                            $errorText .= ' , товар: ' . $productName;
+                                            $errorText .= ' , ошибка: ' . $errorDopRow['error'];
+                                        }
+                                    }
+                                }
+                            }
+
+                            $marketplaceOrders->status_1c = WriteOffsErp::STATUS_ERROR_1С;
+                            $marketplaceOrders->error_text = $errorText;
+                            $marketplaceOrders->save();
+                            if ($marketplaceOrders->getErrors()) {
+                                LogService::apiErrorLog(
+                                    json_encode(
+                                        ["error_id" => 41, "error" => $marketplaceOrders->getErrors()],
+                                        JSON_UNESCAPED_UNICODE
+                                    )
+                                );
+                            }
+                        } else {
+                            if (isset($arr["held"]) && $arr["held"]) {
+                                $marketplaceOrders->number_1c = $arr["number"] ?? '';
+                                $marketplaceOrders->status_1c = WriteOffsErp::STATUS_CREATED_1С;
+                                $marketplaceOrders->save();
+                                if ($marketplaceOrders->getErrors()) {
+                                    LogService::apiErrorLog(
+                                        json_encode(
+                                            ["error_id" => 42, "error" => $marketplaceOrders->getErrors()],
+                                            JSON_UNESCAPED_UNICODE
+                                        )
+                                    );
+                                }
+                            }
+                        }
+                    }
                 }
             }
         } catch (Exception $e) {
diff --git a/erp24/migrations/m250320_142705_alter_table_marketplace_orders_add_field_error_text.php b/erp24/migrations/m250320_142705_alter_table_marketplace_orders_add_field_error_text.php
new file mode 100755 (executable)
index 0000000..e36559a
--- /dev/null
@@ -0,0 +1,42 @@
+<?php
+
+use yii\db\Migration;
+
+/**
+ * Class m250320_142705_alter_table_marketplace_orders_add_field_error_text
+ */
+class m250320_142705_alter_table_marketplace_orders_add_field_error_text extends Migration
+{
+    const TABLE_NAME = 'erp24.marketplace_orders';
+    /**
+     * {@inheritdoc}
+     */
+    public function safeUp()
+    {
+        $table = $this->db->schema->getTableSchema(self::TABLE_NAME);
+        if ($table === null) {
+            return;
+        }
+
+        if (!$this->db->schema->getTableSchema(self::TABLE_NAME, true)->getColumn('error_text')) {
+            $this->addColumn(self::TABLE_NAME, 'error_text', $this->text());
+        }
+
+        if (!$this->db->schema->getTableSchema(self::TABLE_NAME, true)->getColumn('number_1c')) {
+            $this->addColumn(self::TABLE_NAME, 'number_1c', $this->string(100)->null()->comment('Название документа в 1с'));
+        }
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    public function safeDown()
+    {
+        if ($this->db->schema->getTableSchema(self::TABLE_NAME, true)->getColumn('error_text')) {
+            $this->dropColumn(self::TABLE_NAME, 'error_text');
+        }
+        if ($this->db->schema->getTableSchema(self::TABLE_NAME, true)->getColumn('number_1c')) {
+            $this->dropColumn(self::TABLE_NAME, 'number_1c');
+        }
+    }
+}
index ac770012b2e95ab971e10301480b4860d0528d2f..5b61e32caee620ab4f6c599b61bf11f03e0a66ed 100644 (file)
@@ -30,6 +30,8 @@ use Yii;
  * @property int|null $status_1c Статус заказа в 1С
  * @property int|null $marketplace_name Наименование маркетплейса 'ФлауВау' 'ЯндексМаркет'
  * @property int|null $marketplace_id ID маркетплейса: 1 - Flowwow, 2 - YandexMarket
+ * @property string $error_text
+ * @property string|null $number_1c Название документа в 1с
  */
 class MarketplaceOrders extends \yii\db\ActiveRecord
 {
@@ -63,9 +65,10 @@ class MarketplaceOrders extends \yii\db\ActiveRecord
             [[ 'fake'], 'default', 'value' => 0],
             [['store_id', 'status_id', 'substatus_id', 'fake', 'cancel_requested', 'status_1c', 'marketplace_id'], 'integer'],
             [['creation_date', 'updated_at', 'returned_at'], 'safe'],
-            [['return_data', 'raw_data', 'marketplace_name'], 'string'],
+            [['return_data', 'raw_data', 'marketplace_name', 'error_text'], 'string'],
             [['total', 'delivery_total', 'buyer_total_before_discount'], 'number'],
             [['marketplace_order_id'], 'string', 'max' => 64],
+            [['number_1c'], 'string', 'max' => 100],
             [['warehouse_guid', 'guid'], 'string', 'max' => 36],
             [['tax_system', 'payment_type', 'payment_method'], 'string', 'max' => 32],
             [['marketplace_order_id'], 'unique'],
@@ -101,6 +104,8 @@ class MarketplaceOrders extends \yii\db\ActiveRecord
             'status_1c' => 'Статус заказа в 1С',
             'marketplace_name' => 'Наименование маркетплейса',
             'marketplace_id' => 'ID маркетплейса: 1 - Flowwow, 2 - YandexMarket',
+            'error_text' => 'Ошибка',
+            'number_1c' => 'Номер документа в 1с',
         ];
     }