vendor/bluue/sales-bundle/src/Entity/DeliveryNote.php line 45

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\Entity\User;
  11. use App\Entity\Context;
  12. use App\Entity\Currency;
  13. use App\Services\SoftdeleteFilter;
  14. use Bluue\SalesBundle\DoctrineExtensions\EditPricesWithTaxEntity;
  15. use DateTime;
  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 Symfony\Bridge\Doctrine\IdGenerator\UuidGenerator;
  25. use Bluue\SalesBundle\Repository\DeliveryNoteRepository;
  26. use App\DoctrineExtensions\Cancelable\Traits\UserCancelableEntity;
  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=DeliveryNoteRepository::class)
  33.  * @ORM\Table(name="sales_bundle__delivery_note", indexes={
  34.  *  @ORM\Index(name="deleted_at", columns={"deleted_at"}),
  35.  *  @ORM\Index(name="created_at", columns={"created_at"}),
  36.  *  @ORM\Index(name="updated_at", columns={"updated_at"})
  37.  * })
  38.  * @Gedmo\SoftDeleteable(fieldName="deletedAt", timeAware=false, hardDelete=false)
  39.  */
  40. class DeliveryNote
  41. {
  42.     use UserValidatableEntity;
  43.     use UserCancelableEntity;
  44.     use UserTimestampableEntity;
  45.     use UserSoftDeleteableEntity;
  46.     use EditPricesWithTaxEntity;
  47.     use EntityManagerServiceEntity;
  48.     /**
  49.      * @ORM\Id
  50.      * @ORM\Column(type="uuid")
  51.      * @ORM\GeneratedValue(strategy="CUSTOM")
  52.      * @ORM\CustomIdGenerator(class=UuidGenerator::class)
  53.      */
  54.     private ?Uuid $id null;
  55.     /**
  56.      * @ORM\ManyToOne(targetEntity=Context::class)
  57.      */
  58.     private ?Context $context null;
  59.     /**
  60.      * @ORM\ManyToOne(targetEntity=Customer::class)
  61.      * @ORM\JoinColumn(nullable=false)
  62.      */
  63.     private ?Customer $customer null;
  64.     /**
  65.      * @ORM\ManyToOne(targetEntity=User::class)
  66.      */
  67.     private ?User $commercial null;
  68.     /**
  69.      * @ORM\ManyToOne(targetEntity=Customer::class)
  70.      */
  71.     private ?Customer $invoice_address null;
  72.     /**
  73.      * @ORM\ManyToOne(targetEntity=Customer::class)
  74.      */
  75.     private ?Customer $delivery_address null;
  76.     /**
  77.      * @ORM\ManyToOne(targetEntity=Customer::class)
  78.      */
  79.     private ?Customer $invoiceContact null;
  80.     /**
  81.      * @ORM\ManyToOne(targetEntity=Customer::class)
  82.      */
  83.     private ?Customer $deliveryContact null;
  84.     /**
  85.      * @ORM\ManyToOne(targetEntity=Order::class, inversedBy="delivery_notes")
  86.      */
  87.     private ?Order $order null;
  88.     /**
  89.      * @ORM\ManyToOne(targetEntity=Invoice::class, inversedBy="delivery_notes")
  90.      */
  91.     private ?Invoice $invoice null;
  92.     /**
  93.      * @ORM\Column(type="string", length=128, nullable="true")
  94.      */
  95.     private ?string $reference null;
  96.     /**
  97.      * @ORM\ManyToOne(targetEntity=Currency::class)
  98.      */
  99.     private ?Currency $currency null;
  100.     /**
  101.      * @ORM\Column(type="decimal", precision=20, scale=12)
  102.      */
  103.     private ?string $currency_change_rate null;
  104.     /**
  105.      * @ORM\ManyToOne(targetEntity=PaymentMethod::class)
  106.      */
  107.     private ?PaymentMethod $payment_method null;
  108.     /**
  109.      * @ORM\Column(type="boolean")
  110.      */
  111.     private bool $reduced_vat false;
  112.     /**
  113.      * @ORM\Column(type="decimal", precision=20, scale=6, nullable="true")
  114.      */
  115.     private ?string $total_discount_untaxed null;
  116.     /**
  117.      * @ORM\Column(type="decimal", precision=20, scale=6, nullable="true")
  118.      */
  119.     private ?string $totalDiscount null;
  120.     /**
  121.      * @ORM\Column(type="decimal", precision=20, scale=6, nullable="true")
  122.      */
  123.     private ?string $total_amount_no_discount_untaxed null;
  124.     /**
  125.      * @ORM\Column(type="decimal", precision=20, scale=6, nullable="true")
  126.      */
  127.     private ?string $totalAmountNoDiscount null;
  128.     /**
  129.      * @ORM\Column(type="decimal", precision=20, scale=6, nullable="true")
  130.      */
  131.     private ?string $total_amount_untaxed null;
  132.     /**
  133.      * @ORM\Column(type="decimal", precision=20, scale=6, nullable="true")
  134.      */
  135.     private ?string $total_tax_amount null;
  136.     /**
  137.      * @ORM\Column(type="decimal", precision=20, scale=6, nullable="true")
  138.      */
  139.     private ?string $total_amount null;
  140.     /**
  141.      * @ORM\Column(type="json")
  142.      */
  143.     private array $options = [];
  144.     /**
  145.      * @ORM\OneToMany(
  146.      *      targetEntity=DeliveryNoteLine::class,
  147.      *      mappedBy="delivery_note",
  148.      *      cascade={"persist", "remove"},
  149.      *      fetch="EXTRA_LAZY"
  150.      * )
  151.      * @ORM\OrderBy({"position" = "ASC"})
  152.      */
  153.     private Collection $lines;
  154.     /**
  155.      * @ORM\ManyToOne(targetEntity=Establishment::class)
  156.      */
  157.     private ?Establishment $establishment null;
  158.     public function __construct()
  159.     {
  160.         $this->lines = new ArrayCollection();
  161.     }
  162.     /**
  163.      * @return Uuid|null
  164.      */
  165.     public function getId(): ?Uuid
  166.     {
  167.         return $this->id;
  168.     }
  169.     /**
  170.      * @return DeliveryNote
  171.      */
  172.     public function setId(): self
  173.     {
  174.         $this->id null;
  175.         return $this;
  176.     }
  177.     /**
  178.      * @return Context|null
  179.      */
  180.     public function getContext(): ?Context
  181.     {
  182.         return $this->context;
  183.     }
  184.     /**
  185.      * @param Context|null $context
  186.      * @return DeliveryNote
  187.      */
  188.     public function setContext(?Context $context): self
  189.     {
  190.         $this->context $context;
  191.         return $this;
  192.     }
  193.     /**
  194.      * @return Customer|null
  195.      * @throws NonUniqueResultException
  196.      */
  197.     public function getCustomer(): ?Customer
  198.     {
  199.         if ($this->em && $this->customer) {
  200.             SoftdeleteFilter::disable($this->em, [Customer::class]);
  201.             $customer $this->em->getRepository(Customer::class)
  202.                 ->createQueryBuilder('c')
  203.                 ->where('c.id = :customerId')
  204.                 ->setParameter('customerId'$this->customer->getId()->toBinary())
  205.                 ->getQuery()
  206.                 ->getOneOrNullResult();
  207.             SoftdeleteFilter::enable($this->em, [Customer::class]);
  208.             return $customer;
  209.         }
  210.         return $this->customer;
  211.     }
  212.     /**
  213.      * @param Customer $customer
  214.      * @return DeliveryNote
  215.      */
  216.     public function setCustomer(Customer $customer): self
  217.     {
  218.         if ($this->customer && $this->customer->getId() !== $customer->getId()) {
  219.             $this->setInvoiceContact(null)->setDeliveryContact(null);
  220.         }
  221.         $this->customer $customer;
  222.         $this->setDeliveryAddress($customer);
  223.         return $this->setInvoiceAddress($customer);
  224.     }
  225.     /**
  226.      * @return User|null
  227.      * @throws NonUniqueResultException
  228.      */
  229.     public function getCommercial(): ?User
  230.     {
  231.         if ($this->em && $this->commercial) {
  232.             SoftdeleteFilter::disable($this->em, [User::class]);
  233.             $commercial $this->em->getRepository(User::class)
  234.                 ->createQueryBuilder('u')
  235.                 ->where('u.id = :commercialId')
  236.                 ->setParameter('commercialId'$this->commercial->getId()->toBinary())
  237.                 ->getQuery()
  238.                 ->getOneOrNullResult();
  239.             SoftdeleteFilter::enable($this->em, [User::class]);
  240.             return $commercial;
  241.         }
  242.         return $this->commercial;
  243.     }
  244.     /**
  245.      * @param User|null $commercial
  246.      * @return DeliveryNote
  247.      */
  248.     public function setCommercial(?User $commercial): self
  249.     {
  250.         $this->commercial $commercial;
  251.         return $this;
  252.     }
  253.     /**
  254.      * @return Customer|null
  255.      * @throws NonUniqueResultException
  256.      */
  257.     public function getInvoiceAddress(): ?Customer
  258.     {
  259.         if ($this->em && $this->invoice_address) {
  260.             SoftdeleteFilter::disable($this->em, [Customer::class]);
  261.             $invoiceAddress $this->em->getRepository(Customer::class)
  262.                 ->createQueryBuilder('c')
  263.                 ->where('c.id = :invoiceAddressId')
  264.                 ->setParameter('invoiceAddressId'$this->invoice_address->getId()->toBinary())
  265.                 ->getQuery()
  266.                 ->getOneOrNullResult();
  267.             SoftdeleteFilter::enable($this->em, [Customer::class]);
  268.             return $invoiceAddress;
  269.         }
  270.         return $this->invoice_address;
  271.     }
  272.     /**
  273.      * @param Customer|null $invoice_address
  274.      * @return DeliveryNote
  275.      */
  276.     public function setInvoiceAddress(?Customer $invoice_address): DeliveryNote
  277.     {
  278.         $this->invoice_address $invoice_address;
  279.         return $this;
  280.     }
  281.     /**
  282.      * @return Customer|null
  283.      * @throws NonUniqueResultException
  284.      */
  285.     public function getDeliveryAddress(): ?Customer
  286.     {
  287.         if ($this->em && $this->delivery_address) {
  288.             SoftdeleteFilter::disable($this->em, [Customer::class]);
  289.             $deliveryAddress $this->em->getRepository(Customer::class)
  290.                 ->createQueryBuilder('c')
  291.                 ->where('c.id = :deliveryAddressId')
  292.                 ->setParameter('deliveryAddressId'$this->delivery_address->getId()->toBinary())
  293.                 ->getQuery()
  294.                 ->getOneOrNullResult();
  295.             SoftdeleteFilter::enable($this->em, [Customer::class]);
  296.             return $deliveryAddress;
  297.         }
  298.         return $this->delivery_address;
  299.     }
  300.     /**
  301.      * @param Customer|null $delivery_address
  302.      * @return DeliveryNote
  303.      */
  304.     public function setDeliveryAddress(?Customer $delivery_address): DeliveryNote
  305.     {
  306.         $this->delivery_address $delivery_address;
  307.         return $this;
  308.     }
  309.     /**
  310.      * @return Customer|null
  311.      * @throws NonUniqueResultException
  312.      */
  313.     public function getInvoiceContact(): ?Customer
  314.     {
  315.         if ($this->em && $this->invoiceContact) {
  316.             SoftdeleteFilter::disable($this->em, [Customer::class]);
  317.             $invoiceContact $this->em->getRepository(Customer::class)
  318.                 ->createQueryBuilder('c')
  319.                 ->where('c.id = :invoiceContactId')
  320.                 ->setParameter('invoiceContactId'$this->invoiceContact->getId()->toBinary())
  321.                 ->getQuery()
  322.                 ->getOneOrNullResult();
  323.             SoftdeleteFilter::enable($this->em, [Customer::class]);
  324.             return $invoiceContact;
  325.         }
  326.         return $this->invoiceContact;
  327.     }
  328.     /**
  329.      * @param Customer|null $invoiceContact
  330.      * @return DeliveryNote
  331.      */
  332.     public function setInvoiceContact(?Customer $invoiceContact): DeliveryNote
  333.     {
  334.         $this->invoiceContact $invoiceContact;
  335.         return $this;
  336.     }
  337.     /**
  338.      * @return Customer|null
  339.      * @throws NonUniqueResultException
  340.      */
  341.     public function getDeliveryContact(): ?Customer
  342.     {
  343.         if ($this->em && $this->deliveryContact) {
  344.             SoftdeleteFilter::disable($this->em, [Customer::class]);
  345.             $deliveryContact $this->em->getRepository(Customer::class)
  346.                 ->createQueryBuilder('c')
  347.                 ->where('c.id = :deliveryContactId')
  348.                 ->setParameter('deliveryContactId'$this->deliveryContact->getId()->toBinary())
  349.                 ->getQuery()
  350.                 ->getOneOrNullResult();
  351.             SoftdeleteFilter::enable($this->em, [Customer::class]);
  352.             return $deliveryContact;
  353.         }
  354.         return $this->deliveryContact;
  355.     }
  356.     /**
  357.      * @param Customer|null $deliveryContact
  358.      * @return DeliveryNote
  359.      */
  360.     public function setDeliveryContact(?Customer $deliveryContact): DeliveryNote
  361.     {
  362.         $this->deliveryContact $deliveryContact;
  363.         return $this;
  364.     }
  365.     /**
  366.      * @return Order|null
  367.      * @throws NonUniqueResultException
  368.      */
  369.     public function getOrder(): ?Order
  370.     {
  371.         if ($this->em && $this->order) {
  372.             SoftdeleteFilter::disable($this->em, [Order::class]);
  373.             $order $this->em->getRepository(Order::class)
  374.                 ->createQueryBuilder('o')
  375.                 ->where('o.id = :orderId')
  376.                 ->setParameter('orderId'$this->order->getId()->toBinary())
  377.                 ->getQuery()
  378.                 ->getOneOrNullResult();
  379.             SoftdeleteFilter::enable($this->em, [Order::class]);
  380.             return $order;
  381.         }
  382.         return $this->order;
  383.     }
  384.     /**
  385.      * @param Order|null $order
  386.      * @return DeliveryNote
  387.      * @throws NonUniqueResultException
  388.      */
  389.     public function setOrder(?Order $order): self
  390.     {
  391.         $this->order $order;
  392.         if ($order) {
  393.             $this->setCustomer($order->getCustomer());
  394.             $this->setCommercial($order->getCommercial());
  395.             $this->setInvoiceAddress($order->getInvoiceAddress());
  396.             $this->setDeliveryAddress($order->getDeliveryAddress());
  397.             $this->setInvoiceContact($order->getInvoiceContact());
  398.             $this->setDeliveryContact($order->getDeliveryContact());
  399.             $this->setContext($order->getContext());
  400.             $this->setEstablishment($order->getEstablishment());
  401.             $this->setCurrency($order->getCurrency());
  402.             $this->setCurrencyChangeRate($order->getCurrencyChangeRate());
  403.             $this->setReducedVat($order->isReducedVat());
  404.             if (!empty($order->getOptions()['invoice_address'])) {
  405.                 $this->addOptions([
  406.                     'invoice_address' => $order->getOptions()['invoice_address']
  407.                 ]);
  408.             }
  409.             if (!empty($order->getOptions()['delivery_address'])) {
  410.                 $this->addOptions([
  411.                     'delivery_address' => $order->getOptions()['delivery_address']
  412.                 ]);
  413.             }
  414.             if (!empty($order->getOptions()['note'])) {
  415.                 $this->addOptions([
  416.                     'note' => $order->getOptions()['note']
  417.                 ]);
  418.             }
  419.             if (!empty($order->getOptions()['customerNote'])) {
  420.                 $this->addOptions([
  421.                     'customerNote' => $order->getOptions()['customerNote']
  422.                 ]);
  423.             }
  424.         }
  425.         return $this;
  426.     }
  427.     /**
  428.      * @return Invoice|null
  429.      * @throws NonUniqueResultException
  430.      */
  431.     public function getInvoice(): ?Invoice
  432.     {
  433.         if ($this->em && $this->invoice) {
  434.             SoftdeleteFilter::disable($this->em, [Invoice::class]);
  435.             $invoice $this->em->getRepository(Invoice::class)
  436.                 ->createQueryBuilder('i')
  437.                 ->where('i.id = :invoiceId')
  438.                 ->setParameter('invoiceId'$this->invoice->getId()->toBinary())
  439.                 ->getQuery()
  440.                 ->getOneOrNullResult();
  441.             SoftdeleteFilter::enable($this->em, [Invoice::class]);
  442.             return $invoice;
  443.         }
  444.         return $this->invoice;
  445.     }
  446.     /**
  447.      * @param Invoice|null $invoice
  448.      * @return DeliveryNote
  449.      */
  450.     public function setInvoice(?Invoice $invoice): self
  451.     {
  452.         $this->invoice $invoice;
  453.         return $this;
  454.     }
  455.     /**
  456.      * @return string|null
  457.      */
  458.     public function getReference(): ?string
  459.     {
  460.         return $this->reference;
  461.     }
  462.     /**
  463.      * @param string|null $reference
  464.      * @return DeliveryNote
  465.      */
  466.     public function setReference(?string $reference): self
  467.     {
  468.         $this->reference $reference;
  469.         return $this;
  470.     }
  471.     /**
  472.      * @return Currency|null
  473.      */
  474.     public function getCurrency(): ?Currency
  475.     {
  476.         return $this->currency;
  477.     }
  478.     /**
  479.      * @param Currency|null $currency
  480.      * @return DeliveryNote
  481.      */
  482.     public function setCurrency(?Currency $currency): DeliveryNote
  483.     {
  484.         $this->currency $currency;
  485.         return $this->setCurrencyChangeRate($currency->getChangeRate());
  486.     }
  487.     /**
  488.      * @return string|null
  489.      */
  490.     public function getCurrencyChangeRate(): ?string
  491.     {
  492.         return $this->currency_change_rate;
  493.     }
  494.     /**
  495.      * @param string|null $currency_change_rate
  496.      * @return DeliveryNote
  497.      */
  498.     public function setCurrencyChangeRate(?string $currency_change_rate): DeliveryNote
  499.     {
  500.         $this->currency_change_rate $currency_change_rate;
  501.         return $this;
  502.     }
  503.     /**
  504.      * @return PaymentMethod|null
  505.      * @throws NonUniqueResultException
  506.      */
  507.     public function getPaymentMethod(): ?PaymentMethod
  508.     {
  509.         if ($this->em && $this->payment_method) {
  510.             SoftdeleteFilter::disable($this->em, [PaymentMethod::class]);
  511.             $payment_method $this->em->getRepository(PaymentMethod::class)
  512.                 ->createQueryBuilder('pm')
  513.                 ->where('pm.id = :paymentMethodId')
  514.                 ->setParameter('paymentMethodId'$this->payment_method->getId()->toBinary())
  515.                 ->getQuery()
  516.                 ->getOneOrNullResult();
  517.             SoftdeleteFilter::enable($this->em, [PaymentMethod::class]);
  518.             return $payment_method;
  519.         }
  520.         return $this->payment_method;
  521.     }
  522.     /**
  523.      * @param PaymentMethod|null $payment_method
  524.      */
  525.     public function setPaymentMethod(?PaymentMethod $payment_method): void
  526.     {
  527.         $this->payment_method $payment_method;
  528.     }
  529.     /**
  530.      * @return bool
  531.      */
  532.     public function isReducedVat(): bool
  533.     {
  534.         return $this->reduced_vat;
  535.     }
  536.     /**
  537.      * @param bool $reduced_vat
  538.      * @return $this
  539.      */
  540.     public function setReducedVat(bool $reduced_vat): self
  541.     {
  542.         $this->reduced_vat $reduced_vat;
  543.         return $this;
  544.     }
  545.     /**
  546.      * @return string|null
  547.      */
  548.     public function getTotalDiscountUntaxed(): ?string
  549.     {
  550.         return $this->total_discount_untaxed;
  551.     }
  552.     /**
  553.      * @param string|null $total_discount_untaxed
  554.      * @return DeliveryNote
  555.      */
  556.     public function setTotalDiscountUntaxed(?string $total_discount_untaxed): DeliveryNote
  557.     {
  558.         $this->total_discount_untaxed $total_discount_untaxed;
  559.         return $this;
  560.     }
  561.     /**
  562.      * @return string|null
  563.      */
  564.     public function getTotalDiscount(): ?string
  565.     {
  566.         return $this->totalDiscount;
  567.     }
  568.     /**
  569.      * @param string|null $totalDiscount
  570.      * @return DeliveryNote
  571.      */
  572.     public function setTotalDiscount(?string $totalDiscount): DeliveryNote
  573.     {
  574.         $this->totalDiscount $totalDiscount;
  575.         return $this;
  576.     }
  577.     /**
  578.      * @return string|null
  579.      */
  580.     public function getTotalAmountNoDiscountUntaxed(): ?string
  581.     {
  582.         return $this->total_amount_no_discount_untaxed;
  583.     }
  584.     /**
  585.      * @param string|null $total_amount_no_discount_untaxed
  586.      * @return DeliveryNote
  587.      */
  588.     public function setTotalAmountNoDiscountUntaxed(?string $total_amount_no_discount_untaxed): DeliveryNote
  589.     {
  590.         $this->total_amount_no_discount_untaxed $total_amount_no_discount_untaxed;
  591.         return $this;
  592.     }
  593.     /**
  594.      * @return string|null
  595.      */
  596.     public function getTotalAmountNoDiscount(): ?string
  597.     {
  598.         return $this->totalAmountNoDiscount;
  599.     }
  600.     /**
  601.      * @param string|null $totalAmountNoDiscount
  602.      * @return DeliveryNote
  603.      */
  604.     public function setTotalAmountNoDiscount(?string $totalAmountNoDiscount): DeliveryNote
  605.     {
  606.         $this->totalAmountNoDiscount $totalAmountNoDiscount;
  607.         return $this;
  608.     }
  609.     /**
  610.      * @return string|null
  611.      */
  612.     public function getTotalAmountUntaxed(): ?string
  613.     {
  614.         return $this->total_amount_untaxed;
  615.     }
  616.     /**
  617.      * @param string|null $total_amount_untaxed
  618.      * @return DeliveryNote
  619.      */
  620.     public function setTotalAmountUntaxed(?string $total_amount_untaxed): DeliveryNote
  621.     {
  622.         $this->total_amount_untaxed $total_amount_untaxed;
  623.         return $this;
  624.     }
  625.     /**
  626.      * @return string|null
  627.      */
  628.     public function getTotalTaxAmount(): ?string
  629.     {
  630.         return $this->total_tax_amount;
  631.     }
  632.     /**
  633.      * @param string|null $total_tax_amount
  634.      * @return DeliveryNote
  635.      */
  636.     public function setTotalTaxAmount(?string $total_tax_amount): DeliveryNote
  637.     {
  638.         $this->total_tax_amount $total_tax_amount;
  639.         return $this;
  640.     }
  641.     /**
  642.      * @return string|null
  643.      */
  644.     public function getTotalAmount(): ?string
  645.     {
  646.         return $this->total_amount;
  647.     }
  648.     /**
  649.      * @param string|null $total_amount
  650.      * @return DeliveryNote
  651.      */
  652.     public function setTotalAmount(?string $total_amount): DeliveryNote
  653.     {
  654.         $this->total_amount $total_amount;
  655.         return $this;
  656.     }
  657.     /**
  658.      * @return array
  659.      */
  660.     public function getOptions(): array
  661.     {
  662.         return $this->options;
  663.     }
  664.     /**
  665.      * @param array $options
  666.      * @return DeliveryNote
  667.      */
  668.     public function setOptions(array $options): self
  669.     {
  670.         $this->options $options;
  671.         return $this;
  672.     }
  673.     /**
  674.      * @param array $options
  675.      * @return DeliveryNote
  676.      */
  677.     public function addOptions(array $options): self
  678.     {
  679.         return $this->setOptions(array_merge($this->options$options));
  680.     }
  681.     /**
  682.      * @return Collection|DeliveryNoteLine[]
  683.      */
  684.     public function getLines(): Collection
  685.     {
  686.         return $this->getFilterLines();
  687.     }
  688.     /**
  689.      * @param bool $withGroups
  690.      * @param bool $all
  691.      * @param bool $withPacks
  692.      * @return Collection|DeliveryNoteLine[]
  693.      */
  694.     public function getFilterLines(bool $withGroups truebool $all falsebool $withPacks true): Collection
  695.     {
  696.         if ($all) {
  697.             $lines $this->lines;
  698.             $goodLines = [];
  699.             foreach ($lines as $line) {
  700.                 if (!$line->getParent() && !$line->getPackParent()) {
  701.                     $goodLines[] = $line;
  702.                     foreach ($line->getChildrens() as $child) {
  703.                         $goodLines[] = $child;
  704.                         foreach ($child->getPackChildrens() as $grandChild) {
  705.                             $goodLines[] = $grandChild;
  706.                         }
  707.                     }
  708.                     foreach ($line->getPackChildrens() as $child) {
  709.                         $goodLines[] = $child;
  710.                     }
  711.                 }
  712.             }
  713.             return new ArrayCollection($goodLines);
  714.         }
  715.         $criteria Criteria::create();
  716.         if ($withGroups) {
  717.             $criteria
  718.                 ->where(Criteria::expr()->eq('parent'null))
  719.                 ->andWhere(Criteria::expr()->eq('packParent'null));
  720.         } else {
  721.             if ($withPacks) {
  722.                 $criteria->andWhere(Criteria::expr()->eq('is_pack'true));
  723.             } else {
  724.                 $criteria->andWhere(Criteria::expr()->eq('is_pack'false));
  725.             }
  726.             $criteria->where(Criteria::expr()->eq('is_group'false));
  727.         }
  728.         return $this->lines->matching($criteria);
  729.     }
  730.     /**
  731.      * @param DeliveryNoteLine $line
  732.      * @return DeliveryNote
  733.      */
  734.     public function addLine(DeliveryNoteLine $line): self
  735.     {
  736.         if (!$this->lines->contains($line)) {
  737.             $this->lines[] = $line;
  738.             if ($line->getDeliveryNote() !== $this) {
  739.                 $line->setDeliveryNote($this);
  740.             }
  741.         }
  742.         return $this;
  743.     }
  744.     /**
  745.      * @param DeliveryNoteLine $line
  746.      * @return DeliveryNote
  747.      */
  748.     public function removeLine(DeliveryNoteLine $line): self
  749.     {
  750.         $this->lines->removeElement($line);
  751.         return $this;
  752.     }
  753.     /**
  754.      * @return Collection|DeliveryNoteLine[]
  755.      */
  756.     public function getProductLines(): Collection
  757.     {
  758.         return $this->lines->filter(function (DeliveryNoteLine $deliveryNoteLine) {
  759.             return $deliveryNoteLine->isProduct();
  760.         });
  761.     }
  762.     /**
  763.      * @return bool
  764.      */
  765.     public function isCancelable(): bool
  766.     {
  767.         return $this->getValidatedAt()
  768.             && !$this->getCanceledAt()
  769.             && !$this->getInvoice();
  770.     }
  771.     /**
  772.      * @return bool
  773.      */
  774.     public function isInvoicable(): bool
  775.     {
  776.         return $this->isCancelable()
  777.             && (
  778.                 !$this->getOrder()
  779.                 || $this->getOrder()->isDeliveryNoteInvoicable()
  780.             )
  781.         ;
  782.     }
  783.     /**
  784.      * @return Establishment|null
  785.      */
  786.     public function getEstablishment(): ?Establishment
  787.     {
  788.         return $this->establishment;
  789.     }
  790.     /**
  791.      * @param Establishment|null $establishment
  792.      * @return $this
  793.      */
  794.     public function setEstablishment(?Establishment $establishment): self
  795.     {
  796.         $this->establishment $establishment;
  797.         return $this;
  798.     }
  799.     /**
  800.      * @return DeliveryNote
  801.      */
  802.     public function duplicate(): DeliveryNote
  803.     {
  804.         if ($this->id) {
  805.             $clone = clone $this;
  806.             $clone->setId();
  807.             $clone->setCreatedAt(new DateTime());
  808.             $clone->setCreatedBy(null);
  809.             $clone->setUpdatedAt(new DateTime());
  810.             $clone->setUpdatedBy(null);
  811.             $clone->setValidatedAt(null);
  812.             $clone->setValidatedBy(null);
  813.             $clone->setCanceledAt(null);
  814.             $clone->setCanceledBy(null);
  815.             $clone->setContext(null);
  816.             $clone->setEstablishment(null);
  817.             $clone->setReference(null);
  818.             $clone->setOrder(null);
  819.             $clone->setInvoice(null);
  820.             $clone->addOptions([
  821.                 'invoice_address' => null,
  822.                 'delivery_address' => null
  823.             ]);
  824.             $clone->lines = new ArrayCollection();
  825.             foreach ($this->getFilterLines() as $line) {
  826.                 $clone_line $line->duplicate($clone);
  827.                 $clone->addLine($clone_line);
  828.             }
  829.             return $clone;
  830.         }
  831.         return $this;
  832.     }
  833. }