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.             $lines $this->lines;
  664.             $goodLines = [];
  665.             foreach ($lines as $line) {
  666.                 if (!$line->getParent() && !$line->getPackParent()) {
  667.                     $goodLines[] = $line;
  668.                     foreach ($line->getChildrens() as $child) {
  669.                         $goodLines[] = $child;
  670.                         foreach ($child->getPackChildrens() as $grandChild) {
  671.                             $goodLines[] = $grandChild;
  672.                         }
  673.                     }
  674.                     foreach ($line->getPackChildrens() as $child) {
  675.                         $goodLines[] = $child;
  676.                     }
  677.                 }
  678.             }
  679.             return new ArrayCollection($goodLines);
  680.         }
  681.         $criteria Criteria::create();
  682.         if ($withGroups) {
  683.             $criteria
  684.                 ->where(Criteria::expr()->eq('parent'null))
  685.                 ->andWhere(Criteria::expr()->eq('packParent'null));
  686.         } else {
  687.             if ($withPacks) {
  688.                 $criteria->andWhere(Criteria::expr()->eq('is_pack'true));
  689.             } else {
  690.                 $criteria->andWhere(Criteria::expr()->eq('is_pack'false));
  691.             }
  692.             $criteria->where(Criteria::expr()->eq('is_group'false));
  693.         }
  694.         return $this->lines->matching($criteria);
  695.     }
  696.     /**
  697.      * @return Collection|OrderHistory[]
  698.      */
  699.     public function getHistories(): Collection
  700.     {
  701.         return $this->histories;
  702.     }
  703.     /**
  704.     * @param Invoice $invoice
  705.     * @return $this
  706.     */
  707.     public function addInvoice(Invoice $invoice): self
  708.     {
  709.         if (!$this->invoices->contains($invoice)) {
  710.             $this->invoices[] = $invoice;
  711.             if ($invoice->getOrder() !== $this) {
  712.                 $invoice->setOrder($this);
  713.             }
  714.         }
  715.         return $this;
  716.     }
  717.     /**
  718.      * @return Collection|Invoice
  719.      */
  720.     public function getInvoices(): Collection
  721.     {
  722.         $invoices $this->invoices;
  723.         foreach ($this->getFilterDeliveryNotes(falsefalsefalsetrue) as $deliveryNoteValidated) {
  724.             $deliveryNoteInvoice $deliveryNoteValidated->getInvoice();
  725.             if ($deliveryNoteInvoice && !$invoices->contains($deliveryNoteInvoice)) {
  726.                 $invoices->add($deliveryNoteInvoice);
  727.             }
  728.         }
  729.         return $invoices;
  730.     }
  731.     /**
  732.      * @return Invoice|null
  733.      */
  734.     public function getMainInvoice(): ?Invoice
  735.     {
  736.         $collection $this->invoices->filter(function (Invoice $invoice) {
  737.             return !$invoice->getIsDeposit() && $invoice->getDeliveryNotes()->isEmpty() && !$invoice->isCanceled();
  738.         });
  739.         return $collection->count() ? $collection->first() : null;
  740.     }
  741.     /**
  742.      * @return Collection|Invoice[]
  743.      */
  744.     public function getDepositInvoices(): Collection
  745.     {
  746.         return $this->invoices->filter(function (Invoice $invoice) {
  747.             return $invoice->getIsDeposit() && !$invoice->isCanceled();
  748.         });
  749.     }
  750.     /**
  751.      * @return Collection|Invoice[]
  752.      */
  753.     public function getDeliveryNoteInvoices(): Collection
  754.     {
  755.         return $this->invoices->filter(function (Invoice $invoice) {
  756.             return !$invoice->getDeliveryNotes()->isEmpty() && !$invoice->isCanceled();
  757.         });
  758.     }
  759.     /**
  760.      * @return Collection|DeliveryNote[]
  761.      */
  762.     public function getDeliveryNotes(): Collection
  763.     {
  764.         return $this->getFilterDeliveryNotes();
  765.     }
  766.     /**
  767.      * @param bool $all
  768.      * @param bool $draft
  769.      * @param bool $forInvoice
  770.      * @param bool $validated
  771.      * @return Collection|DeliveryNote
  772.      */
  773.     public function getFilterDeliveryNotes(
  774.         bool $all true,
  775.         bool $draft true,
  776.         bool $forInvoice false,
  777.         bool $validated false
  778.     ): Collection {
  779.         if ($all) {
  780.             return $this->delivery_notes;
  781.         }
  782.         $criteria Criteria::create();
  783.         if ($draft) {
  784.             $criteria->where(Criteria::expr()->isNull('validatedAt'));
  785.         } elseif ($forInvoice) {
  786.             $criteria->where(Criteria::expr()->neq('validatedAt'null));
  787.             $criteria->andWhere(Criteria::expr()->isNull('invoice'));
  788.             $criteria->andWhere(Criteria::expr()->isNull('canceledAt'));
  789.         } elseif ($validated) {
  790.             $criteria->where(Criteria::expr()->neq('validatedAt'null));
  791.             $criteria->andWhere(Criteria::expr()->isNull('canceledAt'));
  792.         }
  793.         return $this->delivery_notes->matching($criteria);
  794.     }
  795.     /**
  796.      * @param bool $list
  797.      * @return Collection
  798.      */
  799.     public function getRemainingLines(bool $list true): Collection
  800.     {
  801.         $allLineTypes $this->configService->get('sales_bundle__generation_of_delivery_note_with_all_line_types');
  802.         $withPack $this->configService->get('sales_bundle__generation_of_delivery_note_with_pack');
  803.         $linesWithQtyZero $this->configService->get('sales_bundle__order_remaining_lines_with_quantity_0');
  804.         $lines $this->lines->filter(function (OrderLine $line) use ($withPack$allLineTypes$linesWithQtyZero) {
  805.             return ($line->getRemainingQuantity() > || ($linesWithQtyZero && $line->getQuantity() == 0))
  806.                 && $line->getLineType()
  807.                 && (!$line->getIsPack() || $withPack)
  808.                 && ($allLineTypes || $line->getLineType()->getNumber() == 1);
  809.         });
  810.         if (!$list) {
  811.             return $lines;
  812.         }
  813.         return $lines->map(function (OrderLine $orderLine) {
  814.             return [
  815.                 'id' => (string) $orderLine->getId(),
  816.                 'quantity' => $orderLine->getRemainingQuantity()
  817.             ];
  818.         });
  819.     }
  820.     /**
  821.      * @return bool
  822.      */
  823.     public function isInvoicable(): bool
  824.     {
  825.         return $this->getValidatedAt()
  826.             && $this->getDeliveryNoteInvoices()->isEmpty()
  827.             && !$this->getMainInvoice();
  828.     }
  829.     /**
  830.      * @return bool
  831.      */
  832.     public function isDeliveryNoteInvoicable(): bool
  833.     {
  834.         return $this->getValidatedAt()
  835.             && $this->getResidual() > 0
  836.             && $this->getDepositInvoices()->isEmpty()
  837.             && !$this->getMainInvoice();
  838.     }
  839.     /**
  840.      * @return bool
  841.      */
  842.     public function isDeliverable(): bool
  843.     {
  844.         return $this->getValidatedAt() && !$this->getRemainingLines()->isEmpty();
  845.     }
  846.     /**
  847.      * 0 => Pas prête
  848.      * 1 => Partiellement prête
  849.      * 2 => Prête
  850.      * @return int|null
  851.      */
  852.     public function burstableStatus(): ?int
  853.     {
  854.         if (
  855.             $this->getOptions()
  856.             && isset($this->getOptions()['shipments'])
  857.             && isset($this->getOptions()['shipments']['status'])
  858.         ) {
  859.             return $this->getOptions()['shipments']['status'];
  860.         } else {
  861.             return 0;
  862.         }
  863.     }
  864.     /**
  865.      * @return bool
  866.      */
  867.     public function orderRemovedFromBurst(): bool
  868.     {
  869.         if (
  870.             $this->getOptions()
  871.             && isset($this->getOptions()['shipments'])
  872.             && isset($this->getOptions()['shipments']['orderRemovedFromBurst'])
  873.         ) {
  874.             return $this->getOptions()['shipments']['orderRemovedFromBurst'];
  875.         } else {
  876.             return false;
  877.         }
  878.     }
  879.     /**
  880.      * @return float
  881.      */
  882.     public function calcResidual(): float
  883.     {
  884.         $residual = (float) $this->getTotalAmount();
  885.         foreach ($this->getInvoices() as $invoice) {
  886.             if (!$invoice->isCanceled()) {
  887.                 $residual -= $invoice->getTotalAmount();
  888.             }
  889.         }
  890.         return $residual;
  891.     }
  892.     /**
  893.      * @return float
  894.      */
  895.     public function getAmountInvoicedUntaxed(): float
  896.     {
  897.         $amount 0;
  898.         foreach ($this->getInvoices() as $invoice) {
  899.             if (!$invoice->isCanceled()) {
  900.                 $amount += $invoice->getTotalAmountUntaxed();
  901.             }
  902.         }
  903.         return $amount;
  904.     }
  905.     /**
  906.      * @return Order
  907.      */
  908.     public function duplicate(): Order
  909.     {
  910.         if ($this->id) {
  911.             $clone = clone $this;
  912.             $clone->setId();
  913.             $clone->setCreatedAt(new DateTime());
  914.             $clone->setCreatedBy(null);
  915.             $clone->setUpdatedAt(new DateTime());
  916.             $clone->setUpdatedBy(null);
  917.             $clone->setValidatedAt(null);
  918.             $clone->setValidatedBy(null);
  919.             $clone->setContext(null);
  920.             $clone->setEstablishment(null);
  921.             $clone->setOrderState(null);
  922.             $clone->setReference(null);
  923.             $clone->setResidual(null);
  924.             $clone->addOptions([
  925.                 'invoice_address' => null,
  926.                 'delivery_address' => null
  927.             ]);
  928.             $clone->lines = new ArrayCollection();
  929.             foreach ($this->getFilterLines() as $line) {
  930.                 $clone_line $line->duplicate($clone);
  931.                 $clone->addLine($clone_line);
  932.             }
  933.             return $clone;
  934.         }
  935.         return $this;
  936.     }
  937.     /**
  938.      * @return Customer|null
  939.      */
  940.     public function getFinalDeliveryAddress(): ?Customer
  941.     {
  942.         return $this->getDeliveryAddress() ?: $this->getInvoiceAddress();
  943.     }
  944.     /**
  945.      * @return float
  946.      */
  947.     public function getPercentage(): float
  948.     {
  949.         $residualPercentage $this->getResidual() / (float) $this->getTotalAmount() * 100;
  950.         return round(100 $residualPercentage2);
  951.     }
  952.     /**
  953.      * @return Collection
  954.      */
  955.     public function getQuotations(): Collection
  956.     {
  957.         return $this->quotations;
  958.     }
  959.     /**
  960.      * @param Quotation $quotation
  961.      * @return $this
  962.      */
  963.     public function addQuotation(Quotation $quotation): self
  964.     {
  965.         if (!$this->quotations->contains($quotation)) {
  966.             if ($this->getQuotations()->isEmpty()) {
  967.                 $this->setCommercial($quotation->getCommercial());
  968.                 $this->setCustomer($quotation->getCustomer());
  969.                 $this->setInvoiceAddress($quotation->getInvoiceAddress());
  970.                 $this->setDeliveryAddress($quotation->getDeliveryAddress());
  971.                 $this->setInvoiceContact($quotation->getInvoiceContact());
  972.                 $this->setDeliveryContact($quotation->getDeliveryContact());
  973.                 $this->setCurrency($quotation->getCurrency());
  974.                 $this->setContext($quotation->getContext());
  975.                 $this->setEstablishment($quotation->getEstablishment());
  976.                 $this->setExternalName($quotation->getExternalName());
  977.                 $this->setTotalAmountNoDiscountUntaxed($quotation->getTotalAmountNoDiscountUntaxed());
  978.                 $this->setTotalAmountNoDiscount($quotation->getTotalAmountNoDiscount());
  979.                 $this->setTotalDiscount($quotation->getTotalDiscount());
  980.                 $this->setTotalTaxAmount($quotation->getTotalTaxAmount());
  981.                 $this->setTotalAmountUntaxed($quotation->getTotalAmountUntaxed());
  982.                 $this->setTotalAmount($quotation->getTotalAmount());
  983.                 if (!empty($quotation->getOptions()['note'])) {
  984.                     $this->addOptions([
  985.                         'note' => $quotation->getOptions()['note']
  986.                     ]);
  987.                 }
  988.                 if (!empty($quotation->getOptions()['customerNote'])) {
  989.                     $this->addOptions([
  990.                         'customerNote' => $quotation->getOptions()['customerNote']
  991.                     ]);
  992.                 }
  993.             }
  994.             $this->quotations[] = $quotation;
  995.         }
  996.         return $this;
  997.     }
  998.     /**
  999.      * @return Quotation|null
  1000.      */
  1001.     public function getQuotation(): ?Quotation
  1002.     {
  1003.         if ($this->quotations->isEmpty()) {
  1004.             return null;
  1005.         } else {
  1006.             return $this->quotations->first();
  1007.         }
  1008.     }
  1009.     /**
  1010.      * @return Establishment|null
  1011.      */
  1012.     public function getEstablishment(): ?Establishment
  1013.     {
  1014.         return $this->establishment;
  1015.     }
  1016.     /**
  1017.      * @param Establishment $establishment
  1018.      * @return $this
  1019.      */
  1020.     public function setEstablishment(?Establishment $establishment): self
  1021.     {
  1022.         $this->establishment $establishment;
  1023.         return $this;
  1024.     }
  1025. }