From 6099af5d00c5a99bb2bc82425396ce102cc248cf Mon Sep 17 00:00:00 2001 From: Nabin Hait Date: Mon, 31 Jan 2022 17:24:50 +0530 Subject: [PATCH] fix: Fixed test case and sider issues --- .../cost_center_allocation.py | 9 +++--- .../test_cost_center_allocation.py | 30 +++++++++---------- erpnext/accounts/general_ledger.py | 6 ++-- .../accounts/report/financial_statements.py | 7 +++-- .../v14_0/migrate_cost_center_allocations.py | 6 ++-- 5 files changed, 31 insertions(+), 27 deletions(-) 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 2991c731593..00a591aa582 100644 --- a/erpnext/accounts/doctype/cost_center_allocation/cost_center_allocation.py +++ b/erpnext/accounts/doctype/cost_center_allocation/cost_center_allocation.py @@ -4,7 +4,8 @@ import frappe from frappe import _ from frappe.model.document import Document -from frappe.utils import getdate, format_date, add_days +from frappe.utils import add_days, format_date, getdate + class MainCostCenterCantBeChild(frappe.ValidationError): pass @@ -32,7 +33,7 @@ class CostCenterAllocation(Document): 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 + # Check if GLE exists against the main cost center # If exists ensure from date is set after posting date of last GLE last_gle_date = frappe.db.get_value("GL Entry", @@ -47,7 +48,7 @@ class CostCenterAllocation(Document): def validate_backdated_allocation(self): # Check if there are any future existing allocation records against the main cost center # If exists, warn the user about it - + future_allocation = frappe.db.get_value("Cost Center Allocation", filters = { "main_cost_center": self.main_cost_center, "valid_from": (">=", self.valid_from), @@ -82,7 +83,7 @@ class CostCenterAllocation(Document): def validate_child_cost_centers(self): # Check if child cost center is used as main cost center in any existing allocation - main_cost_centers = [d.main_cost_center for d in + main_cost_centers = [d.main_cost_center for d in frappe.get_all("Cost Center Allocation", {'docstatus': 1}, 'main_cost_center')] for d in self.allocation_percentages: 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 d7d6578c359..137ab7de24e 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 @@ -34,11 +34,11 @@ class TestCostCenterAllocation(unittest.TestCase): gle = frappe.qb.DocType("GL Entry") gl_entries = ( frappe.qb.from_(gle) - .select(gle.cost_center, gle.debit, gle.credit) - .where(gle.voucher_type == 'Journal Entry') - .where(gle.voucher_no == jv.name) - .where(gle.account == 'Sales - _TC') - .orderby(gle.cost_center) + .select(gle.cost_center, gle.debit, gle.credit) + .where(gle.voucher_type == 'Journal Entry') + .where(gle.voucher_no == jv.name) + .where(gle.account == 'Sales - _TC') + .orderby(gle.cost_center) ).run(as_dict=1) self.assertTrue(gl_entries) @@ -57,8 +57,8 @@ class TestCostCenterAllocation(unittest.TestCase): { "Sub Cost Center 1 - _TC": 60, "Main Cost Center 1 - _TC": 40 - } - , save=False) + }, save=False + ) self.assertRaises(MainCostCenterCantBeChild, cca.save) @@ -75,8 +75,8 @@ class TestCostCenterAllocation(unittest.TestCase): cca2 = create_cost_center_allocation("_Test Company", "Sub Cost Center 1 - _TC", { "Sub Cost Center 2 - _TC": 100 - } - , save=False) + }, save=False + ) self.assertRaises(InvalidMainCostCenter, cca2.save) @@ -96,8 +96,8 @@ class TestCostCenterAllocation(unittest.TestCase): { "Main Cost Center 1 - _TC": 60, "Sub Cost Center 1 - _TC": 40 - } - , save=False) + }, save=False + ) self.assertRaises(InvalidChildCostCenter, cca2.save) @@ -108,8 +108,8 @@ class TestCostCenterAllocation(unittest.TestCase): { "Sub Cost Center 1 - _TC": 40, "Sub Cost Center 2 - _TC": 40 - } - , save=False) + }, save=False + ) self.assertRaises(WrongPercentageAllocation, cca.save) def test_valid_from_based_on_existing_gle(self): @@ -122,8 +122,8 @@ class TestCostCenterAllocation(unittest.TestCase): { "Sub Cost Center 1 - _TC": 60, "Sub Cost Center 2 - _TC": 40 - } - , valid_from=add_days(today(), -1), save=False) + }, valid_from=add_days(today(), -1), save=False + ) self.assertRaises(InvalidDateError, cca.save) diff --git a/erpnext/accounts/general_ledger.py b/erpnext/accounts/general_ledger.py index 134b02396ce..6b73e3cb1c4 100644 --- a/erpnext/accounts/general_ledger.py +++ b/erpnext/accounts/general_ledger.py @@ -52,6 +52,9 @@ def validate_accounting_period(gl_map): .format(frappe.bold(accounting_periods[0].name)), ClosedAccountingPeriod) def process_gl_map(gl_map, merge_entries=True, precision=None): + if not gl_map: + return [] + gl_map = distribute_gl_based_on_cost_center_allocation(gl_map, precision) if merge_entries: @@ -86,8 +89,7 @@ def get_cost_center_allocation_data(company, posting_date): child = frappe.qb.DocType("Cost Center Allocation Percentage") records = ( - frappe.qb.from_(par) - .inner_join(child).on(par.name == child.parent) + frappe.qb.from_(par).inner_join(child).on(par.name == child.parent) .select(par.main_cost_center, child.cost_center, child.percentage) .where(par.docstatus == 1) .where(par.company == company) diff --git a/erpnext/accounts/report/financial_statements.py b/erpnext/accounts/report/financial_statements.py index 416d1b0bbbf..03ae0aea132 100644 --- a/erpnext/accounts/report/financial_statements.py +++ b/erpnext/accounts/report/financial_statements.py @@ -388,13 +388,14 @@ def set_gl_entries_by_account( }) gl_entries = frappe.db.sql(""" - select posting_date, account, debit, credit, is_opening, fiscal_year, + 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 - """.format(additional_conditions=additional_conditions), gl_filters, as_dict=True) #nosec + and is_cancelled = 0""".format( + additional_conditions=additional_conditions), gl_filters, as_dict=True + ) if filters and filters.get('presentation_currency'): convert_to_presentation_currency(gl_entries, get_currency(filters), filters.get('company')) diff --git a/erpnext/patches/v14_0/migrate_cost_center_allocations.py b/erpnext/patches/v14_0/migrate_cost_center_allocations.py index c8807962835..d101307d8ba 100644 --- a/erpnext/patches/v14_0/migrate_cost_center_allocations.py +++ b/erpnext/patches/v14_0/migrate_cost_center_allocations.py @@ -35,9 +35,9 @@ def get_existing_cost_center_allocations(): records = ( frappe.qb.from_(par) - .inner_join(child).on(par.name == child.parent) - .select(par.name, child.cost_center, child.percentage_allocation) - .where(par.enable_distributed_cost_center == 1) + .inner_join(child).on(par.name == child.parent) + .select(par.name, child.cost_center, child.percentage_allocation) + .where(par.enable_distributed_cost_center == 1) ).run(as_dict=True) cc_allocations = frappe._dict()