src/Entity/Order.php line 19

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use Doctrine\Common\Collections\ArrayCollection;
  4. use Doctrine\Common\Collections\Collection;
  5. use Doctrine\ORM\Mapping as ORM;
  6. use Symfony\Component\Validator\Context\ExecutionContextInterface;
  7. use Wits\PaymentBundle\Entity\PayableInterface;
  8. use Wits\PaymentBundle\Entity\Payment;
  9. use Gedmo\Mapping\Annotation as Gedmo;
  10. use Symfony\Component\Validator\Constraints as Assert;
  11. /**
  12.  * @ORM\Table(name="order_pack")
  13.  * @ORM\Entity(repositoryClass="App\Repository\OrderRepository")
  14.  * @ORM\EntityListeners({"App\Listener\OrderListener"})
  15.  */
  16. class Order implements PayableInterface
  17. {
  18.     /**
  19.      * @ORM\Id()
  20.      * @ORM\GeneratedValue()
  21.      * @ORM\Column(type="integer")
  22.      */
  23.     private $id;
  24.     /**
  25.      * @ORM\Column(type="datetime")
  26.      * @Gedmo\Timestampable(on="create")
  27.      */
  28.     private $createdAt;
  29.     /**
  30.      * @ORM\ManyToOne(targetEntity="App\Entity\CreditPack")
  31.      * @ORM\JoinColumn(nullable=true)
  32.      */
  33.     private $pack;
  34.     /**
  35.      * when this order is issued in a "pay as you go" process, this collection will contain the related credit costs
  36.      * @ORM\OneToMany(targetEntity=CreditCost::class, mappedBy="order")
  37.      */
  38.     private Collection $creditCosts;
  39.     /**
  40.      * @ORM\OneToMany(targetEntity=StoreIndexation::class, mappedBy="order", cascade={"persist", "remove"})
  41.      */
  42.     private Collection $storeIndexations;
  43.     /**
  44.      * @ORM\OneToOne(targetEntity="Wits\PaymentBundle\Entity\Payment")
  45.      * @ORM\JoinColumn(nullable=true)
  46.      */
  47.     private ?Payment $payment null;
  48.     /**
  49.      * @ORM\OneToMany(targetEntity="Wits\PaymentBundle\Entity\Payment", mappedBy="order")
  50.      */
  51.     private Collection $payments;
  52.     /**
  53.      * @ORM\ManyToOne(targetEntity="App\Entity\User")
  54.      */
  55.     private $user;
  56.     /**
  57.      * @ORM\OneToOne(targetEntity="App\Entity\Invoice", inversedBy="order", cascade={"persist", "remove"})
  58.      */
  59.     private $invoice;
  60.     /**
  61.      * @ORM\ManyToOne(targetEntity="App\Entity\Freelancer", inversedBy="orders")
  62.      * @ORM\JoinColumn(nullable=true)
  63.      */
  64.     private $freelancer;
  65.     /**
  66.      * @ORM\ManyToOne(targetEntity=CentralPurchasing::class, inversedBy="orders")
  67.      */
  68.     private $centralPurchasing;
  69.     public function __construct()
  70.     {
  71.         $this->creditCosts = new ArrayCollection();
  72.         $this->storeIndexations = new ArrayCollection();
  73.         $this->payments = new ArrayCollection();
  74.     }
  75.     public function getLabel(): string
  76.     {
  77.         return 'Commande #' $this->id;
  78.     }
  79.     /**
  80.      * Get the description of the order (pack, store indexation, etc...)
  81.      * @return string
  82.      */
  83.     public function getDescription(): string
  84.     {
  85.         if ($this->getPack()) {
  86.             return $this->getPack()->getName();
  87.         } elseif ($this->hasStoreIndexation()) {
  88.             return 'Indexation de centre (' $this->getStoreIndexations()->count() . ' centre(s) )';
  89.         } elseif ($this->hasCreditCost()) {
  90.             return 'Pay as you go (' $this->getCreditCosts()->count() . ' contact(s) )';
  91.         }
  92.         return '-';
  93.     }
  94.     public function getId(): ?int
  95.     {
  96.         return $this->id;
  97.     }
  98.     public function getPack(): ?CreditPack
  99.     {
  100.         return $this->pack;
  101.     }
  102.     public function setPack(?CreditPack $pack): self
  103.     {
  104.         $this->pack $pack;
  105.         return $this;
  106.     }
  107.     public function hasPack(): bool
  108.     {
  109.         return !is_null($this->pack);
  110.     }
  111.     public function setPayment(Payment $payment)
  112.     {
  113.         $this->payment $payment;
  114.         $this->addPayment($payment);
  115.     }
  116.     public function getPayment(): ?Payment
  117.     {
  118.         return $this->payment;
  119.     }
  120.     public function isLastPaymentConfirmed(): bool
  121.     {
  122.         return !is_null($this->payment) && $this->payment->isConfirmed();
  123.     }
  124.     public function getLineItems(): array
  125.     {
  126.         return [$this->pack];
  127.     }
  128.     /**
  129.      * @return mixed
  130.      */
  131.     public function getUser()
  132.     {
  133.         return $this->user;
  134.     }
  135.     /**
  136.      * @param mixed $user
  137.      */
  138.     public function setUser($user): void
  139.     {
  140.         $this->user $user;
  141.     }
  142.     public function getInvoice(): ?Invoice
  143.     {
  144.         return $this->invoice;
  145.     }
  146.     public function setInvoice(?Invoice $invoice): self
  147.     {
  148.         $this->invoice $invoice;
  149.         return $this;
  150.     }
  151.     public function getFreelancer(): ?Freelancer
  152.     {
  153.         return $this->freelancer;
  154.     }
  155.     public function setFreelancer(?Freelancer $freelancer): self
  156.     {
  157.         $this->freelancer $freelancer;
  158.         return $this;
  159.     }
  160.     /**
  161.      * @return mixed
  162.      */
  163.     public function getCreatedAt()
  164.     {
  165.         return $this->createdAt;
  166.     }
  167.     /**
  168.      * @param mixed $createdAt
  169.      */
  170.     public function setCreatedAt($createdAt): void
  171.     {
  172.         $this->createdAt $createdAt;
  173.     }
  174.     public function getCentralPurchasing(): ?CentralPurchasing
  175.     {
  176.         return $this->centralPurchasing;
  177.     }
  178.     public function setCentralPurchasing(?CentralPurchasing $centralPurchasing): self
  179.     {
  180.         $this->centralPurchasing $centralPurchasing;
  181.         return $this;
  182.     }
  183.     /**
  184.      * return the owner of the order: freelancer or centralPurchasing
  185.      * @return |null
  186.      */
  187.     public function getOwner()
  188.     {
  189.         if ($this->freelancer) {
  190.             return $this->freelancer;
  191.         } elseif ($this->centralPurchasing) {
  192.             return $this->centralPurchasing;
  193.         }
  194.         return null;
  195.     }
  196.     public function getCreditCosts(): Collection
  197.     {
  198.         return $this->creditCosts;
  199.     }
  200.     public function addCreditCost(CreditCost $creditCost): void
  201.     {
  202.         $creditCost->setOrder($this);
  203.         $this->creditCosts->add($creditCost);
  204.     }
  205.     public function hasCreditCost(): bool
  206.     {
  207.         return $this->creditCosts->count() > 0;
  208.     }
  209.     public function getStoreIndexations(): Collection
  210.     {
  211.         return $this->storeIndexations;
  212.     }
  213.     public function addStoreIndexation(StoreIndexation $storeIndexation): void
  214.     {
  215.         $storeIndexation->setOrder($this);
  216.         $this->storeIndexations->add($storeIndexation);
  217.     }
  218.     public function hasStoreIndexation(): bool
  219.     {
  220.         return $this->storeIndexations->count() > 0;
  221.     }
  222.     /**
  223.      * @Assert\Callback()
  224.      */
  225.     public function validate(ExecutionContextInterface $context$payload)
  226.     {
  227.         // if anything but single, situation date must be filled
  228.         if (
  229.             (is_null($this->getCentralPurchasing()) && is_null($this->getFreelancer())) ||
  230.             (!is_null($this->getCentralPurchasing()) && !is_null($this->getFreelancer()))
  231.         ) {
  232.             $context->buildViolation('Vous devez spĂ©cifier un indĂ©pendant OU une central d\'achat')
  233.                 ->atPath('centralPurchasing')
  234.                 ->addViolation();
  235.         }
  236.     }
  237.     public function isPaid(): bool
  238.     {
  239.         return $this->payment && $this->payment->isConfirmed();
  240.     }
  241.     public function getPayments(): Collection
  242.     {
  243.         return $this->payments;
  244.     }
  245.     public function addPayment(Payment $payment): void
  246.     {
  247.         $payment->setOrder($this);
  248.         $this->payments->add($payment);
  249.     }
  250.     public function hasPayment(): bool
  251.     {
  252.         return $this->payments->count() > 0;
  253.     }
  254. }