blob: d8c342d128232b4a83b354f9eaddfba2e2c61717 [file] [log] [blame]
Rushabh Mehtaad45e312013-11-20 12:59:58 +05301# Copyright (c) 2013, Web Notes Technologies Pvt. Ltd. and Contributors
Rushabh Mehtae67d1fb2013-08-05 14:59:54 +05302# License: GNU General Public License v3. See license.txt
3
Nabin Hait1c990352012-07-09 15:25:08 +05304#!/usr/bin/python
5
6# This script is for cleaning up of all data from system including
7# all transactions and masters (excludes default masters).
8# Basically after running this file, system will reset to it's
9# initial state.
10# This script can be executed from lib/wnf.py using
11# lib/wnf.py --cleanup-data
12
Anand Doshi486f9df2012-07-19 13:40:31 +053013from __future__ import unicode_literals
Nabin Hait1c990352012-07-09 15:25:08 +053014import sys
15sys.path.append("lib/py")
16sys.path.append(".")
17sys.path.append("erpnext")
18
Rushabh Mehta793ba6b2014-02-14 15:47:51 +053019import frappe
Nabin Hait1c990352012-07-09 15:25:08 +053020
21#--------------------------------
22
23def delete_transactions():
24 print "Deleting transactions..."
25
Nabin Haitac24d0f2013-07-01 14:03:08 +053026 trans = ['Task', 'Support Ticket', 'Stock Reconciliation', 'Stock Ledger Entry',
Nabin Haitbffbc182013-03-12 10:41:21 +053027 'Stock Entry', 'Sales Order', 'Salary Slip','Sales Invoice', 'Quotation',
28 'Quality Inspection', 'Purchase Receipt', 'Purchase Order', 'Production Order',
29 'POS Setting', 'Period Closing Voucher', 'Purchase Invoice', 'Maintenance Visit',
30 'Maintenance Schedule', 'Leave Application', 'Leave Allocation', 'Lead', 'Journal Voucher',
31 'Installation Note', 'Material Request', 'GL Entry', 'Expense Claim', 'Opportunity',
32 'Delivery Note', 'Customer Issue', 'Bin', 'Authorization Rule', 'Attendance', 'C-Form',
Nabin Haitac24d0f2013-07-01 14:03:08 +053033 'Appraisal', 'Installation Note', 'Communication', "Supplier Quotation", "Newsletter",
34 "Job Applicant", "Web Page", "Website Slideshow", "Blog Post", "Blog Category", "Blogger",
35 "Time Log", "Time Log Batch", "Workflow"]
Nabin Hait1c990352012-07-09 15:25:08 +053036 for d in trans:
Anand Doshie9baaa62014-02-26 12:35:33 +053037 for t in frappe.db.sql("select options from tabDocField where parent='%s' and fieldtype='Table'" % d):
38 frappe.db.sql("delete from `tab%s`" % (t))
39 frappe.db.sql("delete from `tab%s`" % (d))
Nabin Hait1c990352012-07-09 15:25:08 +053040 print "Deleted " + d
41
42
43
44def delete_masters():
45 print "Deleting masters...."
46 masters = {
Nabin Haitbffbc182013-03-12 10:41:21 +053047 'Workstation': ['Default Workstation'],
Nabin Haitbffbc182013-03-12 10:41:21 +053048 'Warehouse': ['Default Warehouse'],
49 'UOM': ['Kg', 'Mtr', 'Box', 'Ltr', 'Nos', 'Ft', 'Pair', 'Set'],
50 'Territory': ['All Territories', 'Default Territory'],
51 'Terms and Conditions': '',
52 'Tag': '',
53 'Supplier Type': ['Default Supplier Type'],
54 'Supplier': '',
55 'Serial No': '',
Nabin Haitcfecd2b2013-07-11 17:49:18 +053056 'Sales Person': ['Sales Team'],
Nabin Haitbffbc182013-03-12 10:41:21 +053057 'Sales Partner': '',
58 'Sales BOM': '',
59 'Salary Structure': '',
60 'Purchase Taxes and Charges Master': '',
61 'Project': '',
62 'Print Heading': '',
63 'Price List': ['Default Price List'],
64 'Sales Taxes and Charges Master': '',
65 'Letter Head': '',
66 'Leave Type': ['Leave Without Pay', 'Privilege Leave', 'Casual Leave', 'PL', 'CL', 'LWP',
Nabin Hait1c990352012-07-09 15:25:08 +053067 'Compensatory Off', 'Sick Leave'],
Nabin Haitbffbc182013-03-12 10:41:21 +053068 'Appraisal Template': '',
69 'Item Group': ['All Item Groups', 'Default'],
70 'Item': '',
71 'Holiday List': '',
Nabin Haitac24d0f2013-07-01 14:03:08 +053072 'Activity Type': '',
Nabin Haitbffbc182013-03-12 10:41:21 +053073 'Grade': '',
74 'Feed': '',
75 'Expense Claim Type': ['Travel', 'Medical', 'Calls', 'Food', 'Others'],
76 'Event': '',
77 'Employment Type': '',
78 'Employee': '',
79 'Earning Type': ['Basic', 'Conveyance', 'House Rent Allowance', 'Dearness Allowance',
Nabin Hait1c990352012-07-09 15:25:08 +053080 'Medical Allowance', 'Telephone'],
Nabin Haitbffbc182013-03-12 10:41:21 +053081 'Designation': '',
82 'Department': '',
83 'Deduction Type': ['Income Tax', 'Professional Tax', 'Provident Fund', 'Leave Deduction'],
84 'Customer Group': ['All Customer Groups', 'Default Customer Group'],
85 'Customer': '',
86 'Cost Center': '',
87 'Contact': '',
88 'Campaign': '',
89 'Budget Distribution': '',
90 'Brand': '',
91 'Branch': '',
92 'Batch': '',
93 'Appraisal': '',
94 'Account': '',
Nabin Hait1c990352012-07-09 15:25:08 +053095 'BOM': ''
96 }
97 for d in masters.keys():
Anand Doshie9baaa62014-02-26 12:35:33 +053098 for t in frappe.db.sql("select options from tabDocField where parent='%s' \
Nabin Hait1c990352012-07-09 15:25:08 +053099 and fieldtype='Table'" % d):
Anand Doshie9baaa62014-02-26 12:35:33 +0530100 frappe.db.sql("delete from `tab%s`" % (t))
Nabin Hait1c990352012-07-09 15:25:08 +0530101 lst = '"'+'","'.join(masters[d])+ '"'
Anand Doshie9baaa62014-02-26 12:35:33 +0530102 frappe.db.sql("delete from `tab%s` where name not in (%s)" % (d, lst))
Nabin Hait1c990352012-07-09 15:25:08 +0530103 print "Deleted " + d
104
105
106
Nabin Hait82efcc72012-09-18 11:19:34 +0530107def reset_all_series():
108 # Reset master series
Anand Doshie9baaa62014-02-26 12:35:33 +0530109 frappe.db.sql("""update tabSeries set current = 0 where name not in
Nabin Hait82efcc72012-09-18 11:19:34 +0530110 ('Ann/', 'BSD', 'DEF', 'DF', 'EV', 'Event Updates/', 'FileData-',
111 'FL', 'FMD/', 'GLM Detail', 'Login Page/', 'MDI', 'MDR', 'MI', 'MIR',
112 'PERM', 'PR', 'SRCH/C/', 'TD', 'TIC/', 'TMD/', 'TW', 'UR', '_FEED',
Nabin Hait94a6c7e2012-07-09 16:06:53 +0530113 '_SRCH', '_TRIGGER', '__NSO', 'CustomField', 'Letter')
Nabin Hait1c990352012-07-09 15:25:08 +0530114 """)
115 print "Series updated"
Nabin Hait82efcc72012-09-18 11:19:34 +0530116
117def reset_transaction_series():
Anand Doshie9baaa62014-02-26 12:35:33 +0530118 frappe.db.sql("""update tabSeries set current = 0 where name in
Nabin Hait82efcc72012-09-18 11:19:34 +0530119 ('JV', 'INV', 'BILL', 'SO', 'DN', 'PO', 'LEAD', 'ENQUIRY', 'ENQ', 'CI',
Nabin Haitbffbc182013-03-12 10:41:21 +0530120 'IN', 'PS', 'IDT', 'QAI', 'QTN', 'STE', 'SQTN', 'SUP', 'SR',
Nabin Hait82efcc72012-09-18 11:19:34 +0530121 'POS', 'LAP', 'LAL', 'EXP')""")
122 print "Series updated"
Nabin Hait1c990352012-07-09 15:25:08 +0530123
124
125def delete_main_masters():
Nabin Haitbffbc182013-03-12 10:41:21 +0530126 main_masters = ['Fiscal Year', 'Company', 'DefaultValue']
Nabin Hait1c990352012-07-09 15:25:08 +0530127 for d in main_masters:
Anand Doshie9baaa62014-02-26 12:35:33 +0530128 for t in frappe.db.sql("select options from tabDocField where parent='%s' and fieldtype='Table'" % d):
129 frappe.db.sql("delete from `tab%s`" % (t))
130 frappe.db.sql("delete from `tab%s`" % (d))
Nabin Hait1c990352012-07-09 15:25:08 +0530131 print "Deleted " + d
Nabin Hait1c990352012-07-09 15:25:08 +0530132
133def reset_global_defaults():
134 flds = {
Nabin Haitbffbc182013-03-12 10:41:21 +0530135 'default_company': None,
136 'default_currency': None,
137 'current_fiscal_year': None,
Nabin Hait1c990352012-07-09 15:25:08 +0530138 'date_format': 'dd-mm-yyyy',
Nabin Haitbffbc182013-03-12 10:41:21 +0530139 'sms_sender_name': None,
Nabin Hait1c990352012-07-09 15:25:08 +0530140 'default_item_group': 'Default',
141 'default_stock_uom': 'Nos',
142 'default_valuation_method': 'FIFO',
Nabin Haitbffbc182013-03-12 10:41:21 +0530143 'tolerance': None,
144 'acc_frozen_upto': None,
145 'bde_auth_role': None,
146 'credit_controller': None,
Nabin Hait1c990352012-07-09 15:25:08 +0530147 'default_customer_group': 'Default Customer Group',
148 'default_territory': 'Default',
149 'default_price_list': 'Standard',
Nabin Haitbffbc182013-03-12 10:41:21 +0530150 'default_supplier_type': 'Default Supplier Type',
151 'hide_currency_symbol': None,
152 'default_price_list_currency': None,
Nabin Hait1c990352012-07-09 15:25:08 +0530153 }
154
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530155 from frappe.model.code import get_obj
Nabin Hait1c990352012-07-09 15:25:08 +0530156 gd = get_obj('Global Defaults', 'Global Defaults')
157 for d in flds:
158 gd.doc.fields[d] = flds[d]
159 gd.doc.save()
160
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530161 frappe.clear_cache()
Nabin Hait1c990352012-07-09 15:25:08 +0530162
163
164def run():
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530165 frappe.connect()
Anand Doshie9baaa62014-02-26 12:35:33 +0530166 frappe.db.begin()
Nabin Hait1c990352012-07-09 15:25:08 +0530167
168 # Confirmation from user
169 confirm = ''
170 while not confirm:
171 confirm = raw_input("Are you sure you want to delete the data from the system (N/Y)?")
172 if confirm.lower() != 'y':
173 raise Exception
174
175 cleanup_type = ''
176 while cleanup_type not in ['1', '2']:
177 cleanup_type = raw_input("""\nWhat type of cleanup you want ot perform?
178 1. Only Transactions
179 2. Both Masters and Transactions
180
181 Please enter your choice (1/2):
182 """)
183
184 # delete
185 delete_transactions()
186
Nabin Hait82efcc72012-09-18 11:19:34 +0530187 if cleanup_type == '1':
Rushabh Mehta4b41afd2012-10-09 15:19:59 +0200188 print "Reset Transaction Series"
Nabin Hait82efcc72012-09-18 11:19:34 +0530189 reset_transaction_series()
Nabin Hait1c990352012-07-09 15:25:08 +0530190 else:
191 delete_masters()
Rushabh Mehta4b41afd2012-10-09 15:19:59 +0200192 print "Reset All Series"
Nabin Hait82efcc72012-09-18 11:19:34 +0530193 reset_all_series()
Nabin Hait1c990352012-07-09 15:25:08 +0530194 delete_main_masters()
195 reset_global_defaults()
196
197 print "System cleaned up succesfully"
Anand Doshie9baaa62014-02-26 12:35:33 +0530198 frappe.db.commit()
199 frappe.db.close()
Nabin Hait1c990352012-07-09 15:25:08 +0530200
201
202if __name__ == '__main__':
203 run()