<?php Fun::lang() ?>

<table class="table table-striped table-hover ">
    <thead>
    <tr>
        <th>#</th>
        <th>{{__('administrator.date')}}</th>
        <th>{{__('administrator.description')}}</th>
        <th>{{__('administrator.reference')}}</th>
        <th>{{__('administrator.user')}}</th>
        <th>{{__('administrator.pay_to_receive_from')}}</th>
        <th>{{__('administrator.note')}}</th>
        <th>{{__('administrator.cash_in')}}</th>
        <th>{{__('administrator.cash_out')}}</th>
        <th>{{__('administrator.balance')}}</th>
        <th>{{__('administrator.action')}}</th>
    </tr>
    </thead>
    <tbody>
    @php
        $balance = 0;
        $balance_kh = 0;
        $key = 1;
        $old_amount = @$old_amount?$old_amount:0;
    @endphp
    @if($old_amount)
        <tr>
            <td>{{$key++}}</td>
            <td>{{date('d-m-Y')}}</td>
            <td>{{__('administrator.opening_balance')}}</td>
            <td>{{date('dmY00001')}}</td>
            <td>{{Auth::user()->name}}</td>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
            <td>$ {{$old_amount}}</td>
            <td></td>
        </tr>
    @endif
    @foreach($cash_in_out as $row)
        @php
            $user = findUser($row->user_id);
            if($row->invoice_id){
                $customer = findCustomer($row->pay_to_receive_from);
                $customer = $customer?$customer->customer_name:'';
            }else{
                $customer = $row->pay_to_receive_from;
            }
         $cash_out = ($row->cash_type=='cash_out'?$row->amount:'');
        $cash_in = ($row->cash_type=='cash_in'?$row->amount:'');
        if($row->cash_type=='cash_in'){
            if($row->currency_id==1){
             $balance_kh += $cash_in;
            }
            if($row->currency_id==2){
             $balance += $cash_in;
            }
        }
        $currency = findCurrency($row->currency_id);
        $currency = $currency?$currency->symbol:'';
        $cash_out = $cash_out?($currency.$cash_out):'';
        $cash_in = $cash_in?($currency.$cash_in):'';
        @endphp

        <tr>
            <td>{{$key++}}</td>
            <td>{{date('d-m-Y',strtotime($row->date))}}</td>
            <td>{{$row->description}}</td>
            <td>{{$row->invoice_number}}</td>
            <td>{{$user->name}}</td>
            <td>{{$customer}}</td>
            <td>{{$row->note}}</td>
            <td>{{$cash_in}}</td>
            <td>{{$cash_out}}</td>
            <td>៛{{$balance_kh}}, ${{$balance}}</td>
            <td><a data-href="{{route('cash-in-cash-out.edit',$row->id)}}"
                   class="button_warning get_edit_form">{{__('administrator.edit')}}</a></td>
        </tr>
    @endforeach
    </tbody>
</table>
<div class="text-right">
    {!! $cash_in_out->render() !!}
</div>


