How to Distribute Sales and Taxes Amount Across Item Quantity in Sales Order

Hi everyone,

I’m currently working on customizing the Sales Order in ERPNext, and I’d like to ask for some guidance or best practices on handling the distribution of sales and taxes across individual items based on total quantity.

Here’s what I’m trying to achieve:

Let’s say the total Sales and Taxes amount is 300, and the total quantity of all items is 3. I want to distribute this amount equally across the items, so each item gets an additional 100 added to its value.

Example:

  • Total Sales and Taxes: 300
  • Total Quantity of Items: 3
  • Distributed Sales/Taxes per Unit: 300 / 3 = 100

If an item currently has:

  • Base Amount: 500
  • Final Amount (after distributing taxes): 500 + 100 = 600

My goal is to reflect this logic in the Sales Order Item table, ideally without affecting the existing tax calculation engine, but just for the purpose of showing the updated amount per item (base + distributed tax).

I’m wondering:

  • Is there an existing DocType or feature in ERPNext that supports this kind of logic?
  • Would it be better to implement this as a custom field and custom script?
  • Any suggestions on how to fetch the total sales taxes and divide it by quantity effectively in a client or server script?

Any advice or direction would be greatly appreciated! Thank you!

How are the taxes calculated? Are they rate based? If you have sales order items with dissimilar unit prices, do you still want to aggregate the tax based on quantity?

Hi. I don’t know if you’ve already tried this, but you can create a new Sales Tax Template specifically for this case. Add a tax row and, in the Type field, select “On Item Quantity”.
For the Account Head, choose the account where you want to register this tax — for example, a generic “Tax per Unit” account.

:warning: Important: This account should NOT be of the account type “Tax”, because those accounts are percentage-based and won’t work correctly with unit-based taxes.
By using an account without the “Tax” type, you can enter a fixed rate in the tax template. ERPNext will then calculate the tax based on the “total quantity” of items in the Sales Invoice.

Also, the Tax Rate field remains editable in the invoice itself if needed.

:exclamation: The limitation is that this tax will apply to all items in the Sales Invoice. It’s not possible to exclude individual items from this tax when using this method.

Maybe you already tried this solution, if not, I hope it helps you reach a simple solution.

Regards

Best to use a custom field in the items table to capture the rate based on the tax amount distributed across the line items per qty.
Assuming the total tax is 100 for 10 total items, then we’re looking at something like
Line 1, 2 qty, rate 100, amount 200, custom_amount 220
Line 2, 3 qty, rate 200, amount 600, custom_amount 630
Line 5, 5 qty, rate 50, amount 250, custom_amount 300

You can just show the custom_amount and even the custom_tax if required in the prints. Don’t need to mess with the system’s logic. Unless you want the accounts to be updated as such as well…

Primero debes crear una Plantilla de impuestos de artículos.
Te recomiendo que guardes tus plantillas con el nombre del porcentaje del impuesto es decir:
Nombre e identificador de la plantilla: 15%

Crea tantas plantillas según valores de impuestos tengas
Segundo asigna las plantillas a los artículos correspondan

Recomendaciones: Procura que la cuentas contables de la plantilla de impuesto de artículos sean iguales a las cuentas de la plantilla de impuesto de ventas.
Es decir:

Plantilla impuesto de articulo: 15% Cuenta contable 23001
Plantilla de Impuesto de venta: 15% Cuenta contables 23001

De esta manera no afectaras el sistema de los calculos de impuesto del ERPNext

Tercero Crea un campo personalizado llamado: custom_ivs
Y otro campo llamado custom_gran_total

Cuarto para realizar el calculo debemos crear un script de cliente el cual te comparto acontinuacion:

frappe.ui.form.on('Sales Invoice',  {
    refresh: function(frm) {
        var percent = 0;
        $.each(frm.doc.items,  function(i, d) {
         if (d.item_tax_template == '15%') { percent = 0.15;}
          else if (d.item_tax_template == '18%') { percent = 0.18;}
          else if (d.item_tax_template == '0%') { percent = 0;}
            
            d.custom_ivs = d.net_amount * percent;
     
        });
     
    } 
});

Cambia el Sales Invoice por Orden Invoice para que lo programes en Orden de venta. Y lo guardas.

Sexto Crea un otro script de cliente de esta manera:

frappe.ui.form.on('Sales Invoice',  {
    refresh: function(frm) {
      
        $.each(frm.doc.items,  function(i, d) {
             d.custom_gran_total = d.amount + d.custom_ivs; });
        } 
   });

Cambia el Sales Invoice por Orden Invoice para que lo programes en Orden de venta. Y lo guardas.

Se vera algo así:
image

Espero te sirva esta información