From 8dbc9f5415bba41c0f155b69f2d41716f93ab718 Mon Sep 17 00:00:00 2001 From: roen-odoo Date: Mon, 22 Aug 2022 11:41:08 +0000 Subject: [PATCH] [FIX] pos_sale: add margin for pos sales in sales report Current behavior: In the sales report of a product, the margin was not appearing for product that were sold in the PoS. Steps to reproduce: - Activate the margin option in the settings - Create a product available in PoS, the product should have a price and cost set - Start a PoS session and sell the product, then close the PoS - Go on the product page, click the "Sold" smart button - Add the "Margin" measure to the pivot - There is no value in the margin column opw-2937616 closes odoo/odoo#101168 X-original-commit: b230194d8f6a93d374a31f05e74c8fb26277d905 Signed-off-by: Trinh Jacky (trj) Signed-off-by: Engels Robin (roen) --- addons/pos_sale/report/sale_report.py | 17 ++++++-- addons/pos_sale_margin/__init__.py | 4 ++ addons/pos_sale_margin/__manifest__.py | 18 ++++++++ addons/pos_sale_margin/report/__init__.py | 4 ++ addons/pos_sale_margin/report/sale_report.py | 13 ++++++ addons/pos_sale_margin/tests/__init__.py | 4 ++ .../tests/test_pos_sale_margin_report.py | 43 +++++++++++++++++++ 7 files changed, 99 insertions(+), 4 deletions(-) create mode 100644 addons/pos_sale_margin/__init__.py create mode 100644 addons/pos_sale_margin/__manifest__.py create mode 100644 addons/pos_sale_margin/report/__init__.py create mode 100644 addons/pos_sale_margin/report/sale_report.py create mode 100644 addons/pos_sale_margin/tests/__init__.py create mode 100644 addons/pos_sale_margin/tests/test_pos_sale_margin_report.py diff --git a/addons/pos_sale/report/sale_report.py b/addons/pos_sale/report/sale_report.py index bcf4bf8f98f..c525e3592b5 100644 --- a/addons/pos_sale/report/sale_report.py +++ b/addons/pos_sale/report/sale_report.py @@ -75,13 +75,22 @@ class SaleReport(models.Model): AS discount_amount, NULL AS order_id""" - additional_fields_info = self._select_additional_fields() + additional_fields = self._select_additional_fields() + additional_fields_info = self._fill_pos_fields(additional_fields) template = """, - NULL AS %s""" - for fname in additional_fields_info.keys(): - select_ += template % fname + %s AS %s""" + for fname, value in additional_fields_info.items(): + select_ += template % (value, fname) return select_ + def _fill_pos_fields(self, additional_fields): + """Hook to fill additional fields for the pos_sale. + + :param values: dictionary of values to fill + :type values: dict + """ + return {x: 'NULL' for x in additional_fields} + def _from_pos(self): return """ pos_order_line l diff --git a/addons/pos_sale_margin/__init__.py b/addons/pos_sale_margin/__init__.py new file mode 100644 index 00000000000..abbf32cef4c --- /dev/null +++ b/addons/pos_sale_margin/__init__.py @@ -0,0 +1,4 @@ +# -*- coding: utf-8 -*- +# Part of Odoo. See LICENSE file for full copyright and licensing details. + +from . import report diff --git a/addons/pos_sale_margin/__manifest__.py b/addons/pos_sale_margin/__manifest__.py new file mode 100644 index 00000000000..334e8cd3b27 --- /dev/null +++ b/addons/pos_sale_margin/__manifest__.py @@ -0,0 +1,18 @@ +# -*- coding: utf-8 -*- +# Part of Odoo. See LICENSE file for full copyright and licensing details. + + +{ + 'name': 'pos_sale_margin', + 'version': '1.1', + 'category': 'Hidden', + 'summary': 'Link module between Point of Sale and Sales Margin', + 'description': """ + +This module adds enable you to view the margin of your Point of Sale orders in the Sales Margin report. +""", + 'depends': ['pos_sale', 'sale_margin'], + 'installable': True, + 'auto_install': True, + 'license': 'LGPL-3', +} diff --git a/addons/pos_sale_margin/report/__init__.py b/addons/pos_sale_margin/report/__init__.py new file mode 100644 index 00000000000..7b0761d5199 --- /dev/null +++ b/addons/pos_sale_margin/report/__init__.py @@ -0,0 +1,4 @@ +# -*- coding: utf-8 -*- +# Part of Odoo. See LICENSE file for full copyright and licensing details. + +from . import sale_report diff --git a/addons/pos_sale_margin/report/sale_report.py b/addons/pos_sale_margin/report/sale_report.py new file mode 100644 index 00000000000..42bd9615d38 --- /dev/null +++ b/addons/pos_sale_margin/report/sale_report.py @@ -0,0 +1,13 @@ +# -*- coding: utf-8 -*- +# Part of Odoo. See LICENSE file for full copyright and licensing details. + +from odoo import models + + +class SaleReport(models.Model): + _inherit = "sale.report" + + def _fill_pos_fields(self, additional_fields): + values = super()._fill_pos_fields(additional_fields) + values['margin'] = 'SUM(l.price_subtotal - l.total_cost / CASE COALESCE(pos.currency_rate, 0) WHEN 0 THEN 1.0 ELSE pos.currency_rate END)' + return values diff --git a/addons/pos_sale_margin/tests/__init__.py b/addons/pos_sale_margin/tests/__init__.py new file mode 100644 index 00000000000..a01c08896d4 --- /dev/null +++ b/addons/pos_sale_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_pos_sale_margin_report diff --git a/addons/pos_sale_margin/tests/test_pos_sale_margin_report.py b/addons/pos_sale_margin/tests/test_pos_sale_margin_report.py new file mode 100644 index 00000000000..5a61bc42e8f --- /dev/null +++ b/addons/pos_sale_margin/tests/test_pos_sale_margin_report.py @@ -0,0 +1,43 @@ +# -*- coding: utf-8 -*- +# Part of Odoo. See LICENSE file for full copyright and licensing details. +import odoo + +from odoo.addons.point_of_sale.tests.common import TestPoSCommon + + +@odoo.tests.tagged('post_install', '-at_install') +class TestPoSSaleMarginReport(TestPoSCommon): + + def setUp(self): + super(TestPoSSaleMarginReport, self).setUp() + self.config = self.basic_config + + def test_pos_sale_margin_report(self): + + product1 = self.create_product('Product 1', self.categ_basic, 150, standard_price=50) + + self.open_new_session() + session = self.pos_session + + self.env['pos.order'].create({ + 'session_id': session.id, + 'lines': [(0, 0, { + 'name': "OL/0001", + 'product_id': product1.id, + 'price_unit': 450, + 'discount': 5.0, + 'qty': 1.0, + 'price_subtotal': 150, + 'price_subtotal_incl': 150, + 'total_cost': 50, + }),], + 'amount_total': 150.0, + 'amount_tax': 0.0, + 'amount_paid': 0.0, + 'amount_return': 0.0, + }) + + # PoS Orders have negative IDs to avoid conflict, so reports[0] will correspond to the newest order + reports = self.env['sale.report'].sudo().search([('product_id', '=', product1.id)], order='id') + + self.assertEqual(reports[0].margin, 100)