/**
  * Process lot data (create or update quantities).
  *
  * @param int $stockItemId Magento ID for stock item related to the lot.
  * @param \Praxigento\Odoo\Data\Odoo\Inventory\Product\Warehouse\Lot $lot
  * @return double quantity of the product in the lot
  */
 public function processLot($stockItemId, $lot)
 {
     $lotIdOdoo = $lot->getIdOdoo();
     $qty = $lot->getQuantity();
     $lotIdMage = $this->_repoAggLot->getMageIdByOdooId($lotIdOdoo);
     $pk = [EntityWarehouseQuantity::ATTR_STOCK_ITEM_REF => $stockItemId, EntityWarehouseQuantity::ATTR_LOT_REF => $lotIdMage];
     $qtyItem = $this->_repoWarehouseEntityQuantity->getById($pk);
     if ($qtyItem) {
         /* update lot qty data */
         $bind = [EntityWarehouseQuantity::ATTR_TOTAL => $qty];
         $this->_repoWarehouseEntityQuantity->updateById($pk, $bind);
     } else {
         /* create lot qty data */
         $pk[EntityWarehouseQuantity::ATTR_TOTAL] = $qty;
         $this->_repoWarehouseEntityQuantity->create($pk);
     }
     return $qty;
 }
 /**
  * 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);
     }
 }