protected function updatePrice($params) { $categories = PriceCategory::find()->all(); foreach ($categories as $category) { $price = Price::findOne(['id_product' => $params['id_product'], 'id_price_category' => $category->id_price_category]); if (!$price) { $price = new Price(); $price->setAttributes(['id_product' => $params['id_product'], 'id_price_category' => $category->id_price_category, 'id_uom' => $params['id_uom'], 'price' => 0]); } if ($price->canSetProperty('logParams')) { $logParams = []; foreach (['app', 'id_ref'] as $key) { if (isset($params[$key]) || array_key_exists($key, $params)) { $logParams[$key] = $params[$key]; } } $price->logParams = $logParams; } $price->price = $this->executePriceFormula($category->formula, $params['price']); if (!$price->save()) { throw new UserException(implode(",\n", $price->firstErrors)); } } return true; }
/** * Creates a new Price model. * If creation is successful, the browser will be redirected to the 'view' page. * @return mixed */ public function actionCreate() { $model = new Price(); if ($model->load(Yii::$app->request->post()) && $model->save()) { return $this->redirect(['view', 'id_product' => $model->id_product, 'id_price_category' => $model->id_price_category]); } else { return $this->render('create', ['model' => $model]); } }
public static function updatePrice($params, $logs = []) { $categories = PriceCategory::find()->all(); foreach ($categories as $category) { $price = Price::findOne(['id_product' => $params['id_product'], 'id_price_category' => $category->id_price_category]); if (!$price) { $price = new Price(); $price->setAttributes(['id_product' => $params['id_product'], 'id_price_category' => $category->id_price_category, 'id_uom' => $params['id_uom'], 'price' => 0]); } if (!empty($logs) && $price->canSetProperty('logParams')) { $price->logParams = $logs; } $price->price = self::executePriceFormula($category->formula, $params['price']); if (!$price->save()) { throw new UserException(implode(",\n", $price->firstErrors)); } } return true; }