/////////////////////////// CHAT GPT STUFF ///////////////////////////////////
- $currentDate = new \DateTime();
+ $currentDate = new \DateTime(date($model->year . '-' . $model->month . '-01'));
- $startDate = (clone $currentDate)->modify('-3 months')->modify('first day of this month');
+ $startDate = (clone $currentDate)->modify('-4 months')->modify('first day of this month');
$endDate = (clone $startDate)->modify('+2 months')->modify('last day of this month');
$sales = Sales::find()->alias('s')->select([
"COUNT(*) as cnt",
- "SUM(CASE WHEN operation='Продажа' THEN s.summ ELSE (CASE WHEN operation='Возврат' THEN -s.summ ELSE 0 END) END) as total",
+ "SUM(CASE WHEN operation='Продажа' THEN sp.summ ELSE (CASE WHEN operation='Возврат' THEN -sp.summ ELSE 0 END) END) as total",
"s.store_id",
"p1cn.category as type",
"TO_CHAR(s.date, 'YYYY-MM') as month"
->asArray()
->all();
- $salesMatrix = Sales::find()->alias('s')->select([
+ $salesMatrixOffline = Sales::find()->alias('s')->select([
"COUNT(*) as cnt",
- "SUM(CASE WHEN operation='Продажа' THEN s.summ ELSE (CASE WHEN operation='Возврат' THEN -s.summ ELSE 0 END) END) as total",
+ "SUM(CASE WHEN operation='Продажа' THEN sp.summ ELSE (CASE WHEN operation='Возврат' THEN -sp.summ ELSE 0 END) END) as total",
"s.store_id",
"p1c.type as type",
"TO_CHAR(s.date, 'YYYY-MM') as month"
$compoundSalesData = [];
for ($i = 0; $i < 3; $i++) {
-;
$dateToStart = (clone $startDate)->modify("+$i months");
$compoundSalesYear = $dateToStart->format('Y');
$compoundSalesMonth = $dateToStart->format('m');
$types[$sale['type']] = 1;
}
$mnths = array_keys($mnths);
+
sort($mnths);
$countMnths = count($mnths);
$weights = [];
foreach ($sales as $sale) {
$table[$sale['store_id']][$sale['type']] = ($table[$sale['store_id']][$sale['type']] ?? 0) + $weights[$sale['month']] * $sale['total'] / $countMnths;
}
- foreach ($salesMatrix as $saleMatrix) {
+ foreach ($salesMatrixOffline as $saleMatrix) {
+ $types[$saleMatrix['type']] = 1;
$table[$saleMatrix['store_id']][$saleMatrix['type']] = ($table[$saleMatrix['store_id']][$saleMatrix['type']] ?? 0) + $weights[$saleMatrix['month']] * $saleMatrix['total'] / $countMnths;
}
//var_dump($table); die();
foreach ($compoundSalesData['offline'] as $monthKey => $categories) {
- $w = $weights[$monthKey];
+ $w = $weights[$monthKey] ?? 0;
foreach ($categories as $item) {
$store = $item['store_id'];
$type = $item['category'];
$sum = (float)$item['sum'];
+ $types[$item['category']] = 1;
$table[$store][$type] = ($table[$store][$type] ?? 0)
+ $w * $sum / $countMnths;
}
/////////////////////////////////////////////////////////////////////////////////////
$salesOnline = Sales::find()->alias('s')->select([
"COUNT(*) as cnt",
- "SUM(CASE WHEN operation='Продажа' THEN s.summ ELSE (CASE WHEN operation='Возврат' THEN -s.summ ELSE 0 END) END) as total",
+ "SUM(CASE WHEN operation='Продажа' THEN sp.summ ELSE (CASE WHEN operation='Возврат' THEN -sp.summ ELSE 0 END) END) as total",
"s.store_id",
- "(CASE WHEN p1c.type = 'Матрица' THEN 'Матрица' ELSE p1cn.category END) as type",
+ "p1cn.category as type",
"TO_CHAR(s.date, 'YYYY-MM') as month"
])
->leftJoin('sales_products sp', 's.id = sp.check_id')
->where(['between', 's.date', $date_start, $date_end])
->andWhere(['not in', 'order_id', ['', '0']])
->andWhere(['s.store_id' => $model->store_id])
- ->andWhere(['not in', 'p1cn.category', ['', 'сборка', 'сервис']])
+ ->andWhere(['p1c.components' => ''])
+ ->andWhere(['not in', 'p1cn.category', ['', 'букет', 'сборка', 'сервис']])
->groupBy([
's.store_id',
"TO_CHAR(s.date, 'YYYY-MM')",
- new \yii\db\Expression("
- CASE
- WHEN p1c.type = 'Матрица' THEN 'Матрица'
- ELSE p1cn.category
- END
- ")
+ "p1cn.category"
])
->orderBy(['month' => SORT_ASC, 'type' => SORT_ASC])
->asArray()
->all();
+ $salesMatrixOnline = Sales::find()->alias('s')->select([
+ "COUNT(*) as cnt",
+ "SUM(CASE WHEN operation='Продажа' THEN sp.summ ELSE (CASE WHEN operation='Возврат' THEN -sp.summ ELSE 0 END) END) as total",
+ "s.store_id",
+ "p1c.type as type",
+ "TO_CHAR(s.date, 'YYYY-MM') as month"
+ ])
+ ->leftJoin('sales_products sp', 's.id = sp.check_id')
+ ->leftJoin('products_1c p1c', 'p1c.id = sp.product_id')
+ ->where(['between', 's.date', $date_start, $date_end])
+ ->andWhere(['not in', 'order_id', ['', '0']])
+ ->andWhere(['p1c.type' => 'Матрица'])
+ ->andWhere(['s.store_id' => $model->store_id])
+ ->groupBy([
+ 's.store_id',
+ "TO_CHAR(s.date, 'YYYY-MM')",
+ "p1c.type"
+ ])
+ ->orderBy(['month' => SORT_ASC, 'type' => SORT_ASC])
+ ->asArray()
+ ->all();
$tableOnline = [];
$types[$sale['type']] = 1;
$tableOnline[$sale['store_id']][$sale['type']] = ($tableOnline[$sale['store_id']][$sale['type']] ?? 0) + $weights[$sale['month']] * $sale['total'] / 3;
}
+ foreach ($salesMatrixOnline as $saleMatrix) {
+ $types[$saleMatrix['type']] = 1;
+ $tableOnline[$saleMatrix['store_id']][$saleMatrix['type']] = ($tableOnline[$saleMatrix['store_id']][$saleMatrix['type']] ?? 0) + $weights[$saleMatrix['month']] * $saleMatrix['total'] / $countMnths;
+ }
+
+ foreach ($compoundSalesData['online'] as $monthKey => $categories) {
+ $w = $weights[$monthKey] ?? 0;
+ foreach ($categories as $item) {
+ $store = $item['store_id'];
+ $type = $item['category'];
+ $sum = (float)$item['sum'];
+ $types[$item['category']] = 1;
+ $tableOnline[$store][$type] = ($tableOnline[$store][$type] ?? 0)
+ + $w * $sum / $countMnths;
+ }
+ }
+ //var_dump($compoundSalesData['online']); die();
/////////////////////////////////////////////////////////////////////////////////////
$eit = ExportImportTable::find()->where(['entity' => 'city_store', 'export_id' => 1, 'entity_id' => $model->store_id])->one();
$store_id = $eit->export_val ?? '';
$writeOffs = WriteOffs::find()->alias('wo')->select([
- 'sum(wo.summ) as total',
- "(CASE WHEN p1c.type = 'Матрица' THEN 'Матрица' ELSE p1cn.category END) as p1ctype",
+ 'sum(wop.summ) as total',
+ "p1cn.category as p1ctype",
+ 'ex.entity_id AS store_id',
"TO_CHAR(wo.date, 'YYYY-MM') as month"
])
->leftJoin('write_offs_products wop', 'wop.write_offs_id = wo.id')
->leftJoin('products_1c p1c', 'p1c.id = wop.product_id')
->leftJoin('products_1c_nomenclature p1cn', 'p1cn.id = wop.product_id')
+ ->leftJoin('export_import_table ex', 'ex.export_val = wo.store_id')
->where(['between', 'wo.date', $date_start, $date_end])
->andWhere(['wo.type' => WriteOffsErp::WRITE_OFFS_TYPE_BRAK])
- ->andWhere(['not in', 'p1cn.category', ['', 'сборка', 'сервис']])
+ ->andWhere(['p1c.components' => ''])
+ ->andWhere(['not in', 'p1cn.category', ['', 'букет', 'сборка', 'сервис']])
->andWhere(['wo.store_id' => $store_id])
->groupBy([
"month",
- new \yii\db\Expression("
- CASE
- WHEN p1c.type = 'Матрица' THEN 'Матрица'
- ELSE p1cn.category
- END
- ")
+ 'ex.entity_id',
+ "p1cn.category"
])
->asArray()->all();
+ $writeOffsMatrix = WriteOffs::find()->alias('wo')->select([
+ 'sum(wop.summ) as total',
+ 'ex.entity_id AS store_id',
+ "p1c.type as p1ctype",
+ "TO_CHAR(wo.date, 'YYYY-MM') as month"
+ ])
+ ->leftJoin('write_offs_products wop', 'wop.write_offs_id = wo.id')
+ ->leftJoin('products_1c p1c', 'p1c.id = wop.product_id')
+ ->leftJoin('products_1c_nomenclature p1cn', 'p1cn.id = wop.product_id')
+ ->leftJoin('export_import_table ex', 'ex.export_val = wo.store_id')
+ ->where(['between', 'wo.date', $date_start, $date_end])
+ ->andWhere(['wo.type' => WriteOffsErp::WRITE_OFFS_TYPE_BRAK])
+ ->andWhere(['p1c.type' => 'Матрица'])
+ ->andWhere(['wo.store_id' => $store_id])
+ ->groupBy([
+ "month",
+ 'ex.entity_id',
+ "p1c.type"
+ ])
+ ->asArray()->all();
+
+
+ $compoundWriteOffsData = [];
+ for ($i = 0; $i < 3; $i++) {
+
+ $dateToStartWriteOffs = (clone $startDate)->modify("+$i months");
+ $compoundSalesYear = $dateToStartWriteOffs->format('Y');
+ $compoundSalesMonth = $dateToStartWriteOffs->format('m');
+ $compoundWriteOffs = $service->getProductsComponentsInCategory($model->store_id, $compoundSalesMonth, $compoundSalesYear, AutoPlannogrammaService::TYPE_WRITE_OFFS);
+
+ $writeoffsCompoundSum = $service->sumProductsComponentsByGroup($compoundWriteOffs, AutoPlannogrammaService::TYPE_WRITE_OFFS);
+
+ $dateKey = $compoundSalesYear . '-' . $compoundSalesMonth;
+ $compoundWriteOffsData[$dateKey] = $writeoffsCompoundSum;
+ }
+
$tableWriteOffs = [];
+
foreach ($writeOffs as $writeoff) {
$types[$writeoff['p1ctype']] = 1;
- $tableWriteOffs[$writeoff['p1ctype']] = ($tableWriteOffs[$writeoff['p1ctype']] ?? 0) + $weights[$writeoff['month']] * $writeoff['total'] / 3;
+ $tableWriteOffs[$writeoff['store_id']][$writeoff['p1ctype']] = ($tableWriteOffs[$writeoff['store_id']][$writeoff['p1ctype']] ?? 0) + $weights[$writeoff['month']] * $writeoff['total'] / $countMnths;
+ }
+
+ foreach ($writeOffsMatrix as $writeOffMatrix) {
+ $types[$writeOffMatrix['p1ctype']] = 1;
+ $tableWriteOffs[$writeOffMatrix['store_id']][$writeOffMatrix['p1ctype']] = ($tableWriteOffs[$writeOffMatrix['store_id']][$writeOffMatrix['p1ctype']] ?? 0) + $weights[$writeOffMatrix['month']] * $writeOffMatrix['total'] / $countMnths;
}
+
+ foreach ($compoundWriteOffsData as $monthKey => $categories) {
+ $w = $weights[$monthKey] ?? 0;
+ foreach ($categories as $item) {
+ $store = $item['store_id'];
+ $type = $item['category'];
+ $sum = (float)$item['sum'];
+ $types[$item['category']] = 1;
+ $tableWriteOffs[$store][$type] = ($tableWriteOffs[$store][$type] ?? 0)
+ + $w * $sum / $countMnths;
+ }
+ }
+
$types = array_keys($types);
/////////////////////////////////////////////////////////////////////////////////////
$categoryPlanNew->offline = $table[$model->store_id][$type] ?? 0;
$categoryPlanNew->internet_shop = $tableOnline[$model->store_id][$type] ?? 0;
$categoryPlanNew->marketplace = 0;
- $categoryPlanNew->write_offs = $tableWriteOffs[$type] ?? 0;
+ $categoryPlanNew->write_offs = $tableWriteOffs[$model->store_id][$type] ?? 0;
$categoryPlanNew->created_at = date('Y-m-d HH:i:s');
$categoryPlanNew->updated_at = date('Y-m-d HH:i:s');
$categoryPlanNew->created_by = Yii::$app->user->id;