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

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