Get invoice sequence by credit_note or invoice

When an invoice is credit note or invoice?

Actually the sequence for credit note is in case all lines, the amount is <= 0 .

But when the invoice have some lines the amount is <= 0 but other lines the amount is >=0, and the untaxed amount < 0, is or not is credit invoice?

Example:

>>> Invoice = pool.get('account.invoice')
>>> invoice = Invoice(325336)
>>> invoice.untaxed_amount
Decimal('-10.00')

At the moment, total_amount is < 0, for me, a credit note.

But, this invoice have two lines, one is > 0 and other < 0.

>>> for line in invoice.lines: line.quantity, line.amount
... 
(1.0, Decimal('10.00'))
(-1.0, Decimal('-20.00'))
>>> (all(l.amount <= 0 for l in invoice.lines if l.product) and invoice.total_amount < 0)
False

When get next number method, return “invoice” sequence and not “credit note” sequence. Is true?

1 Like

With the current implementation, and invoice is only considered as credit note when all line that have products have negative quantities.

If you have a positive product line and a negative product line, then it is considered as invoice no mather if the total amount of the invoice is negative.

A credit note could be positive or negative as an invoice, depend on the origin and the context.

But only consider a credit note when all lines are negative, it’s not correct. At least in Spain.

If we don’t want to control all the possible cases and situations to decid if is a credit note or not (I agree because will be difficult), for me the correct control will be check the total untaxed amount < 0 to decide if an invoice is a credit note or not.

It will be great if you can share the link where the normative defines the cases, so we can decide which can control. If all cases are supported by Tryton we should generate the right credit note, at least for Spain.

Please provide the complete set of official rules.

In accounting, distinguishing between an invoice and a credit note can sometimes be complex, especially when dealing with mixed line amounts. Based on your example, here’s a detailed explanation:
Understanding Credit Notes and Invoices:

Credit Note Definition:
A credit note is typically issued to correct an invoice error or to provide a reduction in the amount owed by the customer. It usually has a negative total amount.

Invoice with Mixed Line Amounts:
When an [invoice](https://sleekbill.in) contains both positive and negative line amounts, the determination of whether it is an invoice or a credit note depends on the total amount.

Example Analysis:

Invoice Total Amount: Decimal('-10.00')
    This indicates that overall, the customer is owed money back.

Line Items:
    Line 1: (1.0, Decimal('10.00')) - Positive amount.
    Line 2: (-1.0, Decimal('-20.00')) - Negative amount.

Given the total amount (-10.00), the document appears to be a credit note because it signifies a net refund to the customer.

However, the condition (all(l.amount <= 0 for l in invoice.lines if l.product) and invoice.total_amount < 0) evaluates to False because not all line amounts are <= 0.
Proper Classification:

Credit Note:
    If the untaxed_amount and total_amount are both < 0, the document should be considered a credit note, regardless of mixed line amounts.
Invoice:
    If the untaxed_amount and total_amount are both >= 0, it is an invoice.

Handling Mixed Amounts:

When an invoice contains mixed line amounts (positive and negative), but the total amount is negative, it should logically be classified as a credit note due to the overall refund to the customer.
Next Number Method:

The method for determining the next number sequence should account for the total amount:

If invoice.total_amount < 0, the next number should follow the "credit note" sequence.
If invoice.total_amount >= 0, the next number should follow the "invoice" sequence.

Final Determination:

Based on the provided example, your invoice with an untaxed_amount of -10.00 and mixed line amounts should be classified as a credit note, as the total amount is negative.

python

Invoice = pool.get(‘account.invoice’)
invoice = Invoice(325336)
invoice.untaxed_amount
Decimal(‘-10.00’)
for line in invoice.lines: line.quantity, line.amount


(1.0, Decimal(‘10.00’))
(-1.0, Decimal(‘-20.00’))
(all(l.amount <= 0 for l in invoice.lines if l.product) and invoice.total_amount < 0)
False

Correct Classification

invoice.is_credit_note = invoice.total_amount < 0

This adjustment ensures accurate classification and sequence assignment based on the total amount.

In Spain, the rules for credit notes (or rectification invoices for the Spain Tax Agency) are more complex than negative or positive amounts.

The main official rules are in the article 15 of the Royal Decree 1619/2012 (sorry, it seems to be only in Spanish).
The rules of the Royal Decree are included into the Rectification invoices section of Practical manual VAT 2024.

Examples of the 2 types of rectification invoices (by replacement or be differences) can be found on questions 2.8 and 2.9 of the VAT Record Books - Frequently Asked Questions.

Maybe a simple update for Spain could be to add a field to the invoice to mark as “Rectification invoice” and force the use of the credit note sequence when the invoice is marked as rectification.

I see nothing that say the current behavior is wrong.

The problem may be in the terms used.
The credit note is normally applied to a refund.
However, Spanish rectification invoices (which require a different invoice series) only match the concept of a credit note in a specific situation. In the rest of the situations they do not imply any refund, in fact they may imply an increase in the amount of the original invoice.

I see nowhere in the provided documentation that they need to have a different numbering?

It’s true. The use of invoice series is specified in a previous section: General content of ordinary invoices

So for me Spain does not difference the invoice and credit note, but use something else much more complicated.

So I think for Spain accounting, the same sequence should be used for invoice and credit note and new criteria should be added to the pattern matching to find the invoice sequence of the fiscal year to use.
Also the es_vat_book_type should be reworked and probably it should be the field stored on the invoice that is used to select the sequence (so the opposite of what is done now).

Yes, that’s right.

An approach to solve this (partially) was made on the TrytonSpain module Account Invoice Multisequence, which extends the fiscal year to allow the posibility to add journals with invoicing sequences. On validating/posting, the journal assigned to the invoice is used to find the invoice sequence to use.
However, this approach still requires an extension that allows determining whether the invoice is corrective or not.

Setting different sequences per journal should not be so complex than what does this module.
But I do not think the journal is the proper way because the rules are not based on that, it is just an hack.

But from the definitions, I do not see how it is possible to automate the selection of the kind of invoice without having to create a very complex ruling system.
If someone comes with a proposal that is not too complex, it could be included in standard.

The module does not sets different sequences per journal.
Following the solution of this module the proposal will be to add the field journal (a M2O over account.journal) to the model account.fiscalyear.invoice_sequence and include the journal of the invoice in the pattern when searching for the invoice sequence on fiscalyear.invoice_sequences.

The field should be unique by fiscal year and period and restricted to revenue or expense type.

As I said the rules are not by journal according to:

So this can not be the standard solution.

Realy, this proposal is not to automate the selection of the invoice sequence but to allow the user to manually select the type of sequence to use.
The current schema used in Tryton for the invoice numbering only allows to match distinct sequences by fiscal year and period (as well as for regular invoice or credit note). But in Spain (I don’t know if in other countries) the use of several invoice sequences (for regular invoices) is relatively common.

Tryton is about automation and avoid error.
Without proper automatic numbering of invoice, you can not for example have a web shop.

I do not care about common. I care about regulation.

So, nobody can have a web shop because Tryton does not automate an specific numbering of invoice based on the web shop.

I never said that. Almost all countries has simple invoice numbering rules. So Tryton work without any issue.

It is just in Spain where there are special rules.