

    function get_Customer_for_account_statment(){

            var cust_typ  = getQueryVariable("typ");

            $.ajax({
                  url : 'models/crul_customer.php',
                  type : 'POST',
                  data : {type:'get_Customer',cust_typ:cust_typ},
                  dataType : 'json',
                  success : function(response) {
                    //console.log(response)
                      var attach ='<option value="" selected="selected">'+Translation('جميع الزبائن')+'</option>';
                      var aaa='';
                        for(var i=response.length-1 ; i >= 0;i--){
                          var tp  = response[i][2];
                          var tp1 = response[i][3];
                          attach = attach + '<option data-subtext=" Phone: '+tp+' || E-Mail: '+tp1+'" value="'+response[i][0]+'">'+response[i][1]+'</option>';
                          aaa = aaa + '<option  value="'+response[i][0]+'">'+response[i][1]+'</option>';

                        }
                        $("#a_s_customer_name").html(attach);
                        $('#a_s_customer_name').selectpicker('refresh');
                        
                  },
                  complete: function() {
                         //  me.data('requestRunning', false);
                  }

            }); // submit ajax

    }


    function get_inv_no_for_account_statment(cus_id=null){

            var cust_typ  = getQueryVariable("typ");
            
            $.ajax({
                  url : 'models/crul_account_statement.php',
                  type : 'POST',
                  data : {type:'get_inv',cus_id:cus_id,cust_typ:cust_typ},
                  dataType : 'json',
                  success : function(response) {
                    //console.log(response)
                      var attach ='<option value="" selected="selected">'+Translation('جميع الوصولات')+'</option>';
                      var aaa='';
                        for(var i=response.length-1 ; i >= 0;i--){
                          var tp  = response[i][2];
                          var tp1 = response[i][3];
                          attach = attach + '<option data-subtext=" '+Translation('الزبون')+' : '+tp+' || '+Translation('تاريخ الوصل')+' : '+tp1+'" value="'+response[i][0]+'">'+response[i][1]+'</option>';
                          aaa = aaa + '<option  value="'+response[i][0]+'">'+response[i][1]+'</option>';

                        }
                        $("#a_s_inv_no").html(attach);
                        $('#a_s_inv_no').selectpicker('refresh');
                        
                  },
                  complete: function() {
                         //  me.data('requestRunning', false);
                  }

            }); // submit ajax

    }

   

    function load_account_statment(cust_id = null,inv_id=null,  inv_date =null, inv_date_end=null, date_operation =null,close_modal=false){
      
          
          var cust_typ  = getQueryVariable("typ");
          
          $.ajax({
                  url : 'models/crul_account_statement.php',
                  type : 'POST',
                  data : {type:'account_statment',cust_id:cust_id,inv_id:inv_id,inv_date:inv_date,inv_date_end:inv_date_end,date_operation:date_operation,cust_typ:cust_typ},
                  dataType : 'json',
                  success : function(response) {
                   
                        if(response==0){
                         
                          noty({
                            width: 200,
                            text: Translation("لا يوجد كشف حساب"),
                            type: 'error',
                            dismissQueue: true,
                            timeout: 3000,
                            layout: 'topRight',
                          });

                        }else{
                          
                          var attach_raw        ='';
                          var attach_curt_m     ='';
                          var attach_curt_s     ='';
                          var attach_other_s    ='';
                          var attach_other_m    ='';
                          var attach_other_raw  ='';
                          var attach_withdraw   ='';

                          var sum_cost_raw      =0;
                          var sum_paid_raw      =0;
                          var sum_remain_raw    =0;

                          var sum_cost_S        =0;
                          var sum_paid_S        =0;
                          var sum_remain_S      =0;

                          var sum_cost_M        =0;
                          var sum_paid_M        =0;
                          var sum_remain_M      =0;

                          var sum_cost_W        =0;
                          var sum_paid_W        =0;
                          var sum_remain_W      =0;
                          var sum_total_W_no    =0;
                    
                          for(var i=0 ; i < response.sell.length;i++){

                            // for tfooter summation 
                            sum_cost_raw    = +sum_cost_raw + +response.sell[i]['total_cost_raw'];
                            sum_paid_raw    = +sum_paid_raw + +response.sell[i]['total_Paid_Raw'];
                            sum_remain_raw  = +sum_remain_raw + +response.sell[i]['total_remain_raw'];

                            sum_cost_S      = +sum_cost_S + +response.sell[i]['total_cost_single'];
                            sum_paid_S      = +sum_paid_S + +response.sell[i]['total_Paid_curtain_single'];
                            sum_remain_S    = +sum_remain_S + +response.sell[i]['total_remain_s'];

                            sum_cost_M      = +sum_cost_M + +response.sell[i]['total_cost_plural'];
                            sum_paid_M      = +sum_paid_M + +response.sell[i]['total_Paid_curtain_plural'];
                            sum_remain_M    = +sum_remain_M + +response.sell[i]['total_remain_m'];
                            // end summation


                            attach_raw = attach_raw + `
                                                        <tr>
                                                          <td>`+(i+1)+`</td>
                                                          <td>`+response.sell[i]['item_name']+`</td>
                                                          <td>`+response.sell[i]['Quantity_Raw']+`</td>
                                                          <td>`+currency_format_display(response.sell[i]['total_cost_raw'])+`</td>
                                                          <td>`+currency_format_display(response.sell[i]['total_Paid_Raw'])+`</td>
                                                          <td>`+currency_format_display(response.sell[i]['total_remain_raw'])+`</td>
                                                        </tr>
                                                          `;

                            attach_curt_m = attach_curt_m + `
                                                        <tr>
                                                          <td>`+(i+1)+`</td>
                                                          <td>`+response.sell[i]['item_name']+`</td>
                                                          <td>`+response.sell[i]['Quantity_plural']+`</td>
                                                          <td>`+currency_format_display(response.sell[i]['total_cost_plural'])+`</td>
                                                          <td>`+currency_format_display(response.sell[i]['total_Paid_curtain_plural'])+`</td>
                                                          <td>`+currency_format_display(response.sell[i]['total_remain_m'])+`</td>
                                                        </tr>
                                                          `;

                            attach_curt_s = attach_curt_s + `
                                                        <tr>
                                                          <td>`+(i+1)+`</td>
                                                          <td>`+response.sell[i]['item_name']+`</td>
                                                          <td>`+response.sell[i]['quantity_single']+`</td>
                                                          <td>`+currency_format_display(response.sell[i]['total_cost_single'])+`</td>
                                                          <td>`+currency_format_display(response.sell[i]['total_Paid_curtain_single'])+`</td>
                                                          <td>`+currency_format_display(response.sell[i]['total_remain_s'])+`</td>
                                                        </tr>
                                                          `;




                          } // for i

                            // for tfooter summation 
                            sum_cost_raw    = +sum_cost_raw + +response.sell[0]['total_cost_other_raw'];
                            sum_paid_raw    = +sum_paid_raw + +response.sell[0]['total_other_Paid_Raw'];
                            sum_remain_raw  = +sum_remain_raw + +response.sell[0]['total_remain_other_raw'];

                            sum_cost_S      = +sum_cost_S + +response.sell[0]['total_cost_other_S'];
                            sum_paid_S      = +sum_paid_S + +response.sell[0]['total_other_Paid_S'];
                            sum_remain_S    = +sum_remain_S + +response.sell[0]['total_remain_other_s'];

                            sum_cost_M      = +sum_cost_M + +response.sell[0]['total_cost_other_M'];
                            sum_paid_M      = +sum_paid_M + +response.sell[0]['total_other_Paid_M'];
                            sum_remain_M    = +sum_remain_M + +response.sell[0]['total_remain_other_m'];
                            // end summation

                            attach_other_s = attach_other_s + `
                                                        <tr>
                                                          <td>`+(response.sell.length+1)+`</td>
                                                          <td>`+Translation('مواد اخرى')+`</td>
                                                          <td>`+response.sell[0]['Quantity_other_S']+`</td>
                                                          <td>`+currency_format_display(response.sell[0]['total_cost_other_S'])+`</td>
                                                          <td>`+currency_format_display(response.sell[0]['total_other_Paid_S'])+`</td>
                                                          <td>`+currency_format_display(response.sell[0]['total_remain_other_s'])+`</td>
                                                        </tr>
                                                          `;
                            attach_other_m = attach_other_m + `
                                                        <tr>
                                                          <td>`+(response.sell.length+1)+`</td>
                                                          <td>`+Translation('مواد اخرى')+`</td>
                                                          <td>`+response.sell[0]['Quantity_other_M']+`</td>
                                                          <td>`+currency_format_display(response.sell[0]['total_cost_other_M'])+`</td>
                                                          <td>`+currency_format_display(response.sell[0]['total_other_Paid_M'])+`</td>
                                                          <td>`+currency_format_display(response.sell[0]['total_remain_other_m'])+`</td>
                                                        </tr>
                                                          `;

                            attach_other_raw = attach_other_raw + `
                                                        <tr>
                                                          <td>`+(response.sell.length+1)+`</td>
                                                          <td>`+Translation('مواد اخرى')+`</td>
                                                          <td>`+response.sell[0]['Quantity_other_Raw']+`</td>
                                                          <td>`+currency_format_display(response.sell[0]['total_cost_other_raw'])+`</td>
                                                          <td>`+currency_format_display(response.sell[0]['total_other_Paid_Raw'])+`</td>
                                                          <td>`+currency_format_display(response.sell[0]['total_remain_other_raw'])+`</td>
                                                        </tr>
                                                          `;

                            // for withdraw
                            for(var i=0 ; i < response.withdraw.length;i++){

                              

                              sum_cost_W      = +sum_cost_W + +response.withdraw[i]['total_withdraw_price'];
                              sum_paid_W      = +sum_paid_W + +response.withdraw[i]['total_withdraw_paid'];
                              sum_remain_W    = +sum_remain_W + +response.withdraw[i]['total_withdraw_remaind'];
                              
                              sum_total_W_no  = +sum_total_W_no + +response.withdraw[i]['total_withdraw_no'];

                              attach_withdraw = attach_withdraw + `
                                                          <tr>
                                                            <td>`+(i+1)+`</td>
                                                            <td>`+response.withdraw[i]['customer_full_name']+`</td>
                                                            <td>`+response.withdraw[i]['total_withdraw_no']+`</td>
                                                            <td>`+currency_format_display(response.withdraw[i]['total_withdraw_price'])+`</td>
                                                            <td>`+currency_format_display(response.withdraw[i]['total_withdraw_paid'])+`</td>
                                                            <td>`+currency_format_display(response.withdraw[i]['total_withdraw_remaind'])+`</td>
                                                          </tr>
                                                            `;
                            }
                            // end withdraw

                            // Tabs according to singular or plural
                            var Tabs_ = "";
                            if(cust_typ == 1){ // it mean its sigle and we should hide curtain plural and raw
                              Tabs_ = `
                                      
                                      <li class="active"><a href="#curtain_s" data-toggle="tab"><i class="icon-bookmark2 position-left"></i> `+Translation('ستائر مفرد')+` <label class="label label-rounded bg-teal-300" title="`+Translation('عدد الوصولات')+`">`+response.sell[0]['No_of_s_inv']+`</label></a></li>
                                      <li ><a href="#withdraw_tab" data-toggle="tab"><i class="icon-grab  position-left"></i>`+Translation('القروض')+`   <label class="label label-rounded bg-danger-300" title="`+Translation('عدد الوصولات')+`">`+sum_total_W_no+`</label></a></li>
                                           
                                        `;
                            }else{ // it mean its plural and we should hide curtain single
                              Tabs_ = `
                                      <li class="active"><a href="#raw" data-toggle="tab"><i class="icon-color-sampler position-left"></i>`+Translation('مواد جملة')+`   <label class="label label-rounded bg-danger-300" title="`+Translation('عدد الوصولات')+`">`+response.sell[0]['No_of_raw_inv']+`</label></a></li>
                                      <li><a href="#curtain_m" data-toggle="tab"><i class="icon-bookmarks position-left"></i> `+Translation('ستائر جملة')+` <label class="label label-rounded bg-info-300" title="`+Translation('عدد الوصولات')+`">`+response.sell[0]['No_of_m_inv']+`</label></a></li>
                                      <li><a href="#withdraw_tab" data-toggle="tab"><i class="icon-grab position-left"></i> `+Translation('القروض')+` <label class="label label-rounded bg-teal-300" title="`+Translation('عدد الوصولات')+`">`+sum_total_W_no+`</label></a></li>
                                                          
                                        `;
                            }

                           var attach =`
                                        <!-- Tabs widget -->
                                        
                                            <div class="tabbable">
                                              <ul class="nav nav-lg nav-tabs nav-tabs-solid nav-tabs-component nav-justified">
                                                `+Tabs_+`
                                              </ul>

                                              <div class="tab-content">
                                                <div class="tab-pane active" id="raw">
                                                      <div class="table-responsive">
                                                        <table class="table table-bordered table-striped table-hover bg-info-700">
                                                          <thead>
                                                            <tr>
                                                              <th>#</th>
                                                              <th>`+Translation('الصنف')+`</th>
                                                              <th>`+Translation('الكمية')+`</th>
                                                              <th>`+Translation('المبلغ الكلي')+`</th>
                                                              <th>`+Translation('المبلغ المدفوع')+`</th>
                                                              <th>`+Translation('المبلغ المتبقي')+`</th>
                                                            </tr>
                                                          </thead>
                                                          <tbody>
                                                      `+attach_raw+ attach_other_raw+`
                                                          </tbody>
                                                          <tfoot>
                                                            <tr class="bg-pink-400">
                                                              <th><i class="icon-sigma"></i></th>
                                                              <th>`+Translation('المجموع')+`</th>
                                                              <th></th>
                                                              <th>`+currency_format_display(sum_cost_raw)+`</th>
                                                              <th>`+currency_format_display(sum_paid_raw)+`</th>
                                                              <th>`+currency_format_display(sum_remain_raw)+`</th>
                                                            </tr>
                                                          </tfoot>
                                                        </table>
                                                      </div>
                                                </div>

                                                <div class="tab-pane" id="curtain_m">
                                                      <div class="table-responsive">
                                                        <table class="table table-bordered table-striped table-hover bg-info-700">
                                                          <thead>
                                                            <tr>
                                                              <th>#</th>
                                                              <th>`+Translation('الصنف')+`</th>
                                                              <th>`+Translation('الكمية')+`</th>
                                                              <th>`+Translation('المبلغ الكلي')+`</th>
                                                              <th>`+Translation('المبلغ المدفوع')+`</th>
                                                              <th>`+Translation('المبلغ المتبقي')+`</th>
                                                            </tr>
                                                          </thead>
                                                          <tbody>
                                                      `+attach_curt_m+attach_other_m+`
                                                          </tbody>
                                                          <tfoot>
                                                            <tr class="bg-pink-400">
                                                              <th><i class="icon-sigma"></i></th>
                                                              <th>`+Translation('المجموع')+`</th>
                                                              <th></th>
                                                              <th>`+currency_format_display(sum_cost_M)+`</th>
                                                              <th>`+currency_format_display(sum_paid_M)+`</th>
                                                              <th>`+currency_format_display(sum_remain_M)+`</th>
                                                            </tr>
                                                          </tfoot>
                                                        </table>
                                                      </div>
                                                </div>

                                                <div class="tab-pane" id="curtain_s">
                                                      <div class="table-responsive">
                                                        <table class="table table-bordered table-striped table-hover bg-info-700">
                                                          <thead>
                                                            <tr>
                                                              <th>#</th>
                                                              <th>`+Translation('الصنف')+`</th>
                                                              <th>`+Translation('الكمية')+`</th>
                                                              <th>`+Translation('المبلغ الكلي')+`</th>
                                                              <th>`+Translation('المبلغ المدفوع')+`</th>
                                                              <th>`+Translation('المبلغ المتبقي')+`</th>
                                                            </tr>
                                                          </thead>
                                                          <tbody>
                                                      `+attach_curt_s+attach_other_s+`
                                                          </tbody>
                                                          <tfoot>
                                                            <tr class="bg-pink-400">
                                                              <th><i class="icon-sigma"></i></th>
                                                              <th>`+Translation('المجموع')+`</th>
                                                              <th></th>
                                                              <th>`+currency_format_display(sum_cost_S)+`</th>
                                                              <th>`+currency_format_display(sum_paid_S)+`</th>
                                                              <th>`+currency_format_display(sum_remain_S)+`</th>
                                                            </tr>
                                                          </tfoot>
                                                        </table>
                                                      </div>
                                                </div>

                                                <div class="tab-pane" id="withdraw_tab">
                                                      <div class="table-responsive">
                                                        <table class="table table-bordered table-striped table-hover bg-info-700">
                                                          <thead>
                                                            <tr>
                                                              <th>#</th>
                                                              <th>`+Translation('اسم الزبون')+`</th>
                                                              <th>`+Translation('عدد القروض الكلي')+`</th>
                                                              <th>`+Translation('المبلغ الكلي')+`</th>
                                                              <th>`+Translation('المبلغ المدفوع')+`</th>
                                                              <th>`+Translation('المبلغ المتبقي')+`</th>
                                                            </tr>
                                                          </thead>
                                                          <tbody>
                                                      `+attach_withdraw+`
                                                          </tbody>
                                                          <tfoot>
                                                            <tr class="bg-pink-400">
                                                              <th><i class="icon-sigma"></i></th>
                                                              <th>`+Translation('المجموع')+`</th>
                                                              <th>`+sum_total_W_no+`</th>
                                                              <th>`+currency_format_display(sum_cost_W)+`</th>
                                                              <th>`+currency_format_display(sum_paid_W)+`</th>
                                                              <th>`+currency_format_display(sum_remain_W)+`</th>
                                                            </tr>
                                                          </tfoot>
                                                        </table>
                                                      </div>
                                                </div>

                                              </div>
                                            </div>
                                         
                                        <!-- /tabs widget -->
                          `;

                          $("#account_statment_modal_div").html(attach);
                          if(close_modal == false){
                            $('#account_statment_modal').modal('toggle');
                          }
                          
                          
                        }
                  },
                  complete: function() {

                    if(cust_id !=null && cust_id > 0){
                      
                      $("#a_s_customer_name").val(cust_id).selectpicker('refresh');
                      get_inv_no_for_account_statment(cust_id);
                    }

                    if(inv_id !=null && inv_id > 0){
                      $("#a_s_inv_no").val(inv_id).selectpicker('refresh');
                    }
                    
                  }

            }); // submit ajax
      // end loading price
    }

   


$(document).ready(function() {
  
   
    get_Customer_for_account_statment();
    get_inv_no_for_account_statment();
    
    

    $("#a_s_customer_name").on('change',function(e){
        e.preventDefault();
        
        var cust_id             = $("#a_s_customer_name").val();
        

        get_inv_no_for_account_statment(cust_id);
        

    });

    // submit account statement filter

    $("#a_s_filter_btn_modal").on('click',function(e){
        e.preventDefault();
        
        var cust_id             = $("#a_s_customer_name").val();
        var inv_id              = $("#a_s_inv_no").val();
        var date_operation      = $("#a_s_date_operation").val();
        var date1               = $("#a_s_date").val();
        var date2               = $("#a_s_date2").val();

        load_account_statment(cust_id,inv_id,date1,date2,date_operation,'on');
        

    });
   


});







$(function() {
//var singleSelect = $('.datatable-column-search-inputs').DataTable();
 
  $('.pickadate-selectors').pickadate({ 
        // singleDatePicker: true,
        selectYears: true,
        selectMonths: true,
        format: "yyyy-mm-dd"
    });


});



