From dd4f3a6a26b3044f369159146d7837903fe9b1e5 Mon Sep 17 00:00:00 2001 From: Victoria Earl Date: Fri, 29 Sep 2023 16:04:51 -0400 Subject: [PATCH] Update processing fee and refundable calculations The processing fee calculation will now default to calculating based on the transaction's amount, which I think was actually needed in an earlier commit (whoops). Also marks transactions as nonrefundable if they're on a closed receipt or their amount left equals their processing fees. Also updates the "total" text on closed receipts because it looked silly. --- uber/models/commerce.py | 29 +++++++++++++++++++---------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/uber/models/commerce.py b/uber/models/commerce.py index 541653413..5964a5ff0 100644 --- a/uber/models/commerce.py +++ b/uber/models/commerce.py @@ -104,11 +104,11 @@ def all_sorted_items_and_txns(self): @property def total_processing_fees(self): - return sum([txn.calc_partial_processing_fee(txn.amount) for txn in self.refundable_txns]) + return sum([txn.calc_processing_fee(txn.amount) for txn in self.refundable_txns]) @property def remaining_processing_fees(self): - return sum([txn.calc_partial_processing_fee(txn.amount_left) for txn in self.refundable_txns]) + return sum([txn.calc_processing_fee(txn.amount_left) for txn in self.refundable_txns]) @property def open_receipt_items(self): @@ -189,6 +189,10 @@ def txn_total(self): @property def total_str(self): + if self.closed: + return "{} in {}".format(format_currency(abs(self.txn_total / 100)), + "Payments" if self.txn_total >= 0 else "Refunds") + return "{} in {} and {} in {} = {} owe {}".format(format_currency(abs(self.item_total / 100)), "Purchases" if self.item_total >= 0 else "Credit", format_currency(abs(self.txn_total / 100)), @@ -283,7 +287,8 @@ def available_actions(self): @property def refundable(self): - return self.charge_id and self.amount_left and self.amount > 0 + return not self.receipt.closed and self.charge_id and self.amount > 0 and \ + self.amount_left and self.amount_left != self.calc_processing_fee() @property def stripe_url(self): @@ -313,8 +318,9 @@ def stripe_id(self): # Return the most relevant Stripe ID for admins return self.refund_id or self.charge_id or self.intent_id - def get_processing_fee(self): - if self.processing_fee: + @request_cached_property + def total_processing_fee(self): + if self.processing_fee and self.amount == self.txn_total: return self.processing_fee if c.AUTHORIZENET_LOGIN_ID: @@ -326,14 +332,17 @@ def get_processing_fee(self): return intent.charges.data[0].balance_transaction.fee_details[0].amount - def calc_partial_processing_fee(self, refund_amount): + def calc_processing_fee(self, amount=0): from decimal import Decimal - if not refund_amount: - return 0 + if not amount: + if self.processing_fee: + return self.processing_fee + + amount = self.amount - refund_pct = Decimal(refund_amount) / Decimal(self.txn_total) - return refund_pct * Decimal(self.get_processing_fee()) + refund_pct = Decimal(amount) / Decimal(self.txn_total) + return refund_pct * Decimal(self.total_processing_fee) def check_stripe_id(self): # Check all possible Stripe IDs for invalid request errors