コード例 #1
0
ファイル: ProductStock.php プロジェクト: deesoft/yii2-dev
 protected function updateCogs($params)
 {
     $cogs = Cogs::findOne(['product_id' => $params['product_id']]);
     if (!$cogs) {
         $cogs = new Cogs(['product_id' => $params['product_id'], 'cogs' => 0.0]);
     }
     $current_stock = MProductStock::find()->where(['product_id' => $params['product_id']])->sum('qty');
     $qty_per_uom = $params['qty_per_uom'];
     $added_stock = $params['qty'] * $qty_per_uom;
     if ($current_stock + $added_stock != 0) {
         $cogs->cogs = 1.0 * ($cogs->cogs * $current_stock + $params['price'] * $params['qty']) / ($current_stock + $added_stock);
     } else {
         $cogs->cogs = 0;
     }
     if ($cogs->canSetProperty('logParams')) {
         $cogs->logParams = ['app' => $params['app'], 'reff_id' => $params['reff_id']];
     }
     if (!$cogs->save()) {
         throw new UserException(implode(",\n", $cogs->firstErrors));
     }
     return true;
 }
コード例 #2
0
ファイル: Product.php プロジェクト: deesoft/yii2-dev
 /**
  * @return \yii\db\ActiveQuery
  */
 public function getCogs()
 {
     return $this->hasOne(Cogs::className(), ['product_id' => 'id']);
 }