//var_dump($filters); die();
$service = new AutoPlannogrammaService();
$data = $service->calculateSpeciesForecastForProductsWithoutHistory($filters['plan_date'], $filters);
- $goals = $service->calculateFullGoalChain($filters);
+ //$goals = $service->calculateFullGoalChain($filters);
+ $monthCategoryShare = $service->getMonthCategoryShareOrWriteOff($filters['plan_date'], $filters);
+ $monthCategoryGoal = $service->getMonthCategoryGoal($monthCategoryShare, $filters['plan_date']);
+ $monthSubcategoryShare = $service->getMonthSubcategoryShareOrWriteOff($filters['plan_date'], $filters);
+ $monthSubcategoryGoal = $service->getMonthSubcategoryGoal($monthSubcategoryShare, $monthCategoryGoal);
+ $monthSpeciesShare = $service->getMonthSpeciesShareOrWriteOff($filters['plan_date'], $filters);
+ $goals = $service->getMonthSpeciesGoalDirty($monthSpeciesShare, $monthSubcategoryGoal);
+
+ if ($filters['type'] == AutoPlannogrammaService::TYPE_WRITE_OFFS) {
+ $monthCategoryWriteOffsShare = $service->getMonthCategoryShareOrWriteOff($filters['plan_date'], $filters, $filters['type']);
+ $monthCategoryWriteOffsGoal = $service->getMonthCategoryGoal($monthCategoryWriteOffsShare, $filters['plan_date'], $filters['type']);
+ $monthSubcategoryWriteOffsShare = $service->getMonthSubcategoryShareOrWriteOff($filters['plan_date'], $filters, $filters['type']);
+ $monthSubcategoryWriteOffsGoals = $service->getMonthSubcategoryGoal($monthSubcategoryWriteOffsShare, $monthCategoryWriteOffsGoal, $filters['type']);
+ $monthSpeciesWriteOffShare = $service->getMonthSpeciesShareOrWriteOff($filters['plan_date'], $filters, $filters['type']);
+ $goals = $service->getMonthSpeciesGoalDirty($monthSpeciesWriteOffShare, $monthSubcategoryWriteOffsGoals, $filters['type'], $data);
+ }
+
+
$result = StorePlanService::calculateHistoricalShare(
$filters['store_id'],
$filters['month'],
$result['with_history']
);
+ $productSalesForecast = $service->calculateProductForecastInPieces(
+ $filters['store_id'],
+ $filters['month'],
+ $filters['category'],
+ $filters['subcategory'],
+ $filters['species'],
+ $productSalesShare,
+ $goals
+ );
-
- var_dump($productSalesShare); die();
+ var_dump($productSalesForecast); die();
return $result;
}
+ public static function calculateProductForecastInPieces(
+ int $storeId,
+ string $month,
+ string $category,
+ string $subcategory,
+ string $species,
+ array $productSalesShare,
+ array $speciesGoals
+ ): array {
+ $result = [];
+
+ $goal = null;
+ foreach ($speciesGoals as $item) {
+ if (
+ $item['store_id'] == $storeId &&
+ $item['category'] == $category &&
+ $item['subcategory'] == $subcategory &&
+ $item['species'] == $species
+ ) {
+ $goal = $item['goal'];
+ break;
+ }
+ }
+
+ if ($goal === null) {
+ return [];
+ }
+
+ $region = CityStoreParams::find()
+ ->where(['store_id' => $storeId])
+ ->select('address_region')
+ ->scalar();
+
+ if (!$region) {
+ $cityId = CityStore::find()->select('city_id')->where(['id' => $storeId])->scalar();
+ $region = ($cityId == 1)
+ ? BouquetComposition::REGION_MSK
+ : BouquetComposition::REGION_NN;
+ }
+
+ foreach ($productSalesShare as $productId => $data) {
+ $share = $data['share'] ?? 0.0;
+
+ if ($share <= 0.0) {
+ continue;
+ }
+
+ $priceRecord = PricesDynamic::find()
+ ->where([
+ 'product_id' => $productId,
+ 'region_id' => $region,
+ 'active' => 1,
+ ])
+ ->one();
+
+ if (!$priceRecord || $priceRecord->price <= 0) {
+ continue;
+ }
+
+ $forecastSum = $goal * $share;
+ $forecastCount = $forecastSum / $priceRecord->price;
+
+ $result[] = [
+ 'product_id' => $productId,
+ 'goal_share' => round($forecastSum, 2),
+ 'price' => $priceRecord->price,
+ 'forecast_pieces' => round($forecastCount),
+ ];
+ }
+
+ return $result;
+ }
+
}
\ No newline at end of file