Good morning,
Our team creates a layout for VAT reasons in the Kingdom. But we face an issue where the VAT in the layout will be calculated by item line instead of Row wise.
Please check the code and advise.
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Noto+Sans+Arabic:wght@100;200;300;400;500;600;700;800;900&display=swap" rel="stylesheet">
{%- macro add_header(page_num, max_pages, doc, letter_head, no_letterhead) -%}
{% if letter_head and not no_letterhead %}
<div style='padding-top: 40px !important'>
<span class="letter-head-1">{{ letter_head }}</span>
<span><img align="right" src="{{doc.qr_code}}"></span>
</div>
<div class="">
</div>
{% endif %}
{%- if doc.meta.is_submittable and doc.docstatus==2-%}
<div class="alert alert-danger text-center">
<h4 style="margin: 0px;">{{ _("CANCELLED") }}</h4>
</div>
{%- endif -%}
{% if max_pages > 1 %}
<p class="text-right">{{ _("Page #{0} of {1}").format(page_num, max_pages) }}</p>
{% endif %}
{%- endmacro -%}
{{ add_header(0,1,doc,letter_head, no_letterhead) }}
<div class="ksa-vat-format">
<div class="row text-center">
<div> <br /><br />
<h2 class="invoice-heading">فاتورة ضريبية</h2>
<h2 class="invoice-heading">TAX INVOICE</h2>
</div>
</div>
{% set customer_tax_id = frappe.db.get_value('Customer', doc.customer, 'tax_id') %}
{% set company = frappe.get_doc("Company", doc.company)%}
<!-- {% if (doc.company_address) %}-->
<!-- {% set supplier_address_doc = frappe.get_doc('Address', doc.company_address) %}-->
<!--{% endif %}-->
{% if (doc.company) %}
{% set supplier_address_doc = frappe.get_doc('Address', doc.company) %}
{% endif %}
{% if(doc.customer_address) %}
{% set customer_address = frappe.get_doc('Address', doc.customer_address ) %}
{% endif %}
{% if(doc.shipping_address_name) %}
{% set customer_shipping_address = frappe.get_doc('Address', doc.shipping_address_name ) %}
{% endif %}
<!--div class='row '>
<div class='col-sm-6'>
<div class='row'>
<div class='col-sm-6'>Invoice#: {{doc.name}}</div>
<div class='col-sm-6'>رقم الفاتورة: {{doc.name}}</div>
</div>
<div class='row '>
<div class='col-sm-6'>Invoice Date: {{doc.posting_date}}</div>
<div class='col-sm-6'>تاريخ الفاتورة: {{doc.posting_date}}</div>
</div>
<div class='row'>
<div class='col-sm-6'>Date of Supply:{{doc.posting_date}}</div>
<div class='col-sm-6'>تاريخ التوريد: {{doc.posting_date}}</div>
</div>
</div-->
<br />
<table class="rwd-table">
<tr>
<th class="th">رقم الفاتورة | Invoice ID</th>
<th class="th">تاريخ الفاتورة | Invoice Date</th>
<th class="th">تاريخ التوريد | Supply Date</th>
</tr>
<tr>
<td class="td-l">{{doc.name}}</td>
<td class="td-c">{{doc.posting_date}}</td>
<td class="td-r">{{doc.posting_date}}</td>
</tr>
</table>
<!--div class='col-sm-6' style=" padding: 0;">
<div class="row" style= " padding: 0;">
<img style="display:block !important" class="qr-code" src={{doc.qr_code}}>
</div>
</div-->
</div>
<div class="row flex">
<div class="col-xs-6">
<table class="ksa-invoice-table">
<thead>
<tr>
<th>Customer</th>
<th></th>
<th class='text-right'>العميل</th>
</tr>
</thead>
<tbody>
<tr>
<td>Name </td>
<td>{{ doc.customer }}</td>
<td class='text-right'>الاسم </td>
</tr>
<tr>
<td>Tax ID </td>
<td>{{ customer_tax_id }}</td>
<td class='text-right'>الرقم الضريبي </td>
</tr>
<tr>
</tr>
{% if(customer_address) %}
{% if customer_address.address_title%}
<tr>
<td> Street Name </td>
<td>{{ customer_address.address_title }}</td>
<td class='text-right'> اسم الشارع </td>
</tr>
{% endif %}
{% if customer_address.address_line1%}
<tr>
<td>Address </td>
<td>{{ customer_address.address_line1}}</td>
<td class='text-right'>العنوان </td>
</tr>
{% endif %}
{% if customer_address.city%}
<tr>
<td>City </td>
<td>{{ customer_address.city}}</td>
<td class='text-right'>المدينة </td>
</tr>
{% endif %}
{% if customer_address.country%}
<tr>
<td>Country </td>
<td>{{ customer_address.country}}</td>
<td class='text-right'>الدولة </td>
</tr>
{% endif %}
{% if customer_address.pincode %}
<tr>
<td>Postal Code </td>
<td>{{ customer_address.pincode}}</td>
<td class='text-right'>الرمز البريدي</td>
</tr>
{% endif %}
{% endif %}
{% if(customer_shipping_address) %}
<!--tr>
<td>Shipping Address</td>
<td>{{ customer_shipping_address.address_line1}}</td>
<td class='text-right'>عنوان الشحن</td>
</tr-->
{% endif %}
{% if(doc.po_no) %}
<tr>
<td>Purchase Order Number</td>
<td>{{ doc.po_no }}</td>
<td class='text-right'>رقم أمر الشراء</td>
</tr>
{% endif %}
<tr>
<td>Payment Due Date </td>
<td> {{ doc.due_date}}</td>
<td class='text-right'>تاريخ استحقاق الدفع</td>
</tr>
<!--tr>
<td>Other Buyer ID: </td>
<td> {{ doc.contact_person }}</td>
<td class='text-right'> معرف اخر</td>
</tr-->
</tbody>
</table>
</div>
<div class="col-xs-6">
<table class="ksa-invoice-table">
<thead>
<tr>
<th>Supplier </th>
<th></th>
<th class='text-right'>المورد</th>
</tr>
</thead>
<tbody>
<tr>
<td>Name </td>
<td>{{ doc.company }}</td>
<td class='text-right'>الاسم </td>
</tr>
<tr>
<td>Tax ID </td>
<td>{{ company.tax_id }}</td>
<td class='text-right'>الرقم الضريبي </td>
</tr>
<tr>
</tr>
{% if(supplier_address_doc) %}
{% if supplier_address_doc.address_title %}
<tr>
<td> Street Name </td>
<td>{{ supplier_address_doc.address_title }}</td>
<td class='text-right'> اسم الشارع </td>
</tr>
{% endif %}
{% if supplier_address_doc.address_line1 %}
<tr>
<td>Address </td>
<td>{{ supplier_address_doc.address_line1 }}</td>
<td class='text-right'>العنوان </td>
</tr>
{% endif %}
{% if supplier_address_doc.city %}
<tr>
<td>City </td>
<td>{{ supplier_address_doc.city}}</td>
<td class='text-right'>المدينة </td>
</tr>
{% endif %}
{% if supplier_address_doc.country %}
<tr>
<td>Country </td>
<td>{{ supplier_address_doc.country }}</td>
<td class='text-right'>الدولة </td>
</tr>
{% endif %}
{% if supplier_address_doc.pincode %}
<tr>
<td>Postal Code </td>
<td>{{ supplier_address_doc.pincode }}</td>
<td class='text-right'>الرمز البريدي </td>
</tr>
{% endif %}
{% endif %}
{% if(customer_shipping_address) %}
<!--tr>
<td>Shipping Address</td>
<td>{{ customer_shipping_address.address_line1}}</td>
<td class='text-right'>عنوان الشحن</td>
</tr-->
{% endif %}
{% if(doc.po_no) %}
<tr>
<td>Delivery note</td>
<td>{{ doc.items[0].delivery_note }}</td>
<td class='text-right'>مذكرة تسليم</td>
</tr>
{% endif %}
<tr>
<td>Payment Due Date </td>
<td> {{ doc.due_date}}</td>
<td class='text-right'>تاريخ استحقاق الدفع</td>
</tr>
<!--tr>
<td>Other Buyer ID </td>
<td> {{ doc.contact_person }}</td>
<td class='text-right'> معرف اخر</td>
</tr-->
</tbody>
</table>
</div>
</div>
<!--Dynamic Colspan for total row columns-->
{% set col = namespace(one = 2, two = 1) %}
{% set length = doc.taxes | length %}
{% set length = length / 2 | round %}
{% set col.one = col.one + length %}
{% set col.two = col.two + length %}
{%- if(doc.taxes | length % 2 > 0 ) -%}
{% set col.two = col.two + 1 %}
{% endif %}
<!-- Items -->
{% set total = namespace(amount = 0) %}<table class="ksa-invoice-table">
<thead>
<tr>
<th class='text-center'>Goods <br />المنتج</th>
<th class='text-center'>Unit price <br />سعر الوحدة</th>
<th class='text-center'>Quantity <br />الكمية</th>
<th class='text-center'>UOM<br />وحدة القياس</th>
<th class='text-center'>Texable Amount <br />المبلغ الخاضع للضريبة</th>
<th class='text-center'>Discount <br /> خصومات </th>
<th class='text-center'>Tax Rate <br />نسبة الضريبة</th>
<th class='text-center'>Tax Amount <br />مبلغ الضريبة</th>
<th class='text-center'>Total <br />المجموع</th>
</tr>
</thead>
<tbody>
{%- for item in doc.items -%}
{% set total.amount = item.amount %}
<tr>
<td class='text-center'>{{ item.description }}</td>
<td class='text-center'>{{ item.get_formatted("rate") }}</td>
<td class='text-center'>{{ item.qty }}</td>
<td class='text-center'>{{ item.uom }}</td>
<td class='text-center'>{{ item.get_formatted("amount") }}</td>
<td class='text-center'>{{ item.discount_amount }}</td>
{% for row in doc.taxes %}
{% set data_object = string_to_json(row.item_wise_tax_detail) %}
<td class='text-center'>
<div class="">
{%- if(data_object[item.item_code][0])-%}
<span>{{ frappe.format(data_object[item.item_code][0] , {'fieldtype': 'Percent'}) }}</span>
{%- endif -%}
<!--<span>-->
<!--{%- if(data_object[item.item_code][1])-%}-->
<!-- {{ frappe.format(data_object[item.item_code][1], {'fieldtype': 'Currency'}) }}</span>-->
<!-- {% set total.amount = total.amount + data_object[item.item_code][1] %}-->
<!--{%- endif -%}-->
</div>
</td>
<td class='text-center'>
{%- if(data_object[item.item_code][1])-%}
{{ frappe.format(data_object[item.item_code][1],
{'fieldtype': 'Currency'}) }}
{%- endif -%}
</td>
{% endfor %}
<td>{{ frappe.format(total.amount, {'fieldtype': 'Currency'}) }}</td>
</tr>
{%- endfor -%}
</tbody>
</table>
<table class="ksa-invoice-table two-columns">
<thead>
<tr>
<th class='text-center'>الإجمالي (باستثناء ضريبة القيمة المضافة) <br />Total (Exluding VAT) </th>
<th class='text-center'> مجموع ضريبة القيمة المضافة <br /> Total VAT </th>
<th class='text-center'>إجمالي المبلغ المستحق <br />Total Amount Due</th>
</tr>
</thead>
<tbody>
<tr>
<td class='text-center'>{{ doc.get_formatted("total") }}</td>
<td class='text-center' style='direction: ltr;'>{{ doc.get_formatted("total_taxes_and_charges") }}</td>
<td class='text-center'>{{ doc.get_formatted("grand_total") }}</td>
</tr>
{% set ex_rate = frappe.get_list('Currency Exchange', ['exchange_rate'], {'from_currency':'SAR', 'to_currency':'USD'})[0] %}
{% if ex_rate %}
{% set exchange_rate = ex_rate.exchange_rate %}
<tr>
<td class='text-center'> {{"USD {:,.2f}".format(doc.total / exchange_rate ) }}</td>
<td class='text-center'> {{"USD {:,.2f}".format(doc.total_taxes_and_charges / exchange_rate ) }}</td>
<td class='text-center'> {{"USD {:,.2f}".format(doc.grand_total / exchange_rate ) }}</td>
</tr>
{% endif %}
</tbody>
</table>
<div id="footer-html" class="has-variables">
{% if footer and not no_letterhead %}
<div id="htmlfooter">
{{ footer }}
</div>
{% endif %}
</div>
Thank you.