brotherton-erpnext/patches/may_2012/remove_communication_log.py

107 lines
3.2 KiB
Python
Raw Normal View History

from __future__ import unicode_literals
2012-05-31 09:23:55 +00:00
def execute():
import webnotes
webnotes.reload_doc('support', 'doctype', 'communication')
2012-05-31 09:23:55 +00:00
webnotes.conn.commit()
webnotes.conn.begin()
# change doctype property setter and custom fields, and save them
move_customizations()
try:
remove_communication_log()
except Exception, e:
if e.args[0] != 1146:
raise e
def move_customizations():
import webnotes.model.doc
import webnotes.model.doctype
2012-06-05 12:58:38 +00:00
2012-05-31 09:23:55 +00:00
res = webnotes.conn.sql("""\
delete from `tabProperty Setter`
where property='previous_field'
and doc_type = 'Communication Log'""")
res = webnotes.conn.sql("""\
select name from `tabCustom Field`
where dt='Communication Log'""")
for r in res:
d = webnotes.model.doc.Document('Custom Field', r[0])
d.dt = 'Communication'
d.save()
2012-06-05 12:58:38 +00:00
from webnotes.model.db_schema import updatedb
updatedb('Communication')
2012-05-31 09:23:55 +00:00
res = webnotes.conn.sql("""\
select field_name from `tabProperty Setter`
where doc_type='Communication Log' and field_name is not null""")
doclist = webnotes.model.doctype.get('Communication', 0)
field_list = [d.fieldname for d in doclist if d.doctype=='DocField']
for r in res:
if r[0] in field_list:
webnotes.conn.sql("""\
update `tabProperty Setter`
set doc_type = 'Communication'
where field_name=%s and doc_type='Communication Log'""", r[0])
webnotes.conn.sql("""\
delete from `tabProperty Setter`
where doc_type='Communication Log'""")
2012-11-29 06:19:56 +00:00
webnotes.clear_cache(doctype="Communication")
2012-05-31 09:23:55 +00:00
def remove_communication_log():
import webnotes
import webnotes.model
import webnotes.model.doc
2012-06-05 12:58:38 +00:00
import webnotes.model.doctype
webnotes.conn.auto_commit_on_many_writes = True
2012-05-31 09:23:55 +00:00
# get all communication log records
comm_log_list = webnotes.conn.sql("select * from `tabCommunication Log`",
as_dict=1)
2012-06-05 12:58:38 +00:00
field_list = [d.fieldname for d in \
webnotes.model.doctype.get('Communication', 0) \
if d.doctype=='DocField']
2012-05-31 09:23:55 +00:00
# copy it to communication
for comm_log in comm_log_list:
d = webnotes.model.doc.Document('Communication')
for key in comm_log.keys():
if key not in webnotes.model.default_fields:
d.fields[key] = comm_log[key]
parenttype = (comm_log.get('parenttype') or '').lower()
2012-06-05 12:58:38 +00:00
if parenttype in field_list:
2012-05-31 09:23:55 +00:00
d.fields[parenttype] = comm_log.get('parent')
2012-06-05 12:58:38 +00:00
d.naming_series = 'COMM-'
2012-05-31 09:23:55 +00:00
d.subject = 'Follow Up'
d.content = comm_log.get('notes') or ''
d.medium = comm_log.get('follow_up_type') or ''
d.sales_person = comm_log.get('follow_up_by')
d.communication_date = comm_log.get('date')
d.category = 'Miscellaneous'
d.action = 'No Action'
2012-06-05 12:58:38 +00:00
d.save(ignore_fields=1)
2012-05-31 09:23:55 +00:00
# delete records with parent type "Customer", "Lead", "Supplier"
webnotes.conn.sql("""\
delete from `tabCommunication Log`
where parenttype in ('Customer', 'Lead', 'Supplier',
'Opportunity', 'Quotation')""")
# if all records deleted, drop table communication log
# and delete doctype communication log
# if for some reason, records remain, dont drop table and dont delete doctype
count = webnotes.conn.sql("select count(*) from `tabCommunication Log`")[0][0]
if not count:
webnotes.model.delete_doc('DocType', 'Communication Log')
webnotes.conn.commit()
webnotes.conn.sql("drop table `tabCommunication Log`")
webnotes.conn.begin()