diff --git a/erpnext/public/js/controllers/accounts.js b/erpnext/public/js/controllers/accounts.js index 15e043b81a9..212d2ae4f1b 100644 --- a/erpnext/public/js/controllers/accounts.js +++ b/erpnext/public/js/controllers/accounts.js @@ -40,7 +40,7 @@ frappe.ui.form.on(cur_frm.doctype, { frm.get_docfield("taxes", "rate").reqd = 0; frm.get_docfield("taxes", "tax_amount").reqd = 0; - $.each(frm.doc.taxes, function(i, d) { + $.each(frm.doc.taxes || [], function(i, d) { if(d.charge_type==="Actual") { d.rate = 0; if(!d.tax_amount) { diff --git a/erpnext/setup/doctype/company/company.js b/erpnext/setup/doctype/company/company.js index 115fdfaf638..817fdb629ac 100644 --- a/erpnext/setup/doctype/company/company.js +++ b/erpnext/setup/doctype/company/company.js @@ -5,37 +5,35 @@ frappe.provide("erpnext.company"); frappe.ui.form.on("Company", { onload_post_render: function(frm) { - frm.get_field("delete_company").$input.addClass("btn-danger"); + frm.get_field("delete_company_transactions").$input.addClass("btn-danger"); }, country: function(frm) { erpnext.company.set_chart_of_accounts_options(frm.doc); }, - delete_company: function(frm) { + delete_company_transactions: function(frm) { var d = frappe.prompt({ fieldtype:"Data", fieldname: "company_name", label: __("Please re-type company name to confirm"), reqd: 1, - description: __("Please make sure you really want to delete this company and all its transactions. Your master data will remain as it is. This action cannot be undone.")}, + description: __("Please make sure you really want to delete all the transactions for this company. Your master data will remain as it is. This action cannot be undone.")}, function(data) { if(data.company_name !== frm.doc.name) { frappe.msgprint("Company name not same"); return; } frappe.call({ - method: "erpnext.setup.doctype.company.delete_company.delete_company", + method: "erpnext.setup.doctype.company.delete_company_transactions.delete_company_transactions", args: { company_name: data.company_name }, freeze: true, - callback: function(r) { - if(!r.exc) { - frappe.model.clear_doc("Company", data.company_name); - window.history.back(); - } + callback: function(r, rt) { + if(!r.exc) + frappe.msgprint(__("Successfully deleted all transactions related to this company!")); } }); - }, __("Delete Comany and all Related Transactions"), __("Delete")); + }, __("Delete all the Transactions for this Company"), __("Delete")); d.get_primary_btn().addClass("btn-danger"); } diff --git a/erpnext/setup/doctype/company/company.json b/erpnext/setup/doctype/company/company.json index f7faf966ae7..0e7b17a148c 100644 --- a/erpnext/setup/doctype/company/company.json +++ b/erpnext/setup/doctype/company/company.json @@ -397,16 +397,16 @@ "read_only": 0 }, { - "fieldname": "delete_company", + "fieldname": "delete_company_transactions", "fieldtype": "Button", - "label": "Delete Company", + "label": "Delete Company Transactions", "permlevel": 0, "precision": "" } ], "icon": "icon-building", "idx": 1, - "modified": "2015-04-17 01:37:32.304374", + "modified": "2015-05-04 11:22:42.116328", "modified_by": "Administrator", "module": "Setup", "name": "Company", diff --git a/erpnext/setup/doctype/company/company.py b/erpnext/setup/doctype/company/company.py index 26a2797cc9d..ad89114bd46 100644 --- a/erpnext/setup/doctype/company/company.py +++ b/erpnext/setup/doctype/company/company.py @@ -167,6 +167,33 @@ class Company(Document): where defkey='Company' and defvalue=%s""", (newdn, olddn)) frappe.defaults.clear_cache() + + def on_trash(self): + """ + Trash accounts and cost centers for this company if no gl entry exists + """ + rec = frappe.db.sql("SELECT name from `tabGL Entry` where company = %s", self.name) + if not rec: + # delete Account + frappe.db.sql("delete from `tabAccount` where company = %s", self.name) + + # delete cost center child table - budget detail + frappe.db.sql("""delete bd.* from `tabBudget Detail` bd, `tabCost Center` cc + where bd.parent = cc.name and cc.company = %s""", self.name) + #delete cost center + frappe.db.sql("delete from `tabCost Center` WHERE company = %s", self.name) + + # delete account from customer and supplier + frappe.db.sql("delete from `tabParty Account` where company=%s", self.name) + + if not frappe.db.get_value("Stock Ledger Entry", {"company": self.name}): + frappe.db.sql("""delete from `tabWarehouse` where company=%s""", self.name) + + frappe.defaults.clear_default("company", value=self.name) + + frappe.db.sql("""update `tabSingles` set value="" + where doctype='Global Defaults' and field='default_company' + and value=%s""", self.name) @frappe.whitelist() def replace_abbr(company, old, new): diff --git a/erpnext/setup/doctype/company/delete_company.py b/erpnext/setup/doctype/company/delete_company_transactions.py similarity index 82% rename from erpnext/setup/doctype/company/delete_company.py rename to erpnext/setup/doctype/company/delete_company_transactions.py index 92b6c5273fb..f27ba86211c 100644 --- a/erpnext/setup/doctype/company/delete_company.py +++ b/erpnext/setup/doctype/company/delete_company_transactions.py @@ -8,26 +8,19 @@ from frappe.utils import cint from frappe import _ @frappe.whitelist() -def delete_company(company_name): +def delete_company_transactions(company_name): frappe.only_for("System Manager") doc = frappe.get_doc("Company", company_name) if frappe.session.user != doc.owner: - frappe.throw(_("Company can only be deleted by the creator"), frappe.PermissionError) + frappe.throw(_("Transactions can only be deleted by the creator of the Company"), frappe.PermissionError) delete_bins(company_name) for doctype in frappe.db.sql_list("""select parent from tabDocField where fieldtype='Link' and options='Company'"""): - delete_for_doctype(doctype, company_name) - - frappe.delete_doc("Company", company_name) - - frappe.defaults.clear_default("company", value=doc.name) - - frappe.db.sql("""update `tabSingles` set value="" - where doctype='Global Defaults' and field='default_company' - and value=%s""", doc.name) + if doctype not in ("Account", "Cost Center", "Warehouse", "Budget Detail", "Party Account"): + delete_for_doctype(doctype, company_name) def delete_for_doctype(doctype, company_name): meta = frappe.get_meta(doctype)