blob: ed04a94bfaa1121e1dd67527197b3ce687ffdc3f [file] [log] [blame]
Nabin Hait1c990352012-07-09 15:25:08 +05301#!/usr/bin/python
2
3# This script is for cleaning up of all data from system including
4# all transactions and masters (excludes default masters).
5# Basically after running this file, system will reset to it's
6# initial state.
7# This script can be executed from lib/wnf.py using
8# lib/wnf.py --cleanup-data
9
Anand Doshi486f9df2012-07-19 13:40:31 +053010from __future__ import unicode_literals
Nabin Hait1c990352012-07-09 15:25:08 +053011import sys
12sys.path.append("lib/py")
13sys.path.append(".")
14sys.path.append("erpnext")
15
16import webnotes
17
18#--------------------------------
19
20def delete_transactions():
21 print "Deleting transactions..."
22
Nabin Haitbffbc182013-03-12 10:41:21 +053023 trans = ['Timesheet', 'Task', 'Support Ticket', 'Stock Reconciliation', 'Stock Ledger Entry',
24 'Stock Entry', 'Sales Order', 'Salary Slip','Sales Invoice', 'Quotation',
25 'Quality Inspection', 'Purchase Receipt', 'Purchase Order', 'Production Order',
26 'POS Setting', 'Period Closing Voucher', 'Purchase Invoice', 'Maintenance Visit',
27 'Maintenance Schedule', 'Leave Application', 'Leave Allocation', 'Lead', 'Journal Voucher',
28 'Installation Note', 'Material Request', 'GL Entry', 'Expense Claim', 'Opportunity',
29 'Delivery Note', 'Customer Issue', 'Bin', 'Authorization Rule', 'Attendance', 'C-Form',
30 'Appraisal', 'Installation Note', 'Communication']
Nabin Hait1c990352012-07-09 15:25:08 +053031 for d in trans:
32 for t in webnotes.conn.sql("select options from tabDocField where parent='%s' and fieldtype='Table'" % d):
33 webnotes.conn.sql("delete from `tab%s`" % (t))
34 webnotes.conn.sql("delete from `tab%s`" % (d))
Nabin Hait1c990352012-07-09 15:25:08 +053035 print "Deleted " + d
36
37
38
39def delete_masters():
40 print "Deleting masters...."
41 masters = {
Nabin Haitbffbc182013-03-12 10:41:21 +053042 'Workstation': ['Default Workstation'],
43 'Warehouse Type': ['Default Warehouse Type', 'Fixed Asset', 'Rejected', 'Reserved',
Nabin Hait1c990352012-07-09 15:25:08 +053044 'Sample', 'Stores', 'WIP Warehouse'],
Nabin Haitbffbc182013-03-12 10:41:21 +053045 'Warehouse': ['Default Warehouse'],
46 'UOM': ['Kg', 'Mtr', 'Box', 'Ltr', 'Nos', 'Ft', 'Pair', 'Set'],
47 'Territory': ['All Territories', 'Default Territory'],
48 'Terms and Conditions': '',
49 'Tag': '',
50 'Supplier Type': ['Default Supplier Type'],
51 'Supplier': '',
52 'Serial No': '',
53 'Sales Person': ['All Sales Persons'],
54 'Sales Partner': '',
55 'Sales BOM': '',
56 'Salary Structure': '',
57 'Purchase Taxes and Charges Master': '',
58 'Project': '',
59 'Print Heading': '',
60 'Price List': ['Default Price List'],
61 'Sales Taxes and Charges Master': '',
62 'Letter Head': '',
63 'Leave Type': ['Leave Without Pay', 'Privilege Leave', 'Casual Leave', 'PL', 'CL', 'LWP',
Nabin Hait1c990352012-07-09 15:25:08 +053064 'Compensatory Off', 'Sick Leave'],
Nabin Haitbffbc182013-03-12 10:41:21 +053065 'Appraisal Template': '',
66 'Item Group': ['All Item Groups', 'Default'],
67 'Item': '',
68 'Holiday List': '',
69 'Grade': '',
70 'Feed': '',
71 'Expense Claim Type': ['Travel', 'Medical', 'Calls', 'Food', 'Others'],
72 'Event': '',
73 'Employment Type': '',
74 'Employee': '',
75 'Earning Type': ['Basic', 'Conveyance', 'House Rent Allowance', 'Dearness Allowance',
Nabin Hait1c990352012-07-09 15:25:08 +053076 'Medical Allowance', 'Telephone'],
Nabin Haitbffbc182013-03-12 10:41:21 +053077 'Designation': '',
78 'Department': '',
79 'Deduction Type': ['Income Tax', 'Professional Tax', 'Provident Fund', 'Leave Deduction'],
80 'Customer Group': ['All Customer Groups', 'Default Customer Group'],
81 'Customer': '',
82 'Cost Center': '',
83 'Contact': '',
84 'Campaign': '',
85 'Budget Distribution': '',
86 'Brand': '',
87 'Branch': '',
88 'Batch': '',
89 'Appraisal': '',
90 'Account': '',
Nabin Hait1c990352012-07-09 15:25:08 +053091 'BOM': ''
92 }
93 for d in masters.keys():
94 for t in webnotes.conn.sql("select options from tabDocField where parent='%s' \
95 and fieldtype='Table'" % d):
96 webnotes.conn.sql("delete from `tab%s`" % (t))
97 lst = '"'+'","'.join(masters[d])+ '"'
98 webnotes.conn.sql("delete from `tab%s` where name not in (%s)" % (d, lst))
Nabin Hait1c990352012-07-09 15:25:08 +053099 print "Deleted " + d
100
101
102
Nabin Hait82efcc72012-09-18 11:19:34 +0530103def reset_all_series():
104 # Reset master series
105 webnotes.conn.sql("""update tabSeries set current = 0 where name not in
106 ('Ann/', 'BSD', 'DEF', 'DF', 'EV', 'Event Updates/', 'FileData-',
107 'FL', 'FMD/', 'GLM Detail', 'Login Page/', 'MDI', 'MDR', 'MI', 'MIR',
108 'PERM', 'PR', 'SRCH/C/', 'TD', 'TIC/', 'TMD/', 'TW', 'UR', '_FEED',
Nabin Hait94a6c7e2012-07-09 16:06:53 +0530109 '_SRCH', '_TRIGGER', '__NSO', 'CustomField', 'Letter')
Nabin Hait1c990352012-07-09 15:25:08 +0530110 """)
111 print "Series updated"
Nabin Hait82efcc72012-09-18 11:19:34 +0530112
113def reset_transaction_series():
114 webnotes.conn.sql("""update tabSeries set current = 0 where name in
115 ('JV', 'INV', 'BILL', 'SO', 'DN', 'PO', 'LEAD', 'ENQUIRY', 'ENQ', 'CI',
Nabin Haitbffbc182013-03-12 10:41:21 +0530116 'IN', 'PS', 'IDT', 'QAI', 'QTN', 'STE', 'SQTN', 'SUP', 'SR',
Nabin Hait82efcc72012-09-18 11:19:34 +0530117 'POS', 'LAP', 'LAL', 'EXP')""")
118 print "Series updated"
Nabin Hait1c990352012-07-09 15:25:08 +0530119
120
121def delete_main_masters():
Nabin Haitbffbc182013-03-12 10:41:21 +0530122 main_masters = ['Fiscal Year', 'Company', 'DefaultValue']
Nabin Hait1c990352012-07-09 15:25:08 +0530123 for d in main_masters:
124 for t in webnotes.conn.sql("select options from tabDocField where parent='%s' and fieldtype='Table'" % d):
125 webnotes.conn.sql("delete from `tab%s`" % (t))
126 webnotes.conn.sql("delete from `tab%s`" % (d))
Nabin Hait1c990352012-07-09 15:25:08 +0530127 print "Deleted " + d
Nabin Hait1c990352012-07-09 15:25:08 +0530128
129def reset_global_defaults():
130 flds = {
Nabin Haitbffbc182013-03-12 10:41:21 +0530131 'default_company': None,
132 'default_currency': None,
133 'current_fiscal_year': None,
Nabin Hait1c990352012-07-09 15:25:08 +0530134 'date_format': 'dd-mm-yyyy',
Nabin Haitbffbc182013-03-12 10:41:21 +0530135 'sms_sender_name': None,
Nabin Hait1c990352012-07-09 15:25:08 +0530136 'default_item_group': 'Default',
137 'default_stock_uom': 'Nos',
138 'default_valuation_method': 'FIFO',
139 'default_warehouse_type': 'Default Warehouse Type',
Nabin Haitbffbc182013-03-12 10:41:21 +0530140 'tolerance': None,
141 'acc_frozen_upto': None,
142 'bde_auth_role': None,
143 'credit_controller': None,
Nabin Hait1c990352012-07-09 15:25:08 +0530144 'default_customer_group': 'Default Customer Group',
145 'default_territory': 'Default',
146 'default_price_list': 'Standard',
Nabin Haitbffbc182013-03-12 10:41:21 +0530147 'default_supplier_type': 'Default Supplier Type',
148 'hide_currency_symbol': None,
149 'default_price_list_currency': None,
Nabin Hait1c990352012-07-09 15:25:08 +0530150 }
151
152 from webnotes.model.code import get_obj
153 gd = get_obj('Global Defaults', 'Global Defaults')
154 for d in flds:
155 gd.doc.fields[d] = flds[d]
156 gd.doc.save()
157
158 webnotes.clear_cache()
159
160
161def run():
162 webnotes.connect()
Rushabh Mehta4b41afd2012-10-09 15:19:59 +0200163 webnotes.conn.begin()
Nabin Hait1c990352012-07-09 15:25:08 +0530164
165 # Confirmation from user
166 confirm = ''
167 while not confirm:
168 confirm = raw_input("Are you sure you want to delete the data from the system (N/Y)?")
169 if confirm.lower() != 'y':
170 raise Exception
171
172 cleanup_type = ''
173 while cleanup_type not in ['1', '2']:
174 cleanup_type = raw_input("""\nWhat type of cleanup you want ot perform?
175 1. Only Transactions
176 2. Both Masters and Transactions
177
178 Please enter your choice (1/2):
179 """)
180
181 # delete
182 delete_transactions()
183
Nabin Hait82efcc72012-09-18 11:19:34 +0530184 if cleanup_type == '1':
Rushabh Mehta4b41afd2012-10-09 15:19:59 +0200185 print "Reset Transaction Series"
Nabin Hait82efcc72012-09-18 11:19:34 +0530186 reset_transaction_series()
Nabin Hait1c990352012-07-09 15:25:08 +0530187 else:
188 delete_masters()
Rushabh Mehta4b41afd2012-10-09 15:19:59 +0200189 print "Reset All Series"
Nabin Hait82efcc72012-09-18 11:19:34 +0530190 reset_all_series()
Nabin Hait1c990352012-07-09 15:25:08 +0530191 delete_main_masters()
192 reset_global_defaults()
193
194 print "System cleaned up succesfully"
Rushabh Mehta4b41afd2012-10-09 15:19:59 +0200195 webnotes.conn.commit()
Nabin Hait1c990352012-07-09 15:25:08 +0530196 webnotes.conn.close()
197
198
199if __name__ == '__main__':
200 run()