recurring invoice : quarterly, half-yearly and yearly

This commit is contained in:
Nabin Hait 2012-06-17 21:48:33 +05:30
parent 3878b2a1ac
commit 8eadf09e99
3 changed files with 36 additions and 13 deletions

View File

@ -484,11 +484,13 @@ def manage_recurring_invoices():
Create recurring invoices on specific date by copying the original one Create recurring invoices on specific date by copying the original one
and notify the concerned people and notify the concerned people
""" """
rv = webnotes.conn.sql("""select name, recurring_id from `tabSales Invoice` where ifnull(convert_into_recurring_invoice, 0) = 1 rv = webnotes.conn.sql("""select name, recurring_id from `tabSales Invoice` \
and next_date = %s and next_date <= ifnull(end_date, '2199-12-31') and docstatus=1""", nowdate()) where ifnull(convert_into_recurring_invoice, 0) = 1 and next_date = %s \
and next_date <= ifnull(end_date, '2199-12-31') and docstatus=1""", nowdate())
for d in rv: for d in rv:
if not webnotes.conn.sql("""select name from `tabSales Invoice` where posting_date = %s and recurring_id = %s and docstatus=1""", (nowdate(), d[1])): if not webnotes.conn.sql("""select name from `tabSales Invoice` \
where posting_date = %s and recurring_id = %s and docstatus=1""", (nowdate(), d[1])):
prev_rv = get_obj('Sales Invoice', d[0], with_children=1) prev_rv = get_obj('Sales Invoice', d[0], with_children=1)
new_rv = create_new_invoice(prev_rv) new_rv = create_new_invoice(prev_rv)
@ -499,13 +501,16 @@ def create_new_invoice(prev_rv):
# clone rv # clone rv
new_rv = clone(prev_rv) new_rv = clone(prev_rv)
mdict = {'Monthly': 1, 'Quarterly': 3, 'Half-yearly': 6, 'Yearly': 12}
mcount = mdict[prev_rv.doc.recurring_type]
# update new rv # update new rv
new_rv.doc.posting_date = new_rv.doc.next_date new_rv.doc.posting_date = new_rv.doc.next_date
new_rv.doc.aging_date = new_rv.doc.next_date new_rv.doc.aging_date = new_rv.doc.next_date
new_rv.doc.due_date = add_days(new_rv.doc.next_date, cint(date_diff(prev_rv.doc.due_date, prev_rv.doc.posting_date))) new_rv.doc.due_date = add_days(new_rv.doc.next_date, cint(date_diff(prev_rv.doc.due_date, prev_rv.doc.posting_date)))
new_rv.doc.invoice_period_from_date = get_next_month_date(new_rv.doc.invoice_period_from_date) new_rv.doc.invoice_period_from_date = get_next_date(new_rv.doc.invoice_period_from_date, mcount)
new_rv.doc.invoice_period_to_date = get_next_month_date(new_rv.doc.invoice_period_to_date) new_rv.doc.invoice_period_to_date = get_next_date(new_rv.doc.invoice_period_to_date, mcount)
new_rv.doc.owner = prev_rv.doc.owner new_rv.doc.owner = prev_rv.doc.owner
new_rv.doc.save() new_rv.doc.save()
@ -515,13 +520,13 @@ def create_new_invoice(prev_rv):
return new_rv return new_rv
def get_next_month_date(dt): def get_next_date(dt, mcount):
import datetime import datetime
m = getdate(dt).month + 1 m = getdate(dt).month + mcount
y = getdate(dt).year y = getdate(dt).year
d = getdate(dt).day d = getdate(dt).day
if m > 12: if m > 12:
m, y = 1, y+1 m, y = m-12, y+1
try: try:
next_month_date = datetime.date(y, m, d) next_month_date = datetime.date(y, m, d)
except: except:

View File

@ -686,7 +686,9 @@ class DocType(TransactionBase):
def convert_into_recurring(self): def convert_into_recurring(self):
if self.doc.convert_into_recurring_invoice: if self.doc.convert_into_recurring_invoice:
if not self.doc.invoice_period_from_date or not self.doc.invoice_period_to_date: if not self.doc.recurring_type:
msgprint("Please select recurring type", raise_exception=1)
elif not self.doc.invoice_period_from_date or not self.doc.invoice_period_to_date:
msgprint("Invoice period from date and to date is mandatory for recurring invoice", raise_exception=1) msgprint("Invoice period from date and to date is mandatory for recurring invoice", raise_exception=1)
self.set_next_date() self.set_next_date()
if not self.doc.recurring_id: if not self.doc.recurring_id:
@ -702,10 +704,11 @@ class DocType(TransactionBase):
will be generated e.g. 05, 28 etc.""", raise_exception=1) will be generated e.g. 05, 28 etc.""", raise_exception=1)
import datetime import datetime
m = getdate(self.doc.posting_date).month + 1 mcount = {'Monthly': 1, 'Quarterly': 3, 'Half-yearly': 6, 'Yearly': 12}
m = getdate(self.doc.posting_date).month + mcount[self.doc.recurring_type]
y = getdate(self.doc.posting_date).year y = getdate(self.doc.posting_date).year
if m > 12: if m > 12:
m, y = 1, y+1 m, y = m-12, y+1
try: try:
next_date = datetime.date(y, m, cint(self.doc.repeat_on_day_of_month)) next_date = datetime.date(y, m, cint(self.doc.repeat_on_day_of_month))
except: except:

View File

@ -3,9 +3,9 @@
# These values are common in all dictionaries # These values are common in all dictionaries
{ {
'creation': '2012-04-13 11:56:18', 'creation': '2012-06-11 12:09:54',
'docstatus': 0, 'docstatus': 0,
'modified': '2012-06-04 14:40:59', 'modified': '2012-06-17 21:37:36',
'modified_by': u'Administrator', 'modified_by': u'Administrator',
'owner': u'Administrator' 'owner': u'Administrator'
}, },
@ -1480,6 +1480,21 @@
'trigger': u'Client' 'trigger': u'Client'
}, },
# DocField
{
'allow_on_submit': 1,
'depends_on': u'eval:doc.convert_into_recurring_invoice==1',
'description': u'Select the period when the invoice will be generated automatically',
'doctype': u'DocField',
'fieldname': u'recurring_type',
'fieldtype': u'Select',
'label': u'Recurring Type',
'no_copy': 1,
'options': u'Monthly\nQuarterly\nHalf-yearly\nYearly',
'permlevel': 0,
'print_hide': 1
},
# DocField # DocField
{ {
'allow_on_submit': 1, 'allow_on_submit': 1,