@extends('client.layouts.app')
@section('title','تقرير المالية ')
@section('content')
<div class="app-content content ">
      <div class="content-overlay"></div>
      <div class="header-navbar-shadow"></div>
      <div class="content-wrapper container-xxl p-0">
        <div class="content-header row">
        </div>
    <div class="content-body">
	<!-- Dashboard Ecommerce Starts -->
		<section id="dashboard-ecommerce">
		<div class="content-header row">
          <div class="content-header-left col-md-9 col-12 mb-2">
            <div class="row breadcrumbs-top">
              <div class="col-12">
                <h2 class="content-header-title float-start mb-0">المالية</h2>
                <div class="breadcrumb-wrapper">
                  <ol class="breadcrumb">
                    <li class="breadcrumb-item"><a href="{{route('client.home')}}">لوحة التحكم</a>
                    </li>
                    <li class="breadcrumb-item"><a href="{{route('client.reports')}}">التقارير </a>

                    <li class="breadcrumb-item active">المالية 
                    </li>
                  </ol>
                </div>
              </div>
            </div>
          </div>
          <div class="content-header-right text-md-end col-md-3 col-12 d-md-block d-none">
            <div class="mb-1 breadcrumb-right">
			
              <div class="dropdown">
               				
            </div>
          </div>
        </div>
            <div class="col-xl-12 col-md-12 col-12">
                <div class="card">
                    <form id="search" name="date" >
                    <div class="card-body row" align="center">
                        <div class="col-lg-2 col-sm-6-md-6"></div>

                        <div class="col-lg-6 col-sm-6-md-6 ">


                            <div class= "row">
                        <div class="col-lg-6 col-sm-6-md-6 mb-1">
                            <center> <label class="form-label" style="font-weight:bold " > من تاريخ</label></center>
                   <input type="date" name="from_date" id="from_date" class="form-control" >
                       
                </div>
                <div class="col-lg-6 col-sm-6-md-6 mb-1">
                    <center> <label class="form-label" style="font-weight:bold " > إلي تاريخ</label></center>
                    <input type="date" name="to_date" id="to_date" class="form-control" >
                   </div>
                            </div>
                </div>
                        <div class="col-lg-4 col-sm-6-md-6">
                            <center> <label class="form-label" style="font-weight:bold " ></label></center>
            
                            <button type="submit" class="btn btn-success me-1">بحث</button>
            
                         </div>
                      </div>
                    </form>
                </div>
              </div>

      <div class="col-xl-12 col-md-12 col-12" id="data-card" style="display: none">
          <div class="card card-statistics">

        <div class="card-body statistics-body">
          <div class="row">

            <div class="card-body" align="center" id="loader">
                <div class="spinner-border text-primary" style="width: 5rem; height: 5rem" role="status">
                </div>
              </div> 

                <div id="header" style="display: none">
              <center>
                  <h3 id="header-title"></h3>
                  <br>
              </center>
                </div>
                        <div class="card-datatable">
                            <div class="table-responsive">
                                <table class="table table-condensed table-striped table-bordered text-center table-hover " id="safeInfo">
                                    <thead>
                                    </thead>
                                    <tbody class="tbody">
                                    </tbody>
                                </table>
                            </div>
                            
                        </div>

                        <div class="divider"></div>

                        <div class="divider divider-primary table">
                            <div class="divider-text"> <h4 >   جميع العمليات في الخزن   </h4></div>
                            <br>
                        </div>

                      <div class="card-datatable">
                        <div class="table-responsive">
                            <table class="table table-condensed table-striped table-bordered text-center table-hover" id="safe_transaction">
                                <thead>
                                <tr>
                                    <th>م</th>
                                    <th>العملية</th>
                                    <th>المبلغ</th>
                                    <th>التاريخ </th>
                                    <th>ملاحظات </th>
                                    <th> المسؤول</th>
                                </tr>
                                </thead>
                                <tbody class="tbody">
                                    
                                </tbody>
                            </table>
                        </div>
                    </div>
                    <div class="divider"></div>

                    <div class="divider divider-primary table">
                        <div class="divider-text"> <h4 >   جميع العمليات في البنوك   </h4></div>
                        <br>
                    </div>

                  <div class="card-datatable">
                    <div class="table-responsive">
                        <table class="table table-condensed table-striped table-bordered text-center table-hover" id="bank_transaction">
                            <thead>
                            <tr>
                                <th>م</th>
                                <th>العملية</th>
                                <th>المبلغ</th>
                                <th>التاريخ </th>
                                <th>ملاحظات </th>
                                <th> المسؤول</th>
                            </tr>
                            </thead>
                            <tbody class="tbody">
                                
                            </tbody>
                        </table>
                    </div>
                </div>
               
          </div>
        </div>
      </div>
    </div>
	</section>

	</div>
      </div>
    </div>
@endsection
@section('script')
<script>

$("#search").on('submit',(function(e) {
e.preventDefault();
  filter();
}));
  var table = $('.table') ;
  function filter() {

var safe_id = $('#safe_id').val();
var from_date = $('#from_date').val();
var to_date = $('#to_date').val();

if(from_date == '' || to_date == ''){$('#header-title').text(' جميع المعاملات المالية ')}else{$('#header-title').text(' جميع المعاملات المالية من تاريخ '+from_date+' إلي تاريخ '+to_date )}
$.ajax({
    url: "{{route('client.report22.post')}}",
    type: "POST",
    data: {
        "_token": "{{ csrf_token() }}",
        "from_date": $("#from_date").val(),
        "to_date" : $("#to_date").val()
    },
    beforeSend : function (){table.hide(); $('#header').hide(); $('#data-card').show();$('#loader').show(); var ifConnected = window.navigator.onLine;if (!ifConnected) {toastr.info("يتم الآن إعادة الإتصال","لا يوجد إتصال بالإنترنت",{positionClass: "toast-top-right",tapToDismiss: !1,timeOut: 2e3,rtl: $("html").attr("data-textdirection")});}},
    success: function (data) {
      var employees_cashs = 0 ; 
      var employees_bank_cashs = 0 ;
        var cashs = 0 ; 
        var suppliersAdvance = 0 ;
        var bankSafeTransfer = 0
        var capitals = 0
        var safeBankTransfer = 0
        var expenses_safe = 0 ; 
        var revenues_safe = 0 ;
        var buy_cashs = 0 ; 
        var clientAdvance = 0 ; 
        var bankCash = 0 ;
        var bank_safe_transfers = 0 ;
        var safe_bank_transfers = 0 ;

        var clientAdvanceBank = 0 ;
        var suppliersAdvanceBank = 0 ;
        var expenses_bank = 0 ;
        var revenues_banks = 0 ;
        var bank_buy_cash = 0 ;
        var safe_to_bank = 0 ;
        var bank_to_safe = 0 ;
        var bank_cash = 0 ;
        var bankIn = 0 ;
        var bankOut = 0 ;


       var counter_safe = 1;
       var counter_bank = 1;
        $('#loader').hide();
        table.show();
        $('.tbody').empty();
        $('#header').show();
        // $('#safeInfo').append(
        //         '<tr>'+
        //         '<td>'+data.safe_k.safe_name+'</td>'+
        //         '<td>'+formatMoney(parseFloat(data.safe_k.balance))+'</td>'+
        //         '</tr>'
        //     );

        $.each( data.capitals, function( key, value ) {
          capitals +=  parseFloat(value.amount);         

            $('#safe_transaction').append(
                '<tr>'+
                '<td>'+(counter_safe++)+'</td>'+
                '<td>رأس مال</td>'+
                '<td>'+formatMoney(parseFloat(value.amount))+'</td>'+
                '<td>'+(value.created_at.split('T')[0])+'</td>'+
                '<td>'+""+'</td>'+
                '<td>'+value.client.name+'</td>'+

                '</tr>'
            );
        });   

        $.each( data.bank_safe_transfers, function( key, value ) {
          bank_safe_transfers +=  parseFloat(value.amount);         
            $('#safe_transaction').append(
                '<tr>'+
                '<td>'+(counter_safe++)+'</td>'+
                '<td> تحويل من بنك : '+value.bank.bank_name+' إلي خزنة : '+value.safe.safe_name+'</td>'+
                '<td>'+formatMoney(parseFloat(value.amount))+'</td>'+
                '<td>'+(value.created_at.split('T')[0])+'</td>'+
                '<td>'+value.reason+'</td>'+
                '<td>'+value.client.name+'</td>'+
                '</tr>'
            );
        });


        $.each( data.safe_bank_transfers, function( key, value ) {
          safe_bank_transfers +=  parseFloat(value.amount); 

            $('#safe_transaction').append(
                '<tr>'+
                '<td>'+(counter_safe++)+'</td>'+
                '<td>تحويل من خزنة : '+value.safe.safe_name+' إلي بنك :'+value.bank.bank_name+'</td>'+
                '<td>'+formatMoney(parseFloat(value.amount))+'</td>'+
                '<td>'+(value.created_at.split('T')[0])+'</td>'+
                '<td>'+value.reason+'</td>'+
                '<td>'+value.client.name+'</td>'+
                '</tr>'
            );

        });

        $.each( data.expenses_safe, function( key, value ) {
          var notes = '';
          var type = '';
            
          if(value.type == 'إيراد'){
            var type = 'إيرادات'
            revenues_safe +=  parseFloat(value.amount);   
          }else if(value.type == 'مصروف'){
          expenses_safe +=  parseFloat(Math.abs(value.amount));   
            var type = 'مصروفات'
          }
          if(value.notes == null){notes = ""}else{notes = value.notes}
            $('#safe_transaction').append(
                '<tr>'+
                '<td>'+(counter_safe++)+'</td>'+
                '<td> '+type+' : '+(value.expense_details)+'</td>'+
                '<td>'+formatMoney(parseFloat(Math.abs(value.amount)))+'</td>'+
                '<td>'+(value.created_at.split('T')[0])+'</td>'+
                '<td>'+notes+'</td>'+
                '<td>'+value.client.name+'</td>'+
                '</tr>'
            );

        });


        $.each( data.employees_cashs, function( key, value ) {
          employees_cashs +=  parseFloat(value.amount);         
          if(value.notes == null){notes = ""}else{notes = value.notes} 
          if(value.amount > 0){var type = 'دفعة إلي موظف '}else{var type = 'سلفة إلي موظف '}
            $('#safe_transaction').append(
                '<tr>'+
                '<td>'+(counter_safe++)+'</td>'+
                '<td>'+type+' :  '+(value.employee.name)+'</td>'+
                '<td>'+formatMoney(parseFloat(value.amount))+'</td>'+
                '<td>'+(value.date)+'</td>'+
                '<td>'+notes+'</td>'+
                '<td>'+value.client.name+'</td>'+
                '</tr>'
            );
        });

        $.each( data.cashs, function( key, value ) {
          if(value.outer_client_id !== null ){var outer_client =  value.outer_client.client_name}else{var outer_client =  'عميل نقدي' ;}
          
          if(value.amount > 0){ if(value.salebill == null ){ var type = 'دفعة من عميل '}else{var type = 'فاتورة مبيعات ' +'('+value.salebill.sale_bill_number+')'}}else{var type = 'سلفة إلي عميل '; }     
         
          if(value.amount > 0){
          cashs +=  Math.abs(parseFloat(value.amount)); 
            }else{
          clientAdvance +=  Math.abs(parseFloat(value.amount)); 
            }

          if(value.notes == null || value.notes == null){notes = ""}else{notes = value.notes} 
          $('#safe_transaction').append(
                '<tr>'+
                '<td>'+(counter_safe++ )+'</td>'+
                '<td>'+type+'<b> <br> عميل :   '+(outer_client)+'</b></td>'+
                '<td>'+(formatMoney(Math.abs(parseFloat(value.amount))))+'</td>'+
                '<td>'+value.date+'</td>'+
                '<td>'+notes+'</td>'+
                '<td>'+value.client.name+'</td>'+
                '</tr>'
            );
        });


        $.each( data.buy_cashs, function( key, value ) {
          if(value.amount > 0){
            buy_cashs +=  Math.abs(parseFloat(value.amount)); 
            }else{
            suppliersAdvance +=  Math.abs(parseFloat(value.amount)); 
            }

                 
          if(value.amount > 0){if(value.buybill == null ){ var type = 'دفعة إلي مورد '}else{var type = 'فاتورة مشتريات ' +'('+value.buybill.buy_bill_number+')'}}else{var type = 'سلفة من مورد '}     
          if(value.notes == null || value.notes == null){notes = ""}else{notes = value.notes} 

            $('#safe_transaction').append(
                '<tr>'+
                '<td>'+(counter_safe++ )+'</td>'+
                '<td>'+type+'<b> <br> مورد :   '+(value.supplier.supplier_name)+'</b></td>'+
                '<td>'+(formatMoney(Math.abs(parseFloat(value.amount))))+'</td>'+
                '<td>'+value.date+'</td>'+
                '<td>'+notes+'</td>'+
                '<td>'+value.client.name+'</td>'+
                '</tr>'
            );
        });

        /////////////////////////////// banks ////////////////////


        $.each( data.bank_safe_transfers, function( key, value ) {
          bank_safe_transfers +=  parseFloat(value.amount);         
            $('#bank_transaction').append(
                '<tr>'+
                '<td>'+(counter_bank++)+'</td>'+
                '<td> تحويل من بنك : '+value.bank.bank_name+' إلي خزنة : '+value.safe.safe_name+'</td>'+
                '<td>'+formatMoney(parseFloat(value.amount))+'</td>'+
                '<td>'+(value.created_at.split('T')[0])+'</td>'+
                '<td>'+value.reason+'</td>'+
                '<td>'+value.client.name+'</td>'+
                '</tr>'
            );
        });

        $.each( data.safe_bank_transfers, function( key, value ) {
          bank_to_safe +=  parseFloat(value.amount); 

            $('#bank_transaction').append(
                '<tr>'+
                '<td>'+(counter_bank++)+'</td>'+
                '<td>تحويل إلي خزنة :  '+value.safe.safe_name+' إلي بنك : '+value.bank.bank_name+'</td>'+
                '<td>'+formatMoney(parseFloat(value.amount))+'</td>'+
                '<td>'+(value.created_at.split('T')[0])+'</td>'+
                '<td>'+value.reason+'</td>'+
                '<td>'+value.client.name+'</td>'+
                '</tr>'
            );

        });

        $.each( data.employees_bank_cashs, function( key, value ) {

          employees_bank_cashs +=  parseFloat(value.amount);         
          if(value.notes == null){notes = ""}else{notes = value.notes} 
          if(value.amount > 0){var type = 'دفعة إلي موظف '}else{var type = 'سلفة إلي موظف '}
            $('#bank_transaction').append(
                '<tr>'+
                '<td>'+(counter_bank++)+'</td>'+
                '<td>'+type+' :  '+(value.employee.name)+'</td>'+
                '<td>'+formatMoney(parseFloat(value.amount))+'</td>'+
                '<td>'+(value.date)+'</td>'+
                '<td>'+notes+'</td>'+
                '<td>'+value.client.name+'</td>'+
                '</tr>'
            );
        });

        $.each( data.expenses_bank, function( key, value ) {

          if(value.type == 'إيراد'){
            var type = 'إيرادات'
            revenues_banks +=  parseFloat((value.amount));   
          }else if(value.type == 'مصروف'){
          expenses_bank +=  parseFloat(Math.abs(value.amount));   
            var type = 'مصروفات'
          }        

            $('#bank_transaction').append(
                '<tr>'+
                '<td>'+(counter_bank++)+'</td>'+
                '<td> '+type+' : '+(value.expense_details)+'</td>'+
                '<td>'+formatMoney(parseFloat(Math.abs(value.amount)))+'</td>'+
                '<td>'+(value.created_at.split('T')[0])+'</td>'+
                '<td>'+notes+'</td>'+
                '<td>'+value.client.name+'</td>'+
                '</tr>'
            );
        });


        $.each( data.bank_cash, function( key, value ) {
          if(value.outer_client_id !== null ){var outer_client =  value.outer_client.client_name}else{var outer_client =  'عميل نقدي' ;}
          
          if(value.amount > 0){ if(value.bill_id == null ){ var type = 'دفعة من عميل '}else{var type = 'فاتورة مبيعات ' +'('+value.bill_id+')'}}else{var type = 'سلفة إلي عميل '; }     
         
          if(value.amount > 0){
            bank_cash +=  Math.abs(parseFloat(value.amount)); 
            }else{
          clientAdvanceBank +=  Math.abs(parseFloat(value.amount)); 
            }

          if(value.notes == null || value.notes == null){notes = ""}else{notes = value.notes} 
          $('#bank_transaction').append(
                '<tr>'+
                '<td>'+(counter_bank++ )+'</td>'+
                '<td>'+type+'<b> <br> عميل :   '+(outer_client)+'</b></td>'+
                '<td>'+(formatMoney(Math.abs(parseFloat(value.amount))))+'</td>'+
                '<td>'+value.date+'</td>'+
                '<td>'+notes+'</td>'+
                '<td>'+value.client.name+'</td>'+
                '</tr>'
            );
        });



        $.each( data.bank_buy_cash, function( key, value ) {
          if(value.amount > 0){
            bank_buy_cash +=  Math.abs(parseFloat(value.amount)); 
            }else{
            suppliersAdvanceBank +=  Math.abs(parseFloat(value.amount)); 
            }

                 
          if(value.amount > 0){if(value.bill_id == null ){ var type = 'دفعة إلي مورد '}else{var type = 'فاتورة مشتريات ' +'('+value.bill_id+')'}}else{var type = 'سلفة من مورد '}     
          if(value.notes == null || value.notes == null){notes = ""}else{notes = value.notes} 

            $('#bank_transaction').append(
                '<tr>'+
                '<td>'+(counter_bank++ )+'</td>'+
                '<td>'+type+'<b> <br> مورد :   '+(value.supplier.supplier_name)+'</b></td>'+
                '<td>'+(formatMoney(Math.abs(parseFloat(value.amount))))+'</td>'+
                '<td>'+value.date+'</td>'+
                '<td>'+notes+'</td>'+
                '<td>'+value.client.name+'</td>'+
                '</tr>'
            );
        });

        /////////////////////////// END ////////////////////////////


        var safeIn = cashs+bankSafeTransfer+capitals+suppliersAdvance+bank_safe_transfers+revenues_safe ;

        var safeOut = buy_cashs+safeBankTransfer+expenses_safe+employees_cashs+clientAdvance+safe_bank_transfers;

        var bankIn = bank_cash+safe_to_bank+suppliersAdvanceBank+revenues_banks;
        var bankOut = bank_buy_cash+expenses_bank+bank_to_safe+clientAdvanceBank+employees_bank_cashs;

        $('#safeInfo').append('<tr><td>إجمالي الداخل إلي الخزن</td><td>'+formatMoney(safeIn)+' {{$company->currency}} </td></tr>');
        $('#safeInfo').append('<tr><td>إجمالي الخارج من الخزن</td><td>'+formatMoney(safeOut)+' {{$company->currency}} </td></tr>');
        $('#safeInfo').append('<tr><td>إجمالي المتبقي في الخزن</td><td>'+formatMoney(safeIn-safeOut)+' {{$company->currency}} </td></tr>');
        
        $('#safeInfo').append('<tr><td>إجمالي الداخل إلي البنوك</td><td>'+formatMoney(bankIn)+' {{$company->currency}} </td></tr>');
        $('#safeInfo').append('<tr><td>إجمالي الخارج إلي البنوك</td><td>'+formatMoney(bankOut)+' {{$company->currency}} </td></tr>');
        $('#safeInfo').append('<tr><td>إجمالي المتبقي في البنوك</td><td>'+formatMoney(bankIn-bankOut)+' {{$company->currency}} </td></tr>');

         if(data.buy_cashs == '' && data.cashs == '' && data.employees_cashs == '' && data.capitals == '' && data.bank_safe_transfers == '' && data.safe_bank_transfers == '' && data.expenses_safe == ''  ){ $('#safe_transaction').append('<tr style="font-weight:bold;">'+'<td colspan="6">لا يوجد أي معاملات  </td>'+'</tr>')}
         if(data.bank_buy_cash == '' && data.bank_cash == ''&& data.employees_bank_cashs == '' && data.bank_safe_transfers == '' && data.safe_bank_transfers == '' && data.expenses_bank == ''   ){ $('#bank_transaction').append('<tr style="font-weight:bold;">'+'<td colspan="6">لا يوجد أي معاملات  </td>'+'</tr>')}


          },
   error: function (data) {
    }
});
};


var selectForm = $('#search');

if (selectForm) {
    selectForm.validate({

        rules: {
      'safe_id': {
        required: true,
      }
    }
  });
}
   </script>
@endsection
