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) diff --git a/account_invoice_line_pricelist/models/account_invoice_line.py b/account_invoice_line_pricelist/models/account_invoice_line.py index 38d0d7c4..aadc581b 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 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() + 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)