public function getCostModel($region_id, $data = null, $force = false) {
$costModel = BouquetCompositionPrice::find()->where(['bouquet_id' => $this->id, 'region_id' => $region_id])->one();
if (!$costModel || $force) {
- $costModel = new BouquetCompositionPrice;
- $costModel->bouquet_id = $this->id;
- $costModel->region_id = $region_id;
+ if (!$costModel) {
+ $costModel = new BouquetCompositionPrice;
+ $costModel->bouquet_id = $this->id;
+ $costModel->region_id = $region_id;
+ }
$costModel->selfcost = $this->getSelfCost($data);
$costModel->selfcost_markup = 30;
$costModel->selfcost_markup_price = 0.3 * $costModel->selfcost;
$costModel->price = round(1.15 * ($costModel->selfcost_markup_price + $costModel->selfcost));
$costModel->price_markup = $costModel->selfcost > 0 ? 100 * ($costModel->price / ($costModel->selfcost_markup_price + $costModel->selfcost) - 1): 0;
$costModel->save();
+ if ($costModel->getErrors()) {
+ throw new Exception(Json::encode($costModel->getErrors()));
+ }
}
return $costModel;
}