Nabin Hait | 1c99035 | 2012-07-09 15:25:08 +0530 | [diff] [blame] | 1 | #!/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 Doshi | 486f9df | 2012-07-19 13:40:31 +0530 | [diff] [blame] | 10 | from __future__ import unicode_literals |
Nabin Hait | 1c99035 | 2012-07-09 15:25:08 +0530 | [diff] [blame] | 11 | import sys |
| 12 | sys.path.append("lib/py") |
| 13 | sys.path.append(".") |
| 14 | sys.path.append("erpnext") |
| 15 | |
| 16 | import webnotes |
| 17 | |
| 18 | #-------------------------------- |
| 19 | |
| 20 | def delete_transactions(): |
| 21 | print "Deleting transactions..." |
| 22 | |
| 23 | trans = ['Timesheet','Task','Support Ticket','Stock Reconciliation', 'Stock Ledger Entry', \ |
| 24 | 'Stock Entry','Sales Order','Salary Slip','Sales Invoice','Quotation', 'Quality Inspection', \ |
| 25 | 'Purchase Receipt','Purchase Order','Production Order', 'POS Setting','Period Closing Voucher', \ |
| 26 | 'Purchase Invoice','Maintenance Visit','Maintenance Schedule','Leave Application', \ |
| 27 | 'Leave Allocation', 'Lead', 'Journal Voucher', 'Installation Note','Purchase Request', \ |
| 28 | 'GL Entry','Expense Claim','Opportunity','Delivery Note','Customer Issue','Bin', \ |
Anand Doshi | 440c1eb | 2012-09-28 16:03:11 +0530 | [diff] [blame] | 29 | 'Authorization Rule','Attendance', 'C-Form', 'Form 16A', 'Lease Agreement', \ |
Nabin Hait | 1c99035 | 2012-07-09 15:25:08 +0530 | [diff] [blame] | 30 | 'Lease Installment', 'TDS Payment', 'TDS Return Acknowledgement', 'Appraisal', \ |
| 31 | 'Installation Note', 'Communication' |
| 32 | ] |
| 33 | for d in trans: |
| 34 | for t in webnotes.conn.sql("select options from tabDocField where parent='%s' and fieldtype='Table'" % d): |
| 35 | webnotes.conn.sql("delete from `tab%s`" % (t)) |
| 36 | webnotes.conn.sql("delete from `tab%s`" % (d)) |
Nabin Hait | 1c99035 | 2012-07-09 15:25:08 +0530 | [diff] [blame] | 37 | print "Deleted " + d |
| 38 | |
| 39 | |
| 40 | |
| 41 | def delete_masters(): |
| 42 | print "Deleting masters...." |
| 43 | masters = { |
| 44 | 'Workstation':['Default Workstation'], |
| 45 | 'Warehouse Type':['Default Warehouse Type', 'Fixed Asset', 'Rejected', 'Reserved', |
| 46 | 'Sample', 'Stores', 'WIP Warehouse'], |
| 47 | 'Warehouse':['Default Warehouse'], |
| 48 | 'UOM':['Kg', 'Mtr', 'Box', 'Ltr', 'Nos', 'Ft', 'Pair', 'Set'], |
| 49 | 'Territory':['All Territories', 'Default Territory'], |
| 50 | 'Terms and Conditions':'', |
| 51 | 'Tag':'', |
| 52 | 'Supplier Type':['Default Supplier Type'], |
| 53 | 'Supplier':'', |
| 54 | 'Serial No':'', |
| 55 | 'Sales Person':['All Sales Persons'], |
| 56 | 'Sales Partner':'', |
| 57 | 'Sales BOM':'', |
| 58 | 'Salary Structure':'', |
| 59 | 'Purchase Taxes and Charges Master':'', |
| 60 | 'Project':'', |
| 61 | 'Print Heading':'', |
| 62 | 'Price List':['Default Price List'], |
Nabin Hait | 1c99035 | 2012-07-09 15:25:08 +0530 | [diff] [blame] | 63 | 'Sales Taxes and Charges Master':'', |
| 64 | 'Letter Head':'', |
| 65 | 'Leave Type':['Leave Without Pay', 'Privilege Leave', 'Casual Leave', 'PL', 'CL', 'LWP', |
| 66 | 'Compensatory Off', 'Sick Leave'], |
| 67 | 'Landed Cost Master':'', |
| 68 | 'Appraisal Template':'', |
| 69 | 'Item Group':['All Item Groups', 'Default'], |
| 70 | 'Item':'', |
| 71 | 'Holiday List':'', |
| 72 | 'Grade':'', |
| 73 | 'Feed':'', |
| 74 | 'Expense Claim Type':['Travel', 'Medical', 'Calls', 'Food', 'Others'], |
| 75 | 'Event':'', |
| 76 | 'Employment Type':'', |
| 77 | 'Employee':'', |
| 78 | 'Earning Type':['Basic', 'Conveyance', 'House Rent Allowance', 'Dearness Allowance', |
| 79 | 'Medical Allowance', 'Telephone'], |
| 80 | 'Designation':'', |
| 81 | 'Department':'', |
| 82 | 'Deduction Type':['Income Tax', 'Professional Tax', 'Provident Fund', 'Leave Deduction'], |
| 83 | 'Customer Group':['All Customer Groups', 'Default Customer Group'], |
| 84 | 'Customer':'', |
| 85 | 'Cost Center':'', |
| 86 | 'Contact':'', |
| 87 | 'Campaign':'', |
| 88 | 'Budget Distribution':'', |
| 89 | 'Brand':'', |
| 90 | 'Branch':'', |
| 91 | 'Batch':'', |
| 92 | 'Appraisal':'', |
| 93 | 'Account':'', |
| 94 | 'BOM': '' |
| 95 | } |
| 96 | for d in masters.keys(): |
| 97 | for t in webnotes.conn.sql("select options from tabDocField where parent='%s' \ |
| 98 | and fieldtype='Table'" % d): |
| 99 | webnotes.conn.sql("delete from `tab%s`" % (t)) |
| 100 | lst = '"'+'","'.join(masters[d])+ '"' |
| 101 | webnotes.conn.sql("delete from `tab%s` where name not in (%s)" % (d, lst)) |
Nabin Hait | 1c99035 | 2012-07-09 15:25:08 +0530 | [diff] [blame] | 102 | print "Deleted " + d |
| 103 | |
| 104 | |
| 105 | |
Nabin Hait | 82efcc7 | 2012-09-18 11:19:34 +0530 | [diff] [blame] | 106 | def reset_all_series(): |
| 107 | # Reset master series |
| 108 | webnotes.conn.sql("""update tabSeries set current = 0 where name not in |
| 109 | ('Ann/', 'BSD', 'DEF', 'DF', 'EV', 'Event Updates/', 'FileData-', |
| 110 | 'FL', 'FMD/', 'GLM Detail', 'Login Page/', 'MDI', 'MDR', 'MI', 'MIR', |
| 111 | 'PERM', 'PR', 'SRCH/C/', 'TD', 'TIC/', 'TMD/', 'TW', 'UR', '_FEED', |
Nabin Hait | 94a6c7e | 2012-07-09 16:06:53 +0530 | [diff] [blame] | 112 | '_SRCH', '_TRIGGER', '__NSO', 'CustomField', 'Letter') |
Nabin Hait | 1c99035 | 2012-07-09 15:25:08 +0530 | [diff] [blame] | 113 | """) |
| 114 | print "Series updated" |
Nabin Hait | 82efcc7 | 2012-09-18 11:19:34 +0530 | [diff] [blame] | 115 | |
| 116 | def reset_transaction_series(): |
| 117 | webnotes.conn.sql("""update tabSeries set current = 0 where name in |
| 118 | ('JV', 'INV', 'BILL', 'SO', 'DN', 'PO', 'LEAD', 'ENQUIRY', 'ENQ', 'CI', |
| 119 | 'IN', 'PS', 'IDT', 'QAI', 'QTN', 'STE', 'SQTN', 'SUP', 'TDSP', 'SR', |
| 120 | 'POS', 'LAP', 'LAL', 'EXP')""") |
| 121 | print "Series updated" |
Nabin Hait | 1c99035 | 2012-07-09 15:25:08 +0530 | [diff] [blame] | 122 | |
| 123 | |
| 124 | def delete_main_masters(): |
| 125 | main_masters = ['Fiscal Year','Company', 'DefaultValue'] |
| 126 | for d in main_masters: |
| 127 | for t in webnotes.conn.sql("select options from tabDocField where parent='%s' and fieldtype='Table'" % d): |
| 128 | webnotes.conn.sql("delete from `tab%s`" % (t)) |
| 129 | webnotes.conn.sql("delete from `tab%s`" % (d)) |
Nabin Hait | 1c99035 | 2012-07-09 15:25:08 +0530 | [diff] [blame] | 130 | print "Deleted " + d |
| 131 | |
| 132 | |
| 133 | |
| 134 | def reset_global_defaults(): |
| 135 | flds = { |
| 136 | 'default_company': '', |
| 137 | 'default_currency': '', |
| 138 | 'default_currency_format': 'Lacs', |
| 139 | 'default_currency_fraction': '', |
| 140 | 'current_fiscal_year': '', |
| 141 | 'date_format': 'dd-mm-yyyy', |
| 142 | 'sms_sender_name': '', |
| 143 | 'default_item_group': 'Default', |
| 144 | 'default_stock_uom': 'Nos', |
| 145 | 'default_valuation_method': 'FIFO', |
| 146 | 'default_warehouse_type': 'Default Warehouse Type', |
| 147 | 'tolerance': '', |
| 148 | 'acc_frozen_upto': '', |
| 149 | 'bde_auth_role': '', |
| 150 | 'credit_controller': '', |
| 151 | 'default_customer_group': 'Default Customer Group', |
| 152 | 'default_territory': 'Default', |
| 153 | 'default_price_list': 'Standard', |
| 154 | 'default_supplier_type': 'Default Supplier Type' |
| 155 | } |
| 156 | |
| 157 | from webnotes.model.code import get_obj |
| 158 | gd = get_obj('Global Defaults', 'Global Defaults') |
| 159 | for d in flds: |
| 160 | gd.doc.fields[d] = flds[d] |
| 161 | gd.doc.save() |
| 162 | |
| 163 | webnotes.clear_cache() |
| 164 | |
| 165 | |
| 166 | def run(): |
| 167 | webnotes.connect() |
Rushabh Mehta | 4b41afd | 2012-10-09 15:19:59 +0200 | [diff] [blame] | 168 | webnotes.conn.begin() |
Nabin Hait | 1c99035 | 2012-07-09 15:25:08 +0530 | [diff] [blame] | 169 | |
| 170 | # Confirmation from user |
| 171 | confirm = '' |
| 172 | while not confirm: |
| 173 | confirm = raw_input("Are you sure you want to delete the data from the system (N/Y)?") |
| 174 | if confirm.lower() != 'y': |
| 175 | raise Exception |
| 176 | |
| 177 | cleanup_type = '' |
| 178 | while cleanup_type not in ['1', '2']: |
| 179 | cleanup_type = raw_input("""\nWhat type of cleanup you want ot perform? |
| 180 | 1. Only Transactions |
| 181 | 2. Both Masters and Transactions |
| 182 | |
| 183 | Please enter your choice (1/2): |
| 184 | """) |
| 185 | |
| 186 | # delete |
| 187 | delete_transactions() |
| 188 | |
Nabin Hait | 82efcc7 | 2012-09-18 11:19:34 +0530 | [diff] [blame] | 189 | if cleanup_type == '1': |
Rushabh Mehta | 4b41afd | 2012-10-09 15:19:59 +0200 | [diff] [blame] | 190 | print "Reset Transaction Series" |
Nabin Hait | 82efcc7 | 2012-09-18 11:19:34 +0530 | [diff] [blame] | 191 | reset_transaction_series() |
Nabin Hait | 1c99035 | 2012-07-09 15:25:08 +0530 | [diff] [blame] | 192 | else: |
| 193 | delete_masters() |
Rushabh Mehta | 4b41afd | 2012-10-09 15:19:59 +0200 | [diff] [blame] | 194 | print "Reset All Series" |
Nabin Hait | 82efcc7 | 2012-09-18 11:19:34 +0530 | [diff] [blame] | 195 | reset_all_series() |
Nabin Hait | 1c99035 | 2012-07-09 15:25:08 +0530 | [diff] [blame] | 196 | delete_main_masters() |
| 197 | reset_global_defaults() |
| 198 | |
| 199 | print "System cleaned up succesfully" |
Rushabh Mehta | 4b41afd | 2012-10-09 15:19:59 +0200 | [diff] [blame] | 200 | webnotes.conn.commit() |
Nabin Hait | 1c99035 | 2012-07-09 15:25:08 +0530 | [diff] [blame] | 201 | webnotes.conn.close() |
| 202 | |
| 203 | |
| 204 | if __name__ == '__main__': |
| 205 | run() |