<!DOCTYPE html>
<html lang="de">
<head>
<meta charset="UTF-8">
<style>
@page {
    size: A4 landscape;
    margin: 1.2cm 0.8cm 1.5cm 0.8cm;
    @bottom-right {
        content: counter(page) " / " counter(pages);
        font-family: 'DejaVu Sans', 'Calibri', sans-serif;
        font-size: 9pt;
        color: #555;
    }
}
body {
    font-family: 'DejaVu Sans', 'Calibri', sans-serif;
    font-size: 10pt;
    margin: 0;
    padding: 0;
    color: #000;
}
.logo-title {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 4px;
}
.logo-title img {
    max-width: 120px;
    max-height: 60px;
}
.logo-title .firmen-name {
    font-size: 16pt;
    font-weight: bold;
    color: #2F5496;
}
.logo-title .aufmass-titel {
    font-size: 16pt;
    font-weight: bold;
    color: #2F5496;
    text-align: right;
}
table.header-grid {
    width: 100%;
    border-collapse: collapse;
    font-size: 9pt;
    margin-bottom: 6px;
}
table.header-grid td {
    padding: 3px 5px;
    border: 0.5pt solid #999;
    vertical-align: middle;
}
table.header-grid td.label {
    background: #f2f2f2;
    font-weight: bold;
    width: 1%;
    white-space: nowrap;
}
table.header-grid td.value {
    font-weight: normal;
    padding-left: 6px;
}
table.header-grid td.spacer {
    border: none;
    width: 1%;
}
table.data-table {
    width: 100%;
    border-collapse: collapse;
    font-size: 8.5pt;
    margin-bottom: 10px;
}
table.data-table th {
    background: #2F5496;
    color: #fff;
    font-weight: bold;
    padding: 4px 3px;
    border: 0.5pt solid #2F5496;
    text-align: center;
    font-size: 8pt;
}
table.data-table td {
    padding: 2px 3px;
    border: 0.5pt solid #ccc;
    vertical-align: top;
}
table.data-table tr.trenner td {
    background: #f5f5f5;
    height: 10px;
    border-top: 1pt dashed #ccc;
    border-bottom: 1pt dashed #ccc;
}
table.data-table tr.summe td {
    font-weight: bold;
    border-top: 1pt solid #999;
}
.summary-section {
    margin-top: 14px;
    page-break-inside: avoid;
}
.summary-title {
    font-size: 11pt;
    font-weight: bold;
    color: #2F5496;
    background: #D6E4F0;
    padding: 4px 8px;
    border: 0.5pt solid #999;
    text-align: center;
}
table.summary-table {
    width: 100%;
    border-collapse: collapse;
    font-size: 9pt;
}
table.summary-table th {
    background: #2F5496;
    color: #fff;
    font-weight: bold;
    padding: 3px 5px;
    border: 0.5pt solid #2F5496;
    text-align: center;
    font-size: 8.5pt;
}
table.summary-table td {
    padding: 2px 5px;
    border: 0.5pt solid #ccc;
}
table.summary-table tr.summe td {
    font-weight: bold;
    border-top: 1pt solid #999;
}
</style>
</head>
<body>

<div class="logo-title">
    <div>
    {% if company and company.logo %}
        <img src="{{ company.logo }}" alt="Logo">
    {% else %}
        <span class="firmen-name">{{ company.name if company else 'Aufmaß' }}</span>
    {% endif %}
    </div>
    <div class="aufmass-titel">Aufmaß</div>
</div>

<table class="header-grid">
    <tr>
        <td class="label">Vertrag:</td>
        <td class="value">{{ project.vertrag or '' }}</td>
        <td class="spacer"></td>
        <td class="label">LV-Name:</td>
        <td class="value">{{ project.lv_name or '' }}</td>
        <td class="spacer"></td>
        <td class="label">Typ:</td>
        <td class="value">{{ aufmass.typ if aufmass else '' }}</td>
        <td class="spacer"></td>
        <td class="label">Aufmaß-Datum:</td>
        <td class="value" colspan="3">{{ project.datum.strftime('%d.%m.%Y') if project.datum else '' }}</td>
    </tr>
</table>

<table class="header-grid">
    <tr>
        <td class="label" style="width:5%">Projekt:</td>
        <td class="value" colspan="3" style="font-weight:bold">{{ project.bezeichnung or '' }}</td>
        <td class="spacer" style="width:0.5%"></td>
        <td class="label" style="width:6%">Baustelle:</td>
        <td class="value" colspan="3">{{ project.baustelle or '' }}</td>
        <td class="spacer" style="width:0.5%"></td>
        <td class="label" style="width:8%">Bauabschnitt:</td>
        <td class="value" colspan="2">{{ project.bauabschnitt or '' }}</td>
    </tr>
</table>

<table class="header-grid">
    <tr>
        <td class="label">SM-Nr.:</td>
        <td class="value">{{ project.sm_nr or '' }}</td>
        <td class="spacer"></td>
        <td class="label">Abruf-Nr.:</td>
        <td class="value">{{ project.abruf_nr or '' }}</td>
        <td class="spacer"></td>
        <td class="label">Startdatum:</td>
        <td class="value">{{ project.datum_start.strftime('%d.%m.%Y') if project.datum_start else '' }}</td>
        <td class="spacer"></td>
        <td class="label">Enddatum:</td>
        <td class="value" colspan="3">{{ project.datum_ende.strftime('%d.%m.%Y') if project.datum_ende else '' }}</td>
    </tr>
</table>

<table class="header-grid">
    <tr>
        <td class="label" style="width:10%">Ansprechpartner:</td>
        <td class="value" colspan="3">{{ (project.ansprechpartner_vorname or '') + ' ' + (project.ansprechpartner_nachname or '') }}</td>
        <td class="spacer" style="width:0.5%"></td>
        <td class="value" colspan="2" style="text-align:left">{{ project.ansprechpartner_tel or '' }}</td>
        <td class="spacer" style="width:0.5%"></td>
        <td class="label" style="width:4%">Email:</td>
        <td class="value" colspan="4">{{ project.ansprechpartner_email or '' }}</td>
    </tr>
</table>

<table class="data-table">
    <thead>
        <tr>
            <th style="width:7%">Abschnitt</th>
            <th style="width:8%">Pos-Nr</th>
            <th style="width:5%">Faktor</th>
            <th style="width:6%">Länge</th>
            <th style="width:6%">Breite</th>
            <th style="width:6%">Tiefe</th>
            <th style="width:7%">Menge</th>
            <th style="width:4%">EH</th>
            <th style="width:18%">Kurztext</th>
            <th style="width:12%">Bemerkung</th>
            <th style="width:7%">Menge</th>
            <th style="width:7%">EP (€)</th>
            <th style="width:7%">GP (€)</th>
        </tr>
    </thead>
    <tbody>
        {% set ns = namespace(pos_counter=0) %}
        {% for pos in positionen %}
        {% set ist_trenner = not pos.pos_nr and (pos.faktor == 0 or pos.faktor is none) and (pos.laenge == 0 or pos.laenge is none) and (pos.breite == 0 or pos.breite is none) and (pos.tiefe == 0 or pos.tiefe is none) and (pos.menge == 0 or pos.menge is none) and (pos.einzelpreis == 0 or pos.einzelpreis is none) and (pos.gesamtpreis == 0 or pos.gesamtpreis is none) %}
        {% if ist_trenner %}
        <tr class="trenner">
            <td colspan="13">&nbsp;</td>
        </tr>
        {% else %}
        {% set ns.pos_counter = ns.pos_counter + 1 %}
        {% set menge = pos.menge if pos.menge else none %}
        {% set menge_hinten = pos.menge_hinten if pos.menge_hinten else none %}
        {% if pos.einheit in ('ST', 'LE', 'STD', 'h', 'Psch') %}
            {% set menge = pos.faktor * 1 if pos.faktor else none %}
        {% endif %}
        <tr>
            <td>{{ pos.abschnitt or '' }}</td>
            <td>{{ pos.pos_nr or '' }}</td>
            <td style="text-align:right">{{ '%.2f'|format(pos.faktor) if pos.faktor else '' }}</td>
            <td style="text-align:right">{{ '%.2f'|format(pos.laenge) if pos.laenge else '' }}</td>
            <td style="text-align:right">{{ '%.2f'|format(pos.breite) if pos.breite else '' }}</td>
            <td style="text-align:right">{{ '%.2f'|format(pos.tiefe) if pos.tiefe else '' }}</td>
            <td style="text-align:right">{{ '%.2f'|format(menge) if menge else '' }}</td>
            <td>{{ pos.einheit or '' }}</td>
            <td>{{ pos.kurztext or '' }}</td>
            <td>{{ pos.bemerkung or '' }}</td>
            <td style="text-align:right">{{ '%.2f'|format(menge_hinten) if menge_hinten else '' }}</td>
            <td style="text-align:right">{{ '%.2f'|format(pos.einzelpreis) if pos.einzelpreis else '' }}</td>
            <td style="text-align:right">{{ '%.2f'|format(pos.gesamtpreis) if pos.gesamtpreis else '' }}</td>
        </tr>
        {% endif %}
        {% endfor %}

        {% if positionen %}
        {% set gesamt = positionen|selectattr('gesamtpreis')|sum(attribute='gesamtpreis') %}
        <tr class="summe">
            <td colspan="11" style="text-align:right">Summe:</td>
            <td></td>
            <td style="text-align:right">{{ '%.2f'|format(gesamt) }}</td>
        </tr>
        {% endif %}
    </tbody>
</table>

{% if ns.pos_counter > 0 %}
{% set groups = {} %}
{% for pos in positionen %}
{% set ist_trenner = not pos.pos_nr and (pos.faktor == 0 or pos.faktor is none) and (pos.laenge == 0 or pos.laenge is none) and (pos.breite == 0 or pos.breite is none) and (pos.tiefe == 0 or pos.tiefe is none) and (pos.menge == 0 or pos.menge is none) and (pos.einzelpreis == 0 or pos.einzelpreis is none) and (pos.gesamtpreis == 0 or pos.gesamtpreis is none) %}
{% if not ist_trenner and pos.pos_nr %}
{% if pos.pos_nr not in groups %}
{% set _ = groups.update({pos.pos_nr: {'kurztext': pos.kurztext or '', 'menge': 0.0, 'ep': 0.0, 'gp': 0.0}}) %}
{% endif %}
{% set _ = groups[pos.pos_nr].update({'menge': groups[pos.pos_nr]['menge'] + (pos.menge_hinten if pos.menge_hinten else (pos.menge or 0))}) %}
{% set _ = groups[pos.pos_nr].update({'ep': pos.einzelpreis or 0}) %}
{% set _ = groups[pos.pos_nr].update({'gp': groups[pos.pos_nr]['gp'] + (pos.gesamtpreis or 0)}) %}
{% endif %}
{% endfor %}

<div class="summary-section">
    <div class="summary-title">Mengen- und Positions-Zusammenfassung</div>
    <table class="summary-table">
        <thead>
            <tr>
                <th style="width:12%">Pos-Nr</th>
                <th>Kurztext</th>
                <th style="width:12%">Menge</th>
                <th style="width:12%">EP (€)</th>
                <th style="width:12%">GP (€)</th>
            </tr>
        </thead>
        <tbody>
            {% for key, g in groups.items() %}
            <tr>
                <td>{{ key }}</td>
                <td>{{ g.kurztext }}</td>
                <td style="text-align:right">{{ '%.2f'|format(g.menge) }}</td>
                <td style="text-align:right">{{ '%.2f'|format(g.ep) }}</td>
                <td style="text-align:right">{{ '%.2f'|format(g.gp) }}</td>
            </tr>
            {% endfor %}
            {% set total_gp = groups.values()|sum(attribute='gp') %}
            <tr class="summe">
                <td colspan="3"></td>
                <td style="text-align:right">Summe:</td>
                <td style="text-align:right">{{ '%.2f'|format(total_gp) }}</td>
            </tr>
        </tbody>
    </table>
</div>
{% endif %}

</body>
</html>