src/EventSubscriber/SupplySubscriber.php line 75

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\Event\SupplyEvent;
  4. use App\Entity\SupplyActionHistory;
  5. use DateTime;
  6. use Doctrine\Persistence\ManagerRegistry;
  7. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  8. class SupplySubscriber implements EventSubscriberInterface
  9. {
  10.     private ManagerRegistry $managerRegistry;
  11.     public function __construct(ManagerRegistry $managerRegistry)
  12.     {
  13.         $this->managerRegistry $managerRegistry;
  14.     }
  15.     /**
  16.      * @return float|int
  17.      */
  18.     private function computeLost(int $qtint $loss)
  19.     {
  20.         if ($loss 0) { return $qt; }
  21.         $loss = ($qt $loss) / 100;
  22.         $qt += $loss;
  23.         $qt is_float($qt) ? round($qt) : $qt;
  24.         return $qt;
  25.     }
  26.     private function save($entity): void
  27.     {
  28.         $manager $this->managerRegistry->getManager();
  29.         $manager->persist($entity);
  30.         $manager->flush();
  31.     }
  32.     public function updateSupplyQty(SupplyEvent $event): void
  33.     {
  34.         $supply                $event->getSupply();
  35.         $supplyHistory         $event->getSupplyHistory();
  36.         $oldQuantity           $event->getOldQuantity();
  37.         $supplyQuantity        $supply->getQuantityInStock();
  38.         $direction             $supplyHistory->getDirection();
  39.         $supplyHistoryQuantity $supplyHistory->getQuantity();
  40.         $quantityWithLost      $this->computeLost($supplyHistoryQuantity$supply->getLost());
  41.         if (!is_null($oldQuantity)) {                                                          # if $oldQuantity is not null, we edit an existing Supply Action History (SAH)
  42.             if (SupplyActionHistory::DIRECTION_OUT === $direction) {                           # if we remove quantity
  43.                 $oldQuantityWithLost $this->computeLost($oldQuantity$supply->getLost());   # to edit this entry, we set the quantity to the initial state
  44.                 $quantityBeforeEntry $supplyQuantity $oldQuantityWithLost;
  45.                 $quantity $quantityBeforeEntry $quantityWithLost;                          # so the qt to remove will be the initial qt minus the new qt with lost
  46.             } else {                                                                           # if we add quantity
  47.                 $quantityBeforeEntry $supplyQuantity $oldQuantity;                         # as above, we set the quantity to the initial state
  48.                 $quantity $quantityBeforeEntry $supplyHistoryQuantity;                     # then we simply remove the new quantity
  49.             }
  50.         } else {                                                                               # in this case, a new SAH is created
  51.             if (SupplyActionHistory::DIRECTION_OUT === $direction) {                           # if we remove quantity
  52.                 $quantity $supplyQuantity $quantityWithLost;                               # get supply qt and remove the quantity from the input form
  53.             } else {                                                                           # otherwise, we add a new quantity in stock
  54.                 $quantity $supplyQuantity $supplyHistoryQuantity;                          # so we add the quantity from the input form to the supply quantity
  55.             }
  56.         }
  57.         $supply->setQuantityInStock($quantity);
  58.         $supply->setUpdatedDate(new DateTime('now'));
  59.         $this->save($supply);
  60.         $event->stopPropagation();
  61.     }
  62.     public function updateSupplyQuantityWhenDeleteHistory(SupplyEvent $supplyEvent): void
  63.     {
  64.         $supply                      $supplyEvent->getSupply();
  65.         $supplyHistory               $supplyEvent->getSupplyHistory();
  66.         $supplyQuantityInStock       $supply->getQuantityInStock();
  67.         $supplyActionHistoryQuantity $supplyHistory->getQuantity();
  68.         switch ($supplyHistory->getDirection()) {
  69.             case SupplyActionHistory::DIRECTION_OUT:
  70.                 $withdrawnQuantityWithLost $this->computeLost($supplyActionHistoryQuantity$supply->getLost());
  71.                 $supply->setQuantityInStock($supplyQuantityInStock $withdrawnQuantityWithLost);
  72.             break;
  73.             case SupplyActionHistory::DIRECTION_IN:
  74.                 $supply->setQuantityInStock($supplyQuantityInStock $supplyActionHistoryQuantity);
  75.             break;
  76.         }
  77.         $this->save($supply);
  78.     }
  79.     public static function getSubscribedEvents(): array
  80.     {
  81.         return [
  82.             SupplyEvent::UPDATE_SUPPLY_QT => 'updateSupplyQty',
  83.             SupplyEvent::UPDATE_QT_WHEN_DELETE_SUPPLY_HISTORY => 'updateSupplyQuantityWhenDeleteHistory'
  84.         ];
  85.     }
  86. }