var table ;
    

    var HTML_VIEW_PAY = '';
    var print_page =  `
                        <script>
                          window.print();
                          window.onload = function() { window.print(); }
                        </script>
                        `;
    var HTML_Table = `
                      <!DOCTYPE html>
                      <html lang="ar" dir="rtl">
                      <head>
                        <meta charset="utf-8">
                        <meta http-equiv="X-UA-Compatible" content="IE=edge">
                        <style>

                          .print {  
                            -webkit-print-color-adjust: exact;
                          }
                          
                          .table0_td {
                            
                            border:none;
                            padding:0in 0pt 0in 0pt;
                            height:9.95pt
                            
                          }
                          
                          .table0_td1 {
                            
                            border:none;
                            border-bottom:solid #984806 1.0pt;
                            padding:0in 0pt 0in 0pt;
                            height:9.95pt
                            
                          }
                          
                          
                          
                          .table1_td {
                            
                            border:solid #984806 1.0pt;
                            border-left-color: white;
                            background:#984806;
                            padding:0pt 0pt 0pt 0pt;
                            height:.1in
                            
                          }
                          
                          .last_td {
                            border-left-color: #984806 !important;
                          }
                          
                          .table1_td1 {
                            
                            border:solid #984806 1.0pt;
                            border-top:none;
                            background:white;
                            padding:0pt 0pt 0pt 0pt;
                            height:.1in
                            
                          }
                          
                          .table2_td {
                            
                            border:solid #984806 1.0pt;
                            border-left-color: white;
                            background:#984806;
                            padding:0pt 0pt 0pt 0pt;
                            height:.1in;
                            
                          }
                          
                          .td_head {
                            text-align: center;
                            
                            color : white;
                            height: .3in;
                          }
                          
                          .td_body {
                            text-align: center;
                            
                            color : black;
                            height: .2in;
                            border:solid #984806 1.0pt;
                          }
                          
                          #inv_table tr:nth-child(odd) {
                            background-color: #FDE9D9;
                          }
                          
                          @font-face {
                            font-family: "Noto Kufi Arabic";
                            src: url(assets/custom_font/NotoKufiArabic-Regular.ttf) format("truetype");
                            }
                          
                          .badge {
                            display: inline-block;
                            min-width: 10px;
                            padding: 3px 7px;
                            font-size: 12px;
                            font-weight: 500;
                            color: #fff;
                            line-height: 1.5384616;
                            vertical-align: middle;
                            white-space: nowrap;
                            text-align: center;
                            background-color: #999999;
                            border-radius: 10px;
                          } 

                          .label {
                              display: inline-block;
                              font-weight: 500;
                              padding: 2px 5px 1px 5px;
                              line-height: 1.5384616;
                              border: 1px solid transparent;
                              text-transform: uppercase;
                              text-align: center;

                              font-size: 10px;
                              letter-spacing: 0.1px;
                              border-radius: 2px;
                          }
                          
                          .badge {
                            padding: 1px 1px 1px 1px;
                            font-size: 10px;
                            letter-spacing: 0px;
                            vertical-align: baseline;
                            
                            
                            
                          }
                          
                          .bg-orange-400 {
                            background-color: #FFA726;
                            border-color: #FFA726;
                            color: #fff;
                          }

                          .bg-success-400 {
                            background-color: #4CAF50;
                            border-color: #4CAF50;
                            color: #fff;
                          }

                        

                        </style>

                      </head>

                      <body class="print" style=" font-family: 'Noto Kufi Arabic', Times, serif;">
                        <div align=center>
                      `;


   


    function add_transfer(){
      $('#transfer_form_cont').modal('toggle');

    }


    function add_profit(){
      $('#profit_form_cont').modal('toggle');

    }


    

  

    function get_Staff(){

      $.ajax({
                      url : 'models/crul_capital.php',
                      type : 'POST',
                      data : {type:'get_Staff'},
                      dataType : 'json',
                      success : function(res) {
                          
                          if(res.is_success == true){
                            
                            var attach ='<option value="" selected="selected">'+Translation('اسم المالك')+'</option>';
                          
                            for(var i=res.data.length-1 ; i >= 0;i--){
                              
                              attach = attach + '<option value="'+res.data[i].staff_id+'">'+res.data[i].full_name+'</option>';
                             

                            }
                            $("#amount_owner_staff_id_fk").html(attach);
                            $("#edit_staff_id_fk").html(attach);
                            $("#staff_name").html(attach);
                            $("#profit_staff_name").html(attach);
                            $("#edit_staff_name").html(attach);
                            $('#amount_owner_staff_id_fk').selectpicker('refresh');
                            $("#edit_staff_id_fk").selectpicker('refresh');
                            $("#staff_name").selectpicker('refresh');
                            $("#edit_staff_name").selectpicker('refresh');

                          }else{
                            swal({
                                        title: Translation("خطأ"),
                                        text: Translation(res.data),
                                        confirmButtonColor: "#2196F3",
                                        type: "error"
                                    });
                          }

                            
                      },
                      complete: function() {
                             //  me.data('requestRunning', false);
                      }

                }); // submit ajax

    }

    function print_invoice(typ,payment=null){
      if(typ=='Print'){
        window.open().document.write(HTML_VIEW_PAY+print_page);
      }else if(typ=='PDF'){
        var nn = $("#inv_numb").val();
        pdf_generator(HTML_VIEW_PAY,nn,'pay')
      }else{
        window.open().document.write(HTML_VIEW_PAY);
      }
    }

   

    function set_date(){

        var now = new Date();
        var month = (now.getMonth() + 1);               
        var day = now.getDate();
        if (month < 10) 
            month = "0" + month;
        if (day < 10) 
            day = "0" + day;
        var today = now.getFullYear() + '-' + month + '-' + day;
        $('#first_date').val(today);
        $('#second_date').val(today);
    }

    function get_Branch(){

                $.ajax({
                      url : 'models/crul_branch.php',
                      type : 'POST',
                      data : {type:'get',branch_id:BRANCH_ID},
                      dataType : 'json',
                      success : function(response) {
                          var attach ='<option class="text-black text-danger-800" selected="selected" value="">'+Translation('اختيار الفرع')+'</option>';
                          
                          for(var i=response.length-1 ; i >= 0;i--){

                            var tp =response[i][2];
                              
                            attach = attach + '<option class="text-black text-danger-800" data-subtext="'+Translation('الموقع')+' : '+response[i]['location']+'" value="'+response[i]['branch_id']+'">'+response[i]['branch_name']+'</option>';
                              
                               
                              
                          }
                          $("#branch_id_fk").html(attach);
                          $("#branch_id_fk1").html(attach);
                          $('#branch_id_fk').selectpicker('refresh');

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

                }); // submit ajax

    }

    function showLoader(msg){
      $("#loader_msg").html(msg);
      $('#loader_modal').modal('toggle');
    }

    function hideLoader(){
      
      $('#loader_modal').modal('toggle');
    }

    function get_Category(){

      $.ajax({
                      url : 'models/crul_category.php',
                      type : 'POST',
                      data : {type:'get'},
                      dataType : 'json',
                      success : function(response) {
                        console.log(response)
                          var attach ='<option value="0" selected="selected">'+Translation('All Category')+'</option>';
                          
                            for(var i=response.length-1 ; i >= 0;i--){
                              
                              attach = attach + '<option value="'+response[i][0]+'">'+response[i][1]+'</option>';
                             

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

                }); // submit ajax

    }


    function get_Customer(){

            $.ajax({
                  url : 'models/crul_customer.php',
                  type : 'POST',
                  data : {type:'get'},
                  dataType : 'json',
                  success : function(response) {
                    //console.log(response)
                      var attach ='<option value="" selected="selected">'+Translation('All Customer')+'</option>';
                      var aaa='';
                        for(var i=response.length-1 ; i >= 0;i--){
                          
                          attach = attach + '<option value="'+response[i][0]+'">'+response[i][1]+'</option>';
                          
                        }
                        $("#customer_id").html(attach);
                        
                        $('#customer_id').selectpicker('refresh');
                       
                  },
                  complete: function() {
                         //  me.data('requestRunning', false);
                  }

            }); // submit ajax

    }


    function get_Saleman(){

            $.ajax({
                  url : 'models/crul_saleman.php',
                  type : 'POST',
                  data : {type:'get'},
                  dataType : 'json',
                  success : function(response) {
                    //console.log(response)
                      var attach ='<option value="0" selected="selected">'+Translation('All SalesMan')+'</option>';
                      var aaa='';
                        for(var i=response.length-1 ; i >= 0;i--){
                          
                          attach = attach + '<option   value="'+response[i][0]+'">'+response[i][1]+'</option>';
                          
                        }
                        $("#saleman_id").html(attach);
                        $('#saleman_id').selectpicker('refresh');

                  },
                  complete: function() {
                         //  me.data('requestRunning', false);
                  }

            }); // submit ajax

    }


    function get_Supplier(){

            $.ajax({
                  url : 'models/crul_supplier.php',
                  type : 'POST',
                  data : {type:'get'},
                  dataType : 'json',
                  success : function(response) {
                    //console.log(response)
                      var attach ='<option value="0" selected="selected">'+Translation('All Suppliers')+'</option>';
                      var aaa='';
                        for(var i=response.length-1 ; i >= 0;i--){
                          
                          attach = attach + '<option  value="'+response[i][0]+'">'+response[i][1]+'</option>';
                      
                        }
                        $("#supplier_id").html(attach);
                        $('#supplier_id').selectpicker('refresh');
                  },
                  complete: function() {
                         //  me.data('requestRunning', false);
                  }

            }); // submit ajax

    }


    function get_Loaner(){

      $.ajax({
                      url : 'models/crul_loaner.php',
                      type : 'POST',
                      data : {type:'get'},
                      dataType : 'json',
                      success : function(res) {
                          
                          if(res!= 0){
                            
                            var attach ='<option value="0" selected="selected">'+Translation('All Loaners')+'</option>';
                          
                            for(var i=res.length-1 ; i >= 0;i--){
                              
                              attach = attach + '<option value="'+res[i][0]+'">'+res[i][1]+'</option>';
                             

                            }
                            $("#loaner_id").html(attach);
                            
                            $('#loaner_id').selectpicker('refresh');
                            
                          }else{
                            swal({
                                        title: Translation("خطأ"),
                                        text: Translation(res.data),
                                        confirmButtonColor: "#2196F3",
                                        type: "error"
                                    });
                          }

                            
                      },
                      complete: function() {
                             //  me.data('requestRunning', false);
                      }

                }); // submit ajax

    }


    function get_Owner(){

      $.ajax({
                      url : 'models/crul_owner.php',
                      type : 'POST',
                      data : {type:'get'},
                      dataType : 'json',
                      success : function(res) {
                          
                            
                            var attach ='<option value="" selected="selected">'+Translation('All Owners')+'</option>';
                          
                            for(var i=res.length-1 ; i >= 0;i--){
                              
                              attach = attach + '<option value="'+res[i][0]+'">'+res[i][1]+'</option>';
                             

                            }
                            $("#owner_id").html(attach);
                           
                            $('#owner_id').selectpicker('refresh');
                           

                            
                      },
                      complete: function() {
                             //  me.data('requestRunning', false);
                      }

                }); // submit ajax

    }

    function get_Product(){

      $.ajax({
                      url : 'models/crul_product.php',
                      type : 'POST',
                      data : {type:'get'},
                      dataType : 'json',
                      success : function(res) {
                          
                            
                            var attach ='<option value="" selected="selected">'+Translation('All Products')+'</option>';
                          
                            for(var i=res.length-1 ; i >= 0;i--){
                              
                              attach = attach + '<option value="'+res[i][0]+'">'+res[i][1]+'</option>';
                             

                            }
                            $("#product_id").html(attach);
                           
                            $('#product_id').selectpicker('refresh');
                           

                            
                      },
                      complete: function() {
                             //  me.data('requestRunning', false);
                      }

                }); // submit ajax

    }

$(document).ready(function() {
  
    get_Staff();
    get_Branch();
    set_date();
    get_Category();
    get_Customer();
    get_Saleman();
    get_Supplier();
    get_Loaner();
    get_Owner();
    get_Product();

    $('#print_pay').click(function(e) {
      e.preventDefault();
         print_invoice('Print','pay');
    });

    $('#pdf_pay').click(function(e) {
      e.preventDefault();
         print_invoice('PDF','pay');
    });

 




    $('#profit_form').submit(function(event) {
              event.preventDefault();
             
              
              
              var transfer_amount     = $("#profit_amount").val();
              var factory_date_of_add = $("#profit_date_of_add").val();
              var transfer_note       = $("#profit_note").val();
              var staff_name          = $("#profit_staff_name").val();
              var branch_id_fk        = $("#branch_id_fk1").val();
              



              var Check1 = true;
              var Check2 = true;
              var Check3 = true;
              var Check4 = true;
              var Check5 = true;
              var Check6 = true;

              Check1 = Checks('profit_amount');
              Check2 = Checks('profit_date_of_add');
              Check3 = Checks('profit_staff_name');
              Check4 = Checks('branch_id_fk1');
              
            

              if(Check1 && Check2 && Check3 && Check4 && Check5 && Check6){
                  $("#profit_form_btn").attr('disabled',true);
                  $.ajax({
                      url: "models/crul_safe.php",
                      type:"POST",
                      dataType : "JSON",
                      data : {
                              staff_name:staff_name,
                              transfer_amount:transfer_amount,
                              factory_date_of_add:factory_date_of_add,
                              transfer_note:transfer_note,
                              branch_id_fk:branch_id_fk,
                              type:'profit'},
                      complete:function(){
                        $("#profit_form_btn").attr('disabled',false);
                      },
                      beforeSend:function(){
                        $("#profit_form_btn").attr('disabled',true);
                      },
                      success: function(res) {
                        if(res.is_success == false){
                          
                          $('#profit_form_cont').modal('toggle');
                          $("#profit_form")[0].reset();
                          swal({
                              title: Translation("خطأ"),
                              text: Translation(res.data),
                              confirmButtonColor: "#66BB6A",
                              type: "warning"
                          });
                        }else{
                          $("#profit_form")[0].reset();
                          $("#profit_staff_name").val('').selectpicker('refresh');
                          

                          $(".datatable-column-search-inputs").DataTable().ajax.reload(null, false);

                          $('#profit_form_cont').modal('toggle');
                          noty({
                                          width: 200,
                                          text: Translation(res.data), //'Product Successfully Update',
                                          type: 'success',
                                          dismissQueue: true,
                                          timeout: 3000,
                                          layout: 'topRight',
                                    });
                        }
                        $("#profit_form_btn").attr('disabled',false);
                      }
                  });
                }
    });

    $('#sold_return_detail_btn').on('click',function(){
      
    
        
        var d1              = $("#first_date").val();
        var d2              = $("#second_date").val();
        var category_id_fk  = $("#category_id_fk").val();
        var saleman_id      = $("#saleman_id").val();
        var product_id      = $("#product_id").val();
        var customer_id     = $("#customer_id").val();
        var branch_id_fk    = $("#branch_id_fk").val();
      
       
       
        var attach = `<thead>
                        <tr class="bg-blue-300">
                          <th>ID</th>
                          <th class="multi_lang">Product Name</th>
                          <th class="multi_lang">Category</th>
                          <th class="multi_lang">Customer</th>
                          <th class="multi_lang">Invoice No.</th>
                          <th class="multi_lang">Invoice Date</th>
                          <th class="multi_lang">Stock Name</th>
                          <th class="multi_lang">Return Qty</th>
                          <th class="multi_lang">Cost Price</th>
                          <th class="multi_lang">Sell Price</th>
                          <th class="multi_lang">Discount</th>
                          <th class="multi_lang">Total</th>
                        </tr>
                      </thead>
                      <tbody>`;

        $("#table1").attr('style','');
       
        $.ajax({
                url: "models/crul_report.php",
                type:"POST",
                dataType : "JSON",
                data: {"type":"return_product_detail_report",
                        
                        d1 : d1,
                        d2 : d2,
                        category_id_fk : category_id_fk,
                        saleman_id : saleman_id,
                        product_id : product_id,
                        customer_id : customer_id,
                        branch_id_fk : branch_id_fk

                      } ,
                complete:function(){
                  hideLoader();
                },
                beforeSend:function(){
                  showLoader("Please Wait While Gathering Report Information");
                },
                success: function(res) {
                  if(res.is_success == true){

                    var total_qty       = 0;
                    var total_qty_cost  = 0;
                    var total_qty_sell  = 0;
                    var total_          = 0;
                    var total_disc      = 0;
                    
                    for(var i=0 ; i <=res.data.length-1;i++){

                      attach = attach + `
                                        <tr>
                                            <td>`+res.data[i].product_id+`</td>
                                            <td>`+res.data[i].product_name+`</td>
                                            <td>`+res.data[i].category+`</td>
                                            <td>`+res.data[i].customer_full_name+`</td>
                                            <td>`+res.data[i].invoice_no+`</td>
                                            <td>`+res.data[i].invoice_date+`</td>
                                            <td>`+res.data[i].stock_name+`</td>
                                            <td>`+res.data[i].sold_quantity+`</td>
                                            <td>`+currency_format_display(res.data[i].sold_quantity_cost)+`</td>
                                            <td>`+currency_format_display(res.data[i].sold_quantity_sell)+`</td>
                                            <td>`+currency_format_display(res.data[i].actual_discount)+`</td>
                                            <td>`+currency_format_display(res.data[i].total_sell)+`</td>
                                           

                                        </tr>
                                        `;
                      total_qty       = +total_qty + +(res.data[i].sold_quantity);
                      total_qty_cost  = +total_qty_cost + +(res.data[i].sold_quantity_cost);
                      total_qty_sell  = +total_qty_sell + +(res.data[i].sold_quantity_sell);
                      total_disc      = +total_disc + +(res.data[i].actual_discount);
                      total_          = +total_ + +(res.data[i].total_sell);

                    }

                    attach = attach + `</tbody>
                                       <tfoot>
                                        <tr class="alpha-info">
                                            <td></td>
                                            <td></td>
                                            <td></td>
                                            <td></td>
                                            <td></td>
                                            <td></td>
                                            
                                            <td class="multi_lang">Totals</td>
                                            <td>${total_qty}</td>
                                            <td>`+currency_format_display(total_qty_cost)+`</td>
                                            <td>`+currency_format_display(total_qty_sell)+`</td>
                                            <td>`+currency_format_display(total_disc)+`</td>
                                            <td>`+currency_format_display(total_)+`</td>
                                        </tr>
                                      </tfoot>
                                        `;

                    $("#table1").html(attach);
                    

                  }else{
                    swal({
                                title: Translation("خطأ"),
                                text: Translation(res.data),
                                confirmButtonColor: "#2196F3",
                                type: "error"
                            });
                  }
                }
        });

      
     


    });

    $('#sold_product_detail_btn').on('click',function(){
      
    
        
        var d1              = $("#first_date").val();
        var d2              = $("#second_date").val();
        var category_id_fk  = $("#category_id_fk").val();
        var saleman_id      = $("#saleman_id").val();
        var product_id      = $("#product_id").val();
        var customer_id     = $("#customer_id").val();
        var branch_id_fk    = $("#branch_id_fk").val();
      
       
       
        var attach = `<thead>
                        <tr class="bg-blue-300">
                          <th>ID</th>
                          <th class="multi_lang">Product Name</th>
                          <th class="multi_lang">Category</th>
                          <th class="multi_lang">Customer</th>
                          <th class="multi_lang">Invoice No.</th>
                          <th class="multi_lang">Invoice Date</th>
                          <th class="multi_lang">Stock Name</th>
                          <th class="multi_lang">Sold Qty</th>
                          <th class="multi_lang">Cost Price</th>
                          <th class="multi_lang">Sell Price</th>
                          <th class="multi_lang">Discount</th>
                          <th class="multi_lang">Total</th>
                        </tr>
                      </thead>
                      <tbody>`;

        $("#table1").attr('style','');
       
        $.ajax({
                url: "models/crul_report.php",
                type:"POST",
                dataType : "JSON",
                data: {"type":"sold_product_detail_report",
                        
                        d1 : d1,
                        d2 : d2,
                        category_id_fk : category_id_fk,
                        saleman_id : saleman_id,
                        product_id : product_id,
                        customer_id : customer_id,
                        branch_id_fk : branch_id_fk

                      } ,
                complete:function(){
                  hideLoader();
                },
                beforeSend:function(){
                  showLoader("Please Wait While Gathering Report Information");
                },
                success: function(res) {
                  if(res.is_success == true){

                    var total_qty       = 0;
                    var total_qty_cost  = 0;
                    var total_qty_sell  = 0;
                    var total_          = 0;
                    var total_disc      = 0;
                    
                    for(var i=0 ; i <=res.data.length-1;i++){

                      attach = attach + `
                                        <tr>
                                            <td>`+res.data[i].product_id+`</td>
                                            <td>`+res.data[i].product_name+`</td>
                                            <td>`+res.data[i].category+`</td>
                                            <td>`+res.data[i].customer_full_name+`</td>
                                            <td>`+res.data[i].invoice_no+`</td>
                                            <td>`+res.data[i].invoice_date+`</td>
                                            <td>`+res.data[i].stock_name+`</td>
                                            <td>`+res.data[i].sold_quantity+`</td>
                                            <td>`+currency_format_display(res.data[i].sold_quantity_cost)+`</td>
                                            <td>`+currency_format_display(res.data[i].sold_quantity_sell)+`</td>
                                            <td>`+currency_format_display(res.data[i].actual_discount)+`</td>
                                            <td>`+currency_format_display(res.data[i].total_sell)+`</td>
                                           

                                        </tr>
                                        `;
                      total_qty       = +total_qty + +(res.data[i].sold_quantity);
                      total_qty_cost  = +total_qty_cost + +(res.data[i].sold_quantity_cost);
                      total_qty_sell  = +total_qty_sell + +(res.data[i].sold_quantity_sell);
                      total_disc      = +total_disc + +(res.data[i].actual_discount);
                      total_          = +total_ + +(res.data[i].total_sell);

                    }

                    attach = attach + `</tbody>
                                       <tfoot>
                                        <tr class="alpha-info">
                                            <td></td>
                                            <td></td>
                                            <td></td>
                                            <td></td>
                                            <td></td>
                                            <td></td>
                                            
                                            <td class="multi_lang">Totals</td>
                                            <td>${total_qty}</td>
                                            <td>`+currency_format_display(total_qty_cost)+`</td>
                                            <td>`+currency_format_display(total_qty_sell)+`</td>
                                            <td>`+currency_format_display(total_disc)+`</td>
                                            <td>`+currency_format_display(total_)+`</td>
                                        </tr>
                                      </tfoot>
                                        `;

                    $("#table1").html(attach);
                    

                  }else{
                    swal({
                                title: Translation("خطأ"),
                                text: Translation(res.data),
                                confirmButtonColor: "#2196F3",
                                type: "error"
                            });
                  }
                }
        });

      
     


    });


    $('#product_filter_btn').on('click',function(){
      
    
        
        var category_id_fk  = $("#category_id_fk").val();
        var branch_id_fk    = $("#branch_id_fk").val();
        
       
       
        var attach = `<thead>
                        <tr class="bg-blue-300">
                          <th>ID</th>
                          <th class="multi_lang">Product Name</th>
                          <th class="multi_lang">Category</th>
                          <th class="multi_lang">Stock Name</th>
                          <th class="multi_lang">Recieved Qty</th>
                          <th class="multi_lang">Sold Qty</th>
                          <th class="multi_lang">Damaged Qty</th>
                          <th class="multi_lang">On Hand Qty</th>
                          <th class="multi_lang">Unit Sell Price</th>
                        </tr>
                      </thead>
                      <tbody>`;

        $("#table1").attr('style','');
       
        $.ajax({
                url: "models/crul_report.php",
                type:"POST",
                dataType : "JSON",
                data: {"type":"product_report",
                        
                        category_id_fk : category_id_fk,
                        branch_id_fk : branch_id_fk

                      } ,
                complete:function(){
                  hideLoader();
                },
                beforeSend:function(){
                  showLoader("Please Wait While Gathering Report Information");
                },
                success: function(res) {
                  if(res.is_success == true){

                    var total_inv = 0;
                    
                    for(var i=0 ; i <=res.data.length-1;i++){

                      attach = attach + `
                                        <tr>
                                            <td>`+res.data[i].product_id+`</td>
                                            <td>`+res.data[i].product_name+`</td>
                                            <td>`+res.data[i].category+`</td>
                                            <td>`+res.data[i].stock_name+`</td>
                                            <td>`+res.data[i].recieved_quantity+`</td>
                                            <td>`+res.data[i].sold_quantity+`</td>
                                            <td>`+res.data[i].damage_quantity+`</td>
                                            <td><span class="label label-success">`+res.data[i].on_hand_quantity+`</span></td>
                                            <td>`+currency_format_display(res.data[i].sell_price)+`</td>

                                        </tr>
                                        `;
                      total_inv = +total_inv + +(res.data[i].on_hand_quantity * res.data[i].sell_price);

                    }

                    attach = attach + `</tbody>
                                       <tfoot>
                                        <tr class="alpha-info">
                                            <td></td>
                                            <td></td>
                                            <td></td>
                                            
                                            <td></td>
                                            <td></td>
                                            <td></td>
                                            <td></td>
                                            <td class="multi_lang">Total Price</td>
                                            <td id="total_invoice">`+currency_format_display(total_inv)+`</td>
                                        </tr>
                                      </tfoot>
                                        `;

                    $("#table1").html(attach);
                    

                  }else{
                    swal({
                                title: Translation("خطأ"),
                                text: Translation(res.data),
                                confirmButtonColor: "#2196F3",
                                type: "error"
                            });
                  }
                }
        });

      
     


    });


    $('#sold_product_btn').on('click',function(){
      
    
        
        var d1              = $("#first_date").val();
        var d2              = $("#second_date").val();
        var category_id_fk  = $("#category_id_fk").val();
        var saleman_id      = $("#saleman_id").val();
        var product_id      = $("#product_id").val();
        var customer_id     = $("#customer_id").val();
        var branch_id_fk    = $("#branch_id_fk").val();
      
       
       
        var attach = `<thead>
                        <tr class="bg-blue-300">
                          <th>ID</th>
                          <th class="multi_lang">Product Name</th>
                          <th class="multi_lang">Category</th>
                          <th class="multi_lang">Stock Name</th>
                          <th class="multi_lang">Sold Qty</th>
                          <th class="multi_lang">Cost Price</th>
                          <th class="multi_lang">Sell Price</th>
                          <th class="multi_lang">Discount</th>
                          <th class="multi_lang">Total</th>
                        </tr>
                      </thead>
                      <tbody>`;

        $("#table1").attr('style','');
       
        $.ajax({
                url: "models/crul_report.php",
                type:"POST",
                dataType : "JSON",
                data: {"type":"sold_product_report",
                        
                        d1 : d1,
                        d2 : d2,
                        category_id_fk : category_id_fk,
                        saleman_id : saleman_id,
                        product_id : product_id,
                        customer_id : customer_id,
                        branch_id_fk : branch_id_fk

                      } ,
                complete:function(){
                  hideLoader();
                },
                beforeSend:function(){
                  showLoader("Please Wait While Gathering Report Information");
                },
                success: function(res) {
                  if(res.is_success == true){

                    var total_qty       = 0;
                    var total_qty_cost  = 0;
                    var total_qty_sell  = 0;
                    var total_          = 0;
                    var total_disc      = 0;
                    
                    for(var i=0 ; i <=res.data.length-1;i++){

                      attach = attach + `
                                        <tr>
                                            <td>`+res.data[i].product_id+`</td>
                                            <td>`+res.data[i].product_name+`</td>
                                            <td>`+res.data[i].category+`</td>
                                            <td>`+res.data[i].stock_name+`</td>
                                            <td>`+res.data[i].sold_quantity+`</td>
                                            <td>`+currency_format_display(res.data[i].sold_quantity_cost)+`</td>
                                            <td>`+currency_format_display(res.data[i].sold_quantity_sell)+`</td>
                                            <td>`+currency_format_display(res.data[i].actual_discount)+`</td>
                                            <td>`+currency_format_display(res.data[i].total_sell)+`</td>
                                           

                                        </tr>
                                        `;
                      total_qty       = +total_qty + +(res.data[i].sold_quantity);
                      total_qty_cost  = +total_qty_cost + +(res.data[i].sold_quantity_cost);
                      total_qty_sell  = +total_qty_sell + +(res.data[i].sold_quantity_sell);
                      total_disc      = +total_disc + +(res.data[i].actual_discount);
                      total_          = +total_ + +(res.data[i].total_sell);

                    }

                    attach = attach + `</tbody>
                                       <tfoot>
                                        <tr class="alpha-info">
                                            <td></td>
                                            <td></td>
                                            <td></td>
                                            
                                            <td class="multi_lang">Totals</td>
                                            <td>${total_qty}</td>
                                            <td>`+currency_format_display(total_qty_cost)+`</td>
                                            <td>`+currency_format_display(total_qty_sell)+`</td>
                                            <td>`+currency_format_display(total_disc)+`</td>
                                            <td>`+currency_format_display(total_)+`</td>
                                        </tr>
                                      </tfoot>
                                        `;

                    $("#table1").html(attach);
                    

                  }else{
                    swal({
                                title: Translation("خطأ"),
                                text: Translation(res.data),
                                confirmButtonColor: "#2196F3",
                                type: "error"
                            });
                  }
                }
        });

      
     


    });


    $('#purchase_filter_btn').on('click',function(){
      
    
        var d1            = $("#first_date").val();
        var d2            = $("#second_date").val();
        var branch_id_fk  = $("#branch_id_fk").val();
       
       
        var attach = `<thead>
                        <tr class="bg-blue-300">
                          <th>ID</th>
                          <th class="multi_lang">Purchase No.</th>
                          <th class="multi_lang">Invoice No.</th>
                          <th class="multi_lang">Date</th>
                          <th class="multi_lang">Inventory</th>
                          <th class="multi_lang">Cost Before Expenses</th>
                          <th class="multi_lang">Total Cost</th>
                          <th class="multi_lang">Supplier</th>
                          <th class="multi_lang">Note</th>
                        </tr>
                      </thead>
                      <tbody>`;

        $("#table1").attr('style','');
       
        $.ajax({
                url: "models/crul_report.php",
                type:"POST",
                dataType : "JSON",
                data: {"type":"purchase_report",
                        
                        d1 : d1,
                        d2 : d2,
                        branch_id_fk : branch_id_fk

                      } ,
                complete:function(){
                  hideLoader();
                },
                beforeSend:function(){
                  showLoader("Please Wait While Gathering Report Information");
                },
                success: function(res) {
                  if(res.is_success == true){

                    var total_inv   = 0;
                    var total_inv_ex = 0;
                    
                    for(var i=0 ; i <=res.data.length-1;i++){

                      attach = attach + `
                                        <tr>
                                            <td>`+res.data[i].purchase_id+`</td>
                                            <td>`+res.data[i].purchase_auto_number+`</td>
                                            <td>`+res.data[i].purchase_number+`</td>
                                            <td>`+res.data[i].purchase_date+`</td>
                                            <td>`+res.data[i].stock_name+`</td>
                                            <td>`+currency_format_display(res.data[i].total_price_before_expenses)+`</td>
                                            <td>`+currency_format_display(res.data[i].total_price)+`</td>
                                            
                                            <td>`+res.data[i].supplier_full_name+`</td>
                                            <td>`+res.data[i].purchase_note+`</td>

                                        </tr>
                                        `;
                      total_inv = +total_inv + +(res.data[i].total_price);
                      total_inv_ex = +total_inv_ex + +(res.data[i].total_price_before_expenses);

                    }

                    attach = attach + `</tbody>
                                       <tfoot>
                                        <tr class="alpha-info">
                                            <td></td>
                                            <td></td>
                                            <td></td>
                                            
                                            <td></td>
                                            <td class="multi_lang">Totals</td>
                                            <td id="total_invoice">`+currency_format_display(total_inv_ex)+`</td>
                                            <td id="total_invoice">`+currency_format_display(total_inv)+`</td>
                                            
                                            <td></td>
                                            <td></td>
                                            
                                        </tr>
                                      </tfoot>
                                        `;

                    $("#table1").html(attach);
                    

                  }else{
                    swal({
                                title: Translation("خطأ"),
                                text: Translation(res.data),
                                confirmButtonColor: "#2196F3",
                                type: "error"
                            });
                  }
                }
        });

      
     


    });


    $('#supplier_filter_btn').on('click',function(){
      
    
        var d1            = $("#first_date").val();
        var d2            = $("#second_date").val();
        var supplier_id  = $("#supplier_id").val();
        var branch_id_fk  = $("#branch_id_fk").val();
       
       
        var attach = `<thead>
                        <tr class="bg-blue-300">
                          <th>ID</th>
                          <th class="multi_lang">Supplier Name</th>
                          <th class="multi_lang">Payment No.</th>
                          <th class="multi_lang">Date</th>
                          <th class="multi_lang">Amount</th>
                          <th class="multi_lang">Note</th>
                          <th class="multi_lang">Purchase Bill No.</th>
                          
                        </tr>
                      </thead>
                      <tbody>`;

        $("#table1").attr('style','');
       
        $.ajax({
                url: "models/crul_report.php",
                type:"POST",
                dataType : "JSON",
                data: {"type":"supplier_report",
                        
                        d1 : d1,
                        d2 : d2,
                        supplier_id : supplier_id,
                        branch_id_fk : branch_id_fk

                      } ,
                complete:function(){
                  hideLoader();
                },
                beforeSend:function(){
                  showLoader("Please Wait While Gathering Report Information");
                },
                success: function(res) {
                  if(res.is_success == true){

                    var total_inv   = 0;
                    var total_inv_ex = 0;
                    
                    for(var i=0 ; i <=res.data.length-1;i++){

                      attach = attach + `
                                        <tr>
                                            <td>`+res.data[i].payment_id+`</td>
                                            <td>`+res.data[i].supplier_full_name+`</td>
                                            <td>`+res.data[i].payment_number+`</td>
                                            <td>`+res.data[i].paid_date+`</td>
                                            <td>`+currency_format_display(res.data[i].paid_amount)+`</td>
                                            
                                            <td>`+res.data[i].payment_note+`</td>
                                            <td>`+res.data[i].purchase_auto_number+`</td>

                                        </tr>
                                        `;
                      total_inv = +total_inv + +(res.data[i].paid_amount);
                      // total_inv_ex = +total_inv_ex + +(res.data[i].total_price_before_expenses);

                    }

                    attach = attach + `</tbody>
                                       <tfoot>
                                        <tr class="alpha-info">
                                            <td></td>
                                            <td></td>
                                            <td></td>
                                            <td class="multi_lang">Totals</td>
                                            <td id="total_invoice">`+currency_format_display(total_inv)+`</td>
                                            <td></td>
                                            <td></td>
                                            
                                        </tr>
                                      </tfoot>
                                        `;

                    $("#table1").html(attach);
                    

                  }else{
                    swal({
                                title: Translation("خطأ"),
                                text: Translation(res.data),
                                confirmButtonColor: "#2196F3",
                                type: "error"
                            });
                  }
                }
        });

      
     


    });


    $('#expenses_filter_btn').on('click',function(){
      
    
        var d1            = $("#first_date").val();
        var d2            = $("#second_date").val();
        var branch_id_fk  = $("#branch_id_fk").val();
       
       
        var attach = `<thead>
                        <tr class="bg-blue-300">
                          <th>ID</th>
                          <th class="multi_lang">Expense Name</th>
                          <th class="multi_lang">Date</th>
                          <th class="multi_lang">Total Price</th>
                          <th class="multi_lang">Invoice No.</th>
                          <th class="multi_lang">Note</th>
                          <th class="multi_lang">Branch Safe</th>
                          
                        </tr>
                      </thead>
                      <tbody>`;

        $("#table1").attr('style','');
       
        $.ajax({
                url: "models/crul_report.php",
                type:"POST",
                dataType : "JSON",
                data: {"type":"expenses_report",
                        
                        d1 : d1,
                        d2 : d2,
                        branch_id_fk : branch_id_fk

                      } ,
                complete:function(){
                  hideLoader();
                },
                beforeSend:function(){
                  showLoader("Please Wait While Gathering Report Information");
                },
                success: function(res) {
                  if(res.is_success == true){

                    var total_inv   = 0;
                    var total_inv_ex = 0;
                    
                    for(var i=0 ; i <=res.data.length-1;i++){

                      attach = attach + `
                                        <tr>
                                            <td>`+res.data[i].expenses_id+`</td>
                                            <td>`+res.data[i].expenses_name+`</td>
                                            <td>`+res.data[i].expenses_date+`</td>
                                            <td>`+currency_format_display(res.data[i].expenses_unit_price)+`</td>
                                            <td>`+res.data[i].bill_number+`</td>
                                            
                                            <td>`+res.data[i].expenses_note+`</td>
                                            <td>`+res.data[i].branch_name+`</td>

                                        </tr>
                                        `;
                      total_inv = +total_inv + +(res.data[i].expenses_unit_price);
                      // total_inv_ex = +total_inv_ex + +(res.data[i].total_price_before_expenses);

                    }

                    attach = attach + `</tbody>
                                       <tfoot>
                                        <tr class="alpha-info">
                                            <td></td>
                                            <td></td>
                                            <td class="multi_lang">Totals</td>
                                            <td id="total_invoice">`+currency_format_display(total_inv)+`</td>
                                            <td></td>
                                            <td></td>
                                            <td></td>
                                            
                                        </tr>
                                      </tfoot>
                                        `;

                    $("#table1").html(attach);
                    

                  }else{
                    swal({
                                title: Translation("خطأ"),
                                text: Translation(res.data),
                                confirmButtonColor: "#2196F3",
                                type: "error"
                            });
                  }
                }
        });

      
     


    });


    $('#sell_filter_btn').on('click',function(){
      
      if($("#first_date").val()!='' && $("#second_date").val()!='' ){

        var d1            = $("#first_date").val();
        var d2            = $("#second_date").val();
        var saleman_id    = $("#saleman_id").val();
        var branch_id_fk  = $("#branch_id_fk").val();
        var customer_id   = $("#customer_id").val();
        
        var _href ="index.php?p=report";

        if(d1 !=''){
         
          _href = _href + "&d1="+d1;
        }

        if(d2 !=''){
         
          _href = _href + "&d2="+d2;
        }

        var attach = `<thead>
                        <tr class="bg-blue-300">
                          <th>ID</th>
                          <th class="multi_lang">Invoice No.</th>
                          <th class="multi_lang">Amount</th>
                          <th class="multi_lang">Date</th>
                          <th class="multi_lang">Customer Name</th>
                          <th class="multi_lang">Staff Name</th>
                          <th class="multi_lang">Saleman</th>
                          <th class="multi_lang"> Branch </th>
                          <th class="multi_lang"> Note </th>
                        </tr>
                      </thead>
                      <tbody>`;

        $("#table1").attr('style','');
       
        $.ajax({
                url: "models/crul_report.php",
                type:"POST",
                dataType : "JSON",
                data: {"type":"sell_report",
                        d1 : d1,
                        d2 : d2,
                        saleman_id : saleman_id,
                        customer_id : customer_id,
                        branch_id_fk : branch_id_fk

                      } ,
                complete:function(){
                  hideLoader();
                },
                beforeSend:function(){
                  showLoader("Please Wait While Gathering Report Information");
                },
                success: function(res) {
                  if(res.is_success == true){

                    var total_inv = 0;
                    
                    for(var i=0 ; i <=res.data.length-1;i++){

                      attach = attach + `
                                        <tr>
                                            <td>`+res.data[i].invoice_id+`</td>
                                            <td>`+res.data[i].invoice_number+`</td>
                                            <td>`+currency_format_display(res.data[i].final_price)+`</td>
                                            <td>`+res.data[i].time+`</td>
                                            <td>`+res.data[i].customer_name+`</td>
                                            <td>`+res.data[i].staff+`</td>
                                            <td>`+res.data[i].saleman_full_name+`</td>
                                            <td>`+res.data[i].branch_name+`</td>
                                            <td>`+res.data[i].invoice_note+`</td>
                                        </tr>
                                        `;
                      total_inv = +total_inv + +(res.data[i].final_price);

                    }

                    attach = attach + `</tbody>
                                       <tfoot>
                                        <tr class="alpha-info">
                                            <td></td>
                                            <td class="multi_lang">Total Price</td>
                                            <td id="total_invoice">`+currency_format_display(total_inv)+`</td>
                                            <td></td>
                                            <td></td>
                                            
                                            <td></td>
                                            <td></td>
                                            <td></td>
                                            <td></td>
                                            
                                        </tr>
                                      </tfoot>
                                        `;

                    $("#table1").html(attach);
                    

                  }else{
                    swal({
                                title: Translation("خطأ"),
                                text: Translation(res.data),
                                confirmButtonColor: "#2196F3",
                                type: "error"
                            });
                  }
                }
        });

      
        // window.location = _href;
      }else{
            swal({
                    title:"Error",
                    text: Translation('الرجاء قم بتحديد السنة وا الشهر'),
                    confirmButtonColor: "#66BB6A",
                    type: "warning"
                });
      }


    });

    $('#profit_filter_btn').on('click',function(){
      
      if($("#first_date").val()!='' && $("#second_date").val()!='' ){

        var d1            = $("#first_date").val();
        var d2            = $("#second_date").val();
        
        var branch_id_fk  = $("#branch_id_fk").val();
        var branch_name   = $("#branch_id_fk option:selected").text();
        
       

        var attach = ``;

        $("#table1").attr('style','');
       
        $.ajax({
                url: "models/crul_report.php",
                type:"POST",
                dataType : "JSON",
                data: {"type":"profit_report",
                        d1 : d1,
                        d2 : d2,
                        
                        branch_id_fk : branch_id_fk

                      } ,
                complete:function(){
                  hideLoader();
                },
                beforeSend:function(){
                  showLoader("Please Wait While Gathering Report Information");
                },
                success: function(res) {
                  if(res.is_success == true){

                    var total_inv = 0;
                    
                    for(var i=0 ; i <=res.data.length-1;i++){

                      attach = attach + `<tbody>
                                            <tr >
                                              
                                              <th  class="multi_lang">Total Sells</th>
                                              <td>`+currency_format_display(res.data[i].total_price)+`</td>

                                            </tr>
                                            <tr>
                                              <th  class="multi_lang">Total Sells Cost</th>
                                              <td>`+currency_format_display(res.data[i].total_invoice_cost)+`</td>

                                            </tr>
                                            <tr class="bg-blue-300">
                                              <th  class="multi_lang">A1 = Total Sells - Total Sells Cost</th>
                                              <td>`+currency_format_display(res.data[i].a1)+`</td>

                                            </tr>
                                            <tr>
                                              <th  class="multi_lang">Total Returned Invoices</th>
                                              <td>`+currency_format_display(res.data[i].total_invoice_return)+`</td>

                                            </tr>
                                            <tr>
                                              <th  class="multi_lang">Total Returned Cost</th>
                                            <td>`+currency_format_display(res.data[i].total_invoice_return_cost)+`</td>

                                            </tr>
                                            <tr class="bg-blue-300">
                                              <th  class="multi_lang">A2 = Total Returned Invoices - Total Returned Cost</th>
                                              <td>`+currency_format_display(res.data[i].a2)+`</td>
                                              
                                            </tr>
                                            <tr>
                                              <th  class="multi_lang">Total Expenses</th>
                                              <td>`+currency_format_display(res.data[i].total_expenses)+`</td>

                                            </tr>
                                            <tr class="bg-pink-300">
                                              <th class="multi_lang">Total Profits = A1 - A2 - total Expenses</th>
                                              <td>`+currency_format_display(res.data[i].total_profits)+`</td>

                                            </tr>
                                            <tr>
                                              <th  class="multi_lang">From Date</th>
                                              <td>`+d1+`</td>

                                            </tr>
                                            <tr>
                                              <th  class="multi_lang">To Date</th>
                                              <td>`+d2+`</td>

                                            </tr>
                                            <tr>
                                              <th  class="multi_lang">Branch</th>
                                              <td>`+branch_name+`</td>

                                            </tr>
                                          </tbody>
                                          

                                        
                                        `;
                      total_inv = +total_inv + +(res.data[i].total_profits);

                    }

                    attach = attach + `
                                       <tfoot>
                                       
                                      </tfoot>
                                        `;

                    $("#table1").html(attach);
                    

                  }else{
                    swal({
                                title: Translation("خطأ"),
                                text: Translation(res.data),
                                confirmButtonColor: "#2196F3",
                                type: "error"
                            });
                  }
                }
        });

      
        // window.location = _href;
      }else{
            swal({
                    title:"Error",
                    text: Translation('الرجاء قم بتحديد السنة وا الشهر'),
                    confirmButtonColor: "#66BB6A",
                    type: "warning"
                });
      }


    });

    
    $('#customer_filter_btn').on('click',function(){
      
    
        var d1            = $("#first_date").val();
        var d2            = $("#second_date").val();
        var customer_id   = $("#customer_id").val();
        var branch_id_fk  = $("#branch_id_fk").val();
        
        if (branch_id_fk =='') {
         
          branch_id_fk=BRANCH_ID;
        }
       
       
        var attach = `<thead>
                        <tr class="bg-blue-300">
                          <th>ID</th>
                          <th width=15% class="multi_lang">Customer Name</th>
                          <th width=5% class="multi_lang">Phone</th>
                          <th class="multi_lang">Total Amount</th>
                          <th class="multi_lang">Total Discount</th>
                          <th class="multi_lang">Total Paid</th>
                          <th class="multi_lang">Total Remained</th>
                          <th width=20% class="multi_lang">Number of Invoices</th>
                          <th width=5% class="multi_lang">Branch</th>
                          
                        </tr>
                      </thead>
                      <tbody>`;

        $("#table1").attr('style','');
       
        $.ajax({
                url: "models/crul_report.php",
                type:"POST",
                dataType : "JSON",
                data: {"type":"customer_account_report",
                        
                        d1 : d1,
                        d2 : d2,
                        customer_id : customer_id,
                        branch_id_fk : branch_id_fk

                      } ,
                complete:function(){
                  hideLoader();
                },
                beforeSend:function(){
                  showLoader("Please Wait While Gathering Report Information");
                },
                success: function(res) {
                  if(res.is_success == true){

                    var total_inv   = 0;
                    var total_inv_dis = 0;
                    var total_inv_p = 0;
                    var total_inv_r = 0;
                    
                    for(var i=0 ; i <=res.data.length-1;i++){

                      var remain  = res.data[i].total_invoice_price - res.data[i].total_payment_discount - res.data[i].total_paid;

                      var inv_no    = res.data[i].invoice_number;
                      var inv_date  = res.data[i].invoice_date;
                      var inv_pric  = res.data[i].invoice_price;
                      var sub_tbl ='';

                      if (res.data[i].No_of_inv > 0) {

                      

                        inv_no_arry   = inv_no.split(',');
                        inv_date_arry = inv_date.split(',');
                        inv_pric_arry = inv_pric.split(',');


                        for (var j = 0; j <inv_no_arry.length ; j++) {

                            // console.log(inv_date_arry[j]);
                            // console.log(i);

                            var s_date = inv_date_arry[j];
                            var j_date = s_date.split(' ');

                            sub_tbl+= `<tr>
                                          <td>${j+1}</td>
                                          <td>${inv_no_arry[j]}</td>
                                          <td>${j_date[0]}</td>
                                          <td>${currency_format_display(inv_pric_arry[j])}</td>
                                        </tr>`;
                        }
                      }



                      attach = attach + `
                                        <tr>
                                            <td>`+res.data[i].customer_id+`</td>
                                            <td>`+res.data[i].customer_full_name+`</td>
                                            <td>`+res.data[i].customer_phone+`</td>
                                            <td>`+currency_format_display(res.data[i].total_invoice_price)+`</td>
                                            <td>`+currency_format_display(res.data[i].total_payment_discount)+`</td>
                                            <td>`+currency_format_display(res.data[i].total_paid)+`</td>
                                            <td>`+currency_format_display(remain)+`</td>
                                            <td>

                                                <table style="text-align:center; font-size:14px;" border=1 cellpadding=0 cellspacing=0>
                                                  <thead>
                                                    <tr>
                                                      <th>#</th>
                                                      <th>Invoice No.</th>
                                                      <th>Date</th>
                                                      <th>Price</th>
                                                    </tr>
                                                  </thead>

                                                  <tbody>
                                                    ${sub_tbl}
                                                  </tbody>
                                                  <tfoot>
                                                    <tr>
                                                      <td colspan=3>Total Number</td>
                                                      <td >${res.data[i].No_of_inv}</td>
                                                    </tr>
                                                  </tfoot>
                                                </table>


                                            </td>
                                          
                                            <td>`+res.data[i].branch_name+`</td>

                                        </tr>
                                        `;
                      total_inv     = +total_inv + +(res.data[i].total_invoice_price);
                      total_inv_dis = +total_inv_dis + +(res.data[i].total_payment_discount);
                      total_inv_p   = +total_inv_p + +(res.data[i].total_paid);
                      total_inv_r   = +total_inv_r + +(remain);

                    }

                    attach = attach + `</tbody>
                                       <tfoot>
                                        <tr class="alpha-info">
                                            <td></td>
                                            <td></td>
                                            <td class="multi_lang">Totals</td>
                                            <td id="total_invoice">`+currency_format_display(total_inv)+`</td>
                                            <td id="total_invoice">`+currency_format_display(total_inv_dis)+`</td>
                                            <td id="total_invoice">`+currency_format_display(total_inv_p)+`</td>
                                            <td id="total_invoice">`+currency_format_display(total_inv_r)+`</td>
                                            <td></td>
                                            <td></td>
                                            
                                        </tr>
                                      </tfoot>
                                        `;

                    $("#table1").html(attach);
                    

                  }else{
                    swal({
                                title: Translation("خطأ"),
                                text: Translation(res.data),
                                confirmButtonColor: "#2196F3",
                                type: "error"
                            });
                  }
                }
        });

      
     


    });


    $('#loaner_filter_btn').on('click',function(){
      
      if($("#first_date").val()!='' && $("#second_date").val()!='' ){

        var d1            = $("#first_date").val();
        var d2            = $("#second_date").val();
        var loaner_id    = $("#loaner_id").val();
        
        

        var attach = `<thead>
                        <tr class="bg-blue-300">
                          <th>ID</th>
                          <th class="multi_lang">Loaner Name</th>
                          <th class="multi_lang">Invoice No.</th>
                          <th class="multi_lang">Amount</th>
                          <th class="multi_lang">Date</th>
                          <th class="multi_lang">Transaction</th>
                          <th class="multi_lang">Note</th>
                          <th class="multi_lang"> Paper Bill No. </th>
                          
                        </tr>
                      </thead>
                      <tbody>`;

        $("#table1").attr('style','');
       
        $.ajax({
                url: "models/crul_report.php",
                type:"POST",
                dataType : "JSON",
                data: {"type":"loan_report",
                        d1 : d1,
                        d2 : d2,
                        loaner_id : loaner_id
                        
                      } ,
                complete:function(){
                  hideLoader();
                },
                beforeSend:function(){
                  showLoader("Please Wait While Gathering Report Information");
                },
                success: function(res) {
                  if(res.is_success == true){

                    var total_inv = 0;
                    
                    for(var i=0 ; i <=res.data.length-1;i++){

                      attach = attach + `
                                        <tr>
                                            <td>`+res.data[i].payment_id+`</td>
                                            <td>`+res.data[i].loaner_full_name+`</td>
                                            <td>`+res.data[i].payment_number+`</td>
                                            <td>`+currency_format_display(res.data[i].paid_amount)+`</td>
                                            <td>`+res.data[i].paid_date+`</td>
                                            <td>`+res.data[i].payment_transaction+`</td>
                                            <td>`+res.data[i].payment_note+`</td>
                                            <td>`+res.data[i].paper_number+`</td>
                                            
                                        </tr>
                                        `;
                      total_inv = +total_inv + +(res.data[i].paid_amount);

                    }

                    attach = attach + `</tbody>
                                       <tfoot>
                                        <tr class="alpha-info">
                                            <td></td>
                                            <td></td>
                                            <td class="multi_lang">Total Loans</td>
                                            <td id="total_invoice">`+currency_format_display(res.data[0].total_loan)+`</td>
                                            <td class="multi_lang">Total Refund</td>
                                            <td id="total_invoice">`+currency_format_display(res.data[0].total_refund)+`</td>
                                           
                                            
                                            <td></td>
                                            <td></td>
                                            
                                            
                                        </tr>
                                      </tfoot>
                                        `;

                    $("#table1").html(attach);
                    

                  }else{
                    swal({
                                title: Translation("خطأ"),
                                text: Translation(res.data),
                                confirmButtonColor: "#2196F3",
                                type: "error"
                            });
                  }
                }
        });

      
        // window.location = _href;
      }else{
            swal({
                    title:"Error",
                    text: Translation('الرجاء قم بتحديد السنة وا الشهر'),
                    confirmButtonColor: "#66BB6A",
                    type: "warning"
                });
      }


    });

    
    $('#assets_filter_btn').on('click',function(){
      
    
        var d1            = $("#first_date").val();
        var d2            = $("#second_date").val();
        var branch_id_fk  = $("#branch_id_fk").val();
        
       
        var attach = `<thead>
                        <tr class="bg-blue-300">
                          <th>ID</th>
                          <th class="multi_lang">Asset Name</th>
                          <th class="multi_lang">Date</th>
                          <th class="multi_lang">Asset Price</th>
                          <th class="multi_lang">Invoice No.</th>
                          <th class="multi_lang">Note</th>
                          <th class="multi_lang">Branch Safe</th>
                          
                        </tr>
                      </thead>
                      <tbody>`;

        $("#table1").attr('style','');
       
        $.ajax({
                url: "models/crul_report.php",
                type:"POST",
                dataType : "JSON",
                data: {"type":"assets_report",
                        
                        d1 : d1,
                        d2 : d2,
                        branch_id_fk : branch_id_fk

                      } ,
                complete:function(){
                  hideLoader();
                },
                beforeSend:function(){
                  showLoader("Please Wait While Gathering Report Information");
                },
                success: function(res) {
                  if(res.is_success == true){

                    var total_inv   = 0;
                    var total_inv_ex = 0;
                    
                    for(var i=0 ; i <=res.data.length-1;i++){

                      attach = attach + `
                                        <tr>
                                            <td>`+res.data[i].asset_id+`</td>
                                            <td>`+res.data[i].expenses_name+`</td>
                                            <td>`+res.data[i].expenses_date+`</td>
                                            <td>`+currency_format_display(res.data[i].expenses_unit_price)+`</td>
                                            <td>`+res.data[i].bill_number+`</td>
                                            
                                            <td>`+res.data[i].expenses_note+`</td>
                                            <td>`+res.data[i].branch_name+`</td>

                                        </tr>
                                        `;
                      total_inv = +total_inv + +(res.data[i].expenses_unit_price);
                      // total_inv_ex = +total_inv_ex + +(res.data[i].total_price_before_expenses);

                    }

                    attach = attach + `</tbody>
                                       <tfoot>
                                        <tr class="alpha-info">
                                            <td></td>
                                            <td></td>
                                            <td class="multi_lang">Totals</td>
                                            <td id="total_invoice">`+currency_format_display(total_inv)+`</td>
                                            <td></td>
                                            <td></td>
                                            <td></td>
                                            
                                        </tr>
                                      </tfoot>
                                        `;

                    $("#table1").html(attach);
                    

                  }else{
                    swal({
                                title: Translation("خطأ"),
                                text: Translation(res.data),
                                confirmButtonColor: "#2196F3",
                                type: "error"
                            });
                  }
                }
        });

      
     


    });


    $('#owner_filter_btn').on('click',function(){
      
    
        var d1            = $("#first_date").val();
        var d2            = $("#second_date").val();
        var owner_id      = $("#owner_id").val();
        
       
        var attach = `<thead>
                        <tr class="bg-blue-300">
                          <th>ID</th>
                          <th class="multi_lang">Owner Name</th>
                          <th class="multi_lang">Date</th>
                          <th class="multi_lang">Amount</th>
                          <th class="multi_lang">Invoice No.</th>
                          <th class="multi_lang">Note</th>
                          <th class="multi_lang">Transaction</th>
                          
                        </tr>
                      </thead>
                      <tbody>`;

        $("#table1").attr('style','');
       
        $.ajax({
                url: "models/crul_report.php",
                type:"POST",
                dataType : "JSON",
                data: {"type":"owner_report",
                        
                        d1 : d1,
                        d2 : d2,
                        owner_id : owner_id

                      } ,
                complete:function(){
                  hideLoader();
                },
                beforeSend:function(){
                  showLoader("Please Wait While Gathering Report Information");
                },
                success: function(res) {
                  if(res.is_success == true){

                    var total_inv   = 0;
                    var total_inv_ex = 0;
                    
                    for(var i=0 ; i <=res.data.length-1;i++){

                      attach = attach + `
                                        <tr>
                                            <td>`+res.data[i].capital_id+`</td>
                                            <td>`+res.data[i].owner_full_name+`</td>
                                            <td>`+res.data[i].date_of_add+`</td>
                                            <td>`+currency_format_display(res.data[i].capital_amount)+`</td>
                                            <td>`+res.data[i].capital_number+`</td>
                                            
                                            <td>`+res.data[i].capital_note+`</td>
                                            <td>`+res.data[i].capital_transaction+`</td>

                                        </tr>
                                        `;
                      total_inv = +total_inv + +(res.data[i].capital_amount);
                      // total_inv_ex = +total_inv_ex + +(res.data[i].total_price_before_expenses);

                    }

                    attach = attach + `</tbody>
                                       <tfoot>
                                        <tr class="alpha-info">
                                            <td></td>
                                            <td></td>
                                            <td class="multi_lang">Totals</td>
                                            <td id="total_invoice">`+currency_format_display(total_inv)+`</td>
                                            
                                            <td></td>
                                            <td></td>
                                            <td></td>
                                            
                                            
                                        </tr>
                                      </tfoot>
                                        `;

                    $("#table1").html(attach);
                    

                  }else{
                    swal({
                                title: Translation("خطأ"),
                                text: Translation(res.data),
                                confirmButtonColor: "#2196F3",
                                type: "error"
                            });
                  }
                }
        });

      
     


    });


    $('#safe_btn').on('click',function(){
      
      if($("#first_date").val()!='' && $("#second_date").val()!='' ){

        var d1            = $("#first_date").val();
        var d2            = $("#second_date").val();
        var branch_id_fk  = $("#branch_id_fk").val();
        
        var _href ="index.php?p=report";

        if(d1 !=''){
         
          _href = _href + "&d1="+d1;
        }

        if(d2 !=''){
         
          _href = _href + "&d2="+d2;
        }

        var attach = `<thead>
                        <tr class="bg-blue-300">
                          <th>ID</th>
                          <th class="multi_lang">Bill No.</th>
                          <th class="multi_lang">Amount</th>
                          <th class="multi_lang">Date</th>
                          <th class="multi_lang">Transaction</th>
                          <th class="multi_lang">Type</th>
                          <th class="multi_lang">Staff</th>
                          <th class="multi_lang"> Branch </th>
                          <th class="multi_lang"> Note </th>
                        </tr>
                      </thead>
                      <tbody>`;

        $("#table1").attr('style','');
       
        $.ajax({
                url: "models/crul_report.php",
                type:"POST",
                dataType : "JSON",
                data: {"type":"safe_report",
                        d1 : d1,
                        d2 : d2,
                        branch_id_fk : branch_id_fk

                      } ,
                complete:function(){
                  hideLoader();
                },
                beforeSend:function(){
                  showLoader("Please Wait While Gathering Report Information");
                },
                success: function(res) {
                  if(res.is_success == true){

                    var total_inv = 0;
                    
                    for(var i=0 ; i <=res.data.length-1;i++){
                      var typ = "";
                      if(res.data[i].transaction_type == 1){
                        typ = "<span class='label label-success'>In to Safe</span>"
                      }else{
                        typ = "<span class='label label-warning'>Out from Safe</span>"
                      }

                      attach = attach + `
                                        <tr>
                                            <td>`+res.data[i].safe_id+`</td>
                                            <td>`+res.data[i].safe_number+`</td>
                                            <td>`+currency_format_display(res.data[i].safe_amount)+`</td>
                                            <td>`+res.data[i].date_of_add+`</td>
                                            <td>`+res.data[i].safe_transaction+`</td>
                                            <td>`+typ +`</td>
                                            <td>`+res.data[i].full_name+`</td>
                                            <td>`+res.data[i].branch_name+`</td>
                                            <td>`+res.data[i].safe_note+`</td>
                                        </tr>
                                        `;
                      // total_inv = +total_inv + +(res.data[i].final_price);

                    }

                    attach = attach + ` </tbody>
                                        <tfoot>
                                          <tr class="alpha-info">
                                              <td></td>
                                              <td class="multi_lang">Total In to Safe</td>
                                              <td id="total_in">`+currency_format_display(res.data[0].total_amount)+`</td>
                                              <td class="multi_lang">Total Out from Safe</td>
                                              <td id="total_out">`+currency_format_display(res.data[0].total_subtract)+`</td>
                                              
                                              <td></td>
                                              <td></td>
                                              <td></td>
                                              <td></td>
                                              
                                          </tr>
                                        </tfoot>
                                        `;

                    $("#table1").html(attach);
                    

                  }else{
                    swal({
                                title: Translation("خطأ"),
                                text: Translation(res.data),
                                confirmButtonColor: "#2196F3",
                                type: "error"
                            });
                  }
                }
        });

      
        // window.location = _href;
      }else{
            swal({
                    title:"Error",
                    text: Translation('الرجاء قم بتحديد السنة وا الشهر'),
                    confirmButtonColor: "#66BB6A",
                    type: "warning"
                });
      }


    });


    $('#saleman_btn').on('click',function(){
      
      if($("#first_date").val()!='' && $("#second_date").val()!='' ){

        var d1            = $("#first_date").val();
        var d2            = $("#second_date").val();
        var branch_id_fk  = $("#branch_id_fk").val();
        var saleman_id    = $("#saleman_id").val();
        
        var _href ="index.php?p=report";

        if(d1 !=''){
         
          _href = _href + "&d1="+d1;
        }

        if(d2 !=''){
         
          _href = _href + "&d2="+d2;
        }

        var attach = `<thead>
                        <tr class="bg-blue-300">
                          <th>ID</th>
                          <th class="multi_lang">Customer</th>
                          <th class="multi_lang">Payment No.</th>
                          <th class="multi_lang">Amount</th>
                          <th class="multi_lang">Date</th>
                          <th class="multi_lang">Transaction</th>
                          <th class="multi_lang">Note</th>
                          <th class="multi_lang"> Paper Bill No. </th>
                          <th class="multi_lang"> Saleman </th>
                        </tr>
                      </thead>
                      <tbody>`;

        $("#table1").attr('style','');
       
        $.ajax({
                url: "models/crul_report.php",
                type:"POST",
                dataType : "JSON",
                data: {"type":"saleman_report",
                        d1 : d1,
                        d2 : d2,
                        saleman_id:saleman_id,
                        branch_id_fk : branch_id_fk

                      } ,
                complete:function(){
                  hideLoader();
                },
                beforeSend:function(){
                  showLoader("Please Wait While Gathering Report Information");
                },
                success: function(res) {
                  if(res.is_success == true){

                    var total_pay = res.data[0].total_paid;
                    var total_dis = res.data[0].total_account_discount;
                    var total_ret = res.data[0].total_returned;
                    
                    for(var i=0 ; i <=res.data.length-1;i++){
                      

                      attach = attach + `
                                        <tr>
                                            <td>`+res.data[i].payment_id+`</td>
                                            <td>`+res.data[i].customer_full_name+`</td>
                                            <td>`+res.data[i].payment_number+`</td>
                                            <td>`+currency_format_display(res.data[i].paid_amount)+`</td>
                                            <td>`+res.data[i].paid_date+`</td>
                                            <td>`+res.data[i].payment_transaction+`</td>
                                            <td>`+res.data[i].payment_note+`</td>
                                            <td>`+res.data[i].paper_number+`</td>
                                            <td>`+res.data[i].saleman_full_name+`</td>
                                        </tr>
                                        `;
                      // total_inv = +total_inv + +(res.data[i].final_price);

                    }

                    attach = attach + ` </tbody>
                                        <tfoot>
                                          <tr class="alpha-info">
                                              <td></td>
                                              <td class="multi_lang">Total Paid</td>
                                              <td id="total_in">`+currency_format_display(res.data[0].total_paid)+`</td>
                                              <td class="multi_lang">Total Discount</td>
                                              <td id="total_out">`+currency_format_display(res.data[0].total_account_discount)+`</td>
                                              <td class="multi_lang">Total Return</td>
                                              <td id="total_out">`+currency_format_display(res.data[0].total_returned)+`</td>
                                              
                                              <td></td>
                                              <td></td>
                                              
                                              
                                          </tr>
                                        </tfoot>
                                        `;

                    $("#table1").html(attach);
                    

                  }else{
                    swal({
                                title: Translation("خطأ"),
                                text: Translation(res.data),
                                confirmButtonColor: "#2196F3",
                                type: "error"
                            });
                  }
                }
        });

      
        // window.location = _href;
      }else{
            swal({
                    title:"Error",
                    text: Translation('الرجاء قم بتحديد السنة وا الشهر'),
                    confirmButtonColor: "#66BB6A",
                    type: "warning"
                });
      }


    });
});





$(function() {
//var singleSelect = $('.datatable-column-search-inputs').DataTable();
  var query_y = getQueryVariable("d1");
  var query_m = getQueryVariable("d2");

  if(query_y != 'not' ){
    $("#first_date").val(query_y);
  }

  if( query_m != 'not'){
    $("#second_date").val(query_m);
  }


  $(".touchspin-basic").TouchSpin({
      min: 1,
      max: 1000000,
      step: 1
  });

  $('.daterange-single').daterangepicker({ 
        singleDatePicker: true,
        locale: {
            format: 'YYYY-MM-DD'
        }
    });

  




    // end highlight row and column


    // Single row selection
    
  


    $('.dataTables_length select').select2({
        minimumResultsForSearch: Infinity,
        width: 'auto'
    });


});



