Skip to content
Snippets Groups Projects
Commit c64b3c22 authored by Nicolas Martinelli's avatar Nicolas Martinelli
Browse files

[FIX] sale: group SO with section in single invoice


- Create SO 1:
  - Section A, sequence 10
  - Product A, sequence 11
- Create SO 2:
  - Section B, sequence 10
  - Product B, sequence 11
- Select both SO
- Create the invoice

The resulting invoice lines are organized as follow:
- Section A, sequence 10
- Section B, sequence 10
- Product A, sequence 11
- Product B, sequence 11

This is obviously not expected as it messes up the organization of the
lines.

This happens because the sequences of the SO lines are kept at invoice
creation, while it is necessary to resequence the invoice lines to keep
them organized.

To do so, we loop on the invoice lines before their creation in order to
assign a sequence corresponding to the order in which they have been
added in the list. Since the invoice lines are added one SO after the
other, this allows us to keep the appropriate ordering.

Note that we only resequence if there are less invoices to create than
SO, meaning that several SO have been merged. Indeed, in case a single
invoice is created from a single SO, there is no need to resequence.

This assumption is not completely true: if not all selected SO were
invoiceable, the number of invoices created is also smaller than the
number of SO. However, resequencing should be safe so in the worst case
we might resequence an invoice while it was not necessary.

We also leave the possibility for third-party addons to alter the
resequencing thanks to the `_get_invoice_line_sequence` method.

opw-2363443

closes odoo/odoo#60785

X-original-commit: 97c2f5ea51a669830b3e6d6d2d7d857742a41481
Signed-off-by: default avatarNicolas Martinelli (nim) <nim@odoo.com>
parent 86423e2d
Branches
Tags
No related merge requests found
......@@ -714,9 +714,37 @@ Reason(s) of this behavior could be:
invoice_vals_list = new_invoice_vals_list
# 3) Create invoices.
# As part of the invoice creation, we make sure the sequence of multiple SO do not interfere
# in a single invoice. Example:
# SO 1:
# - Section A (sequence: 10)
# - Product A (sequence: 11)
# SO 2:
# - Section B (sequence: 10)
# - Product B (sequence: 11)
#
# If SO 1 & 2 are grouped in the same invoice, the result will be:
# - Section A (sequence: 10)
# - Section B (sequence: 10)
# - Product A (sequence: 11)
# - Product B (sequence: 11)
#
# Resequencing should be safe, however we resequence only if there are less invoices than
# orders, meaning a grouping might have been done. This could also mean that only a part
# of the selected SO are invoiceable, but resequencing in this case shouldn't be an issue.
if len(invoice_vals_list) < len(self):
SaleOrderLine = self.env['sale.order.line']
for invoice in invoice_vals_list:
sequence = 1
for line in invoice['invoice_line_ids']:
line[2]['sequence'] = SaleOrderLine._get_invoice_line_sequence(new=sequence, old=line[2]['sequence'])
sequence += 1
# Manage the creation of invoices in sudo because a salesperson must be able to generate an invoice from a
# sale order without "billing" access rights. However, he should not be able to create an invoice from scratch.
moves = self.env['account.move'].sudo().with_context(default_move_type='out_invoice').create(invoice_vals_list)
# 4) Some moves might actually be refunds: convert them if the total amount is negative
# We do this after the moves have been created since we need taxes, etc. to know if the total
# is actually negative or not
......@@ -1529,6 +1557,18 @@ class SaleOrderLine(models.Model):
line.untaxed_amount_to_invoice = amount_to_invoice
def _get_invoice_line_sequence(self, new=0, old=0):
"""
Method intended to be overridden in third-party module if we want to prevent the resequencing
of invoice lines.
:param int new: the new line sequence
:param int old: the old line sequence
:return: the sequence of the SO line, by default the new one.
"""
return new or old
def _prepare_invoice_line(self, **optional_values):
"""
Prepare the dict of values to create the new invoice line for a sales order line.
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment