From ef69c915cbdfcab9b6e35b8f25b6576a82d8731d Mon Sep 17 00:00:00 2001 From: Andreas Osim Date: Mon, 27 Apr 2020 09:37:22 +0200 Subject: [PATCH] tz_dealer_discount - fix problem with sorting in account_invoice --- ext/custom-addons/dp_custom/models/sale.py | 14 ++++++++------ .../models/account_invoice.py | 18 +++++++++++++++++- .../tz_dealer_discount/models/invoice.py | 7 +++++-- .../tz_dealer_discount/models/sale.py | 7 +++++-- .../tz_dealer_discount/views/invoice_view.xml | 2 ++ 5 files changed, 37 insertions(+), 11 deletions(-) diff --git a/ext/custom-addons/dp_custom/models/sale.py b/ext/custom-addons/dp_custom/models/sale.py index 7bf1f195..055b6b57 100644 --- a/ext/custom-addons/dp_custom/models/sale.py +++ b/ext/custom-addons/dp_custom/models/sale.py @@ -530,12 +530,14 @@ class SaleOrder(models.Model): precision = self.env['decimal.precision'].precision_get('Product Unit of Measure') invoices = {} references = {} - for order in self: + seq = 0 + for order in self.sorted(key=lambda o: o.name): collective_bill = order.partner_id.collective_bill and "x" or order.id group_key = order.id if grouped else (collective_bill, order.partner_invoice_id.id, order.currency_id.id) - for line in order.order_line.sorted(key=lambda l: l.qty_to_invoice < 0): + for line in order.order_line.sorted(key=lambda l: (l.sequence, l.qty_to_invoice < 0)): if float_is_zero(line.qty_to_invoice, precision_digits=precision): continue + seq += 1 if group_key not in invoices: inv_data = order._prepare_invoice() invoice = inv_obj.create(inv_data) @@ -551,9 +553,9 @@ class SaleOrder(models.Model): vals['name'] = "siehe Detail" invoices[group_key].write(vals) if line.qty_to_invoice > 0: - line.invoice_line_create(invoices[group_key].id, line.qty_to_invoice) + line.invoice_line_create(invoices[group_key].id, line.qty_to_invoice,seq) elif line.qty_to_invoice < 0 and final: - line.invoice_line_create(invoices[group_key].id, line.qty_to_invoice) + line.invoice_line_create(invoices[group_key].id, line.qty_to_invoice,seq) if references.get(invoices.get(group_key)): if order not in references[invoices[group_key]]: @@ -828,7 +830,7 @@ class SaleOrderLine(models.Model): return res @api.multi - def invoice_line_create(self, invoice_id, qty): + def invoice_line_create(self, invoice_id, qty, seq): """ Overwritten and added a logic to create an extra line for discounts from a retailer :param invoice_id: @@ -840,7 +842,7 @@ class SaleOrderLine(models.Model): for line in self: if not float_is_zero(qty, precision_digits=precision): vals = line._prepare_invoice_line(qty=qty) - vals.update({'invoice_id': invoice_id, 'sale_line_ids': [(6, 0, [line.id])]}) + vals.update({'invoice_id': invoice_id, 'sequence' : seq, 'sale_line_ids': [(6, 0, [line.id])]}) invoice_lines |= self.env['account.invoice.line'].create(vals) if line.order_id.partner_invoice_id.is_retailer: discount = line.calc_discount() diff --git a/ext/custom-addons/dp_reports_account/models/account_invoice.py b/ext/custom-addons/dp_reports_account/models/account_invoice.py index c6a557fc..9485637c 100644 --- a/ext/custom-addons/dp_reports_account/models/account_invoice.py +++ b/ext/custom-addons/dp_reports_account/models/account_invoice.py @@ -1,6 +1,7 @@ # Copyright 2018-Today datenpol gmbh () # License OPL-1 or later (https://www.odoo.com/documentation/user/11.0/legal/licenses/licenses.html#licenses). +from itertools import groupby from odoo import api, fields, models @@ -9,7 +10,22 @@ class AccountInvoice(models.Model): @api.multi def order_lines_layouted(self): - res = super(AccountInvoice, self).order_lines_layouted() +# res = super(AccountInvoice, self).order_lines_layouted() + + self.ensure_one() + res = [[]] + for category, lines in groupby(self.invoice_line_ids.sorted(key=lambda s: s.layout_category_id.name), lambda l: l.layout_category_id): + # If last added category induced a pagebreak, this one will be on a new page + if res[-1] and res[-1][-1]['pagebreak']: + res.append([]) + # Append category to current report page + res[-1].append({ + 'name': category and category.name or 'Uncategorized', + 'subtotal': category and category.subtotal, + 'pagebreak': category and category.pagebreak, + 'lines': list(lines) + }) + uncategorized = False # Rechnungszeilen ohne Kategorie löschen und an den Anfang stellen for idx1, page in enumerate(res): diff --git a/ext/custom-addons/tz_dealer_discount/models/invoice.py b/ext/custom-addons/tz_dealer_discount/models/invoice.py index 313681dc..be4ff272 100644 --- a/ext/custom-addons/tz_dealer_discount/models/invoice.py +++ b/ext/custom-addons/tz_dealer_discount/models/invoice.py @@ -64,7 +64,7 @@ class Invoice(models.Model): discount_pos = "" n=0 nd=0 - for line in invoice.invoice_line_ids: + for line in invoice.invoice_line_ids.sorted(key=lambda l: l.sequence): n += 1 if not line.product_id.product_tmpl_id.material_type_id.no_dealer_discount: nd += 1 @@ -86,6 +86,9 @@ class Invoice(models.Model): layout_category_id = self.env['sale.layout_category'].search([('name', '=', discount_product_id.manufacturing_number)]) if amount > 0: + last_inv_line = self.env['account.invoice.line'].search([('invoice_id', '=', invoice.id)], order='sequence desc', + limit=1) + last_sequence = last_inv_line.sequence + 1 if last_inv_line else 99999 # Create the Invoice line InvoiceLine.create({ 'name': discount_text, @@ -98,7 +101,7 @@ class Invoice(models.Model): 'invoice_id': invoice.id, 'invoice_line_tax_ids': [(6, 0, taxes_ids)], 'layout_category_id':layout_category_id.id, - 'sequence': 99999, + 'sequence': last_sequence, }) super()._onchange_invoice_line_ids() diff --git a/ext/custom-addons/tz_dealer_discount/models/sale.py b/ext/custom-addons/tz_dealer_discount/models/sale.py index b3d66562..b3e273be 100644 --- a/ext/custom-addons/tz_dealer_discount/models/sale.py +++ b/ext/custom-addons/tz_dealer_discount/models/sale.py @@ -56,7 +56,7 @@ class SaleOrder(models.Model): discount_pos = "" n=0 nd=0 - for line in order.order_line: + for line in order.order_line.sorted(key=lambda l: l.sequence): n += 1 if not line.product_id.product_tmpl_id.material_type_id.no_dealer_discount: nd += 1 @@ -70,6 +70,9 @@ class SaleOrder(models.Model): discount_text = discount_text + discount_pos.rstrip(sep) if amount > 0: + last_so_line = self.env['sale.order.line'].search([('order_id', '=', order.id)], order='sequence desc', + limit=1) + last_sequence = last_so_line.sequence + 1 if last_so_line else 99999 # Create the Sale line Line.create({ 'name': discount_text, @@ -79,7 +82,7 @@ class SaleOrder(models.Model): 'product_uom': discount_product_id.uom_id.id, 'product_id': discount_product_id.id, 'order_id': order.id, - 'sequence': 99999, + 'sequence': last_sequence, }) self.dealer_discount_product = '' diff --git a/ext/custom-addons/tz_dealer_discount/views/invoice_view.xml b/ext/custom-addons/tz_dealer_discount/views/invoice_view.xml index 072484e7..c692bab2 100644 --- a/ext/custom-addons/tz_dealer_discount/views/invoice_view.xml +++ b/ext/custom-addons/tz_dealer_discount/views/invoice_view.xml @@ -23,9 +23,11 @@ attrs="{'invisible': ['|',('state','not in', ['draft','sent']),('dealer_discount','=', False)]}" style="margin-right:0px;"/> +