update tests and more tweaks

This commit is contained in:
tundebabzy 2018-03-01 10:47:00 +01:00
parent 7eabbd93e1
commit b91ec79494
2 changed files with 21 additions and 28 deletions

View File

@ -228,8 +228,7 @@ class Subscriptions(Document):
elif self.status in ['Past Due Date', 'Unpaid']:
self.process_for_past_due_date()
if self.status != 'Canceled':
self.save()
self.save()
def process_for_active(self):
if getdate(nowdate()) > getdate(self.current_invoice_end) and not self.has_outstanding_invoice():
@ -240,6 +239,14 @@ class Subscriptions(Document):
if self.current_invoice_is_past_due() and getdate(nowdate()) > getdate(self.current_invoice_end):
self.status = 'Past Due Date'
if self.cancel_at_period_end and getdate(nowdate()) > self.current_invoice_end:
self.cancel_subscription_at_period_end()
def cancel_subscription_at_period_end(self):
self.status = 'Canceled'
if not self.cancelation_date:
self.cancelation_date = nowdate()
def process_for_past_due_date(self):
current_invoice = self.get_current_invoice()
if not current_invoice:
@ -274,10 +281,10 @@ class Subscriptions(Document):
def restart_subscription(self):
"""
This sets the subscription as active. The subscription will be made to be like a new
subscription but new trial periods will not be allowed.
subscription.
"""
self.status = 'Active'
self.start = nowdate()
self.db_set('start', nowdate())
self.update_subscription_period(nowdate())
self.invoices = []
self.save()
@ -298,4 +305,4 @@ def restart_subscription(name):
@frappe.whitelist()
def get_subscription_updates(name):
subscription = frappe.get_doc('Subscriptions', name)
subscription.process()
subscription.process()

View File

@ -106,14 +106,11 @@ class TestSubscriptions(unittest.TestCase):
def test_invoice_is_generated_at_end_of_billing_period(self):
subscription = frappe.new_doc('Subscriptions')
subscription.subscriber = '_Test Customer'
subscription.start = '2018-01-01'
subscription.append('plans', {'plan': '_Test Plan Name'})
subscription.insert()
self.assertEqual(subscription.status, 'Active')
subscription.set_current_invoice_start('2018-01-01')
subscription.set_current_invoice_end()
self.assertEqual(subscription.current_invoice_start, '2018-01-01')
self.assertEqual(subscription.current_invoice_end, '2018-01-31')
subscription.process()
@ -127,9 +124,8 @@ class TestSubscriptions(unittest.TestCase):
subscription = frappe.new_doc('Subscriptions')
subscription.subscriber = '_Test Customer'
subscription.append('plans', {'plan': '_Test Plan Name'})
subscription.start = '2018-01-01'
subscription.insert()
subscription.set_current_invoice_start('2018-01-01')
subscription.set_current_invoice_end()
subscription.process() # generate first invoice
self.assertEqual(len(subscription.invoices), 1)
self.assertEqual(subscription.status, 'Past Due Date')
@ -158,9 +154,8 @@ class TestSubscriptions(unittest.TestCase):
subscription = frappe.new_doc('Subscriptions')
subscription.subscriber = '_Test Customer'
subscription.append('plans', {'plan': '_Test Plan Name'})
subscription.start = '2018-01-01'
subscription.insert()
subscription.set_current_invoice_start('2018-01-01')
subscription.set_current_invoice_end()
subscription.process() # generate first invoice
self.assertEqual(subscription.status, 'Past Due Date')
@ -182,9 +177,8 @@ class TestSubscriptions(unittest.TestCase):
subscription = frappe.new_doc('Subscriptions')
subscription.subscriber = '_Test Customer'
subscription.append('plans', {'plan': '_Test Plan Name'})
subscription.start = '2018-01-01'
subscription.insert()
subscription.set_current_invoice_start('2018-01-01')
subscription.set_current_invoice_end()
subscription.process() # generate first invoice
self.assertEqual(subscription.status, 'Past Due Date')
@ -202,9 +196,8 @@ class TestSubscriptions(unittest.TestCase):
subscription.subscriber = '_Test Customer'
subscription.append('plans', {'plan': '_Test Plan Name'})
subscription.days_until_due = 10
subscription.start = get_datetime_str(add_to_date(nowdate(), months=-1))
subscription.insert()
subscription.set_current_invoice_start(get_datetime_str(add_to_date(nowdate(), months=-1)))
subscription.set_current_invoice_end()
subscription.process() # generate first invoice
self.assertEqual(len(subscription.invoices), 1)
self.assertEqual(subscription.status, 'Active')
@ -220,9 +213,8 @@ class TestSubscriptions(unittest.TestCase):
subscription = frappe.new_doc('Subscriptions')
subscription.subscriber = '_Test Customer'
subscription.append('plans', {'plan': '_Test Plan Name'})
subscription.start = '2018-01-01'
subscription.insert()
subscription.set_current_invoice_start('2018-01-01')
subscription.set_current_invoice_end()
subscription.process() # generate first invoice
self.assertEqual(subscription.status, 'Past Due Date')
@ -287,9 +279,8 @@ class TestSubscriptions(unittest.TestCase):
subscription = frappe.new_doc('Subscriptions')
subscription.subscriber = '_Test Customer'
subscription.append('plans', {'plan': '_Test Plan Name'})
subscription.start = '2018-01-01'
subscription.insert()
subscription.set_current_invoice_start('2018-01-01')
subscription.set_current_invoice_end()
subscription.process() # generate first invoice
invoices = len(subscription.invoices)
@ -322,9 +313,8 @@ class TestSubscriptions(unittest.TestCase):
subscription = frappe.new_doc('Subscriptions')
subscription.subscriber = '_Test Customer'
subscription.append('plans', {'plan': '_Test Plan Name'})
subscription.start = '2018-01-01'
subscription.insert()
subscription.set_current_invoice_start('2018-01-01')
subscription.set_current_invoice_end()
subscription.process() # generate first invoice
self.assertEqual(subscription.status, 'Past Due Date')
@ -360,9 +350,8 @@ class TestSubscriptions(unittest.TestCase):
subscription = frappe.new_doc('Subscriptions')
subscription.subscriber = '_Test Customer'
subscription.append('plans', {'plan': '_Test Plan Name'})
subscription.start = '2018-01-01'
subscription.insert()
subscription.set_current_invoice_start('2018-01-01')
subscription.set_current_invoice_end()
subscription.process() # generate first invoice
self.assertEqual(subscription.status, 'Past Due Date')
@ -384,6 +373,3 @@ class TestSubscriptions(unittest.TestCase):
settings.cancel_after_grace = default_grace_period_action
settings.save()
subscription.delete()
def test_subscription_creation_with_multiple_plans(self):
pass