use yii\db\ActiveRecord;
use yii\db\Query;
+
class OrdersUnion extends ActiveRecord
{
public $delivery_date;
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([
'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']);
'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;
+ }
+ ],
],
]); ?>