Delete company and company transactions are separate functions

This commit is contained in:
Nabin Hait 2015-05-04 11:51:04 +05:30
parent e991c9e075
commit c91a9e5231
5 changed files with 43 additions and 25 deletions

View File

@ -40,7 +40,7 @@ frappe.ui.form.on(cur_frm.doctype, {
frm.get_docfield("taxes", "rate").reqd = 0; frm.get_docfield("taxes", "rate").reqd = 0;
frm.get_docfield("taxes", "tax_amount").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") { if(d.charge_type==="Actual") {
d.rate = 0; d.rate = 0;
if(!d.tax_amount) { if(!d.tax_amount) {

View File

@ -5,37 +5,35 @@ frappe.provide("erpnext.company");
frappe.ui.form.on("Company", { frappe.ui.form.on("Company", {
onload_post_render: function(frm) { 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) { country: function(frm) {
erpnext.company.set_chart_of_accounts_options(frm.doc); erpnext.company.set_chart_of_accounts_options(frm.doc);
}, },
delete_company: function(frm) { delete_company_transactions: function(frm) {
var d = frappe.prompt({ var d = frappe.prompt({
fieldtype:"Data", fieldtype:"Data",
fieldname: "company_name", fieldname: "company_name",
label: __("Please re-type company name to confirm"), label: __("Please re-type company name to confirm"),
reqd: 1, 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) { function(data) {
if(data.company_name !== frm.doc.name) { if(data.company_name !== frm.doc.name) {
frappe.msgprint("Company name not same"); frappe.msgprint("Company name not same");
return; return;
} }
frappe.call({ frappe.call({
method: "erpnext.setup.doctype.company.delete_company.delete_company", method: "erpnext.setup.doctype.company.delete_company_transactions.delete_company_transactions",
args: { args: {
company_name: data.company_name company_name: data.company_name
}, },
freeze: true, freeze: true,
callback: function(r) { callback: function(r, rt) {
if(!r.exc) { if(!r.exc)
frappe.model.clear_doc("Company", data.company_name); frappe.msgprint(__("Successfully deleted all transactions related to this company!"));
window.history.back();
}
} }
}); });
}, __("Delete Comany and all Related Transactions"), __("Delete")); }, __("Delete all the Transactions for this Company"), __("Delete"));
d.get_primary_btn().addClass("btn-danger"); d.get_primary_btn().addClass("btn-danger");
} }

View File

@ -397,16 +397,16 @@
"read_only": 0 "read_only": 0
}, },
{ {
"fieldname": "delete_company", "fieldname": "delete_company_transactions",
"fieldtype": "Button", "fieldtype": "Button",
"label": "Delete Company", "label": "Delete Company Transactions",
"permlevel": 0, "permlevel": 0,
"precision": "" "precision": ""
} }
], ],
"icon": "icon-building", "icon": "icon-building",
"idx": 1, "idx": 1,
"modified": "2015-04-17 01:37:32.304374", "modified": "2015-05-04 11:22:42.116328",
"modified_by": "Administrator", "modified_by": "Administrator",
"module": "Setup", "module": "Setup",
"name": "Company", "name": "Company",

View File

@ -168,6 +168,33 @@ class Company(Document):
frappe.defaults.clear_cache() 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() @frappe.whitelist()
def replace_abbr(company, old, new): def replace_abbr(company, old, new):
frappe.only_for("System Manager") frappe.only_for("System Manager")

View File

@ -8,26 +8,19 @@ from frappe.utils import cint
from frappe import _ from frappe import _
@frappe.whitelist() @frappe.whitelist()
def delete_company(company_name): def delete_company_transactions(company_name):
frappe.only_for("System Manager") frappe.only_for("System Manager")
doc = frappe.get_doc("Company", company_name) doc = frappe.get_doc("Company", company_name)
if frappe.session.user != doc.owner: 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) delete_bins(company_name)
for doctype in frappe.db.sql_list("""select parent from for doctype in frappe.db.sql_list("""select parent from
tabDocField where fieldtype='Link' and options='Company'"""): tabDocField where fieldtype='Link' and options='Company'"""):
delete_for_doctype(doctype, company_name) if doctype not in ("Account", "Cost Center", "Warehouse", "Budget Detail", "Party Account"):
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)
def delete_for_doctype(doctype, company_name): def delete_for_doctype(doctype, company_name):
meta = frappe.get_meta(doctype) meta = frappe.get_meta(doctype)