]> gitweb.erp-flowers.ru Git - erp24_rep/yii-erp24/.git/commitdiff
ERP-329 Новый интерфейс для работы с заказами мп и амо feature_zozirova_erp-329_all_orders
authormarina <m.zozirova@gmail.com>
Tue, 11 Mar 2025 06:32:45 +0000 (09:32 +0300)
committermarina <m.zozirova@gmail.com>
Tue, 11 Mar 2025 06:32:45 +0000 (09:32 +0300)
erp24/controllers/MarketplaceOrdersController.php
erp24/records/OrdersUnion.php
erp24/views/marketplace-orders/all-orders.php
erp24/web/js/marketplace-orders/marketplace-orders.js [new file with mode: 0644]

index 06d36f02301f10a004e2ab9de0a46421fd97e812..b1a1a8e910bc29f83b28cbd65984abdde4c5fe72 100644 (file)
@@ -6,6 +6,8 @@ use app\records\OrdersUnion;
 use Yii;
 use yii\base\Exception;
 use yii\data\ArrayDataProvider;
+use yii\db\Expression;
+use yii\db\Query;
 use yii_app\records\MarketplaceOrders;
 use yii_app\records\MarketplaceOrdersSearch;
 use yii\web\Controller;
@@ -243,4 +245,41 @@ class MarketplaceOrdersController extends Controller
             'dataProvider' => $data,
         ]);
     }
+
+    public function actionGetBouquetComposition($order_id, $source)
+    {
+        if ($source == 'amo') {
+            $query = (new \yii\db\Query())
+                ->select([
+                    'guid' => new \yii\db\Expression("item->>'guid'"),
+                    'name' => new \yii\db\Expression("item->>'name'"),
+                    'price' => new \yii\db\Expression("(item->>'price')::NUMERIC"),
+                    'quantity' => new \yii\db\Expression("(item->>'quantity')::NUMERIC")
+                ])
+                ->from(['orders_amo'])
+                ->leftJoin(['bouquet' => new \yii\db\Expression('jsonb_array_elements(products_json::jsonb)')], 'TRUE') // первый JSON-массив
+                ->leftJoin(['item' => new \yii\db\Expression('jsonb_array_elements(bouquet->\'items\')')], 'TRUE') // второй JSON-массив в элементе
+                ->where(['id' => $order_id]) // фильтр по id
+                ->all();
+        } else {
+            $query = (new Query())
+                ->select([
+                    new Expression("item->>'guid' AS guid"),
+                    new Expression("item->>'name' AS name"),
+                    new Expression("(item->>'price')::NUMERIC AS price"),
+                    new Expression("(item->>'quantity')::NUMERIC AS quantity")
+                ])
+                ->from(new Expression("
+        marketplace_order_items moi
+        LEFT JOIN products_1c p1c ON moi.offer_id = p1c.articule
+        LEFT JOIN LATERAL jsonb_array_elements(
+            COALESCE(NULLIF(p1c.components, '')::jsonb, '[]'::jsonb)
+        ) AS item ON TRUE
+    "))
+                ->where(['moi.order_id' => $order_id])
+                ->all();
+        }
+
+        return $this->asJson($query);
+    }
 }
index 0bc637e824556baff49cea8bd8126f5dbeaa9d6a..6e16f18369a345a7381f961091425a6158064275 100644 (file)
@@ -36,24 +36,13 @@ class OrdersUnion extends ActiveRecord
                 'price',
                 'delivery_adress as delivery_address',
                 new \yii\db\Expression("
-                COALESCE((
-                    SELECT jsonb_agg(jsonb_build_object(
-                        'name', bouquet->>'name',
-                        'price', bouquet->>'price',
-                        'quantity', bouquet->>'count',
-                        'items', (
-                            SELECT jsonb_agg(jsonb_build_object(
-                                'guid', item->>'guid',
-                                'name', item->>'name',
-                                'price', (item->>'price')::NUMERIC,
-                                'quantity', (item->>'quantity')::NUMERIC
-                            ))
-                            FROM jsonb_array_elements(bouquet->'items') AS item
-                        )
-                    ))
-                    FROM jsonb_array_elements(COALESCE(NULLIF(o.products_json, '')::jsonb, '[]'::jsonb)) AS bouquet
-                ), '[]'::jsonb) AS products_json
-            ")
+            COALESCE((
+                SELECT jsonb_agg(jsonb_build_object(
+                    'name', bouquet->>'name'
+                ))
+                FROM jsonb_array_elements(COALESCE(NULLIF(o.products_json, '')::jsonb, '[]'::jsonb)) AS bouquet
+            ), '[]'::jsonb) AS products_json
+        ")
             ])
             ->from('orders_amo o');
 
@@ -61,11 +50,11 @@ class OrdersUnion extends ActiveRecord
         $queryMarketplace = (new Query())
             ->select([
                 new \yii\db\Expression("
-                CASE 
-                    WHEN ms.warehouse_id = 2 THEN 'yandex'
-                    WHEN ms.warehouse_id = 1 THEN 'flowwow'
-                    ELSE 'Marketplace' 
-                END AS source"),
+            CASE 
+                WHEN ms.warehouse_id = 2 THEN 'yandex'
+                WHEN ms.warehouse_id = 1 THEN 'flowwow'
+                ELSE 'Marketplace' 
+            END AS source"),
                 'mo.id',
                 'mod.delivery_end AS delivery_date',
                 'mo.status_id',
@@ -74,39 +63,24 @@ class OrdersUnion extends ActiveRecord
                 'mo.total',
                 new \yii\db\Expression("NULLIF(concat(country, ' ', city, ' ', street, ' ', house, ' ', apartment), '')::TEXT AS delivery_address"),
                 new \yii\db\Expression("(
-                SELECT jsonb_agg(jsonb_build_object(
-                    'name', bouquet.name,
-                    'price', bouquet.price,
-                    'quantity', bouquet.quantity,
-                    'items', COALESCE(items_list, '[]'::jsonb)
-                ))
-                FROM (
-                    SELECT 
-                        p1c.name,
-                        pd.price,
-                        SUM(items.value::INTEGER) AS quantity,
-                        jsonb_agg(jsonb_build_object(
-                            'guid', p2c.id::TEXT,
-                            'name', p2c.name,
-                            'price', pd.price,
-                            'quantity', items.value::INTEGER
-                        )) FILTER (WHERE p2c.id IS NOT NULL) AS items_list
-                    FROM erp24.products_1c p1c
-                    JOIN marketplace_order_items moi ON p1c.articule = moi.offer_id
-                    LEFT JOIN LATERAL jsonb_each_text(
-                        COALESCE(NULLIF(p1c.components, '')::jsonb, '{}'::jsonb)
-                    ) AS items ON true
-                    LEFT JOIN erp24.products_1c p2c ON p2c.id::TEXT = items.key
-                    LEFT JOIN prices_dynamic pd ON p2c.id = pd.product_id
-                    WHERE moi.order_id = mo.id
-                    GROUP BY p1c.name, pd.price
-                ) AS bouquet
-            ) AS products_json")
+            SELECT jsonb_agg(jsonb_build_object(
+                'name', bouquet.name
+            ))
+            FROM (
+                SELECT 
+                    p1c.name
+                FROM erp24.products_1c p1c
+                JOIN marketplace_order_items moi ON p1c.articule = moi.offer_id
+                WHERE moi.order_id = mo.id
+                GROUP BY p1c.name
+            ) AS bouquet
+        ) AS products_json")
             ])
             ->from(['mo' => 'marketplace_orders'])
             ->leftJoin(['mod' => 'marketplace_order_delivery'], 'mo.id = mod.order_id')
             ->leftJoin(['ms' => 'marketplace_store'], 'ms.warehouse_guid::TEXT = mo.warehouse_guid::TEXT');
 
+
         $query = (new Query())
             ->andFilterWhere(['source' => $params['source'] ?? null])
             ->andFilterWhere(['store_id' => $params['store_id'] ?? null])
index 9a4415c2a88e0ace986268655196173305ec7b7e..8d3a342c0b678a3b14daafddffb2788eb7539bd1 100644 (file)
@@ -12,6 +12,9 @@ use yii_app\records\MarketplaceStatus;
 use yii_app\records\OrdersAmo;
 
 $this->title = 'Все заказы';
+
+$this->registerJsFile('/js/marketplace-orders/marketplace-orders.js', ['position' => \yii\web\View::POS_END]);
+?>
 ?>
 <div class="all-orders p-5">
     <h1><?= Html::encode($this->title) ?></h1>
@@ -119,7 +122,7 @@ $this->title = 'Все заказы';
             ],
             [
                 'attribute' => 'products_json',
-                'label' => 'СоÑ\81Ñ\82ав',
+                'label' => 'Ð\91Ñ\83кеÑ\82Ñ\8b',
                 'format' => 'raw',
                 'value' => function ($model) {
                     $products = json_decode($model['products_json'], true);
@@ -128,18 +131,28 @@ $this->title = 'Все заказы';
                         return '<i>(Нет данных)</i>';
                     }
 
-                    $output = '';
+                    $output = '<ul class="bouquet-list">';
                     foreach ($products as $product) {
-                        $output .= "<h4>{$product['name']}</h4>";
-                        $output .= '<table class="table table-bordered"><tr><th>Название</th><th>Кол-во</th><th>Цена</th></tr>';
-                        foreach ($product['items'] as $item) {
-                            $output .= "<tr><td>{$item['name']}</td><td>{$item['quantity']}</td><td>{$item['price']} ₽</td></tr>";
-                        }
-                        $output .= '</table>';
+                        $bouquetName = htmlspecialchars($product['name']);
+                        $id = md5($bouquetName . $model['id']); // Уникальный идентификатор для контейнера
+                        $dataId = $model['id']; // Допустим, вы хотите использовать 'guid' как data-id
+                        $dataSource = $model['source']; // Это пример поля, которое можно использовать для data-source. Убедитесь, что оно существует в модели.
+
+                        $output .= "<li>
+                <span>{$bouquetName}</span>
+                <button class='btn btn-link toggle-composition' data-bouquet='{$bouquetName}' 
+                    data-order='{$model['id']}'
+                    data-id='{$dataId}' data-source='{$dataSource}'>
+                    &#9660;
+                </button>
+                <div id='composition-{$id}' class='composition-container' style='display: none;'></div>
+            </li>";
                     }
+                    $output .= '</ul>';
                     return $output;
                 }
             ],
+
         ],
     ]); ?>
 </div>
diff --git a/erp24/web/js/marketplace-orders/marketplace-orders.js b/erp24/web/js/marketplace-orders/marketplace-orders.js
new file mode 100644 (file)
index 0000000..cb371bc
--- /dev/null
@@ -0,0 +1,79 @@
+$(".toggle-composition").on("click", function () {
+    var button = $(this);
+    var orderId = button.data("id"); // Получаем ID заказа
+    var source = button.data("source"); // Получаем источник данных
+
+    // Если уже открыто — скрываем
+    if (button.next(".composition-content").length) {
+        button.next(".composition-content").slideUp(300, function () {
+            $(this).remove();
+            button.html("&#9660;"); // Стрелка вниз
+        });
+        return;
+    }
+
+    $(".composition-content").remove();
+
+    $.ajax({
+        url: "/marketplace-orders/get-bouquet-composition",
+        type: "GET",
+        data: {
+            order_id: orderId,
+            source: source
+        },
+        beforeSend: function () {
+            button.html("&#8987;"); // Значок загрузки
+        },
+        success: function (response) {
+            // Проверяем, есть ли данные
+            if (!response || response.length === 0 || response.every(item => !item.guid)) {
+                button.after("<div class='composition-content text-muted' style='display: none;'>Нет данных</div>");
+                button.next(".composition-content").slideDown();
+                button.html("&#9660;"); // Стрелка вниз
+                return;
+            }
+
+            // Создаем HTML-таблицу
+            var tableHtml = `
+                <div class="composition-content" style="display: none; margin-top: 5px;">
+                    <table class="table table-bordered mt-2">
+                        <thead>
+                            <tr>
+                                <th>Название</th>
+                                <th>Цена</th>
+                                <th>Кол-во</th>
+                                <th>Сумма</th>
+                            </tr>
+                        </thead>
+                        <tbody>
+            `;
+
+            response.forEach(function (item) {
+                tableHtml += `
+                    <tr>
+                        <td>${item.name || '-'}</td>
+                        <td>${item.price ? parseFloat(item.price).toFixed(2) + ' ₽' : '-'}</td>
+                        <td>${item.quantity ? parseFloat(item.quantity) : '-'}</td>
+                        <td>${(item.price && item.quantity) ? (parseFloat(item.price) * parseFloat(item.quantity)).toFixed(2) + ' ₽' : '-'}</td>
+                    </tr>
+                `;
+            });
+
+            tableHtml += `
+                        </tbody>
+                    </table>
+                </div>
+            `;
+
+            // Вставляем таблицу после кнопки
+            button.after(tableHtml);
+            button.next(".composition-content").slideDown();
+            button.html("&#9650;"); // Стрелка вверх
+        },
+        error: function () {
+            button.after("<div class='composition-content text-danger' style='display: none;'>Ошибка загрузки данных.</div>");
+            button.next(".composition-content").slideDown();
+            button.html("&#9660;"); // Стрелка вниз
+        }
+    });
+});