<?php
/**
* @author Quentin CHATELAIN (contact@scaledev.fr)
* @copyright 2021 - ScaleDEV SAS, 12 RUE CHARLES MORET, 10120 ST ANDRE LES VERGERS
* @license commercial
*/
declare(strict_types=1);
namespace Bluue\SalesBundle\Entity;
use App\Entity\Traits\EntityManagerServiceEntity;
use App\Services\SoftdeleteFilter;
use Bluue\SalesBundle\DoctrineExtensions\EditPricesWithTaxEntity;
use DateTime;
use App\Entity\User;
use DateTimeInterface;
use App\Entity\Context;
use App\Entity\Currency;
use Doctrine\ORM\NonUniqueResultException;
use Symfony\Component\Uid\Uuid;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;
use Doctrine\Common\Collections\Criteria;
use Bluue\CustomersBundle\Entity\Customer;
use Doctrine\Common\Collections\Collection;
use Doctrine\Common\Collections\ArrayCollection;
use Bluue\SalesBundle\Repository\InvoiceRepository;
use Symfony\Bridge\Doctrine\IdGenerator\UuidGenerator;
use App\DoctrineExtensions\Cancelable\Traits\UserCancelableEntity;
use App\DoctrineExtensions\Validatable\Traits\UserValidatableEntity;
use App\DoctrineExtensions\Timestampable\Traits\UserTimestampableEntity;
use App\DoctrineExtensions\SoftDeleteable\Traits\UserSoftDeleteableEntity;
use App\Entity\Establishment;
/**
* @ORM\Entity(repositoryClass=InvoiceRepository::class)
* @ORM\Table(name="sales_bundle__invoice", indexes={
* @ORM\Index(name="internal_name", columns={"internal_name"}),
* @ORM\Index(name="reference", columns={"reference"}),
* @ORM\Index(name="total_amount_untaxed", columns={"total_amount_untaxed"}),
* @ORM\Index(name="total_amount", columns={"total_amount"}),
* @ORM\Index(name="residual", columns={"residual"}),
* @ORM\Index(name="validated_at", columns={"validated_at"}),
* @ORM\Index(name="canceled_at", columns={"canceled_at"}),
* @ORM\Index(name="deleted_at", columns={"deleted_at"}),
* @ORM\Index(name="created_at", columns={"created_at"}),
* @ORM\Index(name="updated_at", columns={"updated_at"})
* })
* @Gedmo\SoftDeleteable(fieldName="deletedAt", timeAware=false, hardDelete=false)
*/
class Invoice
{
use UserValidatableEntity;
use UserCancelableEntity;
use UserTimestampableEntity;
use UserSoftDeleteableEntity;
use EditPricesWithTaxEntity;
use EntityManagerServiceEntity;
/**
* @ORM\Id
* @ORM\Column(type="uuid")
* @ORM\GeneratedValue(strategy="CUSTOM")
* @ORM\CustomIdGenerator(class=UuidGenerator::class)
*/
private ?Uuid $id = null;
/**
* @ORM\ManyToOne(targetEntity=Context::class)
*/
private ?Context $context = null;
/**
* @ORM\ManyToOne(targetEntity=Customer::class)
* @ORM\JoinColumn(nullable=false)
*/
private ?Customer $customer = null;
/**
* @ORM\ManyToOne(targetEntity=User::class)
*/
private ?User $commercial = null;
/**
* @ORM\ManyToOne(targetEntity=Customer::class)
*/
private ?Customer $invoice_address = null;
/**
* @ORM\ManyToOne(targetEntity=Customer::class)
*/
private ?Customer $delivery_address = null;
/**
* @ORM\ManyToOne(targetEntity=Customer::class)
*/
private ?Customer $invoiceContact = null;
/**
* @ORM\ManyToOne(targetEntity=Customer::class)
*/
private ?Customer $deliveryContact = null;
/**
* @ORM\ManyToOne(targetEntity=Quotation::class, inversedBy="invoices")
*/
private ?Quotation $quotation = null;
/**
* @ORM\ManyToOne(targetEntity=Order::class, inversedBy="invoices")
*/
private ?Order $order = null;
/**
* @ORM\ManyToOne(targetEntity=CreditNote::class, inversedBy="invoices")
*/
private ?CreditNote $credit_note = null;
/**
* @ORM\ManyToOne(targetEntity=Currency::class)
*/
private ?Currency $currency = null;
/**
* @ORM\ManyToOne(targetEntity=PaymentTerm::class)
*/
private ?PaymentTerm $payment_term = null;
/**
* @ORM\Column(type="decimal", precision=20, scale=12)
*/
private ?string $currency_change_rate = null;
/**
* @ORM\ManyToOne(targetEntity=PaymentMethod::class)
*/
private ?PaymentMethod $payment_method = null;
/**
* @ORM\Column(type="string", length=128, nullable="true")
*/
private ?string $reference = null;
/**
* @ORM\Column(type="string", length=255, nullable="true")
*/
private ?string $internal_name = null;
/**
* @ORM\Column(type="string", length=255, nullable="true")
*/
private ?string $external_name = null;
/**
* @ORM\Column(type="boolean")
*/
private bool $reduced_vat = false;
/**
* @ORM\Column(type="decimal", precision=20, scale=6, nullable="true")
*/
private ?string $total_discount_untaxed = null;
/**
* @ORM\Column(type="decimal", precision=20, scale=6, nullable="true")
*/
private ?string $totalDiscount = null;
/**
* @ORM\Column(type="decimal", precision=20, scale=6, nullable="true")
*/
private ?string $total_amount_no_discount_untaxed = null;
/**
* @ORM\Column(type="decimal", precision=20, scale=6, nullable="true")
*/
private ?string $totalAmountNoDiscount = null;
/**
* @ORM\Column(type="decimal", precision=20, scale=6, nullable="true")
*/
private ?string $total_amount_untaxed = null;
/**
* @ORM\Column(type="decimal", precision=20, scale=6, nullable="true")
*/
private ?string $total_tax_amount = null;
/**
* @ORM\Column(type="decimal", precision=20, scale=6, nullable="true")
*/
private ?string $total_amount = null;
/**
* @ORM\Column(type="decimal", precision=20, scale=6, nullable="true")
*/
private ?string $residual = null;
/**
* @ORM\Column(type="date", nullable="true")
*/
private ?DateTimeInterface $due_date = null;
/**
* @ORM\Column(type="boolean")
*/
private bool $is_deposit = false;
/**
* @ORM\Column(type="json")
*/
private array $options = [];
/**
* @ORM\OneToMany(
* targetEntity=InvoiceLine::class,
* mappedBy="invoice",
* cascade={"persist", "remove"},
* fetch="EXTRA_LAZY"
* )
* @ORM\OrderBy({"position" = "ASC"})
*/
private Collection $lines;
/**
* @ORM\OneToMany(targetEntity=DocumentPayment::class, mappedBy="invoice", fetch="EXTRA_LAZY")
*/
private Collection $payments;
/**
* @ORM\OneToMany(targetEntity=DeliveryNote::class, mappedBy="invoice")
*/
private Collection $delivery_notes;
/**
* @ORM\OneToMany(targetEntity=CreditNote::class, mappedBy="invoice")
*/
private Collection $creditNotes;
/**
* @var mixed $recurringInvoice
*/
private $recurringInvoice;
/**
* @ORM\ManyToOne(targetEntity=Establishment::class)
*/
private ?Establishment $establishment = null;
public function __construct()
{
$this->lines = new ArrayCollection();
$this->payments = new ArrayCollection();
$this->delivery_notes = new ArrayCollection();
$this->creditNotes = new ArrayCollection();
}
/**
* @return Uuid|null
*/
public function getId(): ?Uuid
{
return $this->id;
}
/**
* @return Invoice
*/
public function setId(): self
{
$this->id = null;
return $this;
}
/**
* @return Context|null
*/
public function getContext(): ?Context
{
return $this->context;
}
/**
* @param Context|null $context
* @return Invoice
*/
public function setContext(?Context $context): self
{
$this->context = $context;
return $this;
}
/**
* @return Customer|null
* @throws NonUniqueResultException
*/
public function getCustomer(): ?Customer
{
if ($this->em && $this->customer) {
SoftdeleteFilter::disable($this->em, [Customer::class]);
$customer = $this->em->getRepository(Customer::class)
->createQueryBuilder('c')
->where('c.id = :customerId')
->setParameter('customerId', $this->customer->getId()->toBinary())
->getQuery()
->getOneOrNullResult();
SoftdeleteFilter::enable($this->em, [Customer::class]);
return $customer;
}
return $this->customer;
}
/**
* @param Customer $customer
* @return Invoice
*/
public function setCustomer(Customer $customer): self
{
if ($this->customer && $this->customer->getId() !== $customer->getId()) {
$this->setInvoiceContact(null)->setDeliveryContact(null);
}
$this->customer = $customer;
$this->setDeliveryAddress(null);
return $this->setInvoiceAddress($customer);
}
/**
* @return User|null
* @throws NonUniqueResultException
*/
public function getCommercial(): ?User
{
if ($this->em && $this->commercial) {
SoftdeleteFilter::disable($this->em, [User::class]);
$commercial = $this->em->getRepository(User::class)
->createQueryBuilder('u')
->where('u.id = :commercialId')
->setParameter('commercialId', $this->commercial->getId()->toBinary())
->getQuery()
->getOneOrNullResult();
SoftdeleteFilter::enable($this->em, [User::class]);
return $commercial;
}
return $this->commercial;
}
/**
* @param User|null $commercial
* @return Invoice
*/
public function setCommercial(?User $commercial): self
{
$this->commercial = $commercial;
return $this;
}
/**
* @return Customer|null
* @throws NonUniqueResultException
*/
public function getInvoiceAddress(): ?Customer
{
if ($this->em && $this->invoice_address) {
SoftdeleteFilter::disable($this->em, [Customer::class]);
$invoiceAddress = $this->em->getRepository(Customer::class)
->createQueryBuilder('c')
->where('c.id = :invoiceAddressId')
->setParameter('invoiceAddressId', $this->invoice_address->getId()->toBinary())
->getQuery()
->getOneOrNullResult();
SoftdeleteFilter::enable($this->em, [Customer::class]);
return $invoiceAddress;
}
return $this->invoice_address;
}
/**
* @param Customer|null $invoice_address
* @return Invoice
*/
public function setInvoiceAddress(?Customer $invoice_address): self
{
$this->invoice_address = $invoice_address;
return $this;
}
/**
* @return Customer|null
* @throws NonUniqueResultException
*/
public function getDeliveryAddress(): ?Customer
{
if ($this->em && $this->delivery_address) {
SoftdeleteFilter::disable($this->em, [Customer::class]);
$deliveryAddress = $this->em->getRepository(Customer::class)
->createQueryBuilder('c')
->where('c.id = :deliveryAddressId')
->setParameter('deliveryAddressId', $this->delivery_address->getId()->toBinary())
->getQuery()
->getOneOrNullResult();
SoftdeleteFilter::enable($this->em, [Customer::class]);
return $deliveryAddress;
}
return $this->delivery_address;
}
/**
* @param Customer|null $delivery_address
* @return Invoice
*/
public function setDeliveryAddress(?Customer $delivery_address): self
{
$this->delivery_address = $delivery_address;
return $this;
}
/**
* @return Customer|null
* @throws NonUniqueResultException
*/
public function getInvoiceContact(): ?Customer
{
if ($this->em && $this->invoiceContact) {
SoftdeleteFilter::disable($this->em, [Customer::class]);
$invoiceContact = $this->em->getRepository(Customer::class)
->createQueryBuilder('c')
->where('c.id = :invoiceContactId')
->setParameter('invoiceContactId', $this->invoiceContact->getId()->toBinary())
->getQuery()
->getOneOrNullResult();
SoftdeleteFilter::enable($this->em, [Customer::class]);
return $invoiceContact;
}
return $this->invoiceContact;
}
/**
* @param Customer|null $invoiceContact
* @return Invoice
*/
public function setInvoiceContact(?Customer $invoiceContact): Invoice
{
$this->invoiceContact = $invoiceContact;
return $this;
}
/**
* @return Customer|null
* @throws NonUniqueResultException
*/
public function getDeliveryContact(): ?Customer
{
if ($this->em && $this->deliveryContact) {
SoftdeleteFilter::disable($this->em, [Customer::class]);
$deliveryContact = $this->em->getRepository(Customer::class)
->createQueryBuilder('c')
->where('c.id = :deliveryContactId')
->setParameter('deliveryContactId', $this->deliveryContact->getId()->toBinary())
->getQuery()
->getOneOrNullResult();
SoftdeleteFilter::enable($this->em, [Customer::class]);
return $deliveryContact;
}
return $this->deliveryContact;
}
/**
* @param Customer|null $deliveryContact
* @return Invoice
*/
public function setDeliveryContact(?Customer $deliveryContact): Invoice
{
$this->deliveryContact = $deliveryContact;
return $this;
}
/**
* @return Quotation|null
* @throws NonUniqueResultException
*/
public function getQuotation(): ?Quotation
{
if ($this->em && $this->quotation) {
SoftdeleteFilter::disable($this->em, [Quotation::class]);
$quotation = $this->em->getRepository(Quotation::class)
->createQueryBuilder('q')
->where('q.id = :quotationId')
->setParameter('quotationId', $this->quotation->getId()->toBinary())
->getQuery()
->getOneOrNullResult();
SoftdeleteFilter::enable($this->em, [Quotation::class]);
return $quotation;
}
return $this->quotation;
}
/**
* @param Quotation|null $quotation
* @return Invoice
* @throws NonUniqueResultException
*/
public function setQuotation(?Quotation $quotation): self
{
$this->quotation = $quotation;
if ($quotation) {
$this->setCustomer($quotation->getCustomer());
$this->setCommercial($quotation->getCommercial());
$this->setInvoiceAddress($quotation->getInvoiceAddress());
$this->setDeliveryAddress($quotation->getDeliveryAddress());
$this->setInvoiceContact($quotation->getInvoiceContact());
$this->setDeliveryContact($quotation->getDeliveryContact());
$this->setCurrency($quotation->getCurrency());
$this->setContext($quotation->getContext());
$this->setEstablishment($quotation->getEstablishment());
$this->setExternalName($quotation->getExternalName());
if (!$this->getIsDeposit()) {
$this->setTotalAmountNoDiscountUntaxed($quotation->getTotalAmountNoDiscountUntaxed());
$this->setTotalAmountNoDiscount($quotation->getTotalAmountNoDiscount());
$this->setTotalDiscountUntaxed($quotation->getTotalDiscountUntaxed());
$this->setTotalDiscount($quotation->getTotalDiscount());
$this->setTotalTaxAmount($quotation->getTotalTaxAmount());
$this->setTotalAmountUntaxed($quotation->getTotalAmountUntaxed());
$this->setTotalAmount($quotation->getTotalAmount());
}
if (!empty($quotation->getOptions()['note'])) {
$this->addOptions([
'note' => $quotation->getOptions()['note']
]);
}
if (!empty($quotation->getOptions()['customerNote'])) {
$this->addOptions([
'customerNote' => $quotation->getOptions()['customerNote']
]);
}
}
return $this;
}
/**
* @return Order|null
*/
public function getOrder(): ?Order
{
return $this->order;
}
/**
* @return array
* @throws NonUniqueResultException
*/
public function getOrders(): array
{
if ($order = $this->getOrder()) {
return [$order];
} else {
$orders = [];
foreach ($this->getDeliveryNotes() as $deliveryNote) {
$order = $deliveryNote->getOrder();
if ($order && !in_array($order, $orders)) {
$orders[] = $order;
}
}
return $orders;
}
}
/**
* @param Order|null $order
* @return Invoice
* @throws NonUniqueResultException
*/
public function setOrder(?Order $order): self
{
$this->order = $order;
if ($order && !$this->quotation) {
$this->setCustomer($order->getCustomer());
$this->setCommercial($order->getCommercial());
$this->setInvoiceAddress($order->getInvoiceAddress());
$this->setDeliveryAddress($order->getDeliveryAddress());
$this->setInvoiceContact($order->getInvoiceContact());
$this->setDeliveryContact($order->getDeliveryContact());
$this->setCurrency($order->getCurrency());
$this->setContext($order->getContext());
$this->setEstablishment($order->getEstablishment());
$this->setExternalName($order->getExternalName());
if (!$this->getIsDeposit()) {
$this->setTotalAmountNoDiscountUntaxed($order->getTotalAmountNoDiscountUntaxed());
$this->setTotalAmountNoDiscount($order->getTotalAmountNoDiscount());
$this->setTotalDiscountUntaxed($order->getTotalDiscountUntaxed());
$this->setTotalDiscount($order->getTotalDiscount());
$this->setTotalTaxAmount($order->getTotalTaxAmount());
$this->setTotalAmountUntaxed($order->getTotalAmountUntaxed());
$this->setTotalAmount($order->getTotalAmount());
}
if (!empty($order->getOptions()['note'])) {
$this->addOptions([
'note' => $order->getOptions()['note']
]);
}
if (!empty($order->getOptions()['customerNote'])) {
$this->addOptions([
'customerNote' => $order->getOptions()['customerNote']
]);
}
}
return $this;
}
/**
* @return CreditNote|null
*/
public function getCreditNote(): ?CreditNote
{
return $this->credit_note;
}
/**
* @param CreditNote|null $credit_note
* @param bool|null $onlyCreditNote
* @return Invoice
* @throws NonUniqueResultException
*/
public function setCreditNote(?CreditNote $credit_note, ?bool $onlyCreditNote = false): self
{
$this->credit_note = $credit_note;
if ($credit_note && !$onlyCreditNote) {
$this->setCustomer($credit_note->getCustomer());
$this->setCommercial($credit_note->getCommercial());
$this->setInvoiceAddress($credit_note->getInvoiceAddress());
$this->setDeliveryAddress($credit_note->getDeliveryAddress());
$this->setInvoiceContact($credit_note->getInvoiceContact());
$this->setDeliveryContact($credit_note->getDeliveryContact());
$this->setTotalAmountUntaxed($credit_note->getResidual());
$this->setTotalAmount($credit_note->getResidual());
$this->setCurrency($credit_note->getCurrency());
$this->setContext($credit_note->getContext());
$this->setEstablishment($credit_note->getEstablishment());
$this->setExternalName($credit_note->getExternalName());
if (!empty($credit_note->getOptions()['note'])) {
$this->addOptions([
'note' => $credit_note->getOptions()['note']
]);
}
if (!empty($credit_note->getOptions()['customerNote'])) {
$this->addOptions([
'customerNote' => $credit_note->getOptions()['customerNote']
]);
}
}
return $this;
}
/**
* @return Currency|null
*/
public function getCurrency(): ?Currency
{
return $this->currency;
}
/**
* @param Currency $currency
* @return Invoice
*/
public function setCurrency(Currency $currency): self
{
$this->currency = $currency;
return $this->setCurrencyChangeRate($currency->getChangeRate());
}
/**
* @return PaymentTerm|null
*/
public function getPaymentTerm(): ?PaymentTerm
{
return $this->payment_term;
}
/**
* @param PaymentTerm|null $payment_term
* @return Invoice
*/
public function setPaymentTerm(?PaymentTerm $payment_term): self
{
$this->payment_term = $payment_term;
return $this;
}
/**
* @return string|null
*/
public function getCurrencyChangeRate(): ?string
{
return $this->currency_change_rate;
}
/**
* @param string|null $currency_change_rate
* @return Invoice
*/
public function setCurrencyChangeRate(?string $currency_change_rate): self
{
$this->currency_change_rate = $currency_change_rate;
return $this;
}
/**
* @return PaymentMethod|null
* @throws NonUniqueResultException
*/
public function getPaymentMethod(): ?PaymentMethod
{
if ($this->em && $this->payment_method) {
SoftdeleteFilter::disable($this->em, [PaymentMethod::class]);
$payment_method = $this->em->getRepository(PaymentMethod::class)
->createQueryBuilder('pm')
->where('pm.id = :paymentMethodId')
->setParameter('paymentMethodId', $this->payment_method->getId()->toBinary())
->getQuery()
->getOneOrNullResult();
SoftdeleteFilter::enable($this->em, [PaymentMethod::class]);
return $payment_method;
}
return $this->payment_method;
}
/**
* @param PaymentMethod|null $payment_method
*/
public function setPaymentMethod(?PaymentMethod $payment_method): void
{
$this->payment_method = $payment_method;
}
/**
* @return string|null
*/
public function getReference(): ?string
{
return $this->reference;
}
/**
* @param string|null $reference
* @return Invoice
*/
public function setReference(?string $reference): self
{
$this->reference = $reference;
return $this;
}
/**
* @return string|null
*/
public function getInternalName(): ?string
{
return $this->internal_name;
}
/**
* @param string|null $internal_name
* @return Invoice
*/
public function setInternalName(?string $internal_name): self
{
$this->internal_name = $internal_name;
return $this;
}
/**
* @return string|null
*/
public function getExternalName(): ?string
{
return $this->external_name;
}
/**
* @param string|null $external_name
* @return Invoice
*/
public function setExternalName(?string $external_name): self
{
$this->external_name = $external_name;
return $this;
}
/**
* @return bool
*/
public function isReducedVat(): bool
{
return $this->reduced_vat;
}
/**
* @param bool $reduced_vat
* @return Invoice
*/
public function setReducedVat(bool $reduced_vat): self
{
$this->reduced_vat = $reduced_vat;
return $this;
}
/**
* @return string|null
*/
public function getTotalDiscountUntaxed(): ?string
{
return $this->total_discount_untaxed;
}
/**
* @param string|null $total_discount_untaxed
* @return Invoice
*/
public function setTotalDiscountUntaxed(?string $total_discount_untaxed): self
{
$this->total_discount_untaxed = $total_discount_untaxed;
return $this;
}
/**
* @return string|null
*/
public function getTotalDiscount(): ?string
{
return $this->totalDiscount;
}
/**
* @param string|null $totalDiscount
* @return Invoice
*/
public function setTotalDiscount(?string $totalDiscount): Invoice
{
$this->totalDiscount = $totalDiscount;
return $this;
}
/**
* @return string|null
*/
public function getTotalAmountNoDiscountUntaxed(): ?string
{
return $this->total_amount_no_discount_untaxed;
}
/**
* @param string|null $total_amount_no_discount_untaxed
* @return Invoice
*/
public function setTotalAmountNoDiscountUntaxed(?string $total_amount_no_discount_untaxed): self
{
$this->total_amount_no_discount_untaxed = $total_amount_no_discount_untaxed;
return $this;
}
/**
* @return string|null
*/
public function getTotalAmountNoDiscount(): ?string
{
return $this->totalAmountNoDiscount;
}
/**
* @param string|null $totalAmountNoDiscount
* @return Invoice
*/
public function setTotalAmountNoDiscount(?string $totalAmountNoDiscount): Invoice
{
$this->totalAmountNoDiscount = $totalAmountNoDiscount;
return $this;
}
/**
* @return string|null
*/
public function getTotalAmountUntaxed(): ?string
{
return $this->total_amount_untaxed;
}
/**
* @param string|null $total_amount_untaxed
* @return Invoice
*/
public function setTotalAmountUntaxed(?string $total_amount_untaxed): self
{
$this->total_amount_untaxed = $total_amount_untaxed;
return $this;
}
/**
* @return string|null
*/
public function getTotalTaxAmount(): ?string
{
return $this->total_tax_amount;
}
/**
* @param string|null $total_tax_amount
* @return Invoice
*/
public function setTotalTaxAmount(?string $total_tax_amount): self
{
$this->total_tax_amount = $total_tax_amount;
return $this;
}
/**
* @return string|null
*/
public function getTotalAmount(): ?string
{
return $this->total_amount;
}
/**
* @param string|null $total_amount
* @return Invoice
*/
public function setTotalAmount(?string $total_amount): self
{
$this->total_amount = $total_amount;
return $this;
}
/**
* @return string|null
*/
public function getResidual(): ?string
{
return $this->residual;
}
/**
* @param string|null $residual
* @return Invoice
*/
public function setResidual(?string $residual): self
{
$this->residual = $residual;
return $this;
}
/**
* @return DateTimeInterface|null
*/
public function getDueDate(): ?DateTimeInterface
{
return $this->due_date;
}
/**
* @param mixed $due_date
* @return Invoice
*/
public function setDueDate($due_date): self
{
if ($due_date instanceof DateTimeInterface) {
$this->due_date = $due_date;
} elseif ($due_date) {
$this->due_date = new DateTime($due_date);
} else {
$this->due_date = null;
}
return $this;
}
/**
* @return bool
*/
public function getIsDeposit(): bool
{
return $this->is_deposit;
}
/**
* @param bool $is_deposit
* @return Invoice
*/
public function setIsDeposit(bool $is_deposit): self
{
$this->is_deposit = $is_deposit;
return $this;
}
/**
* @return array
*/
public function getOptions(): array
{
return $this->options;
}
/**
* @param array $options
* @return Invoice
*/
public function setOptions(array $options): self
{
$this->options = $options;
return $this;
}
/**
* @param array $options
* @return Invoice
*/
public function addOptions(array $options): self
{
return $this->setOptions(array_merge($this->options, $options));
}
/**
* @return Collection|InvoiceLine[]
*/
public function getLines(): Collection
{
return $this->getFilterLines();
}
/**
* @param InvoiceLine $line
* @return $this
*/
public function addLine(InvoiceLine $line): self
{
if (!$this->lines->contains($line)) {
$this->lines[] = $line;
if ($line->getInvoice() !== $this) {
$line->setInvoice($this);
}
}
return $this;
}
/**
* @param InvoiceLine $line
* @return $this
*/
public function removeLine(InvoiceLine $line): self
{
$this->lines->removeElement($line);
return $this;
}
/**
* @param bool $withGroups
* @param bool $all
* @param bool $withPacks
* @return Collection|InvoiceLine[]
*/
public function getFilterLines(bool $withGroups = true, bool $all = false, bool $withPacks = true): Collection
{
if ($all) {
$lines = $this->lines;
$goodLines = [];
foreach ($lines as $line) {
if (!$line->getParent() && !$line->getPackParent()) {
$goodLines[] = $line;
foreach ($line->getChildrens() as $child) {
$goodLines[] = $child;
foreach ($child->getPackChildrens() as $grandChild) {
$goodLines[] = $grandChild;
}
}
foreach ($line->getPackChildrens() as $child) {
$goodLines[] = $child;
}
}
}
return new ArrayCollection($goodLines);
}
$criteria = Criteria::create();
if ($withGroups) {
$criteria
->where(Criteria::expr()->eq('parent', null))
->andWhere(Criteria::expr()->eq('packParent', null));
} else {
if ($withPacks) {
$criteria->andWhere(Criteria::expr()->eq('is_pack', true));
} else {
$criteria->andWhere(Criteria::expr()->eq('is_pack', false));
}
$criteria->where(Criteria::expr()->eq('is_group', false));
}
return $this->lines->matching($criteria);
}
/**
* @return Collection|DocumentPayment[]
*/
public function getPayments(): Collection
{
return $this->payments;
}
/**
* @return Collection|DeliveryNote[]
*/
public function getDeliveryNotes(): Collection
{
return $this->delivery_notes;
}
/**
* @param DeliveryNote $deliveryNote
* @return $this
*/
public function addDeliveryNote(DeliveryNote $deliveryNote): self
{
if (!$this->delivery_notes->contains($deliveryNote)) {
$this->delivery_notes[] = $deliveryNote;
if ($deliveryNote->getInvoice() !== $this) {
$deliveryNote->setInvoice($this);
}
}
return $this;
}
/**
* @return bool
*/
public function isCancelable(): bool
{
return !$this->getCanceledAt() && $this->getResidual() > 0 && $this->getResidual() == $this->getTotalAmount();
}
/**
* @return Invoice
*/
public function duplicate(): Invoice
{
if ($this->id) {
$clone = clone $this;
$clone->setId();
$clone->setCreatedAt(new DateTime());
$clone->setCreatedBy(null);
$clone->setUpdatedAt(new DateTime());
$clone->setUpdatedBy(null);
$clone->setValidatedAt(null);
$clone->setValidatedBy(null);
$clone->setCanceledAt(null);
$clone->setCanceledBy(null);
$clone->setContext(null);
$clone->setEstablishment(null);
$clone->setQuotation(null);
$clone->setCreditNote(null);
$clone->setReference(null);
$clone->setResidual(null);
$clone->setDueDate(null);
$clone->addOptions([
'invoice_address' => null,
'delivery_address' => null
]);
if (!$this->payment_method) {
$clone->setPaymentMethod(null);
}
if (!$this->payment_term) {
$clone->setPaymentTerm(null);
}
$clone->lines = new ArrayCollection();
foreach ($this->getFilterLines() as $line) {
$clone_line = $line->duplicate($clone);
$clone->addLine($clone_line);
}
return $clone;
}
return $this;
}
/**
* @return mixed
*/
public function getRecurringInvoice()
{
return $this->recurringInvoice;
}
/**
* @param mixed $recurringInvoice
* @return $this
*/
public function setRecurringInvoice($recurringInvoice): self
{
$this->recurringInvoice = $recurringInvoice;
return $this;
}
/**
* @return Establishment|null
*/
public function getEstablishment(): ?Establishment
{
return $this->establishment;
}
/**
* @param Establishment|null $establishment
* @return $this
*/
public function setEstablishment(?Establishment $establishment): self
{
$this->establishment = $establishment;
return $this;
}
/**
* @return Collection
*/
public function getCreditNotes(): Collection
{
return $this->creditNotes;
}
}