OCA-git-bot
5 years ago
6 changed files with 185 additions and 3 deletions
-
5pos_payment_terminal/i18n/pos_payment_terminal.pot
-
1pos_payment_terminal/models/__init__.py
-
63pos_payment_terminal/models/pos_order.py
-
68pos_payment_terminal/static/src/js/pos_payment_terminal.js
-
1pos_payment_terminal/tests/__init__.py
-
50pos_payment_terminal/tests/test_transactions.py
@ -0,0 +1,63 @@ |
|||
# -*- coding: utf-8 -*- |
|||
# © 2018 ACSONE SA/NV |
|||
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). |
|||
from collections import defaultdict |
|||
import logging |
|||
|
|||
from odoo import models, api |
|||
|
|||
_logger = logging.getLogger(__name__) |
|||
|
|||
|
|||
class PosOrder(models.Model): |
|||
_inherit = 'pos.order' |
|||
|
|||
@api.model |
|||
def _match_transactions_to_payments(self, pos_order): |
|||
payments = pos_order['statement_ids'] |
|||
transactions = pos_order['transactions'] |
|||
pos_session = self.env['pos.session'].browse( |
|||
pos_order['pos_session_id']) |
|||
currency_digits = pos_session.currency_id.decimal_places |
|||
card_journals = self.env['account.journal'].search([ |
|||
('id', 'in', [p[2]['journal_id'] for p in payments]), |
|||
('payment_mode', '!=', False), |
|||
]) |
|||
card_payments = [record[2] for record in payments |
|||
if record[2]['journal_id'] in card_journals.ids] |
|||
|
|||
def amount_cents(obj): |
|||
if 'amount_cents' in obj: |
|||
return obj['amount_cents'] |
|||
else: |
|||
return int(round(obj['amount'] * pow(10, currency_digits))) |
|||
|
|||
try: |
|||
for payment, transaction in match(card_payments, transactions, |
|||
key=amount_cents): |
|||
payment['note'] = transaction['reference'] |
|||
except ValueError as e: |
|||
_logger.error("Error matching transactions to payments: %s", |
|||
e.args[0]) |
|||
|
|||
def _process_order(self, pos_order): |
|||
if pos_order.get('transactions'): |
|||
self._match_transactions_to_payments(pos_order) |
|||
return super(PosOrder, self)._process_order(pos_order) |
|||
|
|||
|
|||
def group_by(lists, key): |
|||
count = range(len(lists)) |
|||
d = defaultdict(lambda: tuple([[] for _ in count])) |
|||
for i, objects in enumerate(lists): |
|||
for obj in objects: |
|||
d[key(obj)][i].append(obj) |
|||
return d |
|||
|
|||
|
|||
def match(al, bl, key): |
|||
for key, groups in group_by((al, bl), key).items(): |
|||
if groups[0] and len(groups[0]) != len(groups[1]): |
|||
raise ValueError("Missing value for {!r}".format(key)) |
|||
for val in zip(*groups): |
|||
yield val |
@ -0,0 +1 @@ |
|||
from . import test_transactions |
@ -0,0 +1,50 @@ |
|||
# -*- coding: utf-8 -*- |
|||
# Copyright (C) 2018-TODAY ACSONE SA/NV (<https://www.acsone.eu>). |
|||
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). |
|||
|
|||
from odoo.addons.point_of_sale.tests.common import TestPointOfSaleCommon |
|||
|
|||
|
|||
class TestTransactions(TestPointOfSaleCommon): |
|||
|
|||
def test_matching(self): |
|||
card_journal_id = self.env['account.journal'].create({ |
|||
'name': 'Card Journal', |
|||
'code': 'CARD', |
|||
'type': 'bank', |
|||
'payment_mode': 'card', |
|||
}).id |
|||
cash_journal_id = 0 |
|||
pos_order = { |
|||
'pos_session_id': self.pos_order_session0.id, |
|||
'statement_ids': [ |
|||
(0, 0, { |
|||
'name': 'Payment1', |
|||
'amount': 45.2, |
|||
'journal_id': card_journal_id, |
|||
}), |
|||
(0, 0, { |
|||
'name': 'Payment2', |
|||
'amount': 10.5, |
|||
'journal_id': card_journal_id, |
|||
}), |
|||
(0, 0, { |
|||
'name': 'Payment3', |
|||
'amount': 22.0, |
|||
'journal_id': cash_journal_id, |
|||
}), |
|||
], |
|||
'transactions': [ |
|||
{ |
|||
'reference': 'ABCDE', |
|||
'amount_cents': 1050, |
|||
}, |
|||
{ |
|||
'reference': 'XPTO', |
|||
'amount_cents': 4520, |
|||
}, |
|||
] |
|||
} |
|||
self.env['pos.order']._match_transactions_to_payments(pos_order) |
|||
self.assertEquals(pos_order['statement_ids'][0][2]['note'], 'XPTO') |
|||
self.assertEquals(pos_order['statement_ids'][1][2]['note'], 'ABCDE') |
Write
Preview
Loading…
Cancel
Save
Reference in new issue