diff --git a/addons/product_margin/__manifest__.py b/addons/product_margin/__manifest__.py index 6bf8b11ad22..3b7ac65d638 100644 --- a/addons/product_margin/__manifest__.py +++ b/addons/product_margin/__manifest__.py @@ -17,5 +17,4 @@ The wizard to launch the report has several options to help you get the data you 'wizard/product_margin_view.xml', 'views/product_product_views.xml' ], - 'test': ['test/product_margin.yml'], } diff --git a/addons/product_margin/test/product_margin.yml b/addons/product_margin/test/product_margin.yml deleted file mode 100644 index 0f080391fa8..00000000000 --- a/addons/product_margin/test/product_margin.yml +++ /dev/null @@ -1,25 +0,0 @@ -- - In order to test the product_margin module, I create a product margin record -- - !record {model: product.margin, id: product_margin_wiz0}: - from_date: !eval "'%s-01-01' %(datetime.now().year)" - to_date: !eval "'%s-12-31' %(datetime.now().year)" - invoice_state: open_paid -- - I open margin for PC-DEM -- - !python {model: product.margin, id: False}: | - self.browse(ref('product_margin_wiz0')).with_context({"lang": 'en_US', - "search_default_filter_to_sell": "1", "tz": False, "active_model": "product.product", "disable_log": True, - "active_ids": [ref("product.product_product_5")], "active_id": ref("product.product_product_5")}).action_open_window() -- - I read the fields of the product. -- - !python {model: product.product, id: False}: | - fields = ['sale_avg_price', 'expected_margin_rate', 'total_margin_rate', 'total_cost', 'sale_num_invoiced', - 'total_margin', 'sales_gap', 'purchase_num_invoiced', 'expected_margin', 'turnover'] - self.browse(ref('product_margin_wiz0')).with_context({"lang": 'en_US', - "search_default_filter_to_sell": "1", "tz": False, "active_model": "product.product", - "bin_size": True, "active_ids": [ref("product.product_product_5")], - "active_id": ref("product.product_product_5")}).read(fields) - diff --git a/addons/product_margin/tests/__init__.py b/addons/product_margin/tests/__init__.py new file mode 100644 index 00000000000..e0b4e4d897e --- /dev/null +++ b/addons/product_margin/tests/__init__.py @@ -0,0 +1,4 @@ +# -*- coding: utf-8 -*- +# Part of Odoo. See LICENSE file for full copyright and licensing details. + +from . import test_product_margin diff --git a/addons/product_margin/tests/test_product_margin.py b/addons/product_margin/tests/test_product_margin.py new file mode 100644 index 00000000000..c67ffd5eb2f --- /dev/null +++ b/addons/product_margin/tests/test_product_margin.py @@ -0,0 +1,72 @@ +# -*- coding: utf-8 -*- +# Part of Odoo. See LICENSE file for full copyright and licensing details. +from odoo import tools +from odoo.tests import common +from odoo.modules.module import get_resource_path + + +class TestProductMargin(common.TransactionCase): + + def create_account_invoice(self, invoice_type, partner, product, quantity=0.0, price_unit=0.0): + """ Create an invoice as in a view by triggering its onchange methods""" + default_get_vals = self.env['account.invoice'].default_get(list(self.env['account.invoice'].fields_get())) + account_invoice = self.env['account.invoice'].new(default_get_vals) + account_invoice.type = invoice_type + account_invoice._onchange_journal_id() + account_invoice.partner_id = partner.id + account_invoice._onchange_partner_id() + account_invoice.invoice_line_ids = [(0, 0, { + 'quantity': quantity, + 'product_id': product.id, + 'uom_id': self.ref('product.product_uom_unit'), + 'invoice_id': account_invoice, # This value is required for '_onchange_product_id' to work correctly + })] + account_invoice.invoice_line_ids._onchange_product_id() + account_invoice.invoice_line_ids.price_unit = price_unit + account_invoice._onchange_invoice_line_ids() + vals = account_invoice._convert_to_write(account_invoice._cache) + account_invoice = self.env['account.invoice'].create(vals) + account_invoice.action_invoice_open() + + def test_product_margin(self): + ''' In order to test the product_margin module ''' + + # load account_minimal_test.xml file for chart of account in configuration + tools.convert_file(self.cr, 'product_margin', + get_resource_path('account', 'test', 'account_minimal_test.xml'), + {}, 'init', False, 'test', self.registry._assertion_report) + + supplier = self.env['res.partner'].create({'name': 'Supplier', 'supplier': True}) + customer = self.env['res.partner'].create({'name': 'Customer', 'customer': True}) + ipad = self.env.ref("product.product_product_4") + + # Create supplier invoice and customer invoice to test product margin. + # Define supplier invoices + self.create_account_invoice('in_invoice', supplier, ipad, 10.0, 300.00) + self.create_account_invoice('in_invoice', supplier, ipad, 4.0, 450.00) + # Define Customer Invoices + self.create_account_invoice('out_invoice', customer, ipad, 20.0, 750.00) + self.create_account_invoice('out_invoice', customer, ipad, 10.0, 550.00) + + result = ipad._compute_product_margin_fields_values() + + # Sale turnover ( Quantity * Price Subtotal / Quantity) + sale_turnover = ((20.0 * 750.00) + (10.0 * 550.00)) + + # Expected sale (Total quantity * Sale price) + sale_expected = (750.00 * 30.0) + + # Purchase total cost (Quantity * Unit price) + purchase_total_cost = ((10.0 * 300.00) + (4.0 * 450.00)) + + # Purchase normal cost ( Total quantity * Cost price) + purchase_normal_cost = (14.0 * 500.00) + + total_margin = sale_turnover - purchase_total_cost + expected_margin = sale_expected - purchase_normal_cost + + # Check total margin + self.assertEqual(result[ipad.id]['total_margin'], total_margin, "Wrong Total Margin.") + + # Check expected margin + self.assertEqual(result[ipad.id]['expected_margin'], expected_margin, "Wrong Expected Margin.")