/** * Clean up extra lots for the stock item. * * @param int $stockItemId Magento ID for stock item. * @param \Praxigento\Odoo\Data\Odoo\Inventory\Product\Warehouse\Lot[] $lots list of the actual lots. */ public function cleanupLots($stockItemId, $lots) { $where = EntityWarehouseQuantity::ATTR_STOCK_ITEM_REF . '=' . (int) $stockItemId; $lotsExist = $this->_repoWarehouseEntityQuantity->get($where); // create map of the Magento IDs for existing lots $mapMageExist = []; foreach ($lotsExist as $item) { $lotIdMage = $item[EntityWarehouseQuantity::ATTR_LOT_REF]; $mapMageExist[] = $lotIdMage; } // create map for Lots from request $mapOdooExist = []; foreach ($lots as $lot) { $lotIdOdoo = $lot->getIdOdoo(); $lotIdMage = $this->_repoAggLot->getMageIdByOdooId($lotIdOdoo); $mapOdooExist[] = $lotIdMage; } $diff = array_diff($mapMageExist, $mapOdooExist); foreach ($diff as $lotIdMage) { $pk = [EntityWarehouseQuantity::ATTR_STOCK_ITEM_REF => $stockItemId, EntityWarehouseQuantity::ATTR_LOT_REF => $lotIdMage]; $this->_repoWarehouseEntityQuantity->deleteById($pk); } }
/** * Get lot(s) with the closest expiration date and write off quantity from this lot(s). Register write off * quantity in 'prxgt_wrhs_qty_sale'. * * @param int $saleItemId * @param double $total * @param array $lotsData */ public function registerSaleItemQty($saleItemId, $total, $lotsData) { $rest = $total; $def = $this->_manTrans->begin(); try { foreach ($lotsData as $lot) { $stockItemId = $lot[Alias::AS_STOCK_ITEM_ID]; $lotId = $lot[Alias::AS_LOT_ID]; $qty = $lot[Alias::AS_QTY]; $qtyPk = [Quantity::ATTR_STOCK_ITEM_REF => $stockItemId, Quantity::ATTR_LOT_REF => $lotId]; if ($rest < $qty) { /* lot's $qty is greater than $total (or $rest) */ $qtySaleData = [QtySale::ATTR_SALE_ITEM_REF => $saleItemId, QtySale::ATTR_LOT_REF => $lotId, QtySale::ATTR_TOTAL => $rest]; $this->_repoQtySale->create($qtySaleData); /* decrease lot's qty */ $qtyRest = $qty - $rest; $qtyUpdateData = [Quantity::ATTR_TOTAL => $qtyRest]; $this->_repoQty->updateById($qtyPk, $qtyUpdateData); break; } else { /* lot's $qty is less or equal to $total (or $rest) */ $qtySaleData = [QtySale::ATTR_SALE_ITEM_REF => $saleItemId, QtySale::ATTR_LOT_REF => $lotId, QtySale::ATTR_TOTAL => $qty]; $this->_repoQtySale->create($qtySaleData); /* delete zero quantity records from 'prxgt_wrhs_qty' */ $this->_repoQty->deleteById($qtyPk); /* decrease $rest of $total*/ $rest -= $qty; } if ($rest <= 0) { break; } } $this->_manTrans->commit($def); } finally { // transaction will be rolled back if commit is not done (otherwise - do nothing) $this->_manTrans->end($def); } }