/** * Creates a new Cogs model. * If creation is successful, the browser will be redirected to the 'view' page. * @return mixed */ public function actionCreate() { $model = new Cogs(); if ($model->load(Yii::$app->request->post()) && $model->save()) { return $this->redirect(['view', 'id' => $model->product_id]); } else { return $this->render('create', ['model' => $model]); } }
protected function updateCogs($params) { $cogs = Cogs::findOne(['product_id' => $params['id']]); if (!$cogs) { $cogs = new Cogs(['product_id' => $params['id'], 'cogs' => 0.0]); } $current_stock = ProductStock::find()->where(['product_id' => $params['id']])->sum('qty'); if ($current_stock != 0) { $cogs->cogs += $params['qty'] * ($params['cogs'] - $cogs->cogs) / $current_stock; } else { $cogs->cogs = 0; } return $cogs->save(false); }