]);
$model->load(Yii::$app->request->get());
+
$service = new AutoPlannogrammaService();
$isEditable = date($model->year . '-' . $model->month . '-d') > date('Y-m-d') && (
(date('d') < 25) || (date('Y-m-d', strtotime('-1 month', strtotime(date($model->year . '-' . $model->month . '-d')))) > date('Y-m-d')));
$categoryPlan = CategoryPlan::find()->where(['year' => $model->year, 'month' => $model->month, 'store_id' => $model->store_id])->indexBy('category')->asArray()->all();
-
+ $types = [];
+ $table = [];
+ $tableOnline = [];
+ $tableWriteOffs = [];
+ $years = [];
+ for ($i = 3; $i >= 0; $i--) {
+ $year = date("Y") - $i;
+ $years [$year] = $year;
+ }
+ $stores = ArrayHelper::map(CityStore::find()->andWhere(['visible' => '1'])->all(), 'id', 'name');
+ $salesWriteOffsPlan = SalesWriteOffsPlan::find()->where(['year' => $model->year, 'month' => $model->month, 'store_id' => $model->store_id])->one();
/////////////////////////// CHAT GPT STUFF ///////////////////////////////////
+ //var_dump(Yii::$app->request->get()); die();
+ if (
+ $model->year
+ && $model->month
+ && $model->store_id
+ ) {
+ if (Yii::$app->request->get('delete') === '1'
+ ) {
+ $count = Yii::$app->db
+ ->createCommand()
+ ->delete('erp24.category_plan', [
+ 'year' => $model->year,
+ 'month' => $model->month,
+ 'store_id' => $model->store_id,
+ ])
+ ->execute();
+
+ Yii::$app->session->setFlash(
+ $count ? 'success' : 'info',
+ $count
+ ? "Удалено {$count} записей."
+ : 'Ничего не удалено.'
+ );
+
+
+ $params = [
+ 'DynamicModel' => $model->attributes,
+ ];
+
+ $params['DynamicModel'] = array_filter($params['DynamicModel'], function($v) {
+ return $v !== null && $v !== '';
+ });
+
+ return $this->redirect(array_merge(
+ ['index'],
+ $params
+ ));
- $currentDate = new \DateTime(date($model->year . '-' . $model->month . '-01'));
-
- $startDate = (clone $currentDate)->modify('-4 months')->modify('first day of this month');
-
- $endDate = (clone $startDate)->modify('+2 months')->modify('last day of this month');
-
- $date_start = $startDate->format('Y-m-d');
- $date_end = $endDate->format('Y-m-d');
-
- $sales = 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",
- "p1cn.category 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')
- ->leftJoin('products_1c_nomenclature p1cn', 'p1cn.id = sp.product_id')
- ->where(['between', 's.date', $date_start, $date_end])
- ->andWhere(['order_id' => ['', '0']])
- ->andWhere(['p1c.components' => ''])
- ->andWhere(['not in', 'p1cn.category', ['', 'букет', 'сборка', 'сервис']])
- ->andWhere(['s.store_id' => $model->store_id])
- ->groupBy([
- 's.store_id',
- "TO_CHAR(s.date, 'YYYY-MM')",
- "p1cn.category"
- ])
- ->orderBy(['month' => SORT_ASC, 'type' => SORT_ASC])
- ->asArray()
- ->all();
+ }
+ $currentDate = new \DateTime(date($model->year . '-' . $model->month . '-01'));
- $salesMatrixOffline = 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(['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();
+ $startDate = (clone $currentDate)->modify('-4 months')->modify('first day of this month');
+ $endDate = (clone $startDate)->modify('+2 months')->modify('last day of this month');
- $compoundSalesData = [];
- for ($i = 0; $i < 3; $i++) {
- $dateToStart = (clone $startDate)->modify("+$i months");
- $compoundSalesYear = $dateToStart->format('Y');
- $compoundSalesMonth = $dateToStart->format('m');
- $compoundSales = $service->getProductsComponentsInCategory($model->store_id, $compoundSalesMonth, $compoundSalesYear);
- //var_dump($compoundSales); die();
- $offline = [];
- $online = [];
- foreach ($compoundSales as $compoundSale) {
- if (in_array($compoundSale['order_id'], ['', '0'], true)) {
- $offline[] = $compoundSale;
- } else {
- $online[] = $compoundSale;
+ $date_start = $startDate->format('Y-m-d');
+ $date_end = $endDate->format('Y-m-d');
+
+ $sales = 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",
+ "p1cn.category 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')
+ ->leftJoin('products_1c_nomenclature p1cn', 'p1cn.id = sp.product_id')
+ ->where(['between', 's.date', $date_start, $date_end])
+ ->andWhere(['order_id' => ['', '0']])
+ ->andWhere(['p1c.components' => ''])
+ ->andWhere(['not in', 'p1cn.category', ['', 'букет', 'сборка', 'сервис']])
+ ->andWhere(['s.store_id' => $model->store_id])
+ ->groupBy([
+ 's.store_id',
+ "TO_CHAR(s.date, 'YYYY-MM')",
+ "p1cn.category"
+ ])
+ ->orderBy(['month' => SORT_ASC, 'type' => SORT_ASC])
+ ->asArray()
+ ->all();
+
+ $salesMatrixOffline = 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(['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();
+
+
+ $compoundSalesData = [];
+ for ($i = 0; $i < 3; $i++) {
+ $dateToStart = (clone $startDate)->modify("+$i months");
+ $compoundSalesYear = $dateToStart->format('Y');
+ $compoundSalesMonth = $dateToStart->format('m');
+ $compoundSales = $service->getProductsComponentsInCategory($model->store_id, $compoundSalesMonth, $compoundSalesYear);
+ //var_dump($compoundSales); die();
+ $offline = [];
+ $online = [];
+ foreach ($compoundSales as $compoundSale) {
+ if (in_array($compoundSale['order_id'], ['', '0'], true)) {
+ $offline[] = $compoundSale;
+ } else {
+ $online[] = $compoundSale;
+ }
}
- }
- $dateKey = $compoundSalesYear . '-' . $compoundSalesMonth;
- $offlineCompoundSum = $service->sumProductsComponentsByGroup($offline, AutoPlannogrammaService::TYPE_SALES);
- $onlineCompoundSum = $service->sumProductsComponentsByGroup($online, AutoPlannogrammaService::TYPE_SALES);
+ $dateKey = $compoundSalesYear . '-' . $compoundSalesMonth;
+ $offlineCompoundSum = $service->sumProductsComponentsByGroup($offline, AutoPlannogrammaService::TYPE_SALES);
+ $onlineCompoundSum = $service->sumProductsComponentsByGroup($online, AutoPlannogrammaService::TYPE_SALES);
- $compoundSalesData['offline'][$dateKey] = $offlineCompoundSum;
- $compoundSalesData['online'][$dateKey] = $onlineCompoundSum;
+ $compoundSalesData['offline'][$dateKey] = $offlineCompoundSum;
+ $compoundSalesData['online'][$dateKey] = $onlineCompoundSum;
+ }
+ //var_dump($compoundSalesData); die();
- }
- //var_dump($compoundSalesData); die();
+ $mnths = [];
+ foreach ($sales as $sale) {
+ $mnths[$sale['month']] = 1;
+ $types[$sale['type']] = 1;
+ }
+ $mnths = array_keys($mnths);
- $types = [];
- $mnths = [];
- foreach ($sales as $sale) {
- $mnths[$sale['month']] = 1;
- $types[$sale['type']] = 1;
- }
- $mnths = array_keys($mnths);
+ sort($mnths);
+ $countMnths = count($mnths);
+ $weights = [];
+ foreach ($mnths as $ind => $month) {
+ $weights[$month] = $ind + 1;
+ }
- sort($mnths);
- $countMnths = count($mnths);
- $weights = [];
- foreach ($mnths as $ind => $month) {
- $weights[$month] = $ind + 1;
- }
- $table = [];
- foreach ($sales as $sale) {
- $table[$sale['store_id']][$sale['type']] = ($table[$sale['store_id']][$sale['type']] ?? 0) + $weights[$sale['month']] * $sale['total'] / $countMnths;
- }
- 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] ?? 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;
+ foreach ($sales as $sale) {
+ $table[$sale['store_id']][$sale['type']] = ($table[$sale['store_id']][$sale['type']] ?? 0) + $weights[$sale['month']] * $sale['total'] / $countMnths;
}
- }
- // var_dump($table); die();
- /////////////////////////////////////////////////////////////////////////////////////
- $salesOnline = 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",
- "p1cn.category 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')
- ->leftJoin('products_1c_nomenclature p1cn', 'p1cn.id = sp.product_id')
- ->where(['between', 's.date', $date_start, $date_end])
- ->andWhere(['not in', 'order_id', ['', '0']])
- ->andWhere(['s.store_id' => $model->store_id])
- ->andWhere(['p1c.components' => ''])
- ->andWhere(['not in', 'p1cn.category', ['', 'букет', 'сборка', 'сервис']])
- ->groupBy([
- 's.store_id',
- "TO_CHAR(s.date, 'YYYY-MM')",
- "p1cn.category"
+ 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] ?? 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;
+ }
+ }
+ // var_dump($table); die();
+ /////////////////////////////////////////////////////////////////////////////////////
+ $salesOnline = 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",
+ "p1cn.category as type",
+ "TO_CHAR(s.date, 'YYYY-MM') as month"
])
- ->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"
+ ->leftJoin('sales_products sp', 's.id = sp.check_id')
+ ->leftJoin('products_1c p1c', 'p1c.id = sp.product_id')
+ ->leftJoin('products_1c_nomenclature p1cn', 'p1cn.id = sp.product_id')
+ ->where(['between', 's.date', $date_start, $date_end])
+ ->andWhere(['not in', 'order_id', ['', '0']])
+ ->andWhere(['s.store_id' => $model->store_id])
+ ->andWhere(['p1c.components' => ''])
+ ->andWhere(['not in', 'p1cn.category', ['', 'букет', 'сборка', 'сервис']])
+ ->groupBy([
+ 's.store_id',
+ "TO_CHAR(s.date, 'YYYY-MM')",
+ "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"
])
- ->orderBy(['month' => SORT_ASC, 'type' => SORT_ASC])
- ->asArray()
- ->all();
-
-
- $tableOnline = [];
- foreach ($salesOnline as $sale) {
- $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;
- }
+ ->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();
+
+
+ foreach ($salesOnline as $sale) {
+ $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;
+ 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(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(['p1c.components' => ''])
- ->andWhere(['not in', 'p1cn.category', ['', 'букет', 'сборка', 'сервис']])
- ->andWhere(['wo.store_id' => $store_id])
- ->groupBy([
- "month",
- 'ex.entity_id',
- "p1cn.category"
+ //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(wop.summ) as total',
+ "p1cn.category as p1ctype",
+ 'ex.entity_id AS store_id',
+ "TO_CHAR(wo.date, 'YYYY-MM') as month"
])
- ->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"
+ ->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.components' => ''])
+ ->andWhere(['not in', 'p1cn.category', ['', 'букет', 'сборка', 'сервис']])
+ ->andWhere(['wo.store_id' => $store_id])
+ ->groupBy([
+ "month",
+ '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"
])
- ->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;
- }
+ ->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['store_id']][$writeoff['p1ctype']] = ($tableWriteOffs[$writeoff['store_id']][$writeoff['p1ctype']] ?? 0) + $weights[$writeoff['month']] * $writeoff['total'] / $countMnths;
- }
+ foreach ($writeOffs as $writeoff) {
+ $types[$writeoff['p1ctype']] = 1;
+ $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 ($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;
+ 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);
- /////////////////////////////////////////////////////////////////////////////////////
-
- foreach ($types as $type) {
- if (!empty($type) && !isset($categoryPlan[$type])) {
- $categoryPlanNew = new CategoryPlan;
- $categoryPlanNew->year = $model->year;
- $categoryPlanNew->month = $model->month;
- $categoryPlanNew->store_id = $model->store_id;
- $categoryPlanNew->category = $type;
- $categoryPlanNew->offline = $table[$model->store_id][$type] ?? 0;
- $categoryPlanNew->internet_shop = $tableOnline[$model->store_id][$type] ?? 0;
- $categoryPlanNew->marketplace = 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;
- $categoryPlanNew->updated_by = Yii::$app->user->id;
- $categoryPlanNew->save();
- if ($categoryPlanNew->getErrors()) {
- throw new Exception(Json::encode($categoryPlanNew->getErrors()));
+ $types = array_keys($types);
+ /////////////////////////////////////////////////////////////////////////////////////
+
+ foreach ($types as $type) {
+ if (!empty($type) && !isset($categoryPlan[$type])) {
+ $categoryPlanNew = new CategoryPlan;
+ $categoryPlanNew->year = $model->year;
+ $categoryPlanNew->month = $model->month;
+ $categoryPlanNew->store_id = $model->store_id;
+ $categoryPlanNew->category = $type;
+ $categoryPlanNew->offline = $table[$model->store_id][$type] ?? 0;
+ $categoryPlanNew->internet_shop = $tableOnline[$model->store_id][$type] ?? 0;
+ $categoryPlanNew->marketplace = 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;
+ $categoryPlanNew->updated_by = Yii::$app->user->id;
+ $categoryPlanNew->save();
+ if ($categoryPlanNew->getErrors()) {
+ throw new Exception(Json::encode($categoryPlanNew->getErrors()));
+ }
}
}
- }
- $categoryPlan = CategoryPlan::find()->where(['year' => $model->year, 'month' => $model->month, 'store_id' => $model->store_id])->indexBy('category')->asArray()->all();
+ $categoryPlan = CategoryPlan::find()->where(['year' => $model->year, 'month' => $model->month, 'store_id' => $model->store_id])->indexBy('category')->asArray()->all();
- $salesWriteOffsPlan = SalesWriteOffsPlan::find()->where(['year' => $model->year, 'month' => $model->month, 'store_id' => $model->store_id])->one();
- $years = [];
- for ($i = 3; $i >= 0; $i--) {
- $year = date("Y") - $i;
- $years [$year] = $year;
+ $order = [
+ 'Срезка' => 1,
+ 'Сухоцветы' => 2,
+ 'Горшечные_растения' => 3,
+ 'Сопутствующие_товары' => 4,
+ 'Упаковка' => 5,
+ 'Матрица' => 6,
+ ];
+
+ usort($types, function ($a, $b) use ($order) {
+ return ($order[$a] ?? 999) <=> ($order[$b] ?? 999);
+ });
+
}
- $stores = ArrayHelper::map(CityStore::find()->andWhere(['visible' => '1'])->all(), 'id', 'name');
- $order = [
- 'Срезка' => 1,
- 'Сухоцветы' => 2,
- 'Горшечные_растения' => 3,
- 'Сопутствующие_товары' => 4,
- 'Упаковка' => 5,
- 'Матрица' => 6,
- ];
-
- usort($types, function ($a, $b) use ($order) {
- return ($order[$a] ?? 999) <=> ($order[$b] ?? 999);
- });
return $this->render('index', compact('model', 'years', 'stores', 'table', 'tableOnline',
'tableWriteOffs', 'types', 'salesWriteOffsPlan', 'isEditable', 'categoryPlan'));