<?php $GLOBALS['page'] = 1; ?>
<?php 

use Jackiedo\DotenvEditor\Facades\DotenvEditor;

$editor = DotenvEditor::load(storage_path('app/settings.env'));

?>
<html>
<head>

    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <style>
    	body {
			font-family:Verdana, Arial, Helvetica, sans-serif;
			font-size:12px;
		}
        @page {
            __margin: 180px 50px;
            margin:180px 0 100px 0px;
            header: page-header;
            footer: page-footer;
        }

        table {
            width:100%;
        }
        
        table td {vertical-align: top;}
        
        table.main td, table.main th {
            border: 1px solid gray;
        }
    
        #header {
            position: fixed;
            left: -50px; top: -180px; right: -50px; height: 180px;
        }
        
        #footer {
            position: fixed;
            left: -50px; bottom: -180px; right: -50px; height: 160px;
        }
.style3 {font-size: smaller}
.style5 {color: #990000}
    </style>
</head>
<body>

    <htmlpageheader name="page-header" id="header">
        <img src="{{public_path('images/pdf_header.' . $editor->getValue('IMAGE_PDF_HEADER'))}}" style="width:100%; height:auto" >
    </htmlpageheader>

    <htmlpagefooter name="page-footer" id="footer">
        <img src="{{public_path('images/pdf_footer.' . $editor->getValue('IMAGE_PDF_FOOTER'))}}" style="width:100%; height:auto" >
    </htmlpagefooter>

    <table style="width:100%; margin-top:180px; margin-left:50px; margin-right:50px;">
        <tr>
          <td colspan="2" style="width:50%; vertical-align: top; font-size:8px;">{{$editor->getValue('COMPANY_NAME')}} - {{$editor->getValue('COMPANY_STREETADDRESS')}} {{$editor->getValue('COMPANY_HOUSENUMBER')}} - {{$editor->getValue('COMPANY_POSTCODE')}} {{$editor->getValue('COMPANY_LOCATION')}} </td>
        </tr>
        <tr>
          <td style="width:50%; vertical-align: top">&nbsp;</td>
          <td>&nbsp;</td>
        </tr>
        <tr>
          <td style="width:50%; vertical-align: top"><table>
            
            <tr>
              <td width="120">{{$model->client}}</td>
            </tr>
            <tr>
              <td>{{$model->firstname}} {{$model->surname}}</td>
            </tr>
            <tr>
              <td>{{$model->streetadress}} {{$model->housenumber}}</td>
            </tr>
            <tr>
              <td>&nbsp;</td>
            </tr>
          </table></td>
          <td>&nbsp;</td>
        </tr>
        <tr>
          <td style="width:50%; vertical-align: top">{{$model->postcode}} {{$model->location}}</td>
          <td align="right"><table align="right">
            <tr>
              <td width="80">Rechnungs-Nr.:</td>
              <td width="20"></td>
              <td>RG-{{$model->invoice_number}}</td>
            </tr>
            <tr>
              <td>Kundennummer:</td>
              <td></td>
              <td>{{$model->customer_id}}</td>
            </tr>
            <tr>
              <td>Leistungssdatum:</td>
              <td></td>
              <td>{{(new \Carbon\Carbon($model->service_date))->format('d.m.Y')}}</td>
            </tr>
            <tr>
              <td>Rechnungsdatum:</td>
              <td></td>
              <td>{{(new \Carbon\Carbon($model->date))->format('d.m.Y')}}</td>
            </tr>
          </table>          </td>
        </tr>
		<tr>
             <td>&nbsp;</td>
            <td valign="bottom">&nbsp;</td>
        </tr>
        @if($model->descriptor == "Rechnung")
        <tr>
          <td colspan="2"><h2>Rechnungs-Nr. RG-{{$model->invoice_number}} </h2></td>
        </tr>
        @elseif($model->descriptor == "Stornorechnung")
		<tr>
          <td colspan="2"><h2>Stornorechnung</h2></td>
        </tr>
        @endif

        @if($model->header)
		<tr>
          <td colspan="2">&nbsp;</td>
        </tr>
        <tr>
          <td colspan="2">{!! nl2br($model->header)!!}</td>
        </tr>
		@endif
        <!--tr>
          <td colspan="2">&nbsp;</td>
        </tr>
        <tr>
          <td colspan="2">betrifft: {{$model->commission_number}}</td>
        </tr-->
        <tr>
          <td>&nbsp;</td>
          <td>&nbsp;</td>
        </tr>
    </table>


    <h4 style="border-spacing:0px; margin: 0 50px;"></h4>
    <table class="main" width="100%" style="border-spacing:0px; margin: 10px 50px; border-collapse: collapse; border: 1px solid gray;">
        <thead>
            <tr>
                <th align="left"><span class="style3">&nbsp;Pos.</span></th>
                <th align="right"><span class="style3">&nbsp;Menge&nbsp;</span></th>
                <th align="left"><span class="style3">&nbsp;Einheit&nbsp;</span></th>
                <th align="left"><span class="style3">&nbsp;Text</span></th>
                <th align="right"><span class="style3">Einzel &nbsp;</span></th>
                <th align="right"><span class="style3">Gesamt &nbsp;</span></th>
            </tr>
        </thead>
        <tbody>
        
            @php $order = 1; $zsum = 0; $fk = 0; $zsumvat = 0; $fkvat = 0; $pos = 0; $vat=[] @endphp

            @php 
                $main = 0;
                $workingTime = $model->_items->where('_meta._doctype', 'cba3def15e0e6c1151fe202c8b00c2a0c1951909')->sortBy('sort_order');
                @endphp
        
            @if($workingTime->count())
            <tr>
                <td colspan="6">&nbsp;{{++$main}}. Arbeitszeit<br></td>
            </tr>
        

            @foreach($workingTime AS $index=>$row)
            <tr>
                <td>&nbsp;{{$main}}.{{$order}}</td>
                <td align="right" style="font-size:smaller;">&nbsp;{{number_format(str_replace(',', '.', $row->amount), 2, ',', '')}}&nbsp;</td>
                <td style="font-size:smaller;">&nbsp;{{$row->unit}}&nbsp;</td>
                <td style="font-size:smaller;">@foreach(explode("\n", $row->descriptor) as $line)&nbsp;{{ $line }}<br>@endforeach</td>
                <td align="right" style="font-size:smaller;">{{number_format((float) str_replace(',', '.', $row->netprice), 2, ',', '')}}€&nbsp;</td>
                <td align="right" style="font-size:smaller;">{{number_format($pos = round((float) str_replace(',', '.', $row->netprice) * (float) str_replace(',', '.', $row->amount),2), 2, ',', '')}}€&nbsp;</td>
            </tr>
            @php 
                $order++; 
                $zsum = $zsum + $pos; 
                $vat[$row->vat][] = $pos; 
                @endphp
            @endforeach
            
            @endif



          @php

                $services = $model->_items->where('_meta._doctype', 'b76663c559923d5b46f232451fea273f6887bb28')->sortBy('sort_order');
                @endphp

            @if($services->count())
            <tr>
                <td colspan="6">&nbsp;{{++$main}}. Dienstleistungen<br></td>
            </tr>

            @php $order = 1; @endphp
            @foreach($services AS $index=>$row)
            <tr>
                <td>&nbsp;{{$main}}.{{$order}}</td>
                <td align="right" style="font-size:smaller;">&nbsp;{{number_format(str_replace(',', '.', $row->amount), 2, ',', '')}}&nbsp;</td>
                <td style="font-size:smaller;">&nbsp;{{$row->unit}}&nbsp;</td>
                <td style="font-size:smaller;">@foreach(explode("\n", $row->descriptor) as $line){{ $line }}<br>@endforeach &nbsp;</td>
                <td align="right" style="font-size:smaller;">{{number_format(str_replace(',', '.', $row->netprice), 2, ',', '')}}€&nbsp;</td>
                <td align="right" style="font-size:smaller;">{{number_format($pos = round((float) str_replace(',', '.', $row->netprice) * (float) str_replace(',', '.', $row->amount),2), 2, ',', '')}}€&nbsp;</td>
            </tr>
            @php
                $order++;
                $zsum = $zsum + $pos;
                $vat[$row->vat][] = $pos;
                @endphp
            @endforeach

            @endif

            @php
                $order = 1;
                $incidentals = $model->_items->where('_meta._doctype', '050b070c151f8f0b9d76589922f58a908e67535f')->sortBy('sort_order');
                @endphp
               
            @if($incidentals->count())
            <tr>
                <td colspan="6">&nbsp;{{++$main}}. Nebenleistungen<br></td>
            </tr>

            
            @foreach($incidentals AS $index=>$row)
            <tr>
                <td>&nbsp;{{$main}}.{{$order}}</td>
                <td align="right" style="font-size:smaller;">{{number_format(str_replace(',', '.', $row->amount), 2, ',', '')}}&nbsp;</td>
                <td style="font-size:smaller;">&nbsp;{{$row->unit}}&nbsp;</td>
                <td style="font-size:smaller;">@foreach(explode("\n", $row->descriptor) as $line)&nbsp;{{ $line }}<br>@endforeach</td>
                <td align="right" style="font-size:smaller;">{{number_format((float) str_replace(',', '.', $row->netprice), 2, ',', '')}}€&nbsp;</td>
                <td align="right" style="font-size:smaller;">{{number_format($pos = round((float) str_replace(',', '.', $row->netprice) * (float) str_replace(',', '.', $row->amount), 2), 2, ',', '')}}€&nbsp;</td>
            </tr>
            @php 
                $order++; 
                $zsum = $zsum + $pos; 
                $vat[$row->vat][] = $pos; 
                @endphp
            @endforeach

            @endif

            @if($model->_premium_charge)

            <tr>
                <td style="font-size:smaller;" colspan="5">&nbsp;Zwischensumme der Nettobeträge</td>
                <td align="right">{{number_format($zsum, 2, ',', '')}}€&nbsp;</td>
            </tr>

            <tr>
                <td colspan="6">&nbsp;{{++$main}}. Sonderzuschläge<br></td>
´            </tr>

            <tr>
                <td>&nbsp;{{$main}}.{{$order}}</td>
                @if("%" == $model->_premium_charge->unit)
                <td align="right" style="font-size:smaller;">&nbsp;{{$model->_premium_charge->value}}&nbsp;</td>
                <td style="font-size:smaller;">&nbsp;%&nbsp;</td>
                <td style="font-size:smaller;">{{$model->_premium_charge->descriptor}}&nbsp;</td>
                <td align="right" style="font-size:smaller;">{{number_format($sz = $zsum / 100 * (float) str_replace(',', '.', $model->_premium_charge->value), 2, ',', '')}}&nbsp;</td>
                <td align="right" style="font-size:smaller;">{{number_format($sz, 2, ',', '')}}&nbsp;</td>
                @else
                <td align="right" style="font-size:smaller;">&nbsp;1&nbsp;</td>
                <td style="font-size:smaller;">&nbsp;Pauschale&nbsp;</td>
                <td style="font-size:smaller;">{{$model->_premium_charge->descriptor}}&nbsp;</td>
                <td align="right" style="font-size:smaller;">{{number_format($sz = (float) str_replace(',', '.', $model->_premium_charge->value), 2, ',', '')}}&nbsp;</td>
                <td align="right" style="font-size:smaller;">{{number_format($sz, 2, ',', '')}}&nbsp;</td>
                @endif
            </tr>


            @php
              $zsum = $zsum + $sz;
              $vat[$model->_premium_charge->_vat->rate][] = $sz;
              @endphp

            <tr>
                <td style="font-size:smaller;" colspan="5">&nbsp;Zwischensumme der Nettobeträge</td>
                <td align="right">{{number_format($zsum, 2, ',', '')}}€&nbsp;</td>
            </tr>


            @endif
               
            @php
                $order = 1;
                $material = $model->_items->where('_meta._doctype', '854cbb63e8c1319f02330c2e61e7b93bf4682cc2')->sortBy('sort_order');
                @endphp
            
            @if($material->count())
            <tr>
                <td colspan="6">&nbsp;{{++$main}}. Material<br></td>
´            </tr>

            @foreach($material AS $index=>$row)
            <tr>
                <td>&nbsp;{{$main}}.{{$order}}</td>
                <td align="right" style="font-size:smaller;">&nbsp;{{number_format(str_replace(',', '.', $row->amount), 2, ',', '')}}&nbsp;</td>
                <td style="font-size:smaller;">&nbsp;{{$row->unit}}&nbsp;</td>
                <td style="font-size:smaller;">@foreach(explode("\n", $row->descriptor) as $line)&nbsp;{{ Blade::render($line, $model->prepareBladeData()); }}<br>@endforeach</td>
                <td align="right" style="font-size:smaller;">{{number_format(str_replace(',', '.', $row->netprice), 2, ',', '')}}€&nbsp;</td>
                <td align="right" style="font-size:smaller;">{{number_format($pos = round((float) str_replace(',', '.', $row->netprice) * (float) str_replace(',', '.', $row->amount), 2), 2, ',', '')}}€&nbsp;</td>
            </tr>
            @php
                $order++;
                $zsum = $zsum + $pos;
                $vat[$row->vat][] = $pos;
                @endphp
            @endforeach

            @endif

            @php
                $order = 1;
                $misc = $model->_items->where('_meta._doctype', 'e63730ed5536eb59ae0dfd1eb96f5a862be2e5c7')->sortBy('sort_order');
                @endphp

            @if($misc->count())
            <tr>
                <td colspan="6">&nbsp;{{++$main}}. Sonstiges<br></td>
´            </tr>

            @foreach($misc AS $index=>$row)
            <tr>
                <td>&nbsp;{{$main}}.{{$order}}</td>
                <td align="right" style="font-size:smaller;">&nbsp;{{number_format(str_replace(',', '.', $row->amount), 2, ',', '')}}&nbsp;</td>
                <td style="font-size:smaller;">&nbsp;{{$row->unit}}&nbsp;</td>
                <td style="font-size:smaller;">@foreach(explode("\n", $row->descriptor) as $line)&nbsp;{{ Blade::render($line, $model->prepareBladeData()); }}<br>@endforeach</td>
                <td align="right" style="font-size:smaller;">{{number_format(str_replace(',', '.', $row->netprice), 2, ',', '')}}€&nbsp;</td>
                <td align="right" style="font-size:smaller;">{{number_format($pos = round((float) str_replace(',', '.', $row->netprice) * (float) str_replace(',', '.', $row->amount), 2), 2, ',', '')}}€&nbsp;</td>
            </tr>
            @php
                $order++;
                $zsum = $zsum + $pos;
                $vat[$row->vat][] = $pos;
                @endphp
            @endforeach

            @endif

            @php
              $order = 1;
              @endphp

            
            <tr>
                <td style="font-size:smaller;" colspan="5">&nbsp;Summe der Nettobeträge</td>
                <td align="right">{{number_format($zsum, 2, ',', '')}}€&nbsp;</td>
            </tr>

            @if(!$editor->getValue('COMPANY_SBLAW'))
            @foreach($vat AS $index => $val)
            <tr>
                <td colspan="4" style="border-right:0px">&nbsp;zzgl. {{$index}} % Umsatzsteuer auf</td>
                <td align="right" style="border-left:0px">&nbsp;{{number_format(array_sum($val), 2, ',', '')}}€&nbsp;</td>
                <td align="right">{{number_format($vat = round(array_sum($val) / 100 * (float) str_replace(',', '.', $index), 2), 2, ',', '')}}€&nbsp;</td>
                @php $zsumvat += $vat; @endphp
            </tr>
            @endforeach
            @endif
            
            <tr>
                <td colspan="5"><b>&nbsp;Gesamtbetrag</b></td>
                <td align="right"><b>{{number_format($zsum + $zsumvat, 2, ',', '')}}€</b>&nbsp;</td>
            </tr>
            
        </tbody>
    </table>

    @php
        if(number_format((float) str_replace(',', '.', $model->net), 2, ',', '') != number_format($zsum, 2, ',', '')) throw new \Exception('Nettobeträge stimmen nicht überein');
        if(number_format((float) str_replace(',', '.', $model->gross), 2, ',', '') != number_format($zsum + $zsumvat, 2, ',', '')) throw new \Exception('Bruttobeträge stimmen nicht überein');
        @endphp
    
    @php 
        
        $customer = \App\Models\PHPXPress\HwplusClients::with('_payment_term')
            ->whereHas('_meta._doctype', function($query) {
                $query->where('identifier', 'new_client');
            })
            ->where('customer_id', $model->_client->customer_id)
            ->first(); 
    @endphp
       
    <table width="100%" style="border-spacing:0px; margin: 10px 50px;">
        <tr>
            <td>Zahlungshinweis</td>
        </tr>

        <tr>
            <td>{{$customer->_payment_term->text}}</td>
        </tr>
    </table>
    
    
    <table width="100%" style="border-spacing:0px; margin: 10px 50px;">
    @foreach($model->_notes->sortBy('sort') AS $item)
        <tr>
            <td>{{$item->text}}</td>
        </tr>
    @endforeach
    </table>
   
    
    @if($model->print_note)
    <table width="100%" style="border-spacing:0px; margin: 10px 50px;">
        <tr>
          <td>&nbsp;</td>
          <td>&nbsp;</td>
        </tr>
        <tr>
          <td colspan="2"><b>Vermerk:</b></td>
        </tr>
        <tr>
          <td colspan="2">{{$model->note}}</td>
        </tr>
    </table>
    @endif

    
</body>
</html>
 
