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;
}
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(),
+ ];
+ }
+ }
}