Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FIX] 14.0 SO transaction amount: remove paid amount #79

Open
wants to merge 3 commits into
base: 14.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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()
14 changes: 14 additions & 0 deletions shopinvader_payment/models/payment_transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
3 changes: 2 additions & 1 deletion shopinvader_payment/models/sale_order.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
32 changes: 31 additions & 1 deletion shopinvader_payment/services/abstract_payable_sale.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand All @@ -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
Expand All @@ -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:
Expand Down