use yii_app\records\StoreDynamic;
use yii_app\records\WriteOffs;
use yii_app\records\WriteOffsErp;
+use yii_app\services\AutoPlannogrammaService;
use yii_app\services\StorePlanService;
class CategoryPlanController extends Controller {
]);
$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')));
$currentDate = new \DateTime();
- $startDate = (clone $currentDate)->modify('-4 months')->modify('first day of this month');
+ $startDate = (clone $currentDate)->modify('-3 months')->modify('first day of this month');
$endDate = (clone $startDate)->modify('+2 months')->modify('last day of this month');
"COUNT(*) as cnt",
"SUM(CASE WHEN operation='Продажа' THEN s.summ ELSE (CASE WHEN operation='Возврат' THEN -s.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')
->leftJoin('products_1c_nomenclature p1cn', 'p1cn.id = sp.product_id')
->where(['between', 's.date', $date_start, $date_end])
->andWhere(['order_id' => ['', '0']])
- ->andWhere(['not in', 'p1cn.category', ['', 'сборка', 'сервис']])
+ ->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')",
- new \yii\db\Expression("
- CASE
- WHEN p1c.type = 'Матрица' THEN 'Матрица'
- ELSE p1cn.category
- END
- ")
+ "p1cn.category"
+ ])
+ ->orderBy(['month' => SORT_ASC, 'type' => SORT_ASC])
+ ->asArray()
+ ->all();
+
+ $salesMatrix = 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",
+ "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);
+
+ $compoundSalesData['offline'][$dateKey] = $offlineCompoundSum;
+ $compoundSalesData['online'][$dateKey] = $onlineCompoundSum;
+
+
+
+ }
+ //var_dump($compoundSalesData); die();
+
+
$types = [];
$mnths = [];
foreach ($sales as $sale) {
}
$mnths = array_keys($mnths);
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'] / 6;
+ $table[$sale['store_id']][$sale['type']] = ($table[$sale['store_id']][$sale['type']] ?? 0) + $weights[$sale['month']] * $sale['total'] / $countMnths;
+ }
+ foreach ($salesMatrix as $saleMatrix) {
+ $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];
+ foreach ($categories as $item) {
+ $store = $item['store_id'];
+ $type = $item['category'];
+ $sum = (float)$item['sum'];
+ $table[$store][$type] = ($table[$store][$type] ?? 0)
+ + $w * $sum / $countMnths;
+ }
+ }
+ // var_dump($table); die();
/////////////////////////////////////////////////////////////////////////////////////
$salesOnline = Sales::find()->alias('s')->select([
"COUNT(*) as cnt",
->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'] / 6;
+ $tableOnline[$sale['store_id']][$sale['type']] = ($tableOnline[$sale['store_id']][$sale['type']] ?? 0) + $weights[$sale['month']] * $sale['total'] / 3;
}
/////////////////////////////////////////////////////////////////////////////////////
$eit = ExportImportTable::find()->where(['entity' => 'city_store', 'export_id' => 1, 'entity_id' => $model->store_id])->one();
$tableWriteOffs = [];
foreach ($writeOffs as $writeoff) {
$types[$writeoff['p1ctype']] = 1;
- $tableWriteOffs[$writeoff['p1ctype']] = ($tableWriteOffs[$writeoff['p1ctype']] ?? 0) + $weights[$writeoff['month']] * $writeoff['total'] / 6;
+ $tableWriteOffs[$writeoff['p1ctype']] = ($tableWriteOffs[$writeoff['p1ctype']] ?? 0) + $weights[$writeoff['month']] * $writeoff['total'] / 3;
}
$types = array_keys($types);
/////////////////////////////////////////////////////////////////////////////////////
's.id',
's.date',
's.operation',
+ 's.order_id',
'sp.product_id',
'p1c.type',
'p1c.components',
'quantity' => $qty,
'type' => $type,
'operation' => $cp['operation'] ?? '',
+ 'order_id' => $cp['order_id'] ?? '',
];
}
}
$priceDynamics = PricesDynamic::find()
->andWhere(['region_id' => $region])
->andWhere(['product_id' => array_values(ArrayHelper::getColumn($nomenclatures, 'id'))])
- ->andWhere(['<=', 'date_from', $monthEnd])
- ->andWhere(['>=', 'date_to', $monthStart])
+ //->andWhere(['<=', 'date_from', $monthEnd])
+ // ->andWhere(['>=', 'date_to', $monthStart])
->orderBy(['date_from' => SORT_DESC])
->all();
$guid = $componentDataRecord['component_guid'];
$nomenclatureData = $nomenclatures[$guid] ?? null;
$productId = $nomenclatureData?->id;
- $price = 0;
+ $price = null;
$dailyPrices = [];
foreach ($pricesByProduct[$productId] ?? [] as $priceRecordForProduct) {
if (defined($heliumConstant) && $productId == constant($heliumConstant)) {
}
if (!empty($dailyPrices)) {
$price = min($dailyPrices);
- } else {
+ }
+
+ if ($price === null) {
$price = 0.0;
}
$cost = $componentDataRecord['quantity'] * $price * $componentDataRecord['quantity_product'];
'component_cost' => $costComponent,
'type' => $type,
'operation' => $componentDataRecord['operation'],
+ 'order_id' => $componentDataRecord['order_id'],
];
}