var table ,  block = $(".content-wrapper").parent();
    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;
                          }

                          .col-sm-12 {
                            width: 100%;
                          }
                          .col-sm-6 {
                            width: 50%;
                          }

                          .col-sm-4 {
                            width: 33.33333333%;
                          }
                          
                          .col-sm-3 {
                            width: 25%;
                          }

                        </style>

                      </head>

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

   
    
     var table_invoice     =`
                            <html>

                              <head>
                                  <meta http-equiv=Content-Type content="text/html; charset=unicode">
                                  <meta name=Generator content="Microsoft Word 15 (filtered)">
                                  <style>
                                      <!--
                                      /* Font Definitions */
                                      
                                      @font-face {
                                          font-family: "Cambria Math";
                                          panose-1: 2 4 5 3 5 4 6 3 2 4;
                                      }
                                      
                                      @font-face {
                                          font-family: Calibri;
                                          panose-1: 2 15 5 2 2 2 4 3 2 4;
                                      }
                                      
                                      @font-face {
                                          font-family: "Calibri Light";
                                          panose-1: 2 15 3 2 2 2 4 3 2 4;
                                      }
                                      
                                      @font-face {
                                          font-family: "Century Gothic";
                                          panose-1: 2 11 5 2 2 2 2 2 2 4;
                                      }
                                      
                                      @font-face {
                                          font-family: "Sakkal Majalla";
                                          panose-1: 2 0 0 0 0 0 0 0 0 0;
                                      }
                                      /* Style Definitions */
                                      
                                      p.MsoNormal,
                                      li.MsoNormal,
                                      div.MsoNormal {
                                          margin-top: 0in;
                                          margin-right: 0in;
                                          margin-bottom: 8.0pt;
                                          margin-left: 0in;
                                          line-height: 105%;
                                          font-size: 11.0pt;
                                          font-family: "Calibri", sans-serif;
                                      }
                                      
                                      h2 {
                                          mso-style-link: "Heading 2 Char";
                                          margin: 0in;
                                          margin-bottom: .0001pt;
                                          page-break-after: avoid;
                                          font-size: 8.0pt;
                                          font-family: "Calibri Light", sans-serif;
                                          color: #262626;
                                          font-weight: normal;
                                      }
                                      
                                      p.MsoHeader,
                                      li.MsoHeader,
                                      div.MsoHeader {
                                          mso-style-link: "Header Char";
                                          margin: 0in;
                                          margin-bottom: .0001pt;
                                          font-size: 8.0pt;
                                          font-family: "Calibri", sans-serif;
                                          color: #262626;
                                      }
                                      
                                      a:link,
                                      span.MsoHyperlink {
                                          color: blue;
                                          text-decoration: underline;
                                      }
                                      
                                      a:visited,
                                      span.MsoHyperlinkFollowed {
                                          color: purple;
                                          text-decoration: underline;
                                      }
                                      
                                      span.Heading2Char {
                                          mso-style-name: "Heading 2 Char";
                                          mso-style-link: "Heading 2";
                                          font-family: "Calibri Light", sans-serif;
                                          color: #262626;
                                      }
                                      
                                      p.msonormal0,
                                      li.msonormal0,
                                      div.msonormal0 {
                                          mso-style-name: msonormal;
                                          margin-right: 0in;
                                          margin-left: 0in;
                                          font-size: 12.0pt;
                                          font-family: "Times New Roman", serif;
                                      }
                                      
                                      span.HeaderChar {
                                          mso-style-name: "Header Char";
                                          mso-style-link: Header;
                                          font-family: "Calibri", sans-serif;
                                          color: #262626;
                                      }
                                      
                                      p.msopapdefault,
                                      li.msopapdefault,
                                      div.msopapdefault {
                                          mso-style-name: msopapdefault;
                                          margin-right: 0in;
                                          margin-bottom: 8.0pt;
                                          margin-left: 0in;
                                          line-height: 105%;
                                          font-size: 12.0pt;
                                          font-family: "Times New Roman", serif;
                                      }
                                      
                                      p.msochpdefault,
                                      li.msochpdefault,
                                      div.msochpdefault {
                                          mso-style-name: msochpdefault;
                                          margin-right: 0in;
                                          margin-left: 0in;
                                          font-size: 10.0pt;
                                          font-family: "Times New Roman", serif;
                                      }
                                      
                                      p.Slogan,
                                      li.Slogan,
                                      div.Slogan {
                                          mso-style-name: Slogan;
                                          margin-top: 0in;
                                          margin-right: 0in;
                                          margin-bottom: 3.0pt;
                                          margin-left: 0in;
                                          font-size: 7.0pt;
                                          font-family: "Calibri", sans-serif;
                                          color: #A6A6A6;
                                          letter-spacing: .2pt;
                                          font-style: italic;
                                      }
                                      
                                      p.ContactInfo,
                                      li.ContactInfo,
                                      div.ContactInfo {
                                          mso-style-name: "Contact Info";
                                          margin-top: 26.0pt;
                                          margin-right: 0in;
                                          margin-bottom: 0in;
                                          margin-left: 0in;
                                          margin-bottom: .0001pt;
                                          text-align: center;
                                          font-size: 8.0pt;
                                          font-family: "Calibri", sans-serif;
                                          color: #A6A6A6;
                                      }
                                      
                                      p.ThankYou,
                                      li.ThankYou,
                                      div.ThankYou {
                                          mso-style-name: "Thank You";
                                          margin: 0in;
                                          margin-bottom: .0001pt;
                                          text-align: center;
                                          font-size: 9.0pt;
                                          font-family: "Calibri", sans-serif;
                                          color: #262626;
                                          font-weight: bold;
                                          font-style: italic;
                                      }
                                      
                                      span.companyphones {
                                          mso-style-name: company_phones;
                                      }
                                      
                                      span.companyemail {
                                          mso-style-name: company_email;
                                      }
                                      
                                      span.invoiceno {
                                          mso-style-name: invoice_no;
                                      }
                                      
                                      span.invoicedate {
                                          mso-style-name: invoice_date;
                                      }
                                      
                                      span.deliverystatus {
                                          mso-style-name: delivery_status;
                                      }
                                      
                                      span.destination {
                                          mso-style-name: destination;
                                      }
                                      
                                      .MsoChpDefault {
                                          font-size: 10.0pt;
                                      }
                                      
                                      @page WordSection1 {
                                          size: 841.9pt 595.3pt;
                                          margin: .5in .5in .5in .5in;
                                      }
                                      
                                      div.WordSection1 {
                                          page: WordSection1;
                                      }
                                      
                                      -->

                                      @media print {
                                          body{
                                              width: 21cm;
                                              height: 29.7cm;
                                              margin: 20mm 20mm 20mm 20mm; 
                                              /* change the margins as you want them to be. */
                                         } 

                                         thead {display: table-header-group;}
                                      }

                                   
                                  </style>

                              </head>

                              <body lang=EN-US link=blue vlink=purple>

                                  <div class=WordSection1>

                                      <div align=center>
                              `;

    

   
    function account_statement_date(itm, func_name, func_param=''){
      
      $("#customer_id_fk").val(itm);
      $("#func_name").val(func_name);
      $("#func_param").val(func_param);

      $('#account_date_modal').modal('toggle');



    }

    $('#btn_display_all').click(function(e) {
        e.preventDefault();

        var func_name   = $("#func_name").val();
        var itm         = $("#customer_id_fk").val();
        var func_param  = $("#func_param").val();


        switch (func_name){

          case 'show_transaction':
            show_transaction(itm);
            break;

          case 'show_summary':
            show_summary(itm,func_param);
            break;

          case 'show_withdraw_summary':
            show_withdraw_summary(itm, func_param);
            break;



        }

        $('#account_date_modal').modal('toggle');

         
    });





    function show_transaction(itm,opt=null,date1=null,date2=null){

          var headers           ='';
          var attach            = "";
          var attach_other      = "";
          var footers           ="";
          var customer_details  = "";
          var print_attach      = "";
          var total_pay         = "";
          var show_page         = 1;
          var titels            = '';

          

          $.ajax({
                url : 'models/crul_account_statement.php',
                type : 'POST',
                data : {  type:'show_transaction',
                          date_operation : opt,
                          date1 : date1,
                          date2 : date2,
                          id : itm},
                dataType : 'json',
                success : function(response) {
                    console.log(response)
                  if(response.invoice.length > 0){

                      
                      titels = Translation('جميع عمليات الحسابات للزبون حسب التاريخ');
                   
                      headers =`
                                    <table  dir=rtl border=1 cellspacing=0 cellpadding=0
                                       style='background:white;border-collapse:collapse;border:none; font-weight:bold;'>
                                      
                                      
                                       
                                       <tr style='height:9.95pt'>
                                        <td  colspan=4 style='width:221.4pt; text-align : right !important;' class="table0_td">
                                       `+Translation('موبايل')+`  : <span dir="ltr" id="inv_phone"> `+COMPANY_PHONE+` </span>
                                        <br>
                                       `+Translation('البريد الالكتروني')+`  : <span dir="ltr" id="inv_email"> `+COMPANY_EMAIL+` </span>
                                        </td>

                                        <td   colspan=2 rowspan=2 style='width:180.4pt;' class="table0_td">
                                        <span id="inv_logo"> 
                                        <img src="`+COMPANY_LOGO+`" height="150px" align="left">
                                         </span>
                                        </td>

                                       </tr>
                                       
                                       <tr style='height:9.95pt'>
                                        <td colspan=6 width=118 style='width:88.55pt;' class="table0_td1">
                                            <span id="inv_address"> `+Translation(COMPANY_ADDRESS)+` </span>
                                          
                                        </td>
                                       </tr>
                                       
                                       <tr>
                                        <td width=590 colspan=6 class="table0_td1" style='text-align:center; width:6.15in;border:none;padding:0in 5.4pt 0in 5.4pt'>       
                                          <span style=" font-size: 20px;" id="inv_slogan">`+titels+`</span>
                                        </td>
                                       </tr>
                                       
                                      </table>

                                    
                                  
                                          `;
                        
                      customer_details = `

                                            <table  dir=rtl border=1 cellspacing=0 cellpadding=0  width=595 style='width:6.2in;border-collapse:collapse;border:none'>
                                             <tr style='page-break-inside:avoid;' class="td_head">
                                              <td width=307 style='width:3.2in;' class="table1_td">
                                              اسم الزبون
                                              </td>
                                              <td width=144 style='width:1.5in;' class="table1_td">
                                              رقم الهاتف
                                              </td>
                                              <td width=144 style='width:1.5in;' class="table1_td last_td">
                                              العنوان
                                              </td>
                                             </tr>
                                             <tr style='page-break-inside:avoid;' class="td_body">
                                              <td width=307 style='width:3.2in;' class="table1_td1">
                                              <span  id="inv_customer_name"> `+response.invoice[0].customer_full_name+` </span>
                                              </td>
                                              <td width=144 style='width:1.5in;' class="table1_td1">
                                              <span  id="inv_customer_phone"> `+response.invoice[0].customer_phone+` </span>
                                              </td>
                                              <td width=144 style='width:1.5in;' class="table1_td1">
                                              <span  id="inv_customer_address"> `+response.invoice[0].customer_address+` </span>
                                              </td>
                                             </tr>
                                            </table>
                                            <br>
                                            `;

                      attach =  `
                                  <table id="inv_table"  dir=rtl border=1 cellspacing=0 cellpadding=0 width=591 style='width:443.5pt;border-collapse:collapse;border:#000000; '>
                                   <tr style='page-break-inside:avoid;' class="td_head">
                                    <td width=23 style='width:14.4pt;' class="table2_td">
                                    `+Translation('ت')+`
                                    </td>
                                    <td width=81.4 style='width:61.05pt;' class="table2_td">
                                      `+Translation('رقم الوصل')+`
                                    </td>
                                    <td width=81.4 style='width:61.05pt;' class="table2_td">
                                      `+Translation('تاريخ الوصل')+`
                                    </td>
                                    <td width=81.4 style='width:61.05pt;' class="table2_td">
                                      `+Translation('Transaction')+`
                                    </td>
                                    <td width=81.4 style='width:61.05pt;' class="table2_td ">
                                    `+Translation('المبلغ المطلوب')+`
                                    </td>
                                    <td width=81.4 style='width:61.05pt;' class="table2_td">
                                    `+Translation('المبلغ المدفوع')+`
                                    </td>
                                    <td width=81.4 style='width:61.05pt;' class="table2_td">
                                   `+Translation('النوع')+` 
                                    </td>
                                    <td width=152 style='width:113.85pt;' class="table2_td last_td">
                                    `+Translation('ملاحظات')+`
                                    </td>
                                    
                                    
                                   </tr>`;
                     
                      // show sell item details   
                      var sum_total_cost      = 0;
                      var sum_total_paid      = 0;
                      var sum_total_discount  = 0;
                      var sum_total_return    = 0;
                      var sum_total_remaind   = 0;

                      var prev_transaction    = '';
                      var curr_transaction    = '';

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

                        // compute totals
                          sum_total_cost     = +sum_total_cost + +response.invoice[i].Debits;
                          sum_total_paid     = +sum_total_paid + +response.invoice[i].credits;
                          
                       //check types
                        var inv_typ = '';

                        switch  (response.invoice[i].transaction_type){

                          case 'Withdraw':
                            inv_typ = `<span style="color:orange; font-size:12px;">`+Translation('قرض')+`</span>`;
                            break;

                          case '0':
                            inv_typ = `<span style="color:red; font-size:12px;">`+Translation('وصل مواد خام')+`</span>`;
                            break;

                          case '1':
                            if(response.invoice[i].is_single == '0'){
                              inv_typ = `<span style="color:blue; font-size:12px;">`+Translation('وصل ستائر جملة')+`</span>`;
                            }else{
                              inv_typ = `<span style="color:purple; font-size:12px;">`+Translation('وصل ستائر مفرد')+`</span>`;
                            }

                            break;

                          default :
                            
                            var clss = '';

                            if(response.invoice[i].is_single != null && response.invoice[i].is_single != '0' && response.invoice[i].is_single != '1'){
                              clss = Translation(' - صنف : ') + response.invoice[i].is_single;
                            }

                            if(response.invoice[i].transaction_ == 'Payment'){
                              
                              if(response.invoice[i].transaction_type == 'Raw'){
                                inv_typ = `<span style="color:green; font-size:12px;">`+Translation('تسديد دفعه - مواد جملة')+ clss +`</span>`;
                                
                              }else if(response.invoice[i].transaction_type == 'Single'){
                                inv_typ = `<span style="color:green; font-size:12px;">`+Translation('تسديد دفعه - ستائر مفرد')+ clss +`</span>`;

                              }else if(response.invoice[i].transaction_type == 'Plural'){
                                inv_typ = `<span style="color:green; font-size:12px;">`+Translation('تسديد دفعه - ستائر جملة')+ clss +`</span>`;

                              }else{
                                inv_typ = `<span style="color:green; font-size:12px;">`+Translation('تسديد دفعه عامة')+ clss +`</span>`;

                              }
                            
                            }else if (response.invoice[i].transaction_ == 'Discount'){
                              
                              inv_typ = `<span style="color:BLUE; font-size:12px;">`+Translation('خصم من الحساب')+`</span>`;
                            
                            }else if (response.invoice[i].transaction_ == 'Return'){
                              
                              inv_typ = `<span style="color:MAGENTA; font-size:12px;">`+Translation('استرجاع مبلغ من الحساب')+`</span>`;
                            
                            }

                            break;

                        }

                        


                        var transaction_typ = response.invoice[i].transaction_;
                        if( transaction_typ == 'Withdraw'){
                          transaction_typ = `<span style="color:orange; font-weight:bold;">`+transaction_typ+`</span>`;
                          
                        }else if(transaction_typ == 'Payment'){
                          transaction_typ = `<span style="color:green; font-weight:bold;">`+transaction_typ+`</span>`;

                        }else if(transaction_typ == 'Invoice'){
                          transaction_typ = `<span style="color:red; font-weight:bold;">`+transaction_typ+`</span>`;

                        }else if(transaction_typ == 'Discount'){
                          transaction_typ = `<span style="color:BLUE; font-weight:bold;">`+transaction_typ+`</span>`;

                        }else if(transaction_typ == 'Return'){
                          transaction_typ = `<span style="color:MAGENTA; font-weight:bold;">`+transaction_typ+`</span>`;

                        }else{
                          
                            transaction_typ = `<span style="color:purple; font-weight:bold;">`+transaction_typ+`</span>`;

                         
                          
                        }
                        
                        
                        

                        attach = attach + `
                                                  <tr style='page-break-inside:avoid;' class="td_body">
                                                    <td width=23 style='width:14.4pt;'>
                                                      <span  id="inv_count"> `+(i+1)+` </span>
                                                    </td>
                                                    <td width=81.4 style='width:61.05pt;'>
                                                      <span  id="inv_product"> `+response.invoice[i].voucher_no+` </span>
                                                    </td>
                                                    <td width=81.4 style='width:61.05pt;'>
                                                      <span  id="inv_quantity"> `+response.invoice[i].date_+` </span>
                                                    </td>
                                                    <td width=81.4 style='width:61.05pt;'>
                                                      <span  id="inv_quantity"> `+transaction_typ+` </span>
                                                    </td>
                                                    <td width=81.4 style='width:61.05pt;' class="last_td">
                                                      <span  id="inv_total_row"> `+currency_format_display(response.invoice[i].Debits)+`  </span>
                                                    </td>
                                                    <td width=81.4 style='width:61.05pt; font-family:  Times, serif;'>
                                                      <span  id="inv_details"> `+currency_format_display(response.invoice[i].credits)+` </span>
                                                    </td>
                                                    <td width=81.4  style='width:61.05pt;'>
                                                      <span  id="inv_total_length"> `+inv_typ+`  </span>
                                                    </td>
                                                    <td width=152 style='width:113.85pt; font-size:12px;'>
                                                      <span  id="inv_unit_price"> `+(response.invoice[i].note)+` </span>
                                                    </td>
                                                    
                                                    
                                                   </tr>
                                                  `;

                        
                        
                        curr_transaction   = response.invoice[i].transaction_;

                        if( (prev_transaction =="Invoice" || prev_transaction =="Payment" || prev_transaction =="Withdraw" || prev_transaction =="Discount" || prev_transaction =="Return") && (curr_transaction =="Payment" || curr_transaction == "Discount" || curr_transaction == "Return")){
                           
                           var subt = sum_total_cost - sum_total_paid;
                           
                           attach = attach + `
                                                  <tr style='page-break-inside:avoid; background-color:CYAN' class="td_body">
                                                    <td width=23 colspan=4 style='width:14.4pt;'>
                                                      <span  id="inv_count"> المتبقي </span>
                                                    </td>
                                                    
                                                    <td width=81.4 colspan=3 style='width:61.05pt;'>
                                                      <span  id="inv_quantity"> `+currency_format_display(subt)+` </span>
                                                    </td>
                                                    <td width=81.4 style='width:61.05pt;'>
                                                      <span  id="inv_quantity"> تصفية </span>
                                                    </td>
                                                    
                                                    
                                                    
                                                   </tr>
                                                  `;

                        }else{

                          if(i > 0){
                           var subt = sum_total_cost - sum_total_paid;

                            attach = attach + `
                                                  <tr style='page-break-inside:avoid; background-color:#cfff00' class="td_body">
                                                    <td width=23 colspan=4 style='width:14.4pt;'>
                                                      <span  id="inv_count"> المبلغ المطلوب</span>
                                                    </td>
                                                    
                                                    <td width=81.4 colspan=3 style='width:61.05pt;'>
                                                      <span  id="inv_quantity"> `+currency_format_display(subt)+` </span>
                                                    </td>
                                                    <td width=81.4 style='width:61.05pt;'>
                                                      <span  id="inv_quantity"> تصفية </span>
                                                    </td>
                                                    
                                                    
                                                    
                                                   </tr>
                                                  `;
                          }
                        }

                        prev_transaction   = response.invoice[i].transaction_;
                       
                       
                      } // for i

                      // add withdraw total_paid and Total Remained


                      
                      sum_total_remaind   = sum_total_cost - sum_total_paid;
                        

                      footers =`
                                 
                                 <tr style='page-break-inside:avoid;height:.1in'>
                                  <td width=517 colspan=7 valign=top style='width:388.0pt;border:none;
                                  border-left:solid #984806 1.0pt;background:white;padding:2.15pt 5.75pt 2.15pt 5.75pt;
                                  height:.1in'>
                                  `+Translation('المبلغ الكلي')+`
                                  </td>
                                  <td width=74 style='width:55.5pt;border-top:none;border-left:solid #984806 1.0pt;
                                  border-bottom:solid #984806 1.0pt;border-right:none;
                                  padding:0pt .1in 0pt .1in;height:.1in'>
                                   `+currency_format_display(sum_total_cost)+`
                                  </td>
                                 </tr>
                                 <tr style='page-break-inside:avoid;height:.1in'>
                                  <td width=517 colspan=7 valign=top style='width:388.0pt;border:none;
                                  border-left:solid #984806 1.0pt;background:white;padding:2.15pt 5.75pt 2.15pt 5.75pt;
                                  height:.1in'>
                                  `+Translation('المبلغ المدفوع')+`
                                  </td>
                                  <td width=74 style='width:55.5pt;border-top:none;border-left:solid #984806 1.0pt;
                                  border-bottom:solid #984806 1.0pt;border-right:none;
                                  padding:0pt .1in 0pt .1in;height:.1in'>
                                  `+currency_format_display(sum_total_paid)+`
                                  </td>
                                 </tr>
                                 <tr style='page-break-inside:avoid;height:.1in'>
                                  <td width=517 colspan=7 valign=top style='width:388.0pt;border:none;
                                  border-left:solid #984806 1.0pt;background:white;padding:2.15pt 5.75pt 2.15pt 5.75pt;
                                  height:.1in'>
                                  `+Translation('المبلغ المتبقي')+`
                                  </td>
                                  <td width=74 style='width:55.5pt;border-top:none;border-left:solid #984806 1.0pt;
                                  border-bottom:solid #984806 1.0pt;border-right:none;
                                  padding:0pt .1in 0pt .1in;height:.1in'>
                                  `+currency_format_display(sum_total_remaind)+`
                                  </td>
                                 </tr>
                                 <tr style='page-break-inside:avoid;height:.3in'>
                                 
                                  <td width=517 colspan=8  style='width:388.0pt;border:none;background:white;
                                    padding:2.15pt 5.75pt 2.15pt 5.75pt;height:.1in'>
                                    <span id="inv_note">`+Translation('ملاحظة')+` : `+''+` </span>
                                  </td>
                                 </tr>
                                 
                                 <tr style='page-break-inside:avoid;height:.1in; background-color: white'>
                                  <td width=517 colspan=7  style='width:388.0pt;border:none;
                                  padding:2.15pt 5.75pt 2.15pt 5.75pt;height:.1in'>
                                  
                                  </td>
                                  <td width=74 style='width:55.5pt;border:none;padding:2.15pt .15in 2.15pt .15in;
                                  height:.1in'>
                                  `+Translation('التوقيع')+`
                                  </td>
                                 </tr>
                                </table>

                                </div>




                                </body>

                                `+print_attach+`

                                </html>`;

                  }else{
                    show_page = 0;
                    
                  }    

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

                      HTML_VIEW_PAY = HTML_Table + headers + customer_details + attach + attach_other + footers;
                       //  me.data('requestRunning', false);
                     if(show_page==1){
                        $("#invoice_show").html(HTML_VIEW_PAY);
                        $("#inv_numb").val(itm);

                        $('#show_payments_modal').modal('toggle');
                     }else{
                      noty({
                            width: 200,
                            text: Translation('لا توجد وصولات بيع لهذا النوع'),
                            type: 'error',
                            dismissQueue: true,
                            timeout: 2000,
                            layout: 'topRight',
                      });
                     }
                       
                      
                     
                        // window.open().document.write(HTML_VIEW);
                }

          }); // submit ajax


    }

    function show_summary(itm,typ=null,opt=null,date1=null,date2=null){

          var headers           ='';
          var attach            = "";
          var attach_other      = "";
          var footers           ="";
          var customer_details  = "";
          var print_attach      = "";
          var total_pay         = "";
          var show_page         = 1;
          var titels            = '';

          

          $.ajax({
                url : 'models/crul_account_statement.php',
                type : 'POST',
                data : {type:'show_invoices_summary',
                        id : itm,
                        date_operation : opt,
                        date1 : date1,
                        date2 : date2,
                        typ:typ},
                dataType : 'json',
                success : function(response) {
                    console.log(response)
                  if(response.invoice.length > 0){

                      if(typ == null){
                        titels = Translation(' - جميع الوصولات');
                      }else if(typ == 'raw'){
                        // titels = ' - المواد الخامة';
                        titels = Translation(' - المواد الخامة');

                      }else if(typ == 'm'){
                        // titels = ' - ستائر الجملة';
                        titels = Translation(' - ستائر الجملة');

                      }else if(typ == 's'){
                        // titels = ' - ستائر المفرد';
                        titels = Translation(' - ستائر المفرد');

                      }else if(typ == 'w'){
                        titels = Translation(' - القروض المستلمة');
                      }
                    
                      headers =`

                                    <table  dir=rtl border=1 cellspacing=0 cellpadding=0
                                       style='background:white;border-collapse:collapse;border:none; font-weight:bold;'>
                                      
                                      
                                       
                                       <tr style='height:9.95pt'>
                                        <td  colspan=4 style='width:221.4pt; text-align : right !important;' class="table0_td">
                                       `+Translation('موبايل')+`  : <span dir="ltr" id="inv_phone"> `+COMPANY_PHONE+` </span>
                                        <br>
                                       `+Translation('البريد الالكتروني')+`  : <span dir="ltr" id="inv_email"> `+COMPANY_EMAIL+` </span>
                                        </td>

                                        <td   colspan=2 rowspan=2 style='width:180.4pt;' class="table0_td">
                                        <span id="inv_logo"> 
                                        <img src="`+COMPANY_LOGO+`" height="150px" align="left">
                                         </span>
                                        </td>

                                       </tr>
                                       
                                       <tr style='height:9.95pt'>
                                        <td colspan=6 width=118 style='width:88.55pt;' class="table0_td1">
                                            <span id="inv_address"> `+Translation(COMPANY_ADDRESS)+` </span>
                                          
                                        </td>
                                       </tr>
                                       
                                        <td width=590 colspan=6 class="table0_td1" style='text-align:center; width:6.15in;border:none;padding:0in 5.4pt 0in 5.4pt'>       
                                          <span style=" font-size: 20px;" id="inv_slogan">`+Translation('كشف حساب ')+titels+`</span>
                                        </td>
                                       
                                      </table>


                                  
                                          `;
                        
                      customer_details = `

                                            <table  dir=rtl border=1 cellspacing=0 cellpadding=0  width=595 style='width:6.2in;border-collapse:collapse;border:none'>
                                             <tr style='page-break-inside:avoid;' class="td_head">
                                              <td width=307 style='width:3.2in;' class="table1_td">
                                              اسم الزبون
                                              </td>
                                              <td width=144 style='width:1.5in;' class="table1_td">
                                              رقم الهاتف
                                              </td>
                                              <td width=144 style='width:1.5in;' class="table1_td last_td">
                                              العنوان
                                              </td>
                                             </tr>
                                             <tr style='page-break-inside:avoid;' class="td_body">
                                              <td width=307 style='width:3.2in;' class="table1_td1">
                                              <span  id="inv_customer_name"> `+response.invoice[0].customer_full_name+` </span>
                                              </td>
                                              <td width=144 style='width:1.5in;' class="table1_td1">
                                              <span  id="inv_customer_phone"> `+response.invoice[0].customer_phone+` </span>
                                              </td>
                                              <td width=144 style='width:1.5in;' class="table1_td1">
                                              <span  id="inv_customer_address"> `+response.invoice[0].customer_address+` </span>
                                              </td>
                                             </tr>
                                            </table>
                                            <br>
                                            `;

                      attach =  `
                                  <table id="inv_table"  dir=rtl border=1 cellspacing=0 cellpadding=0 width=591 style='width:443.5pt;border-collapse:collapse;border:#000000; '>
                                   <tr style='page-break-inside:avoid;' class="td_head">
                                    <td width=23 style='width:14.4pt;' class="table2_td">
                                    `+Translation('ت')+`
                                    </td>
                                    <td width=81.4 style='width:61.05pt;' class="table2_td">
                                      `+Translation('رقم الوصل')+`
                                    </td>
                                    <td width=81.4 style='width:61.05pt;' class="table2_td">
                                      `+Translation('تاريخ الوصل')+`
                                    </td>
                                    <td width=81.4 style='width:61.05pt;' class="table2_td">
                                      `+Translation('النوع')+`
                                    </td>
                                    <td width=81.4 style='width:61.05pt;' class="table2_td ">
                                    `+Translation('المبلغ الكلي')+`
                                    </td>
                                    <!--<td width=81.4 style='width:61.05pt;' class="table2_td">
                                    `+Translation('المبلغ المدفوع')+`
                                    </td>
                                    <td width=81.4 style='width:61.05pt;' class="table2_td">
                                   `+Translation('المبلغ الباقي')+` 
                                    </td> -->
                                    <td width=132 style='width:80.85pt;' class="table2_td last_td">
                                    `+Translation('ملاحظات')+`
                                    </td>
                                    
                                    
                                   </tr>`;
                     
                      // show sell item details   
                      var sum_total_cost                = 0;
                      var sum_total_paid                = 0;
                      var sum_total_remaind             = 0;
                      var sum_total_discount            = 0;
                      var sum_total_return              = 0;


                      var sum_total_cost_all            = 0;
                      var sum_total_paid_all            = 0;
                      var sum_total_remaind_all         = 0;
                      var sum_total_discount_all        = 0;
                      var sum_total_return_all          = 0;

                      var sum_total_withdraw_paid       = 0;
                      var sum_total_withdraw_remaind    = 0;


                      sum_total_paid                    = response.invoice[0].total_paid;
                      sum_total_discount                = response.invoice[0].total_discount;
                      sum_total_return                  = response.invoice[0].total_return;
                      
                      
                       
                      sum_total_cost_all                = response.account[0].total_invoice_price_all;
                      sum_total_paid_all                = response.account[0].total_Paid_all;
                      sum_total_remaind_all             = response.account[0].total_remained_all;
                      sum_total_discount_all            = response.account[0].total_payment_discount;
                      sum_total_return_all              = response.account[0].total_payment_return;

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

                        // compute totals
                       
                       //check types
                        var inv_typ = '';
                        if(response.invoice[i].invoice_type == 'w'){
                          inv_typ = Translation('قرض');
                          sum_total_withdraw_paid     = response.invoice[i].total_withdraw_paid;
                          sum_total_withdraw_remaind  = response.invoice[i].total_withdraw_remained;
                        }else if(response.invoice[i].invoice_type == '0'){
                          inv_typ = Translation('مواد جملة');

                        }else{
                          if(response.invoice[i].is_single == '0'){
                            inv_typ = Translation('ستائر جملة');
                          }else{
                            inv_typ = Translation('ستائر مفرد');

                          }
                          
                        }
                        

                        sum_total_cost      = +sum_total_cost + +response.invoice[i].invoice_total_price_actual;
                        

                        attach = attach + `
                                                  <tr style='page-break-inside:avoid;' class="td_body">
                                                    <td width=23 style='width:14.4pt;'>
                                                      <span  id="inv_count"> `+(i+1)+` </span>
                                                    </td>
                                                    <td width=81.4 style='width:61.05pt;'>
                                                      <span  id="inv_product"> `+response.invoice[i].invoice_number+` </span>
                                                    </td>
                                                    <td width=81.4 style='width:61.05pt;'>
                                                      <span  id="inv_quantity"> `+response.invoice[i].invoice_date+` </span>
                                                    </td>
                                                    <td width=81.4 style='width:61.05pt;'>
                                                      <span  id="inv_quantity"> `+inv_typ+` </span>
                                                    </td>
                                                    <td width=81.4 style='width:61.05pt;' class="last_td">
                                                      <span  id="inv_total_row"> `+currency_format_display(response.invoice[i].invoice_total_price_actual)+`  </span>
                                                    </td>
                                                    <!--<td width=81.4 style='width:61.05pt; font-family:  Times, serif;'>
                                                      <span  id="inv_details"> `+currency_format_display(response.invoice[i].total_paid)+` </span>
                                                    </td>
                                                    <td width=81.4  style='width:61.05pt;'>
                                                      <span  id="inv_total_length"> `+currency_format_display(response.invoice[i].total_remained)+`  </span>
                                                    </td>-->
                                                    <td width=152 style='width:80.85pt;'>
                                                      <span  id="inv_unit_price"> `+(response.invoice[i].invoice_note)+` </span>
                                                    </td>
                                                    
                                                    
                                                   </tr>
                                                  `;

                       
                       
                      } // for i

                      // add withdraw total_paid and Total Remained


                      sum_total_remaind   = sum_total_cost - sum_total_paid - sum_total_discount + sum_total_return;   

                      footers =`
                                 

                                 <tr style='page-break-inside:avoid;height:.1in'>
                                  <td width=517 colspan=5 valign=top style='width:388.0pt;border:none;
                                  border-left:solid #984806 1.0pt;background:white;padding:2.15pt 5.75pt 2.15pt 5.75pt;
                                  height:.1in'>
                                  `+Translation('المجموع الكلي')+`
                                  </td>
                                  <td width=74 style='width:55.5pt;border-top:none;border-left:solid #984806 1.0pt;
                                  border-bottom:solid #984806 1.0pt;border-right:none;
                                  padding:0pt .1in 0pt .1in;height:.1in'>
                                   `+currency_format_display(sum_total_cost)+`
                                  </td>
                                 </tr>
                                 <tr style='page-break-inside:avoid;height:.1in'>
                                  <td width=517 colspan=5 valign=top style='width:388.0pt;border:none;
                                  border-left:solid #984806 1.0pt;background:white;padding:2.15pt 5.75pt 2.15pt 5.75pt;
                                  height:.1in'>
                                  `+Translation('المبلغ المدفوع')+`
                                  </td>
                                  <td width=74 style='width:55.5pt;border-top:none;border-left:solid #984806 1.0pt;
                                  border-bottom:solid #984806 1.0pt;border-right:none;
                                  padding:0pt .1in 0pt .1in;height:.1in'>
                                  `+currency_format_display(sum_total_paid)+`
                                  </td>
                                 </tr>
                                 <tr style='page-break-inside:avoid;height:.1in'>
                                  <td width=517 colspan=5 valign=top style='width:388.0pt;border:none;
                                  border-left:solid #984806 1.0pt;background:white;padding:2.15pt 5.75pt 2.15pt 5.75pt;
                                  height:.1in'>
                                  `+Translation('المبلغ المتبقي')+`
                                  </td>
                                  <td width=74 style='width:55.5pt;border-top:none;border-left:solid #984806 1.0pt;
                                  border-bottom:solid #984806 1.0pt;border-right:none;
                                  padding:0pt .1in 0pt .1in;height:.1in'>
                                  `+currency_format_display(sum_total_remaind)+`
                                  </td>
                                 </tr>


                                 <tr style='page-break-inside:avoid;height:.1in; background: white;'>
                                  
                                  <td width=591 colspan=6 style='width:443.5pt;border:none;
                                  border-bottom:solid #984806 1.0pt; background: white;
                                  padding:0pt .1in 0pt .1in;height:.1in'>
                                   &nbsp;
                                  </td>
                                  
                                 </tr>

                                 <tr style='page-break-inside:avoid;height:.1in'>
                                  
                                  <td width=81.4 colspan=2 style='width:61.05pt;'>
                                    <span  > `+Translation('حسابه الكلي')+` </span>
                                  </td>

                                  <td width=81.4  style='width:61.05pt;'>
                                    <span  > `+Translation('الدفعات الكلية')+` </span>
                                  </td>

                                  <td width=81.4  style='width:61.05pt;'>
                                    <span  > `+Translation('الخصومات الكلية')+` </span>
                                  </td>

                                  <td width=81.4  style='width:61.05pt;'>
                                    <span  > `+Translation('المبالغ المسترجعة')+` </span>
                                  </td>

                                  <td width=81.4  style='width:61.05pt;'>
                                    <span  > `+Translation('المتبقي الكلي')+` </span>
                                  </td>

                                 </tr>

                                 <tr style='page-break-inside:avoid;height:.1in'>
                                  
                                  <td width=81.4 colspan=2 style='width:61.05pt;'>
                                    <span  > `+currency_format_display(sum_total_cost_all)+` </span>
                                  </td>

                                  <td width=81.4  style='width:61.05pt;'>
                                    <span  > `+currency_format_display(sum_total_paid_all)+` </span>
                                  </td>

                                  <td width=81.4  style='width:61.05pt;'>
                                    <span  > `+currency_format_display(sum_total_discount_all)+` </span>
                                  </td>

                                  <td width=81.4  style='width:61.05pt;'>
                                    <span  > `+currency_format_display(sum_total_return_all)+` </span>
                                  </td>

                                  <td width=81.4  style='width:61.05pt;'>
                                    <span  > `+currency_format_display(sum_total_remaind_all)+` </span>
                                  </td>

                                 </tr>



                                 
                                 <tr style='page-break-inside:avoid;height:.1in; background-color: white'>
                                  <td width=517 colspan=5  style='width:388.0pt;border:none;
                                  padding:2.15pt 5.75pt 2.15pt 5.75pt;height:.1in'>
                                  
                                  </td>
                                  <td width=74 style='width:55.5pt;border:none;padding:2.15pt .15in 2.15pt .15in;
                                  height:.1in'>
                                  `+Translation('التوقيع')+`
                                  </td>
                                 </tr>
                                </table>

                                </div>




                                </body>

                                `+print_attach+`

                                </html>`;

                  }else{
                    show_page = 0;
                    
                  }    

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

                      HTML_VIEW_PAY = HTML_Table + headers + customer_details + attach + attach_other + footers;
                       //  me.data('requestRunning', false);
                     if(show_page==1){
                        $("#invoice_show").html(HTML_VIEW_PAY);
                        $("#inv_numb").val(itm);

                        $('#show_payments_modal').modal('toggle');
                     }else{
                      noty({
                            width: 200,
                            text: Translation('لا توجد وصولات بيع لهذا النوع'),
                            type: 'error',
                            dismissQueue: true,
                            timeout: 2000,
                            layout: 'topRight',
                      });
                     }
                       
                      
                     
                        // window.open().document.write(HTML_VIEW);
                }

          }); // submit ajax


    }


    function show_withdraw_summary(itm,typ=null,opt=null,date1=null,date2=null){

          var headers           ='';
          var attach            = "";
          var attach_other      = "";
          var footers           ="";
          var customer_details  = "";
          var print_attach      = "";
          var total_pay         = "";
          var show_page         = 1;
          var titels            = '';

          

          $.ajax({
                url : 'models/crul_account_statement.php',
                type : 'POST',
                data : {type:'show_withdraw_summary', 
                        id : itm, 
                        date_operation : opt,
                        date1 : date1,
                        date2 : date2,
                        typ:typ},
                dataType : 'json',
                success : function(response) {
                    console.log(response)
                  if(response.invoice.length > 0){

                      if(typ == null){
                        titels = Translation(' - جميع الوصولات');
                      }else if(typ == 'raw'){
                        // titels = ' - المواد الخامة';
                        titels = Translation(' - المواد الخامة');

                      }else if(typ == 'm'){
                        // titels = ' - ستائر الجملة';
                        titels = Translation(' - ستائر الجملة');

                      }else if(typ == 's'){
                        // titels = ' - ستائر المفرد';
                        titels = Translation(' - ستائر المفرد');

                      }else if(typ == 'w'){
                        titels = Translation(' - القروض المستلمة');
                      }
                    
                      headers =`

                                    <table  dir=rtl border=1 cellspacing=0 cellpadding=0
                                       style='background:white;border-collapse:collapse;border:none; font-weight:bold;'>
                                      
                                      
                                       
                                       <tr style='height:9.95pt'>
                                        <td  colspan=4 style='width:221.4pt; text-align : right !important;' class="table0_td">
                                       `+Translation('موبايل')+`  : <span dir="ltr" id="inv_phone"> `+COMPANY_PHONE+` </span>
                                        <br>
                                       `+Translation('البريد الالكتروني')+`  : <span dir="ltr" id="inv_email"> `+COMPANY_EMAIL+` </span>
                                        </td>

                                        <td   colspan=2 rowspan=2 style='width:180.4pt;' class="table0_td">
                                        <span id="inv_logo"> 
                                        <img src="`+COMPANY_LOGO+`" height="150px" align="left">
                                         </span>
                                        </td>

                                       </tr>
                                       
                                       <tr style='height:9.95pt'>
                                        <td colspan=6 width=118 style='width:88.55pt;' class="table0_td1">
                                            <span id="inv_address"> `+Translation(COMPANY_ADDRESS)+` </span>
                                          
                                        </td>
                                       </tr>
                                       
                                        <td width=590 colspan=6 class="table0_td1" style='text-align:center; width:6.15in;border:none;padding:0in 5.4pt 0in 5.4pt'>       
                                          <span style=" font-size: 20px;" id="inv_slogan">`+Translation('كشف حساب ')+titels+`</span>
                                        </td>
                                       
                                      </table>



                                   
                                  
                                          `;
                        
                      customer_details = `

                                            <table  dir=rtl border=1 cellspacing=0 cellpadding=0  width=595 style='width:6.2in;border-collapse:collapse;border:none'>
                                             <tr style='page-break-inside:avoid;' class="td_head">
                                              <td width=307 style='width:3.2in;' class="table1_td">
                                              اسم الزبون
                                              </td>
                                              <td width=144 style='width:1.5in;' class="table1_td">
                                              رقم الهاتف
                                              </td>
                                              <td width=144 style='width:1.5in;' class="table1_td last_td">
                                              العنوان
                                              </td>
                                             </tr>
                                             <tr style='page-break-inside:avoid;' class="td_body">
                                              <td width=307 style='width:3.2in;' class="table1_td1">
                                              <span  id="inv_customer_name"> `+response.invoice[0].customer_full_name+` </span>
                                              </td>
                                              <td width=144 style='width:1.5in;' class="table1_td1">
                                              <span  id="inv_customer_phone"> `+response.invoice[0].customer_phone+` </span>
                                              </td>
                                              <td width=144 style='width:1.5in;' class="table1_td1">
                                              <span  id="inv_customer_address"> `+response.invoice[0].customer_address+` </span>
                                              </td>
                                             </tr>
                                            </table>
                                            <br>
                                            `;

                      attach =  `
                                  <table id="inv_table"  dir=rtl border=1 cellspacing=0 cellpadding=0 width=591 style='width:443.5pt;border-collapse:collapse;border:#000000; '>
                                   <tr style='page-break-inside:avoid;' class="td_head">
                                    <td width=23 style='width:14.4pt;' class="table2_td">
                                    `+Translation('ت')+`
                                    </td>
                                    <td width=81.4 style='width:61.05pt;' class="table2_td">
                                      `+Translation('رقم الوصل')+`
                                    </td>
                                    <td width=81.4 style='width:61.05pt;' class="table2_td">
                                      `+Translation('تاريخ الوصل')+`
                                    </td>
                                    <td width=81.4 style='width:61.05pt;' class="table2_td">
                                      `+Translation('النوع')+`
                                    </td>
                                    <td width=81.4 style='width:61.05pt;' class="table2_td ">
                                    `+Translation('المبلغ الكلي')+`
                                    </td>
                                    <!--<td width=81.4 style='width:61.05pt;' class="table2_td">
                                    `+Translation('المبلغ المدفوع')+`
                                    </td>
                                    <td width=81.4 style='width:61.05pt;' class="table2_td">
                                   `+Translation('المبلغ الباقي')+` 
                                    </td>-->
                                    <td width=152 style='width:113.85pt;' class="table2_td last_td">
                                    `+Translation('ملاحظات')+`
                                    </td>
                                    
                                    
                                   </tr>`;
                     
                      // show sell item details   
                      var sum_total_cost      = 0;
                      var sum_total_paid      = 0;
                      var sum_total_remaind   = 0;
                      var sum_total_discount  = 0;

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

                        // compute totals
                       
                        

                        sum_total_cost      = response.invoice[i].total_withdraw_price;
                        sum_total_paid      = response.invoice[i].total_paid;
                        sum_total_remaind   = response.invoice[i].total_remained;
                        

                        attach = attach + `
                                                  <tr style='page-break-inside:avoid;' class="td_body">
                                                    <td width=23 style='width:14.4pt;'>
                                                      <span  id="inv_count"> `+(i+1)+` </span>
                                                    </td>
                                                    <td width=81.4 style='width:61.05pt;'>
                                                      <span  id="inv_product"> `+response.invoice[i].capital_withdraw_id+` </span>
                                                    </td>
                                                    <td width=81.4 style='width:61.05pt;'>
                                                      <span  id="inv_quantity"> `+response.invoice[i].withdraw_date+` </span>
                                                    </td>
                                                    <td width=81.4 style='width:61.05pt;'>
                                                      <span  id="inv_quantity"> `+Translation('قرض')+` </span>
                                                    </td>
                                                    <td width=81.4 style='width:61.05pt;' class="last_td">
                                                      <span  id="inv_total_row"> `+currency_format_display(response.invoice[i].withdraw_amount)+`  </span>
                                                    </td>
                                                    <!--<td width=81.4 style='width:61.05pt; font-family:  Times, serif;'>
                                                      <span  id="inv_details"> `+currency_format_display(0)+` </span>
                                                    </td>
                                                    <td width=81.4  style='width:61.05pt;'>
                                                      <span  id="inv_total_length"> `+currency_format_display(0)+`  </span>
                                                    </td>-->
                                                    <td width=152 style='width:113.85pt;'>
                                                      <span  id="inv_unit_price"> `+(response.invoice[i].withdraw_note)+` </span>
                                                    </td>
                                                    
                                                    
                                                   </tr>
                                                  `;

                       
                       
                      } // for i

                      

                      footers =`
                                 
                                 <tr style='page-break-inside:avoid;height:.1in'>
                                  <td width=517 colspan=5 valign=top style='width:388.0pt;border:none;
                                  border-left:solid #984806 1.0pt;background:white;padding:2.15pt 5.75pt 2.15pt 5.75pt;
                                  height:.1in'>
                                  `+Translation('المبلغ الكلي')+`
                                  </td>
                                  <td width=74 style='width:55.5pt;border-top:none;border-left:solid #984806 1.0pt;
                                  border-bottom:solid #984806 1.0pt;border-right:none;
                                  padding:0pt .1in 0pt .1in;height:.1in'>
                                   `+currency_format_display(sum_total_cost)+`
                                  </td>
                                 </tr>
                                 <!--<tr style='page-break-inside:avoid;height:.1in'>
                                  <td width=517 colspan=7 valign=top style='width:388.0pt;border:none;
                                  border-left:solid #984806 1.0pt;background:white;padding:2.15pt 5.75pt 2.15pt 5.75pt;
                                  height:.1in'>
                                  `+Translation('المبلغ المدفوع')+`
                                  </td>
                                  <td width=74 style='width:55.5pt;border-top:none;border-left:solid #984806 1.0pt;
                                  border-bottom:solid #984806 1.0pt;border-right:none;
                                  padding:0pt .1in 0pt .1in;height:.1in'>
                                  `+currency_format_display(sum_total_paid)+`
                                  </td>
                                 </tr>
                                 <tr style='page-break-inside:avoid;height:.1in'>
                                  <td width=517 colspan=7 valign=top style='width:388.0pt;border:none;
                                  border-left:solid #984806 1.0pt;background:white;padding:2.15pt 5.75pt 2.15pt 5.75pt;
                                  height:.1in'>
                                  `+Translation('المبلغ المتبقي')+`
                                  </td>
                                  <td width=74 style='width:55.5pt;border-top:none;border-left:solid #984806 1.0pt;
                                  border-bottom:solid #984806 1.0pt;border-right:none;
                                  padding:0pt .1in 0pt .1in;height:.1in'>
                                  `+currency_format_display(sum_total_remaind)+`
                                  </td>
                                 </tr> -->
                                 <tr style='page-break-inside:avoid;height:.3in'>
                                 
                                  <td width=517 colspan=6  style='width:388.0pt;border:none;background:white;
                                    padding:2.15pt 5.75pt 2.15pt 5.75pt;height:.1in'>
                                    <span id="inv_note">`+Translation('ملاحظة')+` : `+''+` </span>
                                  </td>
                                 </tr>
                                 
                                 <tr style='page-break-inside:avoid;height:.1in; background-color: white'>
                                  <td width=517 colspan=5  style='width:388.0pt;border:none;
                                  padding:2.15pt 5.75pt 2.15pt 5.75pt;height:.1in'>
                                  
                                  </td>
                                  <td width=74 style='width:55.5pt;border:none;padding:2.15pt .15in 2.15pt .15in;
                                  height:.1in'>
                                  `+Translation('التوقيع')+`
                                  </td>
                                 </tr>
                                </table>

                                </div>




                                </body>

                                `+print_attach+`

                                </html>`;

                  }else{
                    show_page = 0;
                    
                  }    

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

                      HTML_VIEW_PAY = HTML_Table + headers + customer_details + attach + attach_other + footers;
                       //  me.data('requestRunning', false);
                     if(show_page==1){
                        $("#invoice_show").html(HTML_VIEW_PAY);
                        $("#inv_numb").val(itm);

                        $('#show_payments_modal').modal('toggle');
                     }else{
                      noty({
                            width: 200,
                            text: Translation('لا توجد وصولات بيع لهذا النوع'),
                            type: 'error',
                            dismissQueue: true,
                            timeout: 2000,
                            layout: 'topRight',
                      });
                     }
                       
                      
                     
                        // window.open().document.write(HTML_VIEW);
                }

          }); // submit ajax


    }
   
    function add_invoice_payment(cust_id){
      
      $("#add_payments_form")[0].reset();
      $("#customer_id_fk_pay").val(cust_id);

       $.ajax({
              url : 'models/crul_invoice.php',
              type : 'POST',
              data : {type:'show_payments_for_add',id:cust_id},
              dataType : 'json',
              success : function(response) {
                  
                  if(response != 0){
                    
                    
                     

                    $('span[id$="total_customer_account"]').html(currency_format_display(response.invoice[0].total_invoice_price));
                    $('span[id$="total_customer_account"]').attr('data-original-amount',(response.invoice[0].total_invoice_price));
                    
                    $('span[id$="total_customer_discount"]').html(currency_format_display(response.invoice[0].total_payment_discount));
                    $('span[id$="total_customer_discount"]').attr('data-original-amount',(response.invoice[0].total_payment_discount));
                    
                    $('span[id$="total_customer_paid"]').html(currency_format_display(response.invoice[0].total_Paid));
                    $('span[id$="total_customer_paid"]').attr('data-original-amount',(response.invoice[0].total_Paid));
                 
                    $('span[id$="total_customer_remaind"]').html(currency_format_display(response.invoice[0].total_remained));
                    $('span[id$="total_customer_remaind"]').attr('data-original-amount',(response.invoice[0].total_remained));
                 

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

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

        }); // submit ajax

      
      // $("#customer_all_invoices_price").val(currency_format_display(total_inv));
      // $("#customer_all_invoices_payment").val(currency_format_display(total_paid));
      // $("#customer_all_invoices_remaind").val(currency_format_display(total_remain));
      
      // $("#customer_all_invoices_price").attr('data-price-no-format',(total_inv));
      // $("#customer_all_invoices_payment").attr('data-price-no-format',(total_paid));
      // $("#customer_all_invoices_remaind").attr('data-price-no-format',(total_remain));


      $('#add_payments_modal').modal('toggle');

    }


     

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

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


    

    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 pdf_generator(HTML,INV_NO,payment){

                $.ajax({
                      url : 'models/crul_invoice.php',

                      type : 'POST',
                      data : {type:'PDF',html:HTML,inv_no:INV_NO,pay:payment},
                      dataType : 'json',
                      success : function(response) {
                            console.log(response)
                            if(response.is_success == true) {

                                    noty({
                                        width: 200,
                                        text: Translation('تم توليد ملف الـ PDF بنجاح'),
                                        type: 'success',
                                        dismissQueue: true,
                                        timeout: 3000,
                                        layout: 'top',
                                    });
                                    var LINKS = '<a class="btn btn-labeled btn-labeled-left heading-btn btn-info" href="'+response.data+'" target="_blank" style="float: right;">'+Translation('تحميل')+'  <b><i class="icon-file-download"></i></b> </a>';
                                    if(payment !=null){
                                      $("#link_pdf_pay").append(LINKS);
                                    }else{
                                      $("#link_pdf").append(LINKS);
                                    }

                                  
                                  $("#pdf").attr('disabled',false);
                                  $("#pdf_pay").attr('disabled',false);
                                  $("#print").attr('disabled',false);
                                  $("#print_pay").attr('disabled',false);
                            } else {

                               noty({
                                     width: 200,
                                     text: Translation(response.message),
                                     type: 'error',
                                     dismissQueue: true,
                                     timeout: 3000,
                                     layout: 'top',
                               });

                                  $("#pdf").attr('disabled',false);
                                  $("#pdf_pay").attr('disabled',false);
                                  $("#print").attr('disabled',false);
                                  $("#print_pay").attr('disabled',false);
                            }
                      },
                      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('اختيار الزبون')+'</option>';
                      var aaa='';
                        for(var i=response.length-1 ; i >= 0;i--){
                          var tp  = response[i][2];
                          var tp1 = response[i][3];
                          attach = attach + '<option data-subtext="Phone: '+tp+' || E-Mail: '+tp1+'" value="'+response[i][0]+'">'+response[i][1]+'</option>';
                          aaa = aaa + '<option  value="'+response[i][0]+'">'+response[i][1]+'</option>';

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

            }); // submit ajax

    }

    function get_Item_Type(){

            $.ajax({
                  url : 'models/crul_items.php',
                  type : 'POST',
                  data : {type:'get'},
                  dataType : 'json',
                  success : function(response) {
                    //console.log(response)
                      var attach ='<option value="" selected="selected">'+Translation('جميع الاصناف')+'</option>';
                      // attach = attach + '<option value="w" >'+Translation('القروض')+'</option>';
                      var aaa='';
                        for(var i=response.length-1 ; i >= 0;i--){
                          var tp  = response[i][2];
                          var tp1 = response[i][3];
                          attach = attach + '<option  value="'+response[i][0]+'">'+response[i][1]+'</option>';
                          
                        }
                        $("#item_type_id_fk").html(attach);
                        // $('#item_type_id_fk').selectpicker('refresh');
                        
                  },
                  complete: function() {
                         //  me.data('requestRunning', false);
                  }

            }); // submit ajax

    }

    
    function getQueryVariable(variable) {
        var query = window.location.search.substring(1);
        var vars = query.split("&");
        for (var i=0;i<vars.length;i++) {
          var pair = vars[i].split("=");
          if (pair[0] == variable) {
            return pair[1];
          }
        }
        return 'not';
    }

    function check_paid_amount(){
      var real_paid = parseFloat($("#total_customer_remaind").attr('data-original-amount')) ;

      var curnt_pay = parseFloat($("#payment_withdraw_amount").val());

      if(curnt_pay > real_paid){
        $("#payment_withdraw_amount").val('');
        noty({
                width: 200,
                text: Translation('المبلغ المدفوع اكبر من المبلغ الكي المتبقي .. الرجاء التحقق من المبلغ'),
                type: 'information',
                dismissQueue: true,
                timeout: 3000,
                layout: 'top',
            });
      }
    }


    


    function print_product(ct, printing=null){

          
          var headers           ='';
          var attach            = "";
          var attach_other      = "";
          var footers           ="";
          var customer_details  = "";
          var print_attach      = "";

        

          var today = new Date();
          var dd = today.getDate();
          var mm = today.getMonth()+1; //January is 0!
          var yyyy = today.getFullYear();

          if(dd<10) {
              dd = '0'+dd
          } 

          if(mm<10) {
              mm = '0'+mm
          } 

          today = yyyy + '/' + mm + '/' + dd;

          var cont = 0;

          var search_key = '';

          $('.dataTable tr').each(function () {
              //processing this row
              //how to process each cell(table td) where there is checkbox
              $(this).find('td input').each(function () {

                  cont++ ;

                  if (cont == 2) {
                    search_key = $(this).val();
                    // alert($(this).val())

                  }
                   // it is checked, your code here...
              });
          });


          $.ajax({
                url : 'models/crul_account_statement.php',
                type : 'POST',
                data : {type:'print', cust_type :ct, search :search_key },
                dataType : 'json',
                success : function(response) {
                    console.log(response)
                    
                        headers =`
                                  <table dir="rtl" class=MsoNormalTable border=0 cellspacing=0 cellpadding=0 width="92%" style='width:92.0%;border-collapse:collapse'>
                                      <tr style='height:.25in'>
                                          <td width="33%" nowrap colspan=3 style='width:33.44%;border:none;border-bottom:  solid windowtext 1.0pt;padding:0in 5.4pt 0in 5.4pt;height:.25in'>
                                              <p class=Slogan style='line-height:105%'><span style='font-family:"Century Gothic",sans-serif'>
                                              <img   width=86 height=77 src="`+COMPANY_LOGO+`"></span></p>
                                              <p class=Slogan style='line-height:105%'>
                                                <b><span style='font-size:9.0pt;  line-height:105%;font-family:"Century Gothic",sans-serif;color:#FFC000'>
                                                `+Translation('ANYA ZEEBRA')+` 
                                                </span>
                                              </b></p>
                                              
                                          </td>
                                          <td width="59%" colspan=14 style='width:59.32%;border:none;border-bottom:  solid windowtext 1.0pt;padding:0in 0in 0in 0in;height:.25in'>
                                              <p class=MsoNormal align=center style='margin-bottom:0in;margin-bottom:.0001pt;  text-align:center;line-height:normal'>
                                                <span style='font-size:36.0pt;  font-family:"Century Gothic",sans-serif;color:#A6A6A6'>        
                                                </span>
                                                <span style='font-size:28.0pt;font-family:"Century Gothic",sans-serif;color:#A8D08D'> </span>
                                                <span style='font-size:28.0pt;font-family:"Century Gothic",sans-serif;color:#FFC000'>
                                                `+Translation('ACCOUNT STATEMENTS LIST')+`
                                                </span></p>
                                          </td>
                                          <td width="7%" nowrap style='width:7.24%;border:none;border-bottom:solid windowtext 1.0pt;  padding:0in 5.4pt 0in 5.4pt;height:.25in'>
                                              <p class=MsoNormal dir=RTL style='margin-bottom:0in;margin-bottom:.0001pt;  text-align:right;direction:rtl;unicode-bidi:embed'>
                                                <span dir=LTR><img width=31 height=62 src="image/sys/image005.gif"></span></p>
                                          </td>
                                      </tr>
                                      <tr style='height:25.15pt' align="center">
                                          <td width="23%" nowrap colspan=3 style='width:23.78%;border:solid windowtext 1.0pt;   border-top:none;padding:0in 5.4pt 0in 5.4pt;height:25.15pt'>
                                              <p class=MsoNormal align=center style='margin-bottom:0in;margin-bottom:.0001pt;   text-align:center'>
                                                <b><span style='font-size:12.0pt;line-height:105%'>
                                                `+Translation('رقم القائمة')+`
                                                </span> 
                                              </b>
                                              </p>
                                          </td>
                                          
                                          <td width="11%" colspan=12 style='width:11.66%;border-top:none;border-left:   none;border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;   padding:0in 0in 0in 0in;height:25.15pt'>
                                              
                                          </td>
                                          
                                          <td width="21%" nowrap colspan=3 style='width:21.94%;border-top:none;  border-left:solid windowtext 1.0pt;border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;   padding:0in 5.4pt 0in 5.4pt;height:25.15pt'>
                                              <p class=MsoNormal align=center style='margin-bottom:0in;margin-bottom:.0001pt;   text-align:center;line-height:normal'>
                                                <b><span style='font-size:12.0pt'>
                                                `+Translation('التاريخ')+`
                                                </span> </b>
                                              </p>
                                          </td>
                                      </tr>
                                      <tr style='height:26.95pt' align="center">
                                          <td width="23%" nowrap colspan=3 style='width:23.78%;border:solid windowtext 1.0pt;   border-top:none;padding:0in 5.4pt 0in 5.4pt;height:26.95pt'>
                                           <input type="text" value="1" style="text-align:center; border: hidden;">
                                          </td>
                                          
                                          <td width="11%" colspan=12 style='width:11.66%;border-top:none;border-left:   solid windowtext 1.0pt;border-bottom:solid windowtext 1.0pt;border-right:  none;padding:0in 0in 0in 0in;height:26.95pt'>
                                              
                                          </td>
                                          
                                          <td width="21%" nowrap colspan=3 style='width:21.94%;border-top:none;   border-left:solid windowtext 1.0pt;border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;  padding:0in 5.4pt 0in 5.4pt;height:26.95pt'>
                                              `+today+`
                                          </td>
                                      </tr>
                                      </table>`;
                        
                        

                      customer_details = ``;

                      attach =  `
                                  <table dir="rtl" class=MsoNormalTable border=0 cellspacing=0 cellpadding=0 width="92%" style='width:92.0%;border-collapse:collapse'>
                                   <thead>
                                    <tr id="theadd" style='height:26.0pt'>
                                        <td width="7%" nowrap style='width:7.28%;border:solid windowtext 1.0pt; border-top:none;padding:0in 5.4pt 0in 5.4pt;height:26.0pt'>
                                            <p class=MsoNormal align=center style='margin-bottom:0in;margin-bottom:.0001pt;  text-align:center;line-height:normal'>
                                              <b><span style='color:black'>`+Translation('#')+`</span>
                                              </b>
                                            </p>
                                        </td>
                                        <td width="30%" nowrap colspan=5 style='width:30.0%;border-top:none;  border-left:none;border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;  padding:0in 5.4pt 0in 5.4pt;height:26.0pt'>
                                            <p class=MsoNormal align=center style='margin-bottom:0in;margin-bottom:.0001pt;  text-align:center;line-height:normal'>
                                              <b><span style='color:black'>`+Translation('تفاصيل الزبون')+`</span> </b></p>
                                        </td>
                                        <td width="7%" nowrap colspan=3 style='width:7.52%;border:solid windowtext 1.0pt;  border-left:none;padding:0in 5.4pt 0in 5.4pt;height:26.0pt'>
                                            <p class=MsoNormal align=center style='margin-bottom:0in;margin-bottom:.0001pt;  text-align:center;line-height:normal'>
                                              <b><span style='color:black'>`+Translation('حسابه الكلي')+`</span></b>
                                            </p>
                                        </td>
                                        <td width="9%" nowrap colspan=4 style='width:9.24%;border:solid windowtext 1.0pt;  border-left:none;padding:0in 5.4pt 0in 5.4pt;height:26.0pt'>
                                            <p class=MsoNormal align=center style='margin-bottom:0in;margin-bottom:.0001pt;  text-align:center;line-height:normal'>
                                              <b><span style='color:black'>`+Translation('الدفوعات الكلية')+`</span></b>
                                            </p>
                                        </td>
                                        <td width="6%" style='width:6.32%;border:solid windowtext 1.0pt;border-left: none;padding:0in 0in 0in 0in;height:26.0pt'>
                                            <p class=MsoNormal align=center style='margin-bottom:0in;margin-bottom:.0001pt;  text-align:center;line-height:normal'>
                                              <b>`+Translation('الخصومات الكلية')+`</b>
                                            </p>
                                        </td>
                                        <td width="10%" style='width:10.56%;border:solid windowtext 1.0pt;border-left:  none;padding:0in 5.4pt 0in 5.4pt;height:26.0pt'>
                                            <p class=MsoNormal align=center style='margin-bottom:0in;margin-bottom:.0001pt;  text-align:center;line-height:normal'>
                                              <b><span style='color:black'>`+Translation('المبالغ المسترجعة ')+`</span></b>
                                            </p>
                                        </td>
                                        <td width="12%" nowrap style='width:12.38%;border-top:none;border-left:none;   border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;  padding:0in 5.4pt 0in 5.4pt;height:26.0pt'>
                                            <p class=MsoNormal align=center style='margin-bottom:0in;margin-bottom:.0001pt;  text-align:center;line-height:normal'>
                                              <b><span style='color:black'>`+Translation('المتبقي الكلي')+`</span> </b>
                                            </p>
                                        </td>
                                        <td width="9%" nowrap colspan=2 style='width:9.56%;border:solid windowtext 1.0pt;  border-left:solid windowtext 1.0pt;padding:0in 5.4pt 0in 5.4pt;height:26.0pt'>
                                            <p class=MsoNormal align=center style='margin-bottom:0in;margin-bottom:.0001pt;  text-align:center;line-height:normal'>
                                              <b><span style='color:black'>`+Translation('الاعداد')+`</span></b>
                                            </p>
                                        </td>
                                    </tr>
                                  </thead>`;
                     
                      
                      var total_inv         = 0;
                      var total_pay         = 0;
                      var total_dis         = 0;
                      var total_ret         = 0;
                      var total_rem         = 0;
                      var total_inv_no      = 0;
                      var total_inv_no_row  = 0;
                      var total_inv_no_m    = 0;
                      var total_inv_no_s    = 0;
                      var total_inv_no_w    = 0;

                      // show sell item details                       
                      for(var i=0 ; i < response.invoice.length;i++){                        

                          total_inv         = +total_inv + +(response.invoice[i].total_invoice_price);
                          total_pay         = +total_pay + +(response.invoice[i].total_Paid);
                          total_dis         = +total_dis + +(response.invoice[i].total_payment_discount);
                          total_ret         = +total_ret + +(response.invoice[i].total_payment_return);
                          total_rem         = +total_rem + +(response.invoice[i].total_remained);
                          total_inv_no      = +total_inv_no + +(response.invoice[i].No_of_inv);
                          total_inv_no_row  = +total_inv_no_row + +(response.invoice[i].No_of_raw_inv);
                          total_inv_no_m    = +total_inv_no_m + +(response.invoice[i].No_of_m_inv);
                          total_inv_no_s    = +total_inv_no_s + +(response.invoice[i].No_of_s_inv);
                          total_inv_no_w    = +total_inv_no_w + +(response.invoice[i].No_of_withdraw);

                          attach = attach + ` 

                                              <tr style='height:15.0pt' align="center">
                                                  <td width="7%" nowrap style='width:7.28%;border:solid windowtext 1.0pt;  border-top:none;padding:0in 5.4pt 0in 5.4pt;height:15.0pt'>
                                                      <p class=MsoNormal style='margin-bottom:0in;margin-bottom:.0001pt;line-height:  normal'>
                                                        `+response.invoice[i].customer_id+`
                                                      </p>
                                                  </td>
                                                  <td width="30%" dir="rtl" align="right" nowrap colspan=5 style='width:30.0%;border:none;border-bottom:  solid windowtext 1.0pt;padding:0in 5.4pt 0in 5.4pt;height:15.0pt'>
                                                      <p class=MsoNormal style='font-weight:bold; margin-bottom:0in;margin-bottom:.0001pt;line-height:  normal'>
                                                        <span style='font-size:10.0pt;font-family:"Times New Roman",serif'>
                                                       `+Translation('اسم الزبون')+` : `+response.invoice[i].customer_full_name+`
                                                        </span>
                                                      </p>
                                                      <p class=MsoNormal style='font-weight:bold; margin-bottom:0in;margin-bottom:.0001pt;line-height:  normal'>
                                                        <span style='font-size:10.0pt; color:#076520 ;font-family:"Times New Roman",serif'> 
                                                       `+Translation('رقم الهاتف 1')+` : `+response.invoice[i].customer_phone+`
                                                        </span>
                                                      </p>
                                                      <p class=MsoNormal style='font-weight:bold; margin-bottom:0in;margin-bottom:.0001pt;line-height:  normal'>
                                                        <span style='font-size:10.0pt; color:#984444 ;font-family:"Times New Roman",serif'> 
                                                       `+Translation('رقم الهاتف 2')+` : `+response.invoice[i].customer_phone2+`
                                                        </span>
                                                      </p>
                                                      <p class=MsoNormal style='margin-bottom:0in;margin-bottom:.0001pt;line-height:  normal'>
                                                        <span style='font-size:10.0pt;font-family:"Times New Roman",serif'>
                                                        `+Translation('العنوان')+` : `+response.invoice[i].customer_address+`
                                                        </span>
                                                      </p>
                                                      
                                                  </td>
                                                  <td width="7%" colspan=3 style='width:7.52%;border-top:none;border-left:solid windowtext 1.0pt; border-bottom:solid windowtext 1.0pt;border-right:solid black 1.0pt;  padding:0in 5.4pt 0in 5.4pt;height:15.0pt'>
                                                      <p class=MsoNormal align=center style='margin-bottom:0in;margin-bottom:.0001pt;  text-align:center;line-height:normal'>
                                                        <b>
                                                        <span style='color:black'>
                                                        `+currency_format_display(response.invoice[i].total_invoice_price)+`
                                                        </span></b>
                                                      </p>
                                                  </td>
                                                  <td width="9%" nowrap colspan=4 style='width:9.24%;border-top:none; border-left:none;border-bottom:solid windowtext 1.0pt;border-right:solid gray 1.0pt;  padding:0in 5.4pt 0in 5.4pt;height:15.0pt'>
                                                      
                                                        `+currency_format_display(response.invoice[i].total_Paid)+`
                                                      
                                                  </td>
                                                  <td width="6%" style='width:6.32%;border-top:none;border-left:none;  border-bottom:solid windowtext 1.0pt;border-right:solid gray 1.0pt;  padding:0in 0in 0in 0in;height:15.0pt'>
                                                      `+currency_format_display(response.invoice[i].total_payment_discount)+`
                                                  </td>
                                                  <td width="10%" style='width:10.56%;border-top:none;border-left:none;  border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;  padding:0in 5.4pt 0in 5.4pt;height:15.0pt'>
                                                      `+currency_format_display(response.invoice[i].total_payment_return)+`
                                                  </td>
                                                  <td width="12%" nowrap style='width:12.38%;border-top:none;border-left:none;  border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;  padding:0in 5.4pt 0in 5.4pt;height:15.0pt'>
                                                      `+currency_format_display(response.invoice[i].total_remained)+`
                                                  </td>
                                                  <td width="9%" nowrap dir="rtl" align="right" colspan=2 style='width:9.56%;border-top:none; border-left:solid windowtext 1.0pt;border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;  padding:0in 5.4pt 0in 5.4pt;height:15.0pt'>
                                                      <p class=MsoNormal style='font-weight:bold; margin-bottom:0in;margin-bottom:.0001pt;line-height:  normal'>
                                                        <span style='font-size:10.0pt;font-family:"Times New Roman",serif'>
                                                        `+Translation('عدد الوصولات الكلية')+` :`+response.invoice[i].No_of_inv+`
                                                        </span>
                                                      </p>
                                                      <p class=MsoNormal style='font-weight:bold; margin-bottom:0in;margin-bottom:.0001pt;line-height:  normal'>
                                                        <span style='font-size:10.0pt; color:#076520 ;font-family:"Times New Roman",serif'> 
                                                       `+Translation('عدد وصولات مواد الجملة')+` : `+response.invoice[i].No_of_raw_inv+`
                                                        </span>
                                                      </p>

                                                      <p class=MsoNormal style='font-weight:bold; margin-bottom:0in;margin-bottom:.0001pt;line-height:  normal'>
                                                        <span style='font-size:10.0pt; color:#076520 ;font-family:"Times New Roman",serif'>
                                                        `+Translation('عدد وصولات ستائر الجملة')+` :`+response.invoice[i].No_of_m_inv+`
                                                        </span>
                                                      </p>
                                                      <p class=MsoNormal style='font-weight:bold; margin-bottom:0in;margin-bottom:.0001pt;line-height:  normal'>
                                                        <span style='font-size:10.0pt; color:#984444 ;font-family:"Times New Roman",serif'>
                                                        `+Translation('عدد وصولات ستائر المفرد')+` :`+response.invoice[i].No_of_s_inv+`
                                                        </span>
                                                      </p>
                                                      <p class=MsoNormal style='margin-bottom:0in;margin-bottom:.0001pt;line-height:  normal'>
                                                        <span style='font-size:10.0pt;font-family:"Times New Roman",serif'>
                                                        `+Translation('عدد القروض')+` : `+response.invoice[i].No_of_withdraw+`
                                                        </span>
                                                      </p>
                                                  </td>
                                              </tr>`;
                        
                        
                       
                      } // for i

                      if(printing != null){
                        print_attach = `
                                        <script>
                                          window.print();
                                          window.onload = function() { window.print(); }
                                        </script>
                                        `;
                      }else{
                        print_attach = "";
                      }

                       attach = attach + ` 

                                              <tr style='height:15.0pt; background-color: beige; border: 1px solid;' align="center">
                                                  
                                                  <td width="30%" dir="rtl" align="right" nowrap colspan=6 style='width:30.0%;border:none;border-bottom:  solid windowtext 1.0pt;padding:0in 5.4pt 0in 5.4pt;height:15.0pt'>
                                                      <p class=MsoNormal style='font-weight:bold; margin-bottom:0in;margin-bottom:.0001pt;line-height:  normal'>
                                                        <span style='font-size:10.0pt;font-family:"Times New Roman",serif'>
                                                       `+Translation('المجموع')+` 
                                                        </span>
                                                      </p>
                                                      
                                                      
                                                  </td>
                                                  <td width="7%" colspan=3 style='width:7.52%;border-top:none;border-left:solid windowtext 1.0pt; border-bottom:solid windowtext 1.0pt;border-right:solid black 1.0pt;  padding:0in 5.4pt 0in 5.4pt;height:15.0pt'>
                                                      <p class=MsoNormal align=center style='margin-bottom:0in;margin-bottom:.0001pt;  text-align:center;line-height:normal'>
                                                        <b>
                                                        <span style='color:black'>
                                                        `+currency_format_display(total_inv)+`
                                                        </span></b>
                                                      </p>
                                                  </td>
                                                  <td width="9%" nowrap colspan=4 style='width:9.24%;border-top:none; border-left:none;border-bottom:solid windowtext 1.0pt;border-right:solid gray 1.0pt;  padding:0in 5.4pt 0in 5.4pt;height:15.0pt'>
                                                      
                                                        `+currency_format_display(total_pay)+`
                                                      
                                                  </td>
                                                  <td width="6%" style='width:6.32%;border-top:none;border-left:none;  border-bottom:solid windowtext 1.0pt;border-right:solid gray 1.0pt;  padding:0in 0in 0in 0in;height:15.0pt'>
                                                      `+currency_format_display(total_dis)+`
                                                  </td>
                                                  <td width="10%" style='width:10.56%;border-top:none;border-left:none;  border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;  padding:0in 5.4pt 0in 5.4pt;height:15.0pt'>
                                                      `+currency_format_display(total_ret)+`
                                                  </td>
                                                  <td width="12%" nowrap style='width:12.38%;border-top:none;border-left:none;  border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;  padding:0in 5.4pt 0in 5.4pt;height:15.0pt'>
                                                      `+currency_format_display(total_rem)+`
                                                  </td>
                                                  <td width="9%" nowrap dir="rtl" align="right" colspan=2 style='width:9.56%;border-top:none; border-left:solid windowtext 1.0pt;border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;  padding:0in 5.4pt 0in 5.4pt;height:15.0pt'>
                                                      <p class=MsoNormal style='font-weight:bold; margin-bottom:0in;margin-bottom:.0001pt;line-height:  normal'>
                                                        <span style='font-size:10.0pt;font-family:"Times New Roman",serif'>
                                                        `+Translation('عدد الوصولات الكلية')+` :`+total_inv_no+`
                                                        </span>
                                                      </p>
                                                      <p class=MsoNormal style='font-weight:bold; margin-bottom:0in;margin-bottom:.0001pt;line-height:  normal'>
                                                        <span style='font-size:10.0pt; color:#076520 ;font-family:"Times New Roman",serif'> 
                                                       `+Translation('عدد وصولات مواد الجملة')+` : `+total_inv_no_row+`
                                                        </span>
                                                      </p>

                                                      <p class=MsoNormal style='font-weight:bold; margin-bottom:0in;margin-bottom:.0001pt;line-height:  normal'>
                                                        <span style='font-size:10.0pt; color:#076520 ;font-family:"Times New Roman",serif'>
                                                        `+Translation('عدد وصولات ستائر الجملة')+` :`+total_inv_no_m+`
                                                        </span>
                                                      </p>
                                                      <p class=MsoNormal style='font-weight:bold; margin-bottom:0in;margin-bottom:.0001pt;line-height:  normal'>
                                                        <span style='font-size:10.0pt; color:#984444 ;font-family:"Times New Roman",serif'>
                                                        `+Translation('عدد وصولات ستائر المفرد')+` :`+total_inv_no_s+`
                                                        </span>
                                                      </p>
                                                      <p class=MsoNormal style='margin-bottom:0in;margin-bottom:.0001pt;line-height:  normal'>
                                                        <span style='font-size:10.0pt;font-family:"Times New Roman",serif'>
                                                        `+Translation('عدد القروض')+` : `+total_inv_no_w+`
                                                        </span>
                                                      </p>
                                                  </td>
                                              </tr>`;
                      footers =`

                                <tr style='height:6.0pt'>
                                    <td width="100%" colspan=18 valign=top style='width:100.0%;padding:0in 5.4pt 0in 5.4pt;  height:6.0pt'>
                                        <p class=ThankYou style='line-height:105%'>
                                        `+Translation('Thank you for your business!')+`
                                        </p>
                                    </td>
                                </tr>
                                <tr style='height:6.0pt'>
                                    <td width="100%" colspan=18 valign=top style='width:100.0%;padding:0in 5.4pt 0in 5.4pt;  height:6.0pt'>
                                        <p class=ContactInfo style='margin-top:12.0pt;line-height:105%'>
                                        <span lang=AR-KW dir=RTL style='font-size:10.0pt;line-height:105%;font-family:"Sakkal Majalla";  color:#7F7F7F'>
                                        `+ COMPANY_ADDRESS+`
                                        </span><span dir=LTR></span><span lang=AR-KW><span dir=LTR></span> </span>
                                        </p>
                                    </td>
                                </tr>
                                <tr style='height:.25in'>
                                    <td width="42%" nowrap dir=LTR colspan=5 style='width:42.02%;padding:0in 5.4pt 0in 5.4pt;  height:.25in'>
                                        <p class=MsoNormal dir=LTR align=center style='margin-bottom:0in;margin-bottom:.0001pt;  text-align:center'>
                                        `+COMPANY_PHONE+`
                                        </p>
                                    </td>
                                    <td width="16%" nowrap colspan=7 style='width:16.1%;padding:0in 5.4pt 0in 5.4pt;  height:.25in'>
                                        <p class=MsoNormal align=center style='margin-bottom:0in;margin-bottom:.0001pt;  text-align:center'>
                                        <span style='color:black'>`+Translation('www.anya.co')+`</span> </p>
                                    </td>
                                    <td width="41%" nowrap colspan=6 style='width:41.86%;padding:0in 5.4pt 0in 5.4pt;  height:.25in'>
                                        <p class=MsoNormal align=center style='margin-bottom:0in;margin-bottom:.0001pt;  text-align:center'>
                                        <span style='color:black'>`+COMPANY_EMAIL+`</span></p>
                                    </td>
                                </tr>
                                
                                
                                <tr height=0>
                                    <td width=76 style='border:none'></td>
                                    <td width=105 style='border:none'></td>
                                    <td width=69 style='border:none'></td>
                                    <td width=23 style='border:none'></td>
                                    <td width=38 style='border:none'></td>
                                    <td width=17 style='border:none'></td>
                                    <td width=5 style='border:none'></td>
                                    <td width=6 style='border:none'></td>
                                    <td width=41 style='border:none'></td>
                                    <td width=27 style='border:none'></td>
                                    <td width=13 style='border:none'></td>
                                    <td width=7 style='border:none'></td>
                                    <td width=23 style='border:none'></td>
                                    <td width=25 style='border:none'></td>
                                    <td width=91 style='border:none'></td>
                                    <td width=128 style='border:none'></td>
                                    <td width=12 style='border:none'></td>
                                    <td width=45 style='border:none'></td>
                                </tr>
                            </table>

                        </div>

                    </div>

                </body>



                  
                     `+print_attach+`
                    </html>`;

                      

                     
                },
                complete: function() {
                       //  me.data('requestRunning', false);
                       HTML_VIEW = table_invoice + headers + customer_details + attach + attach_other + footers;
                       //  me.data('requestRunning', false);
                        window.open().document.write(HTML_VIEW);
                }

          }); // submit ajax


    }




$(document).ready(function() {
  
    
    get_Customer();
    get_Item_Type();

     // handling two level of modal in same page 

    $('.modal').on('hidden.bs.modal', function (e) {
        if($('.modal').hasClass('in')) {
        $('body').addClass('modal-open');
        }    
    });

     $('#account_date_form').submit(function(e) {
        e.preventDefault();

        var func_name   = $("#func_name").val();
        var itm         = $("#customer_id_fk").val();
        var func_param  = $("#func_param").val();

        var date_1      = $("#ac_date_1").val();
        var date_2      = $("#ac_date_2").val();
        var operate     = $("#operation_type").val();

        switch (func_name){

          case 'show_transaction':
            show_transaction(itm,operate, date_1, date_2);
            break;

          case 'show_summary':
            show_summary(itm,func_param,operate, date_1, date_2);
            break;

          case 'show_withdraw_summary':
            show_withdraw_summary(itm, func_param,operate, date_1, date_2);
            break;



        }

        $('#account_date_modal').modal('toggle');

         
    });

    
    // $('#add_payments_form').submit(function(event) {
    //         event.preventDefault();
            

    //         var customer_id_fk    = $("#customer_id_fk").val();
    //         var paid_amount       = $("#paid_amount").val();
    //         var paid_date         = $("#paid_date").val();
    //         var payment_note      = $("#payment_note").val();
    //         var item_type_id_fk   = $("#item_type_id_fk").val();
            
    //         var Check1 = true;
    //         var Check2 = true;

    //         if(paid_amount =='' || isNaN(paid_amount)) {
    //            $("#paid_amount").closest('.form-group').removeClass('has-success');
    //            $("#paid_amount").closest('.form-group').addClass('has-error');
    //            Check1 = false;
    //            noty({
    //                     width: 200,
    //                     text: Translation("الرجاء قم بادخال مبلغ الدفعه"),
    //                     type: 'error',
    //                     dismissQueue: true,
    //                     timeout: 3000,
    //                     layout: 'top',
    //               });
    //         }else{
    //            $("#paid_amount").closest('.form-group').removeClass('has-error');
    //            $("#paid_amount").closest('.form-group').addClass('has-success');
    //         }

    //         if(customer_id_fk =='' || isNaN(customer_id_fk)) {
    //            $("#customer_id_fk").closest('.form-group').removeClass('has-success');
    //            $("#customer_id_fk").closest('.form-group').addClass('has-error');
    //            Check2 = false;
    //            noty({
    //                     width: 200,
    //                     text: Translation("حصل خطأ الرجاء اعادة تحميل الصفحة والمحاولة من جديد"),
    //                     type: 'error',
    //                     dismissQueue: true,
    //                     timeout: 3000,
    //                     layout: 'top',
    //               });
    //         }else{
    //            $("#customer_id_fk").closest('.form-group').removeClass('has-error');
    //            $("#customer_id_fk").closest('.form-group').addClass('has-success');
    //         }
            

           

    //         if(Check1 && Check2 ){
    //               $("#submit_add_pay_btn").attr('disabled',true);

    //               $.ajax({
    //                     url: "models/crul_account_statement.php",
    //                     type:"POST",
    //                     dataType : "JSON",
    //                     data: {"type":"add_invoice_payment",
    //                            customer_id_fk:customer_id_fk,
    //                            paid_amount:paid_amount,
    //                            paid_date:paid_date,
    //                            item_type_id_fk:item_type_id_fk,
    //                            payment_note:payment_note} ,
    //                     complete:function(){
    //                      $("#submit_add_pay_btn").attr('disabled',false);
    //                     },
    //                     beforeSend:function(){
    //                       $("#submit_add_pay_btn").attr('disabled',true);
                         
    //                     },
    //                     success: function(res) {

    //                       if(res.is_success == true){
    //                         $('#add_payments_modal').modal('toggle');
    //                         $("#add_payments_form")[0].reset();
    //                         var amm = '';
    //                         if(res.amount !=undefined){
    //                            amm = res.amount;
    //                         }
                            
    //                         swal({
    //                               title: Translation('اضافة'),
    //                               text: Translation(res.data) + amm,
    //                               confirmButtonColor: "#66BB6A",
    //                               type: "success"
    //                           });

    //                         $(".datatable-column-search-inputs").DataTable().ajax.reload(null, false);
    //                       }else{
    //                         swal({
    //                               title: Translation('خطأ'),
    //                               text: Translation(res.data) + amm,
    //                               confirmButtonColor: "#66BB6A",
    //                               type: "error"
    //                           });
    //                       }

    //                       $("#submit_add_pay_btn").attr('disabled',false);
    //                     }
    //                 });
            
               
    //         }
    // });


    $('#add_payments_form').submit(function(event) {
      event.preventDefault();
     
          

          var cust_id                   = $("#customer_id_fk_pay").val();
          var payment_amount            = $("#payment_withdraw_amount").val();
          var paid_date                 = $("#paid_date").val();
          var payment_note              = $("#payment_note").val();
          var pay_type                  = $("#pay_type").val();
          var item_type_id_fk           = $("#item_type_id_fk").val();
         

          var check_paid  = false;
          var check_ids   = true;

          check_paid      = Checks('payment_withdraw_amount','No',0.000001);

        

          if(check_paid && check_ids){
            $("#submit_add_btn").attr('disabled',true);
            $.ajax({
                url: "models/crul_invoice.php",
                type:"POST",
                dataType: "JSON",
                data : {cust_id              :cust_id,
                        payment_amount       :payment_amount,
                        paid_date            :paid_date,
                        payment_note         :payment_note,
                        pay_type             :pay_type,
                        item_type_id_fk      :item_type_id_fk,
                        type                 :'add_payments'}
            })
            .done(function(res) {
               
                  if(res.is_success == true){

                    $("#add_payments_form")[0].reset();
                    $("#payment_gived_to_id_fk").val('').selectpicker('refresh');
                    
                    // get_Customer();

                    $("#total_customer_account").html(currency_format_display(0));
                    $("#total_customer_account").attr('data-original-amount',(0));
                            
                    $("#total_customer_discount").html(currency_format_display(0));
                    $("#total_customer_discount").attr('data-original-amount',(0));
                            
                    $("#total_customer_paid").html(currency_format_display(0));
                    $("#total_customer_paid").attr('data-original-amount',(0));
                         
                    $("#total_customer_remaind").html(currency_format_display(0));
                    $("#total_customer_remaind").attr('data-original-amount',(0));
                         
                    $('#add_payments_modal').modal('toggle');

                   

                    $(".datatable-column-search-inputs").DataTable().ajax.reload(null, false);
                    
                    
                    noty({
                                    width: 200,
                                    text: Translation(res.data),
                                    type: 'success',
                                    dismissQueue: true,
                                    timeout: 2000,
                                    layout: 'topRight',
                                    callback: {
                                              onShow: function() {},
                                              afterShow: function() {},
                                              onClose: function() {},
                                              afterClose: function() {},
                                              onCloseClick: function() {},
                                            },
                              });

                    // i will reload the page to reset all fields
                    
               
                  }else{
                      swal({
                          title: Translation("خطأ"),
                          text: Translation(res.data),
                          confirmButtonColor: "#66BB6A",
                          type: "warning"
                      });
                      //$("#add-form-cont").collapse('hide');
                  }

                $("#submit_add_btn").attr('disabled',false);
            })
            .fail(function(err) {
                 swal({
                    title: Translation("خطأ"),
                    text: (err),
                    confirmButtonColor: "#66BB6A",
                    type: "error"
                });
                $("#submit_add_btn").attr('disabled',false);
            })
            .always(function() {
                console.log("complete");
            });
          }else{
              noty({
                    width: 200,
                    text: Translation('الرجاء التأكد من القيم المدخلة وايضا يجب ادخال دفعة جديدة على الاقل'),
                    type: 'error',
                    dismissQueue: true,
                    timeout: 3000,
                    layout: 'topRight',
              });
          }

    });
    

    $("#payment_withdraw_amount").on('change',function(){
      check_paid_amount();
    });

    $("#payment_withdraw_amount").on('keyup',function(){
      check_paid_amount();
    });

    $("#payment_withdraw_amount").on('click',function(){
      check_paid_amount();
    });

    $("#item_type_id_fk").on('change',function(){
     
      var item_type_id_fk   = $("#item_type_id_fk").val();

      var customer_id_fk    = $("#customer_id_fk").val();
      
      var typo = 'get_totals_item_type';             
      if(item_type_id_fk == 'w'){
        typo = 'get_totals_withdraw';
      }

      $.ajax({
            url: "models/crul_account_statement.php",
            type:"POST",
            dataType : "JSON",
            data: {"type":typo,
                   customer_id_fk:customer_id_fk,
                   item_type_id_fk:item_type_id_fk} ,
            complete:function(){
             $("#submit_add_pay_btn").attr('disabled',false);
            },
            beforeSend:function(){
              $("#submit_add_pay_btn").attr('disabled',true);
             
            },
            success: function(res) {

              $("#customer_all_invoices_price").val(currency_format_display(res[0].total_item_type_price));
              $("#customer_all_invoices_payment").val(currency_format_display(res[0].total_item_type_paid));
              $("#customer_all_invoices_remaind").val(currency_format_display(res[0].total_item_type_remaind));
              
              $("#customer_all_invoices_price").attr('data-price-no-format',(res[0].total_item_type_price));
              $("#customer_all_invoices_payment").attr('data-price-no-format',(res[0].total_item_type_paid));
              $("#customer_all_invoices_remaind").attr('data-price-no-format',(res[0].total_item_type_remaind));

              $("#submit_add_pay_btn").attr('disabled',false);
            }
        });
            
               
            

    });

});







$(function() {
//var singleSelect = $('.datatable-column-search-inputs').DataTable();
    
  $(".touchspin-basic").TouchSpin({
        min: 1,
        max: 10000000,
        step: 1,
        buttondown_class: "btn btn-xs",
        buttonup_class: "btn btn-xs"
    });

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

  // Mini
    $('.select-size-xs').select2({
        containerCssClass: 'select-xs'
    });


   // Setting datatable defaults
    $.extend( $.fn.dataTable.defaults, {
        autoWidth: false,
        columnDefs: [{ 
            orderable: false,
            width: '100px',
            targets: [ 5 ]
        }],
        dom: '<"datatable-header"fBl><"datatable-scroll"t><"datatable-footer"ip>',
        language: {
            search: '<span>'+Translation('بحث')+':</span> _INPUT_',
            searchPlaceholder: Translation('ابحث عن ')+'...',
            lengthMenu: '<span>'+Translation('عرض')+':</span> _MENU_',
            paginate: { 'first': 'First', 'last': 'Last', 'next': '&larr;', 'previous': '&rarr;' }
        },
        drawCallback: function () {
            $(this).find('tbody tr').slice(-3).find('.dropdown, .btn-group').addClass('dropup');
        },
        preDrawCallback: function() {
            $(this).find('tbody tr').slice(-3).find('.dropdown, .btn-group').removeClass('dropup');
        }
    });


    

    // Individual column searching with text inputs
    $('.datatable-column-search-inputs tfoot td').not(':last-child').each(function () {
        var title = $('.datatable-column-search-inputs thead th').eq($(this).index()).text();
        $(this).html('<input type="text" class="form-control input-sm" placeholder="'+Translation('بحث')+' '+title+'" />');
    });
    
    

   var query_ip   = getQueryVariable("ip");
   var query_typ  = getQueryVariable("typ");




   var table = $('.datatable-column-search-inputs').DataTable({
        
        buttons: {            
            buttons: [
                {
                    extend: 'copyHtml5',
                    className: 'btn btn-default',
                    exportOptions: {
                        columns: [ 0, ':visible' ]
                    }
                },
                {
                    extend: 'excelHtml5',
                    className: 'btn btn-default',
                    exportOptions: {
                        columns: ':visible'
                    }
                },
                {
                    extend: 'pdfHtml5',
                    className: 'btn btn-default',
                    exportOptions: {
                        columns: ':visible'
                    }
                },
                {
                    extend: 'print',
                    className: 'btn btn-default',
                    exportOptions: {
                        columns: ':visible'
                    }
                },
                {
                    extend: 'colvis',
                    text: '<i class="icon-three-bars"></i> <span class="caret"></span>',
                    className: 'btn bg-blue btn-icon'
                }
                
            ]
        },
        lengthMenu: [
            [  10, 25, 50, 100 ],
            [  '10', '25', '50', '100' ]
        ],
        "processing": true,
        "serverSide": true,
        "ajax": {
        "url" : "models/crul_account_statement.php",
        "data": function(d){
          d.type = "load";
          if(query_ip !='not'){
            d.ip =query_ip ;
          }
          if(query_typ !='not'){
            d.typ =query_typ ;
          }
          
        }
        },
        drawCallback: function() {
                  $('[data-popup=tooltip]').tooltip();
                  $('[data-popup=popover-custom]').popover({
                    template: '<div class="popover  border-teal-400"><div class="arrow"></div><h3 class="popover-title bg-teal-400"></h3><div class="popover-content"></div></div>'
                  });

            },
        "columnDefs": [
          {
              "targets": 7,
              "data": null,
              "render": function (data,type,row) {

                            return  `<ul class='icons-list '>
                                      

                                        <li class="dropdown">
                                          <a  class="dropdown-toggle" data-toggle="dropdown"  data-original-title='`+Translation('تفاصيل دفوعات الوصل')+` '>
                                            <i class="icon-menu9"></i>
                                          </a>

                                          <ul class="dropdown-menu dropdown-menu-right">

                                            <li class='text-primary-600'>
                                                <a  class='text-danger-400' onclick="account_statement_date(`+row[0]+`,'show_transaction')"  data-original-title='`+Translation('عرض خلاصة حسابات الزبون')+`'>
                                                    <i class='icon-calendar2'></i> `+Translation('عرض جميع عمليات الحساب للزبون')+`
                                                </a>
                                            </li>

                                            <li class='text-primary-600'>
                                                <a  class='text-info-400' onclick="account_statement_date(`+row[0]+`,'show_summary');"  data-original-title='`+Translation('عرض خلاصة حسابات الزبون')+`'>
                                                    <i class='icon-file-spreadsheet2'></i> `+Translation('عرض خلاصة حسابات الزبون')+`
                                                </a>
                                            </li>
                                            
                                            <li class='text-primary-600'>
                                                <a href='#' class='text-teal-400' onclick="account_statement_date(`+row[0]+`,'show_summary','raw');"   data-original-title='`+Translation('عرض خلاصة حسابات الزبون للمواد الخام')+`'>
                                                    <i class='icon-file-spreadsheet2'></i>`+Translation('عرض خلاصة حسابات الزبون للمواد الخام')+`
                                                </a>
                                            </li>

                                            <li class='text-primary-600'>
                                                <a  class='text-orange-400' onclick="account_statement_date(`+row[0]+`,'show_summary','m');"   data-original-title='`+Translation('عرض خلاصة حسابات الزبون للستائر الجملة')+`'>
                                                    <i class='icon-file-spreadsheet2'></i>`+Translation('عرض خلاصة حسابات الزبون للستائر الجملة')+`
                                                </a>
                                            </li>

                                            <li class='text-primary-600'>
                                                <a  class='text-pink-400' onclick="account_statement_date(`+row[0]+`,'show_summary','s');"  data-original-title='`+Translation('عرض خلاصة حسابات الزبون للستائر المفرد')+`'>
                                                    <i class='icon-file-spreadsheet2'></i>`+Translation('عرض خلاصة حسابات الزبون للستائر المفرد')+`
                                                </a>
                                            </li>

                                            <li class='text-primary-600'>
                                                <a  class='text-purple-400' onclick="account_statement_date(`+row[0]+`,'show_withdraw_summary','w');"   data-original-title='`+Translation('عرض خلاصة حسابات الزبون للقروض المستلمة')+`'>
                                                    <i class='icon-file-spreadsheet2'></i>`+Translation('عرض خلاصة حسابات الزبون للقروض المستلمة')+`
                                                </a>
                                            </li>

                                          </ul>
                                        </li>

                                        
                                        <!--
                                        <li class='text-primary-600'>
                                            <a href='#' class='text-teal-400' onclick="show_summary(`+row[0]+`,'raw')" data-popup='tooltip' data-original-title='`+Translation('عرض خلاصة حسابات الزبون للمواد الخام')+`'>
                                                <i class='icon-file-spreadsheet2'></i>
                                            </a>
                                        </li>

                                        <li class='text-primary-600'>
                                            <a href='#' class='text-orange-400' onclick="show_summary(`+row[0]+`,'m')" data-popup='tooltip' data-original-title='`+Translation('عرض خلاصة حسابات الزبون للستائر الجملة')+`'>
                                                <i class='icon-file-spreadsheet2'></i>
                                            </a>
                                        </li>

                                        <li class='text-primary-600'>
                                            <a href='#' class='text-pink-400' onclick="show_summary(`+row[0]+`,'s')" data-popup='tooltip' data-original-title='`+Translation('عرض خلاصة حسابات الزبون للستائر المفرد')+`'>
                                                <i class='icon-file-spreadsheet2'></i>
                                            </a>
                                        </li>

                                        <li class='text-primary-600'>
                                            <a href='#' class='text-purple-400' onclick="show_withdraw_summary(`+row[0]+`,'w')" data-popup='tooltip' data-original-title='`+Translation('عرض خلاصة حسابات الزبون للقروض المستلمة')+`'>
                                                <i class='icon-file-spreadsheet2'></i>
                                            </a>
                                        </li>
                                        

                                        <li class='text-primary-600'>
                                            <a href='#' class='text-purple-400' onclick="add_invoice_payment(`+row[0]+`)" data-popup='tooltip' data-original-title='`+Translation('اضافة دفعه على الحساب')+`'>
                                                <i class='icon-cash4'></i>
                                            </a>
                                        </li>
                                        -->
                                    </ul>`;
                        }
          },


            {
               "render": function(data, type, row){


                        // return  `<a onclick="show_invoice(`+row[0]+`)"  data-popup='tooltip' data-original-title='Show Invoice Details'>`+data+`</a>`;
                        
                        var ret = `<a onclick="load_account_statment(`+row[0]+`)"  data-popup='tooltip' data-original-title='`+Translation('عرض جميع كشوفات الحساب لهذا الزبون')+`'>`+data+`</a>`;
                        // var phones = row[10].split(';');

                        var phones1 = `<span class="label btn-block bg-success"> <i class="icon-phone-wave pull-left"></i> `+row[10]+`</span>`;
                        var phones2 = `<span class="label btn-block bg-purple"> <i class="icon-phone pull-left"></i> `+row[11]+`</span>`;
                        return  ret + phones1 + phones2;


               },
               "targets": 1
            },

            {
                "render": function(data, type, row){


                                                                      
                                            

                     status=  `<a href='#' class='text-success-400 btn-block' onclick="show_summary(`+row[0]+`)" title='`+Translation('عرض خلاصة حسابات الزبون')+`'>

                                <span class="label  label-info btn-block">`+Translation('عدد الوصولات الكلي')+` <span class=" label-rounded badge-warning pull-right">`+(+data + +row[9])+`</span></span>
                              </a>`;
                     status= status +  ` <a href='#' class='text-success-400 btn-block' onclick="show_summary(`+row[0]+`,'raw')" title='`+Translation('عرض خلاصة حسابات الزبون للمواد الخام')+`'>
                                          <span class="label  bg-teal-400 btn-block">`+Translation('عدد وصولات مواد الخام')+` <span class=" label-rounded bg-blue pull-right">`+row[8]+`</span></span>
                                          </a>`;
                     status= status +  `<a href='#' class='text-success-400 btn-block' onclick="show_summary(`+row[0]+`,'m')" title='`+Translation('عرض خلاصة حسابات الزبون للستائر الجملة')+`'>
                                          <span class="label  bg-orange-400 btn-block">`+Translation('عدد وصولات ستائر الجملة')+` <span class=" label-rounded bg-blue pull-right">`+row[7]+`</span></span>
                                          </a>`;
                     status= status +  `<a href='#' class='text-success-400 btn-block' onclick="show_summary(`+row[0]+`,'s')" title='`+Translation('عرض خلاصة حسابات الزبون للستائر المفرد')+`'>
                                          <span class="label  bg-pink-400 btn-block">`+Translation('عدد وصولات ستائر المفرد')+` <span class=" label-rounded bg-blue pull-right">`+row[6]+`</span></span>
                                          </a>`;

                    status= status +  `<a href='#' class='text-success-400 btn-block' onclick="show_withdraw_summary(`+row[0]+`,'w')" title='`+Translation('عرض خلاصة قروض الزبون')+`'>
                                          <span class="label  bg-purple-400 btn-block">`+Translation('عدد القروض المستلمة')+` <span class=" label-rounded bg-blue pull-right">`+row[9]+`</span></span>
                                          </a>`;


                  
                   return  status;
                },
                "targets": 2
            },
            
            {
                "render": function(data, type, row){

                    return currency_format_display(data);
                },
                "targets": 3
            },

            {
                "render": function(data, type, row){

                    return currency_format_display(data);
                },
                "targets": 4
            },

            {
                "render": function(data, type, row){
                    var dd = round(row[3],2) - round(row[4],2) - round(row[12],2) + round(row[13],2);
                    return currency_format_display(dd);
                },
                "targets": 5
            },

            
            

            {
                "render": function(data, type, row){
                  var dd = round(row[3],2) - round(row[4],2);

                   if(dd == 0 ){
                     status=  '<span class="label label-success btn-block ">'+Translation('واصل')+'</span>';
                   }else if(dd >0){
                     status = '<span class="label bg-danger-400  btn-block ">'+Translation('غير واصل')+'  </span>';
                   }else if(dd <0){
                     status = '<span class="label bg-violet  btn-block ">'+Translation('المبلغ المدفوع اكبر من الكلي')+'  </span>';
                   }else{
                     status = '<span class="label bg-violet  btn-block ">'+Translation('غير معروف')+'  </span>';
                   }
                   return  status;
                },
                "targets": 6
            }
            

           
        ],
        "fnRowCallback": function (nRow, aData, iDisplayIndex, iDisplayIndexFull) {


            if(   aData[5] > 0 ){ // outcome type
                $(nRow).children().each(function (index, td) {
                    $(this).addClass('alpha-danger');
                });
            }else if(aData[5] == 0){
                $(nRow).children().each(function (index, td) {
                    $(this).addClass('alpha-success');
                });
            }
        },
        "order": [[0,'desc']],
        "displayLength": 25,
      
        initComplete: function () {
         
         // $("div.datatable-header").append(`<a data-toggle="collapse"
         //                              style="margin-right:10px;"
         //                              data-target="#add-form-cont"
         //                              href="javascript:;"
         //                              aria-controls="add-form-cont"
         //                              aria-expanded="false"
         //                              class="btn btn-primary"
         //                              id="add_Item_Btn">
         //                                <i class="`+CURRENT_BTN_ICON+`"></i> `+CURRENT_BTN+`
         //                            </a>`); 

          $("div.datatable-header").append(`&nbsp;<div class="btn-group">
                          <button id="print_product" type="button"
                          class="btn bg-purple-400 btn-labeled " onclick="print_product(`+query_typ+`);"><b><i class="icon-printer2">
                          </i></b> <span id="inv_filter">`+Translation('طباعة القائمة')+`</span> </button>
                          
                      </div>`);
 
            // invoce_filter_btn_label();

            this.api().columns().every( function() {
                var column = this;
                

            });
            // escape_notes();
        }
    });

    
    table.columns().every( function () {
        var that = this;
        $('input', this.footer()).on('keyup change', function () {
            that.search(this.value).draw();
        });
    });


    // Single row selection
    
    $('.datatable-column-search-inputs tbody').on('click', 'tr', function() {
        if ($(this).hasClass('success')) {
            $(this).removeClass('success');
        }
        else {
            table.$('tr.success').removeClass('success');
            $(this).addClass('success');
        }
    });


         // change filter to top
    $('.datatable-column-search-inputs tfoot tr').insertAfter($('.datatable-column-search-inputs thead tr'))


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


});



