2017-10-03 11:58:50 +00:00
|
|
|
# Copyright (c) 2017, Frappe and Contributors
|
|
|
|
# License: GNU General Public License v3. See license.txt
|
|
|
|
|
|
|
|
from __future__ import unicode_literals
|
|
|
|
import frappe
|
2017-10-25 06:40:44 +00:00
|
|
|
from frappe.utils import getdate
|
2018-05-03 13:36:32 +00:00
|
|
|
from frappe.desk.doctype.auto_repeat.auto_repeat import get_next_schedule_date
|
2017-10-03 11:58:50 +00:00
|
|
|
|
|
|
|
def execute():
|
2018-05-17 13:28:47 +00:00
|
|
|
frappe.reload_doc('accounts', 'doctype', 'subscription')
|
|
|
|
fields = ["name", "reference_doctype", "reference_document",
|
|
|
|
"start_date", "frequency", "repeat_on_day"]
|
|
|
|
|
|
|
|
for d in fields:
|
|
|
|
if not frappe.db.has_column('Subscription', d):
|
|
|
|
return
|
2017-10-03 11:58:50 +00:00
|
|
|
|
|
|
|
doctypes = ('Purchase Order', 'Sales Order', 'Purchase Invoice', 'Sales Invoice')
|
2017-10-25 06:40:44 +00:00
|
|
|
for data in frappe.get_all('Subscription',
|
2018-05-17 13:28:47 +00:00
|
|
|
fields = fields,
|
2017-10-25 06:40:44 +00:00
|
|
|
filters = {'reference_doctype': ('in', doctypes), 'docstatus': 1}):
|
|
|
|
|
|
|
|
recurring_id = frappe.db.get_value(data.reference_doctype, data.reference_document, "recurring_id")
|
|
|
|
if recurring_id:
|
|
|
|
frappe.db.sql("update `tab{0}` set subscription=%s where recurring_id=%s"
|
|
|
|
.format(data.reference_doctype), (data.name, recurring_id))
|
|
|
|
|
|
|
|
date_field = 'transaction_date'
|
|
|
|
if data.reference_doctype in ['Sales Invoice', 'Purchase Invoice']:
|
|
|
|
date_field = 'posting_date'
|
|
|
|
|
|
|
|
start_date = frappe.db.get_value(data.reference_doctype, data.reference_document, date_field)
|
|
|
|
|
|
|
|
if start_date and getdate(start_date) != getdate(data.start_date):
|
|
|
|
last_ref_date = frappe.db.sql("""
|
|
|
|
select {0}
|
|
|
|
from `tab{1}`
|
|
|
|
where subscription=%s and docstatus < 2
|
|
|
|
order by creation desc
|
|
|
|
limit 1
|
|
|
|
""".format(date_field, data.reference_doctype), data.name)[0][0]
|
|
|
|
|
|
|
|
next_schedule_date = get_next_schedule_date(last_ref_date, data.frequency, data.repeat_on_day)
|
|
|
|
|
|
|
|
frappe.db.set_value("Subscription", data.name, {
|
|
|
|
"start_date": start_date,
|
|
|
|
"next_schedule_date": next_schedule_date
|
|
|
|
}, None)
|