Exemplo n.º 1
0
 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);
 }
Exemplo n.º 2
0
 /**
  * @return \yii\db\ActiveQuery
  */
 public function getProductStocks()
 {
     return $this->hasMany(ProductStock::className(), ['product_id' => 'id']);
 }