src/Entity/Invoice.php line 33

  1. <?php
  2. /*
  3.  *  @author Guerby Duval <info@tranzaksyon.com>
  4.  *  @link https://tranzaksyon.com
  5.  *  @copyright You are not allowed to remove this author "Guerby Duval <info@tranzaksyon.com>", the link "https://tranzaksyon.com" neither this copyright.
  6.  */
  7. namespace App\Entity;
  8. use App\Enums\InvoiceTypeEnum;
  9. use App\Enums\InvoiceTypes;
  10. use App\Interfaces\OwnerCompanyInterface;
  11. use App\Interfaces\SriInvoiceInterface;
  12. use App\Interfaces\SriInvoiceMathInterface;
  13. use App\Repository\InvoiceRepository;
  14. use App\Utils\Sri\SriData;
  15. use Doctrine\Common\Collections\ArrayCollection;
  16. use Doctrine\Common\Collections\Collection;
  17. use Doctrine\DBAL\Types\Types;
  18. use Doctrine\ORM\Mapping as ORM;
  19. use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
  20. use Symfony\Component\Serializer\Annotation\Groups;
  21. use Symfony\Component\Validator\Constraints\Count;
  22. use Symfony\Component\Validator\Constraints\IsFalse;
  23. use Symfony\Component\Validator\Constraints\NotBlank;
  24. use Symfony\Component\Validator\Constraints\Valid;
  25. use Symfony\Component\Validator\Constraints\When;
  26. #[ORM\Entity(repositoryClassInvoiceRepository::class)]
  27. #[ORM\Table]
  28. #[ORM\UniqueConstraint(fields: ['externalId''sriInfo'])]
  29. #[UniqueEntity(['sriInfo''externalId'])]
  30. class Invoice extends InvoiceSuperClass implements SriInvoiceInterfaceSriInvoiceMathInterfaceOwnerCompanyInterface
  31. {
  32.     #[ORM\Id]
  33.     #[ORM\GeneratedValue]
  34.     #[ORM\Column]
  35.     #[Groups(['invoice_out''associated_document_invoice_out'])]
  36.     private ?int $id null;
  37.     #[ORM\Column(typeTypes::DATETIME_MUTABLE)]
  38.     #[NotBlank]
  39.     #[Groups(['invoice_out''invoice_in'])]
  40.     private ?\DateTimeInterface $emitDate null;
  41.     #[ORM\ManyToOne(inversedBy'invoices')]
  42.     #[ORM\JoinColumn(nullablefalse)]
  43.     private ?SriInfo $sriInfo null;
  44.     #[ORM\Column(typeTypes::DECIMALprecision23scale8)]
  45.     #[Groups(['invoice_out'])]
  46.     private ?string $total null;
  47.     #[ORM\Column(typeTypes::DECIMALprecision23scale8)]
  48.     #[Groups(['invoice_out'])]
  49.     private float $totalWithoutTax 0;
  50.     #[ORM\Column(typeTypes::DECIMALprecision20scale6)]
  51.     #[Groups(['invoice_out'])]
  52.     private float $totalDiscount 0;
  53.     #[ORM\Column(typeTypes::DECIMALprecision10scale2)]
  54.     private float $propina 0;
  55.     #[ORM\Column(typeTypes::DECIMALprecision23scale3nullabletrue)]
  56.     #[Groups(['invoice_out''invoice_in'])]
  57.     #[When("this.getFormOfPayment()", [new NotBlank()])]
  58.     private ?string $paidAmount null;
  59.     #[ORM\Column(length50nullabletrue)]
  60.     #[Groups(['invoice_out''invoice_in'])]
  61.     private ?string $formOfPayment null;
  62.     #[ORM\ManyToOne(inversedBy'invoices')]
  63.     #[ORM\JoinColumn(nullabletrueonDelete'SET NULL')]
  64.     private ?User $user null;
  65.     #[ORM\OneToMany(mappedBy'Invoice'targetEntityInvoiceDetail::class, cascade: ['persist'], orphanRemovaltrue)]
  66.     #[Valid(traversetrue)]
  67.     #[Count(min1)]
  68.     #[Groups(['invoice_out''invoice_in''invoice_detail_in'])]
  69.     private Collection $invoiceDetails;
  70.     #[ORM\OneToMany(mappedBy'invoice'targetEntityInvoiceTaxes::class, cascade: ['persist'], orphanRemovaltrue)]
  71.     private Collection $invoiceTaxes;
  72.     #[ORM\ManyToOne(cascade: ['persist'], inversedBy'invoices')]
  73.     #[ORM\JoinColumn(nullabletrue)]
  74.     #[Valid(traversetrue)]
  75.     #[Groups(['invoice_out''invoice_in'])]
  76.     private ?Client $client null;
  77.     #[ORM\OneToOne(mappedBy'invoice'cascade: ['persist''remove'])]
  78.     #[Groups(['invoice_out'])]
  79.     #[Valid(groups: ['invoice-received'])]
  80.     private ?SriDocument $sriDocument null;
  81.     #[ORM\OneToMany(mappedBy'invoice'targetEntityAditionalField::class, cascade: ['persist''remove'], orphanRemovaltrue)]
  82.     #[Groups(['invoice_out''invoice_in'])]
  83.     private Collection $aditionalFields;
  84.     #[ORM\ManyToOne(inversedBy'invoices')]
  85.     #[Groups(['invoice_out''invoice_in'])]
  86.     private ?Sucursal $sucursal null;
  87.     #[ORM\Column(length255nullabletrue)]
  88.     private ?string $externalId null;
  89.     #[Groups(['invoice_in'])]
  90.     private bool $send false;
  91.     #[ORM\Column(nullabletrue)]
  92.     #[Groups(['invoice_out''invoice_in'])]
  93.     /**
  94.      * use for Proforma
  95.      */
  96.     private ?bool $isNote null;
  97.     #[ORM\Column(nullabletrue)]
  98.     private ?bool $isXmlGenerated null;
  99.     #[ORM\Column(nullabletrue)]
  100.     #[Groups(['invoice_out'])]
  101.     private ?bool $isReceived null;
  102.     #[ORM\OneToMany(mappedBy'invoice'targetEntitySriPayment::class, cascade: ['persist''remove'], orphanRemovaltrue)]
  103.     #[Valid(traversetrue)]
  104.     #[Groups(['invoice_out''invoice_in'])]
  105.     private Collection $sriPayments;
  106.     #[ORM\Column(typeTypes::DECIMALprecision23scale8nullabletrue)]
  107.     private ?string $totalIva null;
  108.     #[ORM\Column(nullabletrue)]
  109.     #[Groups(['invoice_out''invoice_in'])]
  110.     // Nota de venta
  111.     private ?bool $isSellNote null;
  112.     #[ORM\Column(length50nullabletrue)]
  113.     private ?InvoiceTypeEnum $invoiceTypeEnum null;
  114.     #[ORM\Column(typeTypes::DECIMALprecision23scale8nullabletrue)]
  115.     private ?string $totalGravada null;
  116.     #[ORM\Column(nullabletrue)]
  117.     #[NotBlank]
  118.     #[Groups(['invoice_out''invoice_in'])]
  119.     private ?int $condicion null;
  120.     #[ORM\OneToOne(inversedBy'invoice'cascade: ['persist''remove'], orphanRemovaltrue)]
  121.     #[Valid(traversetrue)]
  122.     #[Groups(['invoice_out''invoice_in'])]
  123.     private ?PaymentCredit $paymentCredit null;
  124.     #[ORM\Column(nullabletrue)]
  125.     #[NotBlank]
  126.     #[Groups(['invoice_out''invoice_in'])]
  127.     private ?int $transactionType null;
  128.     #[ORM\OneToMany(mappedBy'invoice'targetEntityAssociatedDocument::class)]
  129.     private Collection $associatedDocuments;
  130.     public function __construct()
  131.     {
  132.         parent::__construct();
  133.         $this->invoiceDetails = new ArrayCollection();
  134.         $this->invoiceTaxes = new ArrayCollection();
  135.         $this->aditionalFields = new ArrayCollection();
  136.         $this->sriPayments = new ArrayCollection();
  137.         $this->associatedDocuments = new ArrayCollection();
  138.     }
  139.     public function getId(): ?int
  140.     {
  141.         return $this->id;
  142.     }
  143.     public function getEmitDate(): \DateTimeInterface
  144.     {
  145.         return $this->emitDate;
  146.     }
  147.     public function setEmitDate(?\DateTimeInterface $emitDate): self
  148.     {
  149.         $this->emitDate $emitDate;
  150.         return $this;
  151.     }
  152.     public function getSriInfo(): SriInfo
  153.     {
  154.         return $this->sriInfo;
  155.     }
  156.     public function setSriInfo(?SriInfo $sriInfo): self
  157.     {
  158.         $this->sriInfo $sriInfo;
  159.         return $this;
  160.     }
  161.     public function getTotal(): ?string
  162.     {
  163.         return $this->total;
  164.     }
  165.     public function setTotal(string $total): self
  166.     {
  167.         $this->total $total;
  168.         return $this;
  169.     }
  170.     public function getTotalWithoutTax(): float
  171.     {
  172.         return $this->totalWithoutTax;
  173.     }
  174.     public function setTotalWithoutTax(float $totalWithoutTax): self
  175.     {
  176.         $this->totalWithoutTax $totalWithoutTax;
  177.         return $this;
  178.     }
  179.     public function getTotalDiscount(): float
  180.     {
  181.         return $this->totalDiscount;
  182.     }
  183.     public function setTotalDiscount(float $totalDiscount): self
  184.     {
  185.         $this->totalDiscount $totalDiscount;
  186.         return $this;
  187.     }
  188.     public function getPropina(): float
  189.     {
  190.         return $this->propina;
  191.     }
  192.     public function setPropina(float $propina): self
  193.     {
  194.         $this->propina $propina;
  195.         return $this;
  196.     }
  197.     public function getPaidAmount(): ?string
  198.     {
  199.         return $this->paidAmount;
  200.     }
  201.     public function setPaidAmount(string $paidAmount): self
  202.     {
  203.         $this->paidAmount $paidAmount;
  204.         return $this;
  205.     }
  206.     public function getFormOfPayment(): ?string
  207.     {
  208.         return $this->formOfPayment;
  209.     }
  210.     public function setFormOfPayment(string $formOfPayment): self
  211.     {
  212.         $this->formOfPayment $formOfPayment;
  213.         return $this;
  214.     }
  215.     public function getUser(): ?User
  216.     {
  217.         return $this->user;
  218.     }
  219.     public function setUser(?User $user): self
  220.     {
  221.         $this->user $user;
  222.         return $this;
  223.     }
  224.     /**
  225.      * @return Collection<int, InvoiceDetail>
  226.      */
  227.     public function getInvoiceDetails(): Collection
  228.     {
  229.         return $this->invoiceDetails;
  230.     }
  231.     public function addInvoiceDetail(InvoiceDetail $invoiceDetail): self
  232.     {
  233.         if (!$this->invoiceDetails->contains($invoiceDetail)) {
  234.             $this->invoiceDetails->add($invoiceDetail);
  235.             $invoiceDetail->setInvoice($this);
  236.         }
  237.         return $this;
  238.     }
  239.     public function removeInvoiceDetail(InvoiceDetail $invoiceDetail): self
  240.     {
  241.         if ($this->invoiceDetails->removeElement($invoiceDetail)) {
  242.             // set the owning side to null (unless already changed)
  243.             if ($invoiceDetail->getInvoice() === $this) {
  244.                 $invoiceDetail->setInvoice(null);
  245.             }
  246.         }
  247.         return $this;
  248.     }
  249.     /**
  250.      * @return Collection<int, InvoiceTaxes>
  251.      */
  252.     public function getInvoiceTaxes(): Collection
  253.     {
  254.         return $this->invoiceTaxes;
  255.     }
  256.     public function addInvoiceTax(InvoiceTaxes $invoiceTax): self
  257.     {
  258.         if (!$this->invoiceTaxes->contains($invoiceTax)) {
  259.             $this->invoiceTaxes->add($invoiceTax);
  260.             $invoiceTax->setInvoice($this);
  261.         }
  262.         return $this;
  263.     }
  264.     public function removeInvoiceTax(InvoiceTaxes $invoiceTax): self
  265.     {
  266.         if ($this->invoiceTaxes->removeElement($invoiceTax)) {
  267.             // set the owning side to null (unless already changed)
  268.             if ($invoiceTax->getInvoice() === $this) {
  269.                 $invoiceTax->setInvoice(null);
  270.             }
  271.         }
  272.         return $this;
  273.     }
  274.     public function getClient(): ?Client
  275.     {
  276.         return $this->client;
  277.     }
  278.     public function setClient(?Client $client): self
  279.     {
  280.         $this->client $client;
  281.         return $this;
  282.     }
  283.     public function getSriDocument(): ?SriDocument
  284.     {
  285.         return $this->sriDocument;
  286.     }
  287.     public function setSriDocument(?SriDocument $sriDocument): self
  288.     {
  289.         // unset the owning side of the relation if necessary
  290.         if ($sriDocument === null && $this->sriDocument !== null) {
  291.             $this->sriDocument->setInvoice(null);
  292.         }
  293.         // set the owning side of the relation if necessary
  294.         if ($sriDocument !== null && $sriDocument->getInvoice() !== $this) {
  295.             $sriDocument->setInvoice($this);
  296.         }
  297.         $this->sriDocument $sriDocument;
  298.         return $this;
  299.     }
  300.     function getInvoiceType(): InvoiceTypes
  301.     {
  302.         return InvoiceTypes::factura;
  303.     }
  304.     /**
  305.      * @return Collection<int, AditionalField>
  306.      */
  307.     public function getAditionalFields(): Collection
  308.     {
  309.         return $this->aditionalFields;
  310.     }
  311.     public function addAditionalField(AditionalField $aditionalField): self
  312.     {
  313.         if (!$this->aditionalFields->contains($aditionalField)) {
  314.             $this->aditionalFields->add($aditionalField);
  315.             $aditionalField->setInvoice($this);
  316.         }
  317.         return $this;
  318.     }
  319.     public function removeAditionalField(AditionalField $aditionalField): self
  320.     {
  321.         if ($this->aditionalFields->removeElement($aditionalField)) {
  322.             // set the owning side to null (unless already changed)
  323.             if ($aditionalField->getInvoice() === $this) {
  324.                 $aditionalField->setInvoice(null);
  325.             }
  326.         }
  327.         return $this;
  328.     }
  329.     public function getSucursal(): ?Sucursal
  330.     {
  331.         return $this->sucursal;
  332.     }
  333.     public function setSucursal(?Sucursal $sucursal): self
  334.     {
  335.         $this->sucursal $sucursal;
  336.         return $this;
  337.     }
  338.     public function getExternalId(): ?string
  339.     {
  340.         return $this->externalId;
  341.     }
  342.     public function setExternalId(?string $externalId): self
  343.     {
  344.         $this->externalId $externalId;
  345.         return $this;
  346.     }
  347.     public function setSend(bool $send): void
  348.     {
  349.         $this->send $send;
  350.     }
  351.     public function isSend(): bool
  352.     {
  353.         return $this->send;
  354.     }
  355.     public function isIsNote(): ?bool
  356.     {
  357.         return $this->getInvoiceTypeEnum() === InvoiceTypeEnum::note ?? $this->isNote;
  358.     }
  359.     public function setIsNote(?bool $isNote): self
  360.     {
  361.         $this->isNote $isNote;
  362.         return $this;
  363.     }
  364.     public function isIsXmlGenerated(): ?bool
  365.     {
  366.         return $this->isXmlGenerated;
  367.     }
  368.     public function setIsXmlGenerated(?bool $isXmlGenerated): self
  369.     {
  370.         $this->isXmlGenerated $isXmlGenerated;
  371.         return $this;
  372.     }
  373.     #[Groups(['invoice_out'])]
  374.     public function getCanSendToWhatsapp(): bool
  375.     {
  376.         return $this->sriInfo->getWhatsappConfiguration() and $this->sriInfo->getWhatsappConfiguration()->getApiKey() and $this->client?->getPhone();
  377.     }
  378.     public function isIsReceived(): ?bool
  379.     {
  380.         return $this->isReceived;
  381.     }
  382.     public function setIsReceived(?bool $isReceived): static
  383.     {
  384.         $this->isReceived $isReceived;
  385.         return $this;
  386.     }
  387.     /**
  388.      * @return Collection<int, SriPayment>
  389.      */
  390.     public function getSriPayments(): Collection
  391.     {
  392.         return $this->sriPayments;
  393.     }
  394.     public function addSriPayment(SriPayment $sriPayment): static
  395.     {
  396.         if (!$this->sriPayments->contains($sriPayment)) {
  397.             $this->sriPayments->add($sriPayment);
  398.             $sriPayment->setInvoice($this);
  399.         }
  400.         return $this;
  401.     }
  402.     public function removeSriPayment(SriPayment $sriPayment): static
  403.     {
  404.         if ($this->sriPayments->removeElement($sriPayment)) {
  405.             // set the owning side to null (unless already changed)
  406.             if ($sriPayment->getInvoice() === $this) {
  407.                 $sriPayment->setInvoice(null);
  408.             }
  409.         }
  410.         return $this;
  411.     }
  412.     public function getTotalIva(): ?string
  413.     {
  414.         return $this->totalIva;
  415.     }
  416.     public function setTotalIva(?string $totalIva): static
  417.     {
  418.         $this->totalIva $totalIva;
  419.         return $this;
  420.     }
  421.     public function getTotalGravada(): ?string
  422.     {
  423.         return $this->totalGravada;
  424.     }
  425.     public function setTotalGravada(?string $totalGravada): static
  426.     {
  427.         $this->totalGravada $totalGravada;
  428.         return $this;
  429.     }
  430.     public function isIsSellNote(): ?bool
  431.     {
  432.         return $this->isSellNote;
  433.     }
  434.     public function setIsSellNote(?bool $isSellNote): static
  435.     {
  436.         $this->isSellNote $isSellNote;
  437.         return $this;
  438.     }
  439.     public function setInvoiceTypeEnum(?InvoiceTypeEnum $invoiceType): static
  440.     {
  441.         $this->invoiceTypeEnum $invoiceType;
  442.         return $this;
  443.     }
  444.     public function getInvoiceTypeEnum(): ?InvoiceTypeEnum
  445.     {
  446.         return $this->invoiceTypeEnum;
  447.     }
  448.     public function getCondicion(): ?int
  449.     {
  450.         return $this->condicion;
  451.     }
  452.     public function setCondicion(?int $condicion): static
  453.     {
  454.         $this->condicion $condicion;
  455.         return $this;
  456.     }
  457.     public function getPaymentCredit(): ?PaymentCredit
  458.     {
  459.         return $this->paymentCredit;
  460.     }
  461.     public function setPaymentCredit(?PaymentCredit $paymentCredit): static
  462.     {
  463.         $this->paymentCredit $paymentCredit;
  464.         $this->paymentCredit->setInvoice($this);
  465.         return $this;
  466.     }
  467.     #[IsFalse(message'Falta llenar un pago.')]
  468.     public function isPaymentInvalid()
  469.     {
  470.         return $this->getSriPayments()->isEmpty() && $this->getPaymentCredit() === null;
  471.     }
  472.     public function getCondicionDescripcion()
  473.     {
  474.         if (empty($this->condicion)) {
  475.             return '';
  476.         }
  477.         return array_flip(SriData::condicionPagos())[$this->condicion];
  478.     }
  479.     public function getTransactionType(): ?int
  480.     {
  481.         return $this->transactionType;
  482.     }
  483.     public function setTransactionType(?int $transactionType): static
  484.     {
  485.         $this->transactionType $transactionType;
  486.         return $this;
  487.     }
  488.     /**
  489.      * @return Collection<int, AssociatedDocument>
  490.      */
  491.     public function getAssociatedDocuments(): Collection
  492.     {
  493.         return $this->associatedDocuments;
  494.     }
  495.     public function addAssociatedDocument(AssociatedDocument $associatedDocument): static
  496.     {
  497.         if (!$this->associatedDocuments->contains($associatedDocument)) {
  498.             $this->associatedDocuments->add($associatedDocument);
  499.             $associatedDocument->setInvoice($this);
  500.         }
  501.         return $this;
  502.     }
  503.     public function removeAssociatedDocument(AssociatedDocument $associatedDocument): static
  504.     {
  505.         if ($this->associatedDocuments->removeElement($associatedDocument)) {
  506.             // set the owning side to null (unless already changed)
  507.             if ($associatedDocument->getInvoice() === $this) {
  508.                 $associatedDocument->setInvoice(null);
  509.             }
  510.         }
  511.         return $this;
  512.     }
  513. }