Matching Purchase Receipt with Purchase Order

I have a situation where we want to generate a purchase receipt without bringing in the item from the PO.

We want to be able to scan in the items into the Purchase Receipt one by one. We however still want to be able to reference the PR to the PO to enable us know what quantities have been received and which ones are outstanding on the PO.

We created a custom field in the PR form and linked it to PO doctype

We then created a custom script on the PR page as below

frappe.ui.form.on(“Purchase Receipt”, “validate”, function(frm,cdt,cdn) {
$.each(frm.doc.items,
function(i,v) {
if (frm.doc.purchase_order_no) {
v.purchase_order = frm.doc.purchase_order_no;
}
})
frm.refresh_field(“items”);
});

While this appears to attach the PR to the PO (we see the PR in the PO’s dashboard) it does not update the quantity received column of the item in the PO, this defeats the purpose.

Any idea what else can be done ? Or is there any alternative?

The use case scenario is for a wholesaler who orders hundreds of items on one PO, it is very cumbersome to bring in all the items from the PO into the PR and then start scrolling up and down to key in the physical items received. It is more convenient to scan the items as seen into the PR and then link this to the PO.

Any solution will be very appreciated

@AccountingMajor , I have faced this issue also, the updating issue is for cache issue. You can Ctrl+Shift+R to clear the cache or in form’s refresh event you can fetch this item’s quantity from and set value in quantity field that you will get.
For getting specific value from database you can do this:

frappe.db.get_value('Your_Doctype', 'your_field_name', ['quantity']).then(data => {
// set quantity field's value from data. Do console.log(data) for seeing the json.
});

your_field_name is name field of that item and quantity field is the field name of your own quantity field’s name.

Thanks @pabon_cse_12

Am not sure this is a cache issue …I did the Ctrl+Shift+R thingy and this did not update the PO item field with the received item

With respect to the code, where do I put this?

Regards

@AccountingMajor, you need to put this code into javascript file of your doctype. Then place this code mentioned below

frappe.ui.form.on('Your_Doctype', {
    refresh: function(frm, cdt, cdn) {
        // put this code here
    }
})