How do I apply taxes on a profit margin instead of amount?

I’ve tried implementing this with a client script by using a certain item tax template and manually setting the tax amount in the item and on the sales taxes and charges table. The issue is that we need the taxes to be inclusive and I was not able to get that to work with an “Actual” tax head. I also tried methods where I changed the values with my script but ERPNext automatically recalculates everything on reload so that is no use to me.

How should I go about this? I’ve checked similar questions but they are from 2016 and I want to know if there is any built-in way to achieve this

What I basically want is:
Item Price - 1000
Valuation rate - 400
Margin - 600
And then calculate tax on margin

Hi @zeeeeeeeero

This is possible by writing some scripts as I have tried on Quotation Doctype get similar results;

First identify Gross Profit = selling rate (Rate Field) - Valuation Rate and tabulate it in another section as GP on the whole transaction as following;

Then Script will take over and throws tax calculated on accumulated GP for all items in tax table as following;

For Tax Line the following client script may help;

I Have Applied 10 % directly in the script.

frappe.ui.form.on(‘Quotation’, {
gross_profit: function(frm) {
apply_tax_on_gross_profit(frm);
},
refresh: function(frm) {
apply_tax_on_gross_profit(frm);
}
});

function apply_tax_on_gross_profit(frm) {
const tax_rate = 10.0; // 10% tax

if (frm.doc.custom_gross_profit && frm.doc.custom_gross_profit > 0) {
    const tax_amount = (frm.doc.custom_gross_profit * tax_rate) / 100;

    // Check if a custom Gross Profit Tax already exists to avoid duplicates
    const existing = frm.doc.taxes.find(t => t.description === "Tax on Gross Profit");
    if (!existing) {
        frm.add_child("taxes", {
            charge_type: "Actual",
            account_head: "VAT 5 % - T&C", // replace with correct account
            description: "Tax on Gross Profit",
            tax_amount: tax_amount
        });
        frm.refresh_field("taxes");
    }
}

}

And for Calculation of GP, Server Script can be shared upon private request.