Skip to content

Commit

Permalink
Finetuned child service payment function to support custom invoices
Browse files Browse the repository at this point in the history
Child service is paid by calling funcion again. So now if child
service has childs, they get paid.

Updated calcultaion so that multpile custominvoices
wont break basic idea of child payments.
  • Loading branch information
sbeach92 committed Feb 20, 2024
1 parent 30d4c59 commit de291c5
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 21 deletions.
39 changes: 19 additions & 20 deletions utils/businesslogic.py
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,8 @@ def _service_paid_by_transaction(servicesubscription, transaction, add_days):
"""
translation.activate(servicesubscription.user.language)

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
Expand All @@ -392,7 +394,11 @@ def _service_paid_by_transaction(servicesubscription, transaction, add_days):
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."
if transaction.comment != "":
transaction.comment = transaction.comment + f"\r\nFirst payment of {servicesubscription} - added {bonus_days.days} bonus days."
else:
transaction.comment = f"First payment of {servicesubscription} - added {bonus_days.days} bonus days."

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

Expand Down Expand Up @@ -431,25 +437,18 @@ def _service_paid_by_transaction(servicesubscription, transaction, add_days):
if paid_servicesubscription.state == ServiceSubscription.SUSPENDED:
logger.debug("Service is suspended - no action")
else:
extra_days = timedelta(
#Calculate child subscription payment to happen same time that latest parrent subsciption,
#useful with custominvoices that pays Parent subscription multiple times
days = add_days - servicesubscription.service.days_per_payment + paid_servicesubscription.service.days_per_payment
)
paid_servicesubscription.paid_until = transaction.date + extra_days
paid_servicesubscription.last_payment = transaction
paid_servicesubscription.save()
servicesubscription.user.log(
_(
"%(servicename)s is now paid until %(until)s due to %(anotherservicename)s was paid"
)
% {
"servicename": str(paid_servicesubscription),
"until": str(paid_servicesubscription.paid_until),
"anotherservicename": str(servicesubscription.service),
}
)

added_days = servicesubscription.paid_until - transaction.date
child_days = 0
if paid_servicesubscription.paid_until:
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
BusinessLogic._service_paid_by_transaction(paid_servicesubscription, transaction, extra_days)

BusinessLogic._check_servicesubscription_state(
paid_servicesubscription
)
2 changes: 1 addition & 1 deletion www/templates/www/user.html
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ <h2>{% trans 'Bank transactions' %}</h2>
<td>{{ transaction.reference_number }}</td>
<td>{{ transaction.sender|default_if_none:"" }}</td>
<td>{{ transaction.message }}</td>
<td>{{ transaction.comment|default_if_none:"" }}</td>
<td>{{ transaction.comment|default_if_none:""|linebreaks}}</td>
</tr>
{% endfor %}
</table>
Expand Down

0 comments on commit de291c5

Please sign in to comment.