]> gitweb.erp-flowers.ru Git - erp24_rep/yii-erp24/.git/commitdiff
ERP-423 Api для отправки в 1с заказов
authormarina <m.zozirova@gmail.com>
Wed, 4 Jun 2025 06:10:28 +0000 (09:10 +0300)
committermarina <m.zozirova@gmail.com>
Wed, 4 Jun 2025 06:10:28 +0000 (09:10 +0300)
erp24/api2/controllers/OrdersController.php

index d96e3d84834c189dd93d7a799f4e7be9a4b1cc26..ee41a8948150d2c60e25a36bc0df50abf7990198 100644 (file)
@@ -8,9 +8,11 @@ use Yii;
 use yii\helpers\ArrayHelper;
 use yii\helpers\Json;
 use yii\web\Response;
+use yii_app\records\CityStore;
 use yii_app\records\MarketplaceOrder1cStatuses;
 use yii_app\records\MarketplaceOrders;
 use yii_app\records\MarketplaceOrderStatusTypes;
+use yii_app\records\Products1c;
 use yii_app\services\LogService;
 use yii_app\services\MarketplaceService;
 
@@ -180,4 +182,69 @@ class OrdersController extends BaseController
         }
         return $this->asJson($mess);
     }
+
+    public function actionGetOrders()
+    {
+        Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
+
+        try {
+            $rawBody = Yii::$app->request->getRawBody();
+            if (empty($rawBody)) {
+                throw new \Exception('Пустое тело запроса');
+            }
+
+            $data = json_decode($rawBody, true);
+            if (json_last_error() !== JSON_ERROR_NONE) {
+                throw new \Exception('Некорректный JSON');
+            }
+
+            $storesGuidId = array_flip(array_filter(CityStore::getAllActiveGuidId(), fn($v) => is_int($v) || is_string($v)));
+            $storeGuid = $data['store-guid'] ?? null;
+            if (empty($storeGuid)) {
+                throw new \Exception('store-guid не передан или пуст');
+            }
+
+            $storeId = $storesGuidId[$storeGuid];
+            $currentTime = new \DateTime();
+            $startTime = (clone $currentTime)->modify('-24 hours');
+
+            $orders = MarketplaceOrders::find()
+                ->with('items')
+                ->where(['store_id' => $storeId])
+                ->andWhere(['>=', 'creation_date', $startTime->format('Y-m-d H:i:s')])
+                ->andWhere(['<=', 'creation_date', $currentTime->format('Y-m-d H:i:s')])
+                ->orderBy(['id' => SORT_DESC])
+                ->all();
+
+            $result = [];
+            foreach ($orders as $order) {
+                $orderProducts = [];
+
+                foreach ($order->items as $product) {
+                    $product1c = Products1c::findOne(['articule' => $product->shop_sku])->id ?? null;
+                    $orderProducts[] = [
+                        $product1c => $product->count
+                    ];
+                }
+
+                $result[] = [
+                    'order_id' => $order->guid,
+                    'status' => $order->status_id,
+                    'products' => $orderProducts,
+                ];
+            }
+
+            return [
+                'success' => true,
+                'result' => $result,
+            ];
+
+        } catch (\Exception $e) {
+            Yii::error($e->getMessage(), 'api');
+            return [
+                'success' => false,
+                'error' => $e->getMessage(),
+            ];
+        }
+    }
 }