From da3ae50312b678589f2a24a56ba14055d185f9e6 Mon Sep 17 00:00:00 2001 From: Jared Kipe Date: Tue, 18 Apr 2023 22:14:26 +0000 Subject: [PATCH 1/5] [MIG] account_invoice_margin: to 16.0 (from 15.0 hard copy due to errors) --- account_invoice_margin/README.rst | 28 ++++++++ account_invoice_margin/__init__.py | 1 + account_invoice_margin/__manifest__.py | 24 +++++++ account_invoice_margin/i18n/es.po | 48 +++++++++++++ account_invoice_margin/models/__init__.py | 1 + .../models/account_invoice.py | 66 ++++++++++++++++++ account_invoice_margin/tests/__init__.py | 1 + .../tests/test_invoice_margin.py | 69 +++++++++++++++++++ .../views/account_invoice_views.xml | 18 +++++ 9 files changed, 256 insertions(+) create mode 100644 account_invoice_margin/README.rst create mode 100644 account_invoice_margin/__init__.py create mode 100755 account_invoice_margin/__manifest__.py create mode 100644 account_invoice_margin/i18n/es.po create mode 100644 account_invoice_margin/models/__init__.py create mode 100644 account_invoice_margin/models/account_invoice.py create mode 100644 account_invoice_margin/tests/__init__.py create mode 100644 account_invoice_margin/tests/test_invoice_margin.py create mode 100644 account_invoice_margin/views/account_invoice_views.xml diff --git a/account_invoice_margin/README.rst b/account_invoice_margin/README.rst new file mode 100644 index 00000000..5dce01ba --- /dev/null +++ b/account_invoice_margin/README.rst @@ -0,0 +1,28 @@ +****************************** +Hibou - Account Invoice Margin +****************************** + +Include a margin calculation on invoices. + +For more information and add-ons, visit `Hibou.io `_. + + +============= +Main Features +============= + +* Adds computed field `margin` to invoices to give the profitability by calculating the difference between the Unit Price and the Cost. + +.. image:: https://user-images.githubusercontent.com/15882954/45578631-880c0000-b837-11e8-9c4d-d2f15c3c0592.png + :alt: 'Customer Invoice' + :width: 988 + :align: left + + +======= +License +======= + +Please see `LICENSE `_. + +Copyright Hibou Corp. 2023 diff --git a/account_invoice_margin/__init__.py b/account_invoice_margin/__init__.py new file mode 100644 index 00000000..0650744f --- /dev/null +++ b/account_invoice_margin/__init__.py @@ -0,0 +1 @@ +from . import models diff --git a/account_invoice_margin/__manifest__.py b/account_invoice_margin/__manifest__.py new file mode 100755 index 00000000..de6a2931 --- /dev/null +++ b/account_invoice_margin/__manifest__.py @@ -0,0 +1,24 @@ +{ + 'name': 'Invoice Margin', + 'author': 'Hibou Corp. ', + 'version': '16.0.1.0.0', + 'license': 'AGPL-3', + 'category': 'Accounting', + 'sequence': 95, + 'summary': 'Invoices include margin calculation.', + 'description': """ +Invoices include margin calculation. +If the invoice line comes from a sale order line, the cost will come +from the sale order line. + """, + 'website': 'https://hibou.io/', + 'depends': [ + 'account', + 'sale_margin', + ], + 'data': [ + 'views/account_invoice_views.xml', + ], + 'installable': True, + 'application': False, +} diff --git a/account_invoice_margin/i18n/es.po b/account_invoice_margin/i18n/es.po new file mode 100644 index 00000000..8ab81457 --- /dev/null +++ b/account_invoice_margin/i18n/es.po @@ -0,0 +1,48 @@ +# Translation of Odoo Server. +# This file contains the translation of the following modules: +# * account_invoice_margin +# +msgid "" +msgstr "" +"Project-Id-Version: Odoo Server 15.0+e\n" +"Report-Msgid-Bugs-To: \n" +"POT-Creation-Date: 2021-10-11 16:27+0000\n" +"PO-Revision-Date: 2021-10-11 16:27+0000\n" +"Last-Translator: \n" +"Language-Team: \n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=UTF-8\n" +"Content-Transfer-Encoding: \n" +"Plural-Forms: \n" + +#. module: account_invoice_margin +#: model:ir.model.fields,field_description:account_invoice_margin.field_account_move_line__purchase_price +msgid "Cost" +msgstr "Costo" + +#. module: account_invoice_margin +#: model:ir.model.fields,help:account_invoice_margin.field_account_bank_statement_line__margin +#: model:ir.model.fields,help:account_invoice_margin.field_account_move__margin +#: model:ir.model.fields,help:account_invoice_margin.field_account_payment__margin +msgid "" +"It gives profitability by calculating the difference between the Unit Price" +"and the cost." +msgstr "Da la rentabilidad calculando la diferencia entre el precio unitario y costo del producto" + +#. module: account_invoice_margin +#: model:ir.model,name:account_invoice_margin.model_account_move +msgid "Journal Entry" +msgstr "Asiento contable" + +#. module: account_invoice_margin +#: model:ir.model,name:account_invoice_margin.model_account_move_line +msgid "Journal Item" +msgstr "Apunte contable" + +#. module: account_invoice_margin +#: model:ir.model.fields,field_description:account_invoice_margin.field_account_bank_statement_line__margin +#: model:ir.model.fields,field_description:account_invoice_margin.field_account_move__margin +#: model:ir.model.fields,field_description:account_invoice_margin.field_account_move_line__margin +#: model:ir.model.fields,field_description:account_invoice_margin.field_account_payment__margin +msgid "Margin" +msgstr "Margen" diff --git a/account_invoice_margin/models/__init__.py b/account_invoice_margin/models/__init__.py new file mode 100644 index 00000000..8e072db8 --- /dev/null +++ b/account_invoice_margin/models/__init__.py @@ -0,0 +1 @@ +from . import account_invoice diff --git a/account_invoice_margin/models/account_invoice.py b/account_invoice_margin/models/account_invoice.py new file mode 100644 index 00000000..ff7cd3b8 --- /dev/null +++ b/account_invoice_margin/models/account_invoice.py @@ -0,0 +1,66 @@ +from odoo import api, fields, models + + +class AccountMoveLine(models.Model): + _inherit = "account.move.line" + + margin = fields.Float(compute='_product_margin', digits='Product Price', store=True) + purchase_price = fields.Float(string='Cost', digits='Product Price') + + def _compute_margin(self, move, product, product_uom, sale_lines): + # if sale_line_ids and don't re-browse + for line in sale_lines: + return line.purchase_price + frm_cur = move.company_currency_id + to_cur = move.currency_id + purchase_price = product.standard_price + if product_uom and product_uom != product.uom_id: + purchase_price = product.uom_id._compute_price(purchase_price, product_uom) + ctx = self.env.context.copy() + ctx['date'] = move.date if move.date else fields.Date.context_today(move) + price = frm_cur.with_context(ctx)._convert(purchase_price, to_cur, move.company_id, ctx['date'], round=False) + return price + + @api.onchange('product_id', 'product_uom_id') + def product_id_change_margin(self): + for line in self: + if not line.product_id: + line.purchase_price = 0 + else: + line.purchase_price = line._compute_margin(line.move_id, line.product_id, line.product_uom_id, line.sale_line_ids) + + @api.model_create_multi + def create(self, vals): + lines = super(AccountMoveLine, self).create(vals) + if vals and 'purchase_price' not in vals[0]: + lines.product_id_change_margin() + return lines + + @api.depends('product_id', 'purchase_price', 'quantity', 'price_unit', 'price_subtotal') + def _product_margin(self): + for line in self: + currency = line.move_id.currency_id + price = line.purchase_price + margin = line.price_subtotal - (price * line.quantity) + if line.product_id and not price: + date = line.move_id.date if line.move_id.date else fields.Date.context_today(line.move_id) + from_cur = line.move_id.company_currency_id.with_context(date=date) + price = from_cur._convert(line.product_id.standard_price, currency, line.company_id, date, round=False) + margin = line.price_subtotal - (price * line.quantity) + + line.margin = currency.round(margin) if currency else margin + + +class AccountMove(models.Model): + _inherit = "account.move" + + margin = fields.Monetary(compute='_product_margin', + help="It gives profitability by calculating the difference between the Unit Price and the cost.", + currency_field='currency_id', + digits='Product Price', + store=True) + + @api.depends('invoice_line_ids.margin') + def _product_margin(self): + for invoice in self: + invoice.margin = sum(invoice.invoice_line_ids.mapped('margin')) diff --git a/account_invoice_margin/tests/__init__.py b/account_invoice_margin/tests/__init__.py new file mode 100644 index 00000000..982ec0aa --- /dev/null +++ b/account_invoice_margin/tests/__init__.py @@ -0,0 +1 @@ +from . import test_invoice_margin diff --git a/account_invoice_margin/tests/test_invoice_margin.py b/account_invoice_margin/tests/test_invoice_margin.py new file mode 100644 index 00000000..156c14c2 --- /dev/null +++ b/account_invoice_margin/tests/test_invoice_margin.py @@ -0,0 +1,69 @@ +from odoo.addons.sale_margin.tests.test_sale_margin import TestSaleMargin +from datetime import datetime + + +class TestInvoiceMargin(TestSaleMargin): + + def setUp(self): + super(TestInvoiceMargin, self).setUp() + self.AccountMove = self.env['account.move'] + + def test_invoice_margin(self): + """ Test the sale_margin module in Odoo. """ + # Create a sales order for product Graphics Card. + sale_order_so11 = self.SaleOrder.create({ + 'date_order': datetime.today(), + 'name': 'Test_SO011', + 'order_line': [ + (0, 0, { + 'name': '[CARD] Individual Workplace', + 'purchase_price': 700.0, + 'price_unit': 1000.0, + 'product_uom': self.product_uom_id, + 'product_uom_qty': 10.0, + 'state': 'draft', + 'product_id': self.product_id}), + (0, 0, { + 'name': 'Line without product_uom', + 'price_unit': 1000.0, + 'purchase_price': 700.0, + 'product_uom_qty': 10.0, + 'state': 'draft', + 'product_id': self.product_id})], + 'partner_id': self.partner_id, + 'partner_invoice_id': self.partner_invoice_address_id, + 'partner_shipping_id': self.partner_invoice_address_id, + 'pricelist_id': self.pricelist_id}) + # Confirm the sales order. + sale_order_so11.action_confirm() + # Verify that margin field gets bind with the value. + self.assertEqual(sale_order_so11.margin, 6000.00, "Sales order margin should be 6000.00") + + sale_order_so11.order_line.write({'qty_delivered': 10.0}) + + # Invoice the sales order. + inv = sale_order_so11._create_invoices() + self.assertEqual(inv.margin, sale_order_so11.margin) + + account = self.env['account.account'].search([('internal_type', '=', 'other')], limit=1) + inv = self.AccountMove.create({ + 'move_type': 'in_invoice', + 'partner_id': self.partner_id, + 'invoice_line_ids': [ + (0, 0, { + 'account_id': account.id, + 'name': '[CARD] Graphics Card', + 'purchase_price': 600.0, + 'price_unit': 1000.0, + 'quantity': 10.0, + 'product_id': self.product_id}), + (0, 0, { + 'account_id': account.id, + 'name': 'Line without product_uom', + 'price_unit': 1000.0, + 'purchase_price': 800.0, + 'quantity': 10.0,}) + ], + }) + self.assertEqual(len(inv.invoice_line_ids), 2) + self.assertEqual(inv.margin, 6000.0) diff --git a/account_invoice_margin/views/account_invoice_views.xml b/account_invoice_margin/views/account_invoice_views.xml new file mode 100644 index 00000000..e1495b0b --- /dev/null +++ b/account_invoice_margin/views/account_invoice_views.xml @@ -0,0 +1,18 @@ + + + + + account.invoice.margin.view.form + account.move + + + + + + + + + + + + From 0f5b691ba3056d713ab45fcaa9d66868bf476fd0 Mon Sep 17 00:00:00 2001 From: Jared Kipe Date: Tue, 18 Apr 2023 22:53:06 +0000 Subject: [PATCH 2/5] [IMP] account_invoice_margin: add margin_percent and improve tests --- account_invoice_margin/__manifest__.py | 2 +- .../models/account_invoice.py | 30 +++++---- .../tests/test_invoice_margin.py | 63 ++++++++----------- .../views/account_invoice_views.xml | 3 + 4 files changed, 50 insertions(+), 48 deletions(-) diff --git a/account_invoice_margin/__manifest__.py b/account_invoice_margin/__manifest__.py index de6a2931..70bc5892 100755 --- a/account_invoice_margin/__manifest__.py +++ b/account_invoice_margin/__manifest__.py @@ -1,7 +1,7 @@ { 'name': 'Invoice Margin', 'author': 'Hibou Corp. ', - 'version': '16.0.1.0.0', + 'version': '16.0.1.1.0', 'license': 'AGPL-3', 'category': 'Accounting', 'sequence': 95, diff --git a/account_invoice_margin/models/account_invoice.py b/account_invoice_margin/models/account_invoice.py index ff7cd3b8..a8940c11 100644 --- a/account_invoice_margin/models/account_invoice.py +++ b/account_invoice_margin/models/account_invoice.py @@ -4,9 +4,14 @@ from odoo import api, fields, models class AccountMoveLine(models.Model): _inherit = "account.move.line" - margin = fields.Float(compute='_product_margin', digits='Product Price', store=True) - purchase_price = fields.Float(string='Cost', digits='Product Price') + margin = fields.Monetary(compute='_compute_product_margin', digits='Product Price', store=True, + groups='base.group_user') + margin_percent = fields.Float(compute='_product_margin', store=True, string='Margin (%)', + groups='base.group_user') + purchase_price = fields.Monetary(string='Cost', digits='Product Price', + groups='base.group_user') + # Note we are keeping this API because it is easy to customize and extend the purchase price/margin calculation def _compute_margin(self, move, product, product_uom, sale_lines): # if sale_line_ids and don't re-browse for line in sale_lines: @@ -25,7 +30,7 @@ class AccountMoveLine(models.Model): def product_id_change_margin(self): for line in self: if not line.product_id: - line.purchase_price = 0 + line.purchase_price = 0.0 else: line.purchase_price = line._compute_margin(line.move_id, line.product_id, line.product_uom_id, line.sale_line_ids) @@ -37,7 +42,7 @@ class AccountMoveLine(models.Model): return lines @api.depends('product_id', 'purchase_price', 'quantity', 'price_unit', 'price_subtotal') - def _product_margin(self): + def _compute_product_margin(self): for line in self: currency = line.move_id.currency_id price = line.purchase_price @@ -49,18 +54,21 @@ class AccountMoveLine(models.Model): margin = line.price_subtotal - (price * line.quantity) line.margin = currency.round(margin) if currency else margin + line.margin_percent = 1.0 if not line.price_subtotal else line.margin / line.price_subtotal class AccountMove(models.Model): _inherit = "account.move" - margin = fields.Monetary(compute='_product_margin', - help="It gives profitability by calculating the difference between the Unit Price and the cost.", - currency_field='currency_id', - digits='Product Price', - store=True) + margin = fields.Monetary(compute='_compute_product_margin', store=True, digits='Product Price', + help="Profitability by calculating the difference between the Unit Price and the cost.", + groups='base.group_user') + margin_percent = fields.Float(compute='_compute_product_margin', store=True, string='Margin (%)', + groups='base.group_user') - @api.depends('invoice_line_ids.margin') - def _product_margin(self): + @api.depends('invoice_line_ids.margin', 'invoice_line_ids.price_subtotal') + def _compute_product_margin(self): for invoice in self: invoice.margin = sum(invoice.invoice_line_ids.mapped('margin')) + total_price_subtotal = sum(invoice.invoice_line_ids.mapped('price_subtotal')) + invoice.margin_percent = 1.0 if not total_price_subtotal else invoice.margin / total_price_subtotal diff --git a/account_invoice_margin/tests/test_invoice_margin.py b/account_invoice_margin/tests/test_invoice_margin.py index 156c14c2..0ae28f72 100644 --- a/account_invoice_margin/tests/test_invoice_margin.py +++ b/account_invoice_margin/tests/test_invoice_margin.py @@ -1,3 +1,4 @@ +from odoo.fields import Command from odoo.addons.sale_margin.tests.test_sale_margin import TestSaleMargin from datetime import datetime @@ -7,56 +8,45 @@ class TestInvoiceMargin(TestSaleMargin): def setUp(self): super(TestInvoiceMargin, self).setUp() self.AccountMove = self.env['account.move'] + self.SaleOrder = self.env['sale.order'] def test_invoice_margin(self): - """ Test the sale_margin module in Odoo. """ - # Create a sales order for product Graphics Card. - sale_order_so11 = self.SaleOrder.create({ - 'date_order': datetime.today(), - 'name': 'Test_SO011', - 'order_line': [ - (0, 0, { - 'name': '[CARD] Individual Workplace', - 'purchase_price': 700.0, - 'price_unit': 1000.0, - 'product_uom': self.product_uom_id, - 'product_uom_qty': 10.0, - 'state': 'draft', - 'product_id': self.product_id}), - (0, 0, { - 'name': 'Line without product_uom', - 'price_unit': 1000.0, - 'purchase_price': 700.0, - 'product_uom_qty': 10.0, - 'state': 'draft', - 'product_id': self.product_id})], - 'partner_id': self.partner_id, - 'partner_invoice_id': self.partner_invoice_address_id, - 'partner_shipping_id': self.partner_invoice_address_id, - 'pricelist_id': self.pricelist_id}) - # Confirm the sales order. - sale_order_so11.action_confirm() - # Verify that margin field gets bind with the value. - self.assertEqual(sale_order_so11.margin, 6000.00, "Sales order margin should be 6000.00") + self.product.standard_price = 700.0 + order = self.empty_order - sale_order_so11.order_line.write({'qty_delivered': 10.0}) + order.order_line = [ + Command.create({ + 'price_unit': 1000.0, + 'product_uom_qty': 10.0, + 'product_id': self.product.id, + }), + ] + # Confirm the sales order. + order.action_confirm() + # Verify that margin field gets bind with the value. + self.assertEqual(order.margin, 3000.00, "Sales order profit should be 6000.00") + self.assertEqual(order.margin_percent, 0.3, "Sales order margin should be 30%") + + order.order_line.write({'qty_delivered': 10.0}) # Invoice the sales order. - inv = sale_order_so11._create_invoices() - self.assertEqual(inv.margin, sale_order_so11.margin) + inv = order._create_invoices() + self.assertEqual(inv.margin, order.margin) + self.assertEqual(inv.margin_percent, order.margin_percent) - account = self.env['account.account'].search([('internal_type', '=', 'other')], limit=1) + account = self.env['account.account'].search([('account_type', '=', 'expense')], limit=1) + self.assertTrue(account) inv = self.AccountMove.create({ 'move_type': 'in_invoice', - 'partner_id': self.partner_id, + 'partner_id': order.partner_id.id, 'invoice_line_ids': [ (0, 0, { 'account_id': account.id, 'name': '[CARD] Graphics Card', - 'purchase_price': 600.0, 'price_unit': 1000.0, + 'purchase_price': 600.0, 'quantity': 10.0, - 'product_id': self.product_id}), + 'product_id': self.product.id}), (0, 0, { 'account_id': account.id, 'name': 'Line without product_uom', @@ -67,3 +57,4 @@ class TestInvoiceMargin(TestSaleMargin): }) self.assertEqual(len(inv.invoice_line_ids), 2) self.assertEqual(inv.margin, 6000.0) + self.assertEqual(inv.margin_percent, 0.3) diff --git a/account_invoice_margin/views/account_invoice_views.xml b/account_invoice_margin/views/account_invoice_views.xml index e1495b0b..89729da0 100644 --- a/account_invoice_margin/views/account_invoice_views.xml +++ b/account_invoice_margin/views/account_invoice_views.xml @@ -8,6 +8,9 @@ + + () + From 155ffc9072ea699956660d05ffd3e58f17518b0b Mon Sep 17 00:00:00 2001 From: Jared Kipe Date: Mon, 12 Jun 2023 15:59:47 +0000 Subject: [PATCH 3/5] [FIX] account_invoice_margin: compute method name --- account_invoice_margin/models/account_invoice.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/account_invoice_margin/models/account_invoice.py b/account_invoice_margin/models/account_invoice.py index a8940c11..c3eafb13 100644 --- a/account_invoice_margin/models/account_invoice.py +++ b/account_invoice_margin/models/account_invoice.py @@ -6,7 +6,7 @@ class AccountMoveLine(models.Model): margin = fields.Monetary(compute='_compute_product_margin', digits='Product Price', store=True, groups='base.group_user') - margin_percent = fields.Float(compute='_product_margin', store=True, string='Margin (%)', + margin_percent = fields.Float(compute='_compute_product_margin', store=True, string='Margin (%)', groups='base.group_user') purchase_price = fields.Monetary(string='Cost', digits='Product Price', groups='base.group_user') From b6e19684687eb61cb0e49ad04e6825777dd34b60 Mon Sep 17 00:00:00 2001 From: Salomon Chambi Date: Mon, 31 Jul 2023 22:57:33 +0000 Subject: [PATCH 4/5] [IMP] account_invoice_margin: show margin and margin % in the same line --- account_invoice_margin/models/account_invoice.py | 2 +- account_invoice_margin/views/account_invoice_views.xml | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/account_invoice_margin/models/account_invoice.py b/account_invoice_margin/models/account_invoice.py index c3eafb13..edd2e3a7 100644 --- a/account_invoice_margin/models/account_invoice.py +++ b/account_invoice_margin/models/account_invoice.py @@ -5,7 +5,7 @@ class AccountMoveLine(models.Model): _inherit = "account.move.line" margin = fields.Monetary(compute='_compute_product_margin', digits='Product Price', store=True, - groups='base.group_user') + string='Margin', groups='base.group_user') margin_percent = fields.Float(compute='_compute_product_margin', store=True, string='Margin (%)', groups='base.group_user') purchase_price = fields.Monetary(string='Cost', digits='Product Price', diff --git a/account_invoice_margin/views/account_invoice_views.xml b/account_invoice_margin/views/account_invoice_views.xml index 89729da0..39aabe22 100644 --- a/account_invoice_margin/views/account_invoice_views.xml +++ b/account_invoice_margin/views/account_invoice_views.xml @@ -7,8 +7,9 @@ - - + + Margin: + () From c017c1b40b67e90d52fa9b95b770d30f1c412475 Mon Sep 17 00:00:00 2001 From: Salomon Chambi Date: Thu, 2 Nov 2023 22:16:26 +0000 Subject: [PATCH 5/5] [MIG] account_invoice_margin: to 17.0 --- account_invoice_margin/__manifest__.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/account_invoice_margin/__manifest__.py b/account_invoice_margin/__manifest__.py index 70bc5892..c08a4f7d 100755 --- a/account_invoice_margin/__manifest__.py +++ b/account_invoice_margin/__manifest__.py @@ -1,7 +1,7 @@ { 'name': 'Invoice Margin', - 'author': 'Hibou Corp. ', - 'version': '16.0.1.1.0', + 'version': '17.0.1.1.0', + 'author': 'Hibou Corp.', 'license': 'AGPL-3', 'category': 'Accounting', 'sequence': 95,