Score:0

How can I have my own custom invoice number pattern?

zw flag

Hi Im trying to make different pattern for the invoice in Drupal 9. I have products with two kind of price one is for particular and the other one is for profesional.

i would like to have two pattern number for the invoice, for particular 22-ES-00001 and for profesional 22-ES-P-00001. I already create the two kind of pattern number and i have two kind of invoice type and also asign each pattern number to the invoice type.

I just need to associate the invoice type to the role. But dont know the way of how to do it any one have any idea about how can i do it?

Thanks in advance.

Score:1
ru flag

The invoice number is generated from Invoice::presave();, using the following code:

if ($this->getState()->getId() != 'draft' && empty($this->getInvoiceNumber())) {
  /** @var \Drupal\commerce_number_pattern\Entity\NumberPatternInterface $number_pattern */
  $number_pattern = $invoice_type->getNumberPattern();
  if ($number_pattern) {
    $invoice_number = $number_pattern->getPlugin()->generate($this);
    $this->setInvoiceNumber($invoice_number);
  }
}

What I'd do if I were you, in order for this code to be bypassed is that I'd set a "fake" temporary invoice number on invoice creation (by reacting to the InvoiceEvents::INVOICE_CREATE event.

Then from the same event subscriber reacting to InvoiceEvents::INVOICE_PRESAVE I'd add my own logic to invoice the right number pattern plugin.

Setting an invoice number on invoice creation would help bypassing the comemrce_invoice code, and you could have your own logic running on invoice presave.

Here's the documentation around event subscribers in case you need help with that.

leymannx avatar
ne flag
They usually need to whole event subscriber sampled.
Nelkan avatar
zw flag
Thanks for the answer i will try to make it.
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.