<!DOCTYPE html>
<html lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>

    <meta property="og:url" content="<?php echo e(url()->current()); ?>"/>
    <meta property="og:type" content="website"/>
    <meta property="og:title" content="itro.vn"/>
    <meta property="fb:app_id" content="1072839196191686"/>
    <meta property="og:description"
          content="Hóa đơn" />
    <meta property="og:image" content="https://itro.vn/invoice-bg.jpg"/>

    <title>Hóa đơn | iTro</title>
    <script src="/assets/global/plugins/jquery.min.js" type="text/javascript"></script>
    <?php if(!isset($isPrint)): ?>
        <script>
            $(document).ready(function () {
                window.print();
            });
        </script>
    <?php endif; ?>

    <style>
        @media  print {
            .pagebreak { page-break-before: always; } /* page-break-after works, as well */
        }

        /*RESET*/
        * {
            margin: 0;
            padding: 0;
        }

        *, *:before, *:after {
            box-sizing: inherit;
        }

        img, object, embed {
            max-width: 100%;
        }

        img {
            border: 0;
        }

        a {
            background: transparent;
        }

        /*PRINT*/
        @page  {
            size: auto;
            margin: 1cm;
        }

        @media  print {
            .header, .hide {
                visibility: hidden
            }
        }

        /*BASE*/

        <?php if(isset($font)): ?>
        body {
            font-family: DejaVu Sans, sans-serif;
            font-size: 9pt;
            line-height: 12pt;
            font-weight: 400;
        }

        <?php else: ?>
        body {
            font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
            font-size: 9pt;
            line-height: 12pt;
            font-weight: 400;
        }

        <?php endif; ?>

        table {
            margin: 0;
            padding: 0;
            border: 0;
            background: transparent;
            width: 100%;
        }

        table {
            table-layout: auto;
            border-collapse: collapse;
            width: 100%;
        }

        table .absorbing-column {
            width: 100%;
        }

        /*CLASSES*/
        .text-nowrap {
            white-space: nowrap !important;
        }

        .text-center {
            text-align: center !important;
        }

        .text-left {
            text-align: left !important;
        }

        .text-right {
            text-align: right !important;
        }

        .text-uppercase {
            text-transform: uppercase !important;
        }

        .pa-12 {
            padding: 12pt !important;
        }

        .pt-12 {
            padding-top: 12pt !important;
        }

        .pb-12 {
            padding-bottom: 12pt !important;
        }

        .pl-12 {
            padding-left: 12pt !important;
        }

        .pr-12 {
            padding-right: 12pt !important;
        }

        .pa-24 {
            padding: 24pt !important;
        }

        .pt-24 {
            padding-top: 24pt !important;
        }

        .pb-24 {
            padding-bottom: 24pt !important;
        }

        .pl-24 {
            padding-left: 24pt !important;
        }

        .pr-24 {
            padding-right: 24pt !important;
        }

        .ma-12 {
            padding: 12pt !important;
        }

        .mt-12 {
            padding-top: 12pt !important;
        }

        .mb-12 {
            padding-bottom: 12pt !important;
        }

        .ml-12 {
            padding-left: 12pt !important;
        }

        .mr-12 {
            padding-right: 12pt !important;
        }

        .ma-24 {
            padding: 24pt !important;
        }

        .mt-24 {
            padding-top: 24pt !important;
        }

        .mb-24 {
            padding-bottom: 24pt !important;
        }

        .ml-24 {
            padding-left: 24pt !important;
        }

        .mr-24 {
            padding-right: 24pt !important;
        }

        /*MAIN*/
        body {
            /*padding: 1cm;*/
            margin: 0;
            padding: 0;
        }

        <?php use App\User;$ownerColor = User::query()->where('type', User::ADMIN)
         ->where('phone', '0908372472')->first();
         ?>
         <?php if(auth('backend')->check()): ?>
        <?php if(auth('backend')->user()->id == optional($ownerColor)->id ||  auth('backend')->user()->staff_owner_id == optional($ownerColor)->id): ?>
        body {
            /*padding: 1cm;*/
            color: #00388c
        }

        table {
            border-color: #00388c;
        }

        <?php endif; ?>
        <?php endif; ?>

        @media  screen {
            body {
                background-color: #eee;
            }

            .page-container {
                max-width: 1200px;
                margin: 24px auto;
                padding: 24px;
                background-color: #fff;
            }
        }

        /*Header*/
        .invoice__header {
            margin: 0 0 8pt;
        }

        .invoice__header .company__logo img {
            display: block;
            max-width: 100pt;
            max-height: 50pt;
        }

        .invoice__header .company__detail {
            padding: 0 12pt 0 8pt;
        }

        .invoice__header .company__detail .detail-title {
            margin: 0 0 2pt;
            font-size: 12pt;
        }

        .invoice__header .company__detail .detail-info {
            margin: 1pt 0 0;
        }

        .invoice__header .invoice__date-issue {
            margin: 0 0 4pt;
        }

        /*Title*/
        .invoice__title {
            margin: 0 0 8pt;
        }

        .invoice__title .invoice-title {
            margin: 0;
            line-height: 20pt;
            font-size: 14pt;
            font-weight: 700;
        }

        .invoice__title .invoice-subtitle {
            margin: 2pt 0 0;
            line-height: 14pt;
            font-size: 10pt;
            font-weight: 700;
        }

        /*Customer*/
        .invoice__customer {
            margin: 0 0 4pt;
        }

        .invoice__customer td {
            padding-top: 1pt;
            vertical-align: top;
        }

        /*Detail*/
        .invoice__detail {
            width: 100%;
            max-width: 100%;
            margin: 0 0 8pt;
            font-size: 9pt;
            border: 1pt solid #333;
        }

        .invoice__detail th,
        .invoice__detail td {
            line-height: 14pt;
            text-align: center;
            border: 1pt solid #333;
        }

        .invoice__detail th {
            padding: 2pt;
        }

        .invoice__detail td {
            padding: 2pt;
        }

        /*Summary*/
        .invoice__summary {
            margin: 0 0 8pt;
        }

        .invoice__summary td {
            vertical-align: top;
            padding-top: 1pt;
        }

        /*Payment*/
        .invoice__payments {
            margin: 0 0 8pt;
            table-layout: fixed;
        }

        .invoice__payments td {
            vertical-align: top;
        }

        .invoice__payments table td {
            padding-top: 1pt;
        }

        /*Footer*/
        .invoice__footer {
            margin: 12pt 0 8pt;
            table-layout: fixed;
        }

        .invoice__footer td {
            vertical-align: top;
        }

        /*Notes*/
        .invoce__notes {
            margin-top: 8pt;
        }

    </style>




</head>
<body>
<?php $__currentLoopData = $contracts; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $contract): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
    <?php
    if($type == 1) {
        $items = \App\Models\MoneyInfo::query()
                ->where(function ($q) use ($dateCarbon) {
                    $q->orWhere(function ($q2) use ($dateCarbon) {
                        $q2->where('type', \App\Models\MoneyInfo::VOUCHER_SERVICE)
                            ->whereBetween('date_action', [
                                $dateCarbon->copy()->subMonth()->startOfMonth()->startOfDay(),
                                $dateCarbon->copy()->subMonth()->endOfMonth()->endOfDay()
                            ]);
                    });
                    $q->orWhere(function ($q2) use ($dateCarbon) {
                        $q2->whereIn('type', [\App\Models\MoneyInfo::VOUCHER_ROOM_PRICE, \App\Models\MoneyInfo::VOUCHER_CONTRACT])
                            ->whereBetween('date_action', [
                                $dateCarbon->copy()->startOfMonth()->startOfDay(),
                                $dateCarbon->copy()->endOfMonth()->endOfDay()
                            ]);
                    });
                })
                ->where('contract_id', $contract->id)
    ->get();
        } else {

     $items = \App\Models\MoneyInfo::query()
                ->where(function ($q) use ($dateCarbon) {

                            $q->whereBetween('date_action', [
                                $dateCarbon->copy()->startOfMonth()->startOfDay(),
                                $dateCarbon->copy()->endOfMonth()->endOfDay()
                            ]);
                })
                ->where('contract_id', $contract->id)
    ->get();
        }
$item = $items->first();
if(!$item)
    {
        continue;
    }
$sum = $items->sum('amount');
$discount = $items->sum('discount');
$pay = $items->sum('pay');
$details = \App\Models\MoneyDetail::query()->whereIn('money_info_id', $items->pluck('id')->toArray())->get();
            ?>
<div class="page-container">
    <?php
        $electricPrice = 0;
        $waterPrice = 0;

    $hostelFee = \App\Models\HostelFee::where('type', \App\Models\HostelFee::ELECTRIC)
        ->where('hostel_id', $item->room->hostel->id)
        ->first();

        $hostelFeeWater = \App\Models\HostelFee::where('type', \App\Models\HostelFee::WATER)
        ->where('hostel_id', $item->room->hostel->id)
        ->first();

    if ($hostelFee) {
        $electric = \App\Models\ElectricQuota::where('hostel_id', $item->room->hostel->id)
        ->where('fee_id', $hostelFee->id)
        ->first();

        if(!$electric)
        {
        $electric = \App\Models\ElectricQuota::where('hostel_id', $item->room->hostel->id)->first();
        }

    } else {
        $electric = \App\Models\ElectricQuota::where('hostel_id', $item->room->hostel->id)->first();
       // $water = \App\Models\WaterQuota::where('hostel_id', $item->room->hostel->id)->first();
    }

    if($hostelFeeWater)
    {
    $water = \App\Models\WaterQuota::where('hostel_id', $item->room->hostel->id)
    ->where('fee_id', $hostelFeeWater->id)
    ->first();
    if(!$water)
    {
      $water = \App\Models\WaterQuota::where('hostel_id', $item->room->hostel->id)->first();
    }
    } else {
    $water = \App\Models\WaterQuota::where('hostel_id', $item->room->hostel->id)->first();

    }

    if ($electric) {
        $electricPrice = $electric->price_quota_1;
    }

    if($water)
    {
        $waterPrice = $water->price_quota_1;
    }

    ?>

    <table class="invoice__header">
        <tbody>
        <tr>
            
            
            

            <?php $hostel = $item->room->hostel;

            ?>


            <td class="absorbing-column">
                <div class="company__detail">
                    <h2 class="detail-title"><?php echo e($hostel->name); ?></h2>

                    <div class="detail-info">Địa chỉ/Address: <?php echo e($hostel->address_text); ?>

                    </div>
                    
                    <?php if($hostel->owner_id != 31746): ?>
                    <div class="detail-info">SĐT/Phone: <?php echo e($item->hostel->getHotlinePhoneAttribute()); ?></div>
                    <?php endif; ?>
                    <div class="invoice__date-issue text-nowrap">Ngày in / Statement Date: <?php echo e(\Carbon\Carbon::now()->format('d/m/Y')); ?></div>
                </div>
            </td>


            <td>
                <?php $setting = \App\Models\Config::query()->where('owner_id', $hostel->owner_id)->first() ?>
                <?php if($setting): ?>
                    <?php if(!empty($setting->logo)): ?>

                        <img src="/files/<?php echo e($setting->logo); ?>" style="max-width: 250px"/>

                    <?php endif; ?>
                <?php endif; ?>


            </td>

        </tr>
        </tbody>
    </table>


    <table class="invoice__title">
        <tbody>
        <tr>
            <td>
                <div class="text-center">
                    <h1 class="invoice-title">HÓA ĐƠN <br> INVOICE</h1>
                    <?php if(!empty($item->date_aciton)): ?>
                        <div class="invoice-subtitle">Kỳ thanh toán: Tháng <?php echo e($item->date_action->format('m/Y')); ?></div>
                    <?php endif; ?>
                </div>
            </td>
        </tr>
        </tbody>
    </table>


    <table class="invoice__customer">
        <tbody>
        <tr>
            <td class="pr-24">
                <table>
                    <tbody>
                    <tr>
                        <td class="text-nowrap pr-24"><b>Tên khách hàng/Customer name:</b></td>

                        <?php if($item instanceof App\Models\Contract): ?>
                            <td class="absorbing-column"><b><?php echo e($item->name); ?></b></td>
                        <?php else: ?>
                            <td class="absorbing-column"><b><?php echo e(optional($item->contract)->name); ?></b></td>
                        <?php endif; ?>

                    </tr>
                    <tr>
                        <td class="text-nowrap pr-24">Phòng/Room:</td>
                        <td class="absorbing-column"><?php echo e($item->room->name); ?>

                            <?php if($item instanceof App\Models\Contract): ?>
                                <?php if($item->bed): ?> (<?php echo e($item->bed->name); ?>) <?php endif; ?>
                            <?php else: ?>
                                <?php if(optional($item->contract)->bed): ?> (<?php echo e(optional($item->contract)->bed->name); ?>) <?php endif; ?>
                            <?php endif; ?>
                        </td>
                    </tr>

                    </tbody>
                </table>
            </td>
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
        </tr>
        </tbody>
    </table>


    <table class="invoice__detail">
        <thead>
        <tr>
            <th rowspan="2" class="text-left" style="min-width: 100pt">Khoản thu <br>(Description)</th>
            
            <th colspan="2">Chỉ số điện / nước <br>(Electricity/Water)</th>
            <th rowspan="2">Số lượng <br>(Quantity)</th>
            <th rowspan="2">Đơn vị <br> (Unit)</th>
            <th rowspan="2">Đơn giá <br> (Unit Price)</th>
            <th rowspan="2">Thành tiền <br>(Total)</th>

        </tr>
        <tr>
            
            
            <th>Đầu <br>(Previous number)</th>
            <th>Cuối <br>(Present number)</th>

        </tr>
        </thead>
        <tbody>
        <?php $__currentLoopData = $details; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $detail): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
            <?php
            $hostelFee = \App\Models\HostelFee::find($detail->hostel_fee_id);

            ?>
            <tr>
                <?php if($hostel->owner_id != 16088 && $hostel->owner_id != 33005): ?>
                    <td class="text-left"><?php echo nl2br(ucfirst($detail->name)); ?> <?php if(optional($detail->moneyInfo)->type == \App\Models\MoneyInfo::VOUCHER_SERVICE): ?>
                            (<?php echo e($detail->moneyInfo->date_action->format('m/Y')); ?>) <?php endif; ?> </td>
                <?php else: ?>
                    <td class="text-left"><?php echo nl2br(ucfirst($detail->name)); ?> </td>
                <?php endif; ?>

                
                
                <?php if(!empty($detail->start_end)): ?>
                    <?php $startEnd = $detail->start_end ?>

                    <td><?php echo e($startEnd['start']); ?></td>
                    <td><?php echo e($startEnd['end']); ?></td>

                    <td><?php echo e($startEnd['end'] - $startEnd['start']); ?></td>
                <?php else: ?>
                    <td>0</td>
                    <td>0</td>

                    <td><?php echo e($detail->qty); ?></td>
                <?php endif; ?>

                <?php if($detail->is_electric == true): ?>
                    <?php if(!empty($hostelFee)): ?>
                        <?php if(in_array($hostelFee->type, [\App\Models\HostelFee::ELECTRIC, \App\Models\HostelFee::ELECTRIC_BY_CLOCK, \App\Models\HostelFee::ELECTRIC_DYNAMIC ])): ?>
                            <td>kwH</td>
                        <?php else: ?>
                            <td>người</td>
                        <?php endif; ?>
                    <?php else: ?>
                        <td>kwH</td>
                    <?php endif; ?>
                <?php elseif($detail->is_water): ?>
                    <?php if(!empty($hostelFee)): ?>
                        <?php if(in_array($hostelFee->type, [\App\Models\HostelFee::WATER, \App\Models\HostelFee::WATER_BY_CLOCK, \App\Models\HostelFee::WATER_DYNAMIC ])): ?>
                            <td>m3</td>
                        <?php else: ?>
                            <td>người</td>
                        <?php endif; ?>
                    <?php else: ?>
                        <td>m3</td>
                    <?php endif; ?>
                <?php else: ?>
                    <td></td>
                <?php endif; ?>
                <?php if(!empty($hostelFee)): ?>
                    <?php if(!in_array($hostelFee->type, [\App\Models\HostelFee::WATER, \App\Models\HostelFee::ELECTRIC])): ?>

                        <?php if(in_array($hostelFee->type, [
                        \App\Models\HostelFee::WATER_BY_PEOPLE, \App\Models\HostelFee::WATER_BY_CLOCK, \App\Models\HostelFee::ELECTRIC_BY_PEOPLE,
                        \App\Models\HostelFee::ELECTRIC_BY_CLOCK
                        ])): ?>

                            <?php if($detail->value > 1): ?>
                                <td><?php echo e(number_format($detail->value, 0, '.', '.')); ?></td>
                            <?php else: ?>

                                <td><?php echo e(number_format($hostelFee->fee, 0, '.', '.')); ?></td>
                            <?php endif; ?>
                        <?php else: ?>
                            <td><?php echo e(number_format($detail->value, 0, '.', '.')); ?></td>
                        <?php endif; ?>
                    <?php else: ?>
                        <?php if($detail->is_electric == true): ?>
                            <td><?php echo e(number_format($electricPrice, 0, '.', '.')); ?></td>
                        <?php elseif($detail->is_water == true): ?>
                            <td><?php echo e(number_format($waterPrice, 0, '.', '.')); ?></td>
                        <?php else: ?>
                            <td>1</td>
                        <?php endif; ?>
                    <?php endif; ?>
                <?php else: ?>
                    <td><?php echo e(number_format($detail->value, 0, '.', '.')); ?></td>
                <?php endif; ?>

                <?php if(!empty($detail->sum_amount)): ?>
                    <td><?php echo e(number_format($detail->sum_amount, 0, '.', '.')); ?></td>
                <?php else: ?>
                    <td><?php echo e(number_format($detail->amount, 0, '.', '.')); ?></td>
                <?php endif; ?>

            </tr>
        <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
        </tbody>
        <tbody>
        <tr>
            <td colspan="6" class="text-left"><b>Tổng cộng/Total amount</b></td>
            <td><b><?php echo e(number_format($sum, 0, '.', '.')); ?></b></td>

        </tr>
        <tr>
            <td colspan="6" class="text-left"><b>Giảm giá/Discount</b></td>
            <td><b><?php echo e(number_format($discount, 0, '.', '.')); ?></b></td>

        </tr>
        <tr>
            <td colspan="6" class="text-left"><b>Đã trả/Paid</b></td>
            <td><b><?php echo e(number_format($pay, 0, '.', '.')); ?></b></td>

        </tr>

        <tr>
            <td colspan="6" class="text-left"><b>Còn lại/Remaining</b></td>
            <td><b><?php echo e(number_format($sum - $pay - $discount, 0, '.', '.')); ?></b></td>

        </tr>
        </tbody>
    </table>



    <table class="invoice__summary">
        <tbody>
        <?php if(isset($notes)): ?>

            <?php if(!empty( $contract->note_money_info)): ?>
                <tr>
                    <td class="text-nowrap pr-24"><b>Ghi chú: </b><?php echo e(implode(', ', $contract->note_money_info)); ?></td>
                </tr>
                <?php else: ?>
            <tr>
                <td class="text-nowrap pr-24"><b>Ghi chú: </b></td>
            </tr>
            <?php endif; ?>
        <?php else: ?>
            <tr>
                <td class="text-nowrap pr-24"><b>Ghi chú: </b><?php echo e($item->note); ?></td>
            </tr>
        <?php endif; ?>
        <tr>
            <td class="text-nowrap pr-24"><b>Tổng số tiền phải thanh toán kì này (Total)</b></td>
            <td class="text-nowrap pr-24"><?php echo e(number_format($sum, 0, '.', '.')); ?>đ</td>
        </tr>
        <tr>
            <td class="text-nowrap pr-24"><b>Tổng số tiền đã thanh toán trong kỳ (Paid)</b></td>
            <td class="text-nowrap pr-24"><?php echo e(number_format($pay, 0, '.', '.')); ?>đ</td>
        </tr>
        <tr>
            <td class="text-nowrap pr-24"><b>Tổng số tiền còn phải thanh toán (Remaining)</b></td>
            <td class="text-nowrap pr-24"><?php echo e(number_format($sum - $pay - $discount, 0, '.', '.')); ?>đ</td>
        </tr>
        <tr>
            <td class="">Bằng chữ/In words: <?php echo e($f->format($sum - $pay- $discount)); ?> đồng</td>
        </tr>
        </tbody>
    </table>

    <table class="invoice__footer">
        <tbody>
        <tr>
            <td>
                <?php if(isset($text)): ?>
                    <?php echo $text; ?>

                <?php endif; ?>

            </td>
            <td>
                <div class="invoice__footer__signature text-center" style="margin-left: 40%">
                    <div><b>Người lập hóa đơn <br>(Seller)

                            <?php if($item->hostel->owner->phone === '0979652628'): ?>
                            <br> <?php echo e($item->hostel->owner->name); ?>

                                <?php endif; ?>

                        </b></div>
                    <div style="height: 2cm"></div>
                </div>
            </td>
        </tr>
        </tbody>
    </table>


</div>
    <div class="pagebreak"></div>
<?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>

</body>
</html>