From becf288435b46948001a12fefa884761379cd37d Mon Sep 17 00:00:00 2001 From: Victor Feyens Date: Mon, 21 Feb 2022 10:50:41 +0000 Subject: [PATCH] [IMP] sale_margin: clean module * Split python file by classes * Remove empty ir.model.access.csv file * Correct name for views file * Use setUpClass instead of setUp to avoid multiple setups during tests Part-of: odoo/odoo#85001 --- addons/sale_margin/__manifest__.py | 8 ++- addons/sale_margin/data/sale_margin_demo.xml | 4 +- addons/sale_margin/models/__init__.py | 1 + addons/sale_margin/models/sale_order.py | 56 +---------------- addons/sale_margin/models/sale_order_line.py | 60 +++++++++++++++++++ .../sale_margin/security/ir.model.access.csv | 1 - addons/sale_margin/tests/test_sale_margin.py | 23 +++---- ...e_margin_view.xml => sale_order_views.xml} | 0 8 files changed, 81 insertions(+), 72 deletions(-) create mode 100644 addons/sale_margin/models/sale_order_line.py delete mode 100644 addons/sale_margin/security/ir.model.access.csv rename addons/sale_margin/views/{sale_margin_view.xml => sale_order_views.xml} (100%) diff --git a/addons/sale_margin/__manifest__.py b/addons/sale_margin/__manifest__.py index 294e6d6d95b..98dcbe943bd 100644 --- a/addons/sale_margin/__manifest__.py +++ b/addons/sale_margin/__manifest__.py @@ -13,7 +13,11 @@ This gives the profitability by calculating the difference between the Unit Price and Cost Price. """, 'depends':['sale_management'], - 'demo':['data/sale_margin_demo.xml'], - 'data':['security/ir.model.access.csv','views/sale_margin_view.xml'], + 'demo':[ + 'data/sale_margin_demo.xml', + ], + 'data':[ + 'views/sale_order_views.xml', + ], 'license': 'LGPL-3', } diff --git a/addons/sale_margin/data/sale_margin_demo.xml b/addons/sale_margin/data/sale_margin_demo.xml index 1a4e6d698e1..c68aa030d69 100644 --- a/addons/sale_margin/data/sale_margin_demo.xml +++ b/addons/sale_margin/data/sale_margin_demo.xml @@ -1,6 +1,5 @@ - - + 2870.00 @@ -34,5 +33,4 @@ 13.00 - diff --git a/addons/sale_margin/models/__init__.py b/addons/sale_margin/models/__init__.py index 00b62d54b4a..9b5fa8c35d8 100644 --- a/addons/sale_margin/models/__init__.py +++ b/addons/sale_margin/models/__init__.py @@ -2,3 +2,4 @@ # Part of Odoo. See LICENSE file for full copyright and licensing details. from . import sale_order +from . import sale_order_line diff --git a/addons/sale_margin/models/sale_order.py b/addons/sale_margin/models/sale_order.py index e1d0696862a..e5dfa129896 100644 --- a/addons/sale_margin/models/sale_order.py +++ b/addons/sale_margin/models/sale_order.py @@ -1,63 +1,9 @@ # -*- coding: utf-8 -*- # Part of Odoo. See LICENSE file for full copyright and licensing details. + from odoo import api, fields, models -class SaleOrderLine(models.Model): - _inherit = "sale.order.line" - - margin = fields.Float( - "Margin", compute='_compute_margin', - digits='Product Price', store=True, groups="base.group_user", precompute=True) - margin_percent = fields.Float( - "Margin (%)", compute='_compute_margin', store=True, groups="base.group_user", precompute=True) - purchase_price = fields.Float( - string='Cost', compute="_compute_purchase_price", - digits='Product Price', store=True, readonly=False, precompute=True, - groups="base.group_user") - - @api.depends('product_id', 'company_id', 'currency_id', 'product_uom') - def _compute_purchase_price(self): - for line in self: - if not line.product_id: - line.purchase_price = 0.0 - continue - line = line.with_company(line.company_id) - product_cost = line.product_id.standard_price - line.purchase_price = line._convert_price(product_cost, line.product_id.uom_id) - - @api.depends('price_subtotal', 'product_uom_qty', 'purchase_price') - def _compute_margin(self): - for line in self: - line.margin = line.price_subtotal - (line.purchase_price * line.product_uom_qty) - line.margin_percent = line.price_subtotal and line.margin/line.price_subtotal - - def _convert_price(self, product_cost, from_uom): - self.ensure_one() - if not product_cost: - # If the standard_price is 0 - # Avoid unnecessary computations - # and currency conversions - if not self.purchase_price: - return product_cost - from_currency = self.product_id.cost_currency_id - to_cur = self.currency_id or self.order_id.currency_id - to_uom = self.product_uom - if to_uom and to_uom != from_uom: - product_cost = from_uom._compute_price( - product_cost, - to_uom, - ) - return from_currency._convert( - from_amount=product_cost, - to_currency=to_cur, - company=self.company_id or self.env.company, - date=self.order_id.date_order or fields.Date.today(), - round=False, - ) if to_cur and product_cost else product_cost - # The pricelist may not have been set, therefore no conversion - # is needed because we don't know the target currency.. - class SaleOrder(models.Model): _inherit = "sale.order" diff --git a/addons/sale_margin/models/sale_order_line.py b/addons/sale_margin/models/sale_order_line.py new file mode 100644 index 00000000000..8e03988ceac --- /dev/null +++ b/addons/sale_margin/models/sale_order_line.py @@ -0,0 +1,60 @@ +# -*- coding: utf-8 -*- +# Part of Odoo. See LICENSE file for full copyright and licensing details. + +from odoo import api, fields, models + + +class SaleOrderLine(models.Model): + _inherit = "sale.order.line" + + margin = fields.Float( + "Margin", compute='_compute_margin', + digits='Product Price', store=True, groups="base.group_user", precompute=True) + margin_percent = fields.Float( + "Margin (%)", compute='_compute_margin', store=True, groups="base.group_user", precompute=True) + purchase_price = fields.Float( + string='Cost', compute="_compute_purchase_price", + digits='Product Price', store=True, readonly=False, precompute=True, + groups="base.group_user") + + @api.depends('product_id', 'company_id', 'currency_id', 'product_uom') + def _compute_purchase_price(self): + for line in self: + if not line.product_id: + line.purchase_price = 0.0 + continue + line = line.with_company(line.company_id) + product_cost = line.product_id.standard_price + line.purchase_price = line._convert_price(product_cost, line.product_id.uom_id) + + @api.depends('price_subtotal', 'product_uom_qty', 'purchase_price') + def _compute_margin(self): + for line in self: + line.margin = line.price_subtotal - (line.purchase_price * line.product_uom_qty) + line.margin_percent = line.price_subtotal and line.margin/line.price_subtotal + + def _convert_price(self, product_cost, from_uom): + self.ensure_one() + if not product_cost: + # If the standard_price is 0 + # Avoid unnecessary computations + # and currency conversions + if not self.purchase_price: + return product_cost + from_currency = self.product_id.cost_currency_id + to_cur = self.currency_id or self.order_id.currency_id + to_uom = self.product_uom + if to_uom and to_uom != from_uom: + product_cost = from_uom._compute_price( + product_cost, + to_uom, + ) + return from_currency._convert( + from_amount=product_cost, + to_currency=to_cur, + company=self.company_id or self.env.company, + date=self.order_id.date_order or fields.Date.today(), + round=False, + ) if to_cur and product_cost else product_cost + # The pricelist may not have been set, therefore no conversion + # is needed because we don't know the target currency.. diff --git a/addons/sale_margin/security/ir.model.access.csv b/addons/sale_margin/security/ir.model.access.csv deleted file mode 100644 index 4a08a4aa60d..00000000000 --- a/addons/sale_margin/security/ir.model.access.csv +++ /dev/null @@ -1 +0,0 @@ -id,name,model_id:id,group_id:id,perm_read,perm_write,perm_create,perm_unlink diff --git a/addons/sale_margin/tests/test_sale_margin.py b/addons/sale_margin/tests/test_sale_margin.py index 01bb16380ed..69047e7580c 100644 --- a/addons/sale_margin/tests/test_sale_margin.py +++ b/addons/sale_margin/tests/test_sale_margin.py @@ -7,20 +7,21 @@ from datetime import datetime class TestSaleMargin(common.TransactionCase): - def setUp(self): - super(TestSaleMargin, self).setUp() - self.SaleOrder = self.env['sale.order'] + @classmethod + def setUpClass(cls): + super().setUpClass() + cls.SaleOrder = cls.env['sale.order'] - self.product_uom_id = self.ref('uom.product_uom_unit') - self.product = self.env['product.product'].create({'name': 'Individual Workplace'}) - self.product_id = self.product.id - self.partner_id = self.env['res.partner'].create({'name': 'A test partner'}).id - self.partner_invoice_address_id = self.env['res.partner'].create({ + cls.product_uom_id = cls.env.ref('uom.product_uom_unit').id + cls.product = cls.env['product.product'].create({'name': 'Individual Workplace'}) + cls.product_id = cls.product.id + cls.partner_id = cls.env['res.partner'].create({'name': 'A test partner'}).id + cls.partner_invoice_address_id = cls.env['res.partner'].create({ 'name': 'A test partner address', - 'parent_id': self.partner_id, + 'parent_id': cls.partner_id, }).id - self.pricelist_id = self.ref('product.list0') - self.pricelist = self.env.ref('product.list0') + cls.pricelist = cls.env.ref('product.list0') + cls.pricelist_id = cls.pricelist.id def test_sale_margin(self): """ Test the sale_margin module in Odoo. """ diff --git a/addons/sale_margin/views/sale_margin_view.xml b/addons/sale_margin/views/sale_order_views.xml similarity index 100% rename from addons/sale_margin/views/sale_margin_view.xml rename to addons/sale_margin/views/sale_order_views.xml