vendor/bluue/sales-bundle/src/Entity/Order.php line 50

Open in your IDE?
  1. <?php
  2. /**
  3.  * @author Quentin CHATELAIN (contact@scaledev.fr)
  4.  * @copyright 2021 - ScaleDEV SAS, 12 RUE CHARLES MORET, 10120 ST ANDRE LES VERGERS
  5.  * @license commercial
  6.  */
  7. declare(strict_types=1);
  8. namespace Bluue\SalesBundle\Entity;
  9. use App\Entity\Traits\EntityManagerServiceEntity;
  10. use App\Services\SoftdeleteFilter;
  11. use Bluue\SalesBundle\DoctrineExtensions\EditPricesWithTaxEntity;
  12. use DateTime;
  13. use App\Entity\User;
  14. use App\Entity\Context;
  15. use App\Entity\Currency;
  16. use Doctrine\ORM\NonUniqueResultException;
  17. use Symfony\Component\Uid\Uuid;
  18. use Doctrine\ORM\Mapping as ORM;
  19. use Gedmo\Mapping\Annotation as Gedmo;
  20. use Doctrine\Common\Collections\Criteria;
  21. use Bluue\CustomersBundle\Entity\Customer;
  22. use Doctrine\Common\Collections\Collection;
  23. use Doctrine\Common\Collections\ArrayCollection;
  24. use App\Entity\Traits\ConfigurationServiceEntity;
  25. use Bluue\SalesBundle\Repository\OrderRepository;
  26. use Symfony\Bridge\Doctrine\IdGenerator\UuidGenerator;
  27. use App\DoctrineExtensions\Validatable\Traits\UserValidatableEntity;
  28. use App\DoctrineExtensions\Timestampable\Traits\UserTimestampableEntity;
  29. use App\DoctrineExtensions\SoftDeleteable\Traits\UserSoftDeleteableEntity;
  30. use App\Entity\Establishment;
  31. /**
  32.  * @ORM\Entity(repositoryClass=OrderRepository::class)
  33.  * @ORM\Table(name="sales_bundle__order", indexes={
  34.  *  @ORM\Index(name="internal_name", columns={"internal_name"}),
  35.  *  @ORM\Index(name="reference", columns={"reference"}),
  36.  *  @ORM\Index(name="total_amount_untaxed", columns={"total_amount_untaxed"}),
  37.  *  @ORM\Index(name="total_amount", columns={"total_amount"}),
  38.  *  @ORM\Index(name="residual", columns={"residual"}),
  39.  *  @ORM\Index(name="deleted_at", columns={"deleted_at"}),
  40.  *  @ORM\Index(name="created_at", columns={"created_at"}),
  41.  *  @ORM\Index(name="updated_at", columns={"updated_at"})
  42.  * })
  43.  * @Gedmo\SoftDeleteable(fieldName="deletedAt", timeAware=false, hardDelete=false)
  44.  */
  45. class Order
  46. {
  47.     use UserValidatableEntity;
  48.     use UserTimestampableEntity;
  49.     use UserSoftDeleteableEntity;
  50.     use ConfigurationServiceEntity;
  51.     use EditPricesWithTaxEntity;
  52.     use EntityManagerServiceEntity;
  53.     /**
  54.      * @ORM\Id
  55.      * @ORM\Column(type="uuid")
  56.      * @ORM\GeneratedValue(strategy="CUSTOM")
  57.      * @ORM\CustomIdGenerator(class=UuidGenerator::class)
  58.      */
  59.     private ?Uuid $id null;
  60.     /**
  61.      * @ORM\ManyToOne(targetEntity=Context::class)
  62.      */
  63.     private ?Context $context null;
  64.     /**
  65.      * @ORM\ManyToOne(targetEntity=Customer::class)
  66.      * @ORM\JoinColumn(nullable=false)
  67.      */
  68.     private ?Customer $customer null;
  69.     /**
  70.      * @ORM\ManyToOne(targetEntity=User::class)
  71.      */
  72.     private ?User $commercial null;
  73.     /**
  74.      * @ORM\ManyToOne(targetEntity=Customer::class)
  75.      */
  76.     private ?Customer $invoice_address null;
  77.     /**
  78.      * @ORM\ManyToOne(targetEntity=Customer::class)
  79.      */
  80.     private ?Customer $delivery_address null;
  81.     /**
  82.      * @ORM\ManyToOne(targetEntity=Customer::class)
  83.      */
  84.     private ?Customer $invoiceContact null;
  85.     /**
  86.      * @ORM\ManyToOne(targetEntity=Customer::class)
  87.      */
  88.     private ?Customer $deliveryContact null;
  89.     /**
  90.      * @ORM\ManyToOne(targetEntity=OrderState::class, fetch="EXTRA_LAZY")
  91.      */
  92.     private ?OrderState $order_state null;
  93.     /**
  94.      * @ORM\ManyToOne(targetEntity=Currency::class)
  95.      */
  96.     private ?Currency $currency null;
  97.     /**
  98.      * @ORM\Column(type="decimal", precision=20, scale=12)
  99.      */
  100.     private ?string $currency_change_rate null;
  101.     /**
  102.      * @ORM\ManyToOne(targetEntity=PaymentMethod::class)
  103.      */
  104.     private ?PaymentMethod $payment_method null;
  105.     /**
  106.      * @ORM\Column(type="string", length=128, nullable="true")
  107.      */
  108.     private ?string $reference null;
  109.     /**
  110.      * @ORM\Column(type="string", length=255, nullable="true")
  111.      */
  112.     private ?string $internal_name null;
  113.     /**
  114.      * @ORM\Column(type="string", length=255, nullable="true")
  115.      */
  116.     private ?string $external_name null;
  117.     /**
  118.      * @ORM\Column(type="boolean")
  119.      */
  120.     private bool $reduced_vat false;
  121.     /**
  122.      * @ORM\Column(type="decimal", precision=20, scale=6, nullable="true")
  123.      */
  124.     private ?string $total_discount_untaxed null;
  125.     /**
  126.      * @ORM\Column(type="decimal", precision=20, scale=6, nullable="true")
  127.      */
  128.     private ?string $totalDiscount null;
  129.     /**
  130.      * @ORM\Column(type="decimal", precision=20, scale=6, nullable="true")
  131.      */
  132.     private ?string $total_amount_no_discount_untaxed null;
  133.     /**
  134.      * @ORM\Column(type="decimal", precision=20, scale=6, nullable="true")
  135.      */
  136.     private ?string $totalAmountNoDiscount null;
  137.     /**
  138.      * @ORM\Column(type="decimal", precision=20, scale=6, nullable="true")
  139.      */
  140.     private ?string $total_amount_untaxed null;
  141.     /**
  142.      * @ORM\Column(type="decimal", precision=20, scale=6, nullable="true")
  143.      */
  144.     private ?string $total_tax_amount null;
  145.     /**
  146.      * @ORM\Column(type="decimal", precision=20, scale=6, nullable="true")
  147.      */
  148.     private ?string $total_amount null;
  149.     /**
  150.      * @ORM\Column(type="decimal", precision=20, scale=6, nullable="true")
  151.      */
  152.     private ?string $residual null;
  153.     /**
  154.      * @ORM\Column(type="json")
  155.      */
  156.     private array $options = [];
  157.     /**
  158.      * Virtual
  159.      * @var array
  160.      */
  161.     private array $pdfOptions = [];
  162.     /**
  163.      * @ORM\OneToMany(
  164.      *      targetEntity=OrderLine::class,
  165.      *      mappedBy="order",
  166.      *      cascade={"persist", "remove"},
  167.      *      fetch="EXTRA_LAZY"
  168.      * )
  169.      * @ORM\OrderBy({"position" = "ASC"})
  170.      */
  171.     private Collection $lines;
  172.     /**
  173.      * @ORM\OneToMany(targetEntity=OrderHistory::class, mappedBy="order", cascade={"persist"}, fetch="EXTRA_LAZY")
  174.      * @ORM\OrderBy({"id" = "DESC"})
  175.      */
  176.     private Collection $histories;
  177.     /**
  178.      * @ORM\OneToMany(targetEntity=Invoice::class, mappedBy="order", cascade={"persist"}, fetch="EXTRA_LAZY")
  179.      */
  180.     private Collection $invoices;
  181.     /**
  182.      * @ORM\OneToMany(targetEntity=DeliveryNote::class, mappedBy="order", cascade={"persist"}, fetch="EXTRA_LAZY")
  183.      */
  184.     private Collection $delivery_notes;
  185.     /**
  186.      * @ORM\OneToMany(targetEntity=Quotation::class, mappedBy="order", fetch="EXTRA_LAZY")
  187.      */
  188.     private Collection $quotations;
  189.     /**
  190.      * @ORM\ManyToOne(targetEntity=Establishment::class)
  191.      */
  192.     private ?Establishment $establishment null;
  193.     public function __construct()
  194.     {
  195.         $this->lines = new ArrayCollection();
  196.         $this->histories = new ArrayCollection();
  197.         $this->invoices = new ArrayCollection();
  198.         $this->delivery_notes = new ArrayCollection();
  199.         $this->quotations = new ArrayCollection();
  200.     }
  201.     /**
  202.      * @return Uuid|null
  203.      */
  204.     public function getId(): ?Uuid
  205.     {
  206.         return $this->id;
  207.     }
  208.     /**
  209.      * @return Order
  210.      */
  211.     public function setId(): self
  212.     {
  213.         $this->id null;
  214.         return $this;
  215.     }
  216.     /**
  217.      * @return Context|null
  218.      */
  219.     public function getContext(): ?Context
  220.     {
  221.         return $this->context;
  222.     }
  223.     /**
  224.      * @param Context|null $context
  225.      * @return Order
  226.      */
  227.     public function setContext(?Context $context): self
  228.     {
  229.         $this->context $context;
  230.         return $this;
  231.     }
  232.     /**
  233.      * @return Customer|null
  234.      * @throws NonUniqueResultException
  235.      */
  236.     public function getCustomer(): ?Customer
  237.     {
  238.         if ($this->em && $this->customer) {
  239.             SoftdeleteFilter::disable($this->em, [Customer::class]);
  240.             $customer $this->em->getRepository(Customer::class)
  241.                 ->createQueryBuilder('c')
  242.                 ->where('c.id = :customerId')
  243.                 ->setParameter('customerId'$this->customer->getId()->toBinary())
  244.                 ->getQuery()
  245.                 ->getOneOrNullResult();
  246.             SoftdeleteFilter::enable($this->em, [Customer::class]);
  247.             return $customer;
  248.         }
  249.         return $this->customer;
  250.     }
  251.     /**
  252.      * @param Customer $customer
  253.      * @return Order
  254.      */
  255.     public function setCustomer(Customer $customer): self
  256.     {
  257.         if ($this->customer && $this->customer->getId() !== $customer->getId()) {
  258.             $this->setInvoiceContact(null)->setDeliveryContact(null);
  259.         }
  260.         $this->customer $customer;
  261.         $this->setDeliveryAddress(null);
  262.         return $this->setInvoiceAddress($customer);
  263.     }
  264.     /**
  265.      * @return User|null
  266.      * @throws NonUniqueResultException
  267.      */
  268.     public function getCommercial(): ?User
  269.     {
  270.         if ($this->em && $this->commercial) {
  271.             SoftdeleteFilter::disable($this->em, [User::class]);
  272.             $commercial $this->em->getRepository(User::class)
  273.                 ->createQueryBuilder('u')
  274.                 ->where('u.id = :commercialId')
  275.                 ->setParameter('commercialId'$this->commercial->getId()->toBinary())
  276.                 ->getQuery()
  277.                 ->getOneOrNullResult();
  278.             SoftdeleteFilter::enable($this->em, [User::class]);
  279.             return $commercial;
  280.         }
  281.         return $this->commercial;
  282.     }
  283.     /**
  284.      * @param User|null $commercial
  285.      * @return $this
  286.      */
  287.     public function setCommercial(?User $commercial): self
  288.     {
  289.         $this->commercial $commercial;
  290.         return $this;
  291.     }
  292.     /**
  293.      * @return Customer|null
  294.      * @throws NonUniqueResultException
  295.      */
  296.     public function getInvoiceAddress(): ?Customer
  297.     {
  298.         if ($this->em && $this->invoice_address) {
  299.             SoftdeleteFilter::disable($this->em, [Customer::class]);
  300.             $invoiceAddress $this->em->getRepository(Customer::class)
  301.                 ->createQueryBuilder('c')
  302.                 ->where('c.id = :invoiceAddressId')
  303.                 ->setParameter('invoiceAddressId'$this->invoice_address->getId()->toBinary())
  304.                 ->getQuery()
  305.                 ->getOneOrNullResult();
  306.             SoftdeleteFilter::enable($this->em, [Customer::class]);
  307.             return $invoiceAddress;
  308.         }
  309.         return $this->invoice_address;
  310.     }
  311.     /**
  312.      * @param Customer|null $invoice_address
  313.      * @return Order
  314.      */
  315.     public function setInvoiceAddress(?Customer $invoice_address): self
  316.     {
  317.         $this->invoice_address $invoice_address;
  318.         return $this;
  319.     }
  320.     /**
  321.      * @return Customer|null
  322.      * @throws NonUniqueResultException
  323.      */
  324.     public function getDeliveryAddress(): ?Customer
  325.     {
  326.         if ($this->em && $this->delivery_address) {
  327.             SoftdeleteFilter::disable($this->em, [Customer::class]);
  328.             $deliveryAddress $this->em->getRepository(Customer::class)
  329.                 ->createQueryBuilder('c')
  330.                 ->where('c.id = :deliveryAddressId')
  331.                 ->setParameter('deliveryAddressId'$this->delivery_address->getId()->toBinary())
  332.                 ->getQuery()
  333.                 ->getOneOrNullResult();
  334.             SoftdeleteFilter::enable($this->em, [Customer::class]);
  335.             return $deliveryAddress;
  336.         }
  337.         return $this->delivery_address;
  338.     }
  339.     /**
  340.      * @param Customer|null $delivery_address
  341.      * @return Order
  342.      */
  343.     public function setDeliveryAddress(?Customer $delivery_address): self
  344.     {
  345.         $this->delivery_address $delivery_address;
  346.         return $this;
  347.     }
  348.     /**
  349.      * @return Customer|null
  350.      * @throws NonUniqueResultException
  351.      */
  352.     public function getInvoiceContact(): ?Customer
  353.     {
  354.         if ($this->em && $this->invoiceContact) {
  355.             SoftdeleteFilter::disable($this->em, [Customer::class]);
  356.             $invoiceContact $this->em->getRepository(Customer::class)
  357.                 ->createQueryBuilder('c')
  358.                 ->where('c.id = :invoiceContactId')
  359.                 ->setParameter('invoiceContactId'$this->invoiceContact->getId()->toBinary())
  360.                 ->getQuery()
  361.                 ->getOneOrNullResult();
  362.             SoftdeleteFilter::enable($this->em, [Customer::class]);
  363.             return $invoiceContact;
  364.         }
  365.         return $this->invoiceContact;
  366.     }
  367.     /**
  368.      * @param Customer|null $invoiceContact
  369.      * @return Order
  370.      */
  371.     public function setInvoiceContact(?Customer $invoiceContact): Order
  372.     {
  373.         $this->invoiceContact $invoiceContact;
  374.         return $this;
  375.     }
  376.     /**
  377.      * @return Customer|null
  378.      * @throws NonUniqueResultException
  379.      */
  380.     public function getDeliveryContact(): ?Customer
  381.     {
  382.         if ($this->em && $this->deliveryContact) {
  383.             SoftdeleteFilter::disable($this->em, [Customer::class]);
  384.             $deliveryContact $this->em->getRepository(Customer::class)
  385.                 ->createQueryBuilder('c')
  386.                 ->where('c.id = :deliveryContactId')
  387.                 ->setParameter('deliveryContactId'$this->deliveryContact->getId()->toBinary())
  388.                 ->getQuery()
  389.                 ->getOneOrNullResult();
  390.             SoftdeleteFilter::enable($this->em, [Customer::class]);
  391.             return $deliveryContact;
  392.         }
  393.         return $this->deliveryContact;
  394.     }
  395.     /**
  396.      * @param Customer|null $deliveryContact
  397.      * @return Order
  398.      */
  399.     public function setDeliveryContact(?Customer $deliveryContact): Order
  400.     {
  401.         $this->deliveryContact $deliveryContact;
  402.         return $this;
  403.     }
  404.     /**
  405.      * @return OrderState|null
  406.      */
  407.     public function getOrderState(): ?OrderState
  408.     {
  409.         return $this->order_state;
  410.     }
  411.     /**
  412.      * @param OrderState|null $order_state
  413.      * @return Order
  414.      */
  415.     public function setOrderState(?OrderState $order_state): self
  416.     {
  417.         $this->order_state $order_state;
  418.         return $this;
  419.     }
  420.     /**
  421.      * @return Currency|null
  422.      */
  423.     public function getCurrency(): ?Currency
  424.     {
  425.         return $this->currency;
  426.     }
  427.     /**
  428.      * @param Currency $currency
  429.      * @return Order
  430.      */
  431.     public function setCurrency(Currency $currency): self
  432.     {
  433.         $this->currency $currency;
  434.         return $this->setCurrencyChangeRate($currency->getChangeRate());
  435.     }
  436.     /**
  437.      * @return string|null
  438.      */
  439.     public function getCurrencyChangeRate(): ?string
  440.     {
  441.         return $this->currency_change_rate;
  442.     }
  443.     /**
  444.      * @param string|null $currency_change_rate
  445.      * @return Order
  446.      */
  447.     public function setCurrencyChangeRate(?string $currency_change_rate): self
  448.     {
  449.         $this->currency_change_rate $currency_change_rate;
  450.         return $this;
  451.     }
  452.     /**
  453.      * @return PaymentMethod|null
  454.      * @throws NonUniqueResultException
  455.      */
  456.     public function getPaymentMethod(): ?PaymentMethod
  457.     {
  458.         if ($this->em && $this->payment_method) {
  459.             SoftdeleteFilter::disable($this->em, [PaymentMethod::class]);
  460.             $payment_method $this->em->getRepository(PaymentMethod::class)
  461.                 ->createQueryBuilder('pm')
  462.                 ->where('pm.id = :paymentMethodId')
  463.                 ->setParameter('paymentMethodId'$this->payment_method->getId()->toBinary())
  464.                 ->getQuery()
  465.                 ->getOneOrNullResult();
  466.             SoftdeleteFilter::enable($this->em, [PaymentMethod::class]);
  467.             return $payment_method;
  468.         }
  469.         return $this->payment_method;
  470.     }
  471.     /**
  472.      * @param PaymentMethod|null $payment_method
  473.      * @return Order
  474.      */
  475.     public function setPaymentMethod(?PaymentMethod $payment_method): Order
  476.     {
  477.         $this->payment_method $payment_method;
  478.         return $this;
  479.     }
  480.     /**
  481.      * @return string|null
  482.      */
  483.     public function getReference(): ?string
  484.     {
  485.         return $this->reference;
  486.     }
  487.     /**
  488.      * @param string|null $reference
  489.      * @return Order
  490.      */
  491.     public function setReference(?string $reference): self
  492.     {
  493.         $this->reference $reference;
  494.         return $this;
  495.     }
  496.     /**
  497.      * @return string|null
  498.      */
  499.     public function getInternalName(): ?string
  500.     {
  501.         return $this->internal_name;
  502.     }
  503.     /**
  504.      * @param string|null $internal_name
  505.      * @return Order
  506.      */
  507.     public function setInternalName(?string $internal_name): self
  508.     {
  509.         $this->internal_name $internal_name;
  510.         return $this;
  511.     }
  512.     /**
  513.      * @return string|null
  514.      */
  515.     public function getExternalName(): ?string
  516.     {
  517.         return $this->external_name;
  518.     }
  519.     /**
  520.      * @param string|null $external_name
  521.      * @return Order
  522.      */
  523.     public function setExternalName(?string $external_name): self
  524.     {
  525.         $this->external_name $external_name;
  526.         return $this;
  527.     }
  528.     /**
  529.      * @return bool
  530.      */
  531.     public function isReducedVat(): bool
  532.     {
  533.         return $this->reduced_vat;
  534.     }
  535.     /**
  536.      * @param bool $reduced_vat
  537.      * @return Order
  538.      */
  539.     public function setReducedVat(bool $reduced_vat): self
  540.     {
  541.         $this->reduced_vat $reduced_vat;
  542.         return $this;
  543.     }
  544.     /**
  545.      * @return string|null
  546.      */
  547.     public function getTotalDiscountUntaxed(): ?string
  548.     {
  549.         return $this->total_discount_untaxed;
  550.     }
  551.     /**
  552.      * @param string|null $total_discount_untaxed
  553.      * @return Order
  554.      */
  555.     public function setTotalDiscountUntaxed(?string $total_discount_untaxed): self
  556.     {
  557.         $this->total_discount_untaxed $total_discount_untaxed;
  558.         return $this;
  559.     }
  560.     /**
  561.      * @return string|null
  562.      */
  563.     public function getTotalDiscount(): ?string
  564.     {
  565.         return $this->totalDiscount;
  566.     }
  567.     /**
  568.      * @param string|null $totalDiscount
  569.      * @return Order
  570.      */
  571.     public function setTotalDiscount(?string $totalDiscount): Order
  572.     {
  573.         $this->totalDiscount $totalDiscount;
  574.         return $this;
  575.     }
  576.     /**
  577.      * @return string|null
  578.      */
  579.     public function getTotalAmountNoDiscountUntaxed(): ?string
  580.     {
  581.         return $this->total_amount_no_discount_untaxed;
  582.     }
  583.     /**
  584.      * @param string|null $total_amount_no_discount_untaxed
  585.      * @return Order
  586.      */
  587.     public function setTotalAmountNoDiscountUntaxed(?string $total_amount_no_discount_untaxed): self
  588.     {
  589.         $this->total_amount_no_discount_untaxed $total_amount_no_discount_untaxed;
  590.         return $this;
  591.     }
  592.     /**
  593.      * @return string|null
  594.      */
  595.     public function getTotalAmountNoDiscount(): ?string
  596.     {
  597.         return $this->totalAmountNoDiscount;
  598.     }
  599.     /**
  600.      * @param string|null $totalAmountNoDiscount
  601.      * @return Order
  602.      */
  603.     public function setTotalAmountNoDiscount(?string $totalAmountNoDiscount): Order
  604.     {
  605.         $this->totalAmountNoDiscount $totalAmountNoDiscount;
  606.         return $this;
  607.     }
  608.     /**
  609.      * @return string|null
  610.      */
  611.     public function getTotalAmountUntaxed(): ?string
  612.     {
  613.         return $this->total_amount_untaxed;
  614.     }
  615.     /**
  616.      * @param string|null $total_amount_untaxed
  617.      * @return Order
  618.      */
  619.     public function setTotalAmountUntaxed(?string $total_amount_untaxed): self
  620.     {
  621.         $this->total_amount_untaxed $total_amount_untaxed;
  622.         return $this;
  623.     }
  624.     /**
  625.      * @return string|null
  626.      */
  627.     public function getTotalTaxAmount(): ?string
  628.     {
  629.         return $this->total_tax_amount;
  630.     }
  631.     /**
  632.      * @param string|null $total_tax_amount
  633.      * @return Order
  634.      */
  635.     public function setTotalTaxAmount(?string $total_tax_amount): self
  636.     {
  637.         $this->total_tax_amount $total_tax_amount;
  638.         return $this;
  639.     }
  640.     /**
  641.      * @return string|null
  642.      */
  643.     public function getTotalAmount(): ?string
  644.     {
  645.         return $this->total_amount;
  646.     }
  647.     /**
  648.      * @param string|null $total_amount
  649.      * @return Order
  650.      */
  651.     public function setTotalAmount(?string $total_amount): self
  652.     {
  653.         $this->total_amount $total_amount;
  654.         return $this;
  655.     }
  656.     /**
  657.      * @return float|null
  658.      */
  659.     public function getResidual(): ?float
  660.     {
  661.         return (float) $this->residual;
  662.     }
  663.     /**
  664.      * @param string|null $residual
  665.      * @return $this
  666.      */
  667.     public function setResidual(?string $residual): self
  668.     {
  669.         $this->residual $residual $this->total_amount $this->total_amount $residual;
  670.         return $this;
  671.     }
  672.     /**
  673.      * @return array
  674.      */
  675.     public function getOptions(): array
  676.     {
  677.         return $this->options;
  678.     }
  679.     /**
  680.      * @param array $options
  681.      * @return Order
  682.      */
  683.     public function setOptions(array $options): self
  684.     {
  685.         $this->options $options;
  686.         return $this;
  687.     }
  688.     /**
  689.      * @param array $options
  690.      * @return Order
  691.      */
  692.     public function addOptions(array $options): self
  693.     {
  694.         return $this->setOptions(array_merge($this->options$options));
  695.     }
  696.     /**
  697.      * @return array
  698.      */
  699.     public function getPdfOptions(): array
  700.     {
  701.         return $this->pdfOptions;
  702.     }
  703.     /**
  704.      * @param array $pdfOptions
  705.      * @return Order
  706.      */
  707.     public function setPdfOptions(array $pdfOptions): Order
  708.     {
  709.         $this->pdfOptions $pdfOptions;
  710.         return $this;
  711.     }
  712.     /**
  713.      * @return Collection|OrderLine[]
  714.      */
  715.     public function getLines(): Collection
  716.     {
  717.         return $this->getFilterLines();
  718.     }
  719.     /**
  720.      * @param OrderLine $line
  721.      * @return $this
  722.      */
  723.     public function addLine(OrderLine $line): self
  724.     {
  725.         if (!$this->lines->contains($line)) {
  726.             $this->lines[] = $line;
  727.             if ($line->getOrder() !== $this) {
  728.                 $line->setOrder($this);
  729.             }
  730.         }
  731.         return $this;
  732.     }
  733.     /**
  734.      * @param OrderLine $line
  735.      * @return $this
  736.      */
  737.     public function removeLine(OrderLine $line): self
  738.     {
  739.         $this->lines->removeElement($line);
  740.         return $this;
  741.     }
  742.     /**
  743.      * @param bool $withGroups
  744.      * @param bool $all
  745.      * @param bool $withPacks
  746.      * @return Collection|OrderLine[]
  747.      */
  748.     public function getFilterLines(bool $withGroups truebool $all falsebool $withPacks true): Collection
  749.     {
  750.         if ($all) {
  751.             $lines $this->lines;
  752.             $goodLines = [];
  753.             foreach ($lines as $line) {
  754.                 if (!$line->getParent() && !$line->getPackParent()) {
  755.                     $goodLines[] = $line;
  756.                     foreach ($line->getChildrens() as $child) {
  757.                         $goodLines[] = $child;
  758.                         foreach ($child->getPackChildrens() as $grandChild) {
  759.                             $goodLines[] = $grandChild;
  760.                         }
  761.                     }
  762.                     foreach ($line->getPackChildrens() as $child) {
  763.                         $goodLines[] = $child;
  764.                     }
  765.                 }
  766.             }
  767.             return new ArrayCollection($goodLines);
  768.         }
  769.         $criteria Criteria::create();
  770.         if ($withGroups) {
  771.             $criteria
  772.                 ->where(Criteria::expr()->eq('parent'null))
  773.                 ->andWhere(Criteria::expr()->eq('packParent'null));
  774.         } else {
  775.             if ($withPacks) {
  776.                 $criteria->andWhere(Criteria::expr()->eq('is_pack'true));
  777.             } else {
  778.                 $criteria->andWhere(Criteria::expr()->eq('is_pack'false));
  779.             }
  780.             $criteria->where(Criteria::expr()->eq('is_group'false));
  781.         }
  782.         return $this->lines->matching($criteria);
  783.     }
  784.     /**
  785.      * @return Collection|OrderHistory[]
  786.      */
  787.     public function getHistories(): Collection
  788.     {
  789.         return $this->histories;
  790.     }
  791.     /**
  792.     * @param Invoice $invoice
  793.     * @return $this
  794.     */
  795.     public function addInvoice(Invoice $invoice): self
  796.     {
  797.         if (!$this->invoices->contains($invoice)) {
  798.             $this->invoices[] = $invoice;
  799.             if ($invoice->getOrder() !== $this) {
  800.                 $invoice->setOrder($this);
  801.             }
  802.         }
  803.         return $this;
  804.     }
  805.     /**
  806.      * @return Collection|Invoice
  807.      */
  808.     public function getInvoices(): Collection
  809.     {
  810.         $invoices $this->invoices;
  811.         foreach ($this->getFilterDeliveryNotes(falsefalsefalsetrue) as $deliveryNoteValidated) {
  812.             $deliveryNoteInvoice $deliveryNoteValidated->getInvoice();
  813.             if ($deliveryNoteInvoice && !$invoices->contains($deliveryNoteInvoice)) {
  814.                 $invoices->add($deliveryNoteInvoice);
  815.             }
  816.         }
  817.         return $invoices;
  818.     }
  819.     /**
  820.      * @return Invoice|null
  821.      */
  822.     public function getMainInvoice(): ?Invoice
  823.     {
  824.         $collection $this->invoices->filter(function (Invoice $invoice) {
  825.             return !$invoice->getIsDeposit() && $invoice->getDeliveryNotes()->isEmpty() && !$invoice->isCanceled();
  826.         });
  827.         return $collection->count() ? $collection->first() : null;
  828.     }
  829.     /**
  830.      * @return Collection|Invoice[]
  831.      */
  832.     public function getDepositInvoices(): Collection
  833.     {
  834.         return $this->invoices->filter(function (Invoice $invoice) {
  835.             return $invoice->getIsDeposit() && !$invoice->isCanceled();
  836.         });
  837.     }
  838.     /**
  839.      * @return Collection|Invoice[]
  840.      */
  841.     public function getDeliveryNoteInvoices(): Collection
  842.     {
  843.         return $this->invoices->filter(function (Invoice $invoice) {
  844.             return !$invoice->getDeliveryNotes()->isEmpty() && !$invoice->isCanceled();
  845.         });
  846.     }
  847.     /**
  848.      * @return Collection|DeliveryNote[]
  849.      */
  850.     public function getDeliveryNotes(): Collection
  851.     {
  852.         return $this->getFilterDeliveryNotes();
  853.     }
  854.     /**
  855.      * @param bool $all
  856.      * @param bool $draft
  857.      * @param bool $forInvoice
  858.      * @param bool $validated
  859.      * @return Collection|DeliveryNote
  860.      */
  861.     public function getFilterDeliveryNotes(
  862.         bool $all true,
  863.         bool $draft true,
  864.         bool $forInvoice false,
  865.         bool $validated false
  866.     ): Collection {
  867.         if ($all) {
  868.             return $this->delivery_notes;
  869.         }
  870.         $criteria Criteria::create();
  871.         if ($draft) {
  872.             $criteria->where(Criteria::expr()->isNull('validatedAt'));
  873.         } elseif ($forInvoice) {
  874.             $criteria->where(Criteria::expr()->neq('validatedAt'null));
  875.             $criteria->andWhere(Criteria::expr()->isNull('invoice'));
  876.             $criteria->andWhere(Criteria::expr()->isNull('canceledAt'));
  877.         } elseif ($validated) {
  878.             $criteria->where(Criteria::expr()->neq('validatedAt'null));
  879.             $criteria->andWhere(Criteria::expr()->isNull('canceledAt'));
  880.         }
  881.         return $this->delivery_notes->matching($criteria);
  882.     }
  883.     /**
  884.      * @param bool $list
  885.      * @return Collection
  886.      */
  887.     public function getRemainingLines(bool $list true): Collection
  888.     {
  889.         $allLineTypes $this->configService->get('sales_bundle__generation_of_delivery_note_with_all_line_types');
  890.         $withPack $this->configService->get('sales_bundle__generation_of_delivery_note_with_pack');
  891.         $linesWithQtyZero $this->configService->get('sales_bundle__order_remaining_lines_with_quantity_0');
  892.         $lines $this->lines->filter(function (OrderLine $line) use ($withPack$allLineTypes$linesWithQtyZero) {
  893.             return ($line->getRemainingQuantity() > || ($linesWithQtyZero && $line->getQuantity() == 0))
  894.                 && $line->getLineType()
  895.                 && (!$line->getIsPack() || $withPack)
  896.                 && ($allLineTypes || $line->getLineType()->getNumber() == 1);
  897.         });
  898.         if (!$list) {
  899.             return $lines;
  900.         }
  901.         return $lines->map(function (OrderLine $orderLine) {
  902.             return [
  903.                 'id' => (string) $orderLine->getId(),
  904.                 'quantity' => $orderLine->getRemainingQuantity()
  905.             ];
  906.         });
  907.     }
  908.     /**
  909.      * @return bool
  910.      */
  911.     public function isInvoicable(): bool
  912.     {
  913.         return $this->getValidatedAt()
  914.             && $this->getDeliveryNoteInvoices()->isEmpty()
  915.             && !$this->getMainInvoice();
  916.     }
  917.     /**
  918.      * @return bool
  919.      */
  920.     public function isDeliveryNoteInvoicable(): bool
  921.     {
  922.         return $this->getValidatedAt()
  923.             && $this->getResidual() > 0
  924.             && $this->getDepositInvoices()->isEmpty()
  925.             && !$this->getMainInvoice();
  926.     }
  927.     /**
  928.      * @return bool
  929.      */
  930.     public function isDeliverable(): bool
  931.     {
  932.         return $this->getValidatedAt() && !$this->getRemainingLines()->isEmpty();
  933.     }
  934.     /**
  935.      * 0 => Pas prête
  936.      * 1 => Partiellement prête
  937.      * 2 => Prête
  938.      * @return int|null
  939.      */
  940.     public function burstableStatus(): ?int
  941.     {
  942.         if (
  943.             $this->getOptions()
  944.             && isset($this->getOptions()['shipments'])
  945.             && isset($this->getOptions()['shipments']['status'])
  946.         ) {
  947.             return $this->getOptions()['shipments']['status'];
  948.         } else {
  949.             return 0;
  950.         }
  951.     }
  952.     /**
  953.      * @return bool
  954.      */
  955.     public function orderRemovedFromBurst(): bool
  956.     {
  957.         if (
  958.             $this->getOptions()
  959.             && isset($this->getOptions()['shipments'])
  960.             && isset($this->getOptions()['shipments']['orderRemovedFromBurst'])
  961.         ) {
  962.             return $this->getOptions()['shipments']['orderRemovedFromBurst'];
  963.         } else {
  964.             return false;
  965.         }
  966.     }
  967.     /**
  968.      * @return float
  969.      */
  970.     public function calcResidual(): float
  971.     {
  972.         $residual = (float) $this->getTotalAmount();
  973.         foreach ($this->getInvoices() as $invoice) {
  974.             if (!$invoice->isCanceled()) {
  975.                 $residual -= $invoice->getTotalAmount();
  976.             }
  977.         }
  978.         return $residual;
  979.     }
  980.     /**
  981.      * @return float
  982.      */
  983.     public function getAmountInvoicedUntaxed(): float
  984.     {
  985.         $amount 0;
  986.         foreach ($this->getInvoices() as $invoice) {
  987.             if (!$invoice->isCanceled()) {
  988.                 $amount += $invoice->getTotalAmountUntaxed();
  989.             }
  990.         }
  991.         return $amount;
  992.     }
  993.     /**
  994.      * @return Order
  995.      */
  996.     public function duplicate(): Order
  997.     {
  998.         if ($this->id) {
  999.             $clone = clone $this;
  1000.             $clone->setId();
  1001.             $clone->setCreatedAt(new DateTime());
  1002.             $clone->setCreatedBy(null);
  1003.             $clone->setUpdatedAt(new DateTime());
  1004.             $clone->setUpdatedBy(null);
  1005.             $clone->setValidatedAt(null);
  1006.             $clone->setValidatedBy(null);
  1007.             $clone->setContext(null);
  1008.             $clone->setEstablishment(null);
  1009.             $clone->setOrderState(null);
  1010.             $clone->setReference(null);
  1011.             $clone->setResidual(null);
  1012.             $clone->addOptions([
  1013.                 'invoice_address' => null,
  1014.                 'delivery_address' => null
  1015.             ]);
  1016.             $clone->lines = new ArrayCollection();
  1017.             foreach ($this->getFilterLines() as $line) {
  1018.                 $clone_line $line->duplicate($clone);
  1019.                 $clone->addLine($clone_line);
  1020.             }
  1021.             return $clone;
  1022.         }
  1023.         return $this;
  1024.     }
  1025.     /**
  1026.      * @return Customer|null
  1027.      */
  1028.     public function getFinalDeliveryAddress(): ?Customer
  1029.     {
  1030.         return $this->getDeliveryAddress() ?: $this->getInvoiceAddress();
  1031.     }
  1032.     /**
  1033.      * @return float
  1034.      */
  1035.     public function getPercentage(): float
  1036.     {
  1037.         $residualPercentage $this->getResidual() / (float) $this->getTotalAmount() * 100;
  1038.         return round(100 $residualPercentage2);
  1039.     }
  1040.     /**
  1041.      * @return Collection
  1042.      */
  1043.     public function getQuotations(): Collection
  1044.     {
  1045.         return $this->quotations;
  1046.     }
  1047.     /**
  1048.      * @param Quotation $quotation
  1049.      * @return $this
  1050.      */
  1051.     public function addQuotation(Quotation $quotation): self
  1052.     {
  1053.         if (!$this->quotations->contains($quotation)) {
  1054.             if ($this->getQuotations()->isEmpty()) {
  1055.                 $this->setCommercial($quotation->getCommercial());
  1056.                 $this->setCustomer($quotation->getCustomer());
  1057.                 $this->setInvoiceAddress($quotation->getInvoiceAddress());
  1058.                 $this->setDeliveryAddress($quotation->getDeliveryAddress());
  1059.                 $this->setInvoiceContact($quotation->getInvoiceContact());
  1060.                 $this->setDeliveryContact($quotation->getDeliveryContact());
  1061.                 $this->setCurrency($quotation->getCurrency());
  1062.                 $this->setContext($quotation->getContext());
  1063.                 $this->setEstablishment($quotation->getEstablishment());
  1064.                 $this->setExternalName($quotation->getExternalName());
  1065.                 $this->setTotalAmountNoDiscountUntaxed($quotation->getTotalAmountNoDiscountUntaxed());
  1066.                 $this->setTotalAmountNoDiscount($quotation->getTotalAmountNoDiscount());
  1067.                 $this->setTotalDiscount($quotation->getTotalDiscount());
  1068.                 $this->setTotalTaxAmount($quotation->getTotalTaxAmount());
  1069.                 $this->setTotalAmountUntaxed($quotation->getTotalAmountUntaxed());
  1070.                 $this->setTotalAmount($quotation->getTotalAmount());
  1071.                 if (!empty($quotation->getOptions()['note'])) {
  1072.                     $this->addOptions([
  1073.                         'note' => $quotation->getOptions()['note']
  1074.                     ]);
  1075.                 }
  1076.                 if (!empty($quotation->getOptions()['customerNote'])) {
  1077.                     $this->addOptions([
  1078.                         'customerNote' => $quotation->getOptions()['customerNote']
  1079.                     ]);
  1080.                 }
  1081.             }
  1082.             $this->quotations[] = $quotation;
  1083.         }
  1084.         return $this;
  1085.     }
  1086.     /**
  1087.      * @return Quotation|null
  1088.      */
  1089.     public function getQuotation(): ?Quotation
  1090.     {
  1091.         if ($this->quotations->isEmpty()) {
  1092.             return null;
  1093.         } else {
  1094.             return $this->quotations->first();
  1095.         }
  1096.     }
  1097.     /**
  1098.      * @return Establishment|null
  1099.      */
  1100.     public function getEstablishment(): ?Establishment
  1101.     {
  1102.         return $this->establishment;
  1103.     }
  1104.     /**
  1105.      * @param Establishment $establishment
  1106.      * @return $this
  1107.      */
  1108.     public function setEstablishment(?Establishment $establishment): self
  1109.     {
  1110.         $this->establishment $establishment;
  1111.         return $this;
  1112.     }
  1113. }