Example #1
0
 /**
  * @param $itemName
  * @param $unitId
  * @param $currentLevel
  * @param $unitPrice
  * @return callable
  */
 private function createStockItemFormItemLine($itemName, $unitId, $currentLevel, $unitPrice)
 {
     return function (Event $e) use($itemName, $unitId, $currentLevel, $unitPrice) {
         $stockItem = new StockItem();
         $stockItem->setName($itemName);
         /** @var StockUnit $unit */
         $unit = $this->mapper(StockUnit::class)->ref($unitId);
         $stockItem->setUsageUnit($unit);
         $stockItem->setStorageUnit($unit);
         $this->persist($stockItem)->commit();
         $this->getStockService()->addToAllWarehouse($stockItem);
         $warehouse = $e->getParam('warehouse');
         if ($warehouse instanceof Warehouse) {
             $stock = $this->em()->getRepository(Stock::class)->findOneBy(['warehouse' => $warehouse, 'stock_item' => $stockItem]);
             if ($stock instanceof Stock) {
                 $units = $this->getAssocStockUnit();
                 if (isset($units[$unitId])) {
                     $unit = $units[$unitId];
                     $stock->setCurrentLevel($currentLevel * $unit->getRatio());
                     $stock->setCurrentUnitPrice($unitPrice);
                     $this->persist($stock)->commit();
                 }
             }
         }
     };
 }