/** * @inheritdoc */ public function rules() { return [[['product_id', 'uom_id', 'qty', 'price'], 'required'], [['sales_id', 'product_id', 'uom_id'], 'integer'], [['cogs'], 'default', 'value' => function () { $cogs = Cogs::findOne($this->product_id); return $cogs ? $cogs->cogs : null; }], [['cogs'], 'required'], [['qty', 'price', 'cogs', 'discount', 'total_release'], 'number']]; }
/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $query = CogsModel::find(); $query->select(['cogs.*', 'product.*']); $query->joinWith(['product']); $query->orderBy(['product.name' => SORT_ASC]); $dataProvider = new ActiveDataProvider(['query' => $query]); $this->load($params); if (!$this->validate()) { $query->where('1=0'); return $dataProvider; } $query->andFilterWhere(['product_id' => $this->product_id, 'cogs' => $this->cogs, 'last_purchase_price' => $this->last_purchase_price, 'created_at' => $this->created_at, 'created_by' => $this->created_by, 'updated_at' => $this->updated_at, 'updated_by' => $this->updated_by]); $query->andWhere(['like', 'lower(product.name)', strtolower($this->product_name)]); $query->andWhere(['like', 'lower(product.code)', strtolower($this->product_code)]); return $dataProvider; }
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); }
/** * Finds the Cogs model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param integer $id * @return Cogs the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { if (($model = Cogs::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }
/** * @return \yii\db\ActiveQuery */ public function getCogs() { return $this->hasOne(Cogs::className(), ['product_id' => 'product_id']); }