Someone can help me convert this script to hooks event please?
JS:
frappe.ui.form.on("Purchase Invoice", "validate", function(frm) {
$.each(frm.doc.parcelas || [], function(i, d) {
// Create titulo
frappe.call({
method: "financeiro.financeiro.doctype.titulos.titulos.make_titulo",
args : {
vencimento_parcela: d.vencimento_parcela,
valor_parcela: d.valor_parcela,
bill_no: frm.doc.bill_no,
posting_date: frm.doc.posting_date,
supplier_name: frm.doc.supplier_name,
company: frm.doc.company,
},
callback: function(r){}
});
});
});
PY:
@frappe.whitelist()
def make_titulo(vencimento_parcela, valor_parcela, bill_no, posting_date, supplier_name, company):
titulos = frappe.new_doc("Titulos")
titulos.naming_series = "TIT-A-PAGAR-"
titulos.tipo_parte = "Supplier"
titulos.tipo_documento = "Purchase Invoice"
titulos.data_vencimento = vencimento_parcela
titulos.valor_titulo = valor_parcela
titulos.numero_nota = bill_no
titulos.data_postagem = posting_date
titulos.parte = supplier_name
titulos.empresa = company
titulos.save()
return titulos.name
In hooks.py
doc_events = {
"Purchase Invoice": {
"validate": "path_to_your_method"
}
}
Also add following in hooks,
doctype_js = {"Purchase Invoice" : "public/js/purchase_invoice_doctype.js"}
add the js code to be added on Purchase Invoice
Doctype in file your_app/public/js/purchase_invoice_doctype.js
refer erpnext/hooks.py
1 Like
I wanna pass the js code for python, so i don’t need use js anymore…
Like:
def make_titulo(vencimento_parcela, valor_parcela, bill_no, posting_date, supplier_name, company):
for each in parcelas.idx:
doc = frappe.get_doc("Accounts", purchase_invoice.name)
titulos = frappe.new_doc("Titulos")
titulos.naming_series = "TIT-A-PAGAR-"
titulos.tipo_parte = "Supplier"
titulos.tipo_documento = "Purchase Invoice"
titulos.data_vencimento = doc.due_date
titulos.valor_titulo = doc.valor_parcela
titulos.numero_nota = doc.bill_no
titulos.data_postagem = doc.posting_date
titulos.parte = doc.supplier_name
titulos.empresa = doc.company
titulos.save()
return titulos.name
That’s it?
And the hooks just:
doc_events = {
"Purchase Invoice": {
"validate": "titulo.make_titulo"
}
}
Refer ERPNext hooks
doc_event hook for User doctype
Edit hooks.py of your app like this.
{"from_route": "/shipments", "to_route": "Delivery Note"},
{
"from_route": "/shipments/<path:name>",
"to_route": "order",
"defaults": {
"doctype": "Delivery Note",
"parents": [{"label": "Shipments", "route": "shipments"}],
},
},
{"from_route": "/rfq", "to_route": "Request for Quotation"},
{
"from_route": "/rfq/<path:name>",
"to_route": "rfq",
"defaults": {
"doctype": "Request for Quotation",
"parents": [{"label": "Request for Quotation", "route": "rfq"}],
},
},
{"from_route": "/addresses", "to_route": "Address"},
{
"from_route": "/addresses/<path:name>",
python function which adds additional validation to User DocType
Add a function to your app’s python code like this
https://github.com/frappe/erpnext/blob/develop/erpnext/hr/doctype/employee/employee.py#L187
I tried:
hooks.py:
docevents = "Purchase Invoice": {
"validate": "financeiro.financeiro.doctype.titulos.titulos.make_titulo"
}
py function in titulos.py:
def make_titulo(vencimento_parcela, valor_parcela, bill_no, posting_date, supplier_name, company):
for each in parcelas.idx:
doc = frappe.get_doc("Accounts", purchase_invoice.name)
titulos = frappe.new_doc("Titulos")
titulos.naming_series = "TIT-A-PAGAR-"
titulos.tipo_parte = "Supplier"
titulos.tipo_documento = "Purchase Invoice"
titulos.data_vencimento = doc.due_date
titulos.valor_titulo = doc.valor_parcela
titulos.numero_nota = doc.bill_no
titulos.data_postagem = doc.posting_date
titulos.parte = doc.supplier_name
titulos.empresa = doc.company
titulos.save()
return titulos.name
And give me internal server error
Now don’t give me error, i’ll try the python script
My script it’s not taking arguments
I think im not getting the doc in the right way, im passing cdt(current doctype)
(Parcelas it’s a table)
def make_titulo(vencimento_parcela, valor_parcela, bill_no, posting_date, supplier_name, company):
for parcelas in parcelas.idx:
payment_entry = frappe.get_doc("Purchase Invoice", cdt)
titulos = frappe.new_doc("Titulos")
titulos.naming_series = "TIT-A-PAGAR-"
titulos.tipo_parte = "Supplier"
titulos.tipo_documento = "Purchase Invoice"
titulos.data_vencimento = purchase_invoice.parcelas.vencimento_parcela
titulos.valor_titulo = purchase_invoice.parcelas.valor_parcela
titulos.numero_nota = purchase_invoice.bill_no
titulos.data_postagem = purchase_invoice.posting_date
titulos.parte = purchase_invoice.supplier_name
titulos.empresa = purchase_invoice.company
titulos.save()
return
What i’m doing wrong? When i put the js code in Custom Script List of site this works…
Error:
Failed to load resource: the server responded with a status of 500 (INTERNAL SERVER ERROR)
desk.min.js:1402 Traceback (most recent call last):
File "/home/frappe/frappe-bench/apps/frappe/frappe/app.py", line 56, in application
response = frappe.handler.handle()
File "/home/frappe/frappe-bench/apps/frappe/frappe/handler.py", line 21, in handle
data = execute_cmd(cmd)
File "/home/frappe/frappe-bench/apps/frappe/frappe/handler.py", line 52, in execute_cmd
return frappe.call(method, **frappe.form_dict)
File "/home/frappe/frappe-bench/apps/frappe/frappe/__init__.py", line 920, in call
return fn(*args, **newargs)
TypeError: make_titulo() takes at least 2 arguments (6 given)
Hooks
https://github.com/leonardoaugt/financeiro/blob/master/financeiro/hooks.py
PY:
https://github.com/leonardoaugt/financeiro/blob/master/financeiro/financeiro/doctype/titulos/titulos.py#L13
JS:
https://github.com/leonardoaugt/financeiro/blob/master/financeiro/public/js/purchase_invoice_doctype.js#L1
Don’t make sense call the py method by hooks event, cause the js are calling this… So how can i do this now?
1 Like
I removed the py method of hooks and give me:
Uncaught ReferenceError: vencimento_parcela is not defined
I did not understand… if this works in Custom Script List, should be working in another place too…
SanRam
January 28, 2020, 4:13am
12
Try this,
In hooks.py
doc_events = {
“Purchase Invoice”: {
“validate”: “financeiro.financeiro.doctype.titulos.titulos.make_titulo”
}
}
PY:
@frappe.whitelist ()
def make_titulo(doc,method):
titulos = frappe.new_doc(“Titulos”)
titulos.naming_series = “TIT-A-PAGAR-”
titulos.tipo_parte = “Supplier”
titulos.tipo_documento = “Purchase Invoice”
titulos.data_vencimento = doc.vencimento_parcela
titulos.valor_titulo = doc.valor_parcela
titulos.numero_nota = doc.bill_no
titulos.data_postagem = doc.posting_date
titulos.parte = doc.supplier_name
titulos.empresa = doc.company
titulos.save(ignore_permissions=True)