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 [
];
}
-
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
- "),
+ 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')
+ 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
+ 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');
+ $stores = ArrayHelper::map(
+ CityStore::findAll(['visible' => CityStore::IS_VISIBLE]),
+ 'id',
+ 'name'
+ );
return $this->render('index', [
'stores' => $stores,
- 'categories' => $categories
+ 'categories' => $categories,
]);
}
$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'),
+ '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
- ],
+ '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
- ],
+ 'pagination' => ['pageSize' => 100],
]);
-
}
return $this->render('calculating', [
'dataProvider' => $dataProvider,
- 'filters' => $filters,
+ 'filters' => $filters,
]);
}
}
$where = !empty($conditions) ? 'WHERE ' . implode(' AND ', $conditions) : '';
- if (isset($filters['type']) && $filters['type'] == 'writeOffs') {
+
+ 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();
+ 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();
+ 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,
+ 'filters' => $filters,
]);
}
-}
\ No newline at end of file
+}