Update child table on "grand_total" change

Hello,

Any idea would be appreciated, I’ve tried so many scripts.

I added a link field and child table in Customer doctype:
“tabla_comisiones_por_venta” : link field
comisiones_por_venta : child table

On select “tabla_comisiones_por_venta” populates data on child table.

frappe.ui.form.on("Customer", "tabla_comisiones_por_venta", function(frm) {
	frappe.model.with_doc("Tabla Comisiones por Venta", frm.doc.tabla_comisiones_por_venta, function() {
	if(frm.doc.tabla_comisiones_por_venta){
		var tabletransfer= frappe.model.get_doc("Tabla Comisiones por Venta", frm.doc.tabla_comisiones_por_venta);
		$.each(tabletransfer.comisiones, function(index, row){
			d = frm.add_child("comisiones_por_venta");
			d.account_head = row.account_head;
			d.percentage = row.percentage;
			cur_frm.refresh_field("comisiones_por_venta");
		})
	}
	});
});

Works fine.

To fetch on Sales Order…

cur_frm.add_fetch("customer", "tabla_comisiones_por_venta", "tabla_comisiones_por_venta");

But I just can’t make the table updates on “grand_total” changes.

frappe.ui.form.on("Sales Order", "tabla_comisiones_por_venta", function(frm) {
	frappe.model.with_doc("Tabla Comisiones por Venta", frm.doc.tabla_comisiones_por_venta, function() {
	if(frm.doc.tabla_comisiones_por_venta){
		var tabletransfer= frappe.model.get_doc("Tabla Comisiones por Venta", frm.doc.tabla_comisiones_por_venta);
		$.each(tabletransfer.comisiones, function(index, row){
			d = frm.add_child("comisiones_por_venta");
			d.account_head = row.account_head;
			d.percentage = row.percentage;
			d.commission = (row.percentage/100) * frm.doc.grand_total;
			cur_frm.refresh_field("comisiones_por_venta");
		})
	}
	});
});

To update I have to erase data selected on “tabla_comisiones_por_venta” and every row on table. Then select again on “tabla_comisiones_por_venta” to populate
d.commission = (row.percentage/100) * frm.doc.grand_total;

I have tried with no success, scripts like…

frappe.ui.form.on("Comisiones por Venta", {
	validate: function(frm, cdt, cdn) {
        var d = locals[cdt][cdn];
        frappe.model.set_value(d.doctype, d.name, "commission", (d.percentage/100) * frm.doc.grand_total);
	validate = true;
    }
});

Or…

frappe.ui.form.on("Comisiones por Venta", {
            	grand_total: function(frm, cdt, cdn) {
            	var d = locals[cdt][cdn];
            	frappe.model.set_value(d.doctype, d.name, "commission", frm.doc.grand_total);
            	validate = true;
            	frm.refresh();
            	}
});

Or…

frappe.ui.form.on("Sales Order", "grand_total", function(frm) {
	frappe.model.with_doc("Tabla Comisiones por Venta", frm.doc.comisiones_por_venta,
		function(frm, cdt, cdn) {
		frappe.model.set_value(d.doctype, d.name, "commission", frm.doc.grand_total);
		frm.refresh();
	});
});

What I’m looking for is to update “Comisiones por Venta” comisiones_por_venta child table on every change at grand_total field.

Nobody?
Anybody?
Any idea how to?..

What you can do is:

frappe.ui.form.on("Sales Order", "grand_total", function(frm) {
	 //loop your child table something like:
    for(var i=0;i<frm.doc.childtable;i++)
    {  frm.doc.childtable[i].fieldtoupdate = newval }
});

@johnskywalker, hi.

I just can’t make it work using “grand_total” field to trigger, so I decided to calculate that “grand total” value as “sum_grand_total” to be able to use it in another custom field.

frappe.ui.form.on("Sales Order Item", "qty", function(frm, cdt, cdn) {
	var d = locals[cdt][cdn];
	var sum_grand_total = 0;
	$.each(frm.doc.items, function(i, d) { sum_grand_total += d.qty * d.rate });
	frm.set_value("total_venta_recibir", sum_grand_total - (sum_grand_total * (frm.doc.commission_rate/100)));
	console.log (sum_grand_total);
	cur_frm.refresh_field("total_venta_recibir");
}); 

Based on same code, I can update “sum_grand_total” value when a row is deleted

frappe.ui.form.on("Sales Order Item", "items_remove", function(frm, cdt, cdn)

But now the issue I’m having is that when a row is added, if “qty” isn’t modified, “sum_grand_total” is not updated. Note: In Sales Order at items table, number 1 is placed in quantity by default.

I’ve tried with “items_add” and “item_code”, but in any case “console.log (sum_grand_total)” results “NaN”.

I was looking the same thing and the best approach was to Save the form. cur_frm.save(); and everything will be updated automatically, including grand_total. @Francisco_Buendia

1 Like

Thank you!
I will try…