Cost Center not Saving on GL entry when Account is not Income or Expense

The Cost Center in Journal Entry Account is not saving on
GL entry when account is not Income or Expense.

How will i able to change this rule

Cost center is used to track profit/loss so it cannot be used for other than expense/income accounts. This is accounting standard practice.

What i want to do is to add a Cost Center Filter in the GL Entry but the Journal Entry only saves Income or Expense type accounts Cost center in the tabGL Entry

I cant seem to find the script that triggers this.

Bump this please! I would love to know how to add cost centers (or some other data such as territory, department, etc to journal entries).

Unfortunately, this is not easy. The best way of accomplishing this without messing with core code is the following.

  1. Use custom fields to add whatever you want to Journal Entry and Journal Entry Account.
  2. Create a new doctype to store this data (New GL Entry or something like that)
  3. Hook into Journal entry to save your custom fields into this new GL Entry doctype
  4. Write reports to join the Existing GL Entry and new GL Entry to do what you need to do

Overall, this requires development of a new custom app for your needs.

When you add a custom field in Journal Entry or Journal Entry Accounts it does not go directly to GL Entry table to be used for filtering later when modifying reports.

  1. You have to add a custom_field in Journal Entry or Journal Entry Accounts

  2. Add a custom_field in gl_entry via doctype

  3. Then open journal_entry.py in erpnext/erpnext/account/doctype/journal_entry

  4. You have to modify this code

     		gl_map.append(
     			self.get_gl_dict({
     				"account": d.account,
     				"party_type": d.party_type,
     				"party": d.party,
     				"against": d.against_account,
     				"debit": flt(d.debit, d.precision("debit")),
     				"credit": flt(d.credit, d.precision("credit")),
     				"account_currency": curr,
     				"debit_in_account_currency": debit_ac,
     				"credit_in_account_currency": credit_ac,
     				"against_voucher_type": d.reference_type,
     				"against_voucher": d.reference_name,
     				"remarks": self.remark,
     				"cost_center": d.cost_center,
     				"memo": d.memo,
     				"reference_no": self.reference_no,
     				"cheque_no": self.cheque_no
    
  5. For example i added a new custom field in Journal Entry

     	if d.debit or d.credit:
     		gl_map.append(
     			self.get_gl_dict({
     				"account": d.account,
     				"party_type": d.party_type,
     				"party": d.party,
     				"against": d.against_account,
     				"debit": flt(d.debit, d.precision("debit")),
     				"credit": flt(d.credit, d.precision("credit")),
     				"account_currency": curr,
     				"debit_in_account_currency": debit_ac,
     				"credit_in_account_currency": credit_ac,
     				"against_voucher_type": d.reference_type,
     				"against_voucher": d.reference_name,
     				"remarks": self.remark,
     				"cost_center": d.cost_center,
     				"memo": d.memo,
     				"reference_no": self.reference_no,
     				"cheque_no": self.cheque_no,
     				"custom_field": self.custom_field
    
  6. if you added a custom_field in Journal Entry Accounts (use d. instead of self.)

     		gl_map.append(
     			self.get_gl_dict({
     				"account": d.account,
     				"party_type": d.party_type,
     				"party": d.party,
     				"against": d.against_account,
     				"debit": flt(d.debit, d.precision("debit")),
     				"credit": flt(d.credit, d.precision("credit")),
     				"account_currency": curr,
     				"debit_in_account_currency": debit_ac,
     				"credit_in_account_currency": credit_ac,
     				"against_voucher_type": d.reference_type,
     				"against_voucher": d.reference_name,
     				"remarks": self.remark,
     				"cost_center": d.cost_center,
     				"memo": d.memo,
     				"reference_no": self.reference_no,
     				"cheque_no": self.cheque_no,
     				"custom_field": d.custom_field,
    
  7. now you can modify the General Ledger Reports in erpnext/erpnext/accounts/reports/general_ledger

  8. Open the general_ledger.js to add filters for example

     {
     	"fieldname":"custom_field",
     	"label": __("Custom Field"),
     	"fieldtype": "Data",
     },
    
  9. Now Open general_ledger.py find def get_conditions(filters): add this code in here , after this step you can now filter using custom_field

if filters.get(“custom_field”):
conditions.append(“custom_field=%(custom_field)s”)

  1. To make custom_field appear in the report , for example lets put it in the last column, in def columns this code >

def get_columns(filters):
columns = [
_(“Posting Date”) + “:date:90”,
_(“Account”) + “:Link/Account:200”,
_(“Debit”) + “:Float:100”,
_(“Credit”) + “:Float:100”,
_(“Original Debit”)+ “:Float:100”,
_(“Original Credit”)+ “:Float:100”,
_(“Original Currency”)+ “::130”
]

if filters.get("show_in_account_currency"):
	columns += [
		_("Debit") + " (" + filters.account_currency + ")" + ":Float:100",
		_("Credit") + " (" + filters.account_currency + ")" + ":Float:100"
	]

columns += [
	_("Voucher Type") + "::120", 
	_("Voucher No") + ":Dynamic Link/Voucher Type:160",
	_("Against Account") + "::120", 
	_("Party Type") + "::80", 
	_("Party") + "::150",
	_("Cost Center") + ":Link/Cost Center:100", 
	_("Cheque No") + "::80", 
	_("Reference No") + "::100", 
	_("Memo") + "::150", 
	_("Remarks") + "::400"
]

return columns

1 1. Add custom field after Remarks

columns += [
	_("Voucher Type") + "::120", 
	_("Voucher No") + ":Dynamic Link/Voucher Type:160",
	_("Against Account") + "::120", 
	_("Party Type") + "::80", 
	_("Party") + "::150",
	_("Cost Center") + ":Link/Cost Center:100", 
	_("Cheque No") + "::80", 
	_("Reference No") + "::100", 
	_("Memo") + "::150", 
	_("Remarks") + "::400",
	_("Custom Field") + "::150"
]

12 in this code

def get_result_as_list(data, filters):
result =
for d in data:
row = [d.get(“posting_date”), d.get(“account”), d.get(“debit”), d.get(“credit”), d.get(“debit_currency”), d.get(“credit_currency”), d.get(“account_currency”)]

	if filters.get("show_in_account_currency"):
		row += [d.get("debit_in_account_currency"), d.get("credit_in_account_currency")]

	row += [d.get("voucher_type"), d.get("voucher_no"), d.get("against"),
		d.get("party_type"), d.get("party"), d.get("cost_center"), d.get("cheque_no"), d.get("reference_no"), d.get("Memo"), d.get("remarks")
	]

	result.append(row)

return result

13 Add custom field after d.get remarks

	row += [d.get("voucher_type"), d.get("voucher_no"), d.get("against"),
		d.get("party_type"), d.get("party"), d.get("cost_center"), d.get("cheque_no"), d.get("reference_no"), d.get("Memo"), d.get("remarks"), d.get("custom_field")
	]

14 add custom_field in the query . find def get_gl_entries(filters):

gl_entries = frappe.db.sql(“”"
select
posting_date, account, party_type, party,
sum(debit) as debit, sum(credit) as credit,
voucher_type, voucher_no, cost_center, project,
against_voucher_type, against_voucher,
remarks, against, is_opening {select_fields}
from tabGL Entry
where company=%(company)s {conditions}
{group_by_condition}
order by posting_date, account"“”
.format(select_fields=select_fields, conditions=get_conditions(filters),
group_by_condition=group_by_condition), filters, as_dict=1)

15 Add custom_field in this code

gl_entries = frappe.db.sql(“”"

  select
  	posting_date, account, party_type, party,
  	sum(debit) as debit, sum(credit) as credit,
  	voucher_type, voucher_no, cost_center, project,
  	against_voucher_type, against_voucher,
  	remarks, against, **custom_field,** is_opening {select_fields}
  from `tabGL Entry`
  where company=%(company)s {conditions}
  {group_by_condition}
  order by posting_date, account"""\
  .format(select_fields=select_fields, conditions=get_conditions(filters),
  	group_by_condition=group_by_condition), filters, as_dict=1)

il edit this later im in a hurry :grin:

2 Likes