/** * @param Purchase $purchase * @return Log[] */ public function findByPurchase(Purchase $purchase) { $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' => $purchase->getId(), 'type' => Purchase::class]); return $query->getResult(); }
/** * @return int|null */ public function getRecordIdOrNull() { return $this->existingRecord ? $this->existingRecord->getId() : null; }
/** * @param Purchase $purchase * @param User $corrector * @param array $rawItems */ public function increaseStock(Purchase $purchase, User $corrector, array $rawItems) { $getStockId = function ($stock) { if ($stock instanceof Stock) { return $stock->getId(); } else { if (is_numeric($stock)) { return $stock; } else { throw new \InvalidArgumentException(); } } }; $stocks = []; $unitPrices = []; foreach ($rawItems as $item) { // todo: change $items array to object? to prevent missing keys in array. if (isset($item['stock']) && $item['stock'] && isset($item['quantity']) && $item['quantity']) { try { $stocks[$getStockId($item['stock'])] = $item['quantity']; if (isset($item['total']) && $item['total'] && $item['quantity'] > 0) { $unitPrices[$getStockId($item['stock'])] = $item['total'] / $item['quantity']; } } catch (\Exception $e) { continue; } } } /** @var Stock[] $stockEntities */ $stockEntities = $this->findStockInIds(array_keys($stocks)); foreach ($stockEntities as $stockEntity) { if (isset($stocks[$stockEntity->getId()])) { $quantity = $stocks[$stockEntity->getId()]; $ratio = $stockEntity->getStockItem()->getStorageUnit()->getRatio(); $unitQuantity = $quantity * $ratio; if ($unitQuantity) { // create level change $levelChange = new LevelChange(); $levelChange->setStock($stockEntity); $levelChange->setCorrector($corrector); $levelChange->setCurrentLevel($stockEntity->getCurrentLevel()); $levelChange->setType(LevelChange::TYPE_PURCHASE); $levelChange->setDelta($unitQuantity); $levelChange->setAuto(true); $levelChange->setNote(sprintf("#%d (+%s %s)", $purchase->getId(), $quantity, $stockEntity->getStockItem()->getStorageUnit()->getName())); // update stock $stockEntity->setCurrentLevel($stockEntity->getCurrentLevel() + $unitQuantity); $stockEntity->setLastChange(new \DateTime()); if (isset($unitPrices[$stockEntity->getId()])) { $stockEntity->setCurrentUnitPrice($unitPrices[$stockEntity->getId()]); } if ($purchase->getOrderedAt() instanceof \DateTime) { $stockEntity->setLastPurchase($purchase->getOrderedAt()); } $this->em()->persist($levelChange); $this->em()->persist($stockEntity); } } } $this->em()->flush(); }