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

Custom invoice payment is carried by _service_paid_by_transaction function #470

Closed
wants to merge 15 commits into from
Closed
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
19 changes: 19 additions & 0 deletions drfx/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,11 @@
DEFAULT_ACCOUNT_SERVICE = 2

CUSTOM_INVOICE_REFERENCE_BASE = 10000
CUSTOM_INVOICE_DYNAMIC_PRICING = False
CUSTOM_INVOICE_ALLOW_MULTIPLE_PAYMENTS = False

SERVICE_INVOICE_REFERENCE_BASE = 20000
SERVICE_INVOICE_ALLOW_SEPARATE_CHILD_PAYMENT = False

MEMBERSHIP_APPLICATION_NOTIFY_ADDRESS = "example@example.com"

Expand Down Expand Up @@ -386,4 +390,19 @@
# "ACCOUNT_IBAN": (ACCOUNT_IBAN, "IBAN of the association's bank account"),
# "ACCOUNT_BIC": (ACCOUNT_BIC, "BIC of the association's bank account"),
# "ACCOUNT_NAME": (ACCOUNT_NAME, "Name of the association's bank account"),
"CUSTOM_INVOICE_DYNAMIC_PRICING": (
CUSTOM_INVOICE_DYNAMIC_PRICING,
"Allow dynamic pricing when paying with custom invoice",
bool,
),
"CUSTOM_INVOICE_ALLOW_MULTIPLE_PAYMENTS": (
CUSTOM_INVOICE_ALLOW_MULTIPLE_PAYMENTS,
"Allow custom invoice to be used again",
bool,
),
"SERVICE_INVOICE_ALLOW_SEPARATE_CHILD_PAYMENT": (
SERVICE_INVOICE_ALLOW_SEPARATE_CHILD_PAYMENT,
"Allow child service to be paid intependently",
bool,
),
}
9 changes: 9 additions & 0 deletions users/models/custom_invoice.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,12 @@ def __str__(self):
"amount": self.amount,
"reference": self.reference_number,
}

# cost is used if not set
def cost_min(self):
if self.subscription.service.cost_min:
cost_min = self.subscription.service.cost_min
else:
cost_min = self.subscription.service.cost
cm = self.days / self.subscription.service.days_per_payment * cost_min
return cm
342 changes: 342 additions & 0 deletions users/tests/test_payments.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,342 @@
from django.contrib.auth import get_user_model
from django.test import TestCase

from drfx import config
from datetime import date, timedelta
from utils import referencenumber
from utils.businesslogic import BusinessLogic

from users.models import (
BankTransaction,
CustomInvoice,
CustomUser,
MemberService,
ServiceSubscription,
)


class PaymentServices(TestCase):
"""
Tests for payment of services and custom invoices
"""

parent_ref = referencenumber.generate(123)
child_ref = referencenumber.generate(456)
grandchild_ref = referencenumber.generate(789)
custom_invoice_ref = referencenumber.generate(101112)

def setUp(self):
"""
Generate test user, test services and test
"""
self.user = get_user_model().objects.create_customuser(
first_name="FirstName",
last_name="LastName",
email="user1@example.com",
birthday=date.today(),
municipality="City",
nick="user1",
phone="+358123123",
)

self.grandchild_service = MemberService(
name="grandchild_service",
cost=10,
days_per_payment=70,
days_bonus_for_first=20,
)
self.grandchild_service.save()

self.child_service = MemberService(
name="child_service",
cost=10,
days_per_payment=60,
days_bonus_for_first=20,
pays_also_service=self.grandchild_service,
)
self.child_service.save()

self.parent_service = MemberService(
name="parent_service",
cost=30,
days_per_payment=30,
days_bonus_for_first=10,
pays_also_service=self.child_service,
)
self.parent_service.save()

self.parent_subscription = ServiceSubscription(
user=self.user,
service=self.parent_service,
state=ServiceSubscription.ACTIVE,
reference_number=self.parent_ref,
)
self.parent_subscription.save()

self.child_subscription = ServiceSubscription(
user=self.user,
service=self.child_service,
state=ServiceSubscription.ACTIVE,
reference_number=self.child_ref,
)
self.child_subscription.save()

self.grandchild_subscription = ServiceSubscription(
user=self.user,
service=self.grandchild_service,
state=ServiceSubscription.ACTIVE,
reference_number=self.grandchild_ref,
)
self.grandchild_subscription.save()

self.custom_invoice = CustomInvoice(
reference_number=self.custom_invoice_ref,
user=self.user,
days=100,
amount=120,
subscription=self.parent_subscription,
)
self.custom_invoice.save()

def test_static_parent_payment(self):
#
# First test parent payment pays all services
#

amounts = [15, 30, 30, 30, 15]
# [not_enough_money, extradays, days_per_payment, days_per_payment, not_enough_money]
e_p_results = [0, 40, 70, 100, 100]
# virtual_payment = parent.daysleft - parent_days_per_payment + child_days_per_payment
# [not_enough_money, virtual_payment + extradays, virtual_payment, virtual_payment, not_enough_money]
e_c_results = [0, 90, 100, 130, 130]
# [not_enough_money, virtual_payment + extradays, virtual_payment<last=last, virtual_payment, not_enough_money]
e_gc_results = [0, 120, 120, 140, 140]
refs = [self.parent_ref, self.child_ref, self.grandchild_ref]

results = self.payment(amounts, refs, self.user)
self.assertNotEqual(results, False)
p_results, c_results, gc_results = results

self.assertEqual(p_results, e_p_results)
self.assertEqual(c_results, e_c_results)
self.assertEqual(gc_results, e_gc_results)

#
# Then test custom invoice payment with one payment allowed
#

amounts = [50, 120, 120, 120, 50]
refs = [self.custom_invoice_ref, self.child_ref, self.grandchild_ref]
config.CUSTOM_INVOICE_DYNAMIC_PRICING = False
config.CUSTOM_INVOICE_ALLOW_MULTIPLE_PAYMENTS = False

# add custominvoice days from previous test, just one addition possible
e_p_results = [100, 200, 200, 200, 200]
e_c_results = [130, 230, 230, 230, 230]
e_gc_results = [140, 240, 240, 240, 240]

results = self.payment(amounts, refs, self.user)
self.assertNotEqual(results, False)
p_results, c_results, gc_results = results

self.assertEqual(p_results, e_p_results)
self.assertEqual(c_results, e_c_results)
self.assertEqual(gc_results, e_gc_results)

# remove custominvoices, to easily run paymentfunction
CustomInvoiceTransactions = BankTransaction.objects.filter(
reference_number=self.custom_invoice_ref
)
for CustomInvoiceTransaction in CustomInvoiceTransactions:
CustomInvoiceTransaction.delete()
BusinessLogic.updateuser(self.user)

#
# then tests multiple custominvoice payments allowed
#
config.CUSTOM_INVOICE_ALLOW_MULTIPLE_PAYMENTS = True
# add custom invoice days from previous invoice payment
e_p_results = [200, 300, 400, 500, 500]
e_c_results = [230, 330, 430, 530, 530]
e_gc_results = [240, 340, 440, 540, 540]

results = self.payment(amounts, refs, self.user)
self.assertNotEqual(results, False)

p_results, c_results, gc_results = results

self.assertEqual(p_results, e_p_results)
self.assertEqual(c_results, e_c_results)
self.assertEqual(gc_results, e_gc_results)

def test_dynamic_parent_payment(self):
#
# Test parent payment when cost_min is defined
#
self.parent_service.cost_min = 15
self.parent_service.save()
refs = [self.parent_ref]
amounts = [10, 15, 30, 15, 10]
e_p_results = [0, 40, 70, 100, 100]

results = self.payment(amounts, refs, self.user)
self.assertNotEqual(results, False)

p_results = results
self.assertEqual(p_results, e_p_results)

def test_dynamic_custominvoice(self):
#
# First test with custominvoice without minimum payment defined to parent service.
# Minimium should be nominal, in testcase 1€ per day
#

amounts = [50, 100, 100, 100, 50]
refs = [self.custom_invoice_ref, self.child_ref, self.grandchild_ref]

config.CUSTOM_INVOICE_ALLOW_MULTIPLE_PAYMENTS = True
config.CUSTOM_INVOICE_DYNAMIC_PRICING = True
# add custom invoice days from previous invoice payment
e_p_results = [0, 110, 210, 310, 310]
e_c_results = [0, 160, 240, 340, 340]
e_gc_results = [0, 190, 250, 350, 350]

results = self.payment(amounts, refs, self.user)
self.assertNotEqual(results, False)

p_results, c_results, gc_results = results

self.assertEqual(p_results, e_p_results)
self.assertEqual(c_results, e_c_results)
self.assertEqual(gc_results, e_gc_results)

CustomInvoiceTransactions = BankTransaction.objects.filter(
reference_number=self.custom_invoice_ref
)
for CustomInvoiceTransaction in CustomInvoiceTransactions:
CustomInvoiceTransaction.delete()

# reduce minimum to 50%
self.parent_service.cost_min = 15
self.parent_service.save()

amounts = [40, 50, 50, 50, 40]

# add custom invoice days from previous invoice payment
e_p_results = [310, 410, 510, 610, 610]
e_c_results = [340, 440, 540, 640, 640]
e_gc_results = [350, 450, 550, 650, 650]

results = self.payment(amounts, refs, self.user)
self.assertNotEqual(results, False)

p_results, c_results, gc_results = results

self.assertEqual(p_results, e_p_results)
self.assertEqual(c_results, e_c_results)
self.assertEqual(gc_results, e_gc_results)

def parent_payment_parent_suspended(self):
self.parent_subscription.state = ServiceSubscription.SUSPENDED
self.parent_subscription.save()
amounts = [15, 30, 30, 30, 15]
# [Active parent subscription prevent payment]
e_results = [0, 0, 0, 0, 0]
refs = [self.parent_ref]
results = self.payment(amounts, refs, self.user)
self.assertEqual(results, e_results)

def test_child_payment_parent_active(self):
config.SERVICE_INVOICE_ALLOW_SEPARATE_CHILD_PAYMENT = False
amounts = [5, 10, 10, 10, 5]
# [Active parent subscription prevent payment]
e_results = [0, 0, 0, 0, 0]
refs = [self.child_ref]
results = self.payment(amounts, refs, self.user)
self.assertEqual(results, e_results)

# Delete child transactions to retest easily
ChildTransactions = BankTransaction.objects.filter(
reference_number=self.child_ref
)
for ChildTransaction in ChildTransactions:
ChildTransaction.delete()

config.SERVICE_INVOICE_ALLOW_SEPARATE_CHILD_PAYMENT = True
e_results = [0, 80, 140, 200, 200]
results = self.payment(amounts, refs, self.user)
self.assertEqual(results, e_results)

def test_child_payment_parent_deleted(self):
amounts = [5, 10, 10, 10, 5]
# Remove parent subscription
subscriptions = ServiceSubscription.objects.filter(
reference_number=self.parent_ref
)
subscriptions[0].delete()

# [not_enough_money, extradays, days_per_payment, days_per_payment, not_enough_money]
e_results = [0, 80, 140, 200, 200]
refs = [self.child_ref]
results = self.payment(amounts, refs, self.user)
self.assertEqual(results, e_results)

@staticmethod
def payment(amounts, refs, user):
# create payments with reference of refs[0] with amounts[] for user
# returns days_left for ref after each amount paid
# eg payment([10, 10, 10],[123, 456, 789, 101],user)
# returns [20, 40, 60]
# [25, 45, 65]
# [30, 50, 70]
# [40, 60, 100]
n = -1
results = []

for amount in amounts:
deltad = timedelta(days=n)
n += 1
transaction = BankTransaction(
reference_number=refs[0],
archival_reference=refs[0] + n,
date=date.today() + deltad,
amount=amount,
sender=user.first_name,
)
transaction.save()
BusinessLogic.new_transaction(transaction)
BusinessLogic.updateuser(user)

results_row = []
for ref in refs:
subscriptions = ServiceSubscription.objects.filter(reference_number=ref)
custominvoices = CustomInvoice.objects.filter(reference_number=ref)

if len(subscriptions) > 0:
for subscription in subscriptions:
results_row.append(subscription.days_left())

elif len(custominvoices) > 0:
for custominvoice in custominvoices:
results_row.append(custominvoice.subscription.days_left())
else:
return False

results.append(results_row)
transposed_results = [
[results[j][i] for j in range(len(results))] for i in range(len(results[0]))
]
if len(transposed_results) == 1:
result = [
transposed_results[0][i] for i in range(len(transposed_results[0]))
]
return result
else:
return transposed_results

def tearDown(self):
CustomInvoice.objects.all().delete()
CustomUser.objects.all().delete()
MemberService.objects.all().delete()
ServiceSubscription.objects.all().delete()
BankTransaction.objects.all().delete()
Loading
Loading