 @extends('adminlte::page') @section('content')
<?php Fun::lang()?>
    <div class="title-page">
        <div class="page-bar">
            <ul class="page-breadcrumb">
                <li><a href="#"><i class="fa fa-dashboard"></i> Dashboard</a></li>
              <li><a href="#">Report Accounting</a></li>
              <li><a href="#">Report Cash Flow</a></li>
            </ul>
        </div>
    </div>
    <div class="page">
        <div class="top-content">
            <div class="row">
                <div class="col-sm-12 col-xs-12">
                    <div class="title-add">
                        <div class="pull-left">
                            <h3>Report Cash Flow</h3>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <form action="{{url('/realty/report-account-cash-flow-print')}}" method="POST">
            @csrf
            <div class="form-search">
                <div class="row">
                    <div class="col-xs-12 col-sm-6 col-md-6">
                        <div class="form-group">
                            <strong>Start Date</strong> {!! Form::text('start_date', null, array('placeholder' => 'Start Date','class' => 'form-control date_picker start_date','autocomplete' => 'off')) !!}
                        </div>
                    </div>
                    <div class="col-xs-12 col-sm-6 col-md-6">
                        <div class="form-group">
                            <strong>End Date</strong> {!! Form::text('end_date', null, array('placeholder' => 'End Date','class' => 'form-control date_picker end_date','autocomplete' => 'off')) !!}
                        </div>
                    </div>
                    <div class="col-xs-12 col-sm-6 col-md-6">
                        <div class="form-group">
                            <strong>Cash Flow</strong>
                            <select name="status" class="select2 form-control">
                                <option value="">Select Option</option>
                                <option value="1">Cash on Hand</option>
                                <option value="2">Cash in Bank</option>
                            </select>
                        </div>
                    </div>

                    <div class="col-xs-12 col-sm-12 col-md-12 text-right">
                        <button type="button" class="button_primary btn-sm search" onclick="searching()"><span class="fa fa-search"> Search</span></button>&nbsp
                        @can('CashFlow-Print')
                        <button type="submit" class="button_primary btn-sm search"><span class="fa fa-print"> Print</span></button>
                        @endcan
                    </div>
                </div>
            </div>
            {!! Form::close() !!}
            <div class="box-body table-responsive">
                <table id="datatable_accounting_capital_report" class="table table-striped table-bordered table-hover nowrap">
                    <thead>
                        <tr>
                            <th>No</th>
                            <th>Date</th>
                            <th>Type</th>
                            <th>Reference</th>
                            <th>User By</th>
                            <th>Description</th>
                            <th>Cash In</th>
                            <th>Cash Out</th>
                        </tr>
                    </thead>
                    <tbody class="tbody">
                    </tbody>
                    <tfoot class="tfoot">
                        <tr>
                            <td colspan="6" class="text-right">Total :</td>
                            <td></td>
                            <td></td>
                        </tr>
                        <tr>
                            <td colspan="6" class="text-right">Balance :</td>
                            <td colspan="2"></td>
                        </tr>
                    </tfoot>
                </table>
            </div>
    </div>

    @endsection @section('js')

    <script type="text/javascript" src="{{asset('assets/controller/report-cash-flow-administrator.js')}}"></script>
    <script type="text/javascript">

        function searching() {
            $('.tbody').empty();
            $('.tfoot').empty();
            var start_date = $('.start_date').val();
            var end_date = $('.end_date').val();
            var status = $('select[name="status"]').val();
            var table = '';
            var total_cashIN = 0;
            var total_cashOut = 0;
            $.ajax({
                method: 'POST',
                dataType: 'json',
                url: '/realty/report-accounting-cash-flow-lish',
                async: false,
                data: {
                    _token: '{{ csrf_token() }}',
                    start_date: start_date,
                    end_date: end_date,
                    status: status,
                },
                success: function(response) {
                    $.each(response, function(index, val) {

                        str = val.journal_paydate;
                        st2 = str.split("-");

                        yr = st2[0];
                        mo = st2[1];
                        dy = st2[2];
                        var new_date = dy+'-'+mo+'-'+yr;

                        table += "<tr class='remove'>" +
                            "<td>" + (index+1) + "</td>" +
                            "<td>" + new_date + "</td>" +
                            "<td>" + val.journal_type + "</td>" +
                            "<td>" + val.invoice + "</td>" +
                            "<td>" + val.user_by + "</td>" +
                            "<td>" + val.des + "</td>" +
                            "<td>$" + parseFloat(val.cash_in).toFixed(2).replace(/\d(?=(\d{3})+\.)/g, '$&,') + "</td>" +
                            "<td>$" + parseFloat(val.cash_out).toFixed(2).replace(/\d(?=(\d{3})+\.)/g, '$&,') + "</td>" +
                            "</tr>";
                            total_cashIN += parseFloat(val.cash_in);
                            total_cashOut += parseFloat(val.cash_out);
                    });
                    var tfoot = `<tr>
                                    <td colspan="6" class="text-right">Total :</td>
                                    <td>$ ${parseFloat(total_cashIN).toFixed(2).replace(/\d(?=(\d{3})+\.)/g, '$&,')}</td>
                                    <td>$ ${parseFloat(total_cashOut).toFixed(2).replace(/\d(?=(\d{3})+\.)/g, '$&,')}</td>
                                </tr>
                                <tr>
                                    <td colspan="6" class="text-right">Balance :</td>
                                    <td colspan="2">$ ${parseFloat(total_cashIN - total_cashOut).toFixed(2).replace(/\d(?=(\d{3})+\.)/g, '$&,') }</td>
                                </tr>`;
                    $('.dataTables_filter').hide();
                    $('.dataTables_info').hide();
                    $('.pagination').hide();
                    $('.dataTables_length').hide();
                    $('.tbody').append(table);
                    $('.tfoot').append(tfoot);

                },

            });
        }
    </script>
    @endsection