From 01461c9d9ac4965e72e6662c0fdfb7f8b5915ad0 Mon Sep 17 00:00:00 2001 From: Pierrick Brun Date: Thu, 29 Dec 2022 17:34:26 +0100 Subject: [PATCH 1/3] [FIX] SO transaction amount: remove paid amount --- shopinvader_payment/models/sale_order.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/shopinvader_payment/models/sale_order.py b/shopinvader_payment/models/sale_order.py index d5ac8af5..f93d8b26 100644 --- a/shopinvader_payment/models/sale_order.py +++ b/shopinvader_payment/models/sale_order.py @@ -19,8 +19,9 @@ def _invader_prepare_payment_transaction_data(self, acquirer_id): % (acquirer_id.name, self.shopinvader_backend_id.name) ) self.ensure_one() + paid_amount = sum(self.transaction_ids.filtered(lambda l: l.state in ["authorized", "done"]).mapped("amount")) vals = { - "amount": self.amount_total, + "amount": self.amount_total - paid_amount, "currency_id": self.currency_id.id, "partner_id": self.partner_id.id, "acquirer_id": acquirer_id.id, From ac4087e361b19de3e35b9e9e544408443f2342a8 Mon Sep 17 00:00:00 2001 From: Pierrick Brun Date: Thu, 5 Jan 2023 15:57:54 +0100 Subject: [PATCH 2/3] fixup! [FIX] SO transaction amount: remove paid amount --- .../services/abstract_payable_sale.py | 32 ++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/shopinvader_payment/services/abstract_payable_sale.py b/shopinvader_payment/services/abstract_payable_sale.py index 90d40b4a..3c145bbc 100644 --- a/shopinvader_payment/services/abstract_payable_sale.py +++ b/shopinvader_payment/services/abstract_payable_sale.py @@ -3,6 +3,8 @@ # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). +from odoo.tools.float_utils import float_round + from odoo.addons.component.core import AbstractComponent @@ -13,6 +15,9 @@ class AbstractPayableSaleService(AbstractComponent): def _get_available_payment_methods(self, sale): return sale.shopinvader_backend_id.payment_method_ids + def _get_existing_transactions(self, sale): + return sale.transaction_ids + def _get_shopinvader_payment_data(self, sale): """ Specific method to shopinvader to retrieve the payment dict information @@ -25,15 +30,40 @@ def _get_shopinvader_payment_data(self, sale): :return: """ payment_methods = self._get_available_payment_methods(sale) + currency = sale.currency_id + transactions = self._get_existing_transactions(sale) + amount_paid = float_round(sum(transactions.filtered(lambda l: l.state in ["authorized", "done"]).mapped("amount")), precision_rounding=currency.rounding) values = { "available_methods": { "count": len(payment_methods), "items": self._get_payment_method_data(payment_methods), }, - "amount": sale.amount_total, + "existing_transactions": { + "count": len(transactions), + "items": self._get_transaction_data(transactions), + }, + "amount": sale.amount_total - amount_paid, + "amount_paid": amount_paid, + "amount_total": sale.amount_total, } return values + def _get_transaction_data(self, transactions): + res = [] + for transaction in transactions: + res.append( + { + "id": transaction.id, + "name": transaction.reference, + "payment_method_id": transaction.acquirer_id.id, + "payment_method_name": transaction.acquirer_id.name, + "date": transaction.date, + "amount": transaction.amount, + "state": transaction.state, + } + ) + return res + def _get_payment_method_data(self, methods): res = [] for method in methods: From e5fa201e4b1334bb6d4eaca3454810e950b61bcd Mon Sep 17 00:00:00 2001 From: Pierrick Brun Date: Fri, 6 Jan 2023 16:13:27 +0100 Subject: [PATCH 3/3] fixup! fixup! [FIX] SO transaction amount: remove paid amount --- .../payment_transaction_event_listerner.py | 5 ++++- shopinvader_payment/models/payment_transaction.py | 14 ++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/shopinvader_payment/components/payment_transaction_event_listerner.py b/shopinvader_payment/components/payment_transaction_event_listerner.py index b6995afd..8897b6f9 100644 --- a/shopinvader_payment/components/payment_transaction_event_listerner.py +++ b/shopinvader_payment/components/payment_transaction_event_listerner.py @@ -58,4 +58,7 @@ def _confirm_and_invalidate_session(self, sale_order): # end of awful code .... def on_payment_transaction_done(self, sale_order, transaction): - self._confirm_and_invalidate_session(sale_order) + # TODO: remove _confirm_and_invalidate_session if everything works + #self._confirm_and_invalidate_session(sale_order) + # instantly post_process_order + transaction._post_process_after_done() diff --git a/shopinvader_payment/models/payment_transaction.py b/shopinvader_payment/models/payment_transaction.py index 46793acd..83283a0c 100644 --- a/shopinvader_payment/models/payment_transaction.py +++ b/shopinvader_payment/models/payment_transaction.py @@ -13,3 +13,17 @@ def _get_invader_payables(self): if self.sale_order_ids: return self.sale_order_ids return super(PaymentTransaction, self)._get_invader_payables() + + def _check_amount_and_confirm_order(self): + self.ensure_one() + for order in self.sale_order_ids.filtered( + lambda so: so.state in ("draft", "sent") + ): + transactions = order.transaction_ids + done_transactions = transactions.filtered(lambda t: t.state == "done") + paid_amount = sum(done_transactions.mapped("amount")) + if order.currency_id.compare_amounts(order.amount_total, paid_amount) == 0: + order.with_context(send_email=True).action_confirm() + unfinished_transactions = transactions - done_transactions + unfinished_transactions._set_transaction_cancel() + return super()._check_amount_and_confirm_order()