{% set advance_payments_total = 0 %}
{% if PAYMENTS %}
  {% for p in PAYMENTS %}
    {% if p.is_advance_payment %}
      {% set advance_payments_total = advance_payments_total + p.amount %}
    {% endif %}
  {% endfor %}
{% endif %}

{% set to_be_paid = total_incl_vat - advance_payments_total %}

<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
  <title>Faktúra {{ PROFILE.COMPANY.name }} {{ number }}</title>
  <style>
    {% if PDF_EXPORT %}
      * { font-family: Dejavu Sans !important; line-height: 0.95em; font-size: 10pt; }
    {% else %}
      * { font-family: Verdana !important; font-size: 10pt; }
    {% endif %} 
    .fbigger { font-size: 12pt; }
    table { width: 100%; border-collapse: collapse; }
    table td { vertical-align: top; }
    .dmain { border: 0.2mm solid #444444; margin-top: 0.5em; }
    .dcolumn { width: 50%; float: left; }
    .content { padding: 4pt; }
    .odberatel { border: 0.5mm solid #444444; }
    .dodod_info { color: #777; text-transform: uppercase; padding-bottom: 2mm; text-align: left; }
    .dodod_info span { border-bottom: 0.2mm solid #777; font-size: 8pt; font-style: italic; }
    .faktura_info { padding: 16pt 2pt 6pt 2pt; text-align: left; }
    table.polozky td { padding: 0.8pt; font-size: 9pt; vertical-align: middle; }
    table.polozky tr.header td { font-weight: bold; border-bottom: 0px solid #777; }
    table.polozky tr.sep td { width: 100%; height: 4pt; }
    .sumar { width: 100%; float: right; }
    .sumar table td { padding: 0.8pt; vertical-align: middle; }
    .sumar table tr.sep td { width: 100%; height: 10pt; }
    .sumar table tr.suma td { border-top: 0.2mm solid #777; padding-top: 1mm; }
    .text-right { text-align: right; }
  </style>
</head>
<body>
  <!-- TOP -->
  <div>Faktúra - daňový doklad č. <b>{{ number }}</b></div>
  {% if to_be_paid <= 0 %} <div style="color:red">Neuhrádzajte, faktúra je už uhradená.</div> {% endif %}
  <br/>
  <div class="dmain">
    <!-- DODAVATEL -->
    <div class="dcolumn">
      <div class="content">
        <table>
          <tr>
            <td style="padding-top:0.5em">
              <img src="{{ config.uploadUrl }}/{{ PROFILE.COMPANY.logo }}" style="max-width:80px;max-height:80px;">
            </td>
            <td style="width:95%;padding-left:1em;padding-top:0.5em">
              {{ PROFILE.headline }}
            </td>
          </tr>
        </table>
        <br/>
        <div>
          <b>DODÁVATEĽ</b><br/>
          <br/>
          <b>{{ PROFILE.COMPANY.name }}</b><br/>
          {{ PROFILE.COMPANY.street_1 }}<br/>
          {% if PROFILE.COMPANY.street_2 %} {{ PROFILE.COMPANY.street_2 }}<br/> {% endif %}
          {{ PROFILE.COMPANY.zip }} {{ PROFILE.COMPANY.city }}<br/>
          {% if PROFILE.COMPANY.country %} {{ PROFILE.COMPANY.country }}<br/> {% endif %}
          <br/>
          {% if PROFILE.COMPANY.registration_id %} IČO: {{ PROFILE.COMPANY.registration_id }}<br/> {% endif %}
          {% if PROFILE.COMPANY.tax_id %} DIČ: {{ PROFILE.COMPANY.tax_id }}<br/> {% endif %}
          {% if PROFILE.COMPANY.vat_id %} IČDPH: {{ PROFILE.COMPANY.vat_id }}<br/> {% endif %}
          <br/>
          {{ PROFILE.COMPANY.business_register }}<br/>
        </div>
      </div>
    </div>
    <!-- ODBERATEL -->
    <div class="dcolumn">
      <div class="content odberatel">
        <b>ODBERATEĽ</b><br/>
        <br/>
        <b>{{ CUSTOMER.name }}</b><br/>
        {{ CUSTOMER.street_line_1 }}<br/>
        {% if CUSTOMER.street_line_2 %} {{ CUSTOMER.street_line_2 }}<br/> {% endif %}
        {% if CUSTOMER.region %} {{ CUSTOMER.region }}<br/> {% endif %}
        {{ CUSTOMER.postal_code }} {{ CUSTOMER.city }}<br/>
        {% if CUSTOMER.country %} {{ CUSTOMER.country }}<br/> {% endif %}
        <br/>
        {% if CUSTOMER.customer_id %} IČO: {{ CUSTOMER.customer_id }}<br/> {% endif %}
        {% if CUSTOMER.tax_id %} DIČ: {{ CUSTOMER.tax_id }}<br/> {% endif %}
        {% if CUSTOMER.vat_id %} IČDPH: {{ CUSTOMER.vat_id }}<br/> {% endif %}
      </div>
      <div class="content">
        <br/>
        Variabilný symbol: <b>{{ vs }}</b><br/>
        {% if ss %} Špecifický symbol: <b>{{ ss }}</b><br/>{% endif %}
        {% if cs %} Konštantný symbol: <b>{{ cs }}</b><br/>{% endif %}
        Forma úhrady: <b>{{ PAYMENT_METHOD.name }}</b><br/>
        <br/>
        Dátum vystavenia: {{ hubleto.locale().formatDateShort(date_issue ?? '') }}<br/>
        Dátum dodania: {{ hubleto.locale().formatDateShort(date_delivery ?? '') }}<br/>
      </div>
    </div>
    <div style="clear:both;"></div>
  </div>
  <div class="dmain">
    <div class="dcolumn" style="width:60%">
      <div class="content" style="background:#EEEEEE;text-align:center">
        IBAN <br>
        <b style="font-size:1.4em;">{{ PROFILE.iban }}</b>
        <br>
        <br> BIC/SWIFT <br>
        <b style="font-size:1.4em;">{{ PROFILE.swift }}</b>
        <br>
      </div>
    </div>
    <div class="dcolumn" style="width:40%">
      <div class="content" style="background:#EEEEEE;text-align:center">
        Suma k úhrade<br>
        <b style="font-size:1.4em;text-wrap:nowrap">{{ hubleto.locale().formatCurrency(total_incl_vat - advance_payments_total, CURRENCY.symbol) }}</b><br/>
        <br/>
        Dátum splatnosti<br>
        <b style="font-size:1.4em;">{{ hubleto.locale().formatDateShort(date_due ?? '') }}</b>
      </div>
    </div>
    <div style="clear:both;"></div>
  </div>

  {% if description_before %}
    <div style="text-align:left;margin:1em 0em;color:blue">{{ description_before }}</div>
  {% endif %}

  <!-- POLOZKY -->
  <div class="dmain">
    <div class="content">
      <table class="polozky">
        <thead>
          <tr class="header" style="background:#EEEEEE">
            <td style="width:100%;padding:5pt 5pt 5pt 5pt">
              <table style="width:100%">
                <tbody>
                  <tr>
                    <td style="width:50%">Položka</td>
                    <td style="width:50%;text-align:right">Jednotková cena × Počet = Cena</td>
                  </tr>
                </tbody>
              </table>
            </td>
          </tr>
          <tr>
            <td>&nbsp;</td>
          </tr>
        </thead>
        <tbody>
          {% for i in ITEMS %}
            <tr>
              <td style="width:100%;">
                <table style="width:100%">
                  <tbody>
                    <tr>
                      <td style="width:50%;color:blue">
                        <i>{{ i.item }}</i>
                        {% if i.discount > 0 %}
                          <div style="color:orange">
                            Započítaná zľava {{ i.discount|round }} %
                          </div>
                        {% endif %}
                      </td>
                      <td style="width:50%;text-align:right">
                        <span>{{ hubleto.locale().formatCurrency(i.unit_price * (100 - i.discount)/100, CURRENCY.symbol) }}</span>
                        ×
                        {{ hubleto.locale().formatNumber(i.amount, 4) }}
                        = <b><span>{{ hubleto.locale().formatCurrency(i.price_excl_vat, CURRENCY.symbol) }}</span> bez DPH</b>
                        <div style="color:#888888">
                          DPH {{ i.vat|number_format(2, '.', '') }} % = <span>{{ hubleto.locale().formatCurrency(i.price_vat, CURRENCY.symbol) }}</span>
                        </div>
                      </td>
                    </tr>
                  </tbody>
                </table>
              </td>
            </tr>
          {% endfor %}
        </tbody>
      </table>
    </div>
  </div>


  {% if description_after %}
    <div style="text-align:left;margin:1em 0em;white-space:pre-wrap;">{{ description_after }}</div>
  {% endif %}

  <br>
  <br>
  <table style="width:100%">
    <tbody>
      <tr>
        <td style="width:30%">
          <div style="padding-top:5mm;padding-left:5mm">
            {% if PROFILE.stamp_and_signature %}
              <img src="{{ config.uploadUrl }}/{{ PROFILE.stamp_and_signature }}" style="max-width:180px;max-height:180px;"><br/>
              <br/>
            {% endif %}
          </div>
        </td>
        <td style="width:3%">&nbsp;</td>
        <td style="width:46%">
          <!-- SUMAR -->
          <div style="padding-top:5mm;">
            <div class="content">
              <div class="sumar">
                <table>
                  <tbody>
                    <tr>
                      <td>Spolu bez DPH</td>
                      <td class="text-right" style="text-wrap:nowrap">{{ hubleto.locale().formatCurrency(total_excl_vat, CURRENCY.symbol) }}</td>
                    </tr>
                    <tr>
                      <td>DPH</td>
                      <td class="text-right" style="text-wrap:nowrap">{{ hubleto.locale().formatCurrency(total_incl_vat - total_excl_vat, CURRENCY.symbol) }}</td>
                    </tr>
                    <tr>
                      <td><b>Spolu s DPH</b></td>
                      <td class="text-right" style="text-wrap:nowrap"><b>{{ hubleto.locale().formatCurrency(total_incl_vat, CURRENCY.symbol) }}</b></td>
                    </tr>
                    <tr><td>&nbsp;</td></tr>
                    {% if PAYMENTS %}
                      {% for p in PAYMENTS %}
                        {% if p.is_advance_payment %}
                          <tr>
                            <td>Prijatá záloha dňa {{ hubleto.locale().formatDateShort(p.date_payment ?? '') }}</td>
                            <td class="text-right" style="text-wrap:nowrap">{{ hubleto.locale().formatCurrency(p.amount, CURRENCY.symbol) }}</td>
                          </tr>
                        {% endif %}
                      {% endfor %}
                    {% endif %}
                    <tr><td>&nbsp;</td></tr>
                    <tr>
                      <td><b style="font-size:1.2em">K úhrade</b></td>
                      <td class="text-right"><b style="font-size:1.2em;text-wrap:nowrap">{{ hubleto.locale().formatCurrency(to_be_paid, CURRENCY.symbol) }}</b></td>
                    </tr>
                  </tbody>
                </table>
              </div>
              <div style="clear:both;"></div>
            </div>
          </div>
        </td>
        <td style="width:3%">&nbsp;</td>
        <td style="width:20%;text-align:right">
          {% if PAY_BY_SQUARE_QR_CODE %}
            <div style="float:right;border:3px solid #6fa4d7;padding:2mm;border-radius:1mm;width:120px">
              <img
                src="{{ PAY_BY_SQUARE_QR_CODE }}" style="width:100%;"
              >
              <div style="padding:3px">
                <span style="color:#6fa4d7;font-size:8pt">PAY</span>
                <span style="color:#b2b4b9;font-size:8pt">by square</span>
              </div>
            </div>
          {% else %}
            <div style="color:red">{{ PAY_BY_SQUARE_QR_CODE_ERROR }}</div>
          {% endif %}
        </td>
      </tr>
    </tbody>
  </table>
  <br/>
  <br/>
  Vystavil {{ ISSUED_BY.first_name }} {{ ISSUED_BY.last_name }}.
  Vygenerované v ERP Hubleto.
  https://www.hubleto.eu<br/>
</body>