Skip to content

Commit

Permalink
Toxify code
Browse files Browse the repository at this point in the history
  • Loading branch information
sbeach92 committed Feb 24, 2024
1 parent 779293b commit 5542d77
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 16 deletions.
4 changes: 2 additions & 2 deletions users/models/custom_invoice.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,5 +68,5 @@ def __str__(self):

# cost is used if not set
def cost_min(self):
cm = self.days / self.subscription.service.days_per_payment * self.subscription.service.cost_min
return cm
cm = self.days / self.subscription.service.days_per_payment * self.subscription.service.cost_min
return cm
37 changes: 23 additions & 14 deletions utils/businesslogic.py
Original file line number Diff line number Diff line change
Expand Up @@ -292,13 +292,12 @@ def _check_transaction_pays_custominvoice(transaction):
)

for invoice in invoices:
#if transaction.amount >= invoice.amount:
invoice_cost_min = invoice.cost_min()
if config.CUSTOM_INVOICE_DYNAMIC_PRICING and transaction.amount >= invoice_cost_min or transaction.amount >= invoice.amount:
subscription = ServiceSubscription.objects.get(
user=invoice.user, id=invoice.subscription.id
)
BusinessLogic._service_paid_by_transaction(subscription, transaction, invoice.days)
BusinessLogic._service_paid_by_transaction(subscription, transaction, invoice.days)
else:
transaction.comment += f"Insufficient amount for invoice {invoice}\n"
transaction.save()
Expand Down Expand Up @@ -386,19 +385,15 @@ def _service_paid_by_transaction(servicesubscription, transaction, add_days):
logger.debug(f"Paying {servicesubscription} and gained {add_days} days more")

# How many days to add to subscription's paid until
days_to_add = timedelta(
days = add_days
)
days_to_add = timedelta(days=add_days)
# First payment - initialize with payment date and add first time bonus days
if not servicesubscription.paid_until:
bonus_days = timedelta(
days=servicesubscription.service.days_bonus_for_first
)
bonus_days = timedelta(days=servicesubscription.service.days_bonus_for_first)
logger.debug(
f"{servicesubscription} paid for first time, adding bonus of {bonus_days}"
)
transaction.comment += f"First payment of {servicesubscription} - added {bonus_days.days} bonus days.\n"

days_to_add = days_to_add + bonus_days
servicesubscription.paid_until = transaction.date

Expand Down Expand Up @@ -444,12 +439,26 @@ def _service_paid_by_transaction(servicesubscription, transaction, add_days):
if paid_servicesubscription.paid_until > transaction.date:
child_date = paid_servicesubscription.paid_until - transaction.date
child_days = child_date.days
extra_days = added_days.days - servicesubscription.service.days_per_payment + paid_servicesubscription.service.days_per_payment - child_days
logger.debug(f"Child prosess add days calculted by {added_days.days} - {servicesubscription.service.days_per_payment} + {paid_servicesubscription.service.days_per_payment} - {child_days} and gained {extra_days} days more")
#Calculate child subscription payment to happen at same time that latest parrent subsciption,
#useful with custominvoices that pays Parent subscription multiple times

extra_days = (
added_days.days
- servicesubscription.service.days_per_payment
+ paid_servicesubscription.service.days_per_payment
- child_days
)

logger.debug(
f"""Child prosess add days calculted by
{added_days.days}
- {servicesubscription.service.days_per_payment}
+ {paid_servicesubscription.service.days_per_payment}
- {child_days}
= gained {extra_days} days more"""
)
# Calculate child subscription payment to happen at same time that latest parrent subsciption,
# useful with custominvoices that pays Parent subscription multiple times
BusinessLogic._service_paid_by_transaction(paid_servicesubscription, transaction, extra_days)

BusinessLogic._check_servicesubscription_state(
paid_servicesubscription
)

0 comments on commit 5542d77

Please sign in to comment.