@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();

        $moneyInfoItems = \App\Models\MoneyInfo::where('room_id', $roomId)
       ->where(function ($q) use ($contract) {

                    $q->orwhere('user_id', $contract->renter_id);
                    $q->orWhere('contract_id', $contract->id);

                })
        ->where('remain', '>', 0)
        ->whereBetween('date_action', [$startTime, $endTime]);

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

 $moneyInfoItems = $moneyInfoItems->where('remain', '>', 0);
        }
    }

    $moneyInfoItems = $moneyInfoItems->orderBy('id', 'desc')->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) {
                       $sum+= $detail->amount;
                                       }
                        @endphp
                        <!-- BEGIN Portlet PORTLET-->

                            <div class="col-md-12">
                                <div class="portlet box @if($sum == $pay) green-jungle @elseif ($pay == 0) red @else green-steel @endif">
                                    <div class="portlet-title">

                                            <div class="caption" style="float: left">{{ $moneyInfoItem->name }}</div>

                                        <div class="caption" style="float: right">
                                            @if($sum - $moneyInfoItem->discount == $pay) Đã thanh toán @elseif ($pay == 0) Chưa thanh toán. Tổng số tiền: {{ number_format($sum, 0, '.', '.') }} @else Đã
                                            thanh toán một phần. Số tiền còn lại: {{ number_format($sum - $pay - $moneyInfoItem->discount, 0, '.', '.') }} @endif</div>
                                    </div>
                                    <div class="portlet-body">

                                        @if($moneyInfoItem->contract)
                                            @if($moneyInfoItem->type != \App\Models\MoneyInfo::VOUCHER_CONTRACT)
                                                <h5>Hợp đồng: {{ $moneyInfoItem->contract->code }}</h5>

                                            @endif
                                            <h5>Người đại diện: {{ $moneyInfoItem->contract->name }}</h5>
                                        @endif
                                        @include('admin2.money.detail', [
                                    'item' => $moneyInfoItem,
                                    'details' => $details,
                                    'pay' => $pay, 'sum' => $sum])


                                    </div>
                                </div>
                            </div>
                            <div class="col-md-12">
                                <div class="row">
                                    @if(in_array($moneyInfoItem->type, [ App\Models\MoneyInfo::VOUCHER_CONTRACT, App\Models\MoneyInfo::VOUCHER_ROOM_PRICE]))
                                    <div class="col-md-3">
                                        <a href="#" class="update-money-info"
                                           data-money-info="{{ $moneyInfoItem->id }}">Cập nhật hóa đơn</a>
                                    </div>
                                    @endif
                                    <div class="col-md-3">
                                        <a data-money-info="{{ $moneyInfoItem->id }}" data-date="{{ $moneyInfoItem->date_action->format('m/Y') }}" class="print-money-info" target="_blank" href="{{ url('admin/money/print', ['id' => $moneyInfoItem->id]) }}"> In phiếu</a>
                                    </div>
                                    <div class="col-md-3">
                                        <a href="#" class="money-info-email" data-money-info="{{ $moneyInfoItem->id }}">Email thông báo</a>
                                    </div>
                                    <div class="col-md-3">
                                        <a href="#" class="money-info-message" data-money-info="{{ $moneyInfoItem->id }}">Nhắn tin thông báo miễn phí</a>
                                    </div>
                                </div>


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

                                @if(isset($pay))
                                    @if($pay == 0)
                                        @if(auth('backend')->user()->can('add-payment'))
                                        <button class="btn green-jungle btn-paid" data-money-info="{{ $moneyInfoItem->id }}" >Thanh toán</button>
                                            @endif
                                    @elseif ($pay < $sum)
                                        @if(auth('backend')->user()->can('add-payment'))
                                        <button class="btn green-jungle btn-paid" data-money-info="{{ $moneyInfoItem->id }}" >Thanh toán bổ sung</button>
                                            @endif
                                    @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" id="form-paid-{{ $moneyInfoItem->id }}" style="margin-top: 15px; margin-bottom: 15px">

                            </div>
                    @endforeach
                    <!-- END GRID PORTLET-->
                    </div>
                </div>
            </div>
        </div>

    @endif
@endforeach