<!DOCTYPE html>
<html lang="<?php _trans('cldr'); ?>">
<head>
  <?php
    $anyItemHasDescription = array_reduce($items, function ($acc, $item) {
      return $acc || $item->item_description != '';
    }, false);

    $colspan = 3 + ($show_item_discounts ? 1 : 0) + ($anyItemHasDescription ? 1 : 0);
    $isPaid = $invoice->invoice_balance <= 0;
  ?>

  <meta charset="utf-8">
  <title><?php _trans('invoice'); ?></title>
  <link rel="stylesheet"
      href="<?php echo base_url(); ?>assets/<?php echo get_setting('system_theme', 'invoiceplane'); ?>/css/templates.css">
  <link rel="stylesheet" href="<?php echo base_url(); ?>assets/core/css/custom-pdf.css">
  <style>
/* general font size */
body {
  font-size: 12px;
  color: black;
}

/* color of Invoice Heading fixed */
.invoice-title {
  margin-left: 2.5mm;
  margin-top: 5mm;
  font-weight: bold;
  color: black;
}

/* left space due to DIN 5008 2.5mm */
main, footer {
  margin-left: 2.5mm;
}

/* reduces padding in item-table */
table.item-table td {
  padding: 5px 10px;
}

/* defines german DIN_5008_Form_B sichtbriefumschlag position */
#client {
  position: absolute;
  top: 56mm;
  left: 20mm;
  width: 90mm;
}

/* reduced font-size for items */
table.item-table {
  font-size: 11px !important;
}

/* sender in sichtfenster */
#sichtfenster-absender {
  font-size: 8px;
}

/** Pfalzmarken DIN 5008 Form B */
/* 105mm from top with offset */
#pm-1{
  position: absolute;
  top: 101.8mm;
  left: 5mm;
  width: 5mm;
}
/* 148.5mm from top with offset */
#pm-2{
  position: absolute;
  top: 145.3mm;
  left: 5mm;
  width: 7mm;
}
/* 210mm from top with offset */
#pm-3{
  position: absolute;
  top: 206.8mm;
  left: 5mm;
  width: 5mm;
}

.invoice-meta {
  margin-top: 1cm;
}

footer {
  margin-top: 1cm;
  color: black;
}

footer .notes {
  color: #333;
}
  </style>
</head>

<body>

<div id="pm-1"><hr /></div>
<div id="pm-2"><hr /></div>
<div id="pm-3"><hr /></div>

<div id="client">
  <div id="sichtfenster-absender">
    <?php if ($invoice->user_company !== ""): ?>
      <?php
        echo htmlsc($invoice->user_company) . ' - ' . htmlsc($invoice->user_address_1) . ' - ' . htmlsc($invoice->user_zip) . ' ' . htmlsc($invoice->user_city);
      ?>
    <?php else: ?>
      <?php
        echo htmlsc($invoice->user_name) . ' - ' . htmlsc($invoice->user_address_1) . ' - ' . htmlsc($invoice->user_zip) . ' ' . htmlsc($invoice->user_city);
      ?>
    <?php endif; ?>
    <hr />
  </div>
  <div>
    <b><?php _htmlsc(format_client($invoice)); ?></b>
  </div>
  <?php if ($invoice->client_vat_id) {
    echo '<div>' . trans('vat_id_short') . ': ' . $invoice->client_vat_id . '</div>';
  }
  if ($invoice->client_tax_code) {
    echo '<div>' . trans('tax_code_short') . ': ' . $invoice->client_tax_code . '</div>';
  }
  if ($invoice->client_address_1) {
    echo '<div>' . htmlsc($invoice->client_address_1) . '</div>';
  }
  if ($invoice->client_address_2) {
    echo '<div>' . htmlsc($invoice->client_address_2) . '</div>';
  }
  if ($invoice->client_city || $invoice->client_state || $invoice->client_zip) {
    echo '<div>';
    if ($invoice->client_zip) {
      echo htmlsc($invoice->client_zip) . ' ';
    }
    if ($invoice->client_city) {
      echo htmlsc($invoice->client_city) . ' ';
    }
    if ($invoice->client_state) {
      echo htmlsc($invoice->client_state);
    }
    echo '</div>';
  }
  if ($invoice->client_country) {
    echo '<div>' . get_country_name(trans('cldr'), $invoice->client_country) . '</div>';
  }

  echo '<br/>';

  if ($invoice->client_phone) {
    echo '<div>' . trans('phone_abbr') . ': ' . htmlsc($invoice->client_phone) . '</div>';
  } ?>
</div>

<header class="clearfix">

  <div id="logo">
    <?php echo invoice_logo_pdf(); ?>
  </div>

  <h1 class="invoice-title"><?php echo trans('invoice'); ?></h1>

  <!-- client is extracted due to absolute position -->

  <div id="company">
    <div><b>
      <?php if ($invoice->user_company !== "") : ?>
        <?php _htmlsc($invoice->user_company); ?>
      <?php else: ?>
        <?php _htmlsc($invoice->user_name); ?>
      <?php endif; ?>
    </b></div>
    <?php if ($invoice->user_vat_id) {
      echo '<div>' . trans('vat_id_short') . ': ' . $invoice->user_vat_id . '</div>';
    }
    if ($invoice->user_tax_code) {
      echo '<div>' . trans('tax_code_short') . ': ' . $invoice->user_tax_code . '</div>';
    }
    if ($invoice->user_address_1) {
      echo '<div>' . htmlsc($invoice->user_address_1) . '</div>';
    }
    if ($invoice->user_address_2) {
      echo '<div>' . htmlsc($invoice->user_address_2) . '</div>';
    }
    if ($invoice->user_city || $invoice->user_state || $invoice->user_zip) {
      echo '<div>';
      if ($invoice->user_zip) {
        echo htmlsc($invoice->user_zip) . ' ';
      }
      if ($invoice->user_city) {
        echo htmlsc($invoice->user_city) . ' ';
      }
      if ($invoice->user_state) {
        echo htmlsc($invoice->user_state);
      }
      echo '</div>';
    }
    if ($invoice->user_country) {
      echo '<div>' . get_country_name(trans('cldr'), $invoice->user_country) . '</div>';
    }

    echo '<br/>';

    if ($invoice->user_phone) {
      echo '<div>' . trans('phone_abbr') . ': ' . htmlsc($invoice->user_phone) . '</div>';
    }
    if ($invoice->user_fax) {
      echo '<div>' . trans('fax_abbr') . ': ' . htmlsc($invoice->user_fax) . '</div>';
    }
    ?>
  </div>

</header>

<main>

  <div class="invoice-details clearfix">
    <table>
      <tr>
        <td><?php echo trans('invoice_date') . ':'; ?></td>
        <td><?php echo date_from_mysql($invoice->invoice_date_created, true); ?></td>
      </tr>
      <tr>
        <td><?php echo trans('due_date') . ': '; ?></td>
        <td><?php echo date_from_mysql($invoice->invoice_date_due, true); ?></td>
      </tr>
      <tr>
        <td <?php echo ($isPaid ? 'class="text-green"' : ''); ?>><?php echo trans('amount_due') . ': '; ?></td>
        <td <?php echo ($isPaid ? 'class="text-green"' : ''); ?>><?php echo format_currency($invoice->invoice_balance); ?></td>
      </tr>
      <?php if ($payment_method): ?>
        <tr>
          <td><?php echo trans('payment_method') . ': '; ?></td>
          <td><?php _htmlsc($payment_method->payment_method_name); ?></td>
        </tr>
      <?php endif; ?>
    </table>
  </div>

  <p class="invoice-meta">
    Rechnungsnummer: <?php echo $invoice->invoice_number; ?><br />
    <?php if (isset($custom_fields['invoice']['Leistungszeitraum-Anfang']) && isset($custom_fields['invoice']['Leistungszeitraum-Ende'])): ?>
      Leistungszeitraum:
      <?php echo date_from_mysql($custom_fields['invoice']['Leistungszeitraum-Anfang'], true); ?>
      -
      <?php echo date_from_mysql($custom_fields['invoice']['Leistungszeitraum-Ende'], true); ?>
    <?php endif; ?>
  </p>

  <?php if (isset($custom_fields['invoice']['Beschreibung'])): ?>
    <p>
      <?php echo $custom_fields['invoice']['Beschreibung']; ?>
    </p>
  <?php endif; ?>

  <table class="item-table">
    <thead>
      <tr>
        <th class="item-name"><?php _trans('item'); ?></th>
        <?php if ($anyItemHasDescription) : ?>
          <th class="item-desc"><?php _trans('description'); ?></th>
        <?php endif; ?>
        <th class="item-amount text-right"><?php _trans('qty'); ?></th>
        <th class="item-price text-right"><?php _trans('price'); ?></th>
        <?php if ($show_item_discounts) : ?>
          <th class="item-discount text-right"><?php _trans('discount'); ?></th>
        <?php endif; ?>
        <th class="item-total text-right"><?php _trans('total'); ?></th>
      </tr>
    </thead>

    <tbody>
    <?php foreach ($items as $item) { ?>
      <tr>
        <td><?php _htmlsc($item->item_name); ?></td>
        <?php if ($anyItemHasDescription) : ?>
          <td><?php echo nl2br(htmlsc($item->item_description)); ?></td>
        <?php endif; ?>
        <td class="text-right">
          <?php echo format_amount($item->item_quantity); ?>
          <?php if ($item->item_product_unit) : ?>
            <br>
            <small><?php _htmlsc($item->item_product_unit); ?></small>
          <?php endif; ?>
        </td>
        <td class="text-right">
          <?php echo format_currency($item->item_price); ?>
        </td>
        <?php if ($show_item_discounts) : ?>
          <td class="text-right">
            <?php echo format_currency($item->item_discount); ?>
          </td>
        <?php endif; ?>
        <td class="text-right">
          <?php echo format_currency($item->item_total); ?>
        </td>
      </tr>
    <?php } ?>
    </tbody>

    <tbody class="invoice-sums">
    <tr>
      <td colspan="<?php echo $colspan; ?>" class="text-right">
        <?php _trans('subtotal'); ?>
      </td>
      <td class="text-right"><?php echo format_currency($invoice->invoice_item_subtotal); ?></td>
    </tr>

    <?php if ($invoice->invoice_item_tax_total > 0) { ?>
      <tr>
        <td colspan="<?php echo $colspan; ?>" class="text-right">
          <?php _trans('item_tax'); ?>
        </td>
        <td class="text-right">
          <?php echo format_currency($invoice->invoice_item_tax_total); ?>
        </td>
      </tr>
    <?php } ?>

    <?php foreach ($invoice_tax_rates as $invoice_tax_rate) : ?>
      <tr>
        <td colspan="<?php echo $colspan; ?>" class="text-right">
          <?php echo htmlsc($invoice_tax_rate->invoice_tax_rate_name) . ' (' . format_amount($invoice_tax_rate->invoice_tax_rate_percent) . '%)'; ?>
        </td>
        <td class="text-right">
          <?php echo format_currency($invoice_tax_rate->invoice_tax_rate_amount); ?>
        </td>
      </tr>
    <?php endforeach ?>

    <?php if ($invoice->invoice_discount_percent != '0.00') : ?>
      <tr>
        <td colspan="<?php echo $colspan; ?>" class="text-right">
          <?php _trans('discount'); ?>
        </td>
        <td class="text-right">
          <?php echo format_amount($invoice->invoice_discount_percent); ?>%
        </td>
      </tr>
    <?php endif; ?>
    <?php if ($invoice->invoice_discount_amount != '0.00') : ?>
      <tr>
        <td colspan="<?php echo $colspan; ?>" class="text-right">
          <?php _trans('discount'); ?>
        </td>
        <td class="text-right">
          <?php echo format_currency($invoice->invoice_discount_amount); ?>
        </td>
      </tr>
    <?php endif; ?>

    <tr>
      <td colspan="<?php echo $colspan; ?>" class="text-right">
        <b><?php _trans('total'); ?></b>
      </td>
      <td class="text-right">
        <b><?php echo format_currency($invoice->invoice_total); ?></b>
      </td>
    </tr>
    <?php if ($invoice->invoice_paid != '0.00') : ?>
      <tr>
        <td colspan="<?php echo $colspan; ?>" class="text-right">
          <?php _trans('paid'); ?>
        </td>
        <td class="text-right">
          <?php echo format_currency($invoice->invoice_paid); ?>
        </td>
      </tr>
    <?php endif; ?>
    <tr>
      <td colspan="<?php echo $colspan; ?>" class="text-right">
        <b><?php _trans('balance'); ?></b>
      </td>
      <td class="text-right <?php echo ($isPaid ? 'text-green' : ''); ?>">
        <b><?php echo format_currency($invoice->invoice_balance); ?></b>
      </td>
    </tr>
    </tbody>
  </table>
</main>

<?php if ($isPaid): ?>
  <watermarktext content="<?php _trans('paid'); ?>" alpha="0.3" />
<?php endif; ?>

<footer>
  <?php if ($invoice->invoice_terms) : ?>
    <div class="notes">
      <b><?php _trans('terms'); ?></b><br/>
      <?php echo nl2br(htmlsc($invoice->invoice_terms)); ?>
    </div>
  <?php endif; ?>

  <?php if (!$isPaid): ?>
    <p>
      Bitte überweisen Sie den Rechnungsbetrag innerhalb von <?php echo (-1 * $invoice->days_overdue); ?>  Tagen auf untenstehendes Konto:<br />
      Inhaber:
        <?php if ($invoice->user_company !== "") : ?>
          <?php echo $invoice->user_company; ?><br />
        <?php else: ?>
          <?php echo $invoice->user_name; ?><br />
        <?php endif; ?>
      IBAN: <?php echo $invoice->user_iban; ?><br />
      <?php echo invoice_qrcode($invoice->invoice_id); ?>
    </p>
  <?php else: ?>
    </p>Vielen Dank!<p>
  <?php endif; ?>

  <p>
    Mit freundlichen Grüßen<br />
    <br />
    <?php echo $invoice->user_name; ?>
  </p>
</footer>

</body>
</html>