diff --git a/erpnext/accounts/doctype/cost_center/cost_center.js b/erpnext/accounts/doctype/cost_center/cost_center.js index ee23b1be5c..632fab0197 100644 --- a/erpnext/accounts/doctype/cost_center/cost_center.js +++ b/erpnext/accounts/doctype/cost_center/cost_center.js @@ -15,17 +15,6 @@ frappe.ui.form.on('Cost Center', { } } }); - - frm.set_query("cost_center", "distributed_cost_center", function() { - return { - filters: { - company: frm.doc.company, - is_group: 0, - enable_distributed_cost_center: 0, - name: ['!=', frm.doc.name] - } - }; - }); }, refresh: function(frm) { if (!frm.is_new()) { diff --git a/erpnext/accounts/doctype/cost_center/cost_center.py b/erpnext/accounts/doctype/cost_center/cost_center.py index 7ae0a72e3d..558c51605d 100644 --- a/erpnext/accounts/doctype/cost_center/cost_center.py +++ b/erpnext/accounts/doctype/cost_center/cost_center.py @@ -20,24 +20,6 @@ class CostCenter(NestedSet): def validate(self): self.validate_mandatory() self.validate_parent_cost_center() - self.validate_distributed_cost_center() - - def validate_distributed_cost_center(self): - if cint(self.enable_distributed_cost_center): - if not self.distributed_cost_center: - frappe.throw(_("Please enter distributed cost center")) - if sum(x.percentage_allocation for x in self.distributed_cost_center) != 100: - frappe.throw(_("Total percentage allocation for distributed cost center should be equal to 100")) - if not self.get('__islocal'): - if not cint(frappe.get_cached_value("Cost Center", {"name": self.name}, "enable_distributed_cost_center")) \ - and self.check_if_part_of_distributed_cost_center(): - frappe.throw(_("Cannot enable Distributed Cost Center for a Cost Center already allocated in another Distributed Cost Center")) - if next((True for x in self.distributed_cost_center if x.cost_center == x.parent), False): - frappe.throw(_("Parent Cost Center cannot be added in Distributed Cost Center")) - if check_if_distributed_cost_center_enabled(list(x.cost_center for x in self.distributed_cost_center)): - frappe.throw(_("A Distributed Cost Center cannot be added in the Distributed Cost Center allocation table.")) - else: - self.distributed_cost_center = [] def validate_mandatory(self): if self.cost_center_name != self.company and not self.parent_cost_center: @@ -64,10 +46,10 @@ class CostCenter(NestedSet): @frappe.whitelist() def convert_ledger_to_group(self): - if cint(self.enable_distributed_cost_center): - frappe.throw(_("Cost Center with enabled distributed cost center can not be converted to group")) - if self.check_if_part_of_distributed_cost_center(): - frappe.throw(_("Cost Center Already Allocated in a Distributed Cost Center cannot be converted to group")) + if self.if_allocation_exists_against_cost_center(): + frappe.throw(_("Cost Center with Allocation records can not be converted to a group")) + if self.check_if_part_of_cost_center_allocation(): + frappe.throw(_("Cost Center is a part of Cost Center Allocation, hence cannot be converted to a group")) if self.check_gle_exists(): frappe.throw(_("Cost Center with existing transactions can not be converted to group")) self.is_group = 1 @@ -81,8 +63,17 @@ class CostCenter(NestedSet): return frappe.db.sql("select name from `tabCost Center` where \ parent_cost_center = %s and docstatus != 2", self.name) - def check_if_part_of_distributed_cost_center(self): - return frappe.db.get_value("Distributed Cost Center", {"cost_center": self.name}) + def if_allocation_exists_against_cost_center(self): + return frappe.db.get_value("Cost Center Allocation", filters = { + "main_cost_center": self.name, + "docstatus": 1 + }) + + def check_if_part_of_cost_center_allocation(self): + return frappe.db.get_value("Cost Center Allocation Percentage", filters = { + "cost_center": self.name, + "docstatus": 1 + }) def before_rename(self, olddn, newdn, merge=False): # Add company abbr if not provided @@ -126,8 +117,4 @@ def on_doctype_update(): def get_name_with_number(new_account, account_number): if account_number and not new_account[0].isdigit(): new_account = account_number + " - " + new_account - return new_account - -def check_if_distributed_cost_center_enabled(cost_center_list): - value_list = frappe.get_list("Cost Center", {"name": ["in", cost_center_list]}, "enable_distributed_cost_center", as_list=1) - return next((True for x in value_list if x[0]), False) + return new_account \ No newline at end of file diff --git a/erpnext/accounts/doctype/cost_center/test_cost_center.py b/erpnext/accounts/doctype/cost_center/test_cost_center.py index f8615ec03a..ff50a21124 100644 --- a/erpnext/accounts/doctype/cost_center/test_cost_center.py +++ b/erpnext/accounts/doctype/cost_center/test_cost_center.py @@ -23,33 +23,6 @@ class TestCostCenter(unittest.TestCase): self.assertRaises(frappe.ValidationError, cost_center.save) - def test_validate_distributed_cost_center(self): - - if not frappe.db.get_value('Cost Center', {'name': '_Test Cost Center - _TC'}): - frappe.get_doc(test_records[0]).insert() - - if not frappe.db.get_value('Cost Center', {'name': '_Test Cost Center 2 - _TC'}): - frappe.get_doc(test_records[1]).insert() - - invalid_distributed_cost_center = frappe.get_doc({ - "company": "_Test Company", - "cost_center_name": "_Test Distributed Cost Center", - "doctype": "Cost Center", - "is_group": 0, - "parent_cost_center": "_Test Company - _TC", - "enable_distributed_cost_center": 1, - "distributed_cost_center": [{ - "cost_center": "_Test Cost Center - _TC", - "percentage_allocation": 40 - }, { - "cost_center": "_Test Cost Center 2 - _TC", - "percentage_allocation": 50 - } - ] - }) - - self.assertRaises(frappe.ValidationError, invalid_distributed_cost_center.save) - def create_cost_center(**args): args = frappe._dict(args) if args.cost_center_name: diff --git a/erpnext/accounts/doctype/cost_center_allocation/cost_center_allocation.js b/erpnext/accounts/doctype/cost_center_allocation/cost_center_allocation.js index 864bef3ffd..bdc7b1814c 100644 --- a/erpnext/accounts/doctype/cost_center_allocation/cost_center_allocation.js +++ b/erpnext/accounts/doctype/cost_center_allocation/cost_center_allocation.js @@ -10,7 +10,7 @@ frappe.ui.form.on('Cost Center Allocation', { }); } - frm.set_query('main_cost_center', function(doc) { + frm.set_query('main_cost_center', function() { return { filters: filters }; diff --git a/erpnext/accounts/doctype/cost_center_allocation/cost_center_allocation.py b/erpnext/accounts/doctype/cost_center_allocation/cost_center_allocation.py index 37787cb529..2991c73159 100644 --- a/erpnext/accounts/doctype/cost_center_allocation/cost_center_allocation.py +++ b/erpnext/accounts/doctype/cost_center_allocation/cost_center_allocation.py @@ -6,12 +6,16 @@ from frappe import _ from frappe.model.document import Document from frappe.utils import getdate, format_date, add_days -class MainCostCenterCantBeChild(frappe.ValidationError): pass -class InvalidMainCostCenter(frappe.ValidationError): pass -class InvalidChildCostCenter(frappe.ValidationError): pass -class WrongPercentageAllocation(frappe.ValidationError): pass -class InvalidDateError(frappe.ValidationError): pass - +class MainCostCenterCantBeChild(frappe.ValidationError): + pass +class InvalidMainCostCenter(frappe.ValidationError): + pass +class InvalidChildCostCenter(frappe.ValidationError): + pass +class WrongPercentageAllocation(frappe.ValidationError): + pass +class InvalidDateError(frappe.ValidationError): + pass class CostCenterAllocation(Document): def validate(self): @@ -25,7 +29,7 @@ class CostCenterAllocation(Document): total_percentage = sum([d.percentage for d in self.get("allocation_percentages", [])]) if total_percentage != 100: - frappe.throw(_("Total percentage against cost centers should be 100"), WrongPercentageAllocation) + frappe.throw(_("Total percentage against cost centers should be 100"), WrongPercentageAllocation) def validate_from_date_based_on_existing_gle(self): # Check if GLE exists against the main cost center diff --git a/erpnext/accounts/doctype/cost_center_allocation/test_cost_center_allocation.py b/erpnext/accounts/doctype/cost_center_allocation/test_cost_center_allocation.py index af318eeae3..d7d6578c35 100644 --- a/erpnext/accounts/doctype/cost_center_allocation/test_cost_center_allocation.py +++ b/erpnext/accounts/doctype/cost_center_allocation/test_cost_center_allocation.py @@ -3,7 +3,7 @@ import frappe import unittest -from frappe.utils import today, add_months, add_days +from frappe.utils import today, add_days from erpnext.accounts.doctype.cost_center.test_cost_center import create_cost_center from erpnext.accounts.doctype.journal_entry.test_journal_entry import make_journal_entry from erpnext.accounts.doctype.cost_center_allocation.cost_center_allocation import (MainCostCenterCantBeChild, diff --git a/erpnext/accounts/doctype/distributed_cost_center/__init__.py b/erpnext/accounts/doctype/distributed_cost_center/__init__.py deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/erpnext/accounts/doctype/distributed_cost_center/distributed_cost_center.json b/erpnext/accounts/doctype/distributed_cost_center/distributed_cost_center.json deleted file mode 100644 index 45b0e2df9b..0000000000 --- a/erpnext/accounts/doctype/distributed_cost_center/distributed_cost_center.json +++ /dev/null @@ -1,40 +0,0 @@ -{ - "actions": [], - "creation": "2020-03-19 12:34:01.500390", - "doctype": "DocType", - "editable_grid": 1, - "engine": "InnoDB", - "field_order": [ - "cost_center", - "percentage_allocation" - ], - "fields": [ - { - "fieldname": "cost_center", - "fieldtype": "Link", - "in_list_view": 1, - "label": "Cost Center", - "options": "Cost Center", - "reqd": 1 - }, - { - "fieldname": "percentage_allocation", - "fieldtype": "Float", - "in_list_view": 1, - "label": "Percentage Allocation", - "reqd": 1 - } - ], - "istable": 1, - "links": [], - "modified": "2020-03-19 12:54:43.674655", - "modified_by": "Administrator", - "module": "Accounts", - "name": "Distributed Cost Center", - "owner": "Administrator", - "permissions": [], - "quick_entry": 1, - "sort_field": "modified", - "sort_order": "DESC", - "track_changes": 1 -} \ No newline at end of file diff --git a/erpnext/accounts/doctype/distributed_cost_center/distributed_cost_center.py b/erpnext/accounts/doctype/distributed_cost_center/distributed_cost_center.py deleted file mode 100644 index dcf0e3b99d..0000000000 --- a/erpnext/accounts/doctype/distributed_cost_center/distributed_cost_center.py +++ /dev/null @@ -1,10 +0,0 @@ -# Copyright (c) 2020, Frappe Technologies Pvt. Ltd. and contributors -# For license information, please see license.txt - - -# import frappe -from frappe.model.document import Document - - -class DistributedCostCenter(Document): - pass diff --git a/erpnext/accounts/report/budget_variance_report/budget_variance_report.py b/erpnext/accounts/report/budget_variance_report/budget_variance_report.py index 3bb590a564..56ee5008cf 100644 --- a/erpnext/accounts/report/budget_variance_report/budget_variance_report.py +++ b/erpnext/accounts/report/budget_variance_report/budget_variance_report.py @@ -29,18 +29,6 @@ def execute(filters=None): dimension_items = cam_map.get(dimension) if dimension_items: data = get_final_data(dimension, dimension_items, filters, period_month_ranges, data, 0) - else: - DCC_allocation = frappe.db.sql('''SELECT parent, sum(percentage_allocation) as percentage_allocation - FROM `tabDistributed Cost Center` - WHERE cost_center IN %(dimension)s - AND parent NOT IN %(dimension)s - GROUP BY parent''',{'dimension':[dimension]}) - if DCC_allocation: - filters['budget_against_filter'] = [DCC_allocation[0][0]] - ddc_cam_map = get_dimension_account_month_map(filters) - dimension_items = ddc_cam_map.get(DCC_allocation[0][0]) - if dimension_items: - data = get_final_data(dimension, dimension_items, filters, period_month_ranges, data, DCC_allocation[0][1]) chart = get_chart_data(filters, columns, data) diff --git a/erpnext/accounts/report/financial_statements.py b/erpnext/accounts/report/financial_statements.py index 1e89b650c7..416d1b0bbb 100644 --- a/erpnext/accounts/report/financial_statements.py +++ b/erpnext/accounts/report/financial_statements.py @@ -387,42 +387,14 @@ def set_gl_entries_by_account( key: value }) - distributed_cost_center_query = "" - if filters and filters.get('cost_center'): - distributed_cost_center_query = """ - UNION ALL - SELECT posting_date, - account, - debit*(DCC_allocation.percentage_allocation/100) as debit, - credit*(DCC_allocation.percentage_allocation/100) as credit, - is_opening, - fiscal_year, - debit_in_account_currency*(DCC_allocation.percentage_allocation/100) as debit_in_account_currency, - credit_in_account_currency*(DCC_allocation.percentage_allocation/100) as credit_in_account_currency, - account_currency - FROM `tabGL Entry`, - ( - SELECT parent, sum(percentage_allocation) as percentage_allocation - FROM `tabDistributed Cost Center` - WHERE cost_center IN %(cost_center)s - AND parent NOT IN %(cost_center)s - GROUP BY parent - ) as DCC_allocation - WHERE company=%(company)s - {additional_conditions} - AND posting_date <= %(to_date)s - AND is_cancelled = 0 - AND cost_center = DCC_allocation.parent - """.format(additional_conditions=additional_conditions.replace("and cost_center in %(cost_center)s ", '')) - - gl_entries = frappe.db.sql("""select posting_date, account, debit, credit, is_opening, fiscal_year, debit_in_account_currency, credit_in_account_currency, account_currency from `tabGL Entry` + gl_entries = frappe.db.sql(""" + select posting_date, account, debit, credit, is_opening, fiscal_year, + debit_in_account_currency, credit_in_account_currency, account_currency from `tabGL Entry` where company=%(company)s {additional_conditions} and posting_date <= %(to_date)s and is_cancelled = 0 - {distributed_cost_center_query}""".format( - additional_conditions=additional_conditions, - distributed_cost_center_query=distributed_cost_center_query), gl_filters, as_dict=True) #nosec + """.format(additional_conditions=additional_conditions), gl_filters, as_dict=True) #nosec if filters and filters.get('presentation_currency'): convert_to_presentation_currency(gl_entries, get_currency(filters), filters.get('company')) diff --git a/erpnext/accounts/report/general_ledger/general_ledger.py b/erpnext/accounts/report/general_ledger/general_ledger.py index 7f27920547..4ff0297dba 100644 --- a/erpnext/accounts/report/general_ledger/general_ledger.py +++ b/erpnext/accounts/report/general_ledger/general_ledger.py @@ -176,44 +176,7 @@ def get_gl_entries(filters, accounting_dimensions): if accounting_dimensions: dimension_fields = ', '.join(accounting_dimensions) + ',' - distributed_cost_center_query = "" - if filters and filters.get('cost_center'): - select_fields_with_percentage = """, debit*(DCC_allocation.percentage_allocation/100) as debit, - credit*(DCC_allocation.percentage_allocation/100) as credit, - debit_in_account_currency*(DCC_allocation.percentage_allocation/100) as debit_in_account_currency, - credit_in_account_currency*(DCC_allocation.percentage_allocation/100) as credit_in_account_currency """ - - distributed_cost_center_query = """ - UNION ALL - SELECT name as gl_entry, - posting_date, - account, - party_type, - party, - voucher_type, - voucher_no, {dimension_fields} - cost_center, project, - against_voucher_type, - against_voucher, - account_currency, - remarks, against, - is_opening, `tabGL Entry`.creation {select_fields_with_percentage} - FROM `tabGL Entry`, - ( - SELECT parent, sum(percentage_allocation) as percentage_allocation - FROM `tabDistributed Cost Center` - WHERE cost_center IN %(cost_center)s - AND parent NOT IN %(cost_center)s - GROUP BY parent - ) as DCC_allocation - WHERE company=%(company)s - {conditions} - AND posting_date <= %(to_date)s - AND cost_center = DCC_allocation.parent - """.format(dimension_fields=dimension_fields,select_fields_with_percentage=select_fields_with_percentage, conditions=get_conditions(filters).replace("and cost_center in %(cost_center)s ", '')) - - gl_entries = frappe.db.sql( - """ + gl_entries = frappe.db.sql(""" select name as gl_entry, posting_date, account, party_type, party, voucher_type, voucher_no, {dimension_fields} @@ -222,13 +185,11 @@ def get_gl_entries(filters, accounting_dimensions): remarks, against, is_opening, creation {select_fields} from `tabGL Entry` where company=%(company)s {conditions} - {distributed_cost_center_query} {order_by_statement} - """.format( - dimension_fields=dimension_fields, select_fields=select_fields, conditions=get_conditions(filters), distributed_cost_center_query=distributed_cost_center_query, - order_by_statement=order_by_statement - ), - filters, as_dict=1) + """.format( + dimension_fields=dimension_fields, select_fields=select_fields, + conditions=get_conditions(filters), order_by_statement=order_by_statement + ), filters, as_dict=1) if filters.get('presentation_currency'): return convert_to_presentation_currency(gl_entries, currency_map, filters.get('company')) diff --git a/erpnext/accounts/report/profitability_analysis/profitability_analysis.py b/erpnext/accounts/report/profitability_analysis/profitability_analysis.py index 3dcb86267c..f4b8731ba8 100644 --- a/erpnext/accounts/report/profitability_analysis/profitability_analysis.py +++ b/erpnext/accounts/report/profitability_analysis/profitability_analysis.py @@ -109,7 +109,6 @@ def accumulate_values_into_parents(accounts, accounts_by_name): def prepare_data(accounts, filters, total_row, parent_children_map, based_on): data = [] - new_accounts = accounts company_currency = frappe.get_cached_value('Company', filters.get("company"), "default_currency") for d in accounts: @@ -123,19 +122,6 @@ def prepare_data(accounts, filters, total_row, parent_children_map, based_on): "currency": company_currency, "based_on": based_on } - if based_on == 'cost_center': - cost_center_doc = frappe.get_doc("Cost Center",d.name) - if not cost_center_doc.enable_distributed_cost_center: - DCC_allocation = frappe.db.sql("""SELECT parent, sum(percentage_allocation) as percentage_allocation - FROM `tabDistributed Cost Center` - WHERE cost_center IN %(cost_center)s - AND parent NOT IN %(cost_center)s - GROUP BY parent""",{'cost_center': [d.name]}) - if DCC_allocation: - for account in new_accounts: - if account['name'] == DCC_allocation[0][0]: - for value in value_fields: - d[value] += account[value]*(DCC_allocation[0][1]/100) for key in value_fields: row[key] = flt(d.get(key, 0.0), 3) diff --git a/erpnext/patches/v14_0/migrate_cost_center_allocations.py b/erpnext/patches/v14_0/migrate_cost_center_allocations.py index ab8da12412..c880796283 100644 --- a/erpnext/patches/v14_0/migrate_cost_center_allocations.py +++ b/erpnext/patches/v14_0/migrate_cost_center_allocations.py @@ -6,7 +6,8 @@ def execute(): frappe.reload_doc('accounts', 'doctype', dt) cc_allocations = get_existing_cost_center_allocations() - create_new_cost_center_allocation_records(cc_allocations) + if cc_allocations: + create_new_cost_center_allocation_records(cc_allocations) frappe.delete_doc('DocType', 'Distributed Cost Center', ignore_missing=True) @@ -25,8 +26,10 @@ def create_new_cost_center_allocation_records(cc_allocations): cca.save() cca.submit() - def get_existing_cost_center_allocations(): + if not frappe.get_meta("Cost Center").has_field("enable_distributed_cost_center"): + return + par = frappe.qb.DocType("Cost Center") child = frappe.qb.DocType("Distributed Cost Center")