Refresh field when submit other document

i have custom field in purchase order item called outstanding qty it’s calc (Stock qty - received qty ) but when i submit purchase Recipt it’s update only received qty and outstanding qty field not updated any solution

Maybe you can share your code so that someone can help.

Thanks,
Pawan

Dear @Pawan
Thanks for your response

frappe.ui.form.on("Purchase Order Item", {
outstanding_quantity: function(frm, cdt, cdn) {
var row = locals[cdt][cdn];
row.outstanding_quantity = flt(row.qty) - flt(row.received_qty);
frappe.model.set_value(cdt, cdn, 'outstanding_quantity', (flt(row.qty) - flt(row.received_qty)) )
frm.refresh_field("items"); 
},
qty: function(frm, cdt, cdn) {
var row = locals[cdt][cdn];
row.outstanding_quantity = flt(row.qty) - flt(row.received_qty);
frappe.model.set_value(cdt, cdn, 'outstanding_quantity', (flt(row.qty) - flt(row.received_qty)) )
frm.refresh_field("items");
},
received_qty: function(frm, cdt, cdn) {
var row = locals[cdt][cdn];
row.outstanding_quantity = flt(row.qty) - flt(row.received_qty);
frappe.model.set_value(cdt, cdn, 'outstanding_quantity', (flt(row.qty) - flt(row.received_qty)) )
frm.refresh_field("items");
},
});

Dear @Pawan
Any Update

Hi @Alaa_Badri

Put your code in a seperat function (e.g. function calculate(frm, cdt, cdn){…}) and call this function on the “refresh” trigger of “Purchase Order Item”…

Thanks @joelios for your response
it will be in the server side or by JS and could you give me an example

Both is possible, client and server side.
this is an example for client side:

frappe.ui.form.on("Purchase Order Item", {
outstanding_quantity: function(frm, cdt, cdn) {
    calcOutstandingQty(frm, cdt, cdn);
    },
    qty: function(frm, cdt, cdn) {
	calcQty(frm, cdt, cdn);
    },
    received_qty: function(frm, cdt, cdn) {
	calcReceivedQty(frm, cdt, cdn)
    },
    refresh: function(frm, cdt, cdn) {
	calcOutstandingQty(frm, cdt, cdn);
	calcQty(frm, cdt, cdn);
	calcReceivedQty(frm, cdt, cdn)
	    }
});

function calcOutstandingQty(frm, cdt, cdn) {
    var row = locals[cdt][cdn];
    row.outstanding_quantity = flt(row.qty) - flt(row.received_qty);
    frappe.model.set_value(cdt, cdn, 'outstanding_quantity', (flt(row.qty) - flt(row.received_qty)) )
    frm.refresh_field("items");
}
function calcQty(frm, cdt, cdn) {
    var row = locals[cdt][cdn];
    row.outstanding_quantity = flt(row.qty) - flt(row.received_qty);
    frappe.model.set_value(cdt, cdn, 'outstanding_quantity', (flt(row.qty) - flt(row.received_qty)) )
    frm.refresh_field("items");
}
function calcReceivedQty(frm, cdt, cdn) {
    var row = locals[cdt][cdn];
    row.outstanding_quantity = flt(row.qty) - flt(row.received_qty);
    frappe.model.set_value(cdt, cdn, 'outstanding_quantity', (flt(row.qty) - flt(row.received_qty)) )
    frm.refresh_field("items");
}
1 Like

After using above code I’m still need1 to update outstanding filed Manually

that was not a fix ready solution that you can take and it works.
It was an illustrative example of how you could create a corresponding script :wink:

you have to expand the following code block with en forEach loop through all rows of the childtable

refresh: function(frm) {
/* insert foreach loop from here */
calcOutstandingQty(frm, cdt, cdn);
calcQty(frm, cdt, cdn);
calcReceivedQty(frm, cdt, cdn)
/* to here... */
}

Thanks @joelios

I think the action has to be after Purchase Receipt submit with the trigger which update recived_qty tin Purchase Order Item to also update outstanding_quantity

Yeah you’re right…

something like that:

frappe.ui.form.on("Purchase Order Item", {
	outstanding_quantity: function(frm, cdt, cdn) {
		calcOutstandingQty(frm, cdt, cdn);
	},
		qty: function(frm, cdt, cdn) {
		calcQty(frm, cdt, cdn);
	},
		received_qty: function(frm, cdt, cdn) {
		calcReceivedQty(frm, cdt, cdn)
	}
});

frappe.ui.form.on("Purchase Order", {
	refresh: function(frm) {
		if (frm.doc.docstatus == "1") {
			/* forEach block */ {
				calcOutstandingQty(frm, cdt, cdn);
				calcQty(frm, cdt, cdn);
				calcReceivedQty(frm, cdt, cdn)
			} /* end forEach block */
		}
	}
});


function calcOutstandingQty(frm, cdt, cdn) {
	var row = locals[cdt][cdn];
	row.outstanding_quantity = flt(row.qty) - flt(row.received_qty);
	frappe.model.set_value(cdt, cdn, 'outstanding_quantity', (flt(row.qty) - flt(row.received_qty)) )
	frm.refresh_field("items");
}
function calcQty(frm, cdt, cdn) {
	var row = locals[cdt][cdn];
	row.outstanding_quantity = flt(row.qty) - flt(row.received_qty);
	frappe.model.set_value(cdt, cdn, 'outstanding_quantity', (flt(row.qty) - flt(row.received_qty)) )
	frm.refresh_field("items");
}
function calcReceivedQty(frm, cdt, cdn) {
	var row = locals[cdt][cdn];
	row.outstanding_quantity = flt(row.qty) - flt(row.received_qty);
	frappe.model.set_value(cdt, cdn, 'outstanding_quantity', (flt(row.qty) - flt(row.received_qty)) )
	frm.refresh_field("items");
}

Dear @joelios
I appreciate your help but i don’t understand what do you mean about

and the result i have now the outstanding field have the same qty filed value
Screenshot-2018-3-15 ATEC Egypt - Project Table-00005

@Alaa_Badri

use this, this works and is a lot smaller…

frappe.ui.form.on("Purchase Order", {
	refresh: function(frm) {
		if (frm.doc.docstatus == "1") {
			frm.doc.items.forEach(function(item) {
				item.outstanding_quantity = flt(item.stock_qty) - flt(item.received_qty);
			});
			frm.refresh_field("items");
		}
	}
});
1 Like

Thanks very much its working

:grinning::grinning::grinning::grinning::grinning:

but if have any explanation for

This iterates over the PO list of items, to recalculate outstanding_quantity for each item as you specified?

My question is what is the basis for this if (frm.doc.docstatus == “1”) {

With this “if”, the script only appears if the document is safed…

Ah yes thank you @joelios I think I follow you…

For that ahah sense of understanding here, I need more hands on trial and error homework

Dear @joelios
Do you have any suggestions for that

This function seems to document the docstatus states: