protected function updateCogs($params) { $cogs = Cogs::findOne(['id_product' => $params['id_product']]); if (!$cogs) { $smallest_uom = Helper::getSmallestProductUom($params['id_product']); $cogs = new Cogs(); $cogs->setAttributes(['id_product' => $params['id_product'], 'id_uom' => $smallest_uom, 'cogs' => 0.0]); } $current_stock = Helper::getCurrentStockAll($params['id_product']); $qty_per_uom = Helper::getQtyProductUom($params['id_product'], $params['id_uom']); $added_stock = $params['added_stock'] * $qty_per_uom; $cogs->cogs = 1.0 * ($cogs->cogs * $current_stock + $params['price'] * $params['added_stock']) / ($current_stock + $added_stock); if ($cogs->canSetProperty('logParams')) { $cogs->logParams = ['app' => $params['app'], 'id_ref' => $params['id_ref']]; } if (!$cogs->save()) { throw new UserException(implode(",\n", $cogs->firstErrors)); } return true; }
public function search($params) { $query = CogsModel::find(); $dataProvider = new ActiveDataProvider(['query' => $query]); if (!($this->load($params) && $this->validate())) { return $dataProvider; } $query->andFilterWhere(['id_product' => $this->id_product, 'id_uom' => $this->id_uom, 'create_by' => $this->create_by, 'update_by' => $this->update_by]); $query->andFilterWhere(['like', 'cogs', $this->cogs])->andFilterWhere(['like', 'create_date', $this->create_date])->andFilterWhere(['like', 'update_date', $this->update_date]); return $dataProvider; }
/** * @return \yii\db\ActiveQuery */ public function getIdCogs() { return $this->hasOne(Cogs::className(), ['id_product' => 'id_product']); }
public static function updateCogs($params, $logs = []) { $cogs = Cogs::findOne(['id_product' => $params['id_product']]); if (!$cogs) { $cogs = new Cogs(); $cogs->setAttributes(['id_product' => $params['id_product'], 'id_uom' => $params['id_uom'], 'cogs' => 0.0]); } $cogs->cogs = 1.0 * ($cogs->cogs * $params['old_stock'] + $params['price'] * $params['added_stock']) / ($params['old_stock'] + $params['added_stock']); if (!empty($logs) && $cogs->canSetProperty('logParams')) { $cogs->logParams = $logs; } if (!$cogs->save()) { throw new UserException(implode(",\n", $cogs->firstErrors)); } return true; }
/** * @return \yii\db\ActiveQuery */ public function getCogs() { return $this->hasMany(Cogs::className(), ['id_uom' => 'id_uom']); }
public function actionSavePos() { Yii::$app->response->format = Response::FORMAT_JSON; $post = Yii::$app->request->post(); try { $transaction = Yii::$app->db->beginTransaction(); $drawer = Cashdrawer::findOne(['id_cashdrawer' => $post['id_drawer']]); $hdr = new SalesHdr(['id_cashdrawer' => $post['id_drawer'], 'id_branch' => $drawer->id_branch, 'create_by' => $drawer->id_user]); $total = 0.0; $dtls = []; foreach ($post['detail'] as $detail) { $cogs = Cogs::findOne(['id_product' => $detail['id_product']]); $dtl = new SalesDtl(['id_product' => $detail['id_product'], 'id_uom' => $detail['id_uom'], 'sales_price' => $detail['price'], 'sales_qty' => $detail['qty'], 'discount' => $detail['discon'], 'cogs' => $cogs ? $cogs->cogs : 0]); $total += $detail['qty'] * $detail['price'] * (1 - 0.01 * $detail['discon']); $dtls[] = $dtl; } $transaction->commit(); } catch (\Exception $exc) { $transaction->rollback(); } return ['type' => 'E']; }