2017-08-31 21:52:06 +00:00
|
|
|
from __future__ import unicode_literals
|
|
|
|
import frappe
|
|
|
|
|
|
|
|
|
|
|
|
def execute():
|
2017-11-21 14:28:16 +00:00
|
|
|
frappe.reload_doc("selling", "doctype", "customer")
|
|
|
|
frappe.reload_doc("buying", "doctype", "supplier")
|
|
|
|
frappe.reload_doc("setup", "doctype", "supplier_type")
|
2017-09-04 23:23:24 +00:00
|
|
|
frappe.reload_doc("accounts", "doctype", "payment_term")
|
|
|
|
frappe.reload_doc("accounts", "doctype", "payment_terms_template_detail")
|
|
|
|
frappe.reload_doc("accounts", "doctype", "payment_terms_template")
|
|
|
|
|
2017-08-31 21:52:06 +00:00
|
|
|
payment_terms = []
|
2017-11-21 14:28:16 +00:00
|
|
|
records = []
|
|
|
|
for doctype in ("Customer", "Supplier", "Supplier Type"):
|
|
|
|
credit_days = frappe.db.sql("""
|
|
|
|
SELECT DISTINCT `credit_days`, `credit_days_based_on`, `name`
|
|
|
|
from `tab{0}`
|
|
|
|
where
|
2018-01-29 10:37:21 +00:00
|
|
|
((credit_days_based_on='Fixed Days' or credit_days_based_on is null)
|
|
|
|
and credit_days is not null)
|
2017-11-21 14:28:16 +00:00
|
|
|
or credit_days_based_on='Last Day of the Next Month'
|
|
|
|
""".format(doctype))
|
|
|
|
|
|
|
|
credit_records = ((record[0], record[1], record[2]) for record in credit_days)
|
|
|
|
for days, based_on, party_name in credit_records:
|
|
|
|
if based_on == "Fixed Days":
|
|
|
|
pyt_template_name = 'Default Payment Term - N{0}'.format(days)
|
|
|
|
else:
|
|
|
|
pyt_template_name = 'Default Payment Term - EO2M'
|
|
|
|
|
|
|
|
if not frappe.db.exists("Payment Terms Template", pyt_template_name):
|
|
|
|
payment_term = make_payment_term(days, based_on)
|
|
|
|
template = make_template(payment_term)
|
|
|
|
else:
|
|
|
|
template = frappe.get_doc("Payment Terms Template", pyt_template_name)
|
|
|
|
|
2018-09-21 04:50:52 +00:00
|
|
|
payment_terms.append('WHEN `name`={0} THEN {1}'.format(frappe.db.escape(party_name), template.template_name))
|
2017-12-13 13:05:22 +00:00
|
|
|
records.append(frappe.db.escape(party_name))
|
2017-11-21 14:28:16 +00:00
|
|
|
|
|
|
|
begin_query_str = "UPDATE `tab{0}` SET `payment_terms` = CASE ".format(doctype)
|
|
|
|
value_query_str = " ".join(payment_terms)
|
|
|
|
cond_query_str = " ELSE `payment_terms` END WHERE "
|
|
|
|
|
|
|
|
if records:
|
|
|
|
frappe.db.sql(
|
|
|
|
begin_query_str + value_query_str + cond_query_str + '`name` IN %s',
|
|
|
|
(records,)
|
|
|
|
)
|
2017-09-04 12:37:32 +00:00
|
|
|
|
2017-08-31 21:52:06 +00:00
|
|
|
|
|
|
|
def make_template(payment_term):
|
|
|
|
doc = frappe.new_doc('Payment Terms Template Detail')
|
|
|
|
doc.payment_term = payment_term.payment_term_name
|
|
|
|
doc.due_date_based_on = payment_term.due_date_based_on
|
|
|
|
doc.invoice_portion = payment_term.invoice_portion
|
|
|
|
doc.description = payment_term.description
|
|
|
|
doc.credit_days = payment_term.credit_days
|
|
|
|
doc.credit_months = payment_term.credit_months
|
|
|
|
|
|
|
|
template = frappe.new_doc('Payment Terms Template')
|
|
|
|
template.template_name = 'Default Payment Term - {0}'.format(payment_term.payment_term_name)
|
2017-09-04 23:23:24 +00:00
|
|
|
template.append('terms', doc)
|
2017-08-31 21:52:06 +00:00
|
|
|
template.save()
|
|
|
|
|
2017-09-04 23:23:24 +00:00
|
|
|
return template
|
|
|
|
|
2017-08-31 21:52:06 +00:00
|
|
|
|
|
|
|
def make_payment_term(days, based_on):
|
|
|
|
based_on_map = {
|
|
|
|
'Fixed Days': 'Day(s) after invoice date',
|
|
|
|
'Last Day of the Next Month': 'Month(s) after the end of the invoice month'
|
|
|
|
}
|
|
|
|
|
|
|
|
doc = frappe.new_doc('Payment Term')
|
|
|
|
doc.due_date_based_on = based_on_map.get(based_on)
|
|
|
|
doc.invoice_portion = 100
|
|
|
|
|
|
|
|
if based_on == 'Fixed Days':
|
|
|
|
doc.credit_days = days
|
|
|
|
doc.description = 'Net payable within {0} days'.format(days)
|
|
|
|
doc.payment_term_name = 'N{0}'.format(days)
|
|
|
|
else:
|
|
|
|
doc.credit_months = 1
|
2017-09-18 11:08:34 +00:00
|
|
|
doc.description = 'Net payable by the end of next month'
|
2017-08-31 21:52:06 +00:00
|
|
|
doc.payment_term_name = 'EO2M'
|
|
|
|
|
|
|
|
doc.save()
|
|
|
|
return doc
|