return ['id'];
}
- public static function getOrders()
+ public static function getOrders($params = [])
{
$queryAmo = (new Query())
->select([
'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',
+ '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 o');
-
$queryMarketplace = (new Query())
->select([
new \yii\db\Expression("'marketplace' AS source"),
'mod.delivery_end AS delivery_date',
'mo.status_id',
'mo.store_id',
- 'mo.payment_method',
- 'mo.total',
+ 'mo.payment_method AS payment_type_id',
+ 'mo.total AS price',
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")
+ 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")
])
->from(['mo' => 'marketplace_orders'])
->leftJoin(['mod' => 'marketplace_order_delivery'], 'mo.id = mod.order_id');
+ $query = (new Query())
+ ->from(['orders' => $queryAmo->union($queryMarketplace, true)]);
+
+ // Фильтрация данных
+ if (!empty($params['source'])) {
+ $query->andWhere(['orders.source' => $params['source']]);
+ }
+
+ if (!empty($params['delivery_date'])) {
+ $query->andWhere(['orders.delivery_date' => $params['delivery_date']]);
+ }
+
+ if (!empty($params['store_id'])) {
+ $query->andWhere(['orders.store_id' => (int) $params['store_id']]);
+ }
- return (new Query())
- ->limit(20)
- ->where(['!=', 'source', 'amo'])
- ->from(['orders' => $queryAmo->union($queryMarketplace, true)])
- ->all();
+ return new \yii\data\ArrayDataProvider([
+ 'allModels' => $query->all(),
+ 'pagination' => [
+ 'pageSize' => 20,
+ ],
+ 'sort' => [
+ 'attributes' => ['id', 'delivery_date', 'store_id', 'price'],
+ ],
+ ]);
}
public function attributes()
<?php
-/** @var yii\web\View $this */
-/** @var yii\data\ArrayDataProvider $dataProvider */
-use yii\grid\GridView;
use yii\helpers\Html;
+use yii\widgets\ActiveForm;
+use kartik\date\DatePicker;
+use kartik\select2\Select2;
+use yii\grid\GridView;
$this->title = 'Все заказы';
?>
<h1><?= Html::encode($this->title) ?></h1>
+<div class="order-filter">
+ <?php $form = ActiveForm::begin([
+ 'method' => 'get',
+ 'action' => ['all-orders'],
+
+ ]); ?>
+
+ <div style="display: flex; gap: 15px; align-items: center; flex-wrap: wrap;">
+ <?= Select2::widget([
+ 'name' => 'source',
+ 'value' => Yii::$app->request->get('source'),
+ 'data' => ['amo' => 'Amo', 'marketplace' => 'Marketplace'],
+ 'options' => ['placeholder' => 'Выберите источник'],
+ 'pluginOptions' => ['allowClear' => true],
+ ]); ?>
+
+ <?= DatePicker::widget([
+ 'name' => 'delivery_date',
+ 'value' => Yii::$app->request->get('delivery_date'),
+ 'options' => ['placeholder' => 'Выберите дату'],
+ 'pluginOptions' => [
+ 'format' => 'yyyy-mm-dd',
+ 'autoclose' => true,
+ ],
+ ]); ?>
+
+ <?= Select2::widget([
+ 'name' => 'store_id',
+ 'value' => Yii::$app->request->get('store_id'),
+ 'data' => [1 => 'Магазин 1', 2 => 'Магазин 2', 3 => 'Магазин 3'], // Должны быть реальные магазины
+ 'options' => ['placeholder' => 'Выберите магазин'],
+ 'pluginOptions' => ['allowClear' => true],
+ ]); ?>
+
+ <div class="form-group">
+ <?= Html::submitButton('Фильтровать', ['class' => 'btn btn-primary']) ?>
+ <?= Html::a('Сбросить', ['index'], ['class' => 'btn btn-default']) ?>
+ </div>
+ </div>
+
+ <?php ActiveForm::end(); ?>
+</div>
+
<?= GridView::widget([
'dataProvider' => $dataProvider,
- 'filterModel' => null,
'columns' => [
'id',
[
'label' => 'Состав',
'format' => 'raw',
'value' => function ($model) {
- $products = \yii\helpers\Json::decode($model['products_json']); // Декодируем JSON
- if (empty($products) || !is_array($products)) {
+ $products = json_decode($model['products_json'], true);
+ if (empty($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 .= "<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>';
}
-
return $output;
}
],