vendor/bluue/recurring-invoices-bundle/src/Entity/RecurringInvoice.php line 53

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