Discount also calculated like line items (Vat)

Dear Community,
In Sales Invoice discount added based on Apply Additional Discount On =“Grand Total” or “Net Total”


I create custom table the total price , and vat should calculate on discount also,
it should work based on Apply Additional Discount On =“Grand Total” or “Net Total”
image

how to add here

<tr>
                <td class="left" colspan="4">Discount:</td>
                <td></td>
                <td></td>
                <td></td>
                <td class="center">{{ "{:,.2f}".format(doc.discount_amount) }}</td>
            </tr>

for eg:

i need yellow highlighted one. i tried
image
it works on “Grand Total” not in “Net Total”

<tr>
                <td class="left" colspan="4">Discount:</td>
                <td></td>
                <td class="right">{{ "{:,.2f}".format(doc.total - doc.net_total) }}</td>
                <td class="right">{{ "{:,.2f}".format((doc.total - doc.net_total) * 0.15) }}
                <td class="center">{{ "{:,.2f}".format(doc.discount_amount) }}</td>
            </tr>

kindly check my code and solve my query.

Thanks in Advance

I tried thid code now it works but the calculation is correct?

  {% if doc.discount_amount and doc.discount_amount > 0 %}
                {% set vat_rate = 0.15 if doc.company == "ABC" else 0.05 %}
                {% set discount_excluding_vat = doc.discount_amount / (1 + vat_rate) %}
                {% set discount_vat_amount = doc.discount_amount - discount_excluding_vat %}
                
                <tr>
                    <td class="left" colspan="4">Discount:</td>
                    <td></td>
                    <td class="right">{{ "{:,.2f}".format(discount_excluding_vat) }}</td> <!-- Calculated Unit Price for Discount -->
                    <td class="right">{{ "{:,.2f}".format(discount_vat_amount) }}</td> <!-- VAT Amount for Discount -->
                    <td class="right">{{ "{:,.2f}".format(doc.discount_amount) }}</td> <!-- Total + VAT for Discount -->
                </tr>
            {% endif %}

image

Thanks in Advance