Rushabh Mehta | ad45e31 | 2013-11-20 12:59:58 +0530 | [diff] [blame] | 1 | # Copyright (c) 2013, Web Notes Technologies Pvt. Ltd. and Contributors |
Rushabh Mehta | e67d1fb | 2013-08-05 14:59:54 +0530 | [diff] [blame] | 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 | |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 19 | import frappe |
Nabin Hait | 1c99035 | 2012-07-09 15:25:08 +0530 | [diff] [blame] | 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: |
Nabin Hait | 4d713ac | 2014-03-03 15:51:13 +0530 | [diff] [blame] | 37 | for t in frappe.db.sql("select options from tabDocField \ |
| 38 | where parent=%s and fieldtype='Table'", d): |
Anand Doshi | e9baaa6 | 2014-02-26 12:35:33 +0530 | [diff] [blame] | 39 | frappe.db.sql("delete from `tab%s`" % (t)) |
| 40 | frappe.db.sql("delete from `tab%s`" % (d)) |
Nabin Hait | 1c99035 | 2012-07-09 15:25:08 +0530 | [diff] [blame] | 41 | print "Deleted " + d |
| 42 | |
| 43 | |
| 44 | |
| 45 | def delete_masters(): |
| 46 | print "Deleting masters...." |
| 47 | masters = { |
Nabin Hait | bffbc18 | 2013-03-12 10:41:21 +0530 | [diff] [blame] | 48 | 'Workstation': ['Default Workstation'], |
Nabin Hait | bffbc18 | 2013-03-12 10:41:21 +0530 | [diff] [blame] | 49 | 'Warehouse': ['Default Warehouse'], |
| 50 | 'UOM': ['Kg', 'Mtr', 'Box', 'Ltr', 'Nos', 'Ft', 'Pair', 'Set'], |
| 51 | 'Territory': ['All Territories', 'Default Territory'], |
| 52 | 'Terms and Conditions': '', |
| 53 | 'Tag': '', |
| 54 | 'Supplier Type': ['Default Supplier Type'], |
| 55 | 'Supplier': '', |
| 56 | 'Serial No': '', |
Nabin Hait | cfecd2b | 2013-07-11 17:49:18 +0530 | [diff] [blame] | 57 | 'Sales Person': ['Sales Team'], |
Nabin Hait | bffbc18 | 2013-03-12 10:41:21 +0530 | [diff] [blame] | 58 | 'Sales Partner': '', |
| 59 | 'Sales BOM': '', |
| 60 | 'Salary Structure': '', |
| 61 | 'Purchase Taxes and Charges Master': '', |
| 62 | 'Project': '', |
| 63 | 'Print Heading': '', |
| 64 | 'Price List': ['Default Price List'], |
| 65 | 'Sales Taxes and Charges Master': '', |
| 66 | 'Letter Head': '', |
| 67 | 'Leave Type': ['Leave Without Pay', 'Privilege Leave', 'Casual Leave', 'PL', 'CL', 'LWP', |
Nabin Hait | 1c99035 | 2012-07-09 15:25:08 +0530 | [diff] [blame] | 68 | 'Compensatory Off', 'Sick Leave'], |
Nabin Hait | bffbc18 | 2013-03-12 10:41:21 +0530 | [diff] [blame] | 69 | 'Appraisal Template': '', |
| 70 | 'Item Group': ['All Item Groups', 'Default'], |
| 71 | 'Item': '', |
| 72 | 'Holiday List': '', |
Nabin Hait | ac24d0f | 2013-07-01 14:03:08 +0530 | [diff] [blame] | 73 | 'Activity Type': '', |
Nabin Hait | bffbc18 | 2013-03-12 10:41:21 +0530 | [diff] [blame] | 74 | 'Grade': '', |
| 75 | 'Feed': '', |
| 76 | 'Expense Claim Type': ['Travel', 'Medical', 'Calls', 'Food', 'Others'], |
| 77 | 'Event': '', |
| 78 | 'Employment Type': '', |
| 79 | 'Employee': '', |
| 80 | 'Earning Type': ['Basic', 'Conveyance', 'House Rent Allowance', 'Dearness Allowance', |
Nabin Hait | 1c99035 | 2012-07-09 15:25:08 +0530 | [diff] [blame] | 81 | 'Medical Allowance', 'Telephone'], |
Nabin Hait | bffbc18 | 2013-03-12 10:41:21 +0530 | [diff] [blame] | 82 | 'Designation': '', |
| 83 | 'Department': '', |
| 84 | 'Deduction Type': ['Income Tax', 'Professional Tax', 'Provident Fund', 'Leave Deduction'], |
| 85 | 'Customer Group': ['All Customer Groups', 'Default Customer Group'], |
| 86 | 'Customer': '', |
| 87 | 'Cost Center': '', |
| 88 | 'Contact': '', |
| 89 | 'Campaign': '', |
| 90 | 'Budget Distribution': '', |
| 91 | 'Brand': '', |
| 92 | 'Branch': '', |
| 93 | 'Batch': '', |
| 94 | 'Appraisal': '', |
| 95 | 'Account': '', |
Nabin Hait | 1c99035 | 2012-07-09 15:25:08 +0530 | [diff] [blame] | 96 | 'BOM': '' |
| 97 | } |
| 98 | for d in masters.keys(): |
Nabin Hait | 4d713ac | 2014-03-03 15:51:13 +0530 | [diff] [blame] | 99 | for t in frappe.db.sql("select options from tabDocField where parent=%s \ |
| 100 | and fieldtype='Table'", d): |
| 101 | frappe.db.sql("delete from `tab%s`" % (t)) |
| 102 | frappe.db.sql("delete from `tab%s` where name not in (%s)" % |
| 103 | (d, ', '.join(['%s']*len(masters[d]), masters[d]))) |
Nabin Hait | 1c99035 | 2012-07-09 15:25:08 +0530 | [diff] [blame] | 104 | print "Deleted " + d |
| 105 | |
| 106 | |
| 107 | |
Nabin Hait | 82efcc7 | 2012-09-18 11:19:34 +0530 | [diff] [blame] | 108 | def reset_all_series(): |
| 109 | # Reset master series |
Anand Doshi | e9baaa6 | 2014-02-26 12:35:33 +0530 | [diff] [blame] | 110 | frappe.db.sql("""update tabSeries set current = 0 where name not in |
Nabin Hait | 82efcc7 | 2012-09-18 11:19:34 +0530 | [diff] [blame] | 111 | ('Ann/', 'BSD', 'DEF', 'DF', 'EV', 'Event Updates/', 'FileData-', |
| 112 | 'FL', 'FMD/', 'GLM Detail', 'Login Page/', 'MDI', 'MDR', 'MI', 'MIR', |
| 113 | 'PERM', 'PR', 'SRCH/C/', 'TD', 'TIC/', 'TMD/', 'TW', 'UR', '_FEED', |
Nabin Hait | 94a6c7e | 2012-07-09 16:06:53 +0530 | [diff] [blame] | 114 | '_SRCH', '_TRIGGER', '__NSO', 'CustomField', 'Letter') |
Nabin Hait | 1c99035 | 2012-07-09 15:25:08 +0530 | [diff] [blame] | 115 | """) |
| 116 | print "Series updated" |
Nabin Hait | 82efcc7 | 2012-09-18 11:19:34 +0530 | [diff] [blame] | 117 | |
| 118 | def reset_transaction_series(): |
Anand Doshi | e9baaa6 | 2014-02-26 12:35:33 +0530 | [diff] [blame] | 119 | frappe.db.sql("""update tabSeries set current = 0 where name in |
Nabin Hait | 82efcc7 | 2012-09-18 11:19:34 +0530 | [diff] [blame] | 120 | ('JV', 'INV', 'BILL', 'SO', 'DN', 'PO', 'LEAD', 'ENQUIRY', 'ENQ', 'CI', |
Nabin Hait | bffbc18 | 2013-03-12 10:41:21 +0530 | [diff] [blame] | 121 | 'IN', 'PS', 'IDT', 'QAI', 'QTN', 'STE', 'SQTN', 'SUP', 'SR', |
Nabin Hait | 82efcc7 | 2012-09-18 11:19:34 +0530 | [diff] [blame] | 122 | 'POS', 'LAP', 'LAL', 'EXP')""") |
| 123 | print "Series updated" |
Nabin Hait | 1c99035 | 2012-07-09 15:25:08 +0530 | [diff] [blame] | 124 | |
| 125 | |
| 126 | def delete_main_masters(): |
Nabin Hait | bffbc18 | 2013-03-12 10:41:21 +0530 | [diff] [blame] | 127 | main_masters = ['Fiscal Year', 'Company', 'DefaultValue'] |
Nabin Hait | 1c99035 | 2012-07-09 15:25:08 +0530 | [diff] [blame] | 128 | for d in main_masters: |
Nabin Hait | 4d713ac | 2014-03-03 15:51:13 +0530 | [diff] [blame] | 129 | for t in frappe.db.sql("select options from tabDocField \ |
| 130 | where parent=%s and fieldtype='Table'", d): |
Anand Doshi | e9baaa6 | 2014-02-26 12:35:33 +0530 | [diff] [blame] | 131 | frappe.db.sql("delete from `tab%s`" % (t)) |
| 132 | frappe.db.sql("delete from `tab%s`" % (d)) |
Nabin Hait | 1c99035 | 2012-07-09 15:25:08 +0530 | [diff] [blame] | 133 | print "Deleted " + d |
Nabin Hait | 1c99035 | 2012-07-09 15:25:08 +0530 | [diff] [blame] | 134 | |
| 135 | def reset_global_defaults(): |
| 136 | flds = { |
Nabin Hait | bffbc18 | 2013-03-12 10:41:21 +0530 | [diff] [blame] | 137 | 'default_company': None, |
| 138 | 'default_currency': None, |
| 139 | 'current_fiscal_year': None, |
Nabin Hait | 1c99035 | 2012-07-09 15:25:08 +0530 | [diff] [blame] | 140 | 'date_format': 'dd-mm-yyyy', |
Nabin Hait | bffbc18 | 2013-03-12 10:41:21 +0530 | [diff] [blame] | 141 | 'sms_sender_name': None, |
Nabin Hait | 1c99035 | 2012-07-09 15:25:08 +0530 | [diff] [blame] | 142 | 'default_item_group': 'Default', |
| 143 | 'default_stock_uom': 'Nos', |
| 144 | 'default_valuation_method': 'FIFO', |
Nabin Hait | bffbc18 | 2013-03-12 10:41:21 +0530 | [diff] [blame] | 145 | 'tolerance': None, |
| 146 | 'acc_frozen_upto': None, |
| 147 | 'bde_auth_role': None, |
| 148 | 'credit_controller': None, |
Nabin Hait | 1c99035 | 2012-07-09 15:25:08 +0530 | [diff] [blame] | 149 | 'default_customer_group': 'Default Customer Group', |
| 150 | 'default_territory': 'Default', |
| 151 | 'default_price_list': 'Standard', |
Nabin Hait | bffbc18 | 2013-03-12 10:41:21 +0530 | [diff] [blame] | 152 | 'default_supplier_type': 'Default Supplier Type', |
| 153 | 'hide_currency_symbol': None, |
| 154 | 'default_price_list_currency': None, |
Nabin Hait | 1c99035 | 2012-07-09 15:25:08 +0530 | [diff] [blame] | 155 | } |
| 156 | |
Rushabh Mehta | 0a0f249 | 2014-03-31 17:27:06 +0530 | [diff] [blame^] | 157 | gd = frappe.get_doc('Global Defaults', 'Global Defaults') |
Nabin Hait | 1c99035 | 2012-07-09 15:25:08 +0530 | [diff] [blame] | 158 | for d in flds: |
Anand Doshi | f78d1ae | 2014-03-28 13:55:00 +0530 | [diff] [blame] | 159 | gd.set(d, flds[d]) |
| 160 | gd.save() |
Nabin Hait | 1c99035 | 2012-07-09 15:25:08 +0530 | [diff] [blame] | 161 | |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 162 | frappe.clear_cache() |
Nabin Hait | 1c99035 | 2012-07-09 15:25:08 +0530 | [diff] [blame] | 163 | |
| 164 | |
| 165 | def run(): |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 166 | frappe.connect() |
Anand Doshi | e9baaa6 | 2014-02-26 12:35:33 +0530 | [diff] [blame] | 167 | frappe.db.begin() |
Nabin Hait | 1c99035 | 2012-07-09 15:25:08 +0530 | [diff] [blame] | 168 | |
| 169 | # Confirmation from user |
| 170 | confirm = '' |
| 171 | while not confirm: |
| 172 | confirm = raw_input("Are you sure you want to delete the data from the system (N/Y)?") |
| 173 | if confirm.lower() != 'y': |
| 174 | raise Exception |
| 175 | |
| 176 | cleanup_type = '' |
| 177 | while cleanup_type not in ['1', '2']: |
| 178 | cleanup_type = raw_input("""\nWhat type of cleanup you want ot perform? |
| 179 | 1. Only Transactions |
| 180 | 2. Both Masters and Transactions |
| 181 | |
| 182 | Please enter your choice (1/2): |
| 183 | """) |
| 184 | |
| 185 | # delete |
| 186 | delete_transactions() |
| 187 | |
Nabin Hait | 82efcc7 | 2012-09-18 11:19:34 +0530 | [diff] [blame] | 188 | if cleanup_type == '1': |
Rushabh Mehta | 4b41afd | 2012-10-09 15:19:59 +0200 | [diff] [blame] | 189 | print "Reset Transaction Series" |
Nabin Hait | 82efcc7 | 2012-09-18 11:19:34 +0530 | [diff] [blame] | 190 | reset_transaction_series() |
Nabin Hait | 1c99035 | 2012-07-09 15:25:08 +0530 | [diff] [blame] | 191 | else: |
| 192 | delete_masters() |
Rushabh Mehta | 4b41afd | 2012-10-09 15:19:59 +0200 | [diff] [blame] | 193 | print "Reset All Series" |
Nabin Hait | 82efcc7 | 2012-09-18 11:19:34 +0530 | [diff] [blame] | 194 | reset_all_series() |
Nabin Hait | 1c99035 | 2012-07-09 15:25:08 +0530 | [diff] [blame] | 195 | delete_main_masters() |
| 196 | reset_global_defaults() |
| 197 | |
| 198 | print "System cleaned up succesfully" |
Anand Doshi | e9baaa6 | 2014-02-26 12:35:33 +0530 | [diff] [blame] | 199 | frappe.db.commit() |
| 200 | frappe.db.close() |
Nabin Hait | 1c99035 | 2012-07-09 15:25:08 +0530 | [diff] [blame] | 201 | |
| 202 | |
| 203 | if __name__ == '__main__': |
| 204 | run() |