$monthStart = date("Y-m-d 00:00:00", strtotime($year . '-' . $month . '-1'));
$monthEnd = date("Y-m-t 23:59:59", strtotime($year . '-' . $month . '-1'));
+ // Ищем каталог-гуиды категории services и services_delivery
+ $productsClass = ProductsClass::find()->select(['category_id', 'tip'])
+ ->where(['tip' => ['services', 'services_delivery']])
+ ->indexBy('category_id')
+ ->asArray()->all();
+
+ // Ищем продуктовые гуиды по каталог-гуидам категории services и services_delivery
+ $products1c = Products1c::find()->select(['id', 'parent_id', 'name'])
+ ->where(['parent_id' => array_keys($productsClass), 'tip' => 'products'])
+ ->indexBy(['id'])
+ ->asArray()->all();
+
foreach (range(1, 5) as $ind) {
$weekStart = date("Y-m-d 00:00:00", strtotime("+" . (($ind - 1) * 7) . ' days', strtotime($monthStart)));
$weekEnd = date("Y-m-d 23:59:59", strtotime("+" . ($ind * 7 - 1) . ' days', strtotime($monthStart)));
$returnSalesIds = ArrayHelper::getColumn($returnSales, 'sales_check');
// Offline sales
- $salesOffline = Sales::find()->select(['SUM(summ) as total'])
- ->where(['between', 'date', $weekStart, $weekEnd])
- ->andWhere(['order_id' => ['', '0']])
- ->andWhere(['store_id' => $store_id])
- ->andWhere(['operation' => Sales::OPERATION_SALE])
- ->andWhere(['NOT IN', 'id', $returnSalesIds])
+ $salesOffline = Sales::find()->alias('s')->select(['SUM(p.summ) as total'])
+ ->rightJoin('sales_products p', 's.id = p.check_id')
+ ->where(['between', 's.date', $weekStart, $weekEnd])
+ ->andWhere(['s.order_id' => ['', '0']])
+ ->andWhere(['s.store_id' => $store_id])
+ ->andWhere(['s.operation' => Sales::OPERATION_SALE])
+ ->andWhere(['NOT IN', 'p.product_id', array_keys($products1c)])
+ ->andWhere(['NOT IN', 's.id', $returnSalesIds])
->asArray()->one();
// Online sales
- $salesOnline = Sales::find()->select(['SUM(summ) as total'])
- ->where(['between', 'date', $weekStart, $weekEnd])
- ->andWhere(['NOT IN', 'order_id', ['', '0']])
- ->andWhere(['store_id' => $store_id])
- ->andWhere(['operation' => Sales::OPERATION_SALE])
- ->andWhere(['NOT IN', 'id', $returnSalesIds])
+ $salesOnline = Sales::find()->alias('s')->select(['SUM(p.summ) as total'])
+ ->rightJoin('sales_products p', 's.id = p.check_id')
+ ->where(['between', 's.date', $weekStart, $weekEnd])
+ ->andWhere(['NOT IN', 's.order_id', ['', '0']])
+ ->andWhere(['s.store_id' => $store_id])
+ ->andWhere(['s.operation' => Sales::OPERATION_SALE])
+ ->andWhere(['NOT IN', 'p.product_id', array_keys($products1c)])
+ ->andWhere(['NOT IN', 's.id', $returnSalesIds])
->asArray()->one();
$motivation = Motivation::find()->where(['store_id' => $store_id, 'year' => $year, 'month' => $month])->one();
$monthStart = date("Y-m-d 00:00:00", strtotime($year . '-' . $month . '-1'));
$monthEnd = date("Y-m-t 23:59:59", strtotime($year . '-' . $month . '-1'));
+ // Ищем каталог-гуиды категории services и services_delivery
+ $productsClass = ProductsClass::find()->select(['category_id', 'tip'])
+ ->where(['tip' => ['services', 'services_delivery']])
+ ->indexBy('category_id')
+ ->asArray()->all();
+
+ // Ищем продуктовые гуиды по каталог-гуидам категории services и services_delivery
+ $products1c = Products1c::find()->select(['id', 'parent_id', 'name'])
+ ->where(['parent_id' => array_keys($productsClass), 'tip' => 'products'])
+ ->indexBy(['id'])
+ ->asArray()->all();
+
$sales = Sales::find()->where(['between', 'date', $monthStart, $monthEnd])
->andWhere(['operation' => Sales::OPERATION_SALE])
->asArray()->all();
$returnSales = Sales::find()->where(['operation' => Sales::OPERATION_RETURN, 'sales_check' => $salesIds])->all();
$returnSalesIds = ArrayHelper::getColumn($returnSales, 'sales_check');
- $salesOffline = Sales::find()->select(['SUM(summ) as total', 'store_id'])
- ->where(['between', 'date', $monthStart, $monthEnd])
- ->andWhere(['operation' => Sales::OPERATION_SALE])
- ->andWhere(['order_id' => ['', '0']])
- ->andWhere(['NOT IN', 'id', $returnSalesIds])
- ->groupBy(['store_id'])
+ $salesOffline = Sales::find()->alias('s')->select(['SUM(p.summ) as total', 's.store_id'])
+ ->rightJoin('sales_products p', 's.id = p.check_id')
+ ->where(['between', 's.date', $monthStart, $monthEnd])
+ ->andWhere(['s.operation' => Sales::OPERATION_SALE])
+ ->andWhere(['s.order_id' => ['', '0']])
+ ->andWhere(['NOT IN', 'p.product_id', array_keys($products1c)])
+ ->andWhere(['NOT IN', 's.id', $returnSalesIds])
+ ->groupBy(['s.store_id'])
->indexBy('store_id')
->asArray()->all();
- $salesOnline = Sales::find()->select(['SUM(summ) as total', 'store_id'])
- ->where(['between', 'date', $monthStart, $monthEnd])
- ->andWhere(['operation' => Sales::OPERATION_SALE])
- ->andWhere(['NOT IN', 'order_id', ['', '0']])
- ->andWhere(['NOT IN', 'id', $returnSalesIds])
- ->groupBy(['store_id'])
+ $salesOnline = Sales::find()->alias('s')->select(['SUM(p.summ) as total', 's.store_id'])
+ ->rightJoin('sales_products p', 's.id = p.check_id')
+ ->where(['between', 's.date', $monthStart, $monthEnd])
+ ->andWhere(['s.operation' => Sales::OPERATION_SALE])
+ ->andWhere(['NOT IN', 's.order_id', ['', '0']])
+ ->andWhere(['NOT IN', 'p.product_id', array_keys($products1c)])
+ ->andWhere(['NOT IN', 's.id', $returnSalesIds])
+ ->groupBy(['s.store_id'])
->indexBy('store_id')
->asArray()->all();
$motivations = Motivation::find()->where(['year' => $year, 'month' => $month])->indexBy('store_id')->all();
- $motivationValueGroupFact = MotivationValueGroup::find()->where(['alias' => 'fact'])->one();
- $motivationCostsItemOffline = MotivationCostsItem::find()->where(['name' => 'Оффлайн продажи'])->one();
- $motivationCostsItemOnline = MotivationCostsItem::find()->where(['name' => 'Онлайн продажи'])->one();
- /** @var $motivationCostsItemOffline MotivationCostsItem */
- /** @var $motivationCostsItemOnline MotivationCostsItem */
-
-
$storeIds = ArrayHelper::getColumn(CityStore::find()->where(['visible' => '1'])->all(), 'id');
foreach ($storeIds as $store_id) {
if (isset($motivations[$store_id])) {
- $correction = self::getMotivationValue($motivations[$store_id]->id, 8, $motivationCostsItemOffline->code);
-
- $motivationValueOffline = MotivationValue::find()->where(['motivation_id' => $motivations[$store_id]->id,
- 'motivation_group_id' => $motivationValueGroupFact->id, 'value_id' => $motivationCostsItemOffline->code])->one();
- if (!$motivationValueOffline) {
- $motivationValueOffline = new MotivationValue;
- $motivationValueOffline->motivation_id = $motivations[$store_id]->id;
- $motivationValueOffline->motivation_group_id = $motivationValueGroupFact->id;
- $motivationValueOffline->value_id = $motivationCostsItemOffline->code;
- $motivationValueOffline->value_type = $motivationCostsItemOffline->data_type;
- }
- $motivationValueOffline->value_float = ($salesOffline[$store_id]['total'] ?? 0) + $correction;
- $motivationValueOffline->save();
- if ($motivationValueOffline->getErrors()) {
- throw new \Exception(Json::encode($motivationValueOffline->getErrors()));
- }
+ $correction = self::getMotivationValue($motivations[$store_id]->id, 8, self::CODE_OFFLINE_SALES);
- $correction = self::getMotivationValue($motivations[$store_id]->id, 8, $motivationCostsItemOnline->code);
+ self::saveOrUpdateMotivationValue($motivations[$store_id]->id, "fact",
+ self::CODE_OFFLINE_SALES, 'float', ($salesOffline[$store_id]['total'] ?? 0) + $correction);
- $motivationValueOnline = MotivationValue::find()->where(['motivation_id' => $motivations[$store_id]->id,
- 'motivation_group_id' => $motivationValueGroupFact->id, 'value_id' => $motivationCostsItemOnline->code])->one();
- if (!$motivationValueOnline) {
- $motivationValueOnline = new MotivationValue;
- $motivationValueOnline->motivation_id = $motivations[$store_id]->id;
- $motivationValueOnline->motivation_group_id = $motivationValueGroupFact->id;
- $motivationValueOnline->value_id = $motivationCostsItemOnline->code;
- $motivationValueOnline->value_type = $motivationCostsItemOnline->data_type;
- }
- $motivationValueOnline->value_float = ($salesOnline[$store_id]['total'] ?? 0) + $correction;
- $motivationValueOnline->save();
- if ($motivationValueOnline->getErrors()) {
- throw new \Exception(Json::encode($motivationValueOnline->getErrors()));
- }
+ $correction = self::getMotivationValue($motivations[$store_id]->id, 8, self::CODE_ONLINE_SALES);
+
+ self::saveOrUpdateMotivationValue($motivations[$store_id]->id, "fact",
+ self::CODE_ONLINE_SALES, 'float', ($salesOnline[$store_id]['total'] ?? 0) + $correction);
}
}
}