From: fomichev Date: Wed, 16 Jul 2025 12:23:15 +0000 (+0300) Subject: Правки и добавление метода обработки статусов X-Git-Url: https://gitweb.erp-flowers.ru/?a=commitdiff_plain;h=80238baabfb037acf08cdbcd562e1da977b67553;p=erp24_rep%2Fyii-erp24%2F.git Правки и добавление метода обработки статусов --- diff --git a/erp24/api2/controllers/DataController.php b/erp24/api2/controllers/DataController.php index d5f32570..65836751 100644 --- a/erp24/api2/controllers/DataController.php +++ b/erp24/api2/controllers/DataController.php @@ -58,6 +58,7 @@ use yii_app\records\WriteOffsProducts; use yii_app\records\WriteOffsProductsErp; use yii_app\services\InfoLogService; use yii_app\services\LogService; +use yii_app\services\MarketplaceService; use yii_app\services\TelegramService; class DataController extends BaseController @@ -2298,15 +2299,9 @@ class DataController extends BaseController if (!empty($result['marketplace_orders'])) { foreach ($result["marketplace_orders"] as $mpOrder) { - $marketplaceOrder = MarketplaceOrders::findOne(['guid' => $mpOrder['id']]); - if ($marketplaceOrder) { - $marketplaceOrder->seller_id = $mpOrder['seller_id'] ?? null; - $marketplaceOrder->number_1c = $mpOrder['number'] ?? null; - $marketplaceOrder->status_processing_1c = $mpOrder['status'] ?? null; - $marketplaceOrder->save(); - if ($marketplaceOrder->getErrors()) { - LogService::apiErrorLog(json_encode(["error_id" => 40, "error" => $marketplaceOrder->getErrors()], JSON_UNESCAPED_UNICODE)); - } + $updateResult = $this->changeMarketplaceOrderStatusFrom1C($mpOrder); + if ($updateResult !== true) { + LogService::apiErrorLog(json_encode(["error_id" => 40, "error" => $updateResult], JSON_UNESCAPED_UNICODE)); } } } @@ -2872,4 +2867,84 @@ class DataController extends BaseController } return $data; } + + public function changeMarketplaceOrderStatusFrom1C($mpOrder) + { + $result = 'Заказ не найден'; + $marketplaceOrder = MarketplaceOrders::findOne(['guid' => $mpOrder['id']]); + if ($marketplaceOrder) { + $status = $mpOrder['status'] ?? null; + + /** @var $marketplaceOrder MarketplaceOrders */ + $debug = false; + if ($marketplaceOrder->fake == 1) { + $debug = true; + } + + // статусы маркетплейса + $statuses = MarketplaceOrderStatusTypes::find() + ->select(['id', 'code']) + ->indexBy('code') + ->asArray() + ->all(); + $statuses = ArrayHelper::map($statuses, 'code', 'id'); + $statusCodes = array_unique(array_keys($statuses)); + + $marketplaceId = $marketplaceOrder->marketplace_id; + + $statusId1C = MarketplaceOrder1cStatuses::find() + ->where(['marketplace_id' => $marketplaceId]) + ->andWhere(['status_id' => $status]) + ->one(); + if (empty($statusId1C)) { + $mess["result"] = 'error'; + $mess["message_order"] = "Статус не найден"; + $errorJson = json_encode($mess, JSON_UNESCAPED_UNICODE); + LogService::apiLogs(1, $errorJson); + Yii::error('Ошибка: такого статуса не существует' . $status, __METHOD__); + return $errorJson; + } + + // Получаем статусы МП по статусу 1С + $orderStatus1CToCodes = MarketplaceOrders::getStatusCodes($statusId1C->status); + + $statusId = MarketplaceService::getOrCreateStatus($orderStatus1CToCodes[0], $statuses, $statusCodes); + $substatusId = MarketplaceService::getOrCreateStatus($orderStatus1CToCodes[1], $statuses, $statusCodes); + + if ( + in_array($statusId1C->status, ['Отказ', 'Собрано']) && + $marketplaceOrder->marketplace_id == 2 + ) { + if (!$debug) { + $updateResult = MarketplaceService::updateOrderStatus( + $marketplaceOrder->warehouse_guid, + $marketplaceOrder->marketplace_order_id, + $orderStatus1CToCodes[0], + $orderStatus1CToCodes[1] + ); + } + + } else { + if (!empty($orderStatus1CToCodes)) { + $marketplaceOrder->status_id = $statusId; + $marketplaceOrder->substatus_id = $substatusId; + } + } + $marketplaceOrder->status_processing_1c = $statusId1C->id; + $marketplaceOrder->seller_id = $mpOrder['seller_id'] ?? null; + $marketplaceOrder->number_1c = $mpOrder['number'] ?? null; + if ($marketplaceOrder->save()) { + // Обновляем историю статусов + MarketplaceService::createOrUpdateStatusHistory($marketplaceOrder->id, $statusId, $substatusId, [ + 'date' => date('Y-m-d H:i:s') + ]); + $result = true; + } else { + Yii::error('Ошибка сохранения: ' . Json::encode($marketplaceOrder->getErrors(), JSON_UNESCAPED_UNICODE)); + $result = $marketplaceOrder->getErrors(); + } + } + + return $result; + } } diff --git a/erp24/api2/controllers/OrdersController.php b/erp24/api2/controllers/OrdersController.php index 6e27431c..cf566048 100644 --- a/erp24/api2/controllers/OrdersController.php +++ b/erp24/api2/controllers/OrdersController.php @@ -20,7 +20,7 @@ class OrdersController extends BaseController { private static function getPath() { - return dirname(__DIR__, 1); + return dirname(__DIR__, 1) . '/json'; } public function actionChangeStatus() @@ -32,6 +32,7 @@ class OrdersController extends BaseController try { $request = Yii::$app->request->getRawBody(); + LogService::apiDataLogs(1, $request, time()); $data = Json::decode($request, true); } catch (\Exception $ex) { return $this->asJson(['error' => ['code' => 400, 'message' => 'Json body invalid']]); @@ -89,7 +90,7 @@ class OrdersController extends BaseController "result" => 'error', "message" => "Заказ не найден" ]; - LogService::apiLogs(1, Json::encode($result, JSON_UNESCAPED_UNICODE)); + LogService::apiDataLogs(1, Json::encode($result, JSON_UNESCAPED_UNICODE)); $mess[] = $result; continue; } diff --git a/erp24/services/LogService.php b/erp24/services/LogService.php index 1e1d8d0d..8a589abc 100644 --- a/erp24/services/LogService.php +++ b/erp24/services/LogService.php @@ -23,7 +23,9 @@ class LogService $apiLogs->seller_id = ''; $apiLogs->phone = 0; $apiLogs->ip = Yii::$app->request->remoteIP; - $apiLogs->save(); + if (!$apiLogs->save()) { + Yii::error('Ошибка сохранения логов: ' . json_encode($apiLogs->getErrors(), JSON_UNESCAPED_UNICODE)); + } } public static function apiLogs($status, $json, $requestId = ''): void { @@ -43,7 +45,9 @@ class LogService $apiLogs->seller_id = Yii::$app->request->post('seller_id') ?? 'placeholder'; $apiLogs->phone = ClientHelper::phoneClear(Yii::$app->request->post('phone') ?? 0); $apiLogs->ip = Yii::$app->request->remoteIP; - $apiLogs->save(); + if (!$apiLogs->save()) { + Yii::error('Ошибка сохранения логов: ' . json_encode($apiLogs->getErrors(), JSON_UNESCAPED_UNICODE)); + } } } @@ -60,8 +64,9 @@ class LogService $apiErrorLog->hash_input = $hash_input; $apiErrorLog->payload = $jsonString; $apiErrorLog->ip = Yii::$app->request->remoteIP ?? ''; - $apiErrorLog->save(); - + if (!$apiErrorLog->save()) { + Yii::error('Ошибка сохранения логов: ' . json_encode($apiErrorLog->getErrors(), JSON_UNESCAPED_UNICODE)); + } /* if (!self::shouldSendToTelegram($hash_input, $jsonString)) { return; }*/