diff --git a/pos_payment_entries_globalization/README.rst b/pos_payment_entries_globalization/README.rst new file mode 100644 index 00000000..9200854e --- /dev/null +++ b/pos_payment_entries_globalization/README.rst @@ -0,0 +1,62 @@ +.. image:: https://img.shields.io/badge/licence-AGPL--3-blue.svg + :target: http://www.gnu.org/licenses/agpl-3.0-standalone.html + :alt: License: AGPL-3 + +================================= +POS payment entries globalization +================================= + +This module allows to globalize payment entries created by the POS. + +In some cases, all banking transactions are received in a single bank statement line. +With this module, it's possible to reconcile this one line with the payment globalization line. + + +Configuration +============= + +To configure this module, you need to: + + * Configure globalize account and journal on POS payment method (Account journal). + +A globalization entry is generated for each globalization journal and each globalization account defined on payment methods. + +Usage +===== + +.. image:: https://odoo-community.org/website/image/ir.attachment/5784_f2813bd/datas + :alt: Try me on Runbot + :target: https://runbot.odoo-community.org/runbot/184/8.0 + +For further information, please visit: + + * https://www.odoo.com/forum/help-1 + +Bug Tracker +=========== + +Bugs are tracked on `GitHub Issues `_. +In case of trouble, please check there if your issue has already been reported. +If you spotted it first, help us smashing it by providing a detailed and welcomed feedback +`here `_. + +Credits +======= + +Contributors +------------ + +* Adrien Peiffer + +Maintainer +---------- + +.. image:: https://odoo-community.org/logo.png + :alt: Odoo Community Association + :target: https://odoo-community.org + +This module is maintained by the OCA. + +OCA, or the Odoo Community Association, is a nonprofit organization whose mission is to support the collaborative development of Odoo features and promote its widespread use. + +To contribute to this module, please visit http://odoo-community.org. diff --git a/pos_payment_entries_globalization/__init__.py b/pos_payment_entries_globalization/__init__.py new file mode 100644 index 00000000..bd895fe7 --- /dev/null +++ b/pos_payment_entries_globalization/__init__.py @@ -0,0 +1,4 @@ +# -*- coding: utf-8 -*- +# Copyright 2015 ACSONE SA/NV () +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). +from . import models diff --git a/pos_payment_entries_globalization/__openerp__.py b/pos_payment_entries_globalization/__openerp__.py new file mode 100644 index 00000000..c8d5c01d --- /dev/null +++ b/pos_payment_entries_globalization/__openerp__.py @@ -0,0 +1,21 @@ +# -*- coding: utf-8 -*- +# Copyright 2015 ACSONE SA/NV () +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). +{ + 'name': "POS payment entries globalization", + + 'summary': """ + Globalize POS Payment""", + 'author': 'ACSONE SA/NV,' + 'Odoo Community Association (OCA)', + 'website': "http://acsone.eu", + 'category': 'Point Of Sale', + 'version': '8.0.1.0.0', + 'license': 'AGPL-3', + 'depends': [ + 'point_of_sale', + ], + 'data': [ + 'views/account_journal_view.xml', + ], +} diff --git a/pos_payment_entries_globalization/models/__init__.py b/pos_payment_entries_globalization/models/__init__.py new file mode 100644 index 00000000..378a51d4 --- /dev/null +++ b/pos_payment_entries_globalization/models/__init__.py @@ -0,0 +1,5 @@ +# -*- coding: utf-8 -*- +# Copyright 2015 ACSONE SA/NV () +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). +from . import account_journal +from . import pos_session diff --git a/pos_payment_entries_globalization/models/account_journal.py b/pos_payment_entries_globalization/models/account_journal.py new file mode 100644 index 00000000..2918bbdd --- /dev/null +++ b/pos_payment_entries_globalization/models/account_journal.py @@ -0,0 +1,15 @@ +# -*- coding: utf-8 -*- +# Copyright 2015 ACSONE SA/NV () +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). + +from openerp import models, fields + + +class AccountJournal(models.Model): + _inherit = 'account.journal' + + pos_payment_globalization = fields.Boolean() + pos_payment_globalization_account = fields.Many2one( + comodel_name='account.account') + pos_payment_globalization_journal = fields.Many2one( + comodel_name='account.journal') diff --git a/pos_payment_entries_globalization/models/pos_session.py b/pos_payment_entries_globalization/models/pos_session.py new file mode 100644 index 00000000..b8e824ac --- /dev/null +++ b/pos_payment_entries_globalization/models/pos_session.py @@ -0,0 +1,102 @@ +# -*- coding: utf-8 -*- +# Copyright 2015 ACSONE SA/NV () +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). + +from openerp import models, fields, api, _ +from collections import defaultdict + + +class PosSession(models.Model): + _inherit = 'pos.session' + + @api.multi + def _get_move_lines_for_globalization(self): + """ Get all move lines for globalization by journal-account""" + self.ensure_one() + grouped_move_lines = defaultdict(list) + for st in self.statement_ids: + if st.journal_id.pos_payment_globalization: + # One move per journal and account combination + key = (st.journal_id.pos_payment_globalization_account.id, + st.journal_id.pos_payment_globalization_journal.id) + debit_account_id =\ + st.journal_id.default_debit_account_id.id + lines = st.move_line_ids.filtered( + lambda r: r.account_id.id == debit_account_id) + grouped_move_lines[key].extend(lines) + return grouped_move_lines + + @api.model + def _create_globalization_move(self, journal_id, period_id): + """Create the globalization move""" + entries_vals = { + 'journal_id': journal_id, + 'period_id': period_id, + 'date': fields.Date.today(), + 'name': "%s - %s" % ( + self.name, _("Payment globalization")), + } + return self.env['account.move'].create(entries_vals) + + @api.model + def _create_globalization_counterpart_line(self, debit, credit, account_id, + move): + """Create the globalization counterpart line""" + item_vals = { + 'name': _("Payment globalization counterpart"), + 'credit': credit, + 'debit': debit, + 'account_id': account_id, + 'move_id': move.id + } + return self.env['account.move.line'].create(item_vals) + + @api.model + def _create_reverse_line(self, line_to_reverse, move): + """Create move line the reverse payment line in entries + genereted by pos""" + item_vals = { + 'name': "%s - %s" % ( + line_to_reverse.name, _("Payment globalization")), + 'credit': line_to_reverse.debit, + 'debit': line_to_reverse.credit, + 'account_id': line_to_reverse.account_id.id, + 'move_id': move.id + } + return self.env['account.move.line'].create(item_vals) + + @api.multi + def _generate_globalization_entries(self): + """Generate globalization moves""" + self.ensure_one() + grouped_move_lines = self._get_move_lines_for_globalization() + to_reconcile = [] + period = self.env['account.period'].find() + for key, lines in grouped_move_lines.iteritems(): + global_account_id, global_journal_id = key + move = self._create_globalization_move(global_journal_id, + period.id) + counterpart_debit = 0.0 + counterpart_credit = 0.0 + for line in lines: + counterpart_debit += line.debit + counterpart_credit += line.credit + new_line = self._create_reverse_line(line, move) + # Pair to reconcile : payment line and the reverse line + to_reconcile.append(line + new_line) + if counterpart_debit: + self._create_globalization_counterpart_line( + counterpart_debit, 0.0, global_account_id, move) + if counterpart_credit: + self._create_globalization_counterpart_line( + 0.0, counterpart_credit, global_account_id, move) + for lines in to_reconcile: + lines.reconcile() + + @api.multi + def wkf_action_close(self): + res = super(PosSession, self).wkf_action_close() + for record in self: + # Call the method to generate globalization entries + record._generate_globalization_entries() + return res diff --git a/pos_payment_entries_globalization/static/description/icon.png b/pos_payment_entries_globalization/static/description/icon.png new file mode 100644 index 00000000..3a0328b5 Binary files /dev/null and b/pos_payment_entries_globalization/static/description/icon.png differ diff --git a/pos_payment_entries_globalization/tests/__init__.py b/pos_payment_entries_globalization/tests/__init__.py new file mode 100644 index 00000000..e954b8e9 --- /dev/null +++ b/pos_payment_entries_globalization/tests/__init__.py @@ -0,0 +1,5 @@ +# -*- coding: utf-8 -*- +# Copyright 2015 ACSONE SA/NV () +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). + +from . import test_pos_payment_entries_globalization diff --git a/pos_payment_entries_globalization/tests/test_pos_payment_entries_globalization.py b/pos_payment_entries_globalization/tests/test_pos_payment_entries_globalization.py new file mode 100644 index 00000000..b13dee1a --- /dev/null +++ b/pos_payment_entries_globalization/tests/test_pos_payment_entries_globalization.py @@ -0,0 +1,271 @@ +# -*- coding: utf-8 -*- +# Copyright 2015 ACSONE SA/NV () +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). + +from openerp.tests import common + + +class TestPosPaymentEntriesGlobalization(common.TransactionCase): + + def setUp(self): + super(TestPosPaymentEntriesGlobalization, self).setUp() + self.move_line_obj = self.env['account.move.line'] + self.pos_session_obj = self.env['pos.session'] + self.pos_order_obj = self.env['pos.order'] + self.main_config = self.env.ref('point_of_sale.pos_config_main') + self.payment_method_01 = self.env.ref('account.cash_journal') + self.payment_method_02 = self.env.ref('account.bank_journal') + self.payment_method_02.default_debit_account_id.reconcile = True + self.payment_method_03 = self.env.ref('account.check_journal') + self.payment_method_03.default_debit_account_id.reconcile = True + self.income_account = self.env.ref('account.o_income') + self.income_account_02 = self.income_account.copy() + self.misc_journal = self.env.ref('account.miscellaneous_journal') + self.misc_journal_02 = self.misc_journal.copy() + self.product_01 = self.env.ref('point_of_sale.perrier_50cl') + + def open_session(self): + # I create and open a new session + session = self.pos_session_obj.create( + {'config_id': self.main_config.id}) + ctx = self.env.context.copy() + # context is updated in open_cb + # -> Need to call with old api to give unfrozen context + self.registry['pos.session'].open_cb( + self.cr, self.uid, [session.id], context=ctx) + return session + + def create_order(self, amount, session): + # I create a new order + order_vals = { + 'session_id': session.id, + 'lines': [(0, 0, {'product_id': self.product_01.id, + 'price_unit': amount, + })] + } + return self.pos_order_obj.create(order_vals) + + def test_globalization_0(self): + session = self.open_session() + self.payment_method_02.pos_payment_globalization = True + self.payment_method_02.pos_payment_globalization_account =\ + self.income_account + self.payment_method_02.pos_payment_globalization_journal =\ + self.misc_journal + # I create a new order + order_01 = self.create_order(100, session) + # I pay the created order + payment_data = {'amount': 100, + 'journal': self.payment_method_02.id} + self.pos_order_obj.add_payment(order_01.id, payment_data) + if order_01.test_paid(): + order_01.signal_workflow('paid') + # I create a new order + order_02 = self.create_order(100, session) + # I pay the created order + payment_data = {'amount': 100, + 'journal': self.payment_method_02.id} + self.pos_order_obj.add_payment(order_02.id, payment_data) + if order_02.test_paid(): + order_02.signal_workflow('paid') + # I close the session + session.signal_workflow('close') + move_line = self.move_line_obj.search( + [('account_id', '=', self.income_account.id), + ('journal_id', '=', self.misc_journal.id)]) + # I check that there is only one move line + self.assertEqual(len(move_line.ids), 1) + self.assertAlmostEqual(move_line.debit, 200, 2) + domain = [('move_id', '=', move_line.move_id.id), + ('id', '!=', move_line.id)] + reverse_lines = self.move_line_obj.search(domain) + # I ensure that the move contains reverse lines + self.assertEqual(len(reverse_lines), 2) + # I ensure reverse lines are reconciled + not_reconcile_reverse_lines = reverse_lines.filtered( + lambda r: not r.reconcile_ref) + self.assertEqual(len(not_reconcile_reverse_lines), 0) + + def test_globalization_1(self): + session = self.open_session() + self.payment_method_02.pos_payment_globalization = True + self.payment_method_02.pos_payment_globalization_account =\ + self.income_account + self.payment_method_02.pos_payment_globalization_journal =\ + self.misc_journal + self.payment_method_03.pos_payment_globalization = True + self.payment_method_03.pos_payment_globalization_account =\ + self.income_account_02 + self.payment_method_03.pos_payment_globalization_journal =\ + self.misc_journal + # I create a new order + order_01 = self.create_order(100, session) + # I pay the created order + payment_data = {'amount': 100, + 'journal': self.payment_method_02.id} + self.pos_order_obj.add_payment(order_01.id, payment_data) + if order_01.test_paid(): + order_01.signal_workflow('paid') + # I create a new order + order_02 = self.create_order(100, session) + # I pay the created order + payment_data = {'amount': 100, + 'journal': self.payment_method_03.id} + self.pos_order_obj.add_payment(order_02.id, payment_data) + if order_02.test_paid(): + order_02.signal_workflow('paid') + # I close the session + session.signal_workflow('close') + # I check the first globalization account + move_line = self.move_line_obj.search( + [('account_id', '=', self.income_account.id), + ('journal_id', '=', self.misc_journal.id)]) + # I check that there is only one move line + self.assertEqual(len(move_line.ids), 1) + self.assertAlmostEqual(move_line.debit, 100, 2) + domain = [('move_id', '=', move_line.move_id.id), + ('id', '!=', move_line.id)] + reverse_lines = self.move_line_obj.search(domain) + # I ensure that the move contains reverse lines + self.assertEqual(len(reverse_lines), 1) + # I ensure reverse lines are reconciled + not_reconcile_reverse_lines = reverse_lines.filtered( + lambda r: not r.reconcile_ref) + self.assertEqual(len(not_reconcile_reverse_lines), 0) + # I check the second globalization account + move_line = self.move_line_obj.search( + [('account_id', '=', self.income_account_02.id), + ('journal_id', '=', self.misc_journal.id)]) + # I check that there is only one move line + self.assertEqual(len(move_line.ids), 1) + self.assertAlmostEqual(move_line.debit, 100, 2) + domain = [('move_id', '=', move_line.move_id.id), + ('id', '!=', move_line.id)] + reverse_lines = self.move_line_obj.search(domain) + # I ensure that the move contains reverse lines + self.assertEqual(len(reverse_lines), 1) + # I ensure reverse lines are reconciled + not_reconcile_reverse_lines = reverse_lines.filtered( + lambda r: not r.reconcile_ref) + self.assertEqual(len(not_reconcile_reverse_lines), 0) + + def test_globalization_2(self): + session = self.open_session() + self.payment_method_02.pos_payment_globalization = True + self.payment_method_02.pos_payment_globalization_account =\ + self.income_account + self.payment_method_02.pos_payment_globalization_journal =\ + self.misc_journal + self.payment_method_03.pos_payment_globalization = True + self.payment_method_03.pos_payment_globalization_account =\ + self.income_account_02 + self.payment_method_03.pos_payment_globalization_journal =\ + self.misc_journal + # I create a new order + order_01 = self.create_order(200, session) + # I pay the created order + payment_data = {'amount': 100, + 'journal': self.payment_method_02.id} + self.pos_order_obj.add_payment(order_01.id, payment_data) + payment_data = {'amount': 100, + 'journal': self.payment_method_03.id} + self.pos_order_obj.add_payment(order_01.id, payment_data) + if order_01.test_paid(): + order_01.signal_workflow('paid') + # I close the session + session.signal_workflow('close') + # I check the first globalization account + move_line = self.move_line_obj.search( + [('account_id', '=', self.income_account.id), + ('journal_id', '=', self.misc_journal.id)]) + # I check that there is only one move line + self.assertEqual(len(move_line.ids), 1) + self.assertAlmostEqual(move_line.debit, 100, 2) + domain = [('move_id', '=', move_line.move_id.id), + ('id', '!=', move_line.id)] + reverse_lines = self.move_line_obj.search(domain) + # I ensure that the move contains reverse lines + self.assertEqual(len(reverse_lines), 1) + # I ensure reverse lines are reconciled + not_reconcile_reverse_lines = reverse_lines.filtered( + lambda r: not r.reconcile_ref) + self.assertEqual(len(not_reconcile_reverse_lines), 0) + # I check the second globalization account + move_line = self.move_line_obj.search( + [('account_id', '=', self.income_account_02.id), + ('journal_id', '=', self.misc_journal.id)]) + # I check that there is only one move line + self.assertEqual(len(move_line.ids), 1) + self.assertAlmostEqual(move_line.debit, 100, 2) + domain = [('move_id', '=', move_line.move_id.id), + ('id', '!=', move_line.id)] + reverse_lines = self.move_line_obj.search(domain) + # I ensure that the move contains reverse lines + self.assertEqual(len(reverse_lines), 1) + # I ensure reverse lines are reconciled + not_reconcile_reverse_lines = reverse_lines.filtered( + lambda r: not r.reconcile_ref) + self.assertEqual(len(not_reconcile_reverse_lines), 0) + + def test_globalization_3(self): + session = self.open_session() + self.payment_method_02.pos_payment_globalization = True + self.payment_method_02.pos_payment_globalization_account =\ + self.income_account + self.payment_method_02.pos_payment_globalization_journal =\ + self.misc_journal + self.payment_method_03.pos_payment_globalization = True + self.payment_method_03.pos_payment_globalization_account =\ + self.income_account + self.payment_method_03.pos_payment_globalization_journal =\ + self.misc_journal_02 + # I create a new order + order_01 = self.create_order(100, session) + # I pay the created order + payment_data = {'amount': 100, + 'journal': self.payment_method_02.id} + self.pos_order_obj.add_payment(order_01.id, payment_data) + if order_01.test_paid(): + order_01.signal_workflow('paid') + # I create a new order + order_02 = self.create_order(100, session) + # I pay the created order + payment_data = {'amount': 100, + 'journal': self.payment_method_03.id} + self.pos_order_obj.add_payment(order_02.id, payment_data) + if order_02.test_paid(): + order_02.signal_workflow('paid') + # I close the session + session.signal_workflow('close') + # I check the first globalization journal + move_line = self.move_line_obj.search( + [('account_id', '=', self.income_account.id), + ('journal_id', '=', self.misc_journal.id)]) + # I check that there is only one move line + self.assertEqual(len(move_line.ids), 1) + self.assertAlmostEqual(move_line.debit, 100, 2) + domain = [('move_id', '=', move_line.move_id.id), + ('id', '!=', move_line.id)] + reverse_lines = self.move_line_obj.search(domain) + # I ensure that the move contains reverse lines + self.assertEqual(len(reverse_lines), 1) + # I ensure reverse lines are reconciled + not_reconcile_reverse_lines = reverse_lines.filtered( + lambda r: not r.reconcile_ref) + self.assertEqual(len(not_reconcile_reverse_lines), 0) + # I check the second globalization journal + move_line = self.move_line_obj.search( + [('account_id', '=', self.income_account.id), + ('journal_id', '=', self.misc_journal_02.id)]) + # I check that there is only one move line + self.assertEqual(len(move_line.ids), 1) + self.assertAlmostEqual(move_line.debit, 100, 2) + domain = [('move_id', '=', move_line.move_id.id), + ('id', '!=', move_line.id)] + reverse_lines = self.move_line_obj.search(domain) + # I ensure that the move contains reverse lines + self.assertEqual(len(reverse_lines), 1) + # I ensure reverse lines are reconciled + not_reconcile_reverse_lines = reverse_lines.filtered( + lambda r: not r.reconcile_ref) + self.assertEqual(len(not_reconcile_reverse_lines), 0) diff --git a/pos_payment_entries_globalization/views/account_journal_view.xml b/pos_payment_entries_globalization/views/account_journal_view.xml new file mode 100644 index 00000000..8cd9764d --- /dev/null +++ b/pos_payment_entries_globalization/views/account_journal_view.xml @@ -0,0 +1,19 @@ + + + + + POS Journal (pos_payment_entries_globalization) + account.journal + + + + + + + + + + + + +