brotherton-erpnext/erpnext/utilities/cleanup_data.py

206 lines
6.3 KiB
Python
Raw Normal View History

2013-11-20 07:29:58 +00:00
# Copyright (c) 2013, Web Notes Technologies Pvt. Ltd. and Contributors
# License: GNU General Public License v3. See license.txt
2012-07-09 09:55:08 +00:00
#!/usr/bin/python
# This script is for cleaning up of all data from system including
# all transactions and masters (excludes default masters).
# Basically after running this file, system will reset to it's
# initial state.
# This script can be executed from lib/wnf.py using
# lib/wnf.py --cleanup-data
from __future__ import unicode_literals
2012-07-09 09:55:08 +00:00
import sys
sys.path.append("lib/py")
sys.path.append(".")
sys.path.append("erpnext")
2014-02-14 10:17:51 +00:00
import frappe
2012-07-09 09:55:08 +00:00
#--------------------------------
def delete_transactions():
print "Deleting transactions..."
trans = ['Task', 'Support Ticket', 'Stock Reconciliation', 'Stock Ledger Entry',
2013-03-12 05:11:21 +00:00
'Stock Entry', 'Sales Order', 'Salary Slip','Sales Invoice', 'Quotation',
'Quality Inspection', 'Purchase Receipt', 'Purchase Order', 'Production Order',
'POS Setting', 'Period Closing Voucher', 'Purchase Invoice', 'Maintenance Visit',
'Maintenance Schedule', 'Leave Application', 'Leave Allocation', 'Lead', 'Journal Voucher',
'Installation Note', 'Material Request', 'GL Entry', 'Expense Claim', 'Opportunity',
'Delivery Note', 'Customer Issue', 'Bin', 'Authorization Rule', 'Attendance', 'C-Form',
'Appraisal', 'Installation Note', 'Communication', "Supplier Quotation", "Newsletter",
"Job Applicant", "Web Page", "Website Slideshow", "Blog Post", "Blog Category", "Blogger",
"Time Log", "Time Log Batch", "Workflow"]
2012-07-09 09:55:08 +00:00
for d in trans:
2014-03-03 10:21:13 +00:00
for t in frappe.db.sql("select options from tabDocField \
where parent=%s and fieldtype='Table'", d):
2014-02-26 07:05:33 +00:00
frappe.db.sql("delete from `tab%s`" % (t))
frappe.db.sql("delete from `tab%s`" % (d))
2012-07-09 09:55:08 +00:00
print "Deleted " + d
def delete_masters():
print "Deleting masters...."
masters = {
2013-03-12 05:11:21 +00:00
'Workstation': ['Default Workstation'],
'Warehouse': ['Default Warehouse'],
'UOM': ['Kg', 'Mtr', 'Box', 'Ltr', 'Nos', 'Ft', 'Pair', 'Set'],
'Territory': ['All Territories', 'Default Territory'],
'Terms and Conditions': '',
'Tag': '',
'Supplier Type': ['Default Supplier Type'],
'Supplier': '',
'Serial No': '',
'Sales Person': ['Sales Team'],
2013-03-12 05:11:21 +00:00
'Sales Partner': '',
'Sales BOM': '',
'Salary Structure': '',
'Purchase Taxes and Charges Master': '',
'Project': '',
'Print Heading': '',
'Price List': ['Default Price List'],
'Sales Taxes and Charges Master': '',
'Letter Head': '',
'Leave Type': ['Leave Without Pay', 'Privilege Leave', 'Casual Leave', 'PL', 'CL', 'LWP',
2012-07-09 09:55:08 +00:00
'Compensatory Off', 'Sick Leave'],
2013-03-12 05:11:21 +00:00
'Appraisal Template': '',
'Item Group': ['All Item Groups', 'Default'],
'Item': '',
'Holiday List': '',
'Activity Type': '',
2013-03-12 05:11:21 +00:00
'Grade': '',
'Feed': '',
'Expense Claim Type': ['Travel', 'Medical', 'Calls', 'Food', 'Others'],
'Event': '',
'Employment Type': '',
'Employee': '',
'Earning Type': ['Basic', 'Conveyance', 'House Rent Allowance', 'Dearness Allowance',
2012-07-09 09:55:08 +00:00
'Medical Allowance', 'Telephone'],
2013-03-12 05:11:21 +00:00
'Designation': '',
'Department': '',
'Deduction Type': ['Income Tax', 'Professional Tax', 'Provident Fund', 'Leave Deduction'],
'Customer Group': ['All Customer Groups', 'Default Customer Group'],
'Customer': '',
'Cost Center': '',
'Contact': '',
'Campaign': '',
'Budget Distribution': '',
'Brand': '',
'Branch': '',
'Batch': '',
'Appraisal': '',
'Account': '',
2012-07-09 09:55:08 +00:00
'BOM': ''
}
for d in masters.keys():
2014-03-03 10:21:13 +00:00
for t in frappe.db.sql("select options from tabDocField where parent=%s \
and fieldtype='Table'", d):
frappe.db.sql("delete from `tab%s`" % (t))
frappe.db.sql("delete from `tab%s` where name not in (%s)" %
(d, ', '.join(['%s']*len(masters[d]), masters[d])))
2012-07-09 09:55:08 +00:00
print "Deleted " + d
def reset_all_series():
# Reset master series
2014-02-26 07:05:33 +00:00
frappe.db.sql("""update tabSeries set current = 0 where name not in
('Ann/', 'BSD', 'DEF', 'DF', 'EV', 'Event Updates/', 'FileData-',
'FL', 'FMD/', 'GLM Detail', 'Login Page/', 'MDI', 'MDR', 'MI', 'MIR',
'PERM', 'PR', 'SRCH/C/', 'TD', 'TIC/', 'TMD/', 'TW', 'UR', '_FEED',
2012-07-09 10:36:53 +00:00
'_SRCH', '_TRIGGER', '__NSO', 'CustomField', 'Letter')
2012-07-09 09:55:08 +00:00
""")
print "Series updated"
def reset_transaction_series():
2014-02-26 07:05:33 +00:00
frappe.db.sql("""update tabSeries set current = 0 where name in
('JV', 'INV', 'BILL', 'SO', 'DN', 'PO', 'LEAD', 'ENQUIRY', 'ENQ', 'CI',
2013-03-12 05:11:21 +00:00
'IN', 'PS', 'IDT', 'QAI', 'QTN', 'STE', 'SQTN', 'SUP', 'SR',
'POS', 'LAP', 'LAL', 'EXP')""")
print "Series updated"
2012-07-09 09:55:08 +00:00
def delete_main_masters():
2013-03-12 05:11:21 +00:00
main_masters = ['Fiscal Year', 'Company', 'DefaultValue']
2012-07-09 09:55:08 +00:00
for d in main_masters:
2014-03-03 10:21:13 +00:00
for t in frappe.db.sql("select options from tabDocField \
where parent=%s and fieldtype='Table'", d):
2014-02-26 07:05:33 +00:00
frappe.db.sql("delete from `tab%s`" % (t))
frappe.db.sql("delete from `tab%s`" % (d))
2012-07-09 09:55:08 +00:00
print "Deleted " + d
def reset_global_defaults():
flds = {
2013-03-12 05:11:21 +00:00
'default_company': None,
'default_currency': None,
'current_fiscal_year': None,
2012-07-09 09:55:08 +00:00
'date_format': 'dd-mm-yyyy',
2013-03-12 05:11:21 +00:00
'sms_sender_name': None,
2012-07-09 09:55:08 +00:00
'default_item_group': 'Default',
'default_stock_uom': 'Nos',
'default_valuation_method': 'FIFO',
2013-03-12 05:11:21 +00:00
'tolerance': None,
'acc_frozen_upto': None,
'bde_auth_role': None,
'credit_controller': None,
2012-07-09 09:55:08 +00:00
'default_customer_group': 'Default Customer Group',
'default_territory': 'Default',
'default_price_list': 'Standard',
2013-03-12 05:11:21 +00:00
'default_supplier_type': 'Default Supplier Type',
'hide_currency_symbol': None,
'default_price_list_currency': None,
2012-07-09 09:55:08 +00:00
}
2014-02-14 10:17:51 +00:00
from frappe.model.code import get_obj
2012-07-09 09:55:08 +00:00
gd = get_obj('Global Defaults', 'Global Defaults')
for d in flds:
gd.set(d, flds[d])
gd.save()
2012-07-09 09:55:08 +00:00
2014-02-14 10:17:51 +00:00
frappe.clear_cache()
2012-07-09 09:55:08 +00:00
def run():
2014-02-14 10:17:51 +00:00
frappe.connect()
2014-02-26 07:05:33 +00:00
frappe.db.begin()
2012-07-09 09:55:08 +00:00
# Confirmation from user
confirm = ''
while not confirm:
confirm = raw_input("Are you sure you want to delete the data from the system (N/Y)?")
if confirm.lower() != 'y':
raise Exception
cleanup_type = ''
while cleanup_type not in ['1', '2']:
cleanup_type = raw_input("""\nWhat type of cleanup you want ot perform?
1. Only Transactions
2. Both Masters and Transactions
Please enter your choice (1/2):
""")
# delete
delete_transactions()
if cleanup_type == '1':
2012-10-09 13:19:59 +00:00
print "Reset Transaction Series"
reset_transaction_series()
2012-07-09 09:55:08 +00:00
else:
delete_masters()
2012-10-09 13:19:59 +00:00
print "Reset All Series"
reset_all_series()
2012-07-09 09:55:08 +00:00
delete_main_masters()
reset_global_defaults()
print "System cleaned up succesfully"
2014-02-26 07:05:33 +00:00
frappe.db.commit()
frappe.db.close()
2012-07-09 09:55:08 +00:00
if __name__ == '__main__':
run()