From 9564374aa12e2b068d4bb5736afd38a6eb94416e Mon Sep 17 00:00:00 2001 From: roen-odoo Date: Wed, 27 Dec 2023 11:14:24 +0100 Subject: [PATCH] [FIX] point_of_sale: fix session report payment difference Current behavior: If you use a non cash payment method, with "identify customer" option and register a payment difference at the closing of the session. The difference is not taken into account in the session report. Steps to reproduce: - Activate "identify customer" option on the bank payment method - Open a session - Add a product to the order - Pay the order with the bank payment method - Close the session with a payment difference - Print the session report - Open the report, at the bottom you should see the payment difference but it is not the case opw-3597291 closes odoo/odoo#148753 X-original-commit: 30ba7fd9eb3d1910431cbf543e2ca691f0fdcfc0 Signed-off-by: Joseph Caburnay (jcb) Signed-off-by: Robin Engels (roen) --- .../models/report_sale_details.py | 32 ++++++------ addons/point_of_sale/tests/__init__.py | 1 + .../tests/test_report_session.py | 49 +++++++++++++++++++ 3 files changed, 67 insertions(+), 15 deletions(-) create mode 100644 addons/point_of_sale/tests/test_report_session.py diff --git a/addons/point_of_sale/models/report_sale_details.py b/addons/point_of_sale/models/report_sale_details.py index 3bb32b8b869..9024c16205e 100644 --- a/addons/point_of_sale/models/report_sale_details.py +++ b/addons/point_of_sale/models/report_sale_details.py @@ -126,23 +126,25 @@ class ReportSaleDetails(models.AbstractModel): cash_counted = session.cash_register_balance_end_real is_cash_method = False for payment in payments: - account_payments = self.env['account.payment'].search([('pos_session_id', '=', session.id)]) if payment['session'] == session.id: if not payment['cash']: - for account_payment in account_payments: - if payment['id'] == account_payment.pos_payment_method_id.id: - payment['final_count'] = payment['total'] - payment['money_counted'] = account_payment.amount - payment['money_difference'] = payment['money_counted'] - payment['final_count'] - payment['cash_moves'] = [] - if payment['money_difference'] > 0: - move_name = 'Difference observed during the counting (Profit)' - payment['cash_moves'] = [{'name': move_name, 'amount': payment['money_difference']}] - elif payment['money_difference'] < 0: - move_name = 'Difference observed during the counting (Loss)' - payment['cash_moves'] = [{'name': move_name, 'amount': payment['money_difference']}] - payment['count'] = True - break + ref_value = "Closing difference in %s (%s)" % (payment['name'], session.name) + account_move = self.env['account.move'].search([("ref", "=", ref_value)], limit=1) + if account_move: + payment_method = self.env['pos.payment.method'].browse(payment['id']) + is_loss = any(l.account_id == payment_method.journal_id.loss_account_id for l in account_move.line_ids) + is_profit = any(l.account_id == payment_method.journal_id.profit_account_id for l in account_move.line_ids) + payment['final_count'] = payment['total'] + payment['money_difference'] = -account_move.amount_total if is_loss else account_move.amount_total + payment['money_counted'] = payment['final_count'] + payment['money_difference'] + payment['cash_moves'] = [] + if is_profit: + move_name = 'Difference observed during the counting (Profit)' + payment['cash_moves'] = [{'name': move_name, 'amount': payment['money_difference']}] + elif is_loss: + move_name = 'Difference observed during the counting (Loss)' + payment['cash_moves'] = [{'name': move_name, 'amount': payment['money_difference']}] + payment['count'] = True else: is_cash_method = True previous_session = self.env['pos.session'].search([('id', '<', session.id), ('state', '=', 'closed'), ('config_id', '=', session.config_id.id)], limit=1) diff --git a/addons/point_of_sale/tests/__init__.py b/addons/point_of_sale/tests/__init__.py index d67e84eaf61..243eee192c9 100644 --- a/addons/point_of_sale/tests/__init__.py +++ b/addons/point_of_sale/tests/__init__.py @@ -19,4 +19,5 @@ from . import test_pos_with_fiscal_position from . import test_pos_stock_account from . import test_js from . import test_report_pos_order +from . import test_report_session from . import test_res_config_settings diff --git a/addons/point_of_sale/tests/test_report_session.py b/addons/point_of_sale/tests/test_report_session.py new file mode 100644 index 00000000000..73af0b56fbe --- /dev/null +++ b/addons/point_of_sale/tests/test_report_session.py @@ -0,0 +1,49 @@ +# 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 TestReportSession(TestPoSCommon): + + def setUp(self): + super(TestReportSession, self).setUp() + self.config = self.basic_config + + def test_report_session(self): + + product1 = self.create_product('Product 1', self.categ_basic, 150) + self.open_new_session() + session = self.pos_session + + order = self.env['pos.order'].create({ + 'session_id': session.id, + 'partner_id': self.partner_a.id, + 'lines': [(0, 0, { + 'name': "OL/0001", + 'product_id': product1.id, + 'price_unit': 150, + 'discount': 0, + 'qty': 1.0, + 'price_subtotal': 150, + 'price_subtotal_incl': 150, + }),], + 'amount_total': 150.0, + 'amount_tax': 0.0, + 'amount_paid': 0.0, + 'amount_return': 0.0, + 'last_order_preparation_change': '{}' + }) + + payment_context = {"active_ids": order.ids, "active_id": order.id} + order_payment = self.env['pos.make.payment'].with_context(**payment_context).create({ + 'amount': 150, + 'payment_method_id': self.bank_split_pm1.id + }) + order_payment.with_context(**payment_context).check() + session.action_pos_session_closing_control(bank_payment_method_diffs={self.bank_split_pm1.id: 50}) + + # PoS Orders have negative IDs to avoid conflict, so reports[0] will correspond to the newest order + report = self.env['report.point_of_sale.report_saledetails'].get_sale_details(session_ids=[session.id]) + split_payment_bank = [p for p in report['payments'] if p.get('id', 0) == self.bank_split_pm1.id] + self.assertEqual(split_payment_bank[0]['cash_moves'][0]['amount'], 50)