--- /dev/null
+<?php
+
+namespace app\controllers;
+
+use Yii;
+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\Sales;
+use yii_app\services\AutoPlannogrammaService;
+
+class AutoPlannogrammaController extends BaseController
+{
+
+
+ public function behaviors()
+ {
+ return [
+ 'access' => [
+ 'class' => \yii\filters\AccessControl::class,
+ 'rules' => [
+ [
+ 'allow' => true,
+ 'roles' => ['@'],
+ ],
+ ],
+ ],
+ ];
+ }
+
+
+ public function actionIndex()
+ {
+ $categories = (new Query())
+ ->select([
+ 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
+ ])
+ ->all();
+
+ $stores = ArrayHelper::map(CityStore::findAll(['visible' => CityStore::IS_VISIBLE]), 'id', 'name');
+
+ return $this->render('index', [
+ 'stores' => $stores,
+ 'categories' => $categories
+ ]);
+ }
+
+ public function actionCalculating()
+ {
+ $request = Yii::$app->request;
+
+ $filters = [
+ 'category' => $request->get('category'),
+ 'subcategory' => $request->get('subcategory'),
+ 'product_name' => $request->get('product_name'),
+ 'store_id' => $request->get('store_id'),
+ 'plan_date' => $request->get('plan_date'),
+ 'type' => $request->get('type'),
+ ];
+
+ $dataProvider = new ArrayDataProvider([
+ 'allModels' => [],
+ 'pagination' => [
+ 'pageSize' => 100
+ ],
+ ]);
+
+ if (!empty($filters['plan_date'])) {
+ $filters = [
+ 'category' => $request->get('category'),
+ 'subcategory' => $request->get('subcategory'),
+ 'product_name' => $request->get('product_name'),
+ 'store_id' => $request->get('store_id'),
+ 'plan_date' => $request->get('plan_date'),
+ 'type' => $request->get('type'),
+ ];
+
+ $service = new AutoPlannogrammaService();
+
+ $dataProvider = new ArrayDataProvider([
+ 'allModels' => $service->calculateFullGoalChain($filters),
+ 'pagination' => [
+ 'pageSize' => 100
+ ],
+ ]);
+
+ }
+
+ return $this->render('calculating', [
+ 'dataProvider' => $dataProvider,
+ 'filters' => $filters,
+ ]);
+ }
+
+ public function actionControl()
+ {
+ $filters = Yii::$app->request->get();
+ $params = [];
+ $conditions = [];
+
+ if (!empty($filters['product_name'])) {
+ $conditions[] = 'p1n.name LIKE :product_name';
+ $params[':product_name'] = '%' . $filters['product_name'] . '%';
+ }
+
+ if (!empty($filters['category'])) {
+ $conditions[] = 'p1n.category = :category';
+ $params[':category'] = $filters['category'];
+ }
+
+ if (!empty($filters['subcategory'])) {
+ $conditions[] = 'p1n.subcategory = :subcategory';
+ $params[':subcategory'] = $filters['subcategory'];
+ }
+
+ if (!empty($filters['city_name'])) {
+ $conditions[] = 'c.name LIKE :city_name';
+ $params[':city_name'] = '%' . $filters['city_name'] . '%';
+ }
+
+ if (!empty($filters['plan_date'])) {
+ $date = \DateTime::createFromFormat('d-m-Y', $filters['plan_date']);
+ if ($date) {
+ $startDate = $date->format('Y-m-d');
+ $endDate = $date->modify('+1 month')->format('Y-m-d');
+
+ if ($filters['type'] === 'writeOffs') {
+ $conditions[] = 'DATE(w.date) >= :start_date';
+ $conditions[] = 'DATE(w.date) < :end_date';
+ } else {
+ $conditions[] = 'DATE(s.date) >= :start_date';
+ $conditions[] = 'DATE(s.date) < :end_date';
+ }
+
+ $params[':start_date'] = $startDate;
+ $params[':end_date'] = $endDate;
+ }
+ }
+
+ $where = !empty($conditions) ? 'WHERE ' . implode(' AND ', $conditions) : '';
+ if (isset($filters['type']) && $filters['type'] == 'writeOffs') {
+ $query = Yii::$app->db->createCommand("
+ SELECT
+ c.name AS city_name,
+ p1n.category,
+ p1n.subcategory,
+ item ->> 'product_id' AS product_id,
+ p1n.name AS product_name,
+ SUM(CAST(item ->> 'quantity' AS NUMERIC)) AS count,
+ SUM(CAST(item ->> 'summ' AS NUMERIC)) AS sum
+ FROM write_offs w
+ JOIN export_import_table ex ON ex.export_val = w.store_id
+ JOIN city_store c ON c.id = ex.entity_id
+ JOIN LATERAL jsonb_array_elements(w.items::jsonb) AS item ON true
+ LEFT JOIN products_1c_nomenclature p1n ON p1n.id = item ->> 'product_id'
+ $where
+ GROUP BY w.store_id, c.name, p1n.category, p1n.subcategory, product_id, p1n.name
+ ORDER BY w.store_id, p1n.category, p1n.subcategory, p1n.name
+ ")->bindValues($params)->queryAll();
+ } else {
+ $query = Yii::$app->db->createCommand("
+ SELECT
+ c.name AS city_name,
+ p1n.name AS product_name,
+ p1n.category,
+ p1n.subcategory,
+ SUM(CASE
+ WHEN s.operation = 'Продажа' THEN sp.quantity
+ WHEN s.operation = 'Возврат' THEN -sp.quantity
+ ELSE 0
+ END) AS count,
+ SUM(CASE
+ WHEN s.operation = 'Продажа' THEN sp.summ
+ WHEN s.operation = 'Возврат' THEN -sp.summ
+ ELSE 0
+ END) AS sum
+ FROM sales s
+ LEFT JOIN sales_products sp ON sp.check_id = s.id
+ LEFT JOIN products_1c_nomenclature p1n ON p1n.id = sp.product_id
+ LEFT JOIN export_import_table ex ON ex.export_val = store_id_1c
+ LEFT JOIN city_store c ON c.id = entity_id
+ $where
+ GROUP BY p1n.name, p1n.category, p1n.subcategory, c.name, c.id
+ ORDER BY c.id DESC, category, subcategory, p1n.name
+")->bindValues($params)->queryAll();
+ }
+ $dataProvider = new ArrayDataProvider([
+ 'allModels' => $query,
+ 'pagination' => ['pageSize' => 20],
+ ]);
+
+ return $this->render('control', [
+ 'dataProvider' => $dataProvider,
+ 'filters' => $filters,
+ ]);
+ }
+}
\ No newline at end of file
+++ /dev/null
-<?php
-
-namespace app\controllers;
-
-use Yii;
-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\Sales;
-use yii_app\services\AutoPlannogrammaService;
-
-class AutoPlanogrammaController extends BaseController
-{
-
-
- public function behaviors()
- {
- return [
- 'access' => [
- 'class' => \yii\filters\AccessControl::class,
- 'rules' => [
- [
- 'allow' => true,
- 'roles' => ['@'],
- ],
- ],
- ],
- ];
- }
-
-
- public function actionIndex()
- {
- $categories = (new Query())
- ->select([
- 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
- ])
- ->all();
-
- $stores = ArrayHelper::map(CityStore::findAll(['visible' => CityStore::IS_VISIBLE]), 'id', 'name');
-
- return $this->render('index', [
- 'stores' => $stores,
- 'categories' => $categories
- ]);
- }
-
- public function actionCalculating()
- {
- $request = Yii::$app->request;
-
- $filters = [
- 'category' => $request->get('category'),
- 'subcategory' => $request->get('subcategory'),
- 'product_name' => $request->get('product_name'),
- 'store_id' => $request->get('store_id'),
- 'plan_date' => $request->get('plan_date'),
- 'type' => $request->get('type'),
- ];
-
- $dataProvider = new ArrayDataProvider([
- 'allModels' => [],
- 'pagination' => [
- 'pageSize' => 100
- ],
- ]);
-
- if (!empty($filters['plan_date'])) {
- $filters = [
- 'category' => $request->get('category'),
- 'subcategory' => $request->get('subcategory'),
- 'product_name' => $request->get('product_name'),
- 'store_id' => $request->get('store_id'),
- 'plan_date' => $request->get('plan_date'),
- 'type' => $request->get('type'),
- ];
-
- $service = new AutoPlannogrammaService();
-
- $dataProvider = new ArrayDataProvider([
- 'allModels' => $service->calculateFullGoalChain($filters),
- 'pagination' => [
- 'pageSize' => 100
- ],
- ]);
-
- }
-
- return $this->render('calculating', [
- 'dataProvider' => $dataProvider,
- 'filters' => $filters,
- ]);
- }
-
- public function actionControl()
- {
- $filters = Yii::$app->request->get();
- $params = [];
- $conditions = [];
-
- if (!empty($filters['product_name'])) {
- $conditions[] = 'p1n.name LIKE :product_name';
- $params[':product_name'] = '%' . $filters['product_name'] . '%';
- }
-
- if (!empty($filters['category'])) {
- $conditions[] = 'p1n.category = :category';
- $params[':category'] = $filters['category'];
- }
-
- if (!empty($filters['subcategory'])) {
- $conditions[] = 'p1n.subcategory = :subcategory';
- $params[':subcategory'] = $filters['subcategory'];
- }
-
- if (!empty($filters['city_name'])) {
- $conditions[] = 'c.name LIKE :city_name';
- $params[':city_name'] = '%' . $filters['city_name'] . '%';
- }
-
- if (!empty($filters['plan_date'])) {
- $date = \DateTime::createFromFormat('d-m-Y', $filters['plan_date']);
- if ($date) {
- $startDate = $date->format('Y-m-d');
- $endDate = $date->modify('+1 month')->format('Y-m-d');
-
- if ($filters['type'] === 'writeOffs') {
- $conditions[] = 'DATE(w.date) >= :start_date';
- $conditions[] = 'DATE(w.date) < :end_date';
- } else {
- $conditions[] = 'DATE(s.date) >= :start_date';
- $conditions[] = 'DATE(s.date) < :end_date';
- }
-
- $params[':start_date'] = $startDate;
- $params[':end_date'] = $endDate;
- }
- }
-
- $where = !empty($conditions) ? 'WHERE ' . implode(' AND ', $conditions) : '';
- if (isset($filters['type']) && $filters['type'] == 'writeOffs') {
- $query = Yii::$app->db->createCommand("
- SELECT
- c.name AS city_name,
- p1n.category,
- p1n.subcategory,
- item ->> 'product_id' AS product_id,
- p1n.name AS product_name,
- SUM(CAST(item ->> 'quantity' AS NUMERIC)) AS count,
- SUM(CAST(item ->> 'summ' AS NUMERIC)) AS sum
- FROM write_offs w
- JOIN export_import_table ex ON ex.export_val = w.store_id
- JOIN city_store c ON c.id = ex.entity_id
- JOIN LATERAL jsonb_array_elements(w.items::jsonb) AS item ON true
- LEFT JOIN products_1c_nomenclature p1n ON p1n.id = item ->> 'product_id'
- $where
- GROUP BY w.store_id, c.name, p1n.category, p1n.subcategory, product_id, p1n.name
- ORDER BY w.store_id, p1n.category, p1n.subcategory, p1n.name
- ")->bindValues($params)->queryAll();
- } else {
- $query = Yii::$app->db->createCommand("
- SELECT
- c.name AS city_name,
- p1n.name AS product_name,
- p1n.category,
- p1n.subcategory,
- SUM(CASE
- WHEN s.operation = 'Продажа' THEN sp.quantity
- WHEN s.operation = 'Возврат' THEN -sp.quantity
- ELSE 0
- END) AS count,
- SUM(CASE
- WHEN s.operation = 'Продажа' THEN sp.summ
- WHEN s.operation = 'Возврат' THEN -sp.summ
- ELSE 0
- END) AS sum
- FROM sales s
- LEFT JOIN sales_products sp ON sp.check_id = s.id
- LEFT JOIN products_1c_nomenclature p1n ON p1n.id = sp.product_id
- LEFT JOIN export_import_table ex ON ex.export_val = store_id_1c
- LEFT JOIN city_store c ON c.id = entity_id
- $where
- GROUP BY p1n.name, p1n.category, p1n.subcategory, c.name, c.id
- ORDER BY c.id DESC, category, subcategory, p1n.name
-")->bindValues($params)->queryAll();
- }
- $dataProvider = new ArrayDataProvider([
- 'allModels' => $query,
- 'pagination' => ['pageSize' => 20],
- ]);
-
- return $this->render('control', [
- 'dataProvider' => $dataProvider,
- 'filters' => $filters,
- ]);
- }
-}
\ No newline at end of file