]> gitweb.erp-flowers.ru Git - erp24_rep/yii-erp24/.git/commitdiff
ERP-329 Новый интерфейс для работы с заказами мп и амо
authormarina <m.zozirova@gmail.com>
Tue, 4 Mar 2025 12:21:13 +0000 (15:21 +0300)
committermarina <m.zozirova@gmail.com>
Tue, 4 Mar 2025 12:21:13 +0000 (15:21 +0300)
erp24/records/OrdersUnion.php
erp24/views/marketplace-orders/all-orders.php

index 7bea583685f6db3b9090c42b71e2fcfd34c54ae4..b11871275788a6a3caf31891d165fce96062cdbf 100644 (file)
@@ -4,6 +4,7 @@ namespace app\records;
 
 use yii\db\ActiveRecord;
 use yii\db\Query;
+
 class OrdersUnion extends ActiveRecord
 {
     public $delivery_date;
@@ -27,9 +28,29 @@ class OrdersUnion extends ActiveRecord
                 new \yii\db\Expression('NULLIF(store_id, \'\')::INTEGER AS store_id'),
                 'payment_type_id',
                 'price',
-                'delivery_adress as delivery_address'
+                '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
+        ")
             ])
-            ->from('orders_amo');
+            ->from('orders_amo o');
+
 
         $queryMarketplace = (new Query())
             ->select([
@@ -38,18 +59,50 @@ class OrdersUnion extends ActiveRecord
                 'mod.delivery_end AS delivery_date',
                 'mo.status_id',
                 'mo.store_id',
-                'payment_method',
-                'total',
+                'mo.payment_method',
+                '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, -- Берём сумму всех количеств из components
+                    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")
             ])
             ->from(['mo' => 'marketplace_orders'])
             ->leftJoin(['mod' => 'marketplace_order_delivery'], 'mo.id = mod.order_id');
 
+
         return (new Query())
+            ->limit(20)
+            ->where(['!=', 'source', 'amo'])
             ->from(['orders' => $queryAmo->union($queryMarketplace, true)])
             ->all();
     }
-    
+
     public function attributes()
     {
         return array_merge(parent::attributes(), ['delivery_date']);
index 937fb1612186d4bbf59085a03fc2564c840d9469..32b9396b2f7ad3476ca79a56eec3e401a9f4c3e4 100644 (file)
@@ -43,5 +43,48 @@ $this->title = 'Все заказы';
             'attribute' => 'delivery_address',
             'label' => 'Адрес доставки',
         ],
+        [
+            'attribute' => 'products_json',
+            'label' => 'Состав',
+            'format' => 'raw',
+            'value' => function ($model) {
+                $products = \yii\helpers\Json::decode($model['products_json']); // Декодируем JSON
+                if (empty($products) || !is_array($products)) {
+                    return '<i>(Нет данных)</i>';
+                }
+
+                $output = '';
+
+                foreach ($products as $product) {
+                    $name = htmlspecialchars($product['name'] ?? 'Без названия');
+                    $output .= "<h4 style='margin-bottom: 5px;'>{$name}</h4>"; // Название букета заголовком
+
+                    $output .= '<table border="1" cellpadding="5" cellspacing="0" style="border-collapse: collapse; width: 100%; margin-bottom: 15px;">';
+                    $output .= '<tr>
+                            <th style="text-align: left;">Название</th>
+                            <th style="text-align: center;">Кол-во</th>
+                            <th style="text-align: center;">Цена</th>
+                        </tr>';
+
+                    if (!empty($product['items']) && is_array($product['items'])) {
+                        foreach ($product['items'] as $item) {
+                            $itemName = htmlspecialchars($item['name'] ?? 'Без названия');
+                            $itemQuantity = htmlspecialchars($item['quantity'] ?? '0');
+                            $itemPrice = htmlspecialchars($item['price'] ?? '0');
+
+                            $output .= "<tr>
+                                    <td>{$itemName}</td>
+                                    <td style='text-align: center;'>{$itemQuantity}</td>
+                                    <td style='text-align: center;'>{$itemPrice} ₽</td>
+                                </tr>";
+                        }
+                    }
+
+                    $output .= '</table>';
+                }
+
+                return $output;
+            }
+        ],
     ],
 ]); ?>