From 350925b2bf22289505d4e42567877256a490ed64 Mon Sep 17 00:00:00 2001 From: luca-vercelli Date: Tue, 10 Oct 2017 15:48:05 +0200 Subject: [PATCH 1/5] override write() --- .../models/account_invoice.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/account_invoice_line_pricelist/models/account_invoice.py b/account_invoice_line_pricelist/models/account_invoice.py index ee8e9baa..92d1a4fc 100644 --- a/account_invoice_line_pricelist/models/account_invoice.py +++ b/account_invoice_line_pricelist/models/account_invoice.py @@ -22,3 +22,21 @@ def _onchange_partner_id(self): if self.partner_id: self.pricelist_id = self.partner_id.property_product_pricelist return res + + @api.multi + def write(self, vals): + """ + Update pricelist_id whenever missing, before real write() + Remember: do *not* update rec.pricelist_id directly! that triggers write() + """ + for rec in self: + if ('type' in vals and vals['type'] in ['out_invoice', 'out_refund']) or (rec.type in ['out_invoice', 'out_refund']): + if 'partner_id' in vals: + #INSERT/UPDATE partner + if not 'pricelist_id' in vals: + vals['pricelist_id'] = vals['partner_id'].property_product_pricelist + else: + #UPDATE record + if ((not rec.pricelist_id) or(not rec.pricelist_id.id)) and (rec.partner_id) and (rec.partner_id.property_product_pricelist) and (rec.partner_id.property_product_pricelist.id): + vals['pricelist_id'] = rec.partner_id.property_product_pricelist.id + return super(AccountInvoice, self).write(vals) From df208b0dc361b6ef2a5b1ea8b3f0b1a03cc7bf55 Mon Sep 17 00:00:00 2001 From: luca-vercelli Date: Tue, 10 Oct 2017 15:49:56 +0200 Subject: [PATCH 2/5] override write() and create() --- .../models/account_invoice_line.py | 52 +++++++++++++++++-- 1 file changed, 48 insertions(+), 4 deletions(-) diff --git a/account_invoice_line_pricelist/models/account_invoice_line.py b/account_invoice_line_pricelist/models/account_invoice_line.py index 38d0d7c4..f3c2d5c6 100644 --- a/account_invoice_line_pricelist/models/account_invoice_line.py +++ b/account_invoice_line_pricelist/models/account_invoice_line.py @@ -12,17 +12,28 @@ class AccountInvoiceLine(models.Model): @api.onchange('product_id') def _onchange_product_id(self): + res = super(AccountInvoiceLine, self)._onchange_product_id() + price_unit = self._common_recalc_price() + if price_unit is not None and price_unit != 0: + self.price_unit = price_unit + + return res + + def _common_recalc_price(self): + """ + Re/calculate and return given price + """ if self.invoice_id.type not in ['out_invoice', 'out_refund']: - return res + return None partner = self.invoice_id.partner_id pricelist = self.invoice_id.pricelist_id product = self.product_id if not partner or not product or not pricelist: - return res + return None inv_date = self.invoice_id.date_invoice or date.today().strftime(DT) product = product.with_context( @@ -34,9 +45,42 @@ def _onchange_product_id(self): uom=self.uom_id.id ) - self.price_unit = self.env['account.tax']._fix_tax_included_price( + return self.env['account.tax']._fix_tax_included_price( product.price, product.taxes_id, self.invoice_line_tax_ids ) - return res + + def create(self, vals): + """ + Update price if missing, after real create() + """ + line = super(AccountInvoiceLine, self).create(vals) + if line.invoice_id: + invoice = line.invoice_id + if ((not line.price_unit) or price_unit == 0) and (invoice.pricelist_id and invoice.pricelist_id.id): + price_unit = line._common_recalc_price() + if price_unit is not None and price_unit != 0: + line.price_unit = price_unit #triggers write() + return line + + @api.multi + def write(self, vals): + """ + Update price whenever missing, before real write() + Remember: do *not* update rec.price_unit directly! that triggers write() + """ + for rec in self: + if (rec.invoice_id.pricelist_id and rec.invoice_id.pricelist_id.id): + if 'price_unit' in vals and vals['price_unit'] is None or vals['price_unit'] == 0: + #INSERT/UPDATE price_unit + price_unit = rec._common_recalc_price() + if price_unit is not None: + vals['price_unit'] = price_unit + else: + #UPDATE record + if rec.price_unit is None or rec.price_unit == 0: + price_unit = rec._common_recalc_price() + if price_unit is not None and price_unit != 0: + vals['price_unit'] = price_unit + return super(AccountInvoiceLine, self).write(vals) From 34829d4f26238077c7d9608f740b705d1885cf69 Mon Sep 17 00:00:00 2001 From: luca-vercelli Date: Tue, 10 Oct 2017 16:33:23 +0200 Subject: [PATCH 3/5] bugfix --- account_invoice_line_pricelist/models/account_invoice_line.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/account_invoice_line_pricelist/models/account_invoice_line.py b/account_invoice_line_pricelist/models/account_invoice_line.py index f3c2d5c6..562110cf 100644 --- a/account_invoice_line_pricelist/models/account_invoice_line.py +++ b/account_invoice_line_pricelist/models/account_invoice_line.py @@ -18,7 +18,7 @@ def _onchange_product_id(self): price_unit = self._common_recalc_price() if price_unit is not None and price_unit != 0: self.price_unit = price_unit - + return res def _common_recalc_price(self): @@ -58,7 +58,7 @@ def create(self, vals): line = super(AccountInvoiceLine, self).create(vals) if line.invoice_id: invoice = line.invoice_id - if ((not line.price_unit) or price_unit == 0) and (invoice.pricelist_id and invoice.pricelist_id.id): + if ((not line.price_unit) or line.price_unit == 0) and (invoice.pricelist_id and invoice.pricelist_id.id): price_unit = line._common_recalc_price() if price_unit is not None and price_unit != 0: line.price_unit = price_unit #triggers write() From c5b5622f6ce667e3c92260d8b04359cffd204cbe Mon Sep 17 00:00:00 2001 From: luca-vercelli Date: Tue, 10 Oct 2017 17:07:56 +0200 Subject: [PATCH 4/5] bugfix --- account_invoice_line_pricelist/models/account_invoice_line.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/account_invoice_line_pricelist/models/account_invoice_line.py b/account_invoice_line_pricelist/models/account_invoice_line.py index 562110cf..0764d6a6 100644 --- a/account_invoice_line_pricelist/models/account_invoice_line.py +++ b/account_invoice_line_pricelist/models/account_invoice_line.py @@ -50,7 +50,7 @@ def _common_recalc_price(self): product.taxes_id, self.invoice_line_tax_ids ) - + def create(self, vals): """ Update price if missing, after real create() @@ -63,7 +63,7 @@ def create(self, vals): if price_unit is not None and price_unit != 0: line.price_unit = price_unit #triggers write() return line - + @api.multi def write(self, vals): """ From 6934cb229399cfb9217cc741a4913f3196c1b11f Mon Sep 17 00:00:00 2001 From: luca-vercelli Date: Tue, 10 Oct 2017 17:32:00 +0200 Subject: [PATCH 5/5] bugfix --- account_invoice_line_pricelist/models/account_invoice_line.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/account_invoice_line_pricelist/models/account_invoice_line.py b/account_invoice_line_pricelist/models/account_invoice_line.py index 0764d6a6..aadc581b 100644 --- a/account_invoice_line_pricelist/models/account_invoice_line.py +++ b/account_invoice_line_pricelist/models/account_invoice_line.py @@ -72,7 +72,7 @@ def write(self, vals): """ for rec in self: if (rec.invoice_id.pricelist_id and rec.invoice_id.pricelist_id.id): - if 'price_unit' in vals and vals['price_unit'] is None or vals['price_unit'] == 0: + if 'price_unit' in vals and (vals['price_unit'] is None or vals['price_unit'] == 0): #INSERT/UPDATE price_unit price_unit = rec._common_recalc_price() if price_unit is not None: