From 6a0561612f5caa59b1f9d962f58f367b2808f1b1 Mon Sep 17 00:00:00 2001 From: Nabin Hait Date: Mon, 10 Oct 2011 12:23:10 +0530 Subject: [PATCH] cost center groupto ledger and vice versa --- .../doctype/budget_control/budget_control.js | 59 ----------------- .../doctype/cost_center/cost_center.js | 65 ++++++++++++++----- .../doctype/cost_center/cost_center.py | 29 +++++++++ .../doctype/cost_center/cost_center.txt | 26 +++++++- erpnext/patches/patch.py | 4 +- 5 files changed, 104 insertions(+), 79 deletions(-) delete mode 100644 erpnext/accounts/doctype/budget_control/budget_control.js diff --git a/erpnext/accounts/doctype/budget_control/budget_control.js b/erpnext/accounts/doctype/budget_control/budget_control.js deleted file mode 100644 index 3d4d1c1d8c..0000000000 --- a/erpnext/accounts/doctype/budget_control/budget_control.js +++ /dev/null @@ -1,59 +0,0 @@ -class DocType: - def __init__(self,d,dl): - self.doc, self.doclist = d, dl - - # Get monthly budget - #------------------- - def get_monthly_budget(self, distribution_id, cfy, st_date, post_dt, budget_allocated): - - # get month_list - st_date, post_dt = getdate(st_date), getdate(post_dt) - - if distribution_id: - if st_date.month <= post_dt.month: - tot_per_allocated = sql("select ifnull(sum(percentage_allocation),0) from `tabBudget Distribution Detail` where parent='%s' and idx between '%s' and '%s'" % (distribution_id, st_date.month, post_dt.month))[0][0] - - if st_date.month > post_dt.month: - - tot_per_allocated = flt(sql("select ifnull(sum(percentage_allocation),0) from `tabBudget Distribution Detail` where parent='%s' and idx between '%s' and '%s'" % (distribution_id, st_date.month, 12 ))[0][0]) - tot_per_allocated = flt(tot_per_allocated) + flt(sql("select ifnull(sum(percentage_allocation),0) from `tabBudget Distribution Detail` where parent='%s' and idx between '%s' and '%s'" % (distribution_id, 1, post_dt.month))[0][0]) - - return (flt(budget_allocated) * flt(tot_per_allocated)) / 100 - period_diff = sql("select PERIOD_DIFF('%s','%s')" % (post_dt.strftime('%Y%m'), st_date.strftime('%Y%m'))) - - return (flt(budget_allocated) * (flt(period_diff[0][0]) + 1)) / 12 - - def validate_budget(self, acct, cost_center, actual, budget, action): - # action if actual exceeds budget - if flt(actual) > flt(budget): - msgprint("Your monthly expense "+ cstr((action == 'stop') and "will exceed" or "has exceeded") +" budget for Account - "+cstr(acct)+" under Cost Center - "+ cstr(cost_center) + ""+cstr((action == 'Stop') and ", you can not have this transaction." or ".")) - if action == 'Stop': raise Exception - - def check_budget(self,le_list,cancel): - # get value from record - acct, cost_center, debit, credit, post_dt, cfy, company = le_list - - # get allocated budget - bgt = sql("select t1.budget_allocated, t1.actual, t2.distribution_id from `tabBudget Detail` t1, `tabCost Center` t2 where t1.account='%s' and t1.parent=t2.name and t2.name = '%s' and t1.fiscal_year='%s'" % (acct,cost_center,cfy), as_dict =1) - curr_amt = ((cancel and -1 or 1) * flt(debit)) + ((cancel and 1 or -1) * flt(credit)) - - if bgt and bgt[0]['budget_allocated']: - # check budget flag in Company - bgt_flag = sql("select yearly_bgt_flag, monthly_bgt_flag from `tabCompany` where name = '%s'" % company, as_dict =1) - - if bgt_flag and bgt_flag[0]['monthly_bgt_flag'] in ['Stop', 'Warn']: - # get start date and last date - st_date = get_value('Fiscal Year', cfy, 'year_start_date').strftime('%Y-%m-%d') - lt_date = sql("select LAST_DAY('%s')" % post_dt) - - # get Actual - actual = get_obj('GL Control').get_period_difference(acct + '~~~' + cstr(st_date) + '~~~' + cstr(lt_date[0][0]), cost_center) - - # Get Monthly budget - budget = self.get_monthly_budget(bgt and bgt[0]['distribution_id'] or '' , cfy, st_date, post_dt, bgt[0]['budget_allocated']) - - # validate monthly budget - self.validate_budget(acct, cost_center, flt(actual) + flt(curr_amt), budget, 'monthly_bgt_flag') - - # update actual against budget allocated in cost center - sql("update `tabBudget Detail` set actual = ifnull(actual,0) + %s where account = '%s' and fiscal_year = '%s' and parent = '%s'" % (curr_amt,cstr(acct),cstr(cfy),cstr(cost_center))) \ No newline at end of file diff --git a/erpnext/accounts/doctype/cost_center/cost_center.js b/erpnext/accounts/doctype/cost_center/cost_center.js index 24dc50f2fb..58f39e0ebf 100644 --- a/erpnext/accounts/doctype/cost_center/cost_center.js +++ b/erpnext/accounts/doctype/cost_center/cost_center.js @@ -2,38 +2,71 @@ //Account filtering for cost center cur_frm.fields_dict['budget_details'].grid.get_field('account').get_query = function(doc) { - var mydoc = locals[this.doctype][this.docname]; - return 'SELECT DISTINCT `tabAccount`.`name`,`tabAccount`.debit_or_credit,`tabAccount`.group_or_ledger FROM `tabAccount` WHERE `tabAccount`.`company` = "' + doc.company_name + '" AND `tabAccount`.docstatus != 2 AND `tabAccount`.`is_pl_account` = "Yes" AND `tabAccount`.debit_or_credit = "Debit" AND `tabAccount`.`group_or_ledger` != "Group" AND `tabAccount`.`group_or_ledger` is not NULL AND `tabAccount`.`name` LIKE "%s" ORDER BY `tabAccount`.`name` LIMIT 50'; - } + var mydoc = locals[this.doctype][this.docname]; + return 'SELECT DISTINCT `tabAccount`.`name`,`tabAccount`.debit_or_credit,`tabAccount`.group_or_ledger FROM `tabAccount` WHERE `tabAccount`.`company` = "' + doc.company_name + '" AND `tabAccount`.docstatus != 2 AND `tabAccount`.`is_pl_account` = "Yes" AND `tabAccount`.debit_or_credit = "Debit" AND `tabAccount`.`group_or_ledger` != "Group" AND `tabAccount`.`group_or_ledger` is not NULL AND `tabAccount`.`name` LIKE "%s" ORDER BY `tabAccount`.`name` LIMIT 50'; + } cur_frm.fields_dict['parent_cost_center'].get_query = function(doc){ - return 'SELECT DISTINCT `tabCost Center`.name FROM `tabCost Center` WHERE `tabCost Center`.group_or_ledger="Group" AND `tabCost Center`.docstatus != 2 AND `tabCost Center`.company_name="'+ doc.company_name+'" AND `tabCost Center`.company_name is not NULL AND `tabCost Center`.name LIKE "%s" ORDER BY `tabCost Center`.name LIMIT 50'; + return 'SELECT DISTINCT `tabCost Center`.name FROM `tabCost Center` WHERE `tabCost Center`.group_or_ledger="Group" AND `tabCost Center`.docstatus != 2 AND `tabCost Center`.company_name="'+ doc.company_name+'" AND `tabCost Center`.company_name is not NULL AND `tabCost Center`.name LIKE "%s" ORDER BY `tabCost Center`.name LIMIT 50'; } //parent cost center cur_frm.cscript.parent_cost_center = function(doc,cdt,cdn){ - if(!doc.company_name){ - alert('Please enter company name first'); - } + if(!doc.company_name){ + alert('Please enter company name first'); + } } //company abbr cur_frm.cscript.company_name = function(doc,cdt,cdn){ - get_server_fields('get_abbr','','',doc,cdt,cdn,1); + get_server_fields('get_abbr','','',doc,cdt,cdn,1); } //onload if cost center is group cur_frm.cscript.onload = function(doc, cdt, cdn) { - - if(!doc.__islocal && doc.docstatus == 0){ - get_field(doc.doctype,'group_or_ledger',doc.name).permlevel = 1; - refresh_field('group_or_ledger'); - get_field(doc.doctype,'company_name',doc.name).permlevel = 1; - refresh_field('company_name'); - } + + if(!doc.__islocal && doc.docstatus == 0){ + get_field(doc.doctype,'group_or_ledger',doc.name).permlevel = 1; + refresh_field('group_or_ledger'); + get_field(doc.doctype,'company_name',doc.name).permlevel = 1; + refresh_field('company_name'); + } } cur_frm.cscript.refresh = function(doc, cdt, cdn) { - + cur_frm.cscript.hide_unhide_group_ledger(doc); +} + + +// Hide/unhide group or ledger +// ----------------------------------------- +cur_frm.cscript.hide_unhide_group_ledger = function(doc) { + hide_field(['Convert to Group', 'Convert to Ledger']); + if (cstr(doc.group_or_ledger) == 'Group') unhide_field('Convert to Ledger'); + else if (cstr(doc.group_or_ledger) == 'Ledger') unhide_field('Convert to Group'); +} + +// Convert group to ledger +// ----------------------------------------- +cur_frm.cscript['Convert to Ledger'] = function(doc, cdt, cdn) { + $c_obj(make_doclist(cdt,cdn),'convert_group_to_ledger','',function(r,rt) { + if(r.message == 1) { + doc.group_or_ledger = 'Ledger'; + refresh_field('group_or_ledger'); + cur_frm.cscript.hide_unhide_group_ledger(doc); + } + }); +} + +// Convert ledger to group +// ----------------------------------------- +cur_frm.cscript['Convert to Group'] = function(doc, cdt, cdn) { + $c_obj(make_doclist(cdt,cdn),'convert_ledger_to_group','',function(r,rt) { + if(r.message == 1) { + doc.group_or_ledger = 'Group'; + refresh_field('group_or_ledger'); + cur_frm.cscript.hide_unhide_group_ledger(doc); + } + }); } diff --git a/erpnext/accounts/doctype/cost_center/cost_center.py b/erpnext/accounts/doctype/cost_center/cost_center.py index 66d22f017d..d09331ac07 100644 --- a/erpnext/accounts/doctype/cost_center/cost_center.py +++ b/erpnext/accounts/doctype/cost_center/cost_center.py @@ -32,6 +32,35 @@ class DocType: 'company_abbr' : abbr } return ret + + #------------------------------------------------------------------------- + def convert_group_to_ledger(self): + if self.check_if_child_exists(): + msgprint("Cost Center: %s has existing child. You can not convert this cost center to ledger" % (self.doc.name), raise_exception=1) + elif self.check_gle_exists(): + msgprint("Cost Center with existing transaction can not be converted to ledger.", raise_exception=1) + else: + self.doc.group_or_ledger = 'Ledger' + self.doc.save() + return 1 + + #------------------------------------------------------------------------- + def convert_ledger_to_group(self): + if self.check_gle_exists(): + msgprint("Cost Center with existing transaction can not be converted to group.", raise_exception=1) + else: + self.doc.group_or_ledger = 'Group' + self.doc.save() + return 1 + + #------------------------------------------------------------------------- + def check_gle_exists(self): + return sql("select name from `tabGL Entry` where cost_center = %s and ifnull(is_cancelled, 'No') = 'No'", (self.doc.name)) + + + #------------------------------------------------------------------------- + def check_if_child_exists(self): + return sql("select name from `tabCost Center` where parent_cost_center = %s and docstatus != 2", self.doc.name) #------------------------------------------------------------------------- def validate(self): diff --git a/erpnext/accounts/doctype/cost_center/cost_center.txt b/erpnext/accounts/doctype/cost_center/cost_center.txt index 7bc5810f90..94fb02bf44 100644 --- a/erpnext/accounts/doctype/cost_center/cost_center.txt +++ b/erpnext/accounts/doctype/cost_center/cost_center.txt @@ -5,14 +5,14 @@ { 'creation': '2010-08-08 17:08:56', 'docstatus': 0, - 'modified': '2011-09-26 18:55:05', + 'modified': '2011-10-10 12:05:07', 'modified_by': 'Administrator', 'owner': 'Administrator' }, # These values are common for all DocType { - '_last_update': '1316075905', + '_last_update': '1317365120', 'allow_copy': 1, 'allow_trash': 1, 'autoname': 'field:cost_center_name', @@ -27,7 +27,7 @@ 'section_style': 'Simple', 'server_code_error': ' ', 'show_in_menu': 0, - 'version': 107 + 'version': 109 }, # These values are common for all DocField @@ -206,6 +206,26 @@ 'trigger': 'Client' }, + # DocField + { + 'colour': 'White:FFF', + 'doctype': 'DocField', + 'fieldtype': 'Button', + 'label': 'Convert to Group', + 'permlevel': 0, + 'trigger': 'Client' + }, + + # DocField + { + 'colour': 'White:FFF', + 'doctype': 'DocField', + 'fieldtype': 'Button', + 'label': 'Convert to Ledger', + 'permlevel': 0, + 'trigger': 'Client' + }, + # DocField { 'doctype': 'DocField', diff --git a/erpnext/patches/patch.py b/erpnext/patches/patch.py index cc4a164f84..1924fdde7b 100644 --- a/erpnext/patches/patch.py +++ b/erpnext/patches/patch.py @@ -1,7 +1,7 @@ # REMEMBER to update this # ======================== -last_patch = 382 +last_patch = 383 #------------------------------------------- @@ -448,3 +448,5 @@ def execute(patch_no): from webnotes.session_cache import clear_cache clear_cache(webnotes.session['user']) + elif patch_no == 383: + reload_doc('accounts', 'doctype', 'cost_center')