feature to delete company

This commit is contained in:
Rushabh Mehta 2015-04-17 11:47:55 +05:30
parent 63ea99e929
commit 0939a5ac4c
5 changed files with 125 additions and 37 deletions

View File

@ -3,6 +3,45 @@
frappe.provide("erpnext.company");
frappe.ui.form.on("Company", {
onload_post_render: function(frm) {
frm.get_field("delete_company").$input.addClass("btn-danger");
},
country: function(frm) {
erpnext.company.set_chart_of_accounts_options(frm.doc);
},
delete_company: 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.")},
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",
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();
}
}
});
}, __("Delete Comany and all Related Transactions"), __("Delete"));
d.get_primary_btn().addClass("btn-danger");
}
});
cur_frm.cscript.refresh = function(doc, cdt, cdn) {
if(doc.abbr && !doc.__islocal) {
cur_frm.set_df_property("abbr", "read_only", 1);
@ -16,10 +55,6 @@ cur_frm.cscript.refresh = function(doc, cdt, cdn) {
erpnext.company.set_chart_of_accounts_options(doc);
}
frappe.ui.form.on("Company", "country", function(frm) {
erpnext.company.set_chart_of_accounts_options(frm.doc);
})
erpnext.company.set_chart_of_accounts_options = function(doc) {
var selected_value = doc.chart_of_accounts;
if(doc.country) {

View File

@ -377,7 +377,7 @@
"read_only": 0
},
{
"description": "Company registration numbers for your reference. Example: VAT Registration Numbers etc.",
"description": "",
"fieldname": "registration_info",
"fieldtype": "Section Break",
"label": "",
@ -395,11 +395,18 @@
"oldfieldtype": "Code",
"permlevel": 0,
"read_only": 0
},
{
"fieldname": "delete_company",
"fieldtype": "Button",
"label": "Delete Company",
"permlevel": 0,
"precision": ""
}
],
"icon": "icon-building",
"idx": 1,
"modified": "2015-02-25 06:28:13.565128",
"modified": "2015-04-17 01:37:32.304374",
"modified_by": "Administrator",
"module": "Setup",
"name": "Company",

View File

@ -156,35 +156,6 @@ class Company(Document):
frappe.db.set(self, "cost_center", _("Main") + " - " + self.abbr)
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 tabAccount
frappe.db.sql("delete from `tabAccount` where company = %s order by lft desc, rgt desc", 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 order by lft desc, rgt desc", self.name)
# delete account from customer and supplier
frappe.db.sql("delete from `tabParty Account` where company=%s", self.name)
# delete email digest
frappe.db.sql("delete from `tabEmail Digest` 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)
def before_rename(self, olddn, newdn, merge=False):
if merge:
frappe.throw(_("Sorry, companies cannot be merged"))

View File

@ -0,0 +1,74 @@
# Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors
# License: GNU General Public License v3. See license.txt
from __future__ import unicode_literals
import frappe
from frappe.utils import cint
from frappe import _
@frappe.whitelist()
def delete_company(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)
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)
def delete_for_doctype(doctype, company_name):
meta = frappe.get_meta(doctype)
company_fieldname = meta.get("fields", {"fieldtype": "Link",
"options": "Company"})[0].fieldname
if meta.issingle:
single = frappe.get_doc(doctype, doctype)
single.set(company_fieldname, "")
single.flags.ignore_mandatory = True
single.save()
else:
if not meta.istable:
# delete children
for df in meta.get_table_fields():
frappe.db.sql("""delete from `tab{0}` where parent in
(select name from `tab{1}` where `{2}`=%s)""".format(df.options,
doctype, company_fieldname), company_name)
# delete parent
frappe.db.sql("""delete from `tab{0}`
where {1}= %s """.format(doctype, company_fieldname), company_name)
# reset series
naming_series = meta.get_field("naming_series")
if naming_series:
prefixes = sorted(naming_series.options.split("\n"), lambda a, b: len(b) - len(a))
for prefix in prefixes:
if prefix:
last = frappe.db.sql("""select max(name) from `tab{0}`
where name like %s""".format(doctype), prefix + "%")
if last and last[0][0]:
last = cint(last[0][0].replace(prefix, ""))
else:
last = 0
frappe.db.sql("""update tabSeries set current = %s
where name=%s""", (last, prefix))
def delete_bins(company_name):
frappe.db.sql("""delete from tabBin where warehouse in
(select name from tabWarehouse where company=%s)""", company_name)

View File

@ -75,7 +75,7 @@
"icon": "icon-tags",
"idx": 1,
"max_attachments": 1,
"modified": "2015-02-12 17:39:02.825767",
"modified": "2015-04-17 01:54:01.907175",
"modified_by": "Administrator",
"module": "Stock",
"name": "Price List",
@ -130,5 +130,6 @@
"report": 0,
"role": "Manufacturing User"
}
]
],
"search_fields": "currency"
}