Payroll: Can we have employee-wise deduction in Payroll Journal Entry?

In Payroll Settings if “Process Payroll Accounting Entry based on Employee” then Payroll Payable will be booked against each employee in the Journal Entry.

Can we do the same for a deduction component, say for Provident Fund deduction so that in the journal the Provident Fund deductions will be posted against each employee?

Usecase: for Provident Fund deductions, as only one single Journal gets created for each payroll, it is difficult to understand how much is deducted for each employee!

We could use salary register for this, but what if we are migrating from another system, then we will have to look for two reports in order to figure out the total amount when someone is leaving and we need to settle their PF amounts.

It would have been nice if we could implement the same like employee-wise liabilities in journal posting

Hi there,

There’s no built-in way to do this, but it’s possible with event hook scripting.

I’ve got a server script that runs on the Journal Entry’s before_insert event, which splits payable account entries lines down by party.

payroll_entries = [ x.reference_name for x in doc.accounts if x.reference_type =="Payroll Entry" ]
payroll_entry = frappe.get_doc("Payroll Entry", payroll_entries[0]) if len(payroll_entries) > 0 else None

print(payroll_entry)
print(payroll_entries)
if payroll_entry:
    # get slips, accounts, and payability details
    slips = [ frappe.get_doc("Salary Slip", name) for name in frappe.db.get_all("Salary Slip", filters={"docstatus": ["!=", 2], "payroll_entry": payroll_entry.name }, pluck="name") ]
    component_accounts = { x.parent:x.account for x in frappe.db.get_all("Salary Component Account", fields=['parent', 'account'], filters={'company': doc.company}) }
    payable_accounts = frappe.db.get_all("Account", filters={"company": doc.company, "account_type": "Payable"}, pluck="name")

    # go through each account one by one
    for account in doc.accounts[:]:
        replacements = []
        print(f"{account.account} - {payroll_entry.payroll_payable_account}")
        isPayable = account.account in payable_accounts
        for slip in slips:
            if account.account == payroll_entry.payroll_payable_account:
                print("PAYABLE ACCOUNT!")
                print(slip.net_pay)
                replacements.append({
                    'account': account.account, 
                    'exchange_rate': account.exchange_rate, 
                    'cost_center': account.cost_center, 
                    'project': account.project, 
                    'credit_in_account_currency': slip.net_pay,
                    'department': slip.department, 
                    'doctype': 'Journal Entry Account',
                    'party_type': 'Employee',
                    'party': slip.employee
                })
                pass
            else:
                for component in slip.earnings:
                    if component_accounts[component.salary_component] == account.account:
                        replacements.append({
                            'account': account.account, 
                            'exchange_rate': account.exchange_rate, 
                            'cost_center': account.cost_center, 
                            'project': account.project, 
                            'debit_in_account_currency': component.amount, 
                            'department': slip.department, 
                            'doctype': 'Journal Entry Account',
                            # 'party_type': 'Employee' if isPayable else None, # removed because these should never be payable?
                            # 'party': slip.employee if isPayable else None,
                            'reference_type': "Payroll Entry",
                            'reference_name': payroll_entry.name
                        })
                for component in slip.deductions:
                    if component_accounts[component.salary_component] == account.account:
                        replacements.append({
                            'account': account.account, 
                            'exchange_rate': account.exchange_rate, 
                            'cost_center': account.cost_center, 
                            'project': account.project, 
                            'credit_in_account_currency': component.amount, 
                            'department': slip.department, 
                            'doctype': 'Journal Entry Account',
                            'party_type': 'Employee' if isPayable else None,
                            'party': slip.employee if isPayable else None,
                        })
                    
                
        if len(replacements):
            print("replacement time")
            print(replacements)
            doc.accounts.remove(account)
            for replacement in replacements:
                doc.append('accounts', replacement)
            
print("---@@@---")
for account in doc.accounts:
    print(f"{account.account} - {account.debit_in_account_currency} - {account.credit_in_account_currency} ({account.party}, {account.department})")