Sales invoice calculate line item amount with TAX

Hello,

I’m trying to calculate sales invoice line item amount with tax. I have a custom field to show tax percentage as well.

When I try to console log in item details, the amount is shows in the list. but when I try to fetch it from the list it’s 0. then I select another item in the same row it’s showing me a previous items value.

So, I set time out to check it and still output is same.

and I try field name as event as mentioned here but it’s not running at all->Form Scripts

but when I change the item the correct amount is display in the readonly field.
and I tried to make the field writable which is not success. I have added my code here.

Could you please help me for this ? Also, Please note this is not print purpose. and there will be another couple of fields for line item tax and amount incl tax.I just want to read the amount to calculate value for above fields.

TIA

frappe.ui.form.on('Sales Invoice Item', {
    
    refresh: function(frm){
        frm.set_df_property("amount", "read_only", 0);
    },
    
    item_code: function(frm,cdt,cdn){
        var doc = locals[cdt][cdn];
        
        //frm.set_df_property("doc.amount", "read_only", 0);
        
        console.log(doc); // the amount is showing in this list
        console.log(doc.amount); // but this is 0
        
        frappe.db.get_doc('Item', doc.item_code).then(item=>{
            console.log("item");
            console.log(item);
            console.log("item rate");
            console.log(item.custom_tax_rate);
                
            frappe.model.set_value(cdt, cdn, "custom_tax_rate", item.custom_tax_rate);  
            
        });
        
         setTimeout(() => {
                 console.log("doc");
                 console.log(doc.amount);
        }, 100);
    
    },
    
   
    
    amount: function(frm,cdt,cdn){
        var doc = locals[cdt][cdn];
         console.log("amount");
        console.log(doc);
         console.log(doc.amount);
    }
});

Hi @Vasana_Wijewardena,

I suggest adding another field called “Line Amount”. This addition is necessary because you can’t alter the Total/Amount field easily. The Total/Amount field is connected to many calculations such as taxes and shipping costs. If you still want to make changes, you’ll have to override many methods related to “total” or “amount” in your custom app. This process requires finding and overriding those methods. However, if you just want to keep track of changes, you can use a custom field instead.

I hope this helps.

Thank You!

Thank you, But I don’t want to update the default amount and just wanted to read the value. and the value is 0.

But it’s already readable.

It should be, but in the above code, when I console logs first one I can see the amount and other amount is 0. is it because i’m reading the value in the local cache?

Hi @Vasana_Wijewardena,

Please check it.

frappe.ui.form.on('Sales Invoice Item', {
	item_code: function(frm, cdt, cdn) {
		var row = locals[cdt][cdn];
		setTimeout(() => {
		    console.log("Amount: ", row.amount);
		}, 100);
	}
});

Thank You!

1 Like

oh Thank you, My issue was I’m using get_doc before that. If I remove it It’s working fine. and need to add Timeout as well :slight_smile:

Thank you!