Ejemplo n.º 1
0
 /**
  * @param Sale $sale
  * @return Log[]
  */
 public function findBySale(Sale $sale)
 {
     $dql = "select l, u from ent:Log l left join l.user u where l.resource_id = :id and l.resource_type = :type";
     $query = $this->em()->createQuery($dql);
     $query->setParameters(['id' => $sale->getId(), 'type' => Sale::class]);
     return $query->getResult();
 }
Ejemplo n.º 2
0
 /**
  * @param Sale $sale
  * @param array $rawItems
  * @throws \Exception
  */
 public function reduceStock(Sale $sale, array $rawItems)
 {
     if (!$sale->getOutletSale() instanceof OutletSale) {
         return;
     }
     $creatorId = $sale->getCreator() instanceof User ? $sale->getCreator()->getId() : null;
     $warehouse = $sale->getOutletSale()->getOutlet()->getWarehouse();
     $getPriceId = function ($price) {
         if ($price instanceof MenuPrice) {
             return $price->getId();
         } else {
             if (is_numeric($price)) {
                 return $price;
             } else {
                 throw new \InvalidArgumentException();
             }
         }
     };
     $menuPrices = [];
     foreach ($rawItems as $item) {
         if (isset($item['menu_price']) && $item['menu_price'] && isset($item['quantity'])) {
             try {
                 $menuPrices[$getPriceId($item['menu_price'])] = $item['quantity'];
             } catch (\Exception $e) {
                 continue;
             }
         }
     }
     $menuPriceEntities = $this->findMenuPricesInIds(array_keys($menuPrices));
     $insertSql = [];
     $updateSql = [];
     foreach ($menuPriceEntities as $menuPriceEntity) {
         if (isset($menuPrices[$menuPriceEntity->getId()])) {
             $quantity = $menuPrices[$menuPriceEntity->getId()];
             foreach ($menuPriceEntity->getMenu()->getIngredients() as $ingredient) {
                 $ratio = $ingredient->getStockItem()->getUsageUnit()->getRatio();
                 $used = $ingredient->getQuantity() * $ratio * $quantity;
                 $stockItemId = $ingredient->getStockItem()->getId();
                 $warehouseId = $warehouse->getId();
                 $storageUnit = $ingredient->getStockItem()->getStorageUnit();
                 if ($used > 0) {
                     $updateSql[] = $this->createStockUpdateSql($used, $stockItemId, $warehouseId);
                     $insertSql[] = $this->createUsageChangelogSql($sale->getId(), $creatorId, $used, $stockItemId, $storageUnit->getName(), $warehouseId);
                 }
             }
         }
     }
     // execute reduce stock & write changelog
     if (count($updateSql)) {
         $this->executeSql(join(';', $updateSql));
         $this->executeSql(join(';', $insertSql));
     }
     $this->normalizeStockLevel();
 }