1 | <?php |
||
44 | class PlanController extends CrudController |
||
45 | { |
||
46 | /** |
||
47 | * @var PriceModelFactory |
||
48 | */ |
||
49 | public $priceModelFactory; |
||
50 | |||
51 | /** |
||
52 | * PlanController constructor. |
||
53 | * @param string $id |
||
54 | * @param Module $module |
||
55 | * @param PriceModelFactory $priceModelFactory |
||
56 | * @param array $config |
||
57 | */ |
||
58 | public function __construct(string $id, Module $module, PriceModelFactory $priceModelFactory, array $config = []) |
||
59 | { |
||
60 | parent::__construct($id, $module, $config); |
||
61 | |||
62 | $this->priceModelFactory = $priceModelFactory; |
||
63 | } |
||
64 | |||
65 | public function behaviors() |
||
66 | { |
||
67 | return array_merge(parent::behaviors(), [ |
||
68 | [ |
||
69 | 'class' => EasyAccessControl::class, |
||
70 | 'actions' => [ |
||
71 | 'create' => 'plan.create', |
||
72 | 'update' => 'plan.update', |
||
73 | 'update-prices' => 'plan.update', |
||
74 | 'templates' => 'plan.create', |
||
75 | 'create-prices' => 'plan.create', |
||
76 | 'delete' => 'plan.delete', |
||
77 | '*' => 'plan.read', |
||
78 | ], |
||
79 | ], |
||
80 | ]); |
||
81 | } |
||
82 | |||
83 | public function actions() |
||
140 | |||
141 | public function actionCreatePrices(int $plan_id, int $template_plan_id) |
||
142 | { |
||
143 | $plan = $this->findTemplatePlan($plan_id, $plan_id, $template_plan_id); |
||
144 | |||
145 | $suggestions = (new Price())->batchQuery('suggest', [ |
||
146 | 'object_id' => $plan_id, |
||
147 | 'plan_id' => $plan_id, |
||
148 | 'template_plan_id' => $template_plan_id, |
||
149 | 'type' => $plan->type, |
||
150 | ]); |
||
151 | $this->populateWithPrices($plan, $suggestions); |
||
152 | |||
153 | $parentPrices = $this->getParentPrices($plan_id); |
||
154 | |||
155 | $targetPlan = Plan::findOne(['id' => $plan_id]); |
||
156 | |||
157 | $grouper = new PlanInternalsGrouper($plan); |
||
158 | [$plan->name, $plan->id] = [$targetPlan->name, $targetPlan->id]; |
||
159 | $action = ['@plan/update-prices', 'id' => $plan->id, 'scenario' => 'create']; |
||
160 | |||
161 | return $this->render($plan->type . '/' . 'createPrices', |
||
162 | compact('plan', 'grouper', 'parentPrices', 'action', 'plan_id')); |
||
163 | } |
||
164 | |||
165 | public function actionGetPlanHistory(int $plan_id, string $date) |
||
166 | { |
||
167 | $plan = Plan::find() |
||
168 | ->where(['id' => $plan_id]) |
||
169 | ->andWhere(['history_time' => $date]) |
||
170 | ->withSales() |
||
171 | ->withPriceHistory() |
||
172 | ->one(); |
||
173 | |||
174 | return PriceGridView::widget([ |
||
175 | 'boxed' => false, |
||
176 | 'showHeader' => true, |
||
177 | 'showFooter' => false, |
||
178 | 'summaryRenderer' => function (): string { |
||
179 | return ''; |
||
180 | }, |
||
181 | 'emptyText' => Yii::t('hipanel.finance.price', 'No prices found'), |
||
182 | 'dataProvider' => new ArrayDataProvider([ |
||
183 | 'allModels' => $plan->priceHistory, |
||
184 | 'pagination' => false, |
||
185 | ]), |
||
186 | 'columns' => [ |
||
187 | 'object->name', |
||
188 | 'type', |
||
189 | 'old_price', |
||
190 | 'note', |
||
191 | ], |
||
192 | ]); |
||
193 | } |
||
194 | |||
195 | public function actionSuggestPricesModal($id) |
||
206 | |||
207 | public function actionSuggestGroupingPricesModal($id) |
||
208 | { |
||
209 | /** @var Plan $plan */ |
||
210 | $plan = $this->findPlan($id); |
||
211 | $model = new PriceSuggestionRequestForm([ |
||
212 | 'plan_id' => $plan->id, |
||
213 | 'plan_type' => $plan->type, |
||
214 | 'object_id' => $plan->id, |
||
215 | 'scenario' => PriceSuggestionRequestForm::SCENARIO_PREDEFINED_OBJECT, |
||
216 | ]); |
||
217 | |||
218 | return $this->renderAjax('modals/suggestPrices', compact('plan', 'model')); |
||
219 | } |
||
220 | |||
221 | public function actionSuggestSharedPricesModal($id) |
||
222 | { |
||
223 | /** @var Plan $plan */ |
||
224 | $plan = $this->findPlan($id); |
||
225 | $model = new PriceSuggestionRequestForm([ |
||
226 | 'plan_id' => $plan->id, |
||
227 | 'plan_type' => $plan->type, |
||
228 | 'scenario' => PriceSuggestionRequestForm::SCENARIO_PREDEFINED_OBJECT, |
||
229 | ]); |
||
230 | |||
231 | return $this->renderAjax('modals/suggestPrices', compact('plan', 'model')); |
||
232 | } |
||
233 | |||
234 | private function findTemplatePlan(int $targetPlan, int $object_id, int $expectedTemplateId): Plan |
||
235 | { |
||
236 | $result = Plan::perform('search-templates', [ |
||
237 | 'id' => $targetPlan, |
||
238 | 'object_id' => $object_id, |
||
239 | ]); |
||
240 | $plans = ArrayHelper::index($result, 'id'); |
||
241 | |||
242 | if (!isset($plans[$expectedTemplateId])) { |
||
243 | throw new NotFoundHttpException('Requested template plan not found'); |
||
244 | } |
||
245 | |||
246 | $plan = Plan::instantiate($plans[$expectedTemplateId]); |
||
247 | Plan::populateRecord($plan, $plans[$expectedTemplateId]); |
||
248 | |||
249 | return $plan; |
||
250 | } |
||
251 | |||
252 | /** |
||
253 | * @param $id integer |
||
254 | * @throws NotFoundHttpException |
||
255 | * @return Plan|null |
||
256 | */ |
||
257 | private function findPlan(int $id): ?Plan |
||
258 | { |
||
259 | $plan = Plan::findOne(['id' => $id]); |
||
260 | if ($plan === null) { |
||
261 | throw new NotFoundHttpException('Not found'); |
||
262 | } |
||
263 | |||
264 | return $plan; |
||
265 | } |
||
266 | |||
267 | /** |
||
268 | * @param string $plan_id |
||
269 | * @param string|null $object_id Object ID or `null` |
||
270 | * when the desired templates are not related to a specific object |
||
271 | * @param string $name_ilike |
||
272 | * @return array |
||
273 | */ |
||
274 | public function actionTemplates($plan_id, $object_id = null, string $name_ilike = null) |
||
286 | |||
287 | public function actionCalculateCharges() |
||
288 | { |
||
289 | Yii::$app->response->format = Response::FORMAT_JSON; |
||
290 | $request = Yii::$app->request; |
||
291 | |||
292 | $periods = ['now', 'first day of +1 month', 'first day of +1 year']; |
||
293 | $calculations = Plan::perform('calculate-charges', [ |
||
294 | 'actions' => $request->post('actions'), |
||
295 | 'prices' => $request->post('prices'), |
||
296 | 'times' => $periods, |
||
297 | ]); |
||
298 | /** @var PriceChargesEstimator $calculator */ |
||
299 | $calculator = Yii::$container->get(PriceChargesEstimator::class, [$calculations]); |
||
300 | |||
301 | try { |
||
302 | return $calculator->calculateForPeriods($periods); |
||
303 | } catch (ResponseErrorException $exception) { |
||
304 | Yii::$app->response->setStatusCode(412, $exception->getMessage()); |
||
305 | |||
306 | return [ |
||
307 | 'formula' => $exception->getResponse()->getData()['_error_ops']['formula'] ?? null, |
||
308 | ]; |
||
309 | } |
||
310 | } |
||
311 | |||
312 | public function actionCalculateValues($planId) |
||
329 | |||
330 | public function actionUpdatePrices(int $id, string $scenario = 'update') |
||
331 | { |
||
332 | $plan = Plan::find() |
||
333 | ->byId($id) |
||
334 | ->withPrices() |
||
335 | ->one(); |
||
336 | |||
337 | $request = Yii::$app->request; |
||
338 | if ($request->isPost) { |
||
368 | |||
369 | /** |
||
370 | * @param int $plan_id |
||
371 | * @return Price[]|null Array of parent plan prices or `null`, when parent plan was not found |
||
372 | */ |
||
373 | private function getParentPrices(int $plan_id) |
||
387 | |||
388 | /** |
||
389 | * @param Plan $plan |
||
390 | * @param array $pricesData |
||
391 | */ |
||
392 | private function populateWithPrices(Plan $plan, $pricesData): void |
||
414 | } |
||
415 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: