From b72aac67294ae91899b4ff520cdef2512c6b579a Mon Sep 17 00:00:00 2001 From: tundebabzy Date: Mon, 26 Feb 2018 15:56:47 +0100 Subject: [PATCH] adds validation: - ensure trial period dates are in order --- .../doctype/subscriptions/subscriptions.py | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/erpnext/accounts/doctype/subscriptions/subscriptions.py b/erpnext/accounts/doctype/subscriptions/subscriptions.py index 98f154860c..435c364d99 100644 --- a/erpnext/accounts/doctype/subscriptions/subscriptions.py +++ b/erpnext/accounts/doctype/subscriptions/subscriptions.py @@ -42,3 +42,33 @@ class Subscriptions(Document): self.status = 'Active' # todo: then generate new invoice + def is_past_grace_period(self): + current_invoice = self.get_current_invoice() + if self.current_invoice_is_past_due(current_invoice): + grace_period = cint(SUBSCRIPTION_SETTINGS.grace_period) + + return nowdate() > add_days(current_invoice.due_date, grace_period) + + def current_invoice_is_past_due(self, current_invoice=None): + if not current_invoice: + current_invoice = self.get_current_invoice() + + if not current_invoice: + return False + else: + return nowdate() > current_invoice.due_date + + def is_new_subscription(self): + return len(self.invoices) == 0 + + def validate(self): + self.validate_trial_period() + + def validate_trial_period(self): + if self.trial_period_start and self.trial_period_end: + if getdate(self.trial_period_end) > getdate(self.trial_period_start): + frappe.throw(_('Trial Period End Date Cannot be before Trial Period Start Date')) + + elif self.trial_period_start or self.trial_period_end: + frappe.throw(_('Both Trial Period Start Date and Trial Period End Date must be set')) +