Hi
I am asking for some advise. I have completed my custom script which seems to work. But I would like to improve it.
Goal…
In purchase receipt, if the item that is received has a serial number that is generated automatically ,
then Purchase receipt works. If the serial number field in the item master is blank i.e. serial
number to be supplied by the user, purchase receipt does not work.
I decided to create a little custom scripty for this. The process is as follows…
There is now a custom button in the items table. The row of the item that the user wants to create
a serial number is selected and the user selects the “Create serial number” button. This opens a
new serial no doc and auto fills some fields. The user enters the serial number and saves.
There is also an added field in the items table of purchase receipt called “custom_serial_number”.
This is of type LINK that links to serial no doctype. And after the user has created the serial number
as described above, he/she can then select that serial number in that field. The filters has been extended to make available only the created serial number.
This process can work.
I was wondering if there is not a way that, after the user has created the serial number ( using the
custom button) that the field in the items table ( custom_serial_number) can be auto-loaded ??
I hope I have explained clearly.
Does someone perhaps have some advise for me to implement such an improvement ?
Here is code
frappe.ui.form.on('Purchase Receipt', {
refresh(frm) {
if (frm.doc.docstatus === 0) {
frm.fields_dict['items'].grid.add_custom_button(__('Create Serial Number'), function(doc) {
var selected_item = frm.fields_dict['items'].grid.get_selected_children()[0]; // Get the selected row
if (selected_item && !selected_item.serial_no) {
var newDoc = frappe.model.get_new_doc('Serial No');
newDoc.item_code = selected_item.item_code;
newDoc.purchase_document_type = 'Purchase Receipt';
newDoc.purchase_document_no = frm.doc.name;
newDoc.parenttype = 'Purchase Receipt';
newDoc.parentfield = 'serial_no';
newDoc.parent = selected_item.name;
frappe.ui.form.make_quick_entry('Serial No', null, frm, newDoc);
}
});
}
cur_frm.fields_dict["items"].grid.get_field("custom_serial_number").get_query = function(doc, cdt, cdn) {
var row = locals[cdt][cdn];
return {
filters: {
"status": 'Inactive',
"item_code": row.item_code,
"purchase_document_type": 'Purchase Receipt',
"purchase_document_no": frm.doc.name
}
};
};
}
});
frappe.ui.form.on('Purchase Receipt Item', {
custom_serial_number: function(frm, cdt, cdn) {
var row = locals[cdt][cdn];
row.serial_no = row.custom_serial_number;
refresh_field("items");
}
});
Thank you