diff --git a/addons/point_of_sale/models/report_sale_details.py b/addons/point_of_sale/models/report_sale_details.py index 3bb32b8b86912..9024c16205eb9 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 @@ def get_sale_details(self, date_start=False, date_stop=False, config_ids=False, 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 d67e84eaf6143..243eee192c909 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_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 0000000000000..73af0b56fbe26 --- /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)