Rushabh Mehta | e67d1fb | 2013-08-05 14:59:54 +0530 | [diff] [blame] | 1 | # Copyright (c) 2013, Web Notes Technologies Pvt. Ltd. |
| 2 | # License: GNU General Public License v3. See license.txt |
| 3 | |
Nabin Hait | 1c99035 | 2012-07-09 15:25:08 +0530 | [diff] [blame] | 4 | #!/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 Doshi | 486f9df | 2012-07-19 13:40:31 +0530 | [diff] [blame] | 13 | from __future__ import unicode_literals |
Nabin Hait | 1c99035 | 2012-07-09 15:25:08 +0530 | [diff] [blame] | 14 | import sys |
| 15 | sys.path.append("lib/py") |
| 16 | sys.path.append(".") |
| 17 | sys.path.append("erpnext") |
| 18 | |
| 19 | import webnotes |
| 20 | |
| 21 | #-------------------------------- |
| 22 | |
| 23 | def delete_transactions(): |
| 24 | print "Deleting transactions..." |
| 25 | |
Nabin Hait | ac24d0f | 2013-07-01 14:03:08 +0530 | [diff] [blame] | 26 | trans = ['Task', 'Support Ticket', 'Stock Reconciliation', 'Stock Ledger Entry', |
Nabin Hait | bffbc18 | 2013-03-12 10:41:21 +0530 | [diff] [blame] | 27 | '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 Hait | ac24d0f | 2013-07-01 14:03:08 +0530 | [diff] [blame] | 33 | '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 Hait | 1c99035 | 2012-07-09 15:25:08 +0530 | [diff] [blame] | 36 | for d in trans: |
| 37 | for t in webnotes.conn.sql("select options from tabDocField where parent='%s' and fieldtype='Table'" % d): |
| 38 | webnotes.conn.sql("delete from `tab%s`" % (t)) |
| 39 | webnotes.conn.sql("delete from `tab%s`" % (d)) |
Nabin Hait | 1c99035 | 2012-07-09 15:25:08 +0530 | [diff] [blame] | 40 | print "Deleted " + d |
| 41 | |
| 42 | |
| 43 | |
| 44 | def delete_masters(): |
| 45 | print "Deleting masters...." |
| 46 | masters = { |
Nabin Hait | bffbc18 | 2013-03-12 10:41:21 +0530 | [diff] [blame] | 47 | 'Workstation': ['Default Workstation'], |
Nabin Hait | bffbc18 | 2013-03-12 10:41:21 +0530 | [diff] [blame] | 48 | '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 Hait | cfecd2b | 2013-07-11 17:49:18 +0530 | [diff] [blame] | 56 | 'Sales Person': ['Sales Team'], |
Nabin Hait | bffbc18 | 2013-03-12 10:41:21 +0530 | [diff] [blame] | 57 | '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 Hait | 1c99035 | 2012-07-09 15:25:08 +0530 | [diff] [blame] | 67 | 'Compensatory Off', 'Sick Leave'], |
Nabin Hait | bffbc18 | 2013-03-12 10:41:21 +0530 | [diff] [blame] | 68 | 'Appraisal Template': '', |
| 69 | 'Item Group': ['All Item Groups', 'Default'], |
| 70 | 'Item': '', |
| 71 | 'Holiday List': '', |
Nabin Hait | ac24d0f | 2013-07-01 14:03:08 +0530 | [diff] [blame] | 72 | 'Activity Type': '', |
Nabin Hait | bffbc18 | 2013-03-12 10:41:21 +0530 | [diff] [blame] | 73 | '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 Hait | 1c99035 | 2012-07-09 15:25:08 +0530 | [diff] [blame] | 80 | 'Medical Allowance', 'Telephone'], |
Nabin Hait | bffbc18 | 2013-03-12 10:41:21 +0530 | [diff] [blame] | 81 | '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 Hait | 1c99035 | 2012-07-09 15:25:08 +0530 | [diff] [blame] | 95 | 'BOM': '' |
| 96 | } |
| 97 | for d in masters.keys(): |
| 98 | for t in webnotes.conn.sql("select options from tabDocField where parent='%s' \ |
| 99 | and fieldtype='Table'" % d): |
| 100 | webnotes.conn.sql("delete from `tab%s`" % (t)) |
| 101 | lst = '"'+'","'.join(masters[d])+ '"' |
| 102 | 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] | 103 | print "Deleted " + d |
| 104 | |
| 105 | |
| 106 | |
Nabin Hait | 82efcc7 | 2012-09-18 11:19:34 +0530 | [diff] [blame] | 107 | def reset_all_series(): |
| 108 | # Reset master series |
| 109 | webnotes.conn.sql("""update tabSeries set current = 0 where name not in |
| 110 | ('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 Hait | 94a6c7e | 2012-07-09 16:06:53 +0530 | [diff] [blame] | 113 | '_SRCH', '_TRIGGER', '__NSO', 'CustomField', 'Letter') |
Nabin Hait | 1c99035 | 2012-07-09 15:25:08 +0530 | [diff] [blame] | 114 | """) |
| 115 | print "Series updated" |
Nabin Hait | 82efcc7 | 2012-09-18 11:19:34 +0530 | [diff] [blame] | 116 | |
| 117 | def reset_transaction_series(): |
| 118 | webnotes.conn.sql("""update tabSeries set current = 0 where name in |
| 119 | ('JV', 'INV', 'BILL', 'SO', 'DN', 'PO', 'LEAD', 'ENQUIRY', 'ENQ', 'CI', |
Nabin Hait | bffbc18 | 2013-03-12 10:41:21 +0530 | [diff] [blame] | 120 | 'IN', 'PS', 'IDT', 'QAI', 'QTN', 'STE', 'SQTN', 'SUP', 'SR', |
Nabin Hait | 82efcc7 | 2012-09-18 11:19:34 +0530 | [diff] [blame] | 121 | 'POS', 'LAP', 'LAL', 'EXP')""") |
| 122 | print "Series updated" |
Nabin Hait | 1c99035 | 2012-07-09 15:25:08 +0530 | [diff] [blame] | 123 | |
| 124 | |
| 125 | def delete_main_masters(): |
Nabin Hait | bffbc18 | 2013-03-12 10:41:21 +0530 | [diff] [blame] | 126 | main_masters = ['Fiscal Year', 'Company', 'DefaultValue'] |
Nabin Hait | 1c99035 | 2012-07-09 15:25:08 +0530 | [diff] [blame] | 127 | for d in main_masters: |
| 128 | for t in webnotes.conn.sql("select options from tabDocField where parent='%s' and fieldtype='Table'" % d): |
| 129 | webnotes.conn.sql("delete from `tab%s`" % (t)) |
| 130 | webnotes.conn.sql("delete from `tab%s`" % (d)) |
Nabin Hait | 1c99035 | 2012-07-09 15:25:08 +0530 | [diff] [blame] | 131 | print "Deleted " + d |
Nabin Hait | 1c99035 | 2012-07-09 15:25:08 +0530 | [diff] [blame] | 132 | |
| 133 | def reset_global_defaults(): |
| 134 | flds = { |
Nabin Hait | bffbc18 | 2013-03-12 10:41:21 +0530 | [diff] [blame] | 135 | 'default_company': None, |
| 136 | 'default_currency': None, |
| 137 | 'current_fiscal_year': None, |
Nabin Hait | 1c99035 | 2012-07-09 15:25:08 +0530 | [diff] [blame] | 138 | 'date_format': 'dd-mm-yyyy', |
Nabin Hait | bffbc18 | 2013-03-12 10:41:21 +0530 | [diff] [blame] | 139 | 'sms_sender_name': None, |
Nabin Hait | 1c99035 | 2012-07-09 15:25:08 +0530 | [diff] [blame] | 140 | 'default_item_group': 'Default', |
| 141 | 'default_stock_uom': 'Nos', |
| 142 | 'default_valuation_method': 'FIFO', |
Nabin Hait | bffbc18 | 2013-03-12 10:41:21 +0530 | [diff] [blame] | 143 | 'tolerance': None, |
| 144 | 'acc_frozen_upto': None, |
| 145 | 'bde_auth_role': None, |
| 146 | 'credit_controller': None, |
Nabin Hait | 1c99035 | 2012-07-09 15:25:08 +0530 | [diff] [blame] | 147 | 'default_customer_group': 'Default Customer Group', |
| 148 | 'default_territory': 'Default', |
| 149 | 'default_price_list': 'Standard', |
Nabin Hait | bffbc18 | 2013-03-12 10:41:21 +0530 | [diff] [blame] | 150 | 'default_supplier_type': 'Default Supplier Type', |
| 151 | 'hide_currency_symbol': None, |
| 152 | 'default_price_list_currency': None, |
Nabin Hait | 1c99035 | 2012-07-09 15:25:08 +0530 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | from webnotes.model.code import get_obj |
| 156 | gd = get_obj('Global Defaults', 'Global Defaults') |
| 157 | for d in flds: |
| 158 | gd.doc.fields[d] = flds[d] |
| 159 | gd.doc.save() |
| 160 | |
| 161 | webnotes.clear_cache() |
| 162 | |
| 163 | |
| 164 | def run(): |
| 165 | webnotes.connect() |
Rushabh Mehta | 4b41afd | 2012-10-09 15:19:59 +0200 | [diff] [blame] | 166 | webnotes.conn.begin() |
Nabin Hait | 1c99035 | 2012-07-09 15:25:08 +0530 | [diff] [blame] | 167 | |
| 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 Hait | 82efcc7 | 2012-09-18 11:19:34 +0530 | [diff] [blame] | 187 | if cleanup_type == '1': |
Rushabh Mehta | 4b41afd | 2012-10-09 15:19:59 +0200 | [diff] [blame] | 188 | print "Reset Transaction Series" |
Nabin Hait | 82efcc7 | 2012-09-18 11:19:34 +0530 | [diff] [blame] | 189 | reset_transaction_series() |
Nabin Hait | 1c99035 | 2012-07-09 15:25:08 +0530 | [diff] [blame] | 190 | else: |
| 191 | delete_masters() |
Rushabh Mehta | 4b41afd | 2012-10-09 15:19:59 +0200 | [diff] [blame] | 192 | print "Reset All Series" |
Nabin Hait | 82efcc7 | 2012-09-18 11:19:34 +0530 | [diff] [blame] | 193 | reset_all_series() |
Nabin Hait | 1c99035 | 2012-07-09 15:25:08 +0530 | [diff] [blame] | 194 | delete_main_masters() |
| 195 | reset_global_defaults() |
| 196 | |
| 197 | print "System cleaned up succesfully" |
Rushabh Mehta | 4b41afd | 2012-10-09 15:19:59 +0200 | [diff] [blame] | 198 | webnotes.conn.commit() |
Nabin Hait | 1c99035 | 2012-07-09 15:25:08 +0530 | [diff] [blame] | 199 | webnotes.conn.close() |
| 200 | |
| 201 | |
| 202 | if __name__ == '__main__': |
| 203 | run() |