src/Flexy/ShopBundle/Entity/Order/OrderItem.php line 27

  1. <?php
  2. namespace App\Flexy\ShopBundle\Entity\Order;
  3. use ApiPlatform\Core\Annotation\ApiResource;
  4. use App\Flexy\ShopBundle\Entity\Customer\PackEngagement;
  5. use App\Flexy\ShopBundle\Entity\Product\Product;
  6. use App\Flexy\ShopBundle\Entity\Shipping\Shipment;
  7. use App\Flexy\ShopBundle\Entity\Shipping\ShipmentItem;
  8. use App\Flexy\ShopBundle\Repository\Order\OrderItemRepository;
  9. use Doctrine\Common\Collections\ArrayCollection;
  10. use Doctrine\Common\Collections\Collection;
  11. use Doctrine\ORM\Mapping as ORM;
  12. use Doctrine\ORM\Mapping\Entity;
  13. use Doctrine\ORM\Mapping\InheritanceType;
  14. use Symfony\Component\Validator\Constraints as Assert;
  15. use Symfony\Component\Serializer\Annotation\Groups;
  16. #[ORM\Entity(repositoryClassOrderItemRepository::class)]
  17. #[InheritanceType('JOINED')]
  18. #[ApiResource(
  19.     normalizationContext: ['groups' => ['read','readPackEngagements']],
  20.     denormalizationContext: ['groups' => ['write']],
  21. )]
  22. class OrderItem implements \Stringable
  23. {
  24.     #[ORM\Id]
  25.     #[ORM\GeneratedValue]
  26.     #[ORM\Column(type'integer')]
  27.     #[Groups(['read''readDeep'])]
  28.     private $id;
  29.     #[ORM\Column(type'datetime_immutable'nullabletrue)]
  30.     private ?\DateTimeImmutable $createdAt null;
  31.     #[ORM\Column(type'text'nullabletrue)]
  32.     #[Groups(['read''readDeep'])]
  33.     private ?string $description null;
  34.     #[ORM\ManyToOne(targetEntityProduct::class, inversedBy'orderItems'cascade: ['persist'])]
  35.     #[Groups(['read''readDeep'])]
  36.     private ?\App\Flexy\ShopBundle\Entity\Product\Product $product null;
  37.     #[ORM\Column(type'integer')]
  38.     #[Groups(['read''readDeep'])]
  39.     private ?int $quantity null;
  40.     #[ORM\Column(type'float'nullabletrue)]
  41.     #[Groups(['read''readDeep'])]
  42.     private ?float $decimalQuantity null;
  43.     #[ORM\Column(type'float')]
  44.     #[Groups(['read''readDeep'])]
  45.     private ?float $price null;
  46.     
  47.     
  48.     #[ORM\ManyToOne(targetEntityOrder::class, inversedBy'orderItems'cascade: ['persist'])]
  49.     private ?\App\Flexy\ShopBundle\Entity\Order\Order $parentOrder null;
  50.     #[ORM\Column(type'float'nullabletrue)]
  51.     private int|float|null $reduction 0;
  52.     #[ORM\OneToMany(targetEntityOrderItemNote::class, mappedBy'orderItem'cascade: ['persist''remove'])]
  53.     private \Doctrine\Common\Collections\Collection|array $orderItemNotes;
  54.     #[ORM\OneToOne(targetEntityPackEngagement::class, inversedBy'orderItem'cascade: ['persist''remove'])]
  55.     private ?\App\Flexy\ShopBundle\Entity\Customer\PackEngagement $packEngagement null;
  56.     #[ORM\OneToOne(targetEntityShipmentItem::class, inversedBy'orderItem',  cascade: ['persist''remove'] )]
  57.     private $shipmentItem;
  58.     #[ORM\Column(length255nullabletrue)]
  59.     private ?string $image null;
  60.     #[ORM\Column(nullabletrue)]
  61.     private ?float $shippingFees null;
  62.     #[ORM\Column(length255nullabletrue)]
  63.     private ?string $currency null;
  64.     #[ORM\Column(nullabletrue)]
  65.     private ?array $metaData null;
  66.  
  67.     public function __construct()
  68.     {
  69.         
  70.         
  71.     }
  72.     public function __toString(): string
  73.     {
  74.         return $this->getDescription()."("$this->getPrice() ."DH x".$this->getQuantity().")";
  75.     }
  76.     public function getName()
  77.     {
  78.         $name "Undefined Name";
  79.         if($this->packEngagement){
  80.             $name $this->packEngagement->getPack()->getTitle();
  81.         }elseif($this->product){
  82.             $name $this->product->getName();
  83.         }
  84.         return $name;
  85.     }
  86.     public function getId(): ?int
  87.     {
  88.         return $this->id;
  89.     }
  90.     public function getCreatedAt(): ?\DateTimeImmutable
  91.     {
  92.         return $this->createdAt;
  93.     }
  94.     public function setCreatedAt(?\DateTimeImmutable $createdAt): self
  95.     {
  96.         $this->createdAt $createdAt;
  97.         return $this;
  98.     }
  99.     public function getDescription(): ?string
  100.     {
  101.         return $this->description;
  102.     }
  103.     public function setDescription(?string $description): self
  104.     {
  105.         $this->description $description;
  106.         return $this;
  107.     }
  108.     public function getProduct(): ?Product
  109.     {
  110.         return $this->product;
  111.     }
  112.     public function setProduct(?Product $product): self
  113.     {
  114.         $this->product $product;
  115.         return $this;
  116.     }
  117.     public function getQuantity(): ?int
  118.     {
  119.         return $this->quantity;
  120.     }
  121.     public function setQuantity(int $quantity): self
  122.     {
  123.         $this->quantity $quantity;
  124.         return $this;
  125.     }
  126.     public function getPrice(): ?float
  127.     {
  128.         return $this->price;
  129.     }
  130.     public function setPrice(float $price): self
  131.     {
  132.         $this->price $price;
  133.         return $this;
  134.     }
  135.     public function getParentOrder(): ?Order
  136.     {
  137.         return $this->parentOrder;
  138.     }
  139.     public function setParentOrder(?Order $parentOrder): self
  140.     {
  141.         $this->parentOrder $parentOrder;
  142.         return $this;
  143.     }
  144.     public function getFormattedPrice(): ?float
  145.     {
  146.         return $this->price/100;
  147.     }
  148.     
  149.     public function getTotalAmount(): ?float
  150.     {
  151.         $price $this->getFinalPrice();
  152.         if($this->getProduct() and !$price){
  153.             $price $this->getProduct()->getFinalPrice();
  154.             return $price $this->quantity;
  155.         }
  156.         if($this->decimalQuantity and (float)$this->decimalQuantity >= 0){
  157.             return (($price $this->getReduction()) * $this->decimalQuantity);
  158.         }
  159.         return $price  $this->quantity;
  160.     }
  161.     #[Groups(['read''readDeep'])]
  162.     public function getFinalPrice(): ?float
  163.     {
  164.         $price $this->price;
  165.         
  166.         $reduction 0;
  167.         if((float)$this->getReduction() > 0){
  168.             $reduction $this->getReduction();
  169.         }elseif($this->getProduct()){
  170.             if($this->getProduct()->getReduction()){
  171.                 $reduction $this->getProduct()->getReduction();
  172.             }
  173.         }
  174.         
  175.         if($this->getProduct()){
  176.             if($this->getProduct()->getPrice() >  $this->getPrice()){
  177.                 return $this->getPrice();
  178.             } 
  179.         }
  180.        
  181.         
  182.         $price $price $reduction;
  183.         return $price;
  184.     }
  185.     public function getReduction(): ?float
  186.     {
  187.         return (float)$this->reduction;
  188.     }
  189.     public function setReduction(?float $reduction): self
  190.     {
  191.         $this->reduction $reduction;
  192.         return $this;
  193.     }
  194.     /**
  195.      * @return Collection<int, OrderItemNote>
  196.      */
  197.     public function getOrderItemNotes(): Collection
  198.     {
  199.         return $this->orderItemNotes;
  200.     }
  201.     public function addOrderItemNote(OrderItemNote $orderItemNote): self
  202.     {
  203.         if (!$this->orderItemNotes->contains($orderItemNote)) {
  204.             $this->orderItemNotes[] = $orderItemNote;
  205.             $orderItemNote->setOrderItem($this);
  206.         }
  207.         return $this;
  208.     }
  209.     public function removeOrderItemNote(OrderItemNote $orderItemNote): self
  210.     {
  211.         if ($this->orderItemNotes->removeElement($orderItemNote)) {
  212.             // set the owning side to null (unless already changed)
  213.             if ($orderItemNote->getOrderItem() === $this) {
  214.                 $orderItemNote->setOrderItem(null);
  215.             }
  216.         }
  217.         return $this;
  218.     }
  219.     public function getPackEngagement(): ?PackEngagement
  220.     {
  221.         return $this->packEngagement;
  222.     }
  223.     public function setPackEngagement(?PackEngagement $packEngagement): self
  224.     {
  225.         $this->packEngagement $packEngagement;
  226.         return $this;
  227.     }
  228.     /**
  229.      * Get the value of shipmentItem
  230.      */ 
  231.     public function getShipmentItem()
  232.     {
  233.         return $this->shipmentItem;
  234.     }
  235.     /**
  236.      * Set the value of shipmentItem
  237.      *
  238.      * @return  self
  239.      */ 
  240.     public function setShipmentItem($shipmentItem)
  241.     {
  242.         $this->shipmentItem $shipmentItem;
  243.         return $this;
  244.     }
  245.     public function getImage(): ?string
  246.     {
  247.         if($this->getProduct()){
  248.             return $this->getProduct()->getImage();
  249.         }
  250.         return $this->image;
  251.     }
  252.     public function setImage(?string $image): self
  253.     {
  254.         $this->image $image;
  255.         return $this;
  256.     }
  257.     #[Groups(['readPackEngagements'])]
  258.     public function getOrderNumber(){
  259.         return $this->getParentOrder()->getOrderNumber();
  260.     }
  261.     public function getShippingFees(): ?float
  262.     {
  263.         return $this->shippingFees;
  264.     }
  265.     public function setShippingFees(?float $shippingFees): static
  266.     {
  267.         $this->shippingFees $shippingFees;
  268.         return $this;
  269.     }
  270.     public function getCurrency(): ?string
  271.     {
  272.         return $this->currency;
  273.     }
  274.     public function setCurrency(?string $currency): static
  275.     {
  276.         $this->currency $currency;
  277.         return $this;
  278.     }
  279.     public function getMetaData(): ?array
  280.     {
  281.         return $this->metaData;
  282.     }
  283.     public function setMetaData(?array $metaData): static
  284.     {
  285.         $this->metaData $metaData;
  286.         return $this;
  287.     }
  288.     /**
  289.      * Get the value of decimalQuantity
  290.      */ 
  291.     public function getDecimalQuantity()
  292.     {
  293.         return $this->decimalQuantity;
  294.     }
  295.     /**
  296.      * Set the value of decimalQuantity
  297.      *
  298.      * @return  self
  299.      */ 
  300.     public function setDecimalQuantity($decimalQuantity)
  301.     {
  302.         $this->decimalQuantity $decimalQuantity;
  303.         return $this;
  304.     }
  305. }