use yii\data\ArrayDataProvider;
use yii\db\Expression;
use yii\db\Query;
+use yii\helpers\ArrayHelper;
+use yii_app\records\CityStore;
use yii_app\records\Products1cNomenclature;
{
public function run()
{
- $categories = Products1cNomenclature::find()
+ $categories = (new Query())
->select([
- 'category',
- new Expression("jsonb_object_agg(subcategory, products) AS subcategories")
+ new Expression("
+ CASE
+ WHEN category ILIKE 'срезка' THEN 'Срезка'
+ WHEN category ILIKE 'горшечные растения' THEN 'Горшечные растения'
+ WHEN category ILIKE 'сухоцветы' THEN 'Сухоцветы'
+ ELSE 'Остальные категории'
+ END AS category
+ "),
+ 'subcategory',
+ new Expression('json_agg(name) AS products')
+ ])
+ ->from('products_1c_nomenclature')
+ ->groupBy(['category', 'subcategory'])
+ ->orderBy([
+ new Expression("
+ CASE
+ WHEN category ILIKE 'срезка' THEN 1
+ WHEN category ILIKE 'горшечные растения' THEN 2
+ WHEN category ILIKE 'сухоцветы' THEN 3
+ ELSE 4
+ END
+ "),
+ 'subcategory' => SORT_ASC
])
- ->from(
- (new Query())
- ->select([
- 'category',
- 'subcategory',
- new Expression('array_agg(name) AS products')
- ])
- ->from('products_1c_nomenclature')
- ->andWhere(['ilike', 'name', 'роз'])
- ->groupBy(['category', 'subcategory'])
- )
- ->groupBy('category')
- ->asArray()
->all();
- $dataProvider = new ArrayDataProvider([
+ $stores = ArrayHelper::map(CityStore::findAll(['visible' => CityStore::IS_VISIBLE]), 'id', 'name');
+ //1 month_category_share
+ //-- select * from sales where date >= '2025-03-01'
+ //-- select * from sales_products limit 6
+
+ //1 =1.88
+ //weight./category - не поняла зачем в формуле
+// SELECT
+// category,
+// SUM(sp.summ) AS total_sum,
+// ROUND((SUM(sp.summ) * 100) /
+// (SELECT SUM(sp2.summ)
+// FROM sales
+// LEFT JOIN sales_products sp2 ON sp2.check_id = sales.id
+// WHERE date >= '2025-04-01'), 2) AS share_of_total
+// FROM sales
+// LEFT JOIN sales_products sp ON sp.check_id = sales.id
+// LEFT JOIN products_1c_nomenclature p1c ON p1c.id = sp.product_id
+// WHERE date >= '2025-04-01' and category = 'Горшечные_растения'
+// GROUP BY category;
+ //откуда берем 4200000?
+ //2 1.88 * 4200000 = 78960
+ //
+ //3
+// WITH monthly_sales AS (
+// SELECT
+// EXTRACT(MONTH FROM s.date) AS month,
+// EXTRACT(YEAR FROM s.date) AS year,
+// SUM(sp.summ) AS total_sales
+// FROM sales_products sp
+// LEFT JOIN sales s ON s.id = sp.check_id
+// WHERE sp.summ IS NOT NULL
+// GROUP BY EXTRACT(YEAR FROM s.date), EXTRACT(MONTH FROM s.date)
+//),
+//subcategory_sales AS (
+// SELECT
+// EXTRACT(MONTH FROM s.date) AS month,
+// EXTRACT(YEAR FROM s.date) AS year,
+// p1c.subcategory,
+// SUM(sp.summ) AS total_sum
+// FROM sales_products sp
+// LEFT JOIN products_1c_nomenclature p1c ON p1c.id = sp.product_id
+// LEFT JOIN sales s ON s.id = sp.check_id
+// WHERE p1c.category = 'Горшечные_растения'
+// GROUP BY EXTRACT(YEAR FROM s.date), EXTRACT(MONTH FROM s.date), p1c.subcategory
+//)
+//SELECT
+// ss.subcategory,
+// ss.year,
+// ss.month,
+// ss.total_sum,
+// ROUND(
+// (SUM(ss.total_sum) OVER (PARTITION BY ss.month, ss.subcategory) /
+// SUM(ss.total_sum) OVER (PARTITION BY ss.month)) * 100,
+// 2
+// ) AS percent_of_month
+//FROM subcategory_sales ss
+//JOIN monthly_sales ms
+// ON ss.month = ms.month AND ss.year = ms.year
+// -- WHERE ss.month = 1 -- Фильтрация только для января
+//ORDER BY ss.month, ss.subcategory, ss.year;
+ //4 = 40.59 * 78960 = 32049
+// 5WITH monthly_sales AS (
+// SELECT
+// EXTRACT(MONTH FROM s.date) AS month,
+// SUM(sp.summ) AS total_sales
+// FROM erp24.sales_products sp
+// LEFT JOIN erp24.sales s ON s.id = sp.check_id
+// WHERE sp.summ IS NOT NULL
+// GROUP BY EXTRACT(MONTH FROM s.date)
+// ),
+// subcategory_sales AS (
+// SELECT
+// p1.name,
+// p1.subcategory,
+// EXTRACT(MONTH FROM s.date) AS month,
+// SUM(sp.summ) AS total_sum
+// FROM erp24.sales_products sp
+// LEFT JOIN erp24.products_1c_nomenclature p1 ON p1.id = sp.product_id
+// LEFT JOIN erp24.sales s ON s.id = sp.check_id
+// WHERE p1.subcategory ILIKE '%Лиственные%'
+// GROUP BY p1.name, p1.subcategory, EXTRACT(MONTH FROM s.date)
+// )
+// SELECT
+// ss.name,
+// ss.subcategory,
+// ss.month,
+// ss.total_sum,
+// ROUND(
+// (ss.total_sum / SUM(ss.total_sum) OVER (PARTITION BY ss.month)) * 100,
+// 2
+// ) AS percent_of_month
+// FROM subcategory_sales ss
+// JOIN monthly_sales ms
+// ON ss.month = ms.month
+// ORDER BY ss.month, ss.subcategory, ss.name;
+
- ]);
return $this->controller->render('auto-planogramma', [
- 'categories' => $categories
+ 'categories' => $categories,
+ 'stores' => $stores
]);
}
}
use yii\data\ArrayDataProvider;
-$data = [
- ['Адрес' => '01 Ванеева 181', 'Значение' => 300],
- ['Адрес' => '02 Плотникова 4', 'Значение' => 300],
- ['Адрес' => '03 Гагарина 60', 'Значение' => 100],
- ['Адрес' => '04 Московское шоссе 9а', 'Значение' => 50],
- ['Адрес' => '05 Белинского 71', 'Значение' => 300],
- ['Адрес' => '06 Коминтерна 168', 'Значение' => 160],
- ['Адрес' => '07 Аэродромная', 'Значение' => 300],
-];
-$addresses = array_column($data, 'Адрес');
-$values = array_column($data, 'Значение');
-
-$row = array_combine($addresses, $values);
-$dataProvider = new ArrayDataProvider([
- 'allModels' => [$row],
- 'pagination' => false,
-]);
-
$tooltipText = "Оффлайн: 50 Флаумак (off-line): 20 Флаумак (on-line): 10 Флаумак (market): 10 Изумительная (off-line): 30 Списания: 20"; ?>
<h1 class="ms-3 mb-4"><?= Html::encode("Автопланограмма") ?></h1>
</div>
<div class="table-wrapper">
<table class="table">
-
<thead>
<tr class="head">
<th scope="col" style="text-align: left !important;">
</div>
</th>
- <?php foreach ($addresses as $address) { ?>
+ <?php foreach ($stores as $store) { ?>
<th scope="col" class="fixed-column">
- <?= Html::label($address, null, [
+ <?= Html::label($store, null, [
'style' => 'writing-mode: sideways-lr; text-align: center; white-space: nowrap;
font-weight: bold; transform-origin: left bottom; padding-right: 7%;'
]) ?>
</tr>
</thead>
<tbody>
- <tr>
- <td class="category">
- <a class="list-group-item list-group-item-action" data-bs-toggle="collapse" href="#cutting">Срезка
- ▲</a>
- </td>
- </tr>
+ <?php
+ $groupedCategories = [];
- <tr>
- <td style="position: relative; display: flex; justify-content: flex-end;" class="subcategory">
- <a class="list-group-item list-group-item-action" style="width: 95%;">
- Розы ▶
- </a>
- </td>
- </tr>
- <tr>
- <td style="position: relative; display: flex; justify-content: flex-end;">
- <a class="list-group-item list-group-item-action" style="width: 90%;">
- Розы 40 Эк
- </a>
- </td>
+ // Группируем данные по категориям и подкатегориям
+ foreach ($categories as $item) {
+ $groupedCategories[$item['category']][$item['subcategory']] = $item['products'];
+ }
- <?php foreach ($addresses as $address) { ?>
- <td class="items">
- <div style="display: flex; align-items: center;">
- <?= Html::input('text', '', 50, [
- 'class' => 'btn btn-primary input',
- 'data-bs-toggle' => 'tooltip',
- 'data-bs-placement' => 'top',
- 'title' => $tooltipText,
- ]) ?>
- <button style="border: none; background: transparent; cursor: pointer; margin-left: 5px;">
- <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24"
- viewBox="0 0 24 24">
- <path d="M20 11v5a1 1 0 0 1-2 0v-4H7.414l1.293 1.293a1 1 0 0 1-1.414 1.414l-3-3a1 1 0 0 1 0-1.416l3-3a1 1 0 0 1 1.414 1.416L7.414 10H19a1 1 0 0 1 1 1z"
- fill="grey" stroke="none"/>
- </svg>
- </button>
- </div>
+ foreach ($groupedCategories as $category => $subcategories): ?>
+ <tr>
+ <td class="category">
+ <a class="list-group-item list-group-item-action">
+ <?= Html::encode($category) ?> ▲
+ </a>
</td>
- <?php } ?>
- </tr>
- <tr>
- <td style="position: relative; display: flex; justify-content: flex-end;">
- <a class="list-group-item list-group-item-action" style="width: 90%;">
- Розы 80 Эк
- </a>
- </td>
- <?php foreach ($addresses as $address) { ?>
- <td class="items">
- <div style="display: flex; align-items: center;">
- <?= Html::input('text', '', 50, [
- 'class' => 'btn btn-primary input',
- 'data-bs-toggle' => 'tooltip',
- 'data-bs-placement' => 'top',
- 'title' => $tooltipText,
+ </tr>
- ]) ?>
- <button style="border: none; background: transparent; cursor: pointer; margin-left: 5px;">
- <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24"
- viewBox="0 0 24 24">
- <path d="M20 11v5a1 1 0 0 1-2 0v-4H7.414l1.293 1.293a1 1 0 0 1-1.414 1.414l-3-3a1 1 0 0 1 0-1.416l3-3a1 1 0 0 1 1.414 1.416L7.414 10H19a1 1 0 0 1 1 1z"
- fill="grey" stroke="none"/>
- </svg>
- </button>
- </div>
- </td>
- <?php } ?>
- </tr>
- <tr>
- <td style="position: relative; display: flex; justify-content: flex-end;" class="subcategory">
- <a class="list-group-item list-group-item-action" style="width: 95%;">
- Экзотика ▶
- </a>
- </td>
- </tr>
- <tr>
- <td style="position: relative; display: flex; justify-content: flex-end;" class="subcategory">
- <a class="list-group-item list-group-item-action" style="width: 95%;">
- Зелень ▶
- </a>
- </td>
- </tr>
- <tr>
- <td class="category">
- <a class="list-group-item list-group-item-action">Горшечные растения ▲</a>
- </td>
- </tr>
- <tr>
- <td class="category">
- <a class="list-group-item list-group-item-action">Сухоцветы ▲</a>
- </td>
- </tr>
- <tr>
- <td class="category">
- <a class="list-group-item list-group-item-action">Остальные категории ▲</a>
- </td>
- </tr>
+ <?php foreach ($subcategories as $subcategory => $products): ?>
+ <tr>
+ <td style="position: relative; display: flex; justify-content: flex-end;" class="subcategory">
+ <a class="list-group-item list-group-item-action" style="width: 95%;">
+ <?= Html::encode($subcategory) ?> ▶
+ </a>
+ </td>
+ </tr>
+ <?php
+ $products = is_string($products) ? json_decode($products, true) : $products;
+ ?>
+ <?php foreach ($products as $product): ?>
+ <tr>
+ <td style="position: relative; display: flex; justify-content: flex-end;">
+ <a class="list-group-item list-group-item-action" style="width: 90%;">
+ <?= Html::encode($product) ?>
+ </a>
+ </td>
+ <?php foreach ($stores as $store): ?>
+ <td class="items">
+ <div style="display: flex; align-items: center;">
+ <?= Html::input('text', '', 50, [
+ 'class' => 'btn btn-primary input',
+ 'data-bs-toggle' => 'tooltip',
+ 'data-bs-placement' => 'top',
+ 'title' => $tooltipText,
+ ]) ?>
+ <button style="border: none; background: transparent; cursor: pointer; margin-left: 5px;">
+ <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24">
+ <path d="M20 11v5a1 1 0 0 1-2 0v-4H7.414l1.293 1.293a1 1 0 0 1-1.414 1.414l-3-3a1 1 0 0 1 0-1.416l3-3a1 1 0 1 1 1.414 1.416L7.414 10H19a1 1 0 0 1 1 1z"
+ fill="grey" stroke="none"/>
+ </svg>
+ </button>
+ </div>
+ </td>
+ <?php endforeach; ?>
+ </tr>
+ <?php endforeach; ?>
+ <?php endforeach; ?>
+ <?php endforeach; ?>
</tbody>
</table>
</div>