From d9f8da2b9e77ef24c5930471c00d042175eed17d Mon Sep 17 00:00:00 2001 From: Alexis de Lattre Date: Fri, 31 Jul 2015 17:35:53 +0200 Subject: [PATCH] Add margin rate in percentage Margin is now negative on refunds (not only in account.invoice, but also on account.invoice.line) --- account_invoice_margin/account_invoice.py | 22 ++++++++++++++----- .../account_invoice_view.xml | 1 + sale_margin_no_onchange/sale.py | 8 +++++++ sale_margin_no_onchange/sale_view.xml | 1 + 4 files changed, 27 insertions(+), 5 deletions(-) diff --git a/account_invoice_margin/account_invoice.py b/account_invoice_margin/account_invoice.py index b7bd71a..e0094a5 100644 --- a/account_invoice_margin/account_invoice.py +++ b/account_invoice_margin/account_invoice.py @@ -42,17 +42,24 @@ class AccountInvoiceLine(models.Model): string='Margin in Company Currency', readonly=True, store=True, compute='_compute_margin', digits=dp.get_precision('Account')) + margin_rate = fields.Float( + string="Margin Rate", readonly=True, store=True, + compute='_compute_margin', + digits=(16, 2), help="Margin rate in percentage of the sale price") @api.one @api.depends( 'standard_price_company_currency', 'invoice_id.currency_id', - 'invoice_id.move_id', + 'invoice_id.move_id', 'invoice_id.type', 'invoice_id.date_invoice', 'quantity', 'price_subtotal') def _compute_margin(self): standard_price_inv_cur = 0.0 margin_inv_cur = 0.0 margin_comp_cur = 0.0 - if self.invoice_id: + margin_rate = 0.0 + if ( + self.invoice_id and + self.invoice_id.type in ('out_invoice', 'out_refund')): # it works in _get_current_rate # even if we set date = False in context standard_price_inv_cur =\ @@ -65,9 +72,17 @@ class AccountInvoiceLine(models.Model): margin_comp_cur = self.invoice_id.currency_id.with_context( date=self.invoice_id.date_invoice).compute( margin_inv_cur, self.invoice_id.company_id.currency_id) + if self.price_subtotal: + margin_rate = 100 * margin_inv_cur / self.price_subtotal + # for a refund, margin should be negative + # but margin rate should stay positive + if self.invoice_id.type == 'out_refund': + margin_inv_cur *= -1 + margin_comp_cur *= -1 self.standard_price_invoice_currency = standard_price_inv_cur self.margin_invoice_currency = margin_inv_cur self.margin_company_currency = margin_comp_cur + self.margin_rate = margin_rate # We want to copy standard_price on invoice line for customer # invoice/refunds. We can't do that via on_change of product_id, @@ -144,8 +159,5 @@ class AccountInvoice(models.Model): for il in self.invoice_line: margin_inv_cur += il.margin_invoice_currency margin_comp_cur += il.margin_company_currency - if self.type == 'out_refund': - margin_inv_cur *= -1 - margin_comp_cur *= -1 self.margin_invoice_currency = margin_inv_cur self.margin_company_currency = margin_comp_cur diff --git a/account_invoice_margin/account_invoice_view.xml b/account_invoice_margin/account_invoice_view.xml index 1cc845a..c3aeb13 100644 --- a/account_invoice_margin/account_invoice_view.xml +++ b/account_invoice_margin/account_invoice_view.xml @@ -49,6 +49,7 @@ + diff --git a/sale_margin_no_onchange/sale.py b/sale_margin_no_onchange/sale.py index d577c1b..0de39ce 100644 --- a/sale_margin_no_onchange/sale.py +++ b/sale_margin_no_onchange/sale.py @@ -42,6 +42,10 @@ class SaleOrderLine(models.Model): string='Margin in Company Currency', readonly=True, store=True, compute='_compute_margin', digits=dp.get_precision('Account')) + margin_rate = fields.Float( + string="Margin Rate", readonly=True, store=True, + compute='_compute_margin', + digits=(16, 2), help="Margin rate in percentage of the sale price") @api.one @api.depends( @@ -51,6 +55,7 @@ class SaleOrderLine(models.Model): standard_price_sale_cur = 0.0 margin_sale_cur = 0.0 margin_comp_cur = 0.0 + margin_rate = 0.0 if self.order_id and self.order_id.currency_id: # it works in _get_current_rate # even if we set date = False in context @@ -65,9 +70,12 @@ class SaleOrderLine(models.Model): margin_comp_cur = self.order_id.currency_id.with_context( date=self.order_id.date_order).compute( margin_sale_cur, self.order_id.company_id.currency_id) + if self.price_subtotal: + margin_rate = 100 * margin_sale_cur / self.price_subtotal self.standard_price_sale_currency = standard_price_sale_cur self.margin_sale_currency = margin_sale_cur self.margin_company_currency = margin_comp_cur + self.margin_rate = margin_rate # We want to copy standard_price on sale order line @api.model diff --git a/sale_margin_no_onchange/sale_view.xml b/sale_margin_no_onchange/sale_view.xml index f8c6b4e..5719813 100644 --- a/sale_margin_no_onchange/sale_view.xml +++ b/sale_margin_no_onchange/sale_view.xml @@ -28,6 +28,7 @@ +