src/Entity/OnlineShop/Order.php line 34

Open in your IDE?
  1. <?php
  2. namespace App\Entity\OnlineShop;
  3. use App\Annotation\SiteAware;
  4. use App\Entity\AbstractBase;
  5. use App\Entity\Address;
  6. use App\Entity\Garages\Garage;
  7. use App\Entity\Interfaces\SiteInterface;
  8. use App\Entity\Traits\GarageTrait;
  9. use App\Entity\Traits\SiteTrait;
  10. use App\Entity\User;
  11. use App\Entity\Whatsapp\WhatsappOrder;
  12. use App\Enum\OrderStatusTypeEnum;
  13. use App\Repository\OnlineShop\OrderRepository;
  14. use Doctrine\Common\Collections\ArrayCollection;
  15. use Doctrine\Common\Collections\Collection;
  16. use Doctrine\ORM\Mapping as ORM;
  17. use Gedmo\Mapping\Annotation as Gedmo;
  18. use Money\Currency;
  19. use Money\Money;
  20. use Symfony\Component\Security\Core\User\UserInterface;
  21. use Tbbc\MoneyBundle\Formatter\MoneyFormatter;
  22. /**
  23.  * @ORM\Table(name="vulco_order", indexes={@ORM\Index(name="order_site_idx", columns={"site"})})
  24.  *
  25.  * @ORM\Entity(repositoryClass=OrderRepository::class)
  26.  *
  27.  * @Gedmo\SoftDeleteable(fieldName="removedAt", timeAware=false)
  28.  *
  29.  * @SiteAware(siteFieldName="site")
  30.  */
  31. class Order extends AbstractBase implements SiteInterface
  32. {
  33.     use GarageTrait;
  34.     use SiteTrait;
  35.     /**
  36.      * @ORM\Column(type="string", length=200)
  37.      */
  38.     private string $reference;
  39.     /**
  40.      * @ORM\Column(type="integer")
  41.      */
  42.     private int $state;
  43.     /**
  44.      * @ORM\Column(type="integer")
  45.      */
  46.     private int $totalPriceAmount;
  47.     /**
  48.      * @ORM\Column(type="string")
  49.      */
  50.     private string $totalPriceCurrency;
  51.     /**
  52.      * @ORM\Column(type="money", options={"default": "EUR 0"})
  53.      */
  54.     private Money $totalRappel;
  55.     private bool $blocked false;
  56.     /**
  57.      * @ORM\Column(type="datetime", nullable=true)
  58.      */
  59.     private ?\DateTimeInterface $processedAt null;
  60.     /**
  61.      * @ORM\Column(type="boolean", options={"default": 0})
  62.      */
  63.     private bool $rebilled false;
  64.     /**
  65.      * @ORM\ManyToOne(targetEntity="App\Entity\OnlineShop\Supplier")
  66.      *
  67.      * @ORM\JoinColumn(name="supplier_id", referencedColumnName="id", onDelete="SET NULL")
  68.      */
  69.     private ?Supplier $supplier null;
  70.     /**
  71.      * @ORM\OneToMany(targetEntity="App\Entity\OnlineShop\ProductOrder", mappedBy="order", cascade={"persist"}, orphanRemoval=true)
  72.      */
  73.     private ?Collection $products;
  74.     /**
  75.      * @ORM\OneToMany(targetEntity="App\Entity\OnlineShop\OrderSupplierShippingCost", mappedBy="order", cascade={"persist", "remove"}, orphanRemoval=true)
  76.      */
  77.     private ?Collection $shipments;
  78.     /**
  79.      * @ORM\OneToMany(targetEntity="App\Entity\OnlineShop\OrderFamilyDiscount", mappedBy="order", cascade={"persist", "remove"}, orphanRemoval=true)
  80.      */
  81.     private ?Collection $discounts;
  82.     /**
  83.      * @ORM\ManyToOne(targetEntity="App\Entity\Garages\Garage", fetch="EAGER")
  84.      *
  85.      * @ORM\JoinColumn(name="garage_id", referencedColumnName="id")
  86.      */
  87.     private ?Garage $garage null;
  88.     /**
  89.      * @ORM\ManyToOne(targetEntity="App\Entity\User", fetch="EAGER")
  90.      *
  91.      * @ORM\JoinColumn(name="user_id", referencedColumnName="id")
  92.      *
  93.      * User who has placed the order (could be the associated but an admin too)
  94.      */
  95.     private ?User $user null;
  96.     /**
  97.      * @ORM\ManyToOne(targetEntity="App\Entity\User", fetch="EAGER")
  98.      *
  99.      * @ORM\JoinColumn(name="reopened_by_user_id", referencedColumnName="id")
  100.      *
  101.      * User who has reopened the order
  102.      */
  103.     private ?User $reopenedByUser null;
  104.     /**
  105.      * @ORM\ManyToOne(targetEntity="App\Entity\Address")
  106.      *
  107.      * @ORM\JoinColumn(name="address_id", referencedColumnName="id")
  108.      */
  109.     private ?Address $shippingAddress null;
  110.     /**
  111.      * @ORM\Column(type="text", length=10000, nullable=true)
  112.      */
  113.     private ?string $commentUser null;
  114.     /**
  115.      * @ORM\Column(type="text", length=10000, nullable=true)
  116.      */
  117.     private ?string $commentAdmin null;
  118.     /**
  119.      * @ORM\ManyToOne(targetEntity="App\Entity\Whatsapp\WhatsappOrder", cascade={"persist", "remove"})
  120.      * @ORM\JoinColumn(name="whatsapp_order_id", referencedColumnName="id", nullable=true)
  121.      */
  122.     private ?WhatsappOrder $whatsappOrder null;
  123.     public function __construct()
  124.     {
  125.         $this->products = new ArrayCollection();
  126.         $this->shipments = new ArrayCollection();
  127.         $this->discounts = new ArrayCollection();
  128.         $this->state OrderStatusTypeEnum::PENDING;
  129.     }
  130.     public function getReference(): string
  131.     {
  132.         return $this->reference;
  133.     }
  134.     public function setReference(string $reference): self
  135.     {
  136.         $this->reference $reference;
  137.         return $this;
  138.     }
  139.     public function getState(): int
  140.     {
  141.         return $this->state;
  142.     }
  143.     public function setState(int $state): self
  144.     {
  145.         $this->state $state;
  146.         return $this;
  147.     }
  148.     public function getTotalPriceAmount(): int
  149.     {
  150.         return $this->totalPriceAmount;
  151.     }
  152.     public function setTotalPriceAmount(int $totalPriceAmount): self
  153.     {
  154.         $this->totalPriceAmount $totalPriceAmount;
  155.         return $this;
  156.     }
  157.     public function getTotalPriceCurrency(): string
  158.     {
  159.         return $this->totalPriceCurrency;
  160.     }
  161.     public function setTotalPriceCurrency(string $totalPriceCurrency): self
  162.     {
  163.         $this->totalPriceCurrency $totalPriceCurrency;
  164.         return $this;
  165.     }
  166.     public function getTotalRappel(): Money
  167.     {
  168.         return $this->totalRappel;
  169.     }
  170.     public function setTotalRappel(Money $totalRappel): self
  171.     {
  172.         $this->totalRappel $totalRappel;
  173.         return $this;
  174.     }
  175.     public function getTotalRappelAsString(): string
  176.     {
  177.         $moneyFormatter = new MoneyFormatter(2);
  178.         return $moneyFormatter->localizedFormatMoney($this->totalRappel);
  179.     }
  180.     public function isBlocked(): bool
  181.     {
  182.         return $this->blocked;
  183.     }
  184.     public function getBlocked(): bool
  185.     {
  186.         return $this->isBlocked();
  187.     }
  188.     public function setBlocked(bool $blocked): self
  189.     {
  190.         $this->blocked $blocked;
  191.         return $this;
  192.     }
  193.     public function getProcessedAt(): ?\DateTimeInterface
  194.     {
  195.         return $this->processedAt;
  196.     }
  197.     public function getProcessedAtAsString(): string
  198.     {
  199.         return $this->getDateTimeAsString($this->getProcessedAt());
  200.     }
  201.     public function setProcessedAt(?\DateTimeInterface $processedAt): self
  202.     {
  203.         $this->processedAt $processedAt;
  204.         return $this;
  205.     }
  206.     public function isRebilled(): bool
  207.     {
  208.         return $this->rebilled;
  209.     }
  210.     public function setRebilled(bool $rebilled): self
  211.     {
  212.         $this->rebilled $rebilled;
  213.         return $this;
  214.     }
  215.     public function getSupplier(): ?Supplier
  216.     {
  217.         return $this->supplier;
  218.     }
  219.     public function setSupplier(?Supplier $supplier): self
  220.     {
  221.         $this->supplier $supplier;
  222.         return $this;
  223.     }
  224.     public function getProducts(): ?Collection
  225.     {
  226.         return $this->products;
  227.     }
  228.     public function setProducts(?Collection $products): self
  229.     {
  230.         $this->products $products;
  231.         return $this;
  232.     }
  233.     public function addProduct(ProductOrder $product): self
  234.     {
  235.         if (!$this->products->contains($product)) {
  236.             $this->products->add($product);
  237.             $product->setOrder($this);
  238.         }
  239.         return $this;
  240.     }
  241.     public function removeProduct(ProductOrder $product): self
  242.     {
  243.         if ($this->products->contains($product)) {
  244.             $this->products->removeElement($product);
  245.         }
  246.         return $this;
  247.     }
  248.     public function getShipments(): ?Collection
  249.     {
  250.         return $this->shipments;
  251.     }
  252.     public function setShipments(?Collection $shipments): self
  253.     {
  254.         $this->shipments $shipments;
  255.         return $this;
  256.     }
  257.     public function addShipment(OrderSupplierShippingCost $shipment): self
  258.     {
  259.         if (!$this->shipments->contains($shipment)) {
  260.             $this->shipments->add($shipment);
  261.             $shipment->setOrder($this);
  262.             if ($shipment->isBlocked()) {
  263.                 $this->setBlocked(true);
  264.             }
  265.         }
  266.         return $this;
  267.     }
  268.     public function removeShipment(OrderSupplierShippingCost $shipment): self
  269.     {
  270.         if ($this->shipments->contains($shipment)) {
  271.             $this->shipments->removeElement($shipment);
  272.         }
  273.         return $this;
  274.     }
  275.     public function getDiscounts(): ?Collection
  276.     {
  277.         return $this->discounts;
  278.     }
  279.     public function setDiscounts(?Collection $discounts): self
  280.     {
  281.         $this->discounts $discounts;
  282.         return $this;
  283.     }
  284.     public function addDiscount(OrderFamilyDiscount $discount): self
  285.     {
  286.         if (!$this->discounts->contains($discount)) {
  287.             $this->discounts->add($discount);
  288.             $discount->setOrder($this);
  289.         }
  290.         return $this;
  291.     }
  292.     public function removeDiscount(OrderFamilyDiscount $discount): self
  293.     {
  294.         if ($this->discounts->contains($discount)) {
  295.             $this->discounts->removeElement($discount);
  296.         }
  297.         return $this;
  298.     }
  299.     /**
  300.      * User who has placed the order (could be the associated but an admin too).
  301.      */
  302.     public function getUser(): ?User
  303.     {
  304.         return $this->user;
  305.     }
  306.     public function setUser(?UserInterface $user): self
  307.     {
  308.         $this->user $user;
  309.         return $this;
  310.     }
  311.     /**
  312.      * User who has reopened the order.
  313.      */
  314.     public function getReopenedByUser(): ?User
  315.     {
  316.         return $this->reopenedByUser;
  317.     }
  318.     public function setReopenedByUser(?UserInterface $user): self
  319.     {
  320.         $this->reopenedByUser $user;
  321.         return $this;
  322.     }
  323.     public function getShippingAddress(): ?Address
  324.     {
  325.         return $this->shippingAddress;
  326.     }
  327.     public function setShippingAddress(?Address $shippingAddress): self
  328.     {
  329.         $this->shippingAddress $shippingAddress;
  330.         return $this;
  331.     }
  332.     public function getCommentUser(): ?string
  333.     {
  334.         return $this->commentUser;
  335.     }
  336.     public function setCommentUser(?string $commentUser): self
  337.     {
  338.         $this->commentUser $commentUser;
  339.         return $this;
  340.     }
  341.     public function getCommentAdmin(): ?string
  342.     {
  343.         return $this->commentAdmin;
  344.     }
  345.     public function setCommentAdmin(?string $commentAdmin): self
  346.     {
  347.         $this->commentAdmin $commentAdmin;
  348.         return $this;
  349.     }
  350.     public function getWhatsappOrder(): ?WhatsappOrder
  351.     {
  352.         return $this->whatsappOrder;
  353.     }
  354.     public function setWhatsappOrder(?WhatsappOrder $whatsappOrder): Order
  355.     {
  356.         $this->whatsappOrder $whatsappOrder;
  357.         return $this;
  358.     }
  359.     public function getTotalPrice(): ?Money
  360.     {
  361.         if (!$this->totalPriceCurrency) {
  362.             return null;
  363.         }
  364.         if (!$this->totalPriceAmount) {
  365.             return new Money(0, new Currency($this->totalPriceCurrency));
  366.         }
  367.         return new Money($this->totalPriceAmount, new Currency($this->totalPriceCurrency));
  368.     }
  369.     public function setTotalPrice(Money $price): self
  370.     {
  371.         $this->totalPriceAmount $price->getAmount();
  372.         $this->totalPriceCurrency $price->getCurrency()->getCode();
  373.         return $this;
  374.     }
  375.     public function calculateTotalPrice(): Money
  376.     {
  377.         $totalPrice = new Money(0, new Currency(self::DEFAULT_EURO_CURRENCY));
  378.         foreach ($this->products as $productOrder) {
  379.             $totalPrice $totalPrice->add($productOrder->getTotalPrice());
  380.         }
  381.         foreach ($this->getShipments() as $shipment) {
  382.             $totalPrice $totalPrice->add($shipment->getPrice());
  383.         }
  384.         foreach ($this->getDiscounts() as $discount) {
  385.             $totalPrice $totalPrice->subtract($discount->getPrice());
  386.         }
  387.         return $totalPrice;
  388.     }
  389.     public function calculateTotalRappel(): Money
  390.     {
  391.         $totalRappel = new Money(0, new Currency(self::DEFAULT_EURO_CURRENCY));
  392.         /** @var ProductOrder $productOrder */
  393.         foreach ($this->products as $productOrder) {
  394.             $rappelPercentage $productOrder->getProduct()->getFamily()->getSupplier()->getRappelPercentage();
  395.             if ($productOrder->getProduct()->hasSpecialPriceActive()) {
  396.                 $rappel $productOrder->getProduct()->getSpecialPriceActive()->getPrice()->multiply($rappelPercentage 100);
  397.             } else {
  398.                 $rappel $productOrder->getRatePrice()->multiply($rappelPercentage 100);
  399.             }
  400.             $rappel $rappel->multiply($productOrder->getQuantity());
  401.             $totalRappel $totalRappel->add($rappel);
  402.         }
  403.         /** @var OrderFamilyDiscount $discount */
  404.         foreach ($this->getDiscounts() as $discount) {
  405.             $rappelPercentage $discount->getFamily()->getSupplier()->getRappelPercentage();
  406.             $rappel $discount->getPrice()->multiply($rappelPercentage 100);
  407.             $totalRappel $totalRappel->subtract($rappel);
  408.         }
  409.         return $totalRappel;
  410.     }
  411.     public function isPending(): bool
  412.     {
  413.         return OrderStatusTypeEnum::PENDING === $this->state;
  414.     }
  415.     public function isReturned(): bool
  416.     {
  417.         return OrderStatusTypeEnum::RETURNED === $this->state;
  418.     }
  419.     public function isProcessed(): bool
  420.     {
  421.         return OrderStatusTypeEnum::PROCESSED === $this->state;
  422.     }
  423. }