/** * * @return boolean */ public function updateStock($factor) { // update stock $wh_id = $this->warehouse_id; $mv_id = $this->id; $factor = $factor * ($this->type == self::TYPE_RECEIVE ? 1 : -1); $command = Yii::$app->db->createCommand(); foreach ($this->items as $item) { $product_id = $item->product_id; $pu = ProductUom::findOne(['product_id' => $product_id, 'uom_id' => $item->uom_id]); $qty = $factor * $item->qty * ($pu ? $pu->isi : 1); $ps = ProductStock::findOne(['product_id' => $product_id, 'warehouse_id' => $wh_id]); if ($ps) { $ps->qty = new Expression('[[qty]] + :added', [':added' => $qty]); } else { $ps = new ProductStock(['product_id' => $product_id, 'warehouse_id' => $wh_id, 'qty' => $qty]); } if (!$ps->save(false) || !$ps->refresh() || !$command->insert('{{%product_stock_history}}', ['time' => microtime(true), 'warehouse_id' => $wh_id, 'product_id' => $product_id, 'qty_movement' => $qty, 'qty_current' => $ps->qty, 'movement_id' => $mv_id])->execute()) { return false; } if ($item->cogs !== null && $item->cogs !== '') { $paramCogs = ['id' => $product_id, 'qty' => $qty, 'cogs' => $item->cogs]; if (!$this->updateCogs($paramCogs)) { return false; } } } return true; }