src/Entity/ItemTransaction.php line 16

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use App\Service\Helper\DateTimeHelper;
  4. use DateTime;
  5. use Doctrine\ORM\Mapping as ORM;
  6. use Exception;
  7. /**
  8.  * @ORM\Table(name="products__items_transactions", uniqueConstraints={
  9.  *   @ORM\UniqueConstraint(columns={"invoice_product_id", "reason"})
  10.  * })
  11.  * @ORM\Entity(repositoryClass="App\Repository\ItemTransactionRepository")
  12.  */
  13. class ItemTransaction
  14. {
  15.     const REASON_CONFIRM_INVOICE 'confirm_invoice';
  16.     const REASON_REMOVE_INVOICE 'remove_invoice';
  17.     const REASON_CONFIRM_ORDER 'confirm_order';
  18.     const REASON_REFUND_ORDER_ELEMENT 'refund_order_element';
  19.     use IdTrait;
  20.     /**
  21.      * @ORM\Column(name="created_at", type="datetime", nullable=false)
  22.      */
  23.     private DateTime $createdAt;
  24.     /**
  25.      * @ORM\ManyToOne(targetEntity="Item")
  26.      * @ORM\JoinColumn(name="item_id", referencedColumnName="id", nullable=false, onDelete="CASCADE")
  27.      */
  28.     private Item $item;
  29.     /**
  30.      * @ORM\ManyToOne(targetEntity="InvoiceProduct")
  31.      * @ORM\JoinColumn(name="invoice_product_id", referencedColumnName="id", nullable=true, onDelete="SET NULL")
  32.      */
  33.     private ?InvoiceProduct $invoiceProduct;
  34.     /**
  35.      * @ORM\ManyToOne(targetEntity="OrderElement")
  36.      * @ORM\JoinColumn(name="order_element_id", referencedColumnName="id", nullable=true, onDelete="SET NULL")
  37.      */
  38.     private ?OrderElement $orderElement;
  39.     /**
  40.      * @ORM\Column(name="init_stock", type="integer")
  41.      */
  42.     private int $initStock;
  43.     /**
  44.      * @ORM\Column(name="amount", type="integer")
  45.      */
  46.     private int $amount 0;
  47.     /**
  48.      * @ORM\Column(name="new_stock", type="integer")
  49.      */
  50.     private int $newStock;
  51.     /**
  52.      * @ORM\Column(name="reason", type="string")
  53.      */
  54.     private string $reason;
  55.     /**
  56.      * @throws Exception
  57.      */
  58.     public function __construct(Item $itemstring $reason, ?InvoiceProduct $invoiceProduct, ?OrderElement $orderElement)
  59.     {
  60.         $this->item $item;
  61.         $this->reason $reason;
  62.         $this->initStock $item->getStock();
  63.         $this->newStock $item->getStock();
  64.         if($invoiceProduct) {
  65.             if ($reason === self::REASON_CONFIRM_INVOICE$this->newStock += $invoiceProduct->getQuantity();
  66.             elseif ($reason === self::REASON_REMOVE_INVOICE$this->newStock -= $invoiceProduct->getQuantity();
  67.             $this->amount $invoiceProduct->getQuantity();
  68.         }
  69.         $this->invoiceProduct $invoiceProduct;
  70.         if($orderElement) {
  71.             if ($reason === self::REASON_CONFIRM_ORDER$this->newStock -= $orderElement->getQuantity();
  72.             elseif ($reason === self::REASON_REFUND_ORDER_ELEMENT$this->newStock += $orderElement->getQuantity();
  73.             $this->amount $orderElement->getQuantity();
  74.         }
  75.         $this->orderElement $orderElement;
  76.         $this->createdAt DateTimeHelper::getNowDateTime();
  77.     }
  78.     public function getItem(): Item
  79.     {
  80.         return $this->item;
  81.     }
  82.     public function getNewStock(): int
  83.     {
  84.         return $this->newStock;
  85.     }
  86.     public function getReason(): string
  87.     {
  88.         return $this->reason;
  89.     }
  90.     public function getOrderElement(): ?OrderElement
  91.     {
  92.         return $this->orderElement;
  93.     }
  94. }