more tests and code fixes

This commit is contained in:
tundebabzy 2018-02-26 22:17:06 +01:00
parent b84e9519c2
commit 048bcbf080
2 changed files with 45 additions and 16 deletions

View File

@ -22,10 +22,14 @@ class Subscriptions(Document):
self.set_current_invoice_end()
def set_current_invoice_start(self, date=None):
if not date:
self.current_invoice_start = nowdate()
elif self.trial_period_start and self.is_trialling():
if self.trial_period_start and self.is_trialling():
self.current_invoice_start = self.trial_period_start
elif not date:
current_invoice = self.get_current_invoice()
if not current_invoice:
self.current_invoice_start = nowdate()
else:
self.current_invoice_start = current_invoice.posting_date
def set_current_invoice_end(self):
if self.is_trialling():
@ -100,8 +104,6 @@ class Subscriptions(Document):
def generate_invoice(self):
invoice = self.create_invoice()
invoice.save()
invoice.submit()
self.append('invoices', {'invoice': invoice.name})
self.save() # Validates all over again but we don't mind
self.subscription_updated(invoice)
@ -125,14 +127,13 @@ class Subscriptions(Document):
invoice.taxes_and_charges = self.tax_template
# Due date
if cint(self.days_until_due):
invoice.append(
'payment_schedule',
{
'due_date': add_days(nowdate(), cint(self.days_until_due)),
'invoice_portion': 100
}
)
invoice.append(
'payment_schedule',
{
'due_date': add_days(self.current_invoice_end, cint(self.days_until_due)),
'invoice_portion': 100
}
)
# Discounts
if self.apply_additional_discount:
@ -144,6 +145,9 @@ class Subscriptions(Document):
if self.additional_discount_amount:
invoice.additional_discount_amount = self.additional_discount_amount
invoice.save()
invoice.submit()
return invoice
def get_customer(self, subscriber_name):
@ -160,4 +164,4 @@ class Subscriptions(Document):
return item_names
def subscription_updated(self, invoice):
pass
self.update_subscription_period()

View File

@ -5,7 +5,7 @@ from __future__ import unicode_literals
import frappe
import unittest
from frappe.utils.data import nowdate, add_days, get_last_day
from frappe.utils.data import nowdate, add_days, get_last_day, cint, getdate
class TestSubscriptions(unittest.TestCase):
@ -54,8 +54,33 @@ class TestSubscriptions(unittest.TestCase):
self.assertEqual(subscription.trial_period_start, None)
self.assertEqual(subscription.trial_period_end, None)
self.assertEqual(subscription.current_invoice_start, nowdate())
self.assertEqual(subscription.current_invoice_start, getdate(nowdate()))
self.assertEqual(subscription.current_invoice_end, get_last_day(nowdate()))
self.assertEqual(len(subscription.invoices), 1)
subscription.delete()
def test_create_subscription_trial_with_wrong_dates(self):
subscription = frappe.new_doc('Subscriptions')
subscription.subscriber = '_Test Customer'
subscription.trial_period_end = nowdate()
subscription.trial_period_start = add_days(nowdate(), 30)
subscription.append('plans', {'plan': '_Test Plan Name'})
self.assertRaises(frappe.ValidationError, subscription.save)
def test_subscription_invoice_days_until_due(self):
subscription = frappe.new_doc('Subscriptions')
subscription.subscriber = '_Test Customer'
subscription.append('plans', {'plan': '_Test Plan Name'})
subscription.save()
generated_invoice_name = subscription.invoices[-1].invoice
invoice = frappe.get_doc('Sales Invoice', generated_invoice_name)
self.assertEqual(
invoice.due_date,
add_days(subscription.current_invoice_end, cint(subscription.days_until_due))
)
subscription.delete()