src\Entity\Order.php line 17

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Entity;
  4. use DateTime;
  5. use Doctrine\Common\Collections\ArrayCollection;
  6. use Doctrine\Common\Collections\Collection;
  7. use Doctrine\ORM\Mapping as ORM;
  8. use Symfony\Component\Validator\Constraints as Assert;
  9. /**
  10.  * @ORM\Table(name="`order`")
  11.  * @ORM\Entity(repositoryClass="App\Repository\OrderRepository")
  12.  */
  13. class Order
  14. {
  15.     public const STATE_PENDING 0;
  16.     public const STATE_ACCEPTED 1;
  17.     public const STATE_PAID 2;
  18.     public const STATE_PAYMENTPENDING 3;
  19.     public const STATE_REFUNDED 4;
  20.     public const STATE_CANCELED 5;
  21.     public const PAYMENTMODE_CB 0;
  22.     public const PAYMENTMODE_WIRE 1;
  23.     public const PAYMENTMODE_OFFLINE 2;
  24.     /**
  25.      * @ORM\Column(name="id", type="integer")
  26.      * @ORM\Id
  27.      * @ORM\GeneratedValue(strategy="AUTO")
  28.      */
  29.     private $id;
  30.     /**
  31.      * @ORM\Column(type="string", length=10, nullable=false)
  32.      */
  33.     private $reference;
  34.     /**
  35.      * @ORM\OneToOne(targetEntity="App\Entity\Payment")
  36.      */
  37.     private $payment;
  38.     /**
  39.      * @ORM\OneToOne(targetEntity="App\Entity\UserSurvey", inversedBy="order", cascade={"persist","remove"})
  40.      * @ORM\JoinColumn(name="user_survey_id", referencedColumnName="id", onDelete="CASCADE")
  41.      */
  42.     private $userSurvey;
  43.     /**
  44.      * @ORM\Column(type="decimal", precision=9, scale=3, nullable=false)
  45.      */
  46.     private $amount;
  47.     /**
  48.      * @ORM\Column(type="decimal", precision=9, scale=3, nullable=false)
  49.      */
  50.     private $amountNoDiscount;
  51.     /**
  52.      * @ORM\Column(type="string", length=3, nullable=false)
  53.      */
  54.     private $currency;
  55.     /**
  56.      * @ORM\Column(type="smallint", nullable=true)
  57.      */
  58.     private $paymentMode;
  59.     /**
  60.      * @ORM\Column(type="string", length=50, nullable=true)
  61.      */
  62.     private $paymentProvider;
  63.     /**
  64.      * @ORM\Column(type="string", length=50, nullable=true)
  65.      */
  66.     private $paymentStatus;
  67.     /**
  68.      * @ORM\Column(type="smallint", nullable=false)
  69.      */
  70.     private $state;
  71.     private $stateLabel;
  72.     /**
  73.      * @Assert\NotBlank(groups={"order_confirm"})
  74.      * @Assert\NotNull(groups={"order_confirm"})
  75.      * @ORM\Column(type="boolean", nullable=false)
  76.      */
  77.     private $isLegalNoticeValidated;
  78.     /**
  79.      * @ORM\Column(type="datetime", nullable=true)
  80.      */
  81.     private $dateAdd;
  82.     /**
  83.      * @ORM\Column(type="datetime", nullable=true)
  84.      */
  85.     private $dateUpdate;
  86.     /**
  87.      * @ORM\Column(type="datetime", nullable=true)
  88.      */
  89.     private $dateLegalNoticeValidated;
  90.     /**
  91.      * @ORM\Column(type="datetime", nullable=true)
  92.      */
  93.     private $datePayment;
  94.     /**
  95.      * @ORM\Column(type="datetime", nullable=true)
  96.      */
  97.     private $dateCancel;
  98.     /**
  99.      * @ORM\Column(type="boolean", nullable=true)
  100.      */
  101.     private $isCompletedOnce;
  102.     /**
  103.      * @ORM\ManyToMany(targetEntity="App\Entity\Discount", inversedBy="orders",cascade={"persist"})
  104.      * @ORM\JoinColumn(nullable=true)
  105.      */
  106.     private $discounts;
  107.     /**
  108.      * @ORM\Column(type="decimal", precision=9, scale=3, nullable=false)
  109.      */
  110.     private $discountAmount;
  111.     /**
  112.      * @ORM\Column(type="integer", nullable=true)
  113.      */
  114.     private $bexioInvoiceId;
  115.     /**
  116.      * @ORM\OneToMany(targetEntity=OrderSupplement::class, mappedBy="relatedOrder", orphanRemoval=true,cascade={"persist"})
  117.      */
  118.     private $orderSupplements;
  119.     /**
  120.      * @ORM\Column(type="integer", nullable=true)
  121.      */
  122.     private $affiliationCount;
  123.     /**
  124.      * @ORM\Column(type="boolean", nullable=true)
  125.      */
  126.     private $bexioIsPaid;
  127.     /**
  128.      * Order constructor.
  129.      *
  130.      * @throws \Exception
  131.      */
  132.     public function __construct(float      $amount,
  133.                                 UserSurvey $userSurvey,
  134.                                 string     $currency 'CHF',
  135.                                 bool       $isLegalNoticeValidated false,
  136.                                 int        $state Order::STATE_PENDING)
  137.     {
  138.         $this->reference substr(md5($userSurvey->getId() . time()), 010);
  139.         $this->amount $amount;
  140.         $this->amountNoDiscount $amount;
  141.         $this->userSurvey $userSurvey;
  142.         $this->currency $currency;
  143.         $this->isLegalNoticeValidated $isLegalNoticeValidated;
  144.         $this->state $state;
  145.         $this->dateAdd = new DateTime();
  146.         $this->dateUpdate = new DateTime();
  147.         $this->discounts = new ArrayCollection();
  148.         $this->discountAmount 0;
  149.         $this->orderSupplements = new ArrayCollection();
  150.     }
  151.     public function getId(): ?int
  152.     {
  153.         return $this->id;
  154.     }
  155.     public function setId(?int $id): Order
  156.     {
  157.         $this->id $id;
  158.         return $this;
  159.     }
  160.     public function getAmount(): float
  161.     {
  162.         return (float)$this->amount;
  163.     }
  164.     public function setAmount(float $amount): Order
  165.     {
  166.         $this->amount $amount;
  167.         if (!is_null($this->userSurvey)) {
  168.             $this->userSurvey->setPrice($amount);
  169.         }
  170.         return $this;
  171.     }
  172.     public function getCurrency(): string
  173.     {
  174.         return $this->currency;
  175.     }
  176.     public function setCurrency(string $currency): Order
  177.     {
  178.         $this->currency $currency;
  179.         return $this;
  180.     }
  181.     public function isLegalNoticeValidated(): bool
  182.     {
  183.         return $this->isLegalNoticeValidated;
  184.     }
  185.     public function getIsLegalNoticeValidated(): ?bool
  186.     {
  187.         return $this->isLegalNoticeValidated;
  188.     }
  189.     public function setIsLegalNoticeValidated(bool $isLegalNoticeValidated): Order
  190.     {
  191.         $this->isLegalNoticeValidated $isLegalNoticeValidated;
  192.         return $this;
  193.     }
  194.     public function getDateLegalNoticeValidated(): DateTime
  195.     {
  196.         return $this->dateLegalNoticeValidated;
  197.     }
  198.     public function setDateLegalNoticeValidated(DateTime $dateLegalNoticeValidated): Order
  199.     {
  200.         $this->dateLegalNoticeValidated $dateLegalNoticeValidated;
  201.         return $this;
  202.     }
  203.     public function getDatePayment(): ?DateTime
  204.     {
  205.         return $this->datePayment;
  206.     }
  207.     public function setDatePayment(?DateTime $datePayment): Order
  208.     {
  209.         $this->datePayment $datePayment;
  210.         return $this;
  211.     }
  212.     public function getDateCancel(): ?DateTime
  213.     {
  214.         return $this->dateCancel;
  215.     }
  216.     public function setDateCancel(?DateTime $dateCancel): Order
  217.     {
  218.         $this->dateCancel $dateCancel;
  219.         return $this;
  220.     }
  221.     public function getStateLabel(): string
  222.     {
  223.         switch ($this->getState()) {
  224.             case Order::STATE_PAID === $this->getState():
  225.                 $this->setStateLabel('Payée');
  226.                 break;
  227.             case Order::STATE_PAYMENTPENDING === $this->getState():
  228.                 $this->setStateLabel('Paiement en cours');
  229.                 break;
  230.             case Order::STATE_ACCEPTED === $this->getState():
  231.                 $this->setStateLabel('Acceptée');
  232.                 break;
  233.             case Order::STATE_PENDING === $this->getState():
  234.                 $this->setStateLabel('En attente de d\'acceptation');
  235.                 break;
  236.             case Order::STATE_REFUNDED === $this->getState():
  237.                 $this->setStateLabel('Remboursée');
  238.                 break;
  239.             case Order::STATE_CANCELED === $this->getState():
  240.                 $this->setStateLabel('Annulée');
  241.                 break;
  242.             default:
  243.                 $this->setStateLabel('Commande créée');
  244.         }
  245.         return $this->stateLabel;
  246.     }
  247.     public function getState(): int
  248.     {
  249.         return $this->state;
  250.     }
  251.     public function setState(int $state): Order
  252.     {
  253.         $this->state $state;
  254.         return $this;
  255.     }
  256.     private function setStateLabel(string $stateLabel): Order
  257.     {
  258.         $this->stateLabel $stateLabel;
  259.         return $this;
  260.     }
  261.     public function getDateAdd(): ?DateTime
  262.     {
  263.         return $this->dateAdd;
  264.     }
  265.     public function setDateAdd(?DateTime $dateAdd): Order
  266.     {
  267.         $this->dateAdd $dateAdd;
  268.         return $this;
  269.     }
  270.     public function getDateUpdate(): ?DateTime
  271.     {
  272.         return $this->dateUpdate;
  273.     }
  274.     public function setDateUpdate(?DateTime $dateUpdate): Order
  275.     {
  276.         $this->dateUpdate $dateUpdate;
  277.         return $this;
  278.     }
  279.     public function getPayment(): ?Payment
  280.     {
  281.         return $this->payment;
  282.     }
  283.     public function setPayment(?Payment $payment): self
  284.     {
  285.         $this->payment $payment;
  286.         return $this;
  287.     }
  288.     public function getReference(): ?string
  289.     {
  290.         return $this->reference;
  291.     }
  292.     public function setReference(string $reference): self
  293.     {
  294.         $this->reference $reference;
  295.         return $this;
  296.     }
  297.     public function getPaymentMode(): ?int
  298.     {
  299.         return $this->paymentMode;
  300.     }
  301.     public function setPaymentMode(?int $paymentMode): self
  302.     {
  303.         $this->paymentMode $paymentMode;
  304.         return $this;
  305.     }
  306.     public function getPaymentProvider(): ?string
  307.     {
  308.         return $this->paymentProvider;
  309.     }
  310.     public function setPaymentProvider(?string $paymentProvider): self
  311.     {
  312.         $this->paymentProvider $paymentProvider;
  313.         return $this;
  314.     }
  315.     public function isPaid(): ?bool
  316.     {
  317.         if (== $this->state) {
  318.             return true;
  319.         }
  320.         return false;
  321.     }
  322.     public function isPaymentPending(): ?bool
  323.     {
  324.         if ($this->state 2) {
  325.             return true;
  326.         }
  327.         return false;
  328.     }
  329.     public function isPaymentProcessing(): ?bool
  330.     {
  331.         if (== $this->state) {
  332.             return true;
  333.         }
  334.         return false;
  335.     }
  336.     public function getPaymentStatus(): ?string
  337.     {
  338.         return $this->paymentStatus;
  339.     }
  340.     /**
  341.      * Preferable d'utilisé UserSurvey setPaymentStatus,.
  342.      */
  343.     public function setPaymentStatus(?string $paymentStatus): self
  344.     {
  345.         if ('payedout' == $paymentStatus || 'captured' == $paymentStatus) {
  346.             $this->setState(Order::STATE_PAID);
  347.         } elseif ('canceled' == $paymentStatus) {
  348.             $this->setState(Order::STATE_CANCELED);
  349.         } elseif ('pending' == $paymentStatus) {
  350.             $this->setState(Order::STATE_PAYMENTPENDING);
  351.         }
  352.         $this->paymentStatus $paymentStatus;
  353.         return $this;
  354.     }
  355.     public function getIsCompletedOnce(): ?bool
  356.     {
  357.         return $this->isCompletedOnce;
  358.     }
  359.     public function setIsCompletedOnce(?bool $isCompletedOnce): self
  360.     {
  361.         $this->isCompletedOnce $isCompletedOnce;
  362.         return $this;
  363.     }
  364.     public function addDiscount(Discount $discount): self
  365.     {
  366.         if (!$this->discounts->contains($discount)) {
  367.             $this->discounts[] = $discount;
  368.         }
  369.         return $this;
  370.     }
  371.     public function removeDiscount(Discount $discount): self
  372.     {
  373.         if ($this->discounts->contains($discount)) {
  374.             $this->discounts->removeElement($discount);
  375.         }
  376.         return $this;
  377.     }
  378.     public function getAmountNoDiscount(): ?float
  379.     {
  380.         return (float)$this->amountNoDiscount;
  381.     }
  382.     public function setAmountNoDiscount(float $amountNoDiscount): self
  383.     {
  384.         $this->amountNoDiscount $amountNoDiscount;
  385.         return $this;
  386.     }
  387.     public function getBexioInvoiceId(): ?int
  388.     {
  389.         return $this->bexioInvoiceId;
  390.     }
  391.     public function setBexioInvoiceId(int $bexioInvoiceId): self
  392.     {
  393.         $this->bexioInvoiceId $bexioInvoiceId;
  394.         return $this;
  395.     }
  396.     public function updateAmounts($price): void
  397.     {
  398.         $totalSupplements 0;
  399.         $totalReductions 0;
  400.         $priceModified $price;
  401.         /*
  402.          * Les supplements sont appliqués sur le prix de base
  403.          */
  404.         foreach ($this->getOrderSupplements() as $supplement) {
  405.             $supplementAmount $supplement->calculateAmount($price);
  406.             $totalSupplements += $supplementAmount;
  407.             $priceModified += $supplementAmount;
  408.         }
  409.         /*
  410.          * Les réductions sont appliquées sur le prix avec les supplements
  411.          */
  412.         foreach ($this->getDiscounts() as $discount) {
  413.             $reduction 0;
  414.             if ('AFFILIATION' == $discount->getType() && $discount->getUser() == $this->getUserSurvey()->getUser()) {
  415.                 // parrainage
  416.                 if ($this->getAffiliationCount() > 0) {
  417.                     for ($i 1$i <= $this->getAffiliationCount(); ++$i) {
  418.                         if ($priceModified 0) {
  419.                             $reduction += $discount->calculateAmount($priceModified);
  420.                         }
  421.                     }
  422.                 }
  423.             } else {
  424.                 // dans tous les autres cas
  425.                 $reduction $discount->calculateAmount($priceModified);
  426.             }
  427.             $priceModified -= $reduction;
  428.             $totalReductions += $reduction;
  429.         }
  430.         if ($totalReductions $price) {
  431.             $totalReductions $price;
  432.         }
  433.         /*
  434.          * Ensuite, on ajoute les supplement et on déduit les réductions
  435.          */
  436.         $this->setAmountNoDiscount($price);
  437.         $this->setDiscountAmount($totalReductions);
  438.         $this->setAmount(round($price $totalSupplements $totalReductions2));
  439.     }
  440.     /**
  441.      * @return Collection<int, OrderSupplement>
  442.      */
  443.     public function getOrderSupplements(): Collection
  444.     {
  445.         return $this->orderSupplements;
  446.     }
  447.     /**
  448.      * @return Collection|Discount[]
  449.      */
  450.     public function getDiscounts(): Collection
  451.     {
  452.         return $this->discounts;
  453.     }
  454.     public function getUserSurvey(): UserSurvey
  455.     {
  456.         return $this->userSurvey;
  457.     }
  458.     public function setUserSurvey(UserSurvey $userSurvey): Order
  459.     {
  460.         $this->userSurvey $userSurvey;
  461.         return $this;
  462.     }
  463.     public function getAffiliationCount(): ?int
  464.     {
  465.         return $this->affiliationCount;
  466.     }
  467.     public function setAffiliationCount(?int $affiliationCount): self
  468.     {
  469.         $this->affiliationCount $affiliationCount;
  470.         return $this;
  471.     }
  472.     public function getDiscountAmount(): float
  473.     {
  474.         return (float)$this->discountAmount;
  475.     }
  476.     public function setDiscountAmount(float $discountAmount): self
  477.     {
  478.         $this->discountAmount $discountAmount;
  479.         return $this;
  480.     }
  481.     public function addOrderSupplement(OrderSupplement $orderSupplement): self
  482.     {
  483.         if (!$this->orderSupplements->contains($orderSupplement)) {
  484.             $this->orderSupplements[] = $orderSupplement;
  485.             $orderSupplement->setRelatedOrder($this);
  486.         }
  487.         return $this;
  488.     }
  489.     public function removeOrderSupplement(OrderSupplement $orderSupplement): self
  490.     {
  491.         if ($this->orderSupplements->removeElement($orderSupplement)) {
  492.             // set the owning side to null (unless already changed)
  493.             if ($orderSupplement->getRelatedOrder() === $this) {
  494.                 $orderSupplement->setRelatedOrder(null);
  495.             }
  496.         }
  497.         return $this;
  498.     }
  499.     public function hasFidelity(): bool
  500.     {
  501.         foreach ($this->getDiscounts() as $discount) {
  502.             if ('FIDELITY' == $discount->getType()) {
  503.                 return true;
  504.             }
  505.         }
  506.         return false;
  507.     }
  508.     public function hasAffiliation(): bool
  509.     {
  510.         foreach ($this->getDiscounts() as $discount) {
  511.             if ('AFFILIATION' == $discount->getType()) {
  512.                 return true;
  513.             }
  514.         }
  515.         return false;
  516.     }
  517.     public function isBexioIsPaid(): ?bool
  518.     {
  519.         return $this->bexioIsPaid;
  520.     }
  521.     public function setBexioIsPaid(?bool $bexioIsPaid): self
  522.     {
  523.         $this->bexioIsPaid $bexioIsPaid;
  524.         return $this;
  525.     }
  526. }