{%- macro format_float(value) -%}
{{ "%.2f" % value|abs }}
{%- endmacro -%}
{%- macro render_address(address) %}
{{ address.address_line1 }}
{{ address.pincode }}
{{ address.city }}
{%- if address.state_code %}
{{ address.state_code }}
{%- endif %}
{{ address.country_code }}
{%- endmacro %}
{%- macro render_discount_or_margin(item) -%}
{%- if item.discount_percentage > 0.0 or item.margin_type %}
{%- if item.discount_percentage > 0.0 %}
SC
{{ format_float(item.discount_percentage) }}
{%- endif %}
{%- if item.margin_rate_or_amount > 0.0 -%}
MG
{%- if item.margin_type == "Percentage" -%}
{{ format_float(item.margin_rate_or_amount) }}
{%- elif item.margin_type == "Amount" -%}
{{ format_float(item.margin_rate_or_amount) }}
{%- endif -%}
{%- endif %}
{%- endif -%}
{%- endmacro -%}
{{ doc.company_address_data.country_code }}
{{ doc.company_fiscal_code or doc.company_tax_id | replace("IT","") }}
{{ doc.progressive_number }}
{{ doc.transmission_format_code }}
{{ doc.customer_data.recipient_code }}
{% if doc.company_data.phone or doc.company_data.email -%}
{% if doc.company_data.phone -%}{{ doc.company_data.phone }}{%- endif %}
{% if doc.company_data.email -%}{{ doc.company_data.email }}{%- endif %}
{% endif -%}
{{ doc.company_address_data.country_code }}
{{ doc.company_tax_id | replace("IT","") }}
{%- if doc.company_fiscal_code %}
{{ doc.company_fiscal_code }}
{%- endif %}
{{ doc.company }}
{{ doc.company_fiscal_regime.split("-")[0] }}
{{ render_address(doc.company_address_data) }}
{%- if doc.company_data.registration_number %}
{{ doc.company_data.registrar_office_province }}
{{ doc.company_data.registration_number }}
{%- if doc.company_data.share_capital_amount %}
{{ format_float(doc.company_data.share_capital_amount) }}
{%- endif %}
{%- if doc.company_data.no_of_members %}
{{ doc.company_data.no_of_members.split("-")[0] }}
{%- endif %}
{%- if doc.company_data.liquidation_state %}
{{ doc.company_data.liquidation_state.split("-")[0] }}
{%- endif %}
{%- endif %}
{%- if doc.customer_data.customer_type == "Individual" %}
{{ doc.customer_data.fiscal_code }}
{{ doc.customer_data.first_name }}
{{ doc.customer_data.last_name }}
{%- else %}
{{ doc.customer_address_data.country_code }}
{{ doc.tax_id | replace("IT","") }}
{%- if doc.customer_data.fiscal_code %}
{{ doc.customer_data.fiscal_code }}
{%- endif %}
{{ doc.customer_name }}
{%- endif %}
{%- if doc.customer_address_data %}
{{ render_address(doc.customer_address_data) }}
{%- endif %}
{{ doc.type_of_document }}
EUR
{{ doc.posting_date }}
{{ doc.unamended_name }}
{%- if doc.stamp_duty %}
SI
{{ format_float(doc.stamp_duty) }}
{%- endif %}
{%- if doc.discount_amount %}
{%- if doc.discount_amount > 0.0 %}
SC
{%- else %}
MG
{%- endif %}
{%- if doc.additional_discount_percentage > 0.0 %}
{{ format_float(doc.additional_discount_percentage) }}
{%- endif %}
{{ format_float(doc.discount_amount) }}
{%- endif %}
{{ format_float(doc.rounded_total or doc.grand_total) }}
VENDITA
{%- for po_no, po_date in doc.customer_po_data.items() %}
{{ po_no }}
{{ po_date }}
{%- endfor %}
{%- if doc.is_return and doc.return_against_unamended %}
{{ doc.return_against_unamended }}
{%- endif %}
{%- for row in doc.e_invoice_items %}
{%- if row.delivery_note %}
{{ row.delivery_note }}
{{ frappe.db.get_value('Delivery Note', row.delivery_note, 'posting_date') }}
{{ row.idx }}
{%- endif %}
{%- endfor %}
{%- if doc.shipping_address_data %}
{{ render_address(doc.shipping_address_data) }}
{%- endif %}
{%- for item in doc.e_invoice_items %}
{{ item.idx }}
CODICE
{{ item.item_code }}
{{ html2text(item.description or '') or item.item_name }}
{{ format_float(item.qty) }}
{{ item.stock_uom }}
{{ format_float(item.price_list_rate or item.rate) }}
{{ render_discount_or_margin(item) }}
{{ format_float(item.amount) }}
{{ format_float(item.tax_rate) }}
{%- if item.tax_exemption_reason %}
{{ item.tax_exemption_reason.split("-")[0] }}
{%- endif %}
{%- endfor %}
{%- for tax, data in doc.tax_data.items() %}
{{ format_float(tax|flt) }}
{%- if data.tax_exemption_reason %}
{{ data.tax_exemption_reason.split("-")[0] }}
{%- endif %}
{{ format_float(data.taxable_amount) }}
{{ format_float(data.tax_amount) }}
{{ doc.vat_collectability.split("-")[0] }}
{%- if data.tax_exemption_law %}
{{ data.tax_exemption_law }}
{%- endif %}
{%- endfor %}
{%- if doc.payment_schedule %}
{%- if payment_schedule|length > 1 %}
TP01
{%- else %}
TP02
{%- endif %}
{%- for payment_term in doc.payment_schedule %}
{{ payment_term.mode_of_payment_code.split("-")[0] }}
{{ payment_term.due_date }}
{{ format_float(payment_term.payment_amount) }}
{%- if payment_term.bank_account_name %}
{{ payment_term.bank_account_name }}
{%- endif %}
{%- if payment_term.bank_account_iban %}
{{ payment_term.bank_account_iban }}
{{ payment_term.bank_account_iban[5:10] }}
{{ payment_term.bank_account_iban[10:15] }}
{%- endif %}
{%- if payment_term.bank_account_swift_number %}
{{ payment_term.bank_account_swift_number }}
{%- endif %}
{%- endfor %}
{%- endif %}