From 3d11ac0e75d0240c57edd101092ddb685529acf4 Mon Sep 17 00:00:00 2001 From: deepeshgarg007 Date: Sun, 19 May 2019 00:02:01 +0530 Subject: [PATCH] fix: Accounting dimensions for child tables --- .../doctype/journal_entry/journal_entry.py | 2 +- .../doctype/payment_entry/payment_entry.py | 2 +- .../purchase_invoice/purchase_invoice.py | 20 ++++++------- .../doctype/sales_invoice/sales_invoice.py | 2 +- erpnext/accounts/general_ledger.py | 30 ++++++++++++------- erpnext/controllers/accounts_controller.py | 4 ++- erpnext/controllers/stock_controller.py | 4 +-- .../purchase_receipt/purchase_receipt.py | 14 ++++----- .../stock/doctype/stock_entry/stock_entry.py | 4 +-- 9 files changed, 47 insertions(+), 35 deletions(-) diff --git a/erpnext/accounts/doctype/journal_entry/journal_entry.py b/erpnext/accounts/doctype/journal_entry/journal_entry.py index 63f180dccea..26fbc23b4cd 100644 --- a/erpnext/accounts/doctype/journal_entry/journal_entry.py +++ b/erpnext/accounts/doctype/journal_entry/journal_entry.py @@ -509,7 +509,7 @@ class JournalEntry(AccountsController): "cost_center": d.cost_center, "project": d.project, "finance_book": self.finance_book - }) + }, item=d) ) if gl_map: diff --git a/erpnext/accounts/doctype/payment_entry/payment_entry.py b/erpnext/accounts/doctype/payment_entry/payment_entry.py index 9033f659e7c..0161b2e703e 100644 --- a/erpnext/accounts/doctype/payment_entry/payment_entry.py +++ b/erpnext/accounts/doctype/payment_entry/payment_entry.py @@ -507,7 +507,7 @@ class PaymentEntry(AccountsController): "debit_in_account_currency": d.amount, "debit": d.amount, "cost_center": d.cost_center - }) + }, item=d) ) def update_advance_paid(self): diff --git a/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py b/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py index 95d49a4421a..4acf1692d31 100644 --- a/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py +++ b/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py @@ -454,7 +454,7 @@ class PurchaseInvoice(BuyingController): "remarks": self.get("remarks") or _("Accounting Entry for Stock"), "cost_center": item.cost_center, "project": item.project - }, account_currency) + }, account_currency, item=item) ) # Amount added through landed-cost-voucher @@ -466,7 +466,7 @@ class PurchaseInvoice(BuyingController): "remarks": self.get("remarks") or _("Accounting Entry for Stock"), "credit": flt(item.landed_cost_voucher_amount), "project": item.project - })) + }), item=item) # sub-contracting warehouse if flt(item.rm_supp_cost): @@ -480,7 +480,7 @@ class PurchaseInvoice(BuyingController): "cost_center": item.cost_center, "remarks": self.get("remarks") or _("Accounting Entry for Stock"), "credit": flt(item.rm_supp_cost) - }, warehouse_account[self.supplier_warehouse]["account_currency"])) + }, warehouse_account[self.supplier_warehouse]["account_currency"], item=item)) elif not item.is_fixed_asset or (item.is_fixed_asset and is_cwip_accounting_disabled()): gl_entries.append( self.get_gl_dict({ @@ -492,7 +492,7 @@ class PurchaseInvoice(BuyingController): else flt(item.net_amount, item.precision("net_amount"))), "cost_center": item.cost_center, "project": item.project - }, account_currency) + }, account_currency, item=item) ) if self.auto_accounting_for_stock and self.is_opening == "No" and \ @@ -511,7 +511,7 @@ class PurchaseInvoice(BuyingController): "debit": flt(item.item_tax_amount, item.precision("item_tax_amount")), "remarks": self.remarks or "Accounting Entry for Stock", "cost_center": self.cost_center - }) + }, item=item) ) self.negative_expense_to_be_booked += flt(item.item_tax_amount, \ @@ -540,7 +540,7 @@ class PurchaseInvoice(BuyingController): "debit_in_account_currency": (base_asset_amount if asset_rbnb_currency == self.company_currency else asset_amount), "cost_center": item.cost_center - })) + }, item=item)) if item.item_tax_amount: asset_eiiav_currency = get_account_currency(eiiav_account) @@ -553,7 +553,7 @@ class PurchaseInvoice(BuyingController): "credit_in_account_currency": (item.item_tax_amount if asset_eiiav_currency == self.company_currency else item.item_tax_amount / self.conversion_rate) - })) + }, item=item)) else: cwip_account = get_asset_account("capital_work_in_progress_account", item.asset, company = self.company) @@ -567,7 +567,7 @@ class PurchaseInvoice(BuyingController): "debit_in_account_currency": (base_asset_amount if cwip_account_currency == self.company_currency else asset_amount), "cost_center": self.cost_center - })) + }, item=item)) if item.item_tax_amount and not cint(erpnext.is_perpetual_inventory_enabled(self.company)): asset_eiiav_currency = get_account_currency(eiiav_account) @@ -580,7 +580,7 @@ class PurchaseInvoice(BuyingController): "credit_in_account_currency": (item.item_tax_amount if asset_eiiav_currency == self.company_currency else item.item_tax_amount / self.conversion_rate) - })) + }, item=item)) return gl_entries @@ -607,7 +607,7 @@ class PurchaseInvoice(BuyingController): "remarks": self.get("remarks") or _("Stock Adjustment"), "cost_center": item.cost_center, "project": item.project - }, account_currency) + }, account_currency, item=item) ) warehouse_debit_amount = stock_amount diff --git a/erpnext/accounts/doctype/sales_invoice/sales_invoice.py b/erpnext/accounts/doctype/sales_invoice/sales_invoice.py index 31a9c66f6f2..aec965c9f21 100644 --- a/erpnext/accounts/doctype/sales_invoice/sales_invoice.py +++ b/erpnext/accounts/doctype/sales_invoice/sales_invoice.py @@ -784,7 +784,7 @@ class SalesInvoice(SellingController): if account_currency==self.company_currency else flt(item.net_amount, item.precision("net_amount"))), "cost_center": item.cost_center - }, account_currency) + }, account_currency, item=item) ) # expense account gl entries diff --git a/erpnext/accounts/general_ledger.py b/erpnext/accounts/general_ledger.py index a661c03b35b..e15dd2a7a63 100644 --- a/erpnext/accounts/general_ledger.py +++ b/erpnext/accounts/general_ledger.py @@ -7,6 +7,7 @@ from frappe.utils import flt, cstr, cint from frappe import _ from frappe.model.meta import get_field_precision from erpnext.accounts.doctype.budget.budget import validate_expense_against_budget +from erpnext.accounts.doctype.accounting_dimension.accounting_dimension import get_accounting_dimensions class StockAccountInvalidTransaction(frappe.ValidationError): pass @@ -49,10 +50,11 @@ def process_gl_map(gl_map, merge_entries=True): def merge_similar_entries(gl_map): merged_gl_map = [] + accounting_dimensions = get_accounting_dimensions() for entry in gl_map: # if there is already an entry in this account then just add it # to that entry - same_head = check_if_in_list(entry, merged_gl_map) + same_head = check_if_in_list(entry, merged_gl_map, accounting_dimensions) if same_head: same_head.debit = flt(same_head.debit) + flt(entry.debit) same_head.debit_in_account_currency = \ @@ -69,16 +71,24 @@ def merge_similar_entries(gl_map): return merged_gl_map -def check_if_in_list(gle, gl_map): +def check_if_in_list(gle, gl_map, dimensions=None): + account_head_fieldnames = ['party_type', 'party', 'against_voucher', 'against_voucher_type', + 'cost_center', 'project'] + + if dimensions: + account_head_fieldnames = account_head_fieldnames + dimensions + for e in gl_map: - if e.account == gle.account \ - and cstr(e.get('party_type'))==cstr(gle.get('party_type')) \ - and cstr(e.get('party'))==cstr(gle.get('party')) \ - and cstr(e.get('against_voucher'))==cstr(gle.get('against_voucher')) \ - and cstr(e.get('against_voucher_type')) == cstr(gle.get('against_voucher_type')) \ - and cstr(e.get('cost_center')) == cstr(gle.get('cost_center')) \ - and cstr(e.get('project')) == cstr(gle.get('project')): - return e + same_head = True + if e.account != gle.account: + same_head = False + + for fieldname in account_head_fieldnames: + if cstr(e.get(fieldname)) != cstr(gle.get(fieldname)): + same_head = False + + if same_head: + return e def save_entries(gl_map, adv_adj, update_outstanding, from_repost=False): if not from_repost: diff --git a/erpnext/controllers/accounts_controller.py b/erpnext/controllers/accounts_controller.py index fda1a406473..11c70b3dc78 100644 --- a/erpnext/controllers/accounts_controller.py +++ b/erpnext/controllers/accounts_controller.py @@ -339,7 +339,7 @@ class AccountsController(TransactionBase): frappe.throw(_("Row #{0}: Account {1} does not belong to company {2}") .format(d.idx, d.account_head, self.company)) - def get_gl_dict(self, args, account_currency=None): + def get_gl_dict(self, args, account_currency=None, item=None): """this method populates the common properties of a gl entry record""" posting_date = args.get('posting_date') or self.get('posting_date') @@ -372,6 +372,8 @@ class AccountsController(TransactionBase): for dimension in accounting_dimensions: dimension_dict[dimension] = self.get(dimension) + if item and item.get(dimension): + dimension_dict[dimension] = item.get(dimension) gl_dict.update(dimension_dict) gl_dict.update(args) diff --git a/erpnext/controllers/stock_controller.py b/erpnext/controllers/stock_controller.py index d11c644817e..9e791bbd6ea 100644 --- a/erpnext/controllers/stock_controller.py +++ b/erpnext/controllers/stock_controller.py @@ -80,7 +80,7 @@ class StockController(AccountsController): "cost_center": item_row.cost_center, "remarks": self.get("remarks") or "Accounting Entry for Stock", "debit": flt(sle.stock_value_difference, 2), - }, warehouse_account[sle.warehouse]["account_currency"])) + }, warehouse_account[sle.warehouse]["account_currency"], item=item_row)) # to target warehouse / expense account gl_list.append(self.get_gl_dict({ @@ -90,7 +90,7 @@ class StockController(AccountsController): "remarks": self.get("remarks") or "Accounting Entry for Stock", "credit": flt(sle.stock_value_difference, 2), "project": item_row.get("project") or self.get("project") - })) + }, item=item_row)) elif sle.warehouse not in warehouse_with_no_account: warehouse_with_no_account.append(sle.warehouse) diff --git a/erpnext/stock/doctype/purchase_receipt/purchase_receipt.py b/erpnext/stock/doctype/purchase_receipt/purchase_receipt.py index 1bd55f812f1..a3436412155 100644 --- a/erpnext/stock/doctype/purchase_receipt/purchase_receipt.py +++ b/erpnext/stock/doctype/purchase_receipt/purchase_receipt.py @@ -206,7 +206,7 @@ class PurchaseReceipt(BuyingController): "cost_center": d.cost_center, "remarks": self.get("remarks") or _("Accounting Entry for Stock"), "debit": stock_value_diff - }, warehouse_account[d.warehouse]["account_currency"])) + }, warehouse_account[d.warehouse]["account_currency"], item=d)) # stock received but not billed stock_rbnb_currency = get_account_currency(stock_rbnb) @@ -218,7 +218,7 @@ class PurchaseReceipt(BuyingController): "credit": flt(d.base_net_amount, d.precision("base_net_amount")), "credit_in_account_currency": flt(d.base_net_amount, d.precision("base_net_amount")) \ if stock_rbnb_currency==self.company_currency else flt(d.net_amount, d.precision("net_amount")) - }, stock_rbnb_currency)) + }, stock_rbnb_currency, item=d)) negative_expense_to_be_booked += flt(d.item_tax_amount) @@ -231,7 +231,7 @@ class PurchaseReceipt(BuyingController): "remarks": self.get("remarks") or _("Accounting Entry for Stock"), "credit": flt(d.landed_cost_voucher_amount), "project": d.project - })) + }, item=d)) # sub-contracting warehouse if flt(d.rm_supp_cost) and warehouse_account.get(self.supplier_warehouse): @@ -241,7 +241,7 @@ class PurchaseReceipt(BuyingController): "cost_center": d.cost_center, "remarks": self.get("remarks") or _("Accounting Entry for Stock"), "credit": flt(d.rm_supp_cost) - }, warehouse_account[self.supplier_warehouse]["account_currency"])) + }, warehouse_account[self.supplier_warehouse]["account_currency"], item=d)) # divisional loss adjustment valuation_amount_as_per_doc = flt(d.base_net_amount, d.precision("base_net_amount")) + \ @@ -263,7 +263,7 @@ class PurchaseReceipt(BuyingController): "remarks": self.get("remarks") or _("Accounting Entry for Stock"), "debit": divisional_loss, "project": d.project - }, stock_rbnb_currency)) + }, stock_rbnb_currency, item=d)) elif d.warehouse not in warehouse_with_no_account or \ d.rejected_warehouse not in warehouse_with_no_account: @@ -345,7 +345,7 @@ class PurchaseReceipt(BuyingController): "debit": base_asset_amount, "debit_in_account_currency": (base_asset_amount if cwip_account_currency == self.company_currency else asset_amount) - })) + }, item=d)) # Asset received but not billed asset_rbnb_currency = get_account_currency(arbnb_account) @@ -357,7 +357,7 @@ class PurchaseReceipt(BuyingController): "credit": base_asset_amount, "credit_in_account_currency": (base_asset_amount if asset_rbnb_currency == self.company_currency else asset_amount) - })) + }, item=d)) return gl_entries diff --git a/erpnext/stock/doctype/stock_entry/stock_entry.py b/erpnext/stock/doctype/stock_entry/stock_entry.py index d106ed126f6..c881d837ff4 100644 --- a/erpnext/stock/doctype/stock_entry/stock_entry.py +++ b/erpnext/stock/doctype/stock_entry/stock_entry.py @@ -611,7 +611,7 @@ class StockEntry(StockController): "cost_center": d.cost_center, "remarks": self.get("remarks") or _("Accounting Entry for Stock"), "credit": additional_cost - })) + }, item=d)) gl_entries.append(self.get_gl_dict({ "account": d.expense_account, @@ -619,7 +619,7 @@ class StockEntry(StockController): "cost_center": d.cost_center, "remarks": self.get("remarks") or _("Accounting Entry for Stock"), "credit": -1 * additional_cost # put it as negative credit instead of debit purposefully - })) + }, item=d)) return gl_entries