(clone $base)->modify('-1 year')->format('Y-m-t 23:59:59'), // год назад, конец месяца
],
];
- //var_dump($months);die();
+
$query = (new Query())
->select([
'store_id' => 'main.ex_entity_id',
$productTableJoin = $type === self::TYPE_WRITE_OFFS ? ['wp' => 'write_offs_products'] : ['sp' => 'sales_products'];
$productTableJoinCondition = $type === self::TYPE_WRITE_OFFS ? 'wp.write_offs_id = w.id' : 'sp.check_id = s.id';
+ $field = $type === self::TYPE_WRITE_OFFS ? 'w.date' : 's.date';
+ $base = new \DateTime($dateFrom);
$months = [
[
'between',
- $type === self::TYPE_WRITE_OFFS ? 'w.date' : 's.date',
- (new \DateTime($dateFrom))->modify('-1 year')->format('Y-m-01'),
- (new \DateTime($dateFrom))->format('Y-m-t'),
+ $field,
+ (clone $base)->modify('-2 year')->format('Y-m-01 00:00:00'), // два года назад, начало месяца
+ (clone $base)->modify('-2 year')->format('Y-m-t 23:59:59'), // два года назад, конец месяца
],
[
'between',
- $type === self::TYPE_WRITE_OFFS ? 'w.date' : 's.date',
- (new \DateTime($dateFrom))->modify('-1 year')->format('Y-m-01'),
- (new \DateTime($dateFrom))->format('Y-m-t'),
+ $field,
+ (clone $base)->modify('-1 year')->format('Y-m-01 00:00:00'), // год назад, начало месяца
+ (clone $base)->modify('-1 year')->format('Y-m-t 23:59:59'), // год назад, конец месяца
],
];
'total_sum' => 'main.total_sum',
'percent' => new Expression('ROUND(CAST(main.total_sum AS DECIMAL) / NULLIF(totals.total, 0), 4)'),
'type' => new Expression(':type', ['type' => $type]),
+ 'totals_total' => 'totals.total',
])
->from([
'main' => (new Query())
->leftJoin($productTableJoin, $productTableJoinCondition)
->leftJoin('products_1c_nomenclature p1c', "p1c.id = $productJoinCondition")
->leftJoin('export_import_table ex', $storeJoinCondition)
+ ->leftJoin('products_1c p1', "p1.id = $productJoinCondition")
->andWhere(['ex.entity_id' => $storeIds])
- ->andWhere(['<>', 'p1c.species', ''])
+ ->andWhere(['p1.components' => ''])
+ ->andWhere(['not in', 'p1c.category', ['', 'букет', 'сборка', 'сервис']])
->andWhere(['or', ...$months])
->groupBy(['ex.entity_id', 'p1c.category', 'p1c.subcategory', 'p1c.species']),
])
->leftJoin($productTableJoin, $productTableJoinCondition)
->leftJoin('products_1c_nomenclature p1c', "p1c.id = $productJoinCondition")
->leftJoin('export_import_table ex', $storeJoinCondition)
+ ->leftJoin('products_1c p1', "p1.id = $productJoinCondition")
->andWhere(['ex.entity_id' => $storeIds])
- ->andWhere(['<>', 'p1c.species', ''])
+ ->andWhere(['p1.components' => ''])
+ ->andWhere(['not in', 'p1c.category', ['', 'букет', 'сборка', 'сервис']])
->andWhere(['or', ...$months])
->groupBy(['ex.entity_id', 'p1c.category', 'p1c.subcategory'])],
'main.ex_entity_id = totals.store_id
$percent = $this->adjustWriteOffPercent($percent, $salesPercents[$key] ?? 0);
}
+
$result[] = [
'store_id' => $row['store_id'],
'category' => $row['category'],
'subcategory' => $row['subcategory'],
'species' => $row['species'],
'total_sum' => $row['total_sum'],
+ 'totals_total' => $row['totals_total'],
'percent' => $percent,
'type' => $row['type'],
];
}
- return $result;
+ $periods = [
+ (clone $base)->modify('-2 year'),
+ (clone $base)->modify('-1 year'),
+ ];
+
+ $finalResult = [];
+ $componentAdds = [];
+ $componentAddsSumAll = [];
+ $allComponentsProdIds = [];
+
+ foreach ($storeIds as $sid) {
+ foreach ($periods as $dt) {
+ $items = $this->getProductsComponentsInCategory(
+ $sid,
+ (int)$dt->format('n'),
+ (int)$dt->format('Y'),
+ $type
+ );
+ if (empty($items)) {
+ continue;
+ }
+
+ foreach ($items as $it) {
+ $allComponentsProdIds[$sid][] = $it['product_id'];
+ }
+
+
+ $sums = $this->sumProductsComponentsByGroup($items, $type, 'species');
+ $allComponentsProdIds[$sid] = array_unique($allComponentsProdIds[$sid]);
+
+ foreach ($sums as $sr) {
+ $cat = $sr['category'];
+ $subcat = $sr['subcategory'];
+ $spec = $sr['species'];
+ $val = $sr['sum'];
+
+ $componentAdds[$sid][$cat][$subcat][$spec] =
+ ($componentAdds[$sid][$cat][$subcat][$spec] ?? 0) + $val;
+
+ $componentAddsSumAll[$sid][$cat][$subcat] =
+ ($componentAddsSumAll[$sid][$cat][$subcat] ?? 0) + $val;
+ }
+ }
+ }
+
+
+ foreach ($result as $r) {
+ $sid = $r['store_id'];
+ $cat = $r['category'];
+ $subcat = $r['subcategory'];
+ $spec = $r['species'];
+
+ $specTotal = (float)$r['total_sum'];
+ $subcatTotal = (float)$r['totals_total'];
+
+
+ $addSubcat = $componentAddsSumAll[$sid][$cat][$subcat] ?? 0.0;
+ $addSpecies = $componentAdds[$sid][$cat][$subcat][$spec] ?? 0.0;
+
+
+ $newSubcatTotal = $subcatTotal + $addSubcat;
+ $newSpecTotal = $specTotal + $addSpecies;
+
+
+ $percent = $newSubcatTotal > 0
+ ? round($newSpecTotal / $newSubcatTotal, 4)
+ : 0.0;
+
+ $finalResult[$sid][] = [
+ 'category' => $cat,
+ 'subcategory' => $subcat,
+ 'species' => $spec ,
+ 'total_sum' => $newSpecTotal,
+ 'percent' => $percent,
+ 'type' => $type,
+ 'new_total_store' => $newSubcatTotal,
+ 'base_total_store' => $subcatTotal,
+ 'products_components_list' => implode(',', $allComponentsProdIds[$sid] ?? []),
+ ];
+ }
+
+ return $finalResult;
}
/**