@foreach($items as $item)
    @php


        $startTime = \Carbon\Carbon::createFromFormat('d/m/Y', '01/'.$item->date_action_group)->startOfMonth()->toDateString();
        $endTime = \Carbon\Carbon::createFromFormat('d/m/Y', '01/'.$item->date_action_group)->endOfMonth()->toDateString();

if(!empty($hostelId))
{
$moneyInfoItems = \App\Models\MoneyInfo::where('hostel_id', $hostelId);
} else {
if(auth('backend')->user()->type == \App\User::STAFF)
{
    $hostels = \App\Components\Functions::getHostelArrStaff();
} else {
$hostels = \App\Models\Hostel::where('owner_id', auth('backend')->user()->id)->pluck('id')->toArray();
}

$moneyInfoItems = \App\Models\MoneyInfo::whereIn('hostel_id', $hostels);

}
        $moneyInfoItems = $moneyInfoItems

        ->when($owner->type_display_money_info == \App\User::TYPE_DISPLAY_MONEY_INFO_PREVIOUS_MONTH, function ($q) use ($month) {
                    $q->where(function ($q) use ($month) {
                        $q->orWhere(function ($q) use ($month) {
                            $q->whereBetween('date_action', [
                                $month->copy()->startOfMonth(),
                                $month->copy()->endOfMonth()
                            ]);
                            $q->whereIn('type', [
                                \App\Models\MoneyInfo::VOUCHER_CONTRACT,
                                \App\Models\MoneyInfo::VOUCHER_ROOM_PRICE
                            ]);
                        });

                        $q->orWhere(function ($q) use ($month) {
                            $q->whereBetween('date_action', [
                                $month->copy()->subMonth()->startOfMonth(),
                                $month->copy()->subMonth()->endOfMonth()
                            ]);
                            $q->where('type', \App\Models\MoneyInfo::VOUCHER_SERVICE);
                        });
                    });

                }, function ($q) use ($month) {

                    $q->whereBetween('date_action', [
                        $month->copy()->startOfMonth(),
                        $month->copy()->endOfMonth()
                    ]);

                });


    if(!empty($type))
    {
        if($type == 'paid')
        {
        $moneyInfoItems = $moneyInfoItems->where('remain', 0);

        } else if ($type == 'not-paid')
        {
 $moneyInfoItems = $moneyInfoItems->where('remain', '>', 0);
        }
    }

    if(!empty($contractId))
    {
        $moneyInfoItems = $moneyInfoItems->where('contract_id', $contractId);
    }

    $moneyInfoItems = $moneyInfoItems->get();

    @endphp

    @if($moneyInfoItems->count() > 0)

        <div class="portlet box yellow">
            <div class="portlet-title">
                <div class="caption">Tháng {{ $item->date_action_group }} </div>
                <div class="tools">
                    <a href="javascript:;" class="collapse" data-original-title="" title=""> </a>
                </div>
            </div>
            <div class="portlet-body" style="display: block;">
                <div class="row">
                    <div class="col-md-12">


                    @foreach($moneyInfoItems as $moneyInfoItem)
                        @php
                            $details = \App\Models\MoneyDetail::where('money_info_id', $moneyInfoItem->id)->get();
               $room = $moneyInfoItem->room;

               $dateAction = $item->date_action->format('m/Y');
               $pay = \App\Components\Functions::calculateAmountForOrder($moneyInfoItem->id)['pay'];
               $sum = 0;
                           foreach($details as $detail) {
                           if(!empty($detail->sum_amount))
                           {
                            $sum+= $detail->sum_amount;
                           } else {
                       $sum+= $detail->amount;
                                       }
                        }


                        @endphp
                        <!-- BEGIN Portlet PORTLET-->

                            <div class="portlet box @if($sum == ($pay + $moneyInfoItem->discount)) green-jungle @elseif ($pay == 0) red @else green-steel @endif">
                                <div class="portlet-title">
                                    <div class="caption">
                                        {{ !empty($moneyInfoItem->hostel) ? $moneyInfoItem->hostel->name : null }} -
                                        Phòng {{ !empty($moneyInfoItem->room) ? $moneyInfoItem->room->name : null }} -
                                        {{ !empty($moneyInfoItem->contract) ? $moneyInfoItem->contract->name : null }} -
                                    @if($sum == $pay) Đã thanh toán @elseif ($pay == 0) Chưa thanh toán @else Đã
                                        thanh toán một phần @endif</div>
                                </div>
                                <div class="portlet-body">
                                    @include('admin2.money.detail', [
                                    'item' => $moneyInfoItem,
                                    'details' => $details,
                                    'pay' => $pay, 'sum' => $sum])


                                </div>
                            </div>
                    @endforeach
                    <!-- END GRID PORTLET-->
                    </div>
                </div>
                {{--<div class="row">--}}
                    {{--<div class="col-md-12">--}}
                        {{--<div class="row">--}}
                            {{--<div class="col-md-3">--}}
                                {{--<a target="_blank" href="{{ url('admin/money/print', ['id' => $moneyInfoItem->id]) }}"> In phiếu</a>--}}
                            {{--</div>--}}
                            {{--<div class="col-md-3">--}}
                                {{--<a href="#">Email thông báo</a>--}}
                            {{--</div>--}}
                            {{--<div class="col-md-3">--}}
                                {{--<a href="#">Nhắn tin thông báo miễn phí</a>--}}
                            {{--</div>--}}
                        {{--</div>--}}


                    {{--</div>--}}
                {{--</div>--}}
                {{--<div class="row" style="margin-top: 15px">--}}
                    {{--<div class="col-md-12">--}}

                        {{--@if(isset($pay))--}}
                            {{--@if($pay == 0)--}}
                                {{--<button class="btn green-jungle btn-paid">Thanh toán</button>--}}
                            {{--@elseif ($pay < $sum)--}}
                                {{--<button class="btn green-jungle btn-paid">Thanh toán bổ sung</button>--}}
                            {{--@endif--}}
                            {{--<a href="#detail-history" data-toggle="modal" data-room="{{ $room->id }}}}"--}}
                               {{--data-money-info="{{ $moneyInfoItem->id }}" class="btn purple-plum btn-detail-history">Lịch--}}
                                {{--sử thanh toán</a>--}}
                        {{--@endif--}}
                    {{--</div>--}}
                    {{--<div class="col-md-12 form-paid" style="margin-top: 15px">--}}

                    {{--</div>--}}

                {{--</div>--}}
            </div>
        </div>

    @endif
@endforeach