Here I have built a custom app, with a doctype to automate the Journal Entry using the values available.
Conditions are: I am passing following values for JE
principal_in_usd = round(self.principal_in_usd, 2)
fx_in_usd = round(self.fx_in_usd, 2)
charges_in_usd = round(self.charges_in_usd, 2)
principal_in_npr = round(self.principal_in_npr, 2)
fx_in_npr = round(self.fx_in_npr, 2)
charges_in_npr = round(self.charges_in_npr, 2)
exchange_rate = round(self.exchange_rate, 2)
net_receivable = principal_in_usd + fx_in_usd + charges_in_usd
total_debit = net_receivable * exchange_rate
total_credit = principal_in_npr + fx_in_npr + charges_in_npr
net_receivable = round(net_receivable, 2)
# Calculate net_roundoff more precisely
net_roundoff = round(total_debit - total_credit, 2)
default_cost_center = frappe.db.get_value("Company", company, "cost_center")
frappe.msgprint(f"Net Receivable: {net_receivable}, Principal in NPR: {principal_in_npr}, FX in NPR: {fx_in_npr}, Charges in NPR: {charges_in_npr}, Net Roundoff: {net_roundoff}")
frappe.logger().info(f"Total Debit: {total_debit}, Total Credit: {total_credit}, Net Roundoff: {net_roundoff}")
accounts = [
{
"account": partner.default_receivable_account,
"party_type": "Partner",
"party": self.partner,
"debit_in_account_currency": round(net_receivable, 2),
"exchange_rate": exchange_rate,
},
{
"account": partner.default_principal_account,
"credit_in_account_currency": principal_in_npr,
"exchange_rate": 1,
}
]
if charges_in_npr != 0:
accounts.append({
"account": partner.default_commission_account,
"credit_in_account_currency": charges_in_npr if charges_in_npr > 0 else 0,
"debit_in_account_currency": abs(charges_in_npr) if charges_in_npr < 0 else 0,
"cost_center": default_cost_center,
"exchange_rate": 1,
})
if fx_in_npr != 0:
accounts.append({
"account": partner.default_fx_account,
"credit_in_account_currency": fx_in_npr if fx_in_npr > 0 else 0,
"debit_in_account_currency": abs(fx_in_npr) if fx_in_npr < 0 else 0,
"cost_center": default_cost_center,
"exchange_rate": 1,
})
if net_roundoff != 0:
accounts.append({
"account": default_roundoff_account,
"credit_in_account_currency": net_roundoff if net_roundoff > 0 else 0,
"debit_in_account_currency": abs(net_roundoff) if net_roundoff < 0 else 0,
"cost_center": default_cost_center,
"exchange_rate": 1,
})
# Append accounts to the Journal Entry
for account in accounts:
je.append("accounts", account)
je.user_remark = f"Journal Entry for Money Transfer {self.name} linked with trans. no. {self.transaction_no}"
# Save and submit the Journal Entry
je.save()
je.submit()
self.receivable_journal_entry = je.name
I tried everything, but for some entries where it is hitting rounding error, is unable to submit. Is there any better way to handle JE entries through controller? Thank you experts for your time and help.