2014-06-23 12:34:07 +00:00
|
|
|
# Copyright (c) 2013, Web Notes Technologies Pvt. Ltd. and Contributors
|
|
|
|
# License: GNU General Public License v3. See license.txt
|
|
|
|
|
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
|
|
|
import frappe
|
2014-09-09 07:08:21 +00:00
|
|
|
from frappe.custom.doctype.property_setter.property_setter import make_property_setter
|
2014-06-23 12:34:07 +00:00
|
|
|
|
|
|
|
doctype_series_map = {
|
|
|
|
'Attendance': 'ATT-',
|
|
|
|
'C-Form': 'C-FORM-',
|
|
|
|
'Customer': 'CUST-',
|
2015-02-17 05:15:50 +00:00
|
|
|
'Warranty Claim': 'CI-',
|
2014-06-23 12:34:07 +00:00
|
|
|
'Delivery Note': 'DN-',
|
|
|
|
'Installation Note': 'IN-',
|
|
|
|
'Item': 'ITEM-',
|
2014-12-25 11:44:18 +00:00
|
|
|
'Journal Entry': 'JV-',
|
2014-06-23 12:34:07 +00:00
|
|
|
'Lead': 'LEAD-',
|
|
|
|
'Opportunity': 'OPTY-',
|
|
|
|
'Packing Slip': 'PS-',
|
|
|
|
'Production Order': 'PRO-',
|
|
|
|
'Purchase Invoice': 'PINV-',
|
|
|
|
'Purchase Order': 'PO-',
|
|
|
|
'Purchase Receipt': 'PREC-',
|
|
|
|
'Quality Inspection': 'QI-',
|
|
|
|
'Quotation': 'QTN-',
|
|
|
|
'Sales Invoice': 'SINV-',
|
|
|
|
'Sales Order': 'SO-',
|
|
|
|
'Stock Entry': 'STE-',
|
|
|
|
'Supplier': 'SUPP-',
|
|
|
|
'Supplier Quotation': 'SQTN-',
|
2014-11-28 06:15:02 +00:00
|
|
|
'Issue': 'SUP-'
|
2014-06-23 12:34:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
def execute():
|
|
|
|
series_to_set = get_series_to_set()
|
|
|
|
for doctype, opts in series_to_set.items():
|
|
|
|
print "Setting naming series", doctype, opts
|
|
|
|
set_series(doctype, opts["options"], opts["default"])
|
|
|
|
|
|
|
|
def set_series(doctype, options, default):
|
|
|
|
make_property_setter(doctype, "naming_series", "options", options, "Text")
|
|
|
|
make_property_setter(doctype, "naming_series", "default", default, "Text")
|
|
|
|
|
|
|
|
def get_series_to_set():
|
|
|
|
series_to_set = {}
|
|
|
|
|
|
|
|
for doctype, new_series in doctype_series_map.items():
|
|
|
|
# you can't fix what does not exist :)
|
|
|
|
if not frappe.db.a_row_exists(doctype):
|
|
|
|
continue
|
|
|
|
|
|
|
|
series_to_preserve = get_series_to_preserve(doctype, new_series)
|
|
|
|
|
|
|
|
if not series_to_preserve:
|
|
|
|
continue
|
|
|
|
|
|
|
|
default_series = get_default_series(doctype, new_series)
|
|
|
|
if not default_series:
|
|
|
|
continue
|
|
|
|
|
|
|
|
existing_series = (frappe.get_meta(doctype).get_field("naming_series").options or "").split("\n")
|
|
|
|
existing_series = filter(None, [d.strip() for d in existing_series])
|
|
|
|
|
|
|
|
if (not (set(existing_series).difference(series_to_preserve) or set(series_to_preserve).difference(existing_series))
|
|
|
|
and len(series_to_preserve)==len(existing_series)):
|
|
|
|
# print "No change for", doctype, ":", existing_series, "=", series_to_preserve
|
|
|
|
continue
|
|
|
|
|
|
|
|
# set naming series property setter
|
|
|
|
series_to_preserve = list(set(series_to_preserve + existing_series))
|
|
|
|
if new_series in series_to_preserve:
|
|
|
|
series_to_preserve.remove(new_series)
|
|
|
|
|
|
|
|
if series_to_preserve:
|
|
|
|
series_to_set[doctype] = {"options": "\n".join(series_to_preserve), "default": default_series}
|
|
|
|
|
|
|
|
return series_to_set
|
|
|
|
|
|
|
|
def get_series_to_preserve(doctype, new_series):
|
|
|
|
series_to_preserve = frappe.db.sql_list("""select distinct naming_series from `tab{doctype}`
|
|
|
|
where ifnull(naming_series, '') not in ('', %s)""".format(doctype=doctype), new_series)
|
|
|
|
|
|
|
|
series_to_preserve.sort()
|
|
|
|
|
|
|
|
return series_to_preserve
|
|
|
|
|
|
|
|
def get_default_series(doctype, new_series):
|
|
|
|
default_series = frappe.db.sql("""select naming_series from `tab{doctype}` where ifnull(naming_series, '') not in ('', %s)
|
|
|
|
and creation=(select max(creation) from `tab{doctype}`
|
|
|
|
where ifnull(naming_series, '') not in ('', %s)) order by creation desc limit 1""".format(doctype=doctype),
|
|
|
|
(new_series, new_series))
|
|
|
|
|
|
|
|
if not (default_series and default_series[0][0]):
|
|
|
|
print "[Skipping] Cannot guess which naming series to use for", doctype
|
|
|
|
return
|
|
|
|
|
|
|
|
return default_series[0][0]
|