src/Entity/Order/Order.php line 25

Open in your IDE?
  1. <?php
  2. namespace App\Entity\Order;
  3. use App\Entity\PaymentMethod\PaymentMethod;
  4. use App\Entity\ProduitCarrier;
  5. use App\Repository\OrderRepository;
  6. use Doctrine\Common\Collections\ArrayCollection;
  7. use Doctrine\Common\Collections\Collection;
  8. use Doctrine\DBAL\Types\Types;
  9. use Doctrine\ORM\Mapping as ORM;
  10. use Doctrine\Bundle\DoctrineBundle\Attribute\AsEntityListener;
  11. use Doctrine\ORM\Events;
  12. use Doctrine\ORM\Event\PrePersistEventArgs;
  13. use App\Entity\Promo\Promo;
  14. use App\Entity\Address\{BillingAddressShippingAddress};
  15. use App\Entity\Customer;
  16. use App\Entity\Carrier\Carrier;
  17. use App\EventListener\OrderListener;
  18. use App\Entity\Produit\Produit;
  19. #[ORM\Entity(repositoryClassOrderRepository::class)]
  20. #[ORM\Table(name'`order`')]
  21. #[ORM\HasLifecycleCallbacks]
  22. class Order
  23. {
  24.     #[ORM\Id]
  25.     #[ORM\GeneratedValue]
  26.     #[ORM\Column(type'integer')]
  27.     private $id;
  28.     #[ORM\Column(type'datetime')]
  29.     private $createdAt;
  30.     #[ORM\Column(type'datetime')]
  31.     private $updatedAt;
  32.     #[ORM\Column(type'integer')]
  33.     private $state;
  34.     #[ORM\Column(type'decimal'precision10scale2nullable:true)]
  35.     private $totalPriceHt;
  36.     #[ORM\Column(type'decimal'precision10scale2nullable:true)]
  37.     private $totalVat;
  38.     #[ORM\Column(type'decimal'precision10scale2nullable:true)]
  39.     private $totalPrice;
  40.     #[ORM\ManyToOne(targetEntityCustomer::class, inversedBy'orders')]
  41.     private $customer;
  42.     #[ORM\ManyToOne(targetEntityBillingAddress::class, inversedBy'ordersBilling'cascade: ['persist'])]
  43.     private $billingAddress;
  44.     #[ORM\ManyToOne(targetEntityShippingAddress::class, inversedBy'ordersShipping'cascade: ['persist'])]
  45.     private $shippingAddress;
  46.     #[ORM\OneToMany(mappedBy'orderentity'targetEntityOrderLine::class, orphanRemovaltrue cascade: ["persist""remove"])]
  47.     private Collection $orderLines;
  48.     #[ORM\Column(typeTypes::DECIMALprecision10scale2nullabletrue)]
  49.     private ?string $discount null;
  50.     #[ORM\Column(length255nullabletrue)]
  51.     private ?string $discountType null;
  52.     #[ORM\Column(typeTypes::DECIMALprecision10scale2nullabletrue)]
  53.     private ?string $totalWithoutDiscount null;
  54.     #[ORM\Column(type'string'length128uniquetruenullable:true)]
  55.     private ?string $numOrder null;
  56.     #[ORM\ManyToOne(inversedBy'orders')]
  57.     private ?Promo $promo null;
  58.     #[ORM\Column(typeTypes::DATETIME_MUTABLEnullabletrue)]
  59.     private ?\DateTimeInterface $datePayment null;
  60.     #[ORM\Column(nullabletrue)]
  61.     private ?bool $acceptCommunication null;
  62.     #[ORM\ManyToOne(inversedBy'orders')]
  63.     private ?Carrier $carrier null;
  64.     #[ORM\ManyToOne(inversedBy'orders')]
  65.     private ?PaymentMethod $paymentMethod null;
  66.     #[ORM\Column(typeTypes::DECIMALprecision10scale2nullabletrue)]
  67.     private ?string $totalCarrierWithTaxes null;
  68.     #[ORM\Column(typeTypes::DECIMALprecision10scale2nullabletrue)]
  69.     private ?string $totalCarrierWithoutTaxes null;
  70.     #[ORM\Column(length128uniquetruenullable:true)]
  71.     private $slug;
  72.     #[ORM\Column(typeTypes::DECIMALprecision10scale2nullabletrue)]
  73.     private ?string $totalDiscountHt null;
  74.     #[ORM\Column(typeTypes::DECIMALprecision10scale2nullabletrue)]
  75.     private ?string $totalDiscountTtc null;
  76.     #[ORM\Column(typeTypes::DECIMALprecision10scale2nullabletrue)]
  77.     private ?string $totalPriceTtc null;
  78.     #[ORM\Column(typeTypes::DECIMALprecision10scale2nullabletrue)]
  79.     private ?string $totalPriceTtcFinal null;
  80.     #[ORM\Column(typeTypes::DECIMALprecision10scale2nullabletrue)]
  81.     private ?string $totalPriceHtFinal null;
  82.     #[ORM\Column(nullabletrue)]
  83.     private ?int $typeCommand null;
  84.     #[ORM\Column(length255nullabletrue)]
  85.     private ?string $email null;
  86.     #[ORM\Column(nullabletrue)]
  87.     private ?int $number null;
  88.     #[ORM\OneToMany(mappedBy'ord'targetEntityPayment::class)]
  89.     private Collection $payments;
  90.     #[ORM\Column(typeTypes::DECIMALprecision10scale2nullabletrue)]
  91.     private ?string $amountDue null;
  92.     const STATE_PAID ;
  93.     const STATE_VALIDATE ;
  94.     const STATE_NO_PAID ;
  95.     const STATE_REFUSED ;
  96.     const STATE_CANCELED ;
  97.     const STATE_REFUND 5;
  98.     const STATE_CART 6;
  99.     const STATE_PREP 7;
  100.     const STATE_SEND 8;
  101.     const DISCOUNT_TYPE_PERCENTAGE 1;
  102.     const DISCOUNT_TYPE_EURO 2;
  103.     const TYPE_COMMAND 0;
  104.     const TYPE_DEVIS 1;
  105.     const DISCOUNT_TYPE_CHOICES = [
  106.         'percentage' => self::DISCOUNT_TYPE_PERCENTAGE,
  107.         'euro' => self::DISCOUNT_TYPE_EURO,
  108.     ];
  109.     public function __construct()
  110.     {
  111.         $this->orderLines = new ArrayCollection();
  112.         $this->payments = new ArrayCollection();
  113.     }
  114.     public function getId(): ?int
  115.     {
  116.         return $this->id;
  117.     }
  118.     public function getState(): ?int
  119.     {
  120.         return $this->state;
  121.     }
  122.     public function setState(int $state): self
  123.     {
  124.         $this->state $state;
  125.         return $this;
  126.     }
  127.     public function getTotalPriceHt(): ?string
  128.     {
  129.         return $this->totalPriceHt;
  130.     }
  131.     public function setTotalPriceHt(string $totalPriceHt): self
  132.     {
  133.         $this->totalPriceHt $totalPriceHt;
  134.         return $this;
  135.     }
  136.     public function getTotalVat(): ?string
  137.     {
  138.         return $this->totalVat;
  139.     }
  140.     public function setTotalVat(string $totalVat): self
  141.     {
  142.         $this->totalVat $totalVat;
  143.         return $this;
  144.     }
  145.     public function getTotalPrice(): ?string
  146.     {
  147.         return $this->totalPrice;
  148.     }
  149.     public function setTotalPrice(string $totalPrice): self
  150.     {
  151.         $this->totalPrice $totalPrice;
  152.         return $this;
  153.     }
  154.     public function getCustomer(): ?Customer
  155.     {
  156.         return $this->customer;
  157.     }
  158.     public function setCustomer(?Customer $customer): self
  159.     {
  160.         $this->customer $customer;
  161.         return $this;
  162.     }
  163.     public function getBillingAddress(): ?BillingAddress
  164.     {
  165.         return $this->billingAddress;
  166.     }
  167.     public function setBillingAddress(?BillingAddress $billingAddress): self
  168.     {
  169.         $this->billingAddress $billingAddress;
  170.         return $this;
  171.     }
  172.     public function getShippingAddress(): ?ShippingAddress
  173.     {
  174.         return $this->shippingAddress;
  175.     }
  176.     public function setShippingAddress(?ShippingAddress $shippingAddress): self
  177.     {
  178.         $this->shippingAddress $shippingAddress;
  179.         return $this;
  180.     }
  181.     /**
  182.      * @return Collection<int, OrderLine>
  183.      */
  184.     public function getOrderLines(): Collection
  185.     {
  186.         return $this->orderLines;
  187.     }
  188.     public function clearOrderLines(){
  189.         $this->orderLines = new ArrayCollection();
  190.         return $this;
  191.     }
  192.     public function addOrderLine(OrderLine $orderLine): self
  193.     {
  194.         if (!$this->orderLines->contains($orderLine)) {
  195.             $this->orderLines->add($orderLine);
  196.             $orderLine->setOrderentity($this);
  197.         }
  198.         return $this;
  199.     }
  200.     public function removeOrderLine(OrderLine $orderLine): self
  201.     {
  202.         if ($this->orderLines->removeElement($orderLine)) {
  203.             // set the owning side to null (unless already changed)
  204.             if ($orderLine->getOrderentity() === $this) {
  205.                 $orderLine->setOrderentity(null);
  206.             }
  207.         }
  208.         return $this;
  209.     }
  210.      public function getStateText(){
  211.         if($this->getState() == '') return '';
  212.         return $this->getStates()[$this->getState()];
  213.     }
  214.     public static function getStatesChoice(){
  215.         return array_flip([
  216.             self::STATE_PAID => 'state.paid',
  217.             self::STATE_REFUSED => 'state.refused',
  218.             self::STATE_VALIDATE => 'state.validate',
  219.             self::STATE_NO_PAID => 'state.nopaid',
  220.             self::STATE_CANCELED => 'state.canceled',
  221.             self::STATE_REFUND => 'state.refund',
  222.             self::STATE_PREP => 'state.prep',
  223.             self::STATE_SEND=> 'state.send',
  224.         ]);
  225.     }
  226.      public static function getStates(){
  227.         return ([
  228.             self::STATE_PAID => 'state.paid',
  229.             self::STATE_REFUSED => 'state.refused',
  230.             self::STATE_VALIDATE => 'state.validate',
  231.             self::STATE_NO_PAID => 'state.nopaid',
  232.             self::STATE_CANCELED => 'state.canceled',
  233.             self::STATE_REFUND => 'state.refund',
  234.             self::STATE_PREP => 'state.prep',
  235.             self::STATE_SEND=> 'state.send'
  236.         ]);
  237.     }
  238.     public function getTypeCommandText(){
  239.         return $this->getNumOrder();
  240.         //if($this->getTypeCommand() == '') return '';
  241.         //return $this->getTypeCommand() == 0 ? "ORD ".$this->getNumOrder() : "DEV ".$this->getNumOrder();
  242.     }
  243.     public static function getTypeCommandChoice(){
  244.         return array_flip([
  245.             self::TYPE_COMMAND => 'Commande',
  246.             self::TYPE_DEVIS => 'Devis',
  247.         ]);
  248.     }
  249.     public static function getTypesCommand(){
  250.         return ([
  251.             self::TYPE_COMMAND => 'typeCommand.command',
  252.             self::TYPE_DEVIS => 'typeCommand.devis',
  253.         ]);
  254.     }
  255.     public function getDiscount()
  256.     {
  257.         return $this->discount;
  258.     }
  259.     public function setDiscount($discount): self
  260.     {
  261.         $this->discount $discount;
  262.         return $this;
  263.     }
  264.     public function getDiscountType(): ?string
  265.     {
  266.         return $this->discountType;
  267.     }
  268.     public function setDiscountType(?string $discountType): self
  269.     {
  270.         $this->discountType $discountType;
  271.         return $this;
  272.     }
  273.     public function getTotalWithoutDiscount(): ?string
  274.     {
  275.         return $this->totalWithoutDiscount;
  276.     }
  277.     public function setTotalWithoutDiscount(string $totalWithoutDiscount): self
  278.     {
  279.         $this->totalWithoutDiscount $totalWithoutDiscount;
  280.         return $this;
  281.     }
  282.     public function getNumOrder(): ?string
  283.     {
  284.         return $this->numOrder;
  285.     }
  286.     public function setNumOrder(?string $numOrder): self
  287.     {
  288.         $this->numOrder $numOrder;
  289.         return $this;
  290.     }
  291.     public function getPromo(): ?Promo
  292.     {
  293.         return $this->promo;
  294.     }
  295.     public function setPromo(?Promo $promo): self
  296.     {
  297.         $this->promo $promo;
  298.         return $this;
  299.     }
  300.     public function getDatePayment(): ?\DateTimeInterface
  301.     {
  302.         return $this->datePayment;
  303.     }
  304.     public function setDatePayment(?\DateTimeInterface $datePayment): self
  305.     {
  306.         $this->datePayment $datePayment;
  307.         return $this;
  308.     }
  309.     public function isAcceptCommunication(): ?bool
  310.     {
  311.         return $this->acceptCommunication;
  312.     }
  313.     public function setAcceptCommunication(?bool $acceptCommunication): self
  314.     {
  315.         $this->acceptCommunication $acceptCommunication;
  316.         return $this;
  317.     }
  318.     public function getCarrier(): ?Carrier
  319.     {
  320.         return $this->carrier;
  321.     }
  322.     public function setCarrier(?Carrier $carrier): static
  323.     {
  324.         $this->carrier $carrier;
  325.         return $this;
  326.     }
  327.     public function getPaymentMethod(): ?PaymentMethod
  328.     {
  329.         return $this->paymentMethod;
  330.     }
  331.     public function setPaymentMethod(?PaymentMethod $paymentMethod): static
  332.     {
  333.         $this->paymentMethod $paymentMethod;
  334.         return $this;
  335.     }
  336.     public function getTotalCarrierWithTaxes(): ?string
  337.     {
  338.         return $this->totalCarrierWithTaxes;
  339.     }
  340.     public function setTotalCarrierWithTaxes(?string $totalCarrierWithTaxes): static
  341.     {
  342.         $this->totalCarrierWithTaxes $totalCarrierWithTaxes;
  343.         return $this;
  344.     }
  345.     public function getTotalCarrierWithoutTaxes(): ?string
  346.     {
  347.         return $this->totalCarrierWithoutTaxes;
  348.     }
  349.     public function setTotalCarrierWithoutTaxes(string $totalCarrierWithoutTaxes): static
  350.     {
  351.         $this->totalCarrierWithoutTaxes $totalCarrierWithoutTaxes;
  352.         return $this;
  353.     }
  354.         public function getCreatedAt(): ?\DateTimeInterface
  355.     {
  356.         return $this->createdAt;
  357.     }
  358.     public function setCreatedAt(\DateTimeInterface $createdAt): self
  359.     {
  360.         $this->createdAt $createdAt;
  361.         return $this;
  362.     }
  363.     public function getUpdatedAt(): ?\DateTimeInterface
  364.     {
  365.         return $this->updatedAt;
  366.     }
  367.     public function setUpdatedAt(\DateTimeInterface $updatedAt): self
  368.     {
  369.         $this->updatedAt $updatedAt;
  370.         return $this;
  371.     }
  372.     public function updateTotaux()
  373.     {
  374.         $totalHt 0;
  375.         $totalTtc 0;
  376.         $carrierHt 0;
  377.         $carrierTtc 0;
  378.         $carrier $this->getCarrier();
  379.         if(!empty($this->getOrderLines()->toArray())){
  380.             foreach($this->getOrderLines()->toArray() as $line){
  381.                 $line->updateTotalLine();
  382.                 $totalHt += $line->getTotalLineHt();
  383.                 $totalTtc += $line->getTotalLineTtc();
  384.                 if(!empty($carrier) and !$carrier->isIsFree()){
  385.                     $carrier_produit $line->getProduit()->getProduitCarriers()->toArray();
  386.                     if(!empty($carrier_produit) and array_key_exists($carrier->getId(), $carrier_produit)){
  387.                         $carrierHt += $carrier_produit[$carrier->getId()]->getPriceHt();
  388.                         $carrierTtc += $carrier_produit[$carrier->getId()]->getPriceTtc();
  389.                     }
  390.                 }
  391.             }
  392.         }
  393.         $totalTtc $totalTtc $carrierTtc;
  394.         $totalHt $totalHt $carrierHt;
  395.  
  396.         $this->setTotalWithoutDiscount($totalTtc);
  397.         $totalDiscountHt 0;
  398.         $totalDiscountTtc 0;
  399.         //CORRIGER
  400.         if(!empty($this->getDiscountType())){
  401.             $discount $this->getDiscount();
  402.             if($this->getDiscountType() == Order::DISCOUNT_TYPE_EURO){
  403.                 $totalDiscountTtc $discount;
  404.                 $totalDiscountHt $discount - ($discount 0.21);
  405.             }else if($this->getDiscountType() == Order::DISCOUNT_TYPE_PERCENTAGE){
  406.                 $dicountFix = ($discount/100);
  407.                 $totalDiscountTtc $totalTtc $dicountFix;
  408.                 $totalDiscountHt =  $totalHt $dicountFix;
  409.             }
  410.         }else if($this->getPromo() and !empty($this->getPromo()->getDiscount())){
  411.             $discount $this->getPromo()->getDiscount();
  412.             $dicountFix = ($discount/100);
  413.             $totalDiscountTtc $totalTtc $dicountFix;
  414.             $totalDiscountHt $totalHt $dicountFix;
  415.         }   
  416.         $this->setTotalCarrierWithoutTaxes($carrierHt);
  417.         $this->setTotalCarrierWithTaxes($carrierTtc);
  418.         $this->setTotalPriceHt($totalHt);
  419.         $this->setTotalPriceTtc($totalTtc);
  420.         $this->setTotalDiscountHt($totalDiscountHt);
  421.         $this->setTotalDiscountTtc($totalDiscountTtc);
  422.         $this->setTotalPriceTtcFinal($totalTtc-$totalDiscountTtc);
  423.         $this->setTotalPriceHtFinal($totalHt-$totalDiscountHt);
  424.         $this->setTotalVat($totalTtc$totalHt);
  425.     }
  426.     public function getTotalProductsTtc(){
  427.         $total 0;
  428.         foreach($this->getOrderLines()->toArray() as $line){
  429.             $total += $line->getTotalTtcProduct();
  430.         }
  431.         return $total;
  432.     }
  433.     public function setSlug($slug)
  434.     {
  435.         $this->slug $slug;
  436.         return $this;
  437.     }
  438.     public function getSlug()
  439.     {
  440.         return $this->slug;
  441.     }
  442.     public function getTotalDiscountHt(): ?string
  443.     {
  444.         return $this->totalDiscountHt;
  445.     }
  446.     public function setTotalDiscountHt(?string $totalDiscountHt): static
  447.     {
  448.         $this->totalDiscountHt $totalDiscountHt;
  449.         return $this;
  450.     }
  451.     public function getTotalDiscountTtc(): ?string
  452.     {
  453.         return $this->totalDiscountTtc;
  454.     }
  455.     public function setTotalDiscountTtc(?string $totalDiscountTtc): static
  456.     {
  457.         $this->totalDiscountTtc $totalDiscountTtc;
  458.         return $this;
  459.     }
  460.     public function getTotalPriceTtc(): ?string
  461.     {
  462.         return $this->totalPriceTtc;
  463.     }
  464.     public function setTotalPriceTtc(?string $totalPriceTtc): static
  465.     {
  466.         $this->totalPriceTtc $totalPriceTtc;
  467.         return $this;
  468.     }
  469.     public function getTotalPriceTtcFinal(): ?string
  470.     {
  471.         return $this->totalPriceTtcFinal;
  472.     }
  473.     public function setTotalPriceTtcFinal(?string $totalPriceTtcFinal): static
  474.     {
  475.         $this->totalPriceTtcFinal $totalPriceTtcFinal;
  476.         return $this;
  477.     }
  478.     public function getTotalPriceHtFinal(): ?string
  479.     {
  480.         return $this->totalPriceHtFinal;
  481.     }
  482.     public function setTotalPriceHtFinal(?string $totalPriceHtFinal): static
  483.     {
  484.         $this->totalPriceHtFinal $totalPriceHtFinal;
  485.         return $this;
  486.     }
  487.     public function getTypeCommand(): ?int
  488.     {
  489.         return $this->typeCommand;
  490.     }
  491.     public function setTypeCommand(?int $typeCommand): static
  492.     {
  493.         $this->typeCommand $typeCommand;
  494.         return $this;
  495.     }
  496.     public function getEmail(): ?string
  497.     {
  498.         return $this->email;
  499.     }
  500.     public function setEmail(?string $email): static
  501.     {
  502.         $this->email $email;
  503.         return $this;
  504.     }
  505.     public function getNumber(): ?int
  506.     {
  507.         return $this->number;
  508.     }
  509.     public function setNumber(?int $number): static
  510.     {
  511.         $this->number $number;
  512.         return $this;
  513.     }
  514.     /**
  515.      * @return Collection<int, Payment>
  516.      */
  517.     public function getPayments(): Collection
  518.     {
  519.         return $this->payments;
  520.     }
  521.     public function addPayment(Payment $payment): static
  522.     {
  523.         if (!$this->payments->contains($payment)) {
  524.             $this->payments->add($payment);
  525.             $payment->setOrd($this);
  526.         }
  527.         return $this;
  528.     }
  529.     public function removePayment(Payment $payment): static
  530.     {
  531.         if ($this->payments->removeElement($payment)) {
  532.             // set the owning side to null (unless already changed)
  533.             if ($payment->getOrd() === $this) {
  534.                 $payment->setOrd(null);
  535.             }
  536.         }
  537.         return $this;
  538.     }
  539.     public function getAmountDue(): ?string
  540.     {
  541.         return $this->amountDue;
  542.     }
  543.     public function setAmountDue(?string $amountDue): static
  544.     {
  545.         $this->amountDue $amountDue;
  546.         return $this;
  547.     }
  548. }