How to call function in .py file and solve this error(The resource you are looking for is not available)

Hello,

I am very new to erpnext guys please help me out.
my js code:

frappe.ui.form.on('Employee', 'apply_for_loan', function(frm){ 
	if(frm.doc.initial_payment > frm.doc.induction_fee_total_amount){
		msgprint("Initial payment can not be grater than Induction Fee Total Amount");
		validated = false;
	}
	else{   
		if(frm.doc.induction_fee_total_amount > 0 && frm.doc.initial_payment > 0){
			cur_frm.set_value("remaining_amount" , frm.doc.induction_fee_total_amount - frm.doc.initial_payment);
			frappe.call({
				method: "erpnext.hr.doctype.employee.make_loan_entry",
			});
		}
	}
});

and my python code:(in employee.py)

@frappe.whitelist()
def make_loan_entry():
        le = frappe.new_doc('Loan')
        print("test")

Guys please help me out i m stuck from 2 day and not getting what is the exact error and how to tackle with it.
Thanks

Are you getting any errors ?

Also, the Python code simply makes a New document of Loan and prints “test”.

Also in JS code:

erpnext.hr.doctype.employee.make_loan_entry

this is not the correct path. this one is

erpnext.hr.doctype.employee.employee.make_loan_entry

The first employee denotes the directory employee and the second one is for the employee.py file.

What do you exactly wish to do ? Explain your use case

Did you create “Loan” Doctype?

Make Sure you have Loan Doctype. I think you don’t have Loan Doctype.

Thank u sir for your reply.U said correct my path was wrong. its working now

Yes i know sir but i just want to make entry in loan if user click apply_for_loan Button. i do not want to show loan page to user. Thats why i m doing in this way sir.

This the code sir

@frappe.whitelist()
def make_loan_entry(data):
data= eval(data)
le = frappe.new_doc(‘Loan’)
le.applicant_type = “Employee”
le.applicant= data[“id”]
le.loan_type= “Uniform loan”
le.posting_date= data[“date_of_joining”]
le.company= data[“company”]
le.status= “Disbursed”
le.repay_from_salary= 1
le.loan_amount= data[“remaining_amount”]
le.disbursement_date= frappe.utils.today()
le.repayment_start_date= frappe.utils.today()
le.repayment_method= “Repay Fixed Amount per Period”
le.monthly_repayment_amount= data[“remaining_amount”]
le.mode_of_payment= “Cash”
le.payment_account= “Cash - SPS”
le.loan_account= “90295315941234 - Loan To Office Staf - SPS”
le.interest_income_account= “Sales - SPS”
le.total_payment= data[“remaining_amount”]
le.total_amount_paid= 0.000000
le.applicant_name= data[“full_name”]
le.rate_of_interest= 0.000000
le.total_interest_payable=0.000000
le.save()
return “test”, dat

Great. Please acquaint yourself with the erpnext directory structure. It is the same in most cases.

Also, I suggest that you don’t directly edit the source files - I understand that you have added this function in the original employee.py. Right now it may seem easy, but later when you to update, it will lead to conflicts. It is advisable you keep your functions in another custom app.

Also, same goes for doctypes - if you wish to add a new field to a default doctype, then use Customize Form not Edit Doctype. Also export these fixtures to your own app. Please read up about fixtures and custom app on this forum and you will get replies for any questions that come up.

OK Sir i will try to avoid making any changes in core files.
But Sir can i ask you that, if i want very small changes in any existing doc-type then crating a new doc or app would be long process.
That’s why i did in this way.

Thank and regards

Just make Loan doctype “User Cannot Create.” You can refer “GL Entry” For that.

Sir can you explain in details. Didn’t get You what You trying to say.

So user can’t create it from UI.

You can do one thing. Make one Python file. Write your function say make_loan_entry there (and yes it should be whitelisted via @frappe.whitelist() ) and use it. In this case , you don’t touch the original file and you get your function. Depending on the number of functions you write you may later move on to multiple files say modulewise.

Also, you’ll have to change the path since then the function is now in a different file.

Also, each time you update, be careful that this file is not deleted.

1 Like