var table ;
    

    var HTML_VIEW_PAY = '';
    var print_page =  `
                        <script>
                          setTimeout(function() {
                            window.print()
                          }, 2000)
                        </script>
                        `;
    

     var HTML_Table = `
                      <!DOCTYPE html>
                      <html lang="ar" dir="rtl">
                      <head>
                        <title>aaaa</title>
                        <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:#9f9f9f;
                            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:#9f9f9f;
                            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;
                          } 
                          
                          .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;
                          }
                          
                          .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     =`
                            
                                  <meta http-equiv=Content-Type content="text/html; charset=utf-8">
                                  <meta name=Generator content="Microsoft Word 15 (filtered)">
                                  <style>
                                  
                                  .print {  
                                      -webkit-print-color-adjust: exact;
                                      }
                                      <!--
                                      /* 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: Tahoma;
                                          panose-1: 2 11 6 4 3 5 4 4 2 4;
                                      }
                                      /* 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: 595.3pt 841.9pt;
                                          margin: .3in .3in .3in .3in;
                                      }
                                      
                                      div.WordSection1 {
                                          page: WordSection1;
                                      }
                                      
                                      -->
                                  </style>

                              </head>

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

                                  <div class=WordSection1>

                                      <div align=center>

                              `;

 

   
    
  function show_invoice(itm,printing=null,detail_show=null){


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

          $.ajax({
                url : 'models/crul_archive.php',
                type : 'POST',
                data : {type:'show_invoice', id : itm},
                dataType : 'json',
                success : function(response) {
                    // console.log(response)
                    
                        headers =`

                                     <table  dir=rtl border=1 cellspacing=0 cellpadding=0
                                       style='background:white;border-collapse:collapse;border:none; font-weight:bold;'>
                                       <!--<tr>
                                        <td width=590 colspan=6 style='width:6.15in;border:none;padding:0in 5.4pt 0in 5.4pt'>       
                                          <span style="float: left; font-size: 20px;" id="inv_slogan">`+COMPANY_NAME+`</span>
                                        </td>
                                       </tr>-->
                                       <tr style='height:9.95pt'>
                                        <td width=295 colspan=3 style='width:221.4pt;' class="table0_td">
                                        `+Translation('رقم الوصل')+`  :
                                        <span id="inv_num" style="color:red;">`+response.invoice[0].invoice_number+`</span>
                                        <br>
                                        `+Translation('التــــــــــــاريخ')+`  :  
                                        <span id="inv_date">`+response.invoice[0].invoice_date+`</span>

                                        </td>
                                        <td width=295 align="center" colspan=2 rowspan=3 style='width:221.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 width=295 colspan=4 style='width:221.4pt;' 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>
                                       </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>
                                       
                                       
                                      </table>
                                      <br>
                                          `;
                        
                        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">
                                              `+Translation('اﺳﻢ اﻟﺰﺑﻮﻥ')+`
                                              </td>
                                              <td width=144 style='width:1.5in;' class="table1_td">
                                              `+Translation('ﺭﻗﻢ اﻟﻬﺎﺗﻒ')+`
                                              </td>
                                              <td width=144 style='width:1.5in;' class="table1_td last_td">
                                              `+Translation('اﻟﻌﻨﻮاﻥ')+`
                                              </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=68 style='width:50.7pt;' class="table2_td">
                                   `+Translation('اﻟﻤﺎﺩﺓ')+` 
                                    </td>
                                    <td width=64 style='width:48.3pt;' class="table2_td">
                                   `+Translation('اﻟﻜﻤﻴﺔ')+` 
                                    </td>
                                    <td width=114 style='width:85.5pt;' class="table2_td">
                                   `+Translation('اﻟﺘﻔﺎﺻﻴﻞ')+` 
                                    </td>
                                    <td width=87 style='width:65.25pt;' class="table2_td">
                                   `+Translation('اﻟﻄﻮﻝ اﻟﻜﻠﻲ')+` 
                                    </td>
                                    <td width=73 style='width:54.9pt;' class="table2_td">
                                   `+Translation('ﺳﻌﺮ اﻟﻮﺣﺪﺓ')+` 
                                    </td>
                                    <td width=79 style='width:58.95pt;' class="table2_td">
                                   `+Translation('ملاحظات')+` 
                                    </td>
                                    <td width=74 style='width:55.5pt;' class="table2_td last_td">
                                   `+Translation('اﻟﻤﺠﻤﻮﻉ')+` 
                                    </td>
                                   </tr>`;
                     
                      // show sell item details                       
                      for(var i=0 ; i < response.invoice.length;i++){

                        // compute and format discount
                        var disss = 0;
                        if(response.invoice[i].sell_discount != '0.00'){
                          if(response.invoice[i].sell_discount_type == 0){
                          var disss = response.invoice[i].sell_discount + " %";
                          }else{
                            var disss = currency_format_display(response.invoice[i].sell_discount);
                          }
                        }

                        // compute total row price

                        var total_row_price = 0;

                        if(response.invoice[i].other_id_fk > 0){
                          total_row_price = response.invoice[i].sell_item_quantity * response.invoice[i].item_sell_price_per_meter - response.invoice[i].actual_discount;
                          total_row_price = currency_format_display(total_row_price);
                        
                        }else{
                          total_row_price = response.invoice[i].total_length * response.invoice[i].item_sell_price_per_meter - response.invoice[i].actual_discount;
                          total_row_price = currency_format_display(total_row_price);
                        }



                        if(response.invoice[i].product_id_fk > 0){
  
                           // display sell item details
                        
                          var Det = '';
                          if(detail_show != null){

                            for(var j=0 ; j < response.details.length;j++){
                              var sell_id = response.invoice[i].sell_item_id;

                              if(sell_id == response.details[j].sell_item_id_fk){
                                // Det = Det + ` <input types="text"  class=" col-sm-4" value="`+response.details[j].sell_item_detail_height+`" readonly> `;
                                Det = Det + ` <span style="border: 1px solid black"  class="">`+response.details[j].sell_item_detail_height+`</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=68 style='width:50.7pt;'>
                                                      <span  id="inv_product"> `+response.invoice[i].product_name+` </span>
                                                    </td>
                                                    <td width=64 style='width:48.3pt;'>
                                                      <span  id="inv_quantity"> `+parseInt(response.invoice[i].sell_item_quantity)+` </span>
                                                    </td>
                                                    <td width=114 style='width:85.5pt; font-family:  Times, serif;'>
                                                      <span  id="inv_details">
                                            `+Det+`         </span>
                                                    </td>
                                                    <td width=87  style='width:65.25pt;'>
                                                      <span  id="inv_total_length"> `+response.invoice[i].total_length+` ﻡ </span>
                                                    </td>
                                                    <td width=73 style='width:54.9pt;'>
                                                      <span  id="inv_unit_price"> `+currency_format_display(response.invoice[i].item_sell_price_per_meter)+` </span>
                                                    </td>
                                                    <td width=79 style='width:58.95pt;'>
                                                      <span  id="inv_discount">`+ response.invoice[i].sell_item_notes +` </span>
                                                    </td>
                                                    <td width=74 style='width:55.5pt;' class="last_td">
                                                      <span  id="inv_total_row"> `+total_row_price+` </span>
                                                    </td>
                                                   </tr>
                                                  `;
                        
                        }else{ // other product

                          attach_other = attach_other + `
                                                  <tr style='page-break-inside:avoid;' class="td_body">
                                                    <td width=23 style='width:14.4pt;'>
                                                      <span  id="inv_count"> `+(response.invoice.length)+` </span>
                                                    </td>
                                                    <td width=68 style='width:50.7pt;'>
                                                      <span  id="inv_product"> `+response.invoice[i].product_name+` </span>
                                                    </td>
                                                    <td width=64 style='width:48.3pt;'>
                                                      <span  id="inv_quantity"> `+response.invoice[i].sell_item_quantity+` </span>
                                                    </td>
                                                    <td width=114 style='width:85.5pt;'>
                                                      <span  id="inv_details">
                                                      </span>
                                                    </td>
                                                    <td width=87  style='width:65.25pt;'>
                                                      <span  id="inv_total_length">  </span>
                                                    </td>
                                                    <td width=73 style='width:54.9pt;'>
                                                      <span  id="inv_unit_price"> `+currency_format_display(response.invoice[i].item_sell_price_per_meter)+` </span>
                                                    </td>
                                                    <td width=79 style='width:58.95pt;'>
                                                      <span  id="inv_discount">`+ response.invoice[i].sell_item_notes +` </span>
                                                    </td>
                                                    <td width=74 style='width:55.5pt;' class="last_td">
                                                      <span  id="inv_total_row"> `+total_row_price+` </span>
                                                    </td>
                                                   </tr>
                                                  `;

                        }
                       
                      } // for i

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

                      footers =`<tr style='page-break-inside:avoid;height:.1in'>
                                  <td width=439 colspan=7  style='width:329.05pt;border-top:solid #984806 1.0pt;
                                  border-left:solid #984806 1.0pt;border-bottom:none;border-right:none;
                                  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(response.invoice[0].total_discount)+`
                                  </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(response.invoice[0].invoice_total_price_actual)+`
                                  </td>
                                 </tr>

                                 <tr style='page-break-inside:avoid;height:.1in; background: white;'>
                                  
                                  <td width=591 colspan=8 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=517 colspan=6 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 colspan=2 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(response.account[0].total_invoice_price_all)+`
                                  </td>
                                 </tr>
                                 <tr style='page-break-inside:avoid;height:.1in'>
                                  <td width=517 colspan=6 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; color:#2b9408'>
                                 `+Translation('اﻟﻤﺒﻠﻎ اﻟﻜﻠﻲ اﻟﻤﺪﻓﻮﻉ')+` 
                                  </td>
                                  <td width=74 colspan=2 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(response.account[0].total_Paid_all)+`
                                  </td>
                                 </tr>
                                 <tr style='page-break-inside:avoid;height:.1in'>
                                  <td width=517 colspan=6 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; color:#2b9408'>
                                 `+Translation('خصومات الحساب الكلية')+`
                                  </td>
                                  <td width=74 colspan=2 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(response.account[0].total_payment_discount)+`
                                  </td>
                                 </tr>
                                 <tr style='page-break-inside:avoid;height:.1in'>
                                  <td width=517 colspan=6 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; color:#b92909'>
                                 `+Translation('اﻟﻤﺒﻠﻎ اﻟﻜﻠﻲ اﻟﻤﺘﺒﻘﻲ')+` 
                                  </td>
                                  <td width=74 colspan=2 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(response.account[0].total_remained_all)+`
                                  </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('ﻣﻼﺣﻈﺔ')+` : `+response.invoice[0].invoice_note+` </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>`;

                      

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

          }); // submit ajax


  }

   
  function show_invoice_curt(itm,printing=null,detail_show=null){

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

          $.ajax({
                url : 'models/crul_archive.php',
                type : 'POST',
                data : {type:'show_invoice_curt', id : itm},
                dataType : 'json',
                success : function(response) {
                    console.log(response)
                    
                        headers =`

                                    <table  dir=rtl border=1 cellspacing=0 cellpadding=0
                                       style='background:white;border-collapse:collapse;border:none; font-weight:bold;'>
                                       <!--<tr>
                                        <td width=590 colspan=6 style='width:6.15in;border:none;padding:0in 5.4pt 0in 5.4pt'>       
                                          <span style="float: left; font-size: 20px;" id="inv_slogan">`+COMPANY_NAME+`</span>
                                        </td>
                                       </tr>-->
                                       <tr style='height:9.95pt'>
                                        <td width=295 colspan=3 style='width:221.4pt;' class="table0_td">
                                        `+Translation('رقم الوصل')+`  :
                                        <span id="inv_num" style="color:red;">`+response.invoice[0].invoice_number+`</span>
                                        <br>
                                        `+Translation('التــــــــــــاريخ')+`  :  
                                        <span id="inv_date">`+response.invoice[0].invoice_date+`</span>

                                        </td>
                                        <td width=295 align="center" colspan=2 rowspan=3 style='width:221.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 width=295 colspan=4 style='width:221.4pt;' 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>
                                       </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>
                                       
                                       
                                      </table>
                                      <br>
                                          `;
                        
                        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">
                                             `+Translation('اسم الزبون')+` 
                                              </td>
                                              <td width=144 style='width:1.5in;' class="table1_td">
                                             `+Translation('رقم الهاتف')+` 
                                              </td>
                                              <td width=144 style='width:1.5in;' class="table1_td last_td">
                                             `+Translation('العنوان')+` 
                                              </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=68 style='width:50.7pt;' class="table2_td">
                                   `+Translation('المادة')+` 
                                    </td>
                                    <td width=64 style='width:48.3pt;' class="table2_td">
                                   `+Translation('الكمية')+` 
                                    </td>
                                    <td width=57 style='width:42.75pt;' class="table2_td">
                                    
                                   `+Translation('العرض')+` 
                                    </td>
                                    <td width=57 style='width:42.75pt;' class="table2_td">
                                   `+Translation('الارتفاع')+` 
                                    </td>
                                    <td width=87 style='width:65.25pt;' class="table2_td">
                                   `+Translation('الطول الكلي  / المساحة')+` 
                                    </td>
                                    <td width=73 style='width:54.9pt;' class="table2_td">
                                   `+Translation('سعر الوحدة')+`  
                                    </td>
                                    <td width=73 style='width:54.9pt;' class="table2_td">
                                   `+Translation('سعر متر الديكور')+`  
                                    </td>
                                    <td width=79 style='width:58.95pt;' class="table2_td">
                                   `+Translation('الملاحظات')+` 
                                    </td>
                                    <td width=74 style='width:55.5pt;' class="table2_td last_td">
                                   `+Translation('المجموع')+` 
                                    </td>
                                   </tr>`;
                     
                      var all_area = 0;
                      // show sell item details                       
                      for(var i=0 ; i < response.invoice.length;i++){

                        // compute and format discount
                        var disss = 0;
                        if(response.invoice[i].sell_discount != '0.00'){
                          if(response.invoice[i].sell_discount_type == 0){
                          var disss = response.invoice[i].sell_discount + " %";
                          }else{
                            var disss = currency_format_display(response.invoice[i].sell_discount);
                          }
                        }

                        // compute total row price

                        var total_row_price = 0;

                        all_area = +all_area + +(response.invoice[i].total_area);

                        if(response.invoice[i].other_id_fk > 0){

                          
                          total_row_price = response.invoice[i].sell_item_quantity * response.invoice[i].item_sell_price_per_meter - response.invoice[i].actual_discount;
                          // total_row_price = currency_format_display(total_row_price);
                        
                        }else{
                          if(response.invoice[i].total_area > 0){
                            total_row_price = +( round(response.invoice[i].total_area,2)  * response.invoice[i].item_sell_price_per_meter) + +(response.invoice[i].total_decor_width * response.invoice[i].decorate_price_per_meter) - response.invoice[i].actual_discount;
                            // total_row_price = currency_format_display(total_row_price);
                          }else{
                            total_row_price = response.invoice[i].total_length * response.invoice[i].item_sell_price_per_meter - response.invoice[i].actual_discount;
                            // total_row_price = currency_format_display(total_row_price);
                          }
                          
                        }

                        total_row_price = currency_format_display(round(total_row_price,2));


                        if(response.invoice[i].product_id_fk > 0){
  
                          // we should Check for total area if greater than 0 it mean its curtain and we should show the width else we show just height and collpase td
                          
                          if(response.invoice[i].total_area > 0){

                            // display sell item details
                            var attach_height ='';
                            var attach_width ='';
                            // i will show just one height and width because we have quantity
                            for(var j=0 ; j < response.details.length;j++){
                              var sell_id = response.invoice[i].sell_item_id;

                              if(sell_id == response.details[j].sell_item_id_fk){
                                
                                // attach_height =  ` <input types="text"  class="label bg-success-400 col-sm-6" value="`+response.details[j].sell_item_detail_height+`" readonly> `;
                                // attach_width  =  ` <input types="text"  class="label bg-success-400 col-sm-6" value="`+response.details[j].sell_item_detail_width+`" readonly> `;
                                
                                attach_height =  ` <span   class="  col-sm-4" >`+response.details[j].sell_item_detail_height+`</span>`;

                                attach_width  =  ` <span   class="  col-sm-4" >`+response.details[j].sell_item_detail_width+`</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=68 style='width:50.7pt;'>
                                                      <span  id="inv_product"> `+response.invoice[i].product_name+` </span>
                                                    </td>
                                                    <td width=64 style='width:48.3pt;'>
                                                      <span  id="inv_quantity"> `+parseInt(response.invoice[i].sell_item_quantity)+` </span>
                                                    </td>
                                                    <td width=57 style='width:42.75pt; font-family:  Times, serif;'>
                                                      <span  id="inv_details">
                                                      `+attach_width+`
                                                      </span>
                                                    </td>
                                                    <td width=57 style='width:42.75pt; font-family:  Times, serif;'>
                                                      <span  id="inv_details">
                                                      `+attach_height+`
                                                      </span>
                                                    </td>
                                                
                                                    <td width=87  style='width:65.25pt;'>
                                                      <span  id="inv_total_length"> `+round(response.invoice[i].total_area,2)+` م<sup>2</sup> </span>
                                                    </td>
                                                    <td width=73 style='width:54.9pt;'>
                                                      <span  id="inv_unit_price"> `+currency_format_display(response.invoice[i].item_sell_price_per_meter)+` </span>
                                                    </td>
                                                    <td width=73 style='width:54.9pt;'>
                                                      <span  id="inv_unit_price"> `+currency_format_display(response.invoice[i].decorate_price_per_meter)+` </span>
                                                    </td>
                                                    <td width=79 style='width:58.95pt;'>
                                                      <span  id="inv_discount">`+ response.invoice[i].sell_item_notes +` </span>
                                                    </td>
                                                    <td width=74 style='width:55.5pt;' class="last_td">
                                                      <span  id="inv_total_row"> `+total_row_price+` </span>
                                                    </td>
                                                   </tr>
                                                  `;

                          }else{ // raw products

                            // display sell item details
                        
                            var Det = '';
                            if(detail_show != null){

                              // display sell item details
                              for(var j=0 ; j < response.details.length;j++){
                                var sell_id = response.invoice[i].sell_item_id;

                                if(sell_id == response.details[j].sell_item_id_fk){
                                  Det = Det + ` <span   class="  col-sm-4" >`+response.details[j].sell_item_detail_height+`</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=68 style='width:50.7pt;'>
                                                      <span  id="inv_product"> `+response.invoice[i].product_name+` </span>
                                                    </td>
                                                    <td width=64 style='width:48.3pt;'>
                                                      <span  id="inv_quantity"> `+response.invoice[i].sell_item_quantity+` </span>
                                                    </td>
                                                    <td colspan="2" width=114 style='width:85.5pt; font-family:  Times, serif;'>
                                                      <span  id="inv_details">
                                            `+Det+`   </span>
                                                    </td>
                                                    <td width=87  style='width:65.25pt;'>
                                                      <span  id="inv_total_length"> `+response.invoice[i].total_length+` م </span>
                                                    </td>
                                                    <td width=73 style='width:54.9pt;'>
                                                      <span  id="inv_unit_price"> `+currency_format_display(response.invoice[i].item_sell_price_per_meter)+` </span>
                                                    </td>
                                                    <td width=73 style='width:54.9pt;'>
                                                      <span  id="inv_unit_price"> `+currency_format_display(response.invoice[i].decorate_price_per_meter)+` </span>
                                                    </td>
                                                    <td width=79 style='width:58.95pt;'>
                                                      <span  id="inv_discount">`+ response.invoice[i].sell_item_notes +` </span>
                                                    </td>
                                                    <td width=74 style='width:55.5pt;' class="last_td">
                                                      <span  id="inv_total_row"> `+total_row_price+` </span>
                                                    </td>
                                                   </tr>
                                                  `;


                          }
                          
                        
                        }else{ // other product

                          attach_other = attach_other + `
                                                  <tr style='page-break-inside:avoid;' class="td_body">
                                                    <td width=23 style='width:14.4pt;'>
                                                      <span  id="inv_count"> `+(response.invoice.length)+` </span>
                                                    </td>
                                                    <td width=68 style='width:50.7pt;'>
                                                      <span  id="inv_product"> `+response.invoice[i].product_name+` </span>
                                                    </td>
                                                    <td width=64 style='width:48.3pt;'>
                                                      <span  id="inv_quantity"> `+response.invoice[i].sell_item_quantity+` </span>
                                                    </td>
                                                    <td colspan="2" width=114 style='width:85.5pt;'>
                                                      <span  id="inv_details">
                                                      </span>
                                                    </td>
                                                    <td width=87  style='width:65.25pt;'>
                                                      <span  id="inv_total_length">  </span>
                                                    </td>
                                                    <td width=73 style='width:54.9pt;'>
                                                      <span  id="inv_unit_price"> `+currency_format_display(response.invoice[i].item_sell_price_per_meter)+` </span>
                                                    </td>
                                                    <td width=73 style='width:54.9pt;'>
                                                      <span  id="inv_unit_price">  </span>
                                                    </td>
                                                    <td width=79 style='width:58.95pt;'>
                                                      <span  id="inv_discount">`+ response.invoice[i].sell_item_notes +` </span>
                                                    </td>
                                                    <td width=74 style='width:55.5pt;' class="last_td">
                                                      <span  id="inv_total_row"> `+total_row_price+` </span>
                                                    </td>
                                                   </tr>
                                                  `;

                        }
                       
                      } // for i

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


                      all_area = round(all_area,2);


                      footers =`<tr style='page-break-inside:avoid;height:.1in'>
                                  <td width=439 colspan=9  style='width:329.05pt;border-top:solid #984806 1.0pt;
                                  border-left:solid #984806 1.0pt;border-bottom:none;border-right:none;
                                  background:white;padding:2.15pt 5.75pt 2.15pt 5.75pt;height:.1in'>
                                 `+Translation('التنزيلات')+` 
                                  </td>
                                  <td width=79 style='width:58.95pt;border:solid #984806 1.0pt;border-right:
                                  none;padding:0pt 0in 0pt 0in;height:.1in;text-align: center;'>
                                  `+currency_format_display(response.invoice[0].total_discount)+`
                                  </td>
                                  
                                 </tr>
                                 <tr style='page-break-inside:avoid;height:.1in'>
                                  <td width=517 colspan=9 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'>
                                   `+(all_area)+` م<sup>2</sup>
                                  </td>
                                 </tr>

                                 <tr style='page-break-inside:avoid;height:.1in'>
                                  <td width=517 colspan=9 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(response.invoice[0].invoice_total_price_actual)+`
                                  </td>
                                 </tr>

                                 <tr style='page-break-inside:avoid;height:.1in; background: white;'>
                                  
                                  <td width=591 colspan=10 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=517 colspan=8 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 colspan=2 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(response.account[0].total_invoice_price_all)+`
                                  </td>
                                 </tr>
                                 <tr style='page-break-inside:avoid;height:.1in'>
                                  <td width=517 colspan=8 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; color:#2b9408'>
                                 `+Translation('اﻟﻤﺒﻠﻎ اﻟﻜﻠﻲ اﻟﻤﺪﻓﻮﻉ')+` 
                                  </td>
                                  <td width=74 colspan=2 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(response.account[0].total_Paid_all)+`
                                  </td>
                                 </tr>

                                 <tr style='page-break-inside:avoid;height:.1in'>
                                  <td width=517 colspan=8 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; color:#2b9408'>
                                 `+Translation('خصومات الحساب الكلية')+` 
                                  </td>
                                  <td width=74 colspan=2 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(response.account[0].total_payment_discount)+`
                                  </td>
                                 </tr>
                                 <tr style='page-break-inside:avoid;height:.1in'>
                                  <td width=517 colspan=8 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; color:#b92909'>
                                 `+Translation('اﻟﻤﺒﻠﻎ اﻟﻜﻠﻲ اﻟﻤﺘﺒﻘﻲ')+` 
                                  </td>
                                  <td width=74 colspan=2 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(response.account[0].total_remained_all)+`
                                  </td>
                                 </tr>
                                 <tr style='page-break-inside:avoid;height:.3in'>
                                 
                                  <td width=517 colspan=10  style='width:388.0pt;border:none;background:white;
                                    padding:2.15pt 5.75pt 2.15pt 5.75pt;height:.1in'>
                                    <span id="inv_note"> `+Translation('ملاحظة')+` : `+response.invoice[0].invoice_note+` </span>
                                  </td>
                                 </tr>
                                 
                                 <tr style='page-break-inside:avoid;height:.1in; background-color: white'>
                                  <td width=517 colspan=9  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>`;

                      

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

          }); // submit ajax


  }


 

    function show_sticker(itm,printing=null){

          var headers           ='';
          var attach            = "";
          var attach_other      = "";
          var footers           ="";
          var customer_details  = "";
          var print_attach      = "";
          var total_pay         = "";
          $.ajax({
                url : 'models/crul_invoice_curt.php',
                type : 'POST',
                data : {type:'show_sticker', id : itm},
                dataType : 'json',
                success : function(response) {
                    console.log(response)
                    
                       
                       var tot = response.sticker.length;
                     
                      // show all stickers                       
                      for(var i=0 ; i < response.sticker.length;i++){

                          attach = attach + `
                                            <table class=MsoNormalTable dir=rtl border=0 cellspacing=0 cellpadding=0 width=353 style='width:260.8pt;border-collapse:collapse; font-weight: bolder;'>
                                               <tr style='height:19.9pt; text-align: center;'>
                                                  <td width=108 style='font-size:14px; width:81.05pt;padding:0in .4pt 0in .4pt;height:19.9pt'>
                                                   <span style="float:right;">`+response.sticker[i].invoice_number+`</span>
                                                  
                                                  </td>
                                                  <td width=245 colspan=2 style='width:183.75pt;padding:0in .4pt 0in .4pt;
                                                  height:19.9pt'>
                                                  `+COMPANY_NAME+`
                                                  </td>
                                                  <td width=108 style='font-size:14px; width:81.05pt;padding:0in .4pt 0in .4pt;height:19.9pt'>
                                                  `+response.sticker[i].invoice_date+`
                                                  </td>

                                               </tr>
                                               <tr style='height:19.9pt'>
                                                  <td width=95 style='width:71.25pt;padding:0in .4pt 0in .4pt;height:19.9pt'>
                                                  `+Translation('الزبون')+` :
                                                  </td>
                                                  <td width=258 colspan=3 style='width:193.55pt;padding:0in .4pt 0in .4pt;
                                                  height:19.9pt'>
                                                  `+response.sticker[i].customer_full_name+`
                                                  </td>
                                               </tr>
                                               <tr style='height:19.9pt'>
                                                  <td width=95 style='width:71.25pt;padding:0in .4pt 0in .4pt;height:19.9pt'>
                                                  `+Translation('الرمز')+` :
                                                  </td>
                                                  <td width=258 colspan=3 style='width:193.55pt;padding:0in .4pt 0in .4pt;
                                                  height:19.9pt'>
                                                  `+response.sticker[i].product_name+`
                                                  </td>
                                               </tr>
                                               <tr style='height:19.9pt'>
                                                  <td width=95 style='width:71.25pt;padding:0in .4pt 0in .4pt;height:19.9pt'>
                                                  `+Translation('العرض (م)')+` :
                                                  </td>
                                                  <td width=67 style='width:50.5pt;padding:0in .4pt 0in .4pt;height:19.9pt'>
                                                  `+response.sticker[i].sell_item_detail_width+`
                                                  </td>
                                                  <td width=108 style='width:81.05pt;padding:0in .4pt 0in .4pt;height:19.9pt'>
                                                  `+Translation('الارتفاع (م)')+` :
                                                  </td>
                                                  <td width=83 style='width:62.0pt;padding:0in .4pt 0in .4pt;height:19.9pt'>
                                                  `+response.sticker[i].sell_item_detail_height+`
                                                  </td>
                                               </tr>
                                               <tr style='height:19.9pt'>
                                                  <td width=95 style='width:71.25pt;padding:0in .4pt 0in .4pt;height:19.9pt'>
                                                  `+Translation('ملاحظات')+` :
                                                  </td>
                                                  <td width=258 colspan=3 style='width:193.55pt;padding:0in .4pt 0in .4pt;
                                                  height:19.9pt'>
                                                  `+response.sticker[i].sell_item_notes+`
                                                  </td>
                                               </tr>
                                               <tr style='height:19.9pt'>
                                                  <td width=95 style='width:71.25pt;padding:0in .4pt 0in .4pt;height:19.9pt'>
                                                  `+Translation('المستلم')+` :
                                                  </td>
                                                  <td width=258 colspan=3 style='width:193.55pt;padding:0in .4pt 0in .4pt;
                                                  height:19.9pt'>
                                                  `+response.sticker[i].sell_item_delivery_name+`
                                                  <span style="float:left;">`+(+i+1)+`/`+tot+` </span>
                                                  </td>
                                               </tr>
                                               
                                            </table>
                                            <div class="page-break"></div>
                                            <br>
                                                  `;
                      
                       
                      } // for i

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

                      footers =`        </div>

                                      </div>

                                  </body>

                                </html>

                                `+print_attach+`
                               `;

                      

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

                      HTML_VIEW_PAY = HTML_Sticker + headers + customer_details + attach + attach_other + footers;
                       //  me.data('requestRunning', false);
                      
                        $("#sticker_show").html(HTML_VIEW_PAY);
                        $("#sticker_inv_numb").val(itm);

                        $('#show_sticker_modal').modal('toggle');
                      
                     
                        // window.open().document.write(HTML_VIEW);
                }

          }); // submit ajax


    }

  

  







    

  

    function get_Staff(){

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

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

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

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

                }); // submit ajax

    }

    function print_invoice(){
  
      var tbl = $("#print_report").html();
      window.open().document.write(HTML_Table+tbl+print_page);
      
    }

    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 set_date(){

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

     

    function get_Customer(){

            $.ajax({
                  url : 'models/crul_customer.php',
                  type : 'POST',
                  data : {type:'get_Customer',cust_typ:0},
                  dataType : 'json',
                  success : function(response) {
                    //console.log(response)
                      var attach ='<option value="" selected="selected">'+Translation('ﺗﺤﺪﻳﺪ اﻟﺰﺑﻮﻥ')+'</option>';
                      var aaa='';
                        for(var i=response.length-1 ; i >= 0;i--){
                          var tp  = response[i][2];
                          var tp1 = response[i][3];
                          attach = attach + '<option data-subtext="'+Translation('ﻫﺎﺗﻒ')+': '+tp+' || 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);
                        $('#customer_id_fk').selectpicker('refresh');
                  },
                  complete: function() {
                         //  me.data('requestRunning', false);
                  }

            }); // submit ajax

    }

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

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



    function archive() {
      
        swal({
            title: Translation("هل انت متأكد؟!"),
            text: Translation(`سوف يتم ترحيل بيانات النظام وارشفتها الى جداول الارشفة ، والجداول التي سوف يتم ترحيلها هي :
                                  1- وصولات البيع 
                                  2- كشوفات الحسابات (حيث سيتم احتساب المبلغ المتبقي على كل زبون واضافته في جدول القروض كدين سابق)
                                  3- المخازن
                                  4- تسديد دفوعات الزبائن
                                  5- القروض والديون

                                  الرجاء قم بأخذ نسخة احتياطية لقاعدة بيانات النظام قبل البدء بعملية الترحيل لانه بمجرد البدء سوف لن تستطيع اعادة البيانات المرحلة
                                  `),
            type: "warning",
            showCancelButton: true,
            confirmButtonColor: "#EF5350",
            confirmButtonText: Translation("موافق"),
            cancelButtonText: Translation("الغاء الامر"),
            closeOnConfirm: false,
            closeOnCancel: true
        },
        function(isConfirm){
            if (isConfirm) {

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

              $("#close_yearly").attr('disabled',true);


              $.ajax({

                url: "models/crul_archive.php",
                type:"POST",
                dataType : "JSON",
                data: {"type":"archive",
                        d1 : d1,
                        d2 : d2
                      },
                complete:function(){
                  hideLoader();
                  $("#close_yearly").attr('disabled',false);

                },
                beforeSend:function(){
                  showLoader("الرجاء الانتظار لحين استكمال عملية الترحيل ..ويرجى عدم استخدام النظام من قبل اي موظف لحين استكمال عملية الترحيل");
                  
                },
                success: function(res) {
                  if(res.is_success == true){
                    swal({
                                title: Translation("تمت عملية الترحيل"),
                                text: (res.data),
                                confirmButtonColor: "#66BB6A",
                                type: "success"
                            });
                    // $(".datatable-column-search-inputs").DataTable().ajax.reload(null, false);
                  }else{
                    swal({
                                title: Translation("خطأ"),
                                text: (res.data),
                                confirmButtonColor: "#2196F3",
                                type: "error"
                            });
                  }
                }
            });
            }
        }); // /confirm
    }



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

    $('#print_btn').click(function(e) {
      e.preventDefault();
         print_invoice();
    });

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

   

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

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

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


    });




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

        var d1             = $("#first_date").val();
        var d2             = $("#second_date").val();
        var box_id_fk      = $("#box_id_fk").val();
        var report_type    = $("#report_type").val();
        var customer_id_fk = $("#customer_id_fk").val();
        
        

        var attach = "";

        
        $("#table2").attr('style','display:none;');
        $("#table2_sum").attr('style','display:none;');

        $.ajax({
                url: "models/crul_archive.php",
                type:"POST",
                dataType : "JSON",
                data: {"type":"sell_report",
                        d1 : d1,
                        d2 : d2,
                        customer_id_fk : customer_id_fk,
                        report_type : report_type

                      } ,
                complete:function(){
                  hideLoader();
                },
                beforeSend:function(){
                  showLoader("الرجاء الانتظار لحين استكمال تجميع معلومات التقرير");
                  
                },
                success: function(res) {
                  if(res.is_success == true){

                    var total_inv = 0;
                    var total_paid = 0;
                    var total_disc = 0;
                    var total_m = 0;
                    var total_tac = 0;
                    var total_isik = 0;
                    var total_cost = 0;

                    $("#table1").attr('style','');
                    
                    if(report_type == 1){
                      $("#table1").attr('style','');

                      attach = attach + `<thead>
                                            <tr class="bg-blue-300">
                                              <th>ID</th>
                                              <th class="multi_lang">رقم الوصل</th>
                                              <th class="multi_lang">التاريخ</th>
                                              <th class="multi_lang">الزبون</th>
                                              <th class="multi_lang">المبلغ</th>
                                              <th class="multi_lang"> التنزيلات </th>
                                              <th class="multi_lang">المبلغ الصافي</th>
                                              <th class="multi_lang">الملاحظات</th>
                                              <th class="multi_lang">الموظف</th>
                                             
                                              <th class="multi_lang">Action</th>
                                            </tr>
                                          </thead>
                                          <tbody id="table_report">`;

                      for(var i=0 ; i <=res.data.length-1;i++){
                        var mm = res.data[i].total_PVC;
                        mm = parseFloat(mm)
                        attach = attach + `
                                          <tr>
                                              <td>`+res.data[i].invoice_id+`</td>
                                              <td><a  onclick="show_invoice('`+res.data[i].invoice_id+`')" class='text-info-400' data-popup='tooltip' data-original-title='`+Translation('عرض صور المادة')+`'>
                                              `+res.data[i].invoice_number+`</a></td>
                                              <td>`+res.data[i].time+`</td>
                                              <td>`+res.data[i].customer_name+`</td>
                                              <td>`+currency_format_display(res.data[i].total_invoice_discount)+`</td>
                                              <td>`+currency_format_display(res.data[i].actual_discount)+`</td>
                                              <td>`+currency_format_display(res.data[i].final_price)+`</td>
                                              <td>`+res.data[i].invoice_note+`</td>
                                              <td>`+res.data[i].staff+`</td>
                                              <td><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-orange-400' onclick="show_invoice(`+res.data[i].invoice_id+`)" data-popup='tooltip' data-original-title='`+Translation('عرض وصل البيع')+`'>
                                                              <i class='icon-calculator'></i> `+Translation('عرض وصل البيع')+`
                                                          </a>
                                                      </li>

                                                      <li class='text-primary-600'>
                                                          <a  class='text-pink-400' onclick="show_invoice(`+res.data[i].invoice_id+`,'Print')" data-popup='tooltip' data-original-title='`+Translation('طباعة الوصل')+`'>
                                                              <i class='icon-printer4'></i>`+Translation('طباعة الوصل')+`
                                                          </a>
                                                      </li>

                                                     

                                                    

                                                    </ul>
                                                  </li>
                                                  


                                              </ul></td>
                                          </tr>
                                          `;
                        total_inv   = +total_inv + +(res.data[i].final_price);
                        total_paid  = +total_paid + +(res.data[i].total_invoice_discount);
                        total_disc  = +total_disc + +(res.data[i].actual_discount);
                        

                      }

                      attach = attach + `</tbody>
                                          <tfoot>
                                            <tr class="alpha-info">
                                                      
                                                      <td colspan="2" class="multi_lang"><i  class="icon-sigma "></i>
                                                      المجموع</td>
                                                      <td></td>
                                                      <td></td>
                                                      

                                                      <td id="total_paid">`+currency_format_display(total_paid)+`</td>
                                                      <td id="total_disc">`+currency_format_display(total_disc)+`</td>
                                                      <td id="total_invoice">`+currency_format_display(total_inv)+`</td>
                                                      
                                                      <td></td>
                                                      <td></td>
                                                      <td></td>
                                                      
                                                      
                                                  </tr>
                                          </tfoot>`;

                      $("#table1").html(attach);
                     
                      
                    }else{

                      attach = attach + `<thead>
                                            <tr class="bg-blue-300">
                                              <th>ID</th>
                                              <th class="multi_lang">مبلغ المبيعات</th>
                                              <th class="multi_lang"> التنزيلات </th>
                                             
                                            </tr>
                                          </thead>
                                          <tbody id="table_report_sum">`;
                      
                      for(var i=0 ; i <=res.data.length-1;i++){

                        attach = attach + `
                                          <tr>
                                              <td>1</td>
                                             
                                              <td>`+currency_format_display(res.data[i].final_price)+`</td>
                                              <td>`+res.data[i].actual_discount+`</td>
                                              
                                          </tr>
                                          `;
                        total_inv   = +total_inv + +(res.data[i].final_price);
                       
                      }

                      attach = attach + `</tbody>
                                          <tfoot>
                                              <tr class="alpha-info">
                                                <td> <i class="icon-sigma "></i></td>
                                                <td id="total_invoice_sum" class="multi_lang">`+currency_format_display(total_inv)+`</td>
                                                <td ></td>
                                                      
                                                      
                                                      
                                              </tr>
                                          </tfoot> `;

                      $("#table1").html(attach);
                      // $("#total_invoice_sum").html(currency_format_display(total_inv));
                      // $("#total_paid_sum").html(currency_format_display(total_paid));
                      // $("#total_m_sum").html((total_m.toFixed(4) ));
                      // $("#total_tac_sum").html((total_tac));
                      // $("#total_isik_sum").html((total_isik));
                      // $("#total_cost_sum").html(currency_format_display(total_cost));

                     

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

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


    });


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

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

        var attach = "";

        
        $("#table2").attr('style','display:none;');
        $("#table2_sum").attr('style','display:none;');

        $.ajax({
                url: "models/crul_archive.php",
                type:"POST",
                dataType : "JSON",
                data: {"type":"sell_report_plural",
                        d1 : d1,
                        d2 : d2,
                        customer_id_fk : customer_id_fk,
                        report_type : report_type

                      } ,
                complete:function(){
                  hideLoader();
                },
                beforeSend:function(){
                  showLoader("الرجاء الانتظار لحين استكمال تجميع معلومات التقرير");
                  
                },
                success: function(res) {
                  if(res.is_success == true){

                    var total_inv = 0;
                    var total_paid = 0;
                    var total_disc = 0;
                    var total_m = 0;
                    var total_tac = 0;
                    var total_isik = 0;
                    var total_cost = 0;

                    $("#table1").attr('style','');
                    
                    if(report_type == 1){
                      $("#table1").attr('style','');

                      attach = attach + `<thead>
                                            <tr class="bg-blue-300">
                                              <th>ID</th>
                                              <th class="multi_lang">رقم الوصل</th>
                                              <th class="multi_lang">التاريخ</th>
                                              <th class="multi_lang">الزبون</th>
                                              <th class="multi_lang">المبلغ</th>
                                              <th class="multi_lang"> التنزيلات </th>
                                              <th class="multi_lang">المبلغ الصافي</th>
                                              <th class="multi_lang">الملاحظات</th>
                                              <th class="multi_lang">الموظف</th>
                                             
                                              <th class="multi_lang">Action</th>
                                            </tr>
                                          </thead>
                                          <tbody id="table_report">`;

                      for(var i=0 ; i <=res.data.length-1;i++){
                        var mm = res.data[i].total_PVC;
                        mm = parseFloat(mm)
                        attach = attach + `
                                          <tr>
                                              <td>`+res.data[i].invoice_id+`</td>
                                              <td><a  onclick="show_invoice_curt('`+res.data[i].invoice_id+`')" class='text-info-400' data-popup='tooltip' data-original-title='`+Translation('عرض صور المادة')+`'>
                                              `+res.data[i].invoice_number+`</a></td>
                                              <td>`+res.data[i].time+`</td>
                                              <td>`+res.data[i].customer_name+`</td>
                                              <td>`+currency_format_display(res.data[i].total_invoice_discount)+`</td>
                                              <td>`+currency_format_display(res.data[i].actual_discount)+`</td>
                                              <td>`+currency_format_display(res.data[i].final_price)+`</td>
                                              <td>`+res.data[i].invoice_note+`</td>
                                              <td>`+res.data[i].staff+`</td>
                                              <td><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-orange-400' onclick="show_invoice_curt(`+res.data[i].invoice_id+`)" data-popup='tooltip' data-original-title='`+Translation('عرض وصل البيع')+`'>
                                                              <i class='icon-calculator'></i> `+Translation('عرض وصل البيع')+`
                                                          </a>
                                                      </li>

                                                      <li class='text-primary-600'>
                                                          <a  class='text-pink-400' onclick="show_invoice_curt(`+res.data[i].invoice_id+`,'Print')" data-popup='tooltip' data-original-title='`+Translation('طباعة الوصل')+`'>
                                                              <i class='icon-printer4'></i>`+Translation('طباعة الوصل')+`
                                                          </a>
                                                      </li>

                                                     

                                                    

                                                    </ul>
                                                  </li>
                                                  


                                              </ul></td>
                                          </tr>
                                          `;
                        total_inv   = +total_inv + +(res.data[i].final_price);
                        total_paid  = +total_paid + +(res.data[i].total_invoice_discount);
                        total_disc  = +total_disc + +(res.data[i].actual_discount);
                        

                      }

                      attach = attach + `</tbody>
                                          <tfoot>
                                            <tr class="alpha-info">
                                                      
                                                      <td colspan="2" class="multi_lang"><i  class="icon-sigma "></i>
                                                      المجموع</td>
                                                      <td></td>
                                                      <td></td>
                                                      

                                                      <td id="total_paid">`+currency_format_display(total_paid)+`</td>
                                                      <td id="total_disc">`+currency_format_display(total_disc)+`</td>
                                                      <td id="total_invoice">`+currency_format_display(total_inv)+`</td>
                                                      
                                                      <td></td>
                                                      <td></td>
                                                      <td></td>
                                                      
                                                      
                                                  </tr>
                                          </tfoot>`;

                      $("#table1").html(attach);
                     
                      
                    }else{

                      attach = attach + `<thead>
                                            <tr class="bg-blue-300">
                                              <th>ID</th>
                                              <th class="multi_lang">مبلغ المبيعات</th>
                                              <th class="multi_lang"> التنزيلات </th>
                                              
                                            </tr>
                                          </thead>
                                          <tbody id="table_report_sum">`;
                      
                      for(var i=0 ; i <=res.data.length-1;i++){

                        attach = attach + `
                                          <tr>
                                              <td>1</td>
                                             
                                              <td>`+currency_format_display(res.data[i].final_price)+`</td>
                                              <td>`+res.data[i].actual_discount+`</td>
                                              
                                          </tr>
                                          `;
                        total_inv   = +total_inv + +(res.data[i].final_price);
                        
                      }

                      attach = attach + `</tbody>
                                          <tfoot>
                                              <tr class="alpha-info">
                                                <td> <i class="icon-sigma "></i></td>
                                                <td id="total_invoice_sum" class="multi_lang">`+currency_format_display(total_inv)+`</td>
                                                <td ></td>
                                                 
                                                      
                                                      
                                              </tr>
                                          </tfoot> `;

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

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

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


    });

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

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

        var attach = "";

        
        $("#table2").attr('style','display:none;');
        $("#table2_sum").attr('style','display:none;');

        $.ajax({
                url: "models/crul_archive.php",
                type:"POST",
                dataType : "JSON",
                data: {"type":"sell_report_single",
                        d1 : d1,
                        d2 : d2,
                        customer_id_fk : customer_id_fk,
                        report_type : report_type

                      } ,
                complete:function(){
                  hideLoader();
                },
                beforeSend:function(){
                  showLoader("الرجاء الانتظار لحين استكمال تجميع معلومات التقرير");
                  
                },
                success: function(res) {
                  if(res.is_success == true){

                    var total_inv = 0;
                    var total_paid = 0;
                    var total_disc = 0;
                    var total_m = 0;
                    var total_tac = 0;
                    var total_isik = 0;
                    var total_cost = 0;

                    $("#table1").attr('style','');
                    
                    if(report_type == 1){
                      $("#table1").attr('style','');

                      attach = attach + `<thead>
                                            <tr class="bg-blue-300">
                                              <th>ID</th>
                                              <th class="multi_lang">رقم الوصل</th>
                                              <th class="multi_lang">التاريخ</th>
                                              <th class="multi_lang">الزبون</th>
                                              <th class="multi_lang">المبلغ</th>
                                              <th class="multi_lang"> التنزيلات </th>
                                              <th class="multi_lang">المبلغ الصافي</th>
                                              <th class="multi_lang">الملاحظات</th>
                                              <th class="multi_lang">الموظف</th>
                                             
                                              <th class="multi_lang">Action</th>
                                            </tr>
                                          </thead>
                                          <tbody id="table_report">`;

                      for(var i=0 ; i <=res.data.length-1;i++){
                        var mm = res.data[i].total_PVC;
                        mm = parseFloat(mm)
                        attach = attach + `
                                          <tr>
                                              <td>`+res.data[i].invoice_id+`</td>
                                              <td><a  onclick="show_invoice_curt('`+res.data[i].invoice_id+`')" class='text-info-400' data-popup='tooltip' data-original-title='`+Translation('عرض صور المادة')+`'>
                                              `+res.data[i].invoice_number+`</a></td>
                                              <td>`+res.data[i].time+`</td>
                                              <td>`+res.data[i].customer_name+`</td>
                                              <td>`+currency_format_display(res.data[i].total_invoice_discount)+`</td>
                                              <td>`+currency_format_display(res.data[i].actual_discount)+`</td>
                                              <td>`+currency_format_display(res.data[i].final_price)+`</td>
                                              <td>`+res.data[i].invoice_note+`</td>
                                              <td>`+res.data[i].staff+`</td>
                                              <td><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-orange-400' onclick="show_invoice_curt(`+res.data[i].invoice_id+`)" data-popup='tooltip' data-original-title='`+Translation('عرض وصل البيع')+`'>
                                                              <i class='icon-calculator'></i> `+Translation('عرض وصل البيع')+`
                                                          </a>
                                                      </li>

                                                      <li class='text-primary-600'>
                                                          <a  class='text-pink-400' onclick="show_invoice_curt(`+res.data[i].invoice_id+`,'Print')" data-popup='tooltip' data-original-title='`+Translation('طباعة الوصل')+`'>
                                                              <i class='icon-printer4'></i>`+Translation('طباعة الوصل')+`
                                                          </a>
                                                      </li>

                                                     

                                                    

                                                    </ul>
                                                  </li>
                                                  


                                              </ul></td>
                                          </tr>
                                          `;
                        total_inv   = +total_inv + +(res.data[i].final_price);
                        total_paid  = +total_paid + +(res.data[i].total_invoice_discount);
                        total_disc  = +total_disc + +(res.data[i].actual_discount);
                        

                      }

                      attach = attach + `</tbody>
                                          <tfoot>
                                            <tr class="alpha-info">
                                                      
                                                      <td colspan="2" class="multi_lang"><i  class="icon-sigma "></i>
                                                      المجموع</td>
                                                      <td></td>
                                                      <td></td>
                                                      

                                                      <td id="total_paid">`+currency_format_display(total_paid)+`</td>
                                                      <td id="total_disc">`+currency_format_display(total_disc)+`</td>
                                                      <td id="total_invoice">`+currency_format_display(total_inv)+`</td>
                                                      
                                                      <td></td>
                                                      <td></td>
                                                      <td></td>
                                                      
                                                      
                                                  </tr>
                                          </tfoot>`;

                      $("#table1").html(attach);
                     
                      
                    }else{

                      attach = attach + `<thead>
                                            <tr class="bg-blue-300">
                                              <th>ID</th>
                                              <th class="multi_lang">مبلغ المبيعات</th>
                                              <th class="multi_lang"> التنزيلات </th>
                                              
                                            </tr>
                                          </thead>
                                          <tbody id="table_report_sum">`;
                      
                      for(var i=0 ; i <=res.data.length-1;i++){

                        attach = attach + `
                                          <tr>
                                              <td>1</td>
                                             
                                              <td>`+currency_format_display(res.data[i].final_price)+`</td>
                                              <td>`+res.data[i].actual_discount+`</td>
                                             
                                          </tr>
                                          `;
                        total_inv   = +total_inv + +(res.data[i].final_price);
                       
                      }

                      attach = attach + `</tbody>
                                          <tfoot>
                                              <tr class="alpha-info">
                                                <td> <i class="icon-sigma "></i></td>
                                                <td id="total_invoice_sum" class="multi_lang">`+currency_format_display(total_inv)+`</td>
                                                <td ></td>
                                                   
                                                      
                                                      
                                              </tr>
                                          </tfoot> `;

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

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

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


    });


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

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

        var attach = "";

        $("#table1").attr('style','');
        


        $.ajax({
                url: "models/crul_archive.php",
                type:"POST",
                dataType : "JSON",
                data: {"type":"safe_report",
                        d1 : d1,
                        d2 : d2,
                        box_id_fk : box_id_fk,
                        report_type : report_type

                      } ,
                complete:function(){
                  hideLoader();
                },
                beforeSend:function(){
                  showLoader("الرجاء الانتظار لحين استكمال تجميع معلومات التقرير");

                },
                success: function(res) {
                  if(res.is_success == true){

                    var total_inv = 0;
                    
                    if(report_type == 1){

                      attach = attach + `<thead>
                                          <tr class="bg-blue-300">
                                            <th>ID</th>
                                            <th class="multi_lang">رقم الوصل</th>
                                            <th class="multi_lang">المبلغ</th>
                                            <th class="multi_lang">التاريخ</th>
                                            <th class="multi_lang">Transaction</th>
                                            <th class="multi_lang">النوع</th>
                                            <th class="multi_lang">الموظف</th>
                                            <th class="multi_lang"> الصندوق </th>
                                            <th class="multi_lang"> ملاحظة </th>
                                            <th class="multi_lang"> خصائص </th>
                                            
                                          </tr>
                                        </thead>
                                        <tbody id="table_report_safe">`;

                      
                      for(var i=0 ; i <=res.data.length-1;i++){
                        var typ = "";
                        var proper = "";
                        if(res.data[i].transaction_type == 1){
                          typ = "<span class='label label-success'>اضافة الى الخزنة</span>"
                        }else if(res.data[i].transaction_type == 2){
                          typ = "<span class='label label-warning'>سحب من الخزنة</span>"
                        }else if(res.data[i].transaction_type == 3){
                          typ = "<span class='label label-info'>وصل بيع</span>";

                          var are = parseFloat((res.data[i].area)/1000000);
                          proper = "M<sup>2</sup> : " + are.toFixed(4)
                          proper = proper + "<br>TAC : " + res.data[i].tac
                          proper =proper + "<br>ISIK : " + res.data[i].isik
                        }

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

                      }

                       attach = attach + `
                                          </tbody>
                                          <tfoot>
                                            <tr class="alpha-info">
                                                      <td></td>
                                                      <td class="multi_lang">مجموع المبالغ الداخلة</td>
                                                      <td id="total_in">`+currency_format_display(res.data[0].total_amount)+`</td>
                                                      <td class="multi_lang">مجموع المبالغ الخارجة</td>
                                                      <td id="total_out">`+currency_format_display(res.data[0].total_subtract)+`</td>
                                                      <td class="multi_lang">المتبقي في الخزنة</td>
                                                      <td id="total_rem">`+currency_format_display(res.data[0].total_amount - res.data[0].total_subtract)+`</td>
                                                      <td class="multi_lang">مجموع المبيعات</td>
                                                      <td id="total_sell">`+currency_format_display(res.data[0].total_sell)+`</td>
                                                      
                                                      <td></td>
                                                      
                                                      
                                                  </tr>
                                          </tfoot>
                                           `;

                      $("#table1").html(attach);
                      // $("#total_in").html(currency_format_display(res.data[0].total_amount));
                      // $("#total_out").html(currency_format_display(res.data[0].total_subtract));
                      // $("#total_rem").html(currency_format_display(res.data[0].total_amount - res.data[0].total_subtract));
                      // $("#total_sell").html(currency_format_display(res.data[0].total_sell));

                    }else{

                      attach = attach + `<thead>
                                            <tr class="bg-blue-300">
                                              <th>ID</th>
                                              <th class="multi_lang">المبلغ الكلي المظاف الى الخزنة</th>
                                              <th class="multi_lang">المبلغ الكلي المسحوب من الخزنة</th>
                                              <th class="multi_lang bg-blue-300">المبلغ المتوفر في الصندوق</th>
                                              <th class="multi_lang">المبيعات الكلية</th>
                                              <th class="multi_lang">Total M<sup>2</sup></th>
                                              <th class="multi_lang">Total TAC</th>
                                              <th class="multi_lang">Total ISIK</th>
                                              <th class="multi_lang"> الصندوق </th>
                                              
                                            </tr>
                                          </thead>
                                          <tbody id="table_report_safe_sum">`;
                      
                      for(var i=0 ; i <=res.data.length-1;i++){
                        var typ = "";
                        if(res.data[i].transaction_type == 1){
                          typ = "<span class='label label-success'>اضافة الى الخزنة</span>"
                        }else if(res.data[i].transaction_type == 2){
                          typ = "<span class='label label-warning'>سحب من الخزنة</span>"
                        }else if(res.data[i].transaction_type == 3){
                          typ = "<span class='label label-info'>وصل بيع</span>"
                        }

                        var are = parseFloat((res.data[i].area)/1000000);

                        attach = attach + `
                                          <tr>
                                              <td>1</td>
                                              <td>`+currency_format_display(res.data[i].total_amount)+`</td>
                                              <td>`+currency_format_display(res.data[i].total_subtract)+`</td>
                                              <td>`+currency_format_display(res.data[i].total_amount - res.data[i].total_subtract)+`</td>
                                              <td>`+currency_format_display(res.data[i].total_sell)+`</td>
                                              <td>`+are.toFixed(4)+`</td>
                                              <td>`+res.data[i].tac+`</td>
                                              <td>`+res.data[i].isik+`</td>
                                              <td>`+res.data[i].box_name+`</td>
                                              
                                          </tr>
                                          `;
                        // total_inv = +total_inv + +(res.data[i].final_price);

                      }

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

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


    });

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

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

        var attach = "";

        $("#table1").attr('style','');
        


        $.ajax({
                url: "models/crul_archive.php",
                type:"POST",
                dataType : "JSON",
                data: {"type":"expenses_report",
                        d1 : d1,
                        d2 : d2,
                        box_id_fk : box_id_fk,
                        report_type : report_type

                      } ,
                complete:function(){
                  hideLoader();
                },
                beforeSend:function(){
                  showLoader("الرجاء الانتظار لحين استكمال تجميع معلومات التقرير");

                },
                success: function(res) {
                  if(res.is_success == true){

                    var total_inv = 0;
                    
                    if(report_type == 1){

                      attach = attach + `<thead>
                                          <tr class="bg-blue-300">
                                            <th>ID</th>
                                            <th class="multi_lang">الاسم</th>
                                            <th class="multi_lang">السعر الكلي</th>
                                            <th class="multi_lang">التاريخ</th>
                                            <th class="multi_lang">رقم الوصل</th>
                                            <th class="multi_lang">الملاحظات</th>
                                            <th class="multi_lang">الفئة</th>
                                            <th class="multi_lang"> الصندوق </th>
                                            
                                            
                                          </tr>
                                        </thead>
                                        <tbody id="table_report_safe">`;

                      
                      for(var i=0 ; i <=res.data.length-1;i++){
                        var typ = "";
                        var proper = "";
                        
                        attach = attach + `
                                          <tr>
                                              <td>`+res.data[i].expenses_id+`</td>
                                              <td>`+res.data[i].expenses_name+`</td>
                                              <td>`+currency_format_display(res.data[i].expenses_unit_price)+`</td>
                                              <td>`+res.data[i].expenses_date+`</td>
                                              <td>`+res.data[i].bill_number+`</td>
                                              
                                              <td>`+res.data[i].expenses_note+`</td>
                                              <td>`+res.data[i].group_name+`</td>
                                              <td>`+res.data[i].box_name+`</td>
                                              
                                          </tr>
                                          `;
                        // total_inv = +total_inv + +(res.data[i].final_price);

                      }

                       attach = attach + `
                                          </tbody>
                                          <tfoot>
                                            <tr class="alpha-info">
                                                      <td></td>
                                                      <td class="multi_lang">المجموع</td>
                                                      <td id="total_in">`+currency_format_display(res.data[0].total_amount)+`</td>
                                                      <td class="multi_lang"></td>
                                                      <td id="total_out"></td>
                                                      <td class="multi_lang"></td>
                                                      <td id="total_rem"></td>
                                                      <td class="multi_lang"></td>
                                                      
                                                      
                                                      
                                                  </tr>
                                          </tfoot>
                                           `;

                      $("#table1").html(attach);
                      // $("#total_in").html(currency_format_display(res.data[0].total_amount));
                      // $("#total_out").html(currency_format_display(res.data[0].total_subtract));
                      // $("#total_rem").html(currency_format_display(res.data[0].total_amount - res.data[0].total_subtract));
                      // $("#total_sell").html(currency_format_display(res.data[0].total_sell));

                    }else{

                      attach = attach + `<thead>
                                            <tr class="bg-blue-300">
                                              <th>ID</th>
                                              <th class="multi_lang">المصاريف الكلية</th>
                                              <th class="multi_lang">الصندوق</th>
                                             
                                              
                                            </tr>
                                          </thead>
                                          <tbody id="table_report_safe_sum">`;
                      
                      for(var i=0 ; i <=res.data.length-1;i++){
                        

                        attach = attach + `
                                          <tr>
                                              <td>1</td>
                                              <td>`+currency_format_display(res.data[i].final_price)+`</td>
                                             
                                              <td>`+res.data[i].box_name+`</td>
                                              
                                          </tr>
                                          `;
                        // total_inv = +total_inv + +(res.data[i].final_price);

                      }

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

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


    });


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

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

        var attach = "";

        $("#table1").attr('style','');
        


        $.ajax({
                url: "models/crul_archive.php",
                type:"POST",
                dataType : "JSON",
                data: {"type":"assets_report",
                        d1 : d1,
                        d2 : d2,
                        box_id_fk : box_id_fk,
                        report_type : report_type

                      } ,
                complete:function(){
                  hideLoader();
                },
                beforeSend:function(){
                  showLoader("الرجاء الانتظار لحين استكمال تجميع معلومات التقرير");

                },
                success: function(res) {
                  if(res.is_success == true){

                    var total_inv = 0;
                    
                    if(report_type == 1){

                      attach = attach + `<thead>
                                          <tr class="bg-blue-300">
                                            <th>ID</th>
                                            <th class="multi_lang">الاسم</th>
                                            <th class="multi_lang">السعر الكلي</th>
                                            <th class="multi_lang">التاريخ</th>
                                            <th class="multi_lang">رقم الوصل</th>
                                            <th class="multi_lang">الملاحظات</th>
                                            <th class="multi_lang"> الصندوق </th>
                                            
                                            
                                          </tr>
                                        </thead>
                                        <tbody id="table_report_safe">`;

                      
                      for(var i=0 ; i <=res.data.length-1;i++){
                        var typ = "";
                        var proper = "";
                        
                        attach = attach + `
                                          <tr>
                                              <td>`+res.data[i].asset_id+`</td>
                                              <td>`+res.data[i].expenses_name+`</td>
                                              <td>`+currency_format_display(res.data[i].expenses_unit_price)+`</td>
                                              <td>`+res.data[i].expenses_date+`</td>
                                              <td>`+res.data[i].bill_number+`</td>
                                              
                                              <td>`+res.data[i].expenses_note+`</td>
                                              <td>`+res.data[i].box_name+`</td>
                                              
                                          </tr>
                                          `;
                        // total_inv = +total_inv + +(res.data[i].final_price);

                      }

                       attach = attach + `
                                          </tbody>
                                          <tfoot>
                                            <tr class="alpha-info">
                                                      <td></td>
                                                      <td class="multi_lang">المجموع</td>
                                                      <td id="total_in">`+currency_format_display(res.data[0].total_amount)+`</td>
                                                      <td class="multi_lang"></td>
                                                      <td id="total_out"></td>
                                                      <td class="multi_lang"></td>
                                                      <td class="multi_lang"></td>
                                                      
                                                      
                                                      
                                                  </tr>
                                          </tfoot>
                                           `;

                      $("#table1").html(attach);
                    
                    }else{

                      attach = attach + `<thead>
                                            <tr class="bg-blue-300">
                                              <th>ID</th>
                                              <th class="multi_lang">سعر المودودات  الكلية</th>
                                              <th class="multi_lang">الصندوق</th>
                                             
                                              
                                            </tr>
                                          </thead>
                                          <tbody id="table_report_safe_sum">`;
                      
                      for(var i=0 ; i <=res.data.length-1;i++){
                        

                        attach = attach + `
                                          <tr>
                                              <td>1</td>
                                              <td>`+currency_format_display(res.data[i].final_price)+`</td>
                                             
                                              <td>`+res.data[i].box_name+`</td>
                                              
                                          </tr>
                                          `;
                        // total_inv = +total_inv + +(res.data[i].final_price);

                      }

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

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


    });

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

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

        var attach = "";

        $("#table1").attr('style','');
        


        $.ajax({
                url: "models/crul_archive.php",
                type:"POST",
                dataType : "JSON",
                data: {"type":"capital_report",
                        d1 : d1,
                        d2 : d2,
                        box_id_fk : box_id_fk,
                        report_type : report_type

                      } ,
                complete:function(){
                  hideLoader();
                },
                beforeSend:function(){
                  showLoader("الرجاء الانتظار لحين استكمال تجميع معلومات التقرير");

                },
                success: function(res) {
                  if(res.is_success == true){

                    var total_inv = 0;
                    
                    if(report_type == 1){

                      attach = attach + `<thead>
                                          <tr class="bg-blue-300">
                                            <th>ID</th>
                                            <th class="multi_lang">رقم الوصل</th>
                                            <th class="multi_lang">المبلغ المضاف</th>
                                            <th class="multi_lang">التاريخ</th>
                                            <th class="multi_lang">مالك المبلغ/ المحول</th>
                                            <th class="multi_lang">Transaction</th>
                                            <th class="multi_lang"> الملاحظات </th>
                                            
                                            
                                          </tr>
                                        </thead>
                                        <tbody id="table_report_safe">`;

                      
                      for(var i=0 ; i <=res.data.length-1;i++){
                        var typ = "";
                        var proper = "";
                        
                        attach = attach + `
                                          <tr>
                                              <td>`+res.data[i].capital_id+`</td>
                                              <td>`+res.data[i].capital_number+`</td>
                                              <td>`+currency_format_display(res.data[i].capital_amount)+`</td>
                                              <td>`+res.data[i].date_of_add+`</td>
                                              <td>`+res.data[i].full_name+`</td>
                                              
                                              <td>`+res.data[i].capital_transaction+`</td>
                                              <td>`+res.data[i].capital_note+`</td>
                                              
                                          </tr>
                                          `;
                        // total_inv = +total_inv + +(res.data[i].final_price);

                      }

                       attach = attach + `
                                          </tbody>
                                          <tfoot>
                                            <tr class="alpha-info">
                                                      <td></td>
                                                      <td class="multi_lang">المبالغ المضافة</td>
                                                      <td id="total_in">`+currency_format_display(res.data[0].total_amount)+`</td>
                                                      <td class="multi_lang"></td>
                                                      <td id="total_out">المبالغ المحولة الى المصنع</td>
                                                      <td class="multi_lang">`+currency_format_display(res.data[0].total_transfered)+`</td>
                                                      <td class="multi_lang"></td>
                                                      
                                                      
                                                      
                                                  </tr>
                                          </tfoot>
                                           `;

                      $("#table1").html(attach);
                    
                    }else{

                      attach = attach + `<thead>
                                            <tr class="bg-blue-300">
                                              <th>ID</th>
                                              <th class="multi_lang">المبلغ الكلي المضاف </th>
                                              <th class="multi_lang">المبلغ الكلي المحول الى المصنع</th>
                                             
                                              
                                            </tr>
                                          </thead>
                                          <tbody id="table_report_safe_sum">`;
                      
                      for(var i=0 ; i <=res.data.length-1;i++){
                        

                        attach = attach + `
                                          <tr>
                                              <td>1</td>
                                              <td>`+currency_format_display(res.data[i].total_amount)+`</td>
                                             
                                              <td>`+currency_format_display(res.data[i].total_transfered)+`</td>
                                              
                                          </tr>
                                          `;
                        // total_inv = +total_inv + +(res.data[i].final_price);

                      }

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

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


    });

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

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

        var attach = "";

        $("#table1").attr('style','');
        


        $.ajax({
                url: "models/crul_archive.php",
                type:"POST",
                dataType : "JSON",
                data: {"type":"capital_factory_report",
                        d1 : d1,
                        d2 : d2,
                        box_id_fk : box_id_fk,
                        report_type : report_type

                      } ,
                complete:function(){
                  hideLoader();
                },
                beforeSend:function(){
                  showLoader("الرجاء الانتظار لحين استكمال تجميع معلومات التقرير");

                },
                success: function(res) {
                  if(res.is_success == true){

                    var total_inv = 0;
                    
                    if(report_type == 1){

                      attach = attach + `<thead>
                                          <tr class="bg-blue-300">
                                            <th>ID</th>
                                            <th class="multi_lang">رقم الوصل</th>
                                            <th class="multi_lang">المبلغ المضاف</th>
                                            <th class="multi_lang">التاريخ</th>
                                            <th class="multi_lang">مالك المبلغ/ المحول</th>
                                            <th class="multi_lang">Transaction</th>
                                            <th class="multi_lang"> الملاحظات </th>
                                            
                                            
                                          </tr>
                                        </thead>
                                        <tbody id="table_report_safe">`;

                      
                      for(var i=0 ; i <=res.data.length-1;i++){
                        var typ = "";
                        var proper = "";
                        
                        attach = attach + `
                                          <tr>
                                              <td>`+res.data[i].capital_id+`</td>
                                              <td>`+res.data[i].capital_number+`</td>
                                              <td>`+currency_format_display(res.data[i].capital_amount)+`</td>
                                              <td>`+res.data[i].date_of_add+`</td>
                                              <td>`+res.data[i].full_name+`</td>
                                              
                                              <td>`+res.data[i].capital_transaction+`</td>
                                              <td>`+res.data[i].capital_note+`</td>
                                              
                                          </tr>
                                          `;
                        // total_inv = +total_inv + +(res.data[i].final_price);

                      }

                       attach = attach + `
                                          </tbody>
                                          <tfoot>
                                            <tr class="alpha-info">
                                                      <td></td>
                                                      <td class="multi_lang">المبلغ الكلي</td>
                                                      <td id="total_in">`+currency_format_display(res.data[0].total_amount)+`</td>
                                                      <td class="multi_lang">المبلغ المستلم</td>
                                                      <td id="total_out">`+currency_format_display(res.data[0].total_approved)+`</td>
                                                      <td class="multi_lang">المبلغ غير المؤكد</td>
                                                      <td class="multi_lang">`+currency_format_display(res.data[0].total_not_approved)+`</td>
                                                      
                                                      
                                                      
                                                  </tr>
                                          </tfoot>
                                           `;

                      $("#table1").html(attach);
                    
                    }else{

                      attach = attach + `<thead>
                                            <tr class="bg-blue-300">
                                              <th>المبلغ الكلي</th>
                                              <th class="multi_lang">المبلغ المستلم</th>
                                              <th class="multi_lang">المبلغ غير المؤكد</th>
                                             
                                              
                                            </tr>
                                          </thead>
                                          <tbody id="table_report_safe_sum">`;
                      
                      for(var i=0 ; i <=res.data.length-1;i++){
                        

                        attach = attach + `
                                          <tr>
                                              <td>`+currency_format_display(res.data[i].total_amount)+`</td>
                                              <td>`+currency_format_display(res.data[i].total_approved)+`</td>
                                             
                                              <td>`+currency_format_display(res.data[i].total_not_approved)+`</td>
                                              
                                          </tr>
                                          `;
                        // total_inv = +total_inv + +(res.data[i].final_price);

                      }

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

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


    });


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

        var d1            = $("#first_date").val();
        var d2            = $("#second_date").val();
        // var box_id_fk     = $("#box_id_fk").val();
        var report_type   = $("#report_type").val();
        
       

        var attach = "";

        $("#table1").attr('style','');
        


        $.ajax({
                url: "models/crul_archive.php",
                type:"POST",
                dataType : "JSON",
                data: {"type":"withdraw_report",
                        d1 : d1,
                        d2 : d2,
                        report_type : report_type

                      } ,
                complete:function(){
                  hideLoader();
                },
                beforeSend:function(){
                  showLoader("الرجاء الانتظار لحين استكمال تجميع معلومات التقرير");

                },
                success: function(res) {
                  if(res.is_success == true){

                    var total_inv = 0;
                    
                    if(report_type == 1){

                      attach = attach + `<thead>
                                          <tr class="bg-blue-300">
                                            <th>ID</th>
                                            <th class="multi_lang">مبلغ القرض</th>
                                            <th class="multi_lang">التاريخ</th>
                                            <th class="multi_lang">الدائن</th>
                                            <th class="multi_lang">المدين</th>
                                            <th class="multi_lang"> الملاحظات </th>
                                            
                                            
                                          </tr>
                                        </thead>
                                        <tbody id="table_report_safe">`;

                      
                      for(var i=0 ; i <=res.data.length-1;i++){
                        var typ = "";
                        var proper = "";
                        
                        attach = attach + `
                                          <tr>
                                              <td>`+res.data[i].capital_withdraw_id+`</td>
                                              <td>`+currency_format_display(res.data[i].withdraw_amount)+`</td>
                                              <td>`+res.data[i].withdraw_date+`</td>
                                              <td>`+res.data[i].full_name+`</td>
                                              
                                              <td>`+res.data[i].customer_full_name+`</td>
                                              <td>`+res.data[i].withdraw_note+`</td>
                                              
                                          </tr>
                                          `;
                        // total_inv = +total_inv + +(res.data[i].final_price);

                      }

                       attach = attach + `
                                          </tbody>
                                          <tfoot>
                                            <tr class="alpha-info">
                                                      <td class="multi_lang">مبلغ القروض الكلي</td>
                                                      <td id="total_in">`+currency_format_display(res.data[0].total_amount)+`</td>
                                                      <td class="multi_lang"></td>
                                                      <td id="total_out"></td>
                                                      <td class="multi_lang"></td>
                                                      <td class="multi_lang"></td>
                                                      
                                                      
                                                      
                                                  </tr>
                                          </tfoot>
                                           `;

                      $("#table1").html(attach);
                    
                    }else{

                      attach = attach + `<thead>
                                            <tr class="bg-blue-300">
                                              <th>1</th>
                                              <th class="multi_lang">مبلغ القروض الكلي</th>
                                              <th class="multi_lang"></th>
                                             
                                              
                                            </tr>
                                          </thead>
                                          <tbody id="table_report_safe_sum">`;
                      
                      for(var i=0 ; i <=res.data.length-1;i++){
                        

                        attach = attach + `
                                          <tr>
                                              <td>1</td>
                                              <td>`+currency_format_display(res.data[i].total_amount)+`</td>
                                             
                                              <td></td>
                                              
                                          </tr>
                                          `;
                        // total_inv = +total_inv + +(res.data[i].final_price);

                      }

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

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


    });

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

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

        var attach = "";

        $("#table1").attr('style','');
        


        $.ajax({
                url: "models/crul_archive.php",
                type:"POST",
                dataType : "JSON",
                data: {"type":"payment_report",
                        d1 : d1,
                        d2 : d2,
                        customer_id_fk : customer_id_fk,
                        report_type : report_type

                      } ,
                complete:function(){
                  hideLoader();
                },
                beforeSend:function(){
                  showLoader("الرجاء الانتظار لحين استكمال تجميع معلومات التقرير");

                },
                success: function(res) {
                  if(res.is_success == true){

                    var total_inv = 0;
                    
                    if(report_type == 1){

                      attach = attach + `<thead>
                                          <tr class="bg-blue-300">
                                            <th>ID</th>
                                            <th class="multi_lang">اسم الزبون</th>
                                            <th class="multi_lang">رقم الدفعة</th>
                                            <th class="multi_lang">المبلغ</th>
                                            <th class="multi_lang">التاريخ</th>
                                            <th class="multi_lang">Transaction</th>
                                            <th class="multi_lang"> الملاحظات </th>
                                            
                                            
                                          </tr>
                                        </thead>
                                        <tbody id="table_report_safe">`;

                      
                      for(var i=0 ; i <=res.data.length-1;i++){
                        var typ = "";
                        var proper = "";
                        
                        attach = attach + `
                                          <tr>
                                              <td>`+res.data[i].payment_id+`</td>
                                              <td>`+res.data[i].customer_full_name+`</td>
                                              <td>`+res.data[i].payment_number+`</td>
                                              <td>`+currency_format_display(res.data[i].paid_amount)+`</td>
                                              <td>`+res.data[i].paid_date+`</td>
                                              
                                              <td>`+res.data[i].payment_transaction+`</td>
                                              <td>`+res.data[i].payment_note+`</td>
                                              
                                          </tr>
                                          `;
                        // total_inv = +total_inv + +(res.data[i].final_price);

                      }

                       attach = attach + `
                                          </tbody>
                                          <tfoot>
                                            <tr class="alpha-info">
                                                      <td></td>
                                                      <td class="multi_lang">مجموع الدفوعات الكلي</td>
                                                      <td id="total_in">`+currency_format_display(res.data[0].total_paid)+`</td>
                                                      <td class="multi_lang">مجموع الخصومات على الحساب</td>
                                                      <td id="total_in">`+currency_format_display(res.data[0].total_account_discount)+`</td>
                                                      <td class="multi_lang">مجموع المبالغ المسترجعة</td>
                                                      <td id="total_in">`+currency_format_display(res.data[0].total_returned)+`</td>
                                                      
                                                      
                                                      
                                                      
                                                  </tr>
                                          </tfoot>
                                           `;

                      $("#table1").html(attach);
                    
                    }else{

                      attach = attach + `<thead>
                                            <tr class="bg-blue-300">
                                              <th>#</th>
                                              <th class="multi_lang">مجموع الدفوعات الكلية</th>
                                              <th class="multi_lang">مجموع الخصومات على الحساب الكلية</th>
                                              <th class="multi_lang">مجموع المبالغ المسترجعة</th>
                                             
                                              
                                            </tr>
                                          </thead>
                                          <tbody id="table_report_safe_sum">`;
                      
                      for(var i=0 ; i <=res.data.length-1;i++){
                        

                        attach = attach + `
                                          <tr>
                                              <td>1</td>
                                              <td>`+currency_format_display(res.data[i].total_paid)+`</td>
                                              <td>`+currency_format_display(res.data[i].total_account_discount)+`</td>
                                              <td>`+currency_format_display(res.data[i].total_returned)+`</td>
                                             
                                             
                                              
                                          </tr>
                                          `;
                        // total_inv = +total_inv + +(res.data[i].final_price);

                      }

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

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


    });


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

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

        var attach = "";

        $("#table1").attr('style','');
        


        $.ajax({
                url: "models/crul_archive.php",
                type:"POST",
                dataType : "JSON",
                data: {"type":"purchase_report",
                        d1 : d1,
                        d2 : d2,
                        customer_id_fk : customer_id_fk,
                        report_type : report_type

                      } ,
                complete:function(){
                  hideLoader();
                },
                beforeSend:function(){
                  showLoader("الرجاء الانتظار لحين استكمال تجميع معلومات التقرير");

                },
                success: function(res) {
                  if(res.is_success == true){

                    var total_rol = 0;
                    var total_dmg = 0;
                    
                    if(report_type == 1){

                      attach = attach + `<thead>
                                          <tr class="bg-blue-300">
                                            <th>ID</th>
                                            <th class="multi_lang">اسم المخزن</th>
                                            <th class="multi_lang">نوع المخزن</th>
                                            <th class="multi_lang">الرمز المحلي</th>
                                            <th class="multi_lang">الرمز الاجنبي</th>
                                            <th class="multi_lang">Barcode</th>
                                            <th class="multi_lang"> طول المادة (متر) </th>
                                            <th class="multi_lang"> التالف (متر) </th>
                                            <th class="multi_lang">الحالة</th>
                                            
                                            
                                          </tr>
                                        </thead>
                                        <tbody id="table_report_safe">`;

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

                        if (res.data[i].stock_type ==0) {
                          typ = "خزن";
                        }else{
                          typ = "انتاج";
                        }

                        if (res.data[i].item_status ==1) {
                          proper = "مباع بالكامل";
                        }
                        
                        total_rol = +total_rol + +res.data[i].item_length;
                        total_dmg = +total_dmg + +res.data[i].damage_length;

                        attach = attach + `
                                          <tr>
                                              <td>`+res.data[i].purchase_item_detail_id+`</td>
                                              <td>`+res.data[i].stock_name+`</td>
                                              <td>`+typ+`</td>
                                              <td>`+res.data[i].erbil_code+`</td>
                                              <td>`+res.data[i].turkish_code+`</td>
                                              <td>`+res.data[i].barcode+`</td>
                                              
                                              <td>`+res.data[i].item_length+`</td>
                                              <td>`+res.data[i].damage_length+`</td>
                                              <td>`+proper+`</td>
                                              
                                          </tr>
                                          `;
                        // total_inv = +total_inv + +(res.data[i].final_price);

                      }

                       attach = attach + `
                                          </tbody>
                                          <tfoot>
                                            <tr class="alpha-info">
                                                      <td></td>
                                                      <td class="multi_lang">عدد الطوب</td>
                                                      <td id="total_in">${res.data.length}</td>
                                                      <td class="multi_lang"></td>
                                                      <td id="total_in"></td>
                                                      <td class="multi_lang"></td>
                                                      <td id="total_in">${total_rol}</td>
                                                      <td id="total_in">${total_dmg}</td>
                                                      <td id="total_in"></td>
                                                      
                                                      
                                                      
                                                      
                                                  </tr>
                                          </tfoot>
                                           `;

                      $("#table1").html(attach);
                    
                    }else{

                      attach = attach + `<thead>
                                            <tr class="bg-blue-300">
                                              <th>#</th>
                                              <th class="multi_lang">مجموع المشتريات الكلي</th>
                                              
                                              
                                            </tr>
                                          </thead>
                                          <tbody id="table_report_safe_sum">`;
                      
                      for(var i=0 ; i <=res.data.length-1;i++){
                        

                        attach = attach + `
                                          <tr>
                                              <td>1</td>
                                              <td>`+currency_format_display(res.data[i].total_price)+`</td>
                                              
                                             
                                              
                                          </tr>
                                          `;
                        // total_inv = +total_inv + +(res.data[i].final_price);

                      }

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

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


    });

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

        var d1            = $("#first_date").val();
        var d2            = $("#second_date").val();
        var box_id_fk     = $("#box_id_fk").val();
        var report_type   = $("#report_type").val();
        var customer_id_fk = $("#customer_id_fk").val();
       

        var attach = "";

        $("#table1").attr('style','');
        


        $.ajax({
                url: "models/crul_archive.php",
                type:"POST",
                dataType : "JSON",
                data: {"type":"supplier_report",
                        d1 : d1,
                        d2 : d2,
                        box_id_fk : box_id_fk,
                        customer_id_fk : customer_id_fk,
                        report_type : report_type

                      } ,
                complete:function(){
                  hideLoader();
                },
                beforeSend:function(){
                  showLoader("الرجاء الانتظار لحين استكمال تجميع معلومات التقرير");

                },
                success: function(res) {
                  if(res.is_success == true){

                    var total_inv = 0;
                    
                    if(report_type == 1){

                      attach = attach + `<thead>
                                          <tr class="bg-blue-300">
                                            <th>ID</th>
                                            <th class="multi_lang">اسم المورد</th>
                                            <th class="multi_lang">رقم الدفعة</th>
                                            <th class="multi_lang">المبلغ</th>
                                            <th class="multi_lang">التاريخ</th>
                                            <th class="multi_lang">Transaction</th>
                                            <th class="multi_lang"> الملاحظات </th>
                                            <th class="multi_lang"> رقم قائمة المشتريات </th>
                                            
                                            
                                          </tr>
                                        </thead>
                                        <tbody id="table_report_safe">`;

                      
                      for(var i=0 ; i <=res.data.length-1;i++){
                        var typ = "";
                        var proper = "";
                        
                        attach = attach + `
                                          <tr>
                                              <td>`+res.data[i].payment_id+`</td>
                                              <td>`+res.data[i].supplier_full_name+`</td>
                                              <td>`+res.data[i].payment_number+`</td>
                                              <td>`+currency_format_display(res.data[i].paid_amount)+`</td>
                                              <td>`+res.data[i].paid_date+`</td>
                                              <td>`+res.data[i].payment_transaction+`</td>
                                              
                                              <td>`+res.data[i].payment_note+`</td>
                                              <td>`+res.data[i].purchase_auto_number+`</td>
                                              
                                          </tr>
                                          `;
                        // total_inv = +total_inv + +(res.data[i].final_price);

                      }

                       attach = attach + `
                                          </tbody>
                                          <tfoot>
                                            <tr class="alpha-info">
                                                      <td></td>
                                                      <td class="multi_lang">الدفوعات الكلية</td>
                                                      <td id="total_in">`+currency_format_display(res.data[0].total_paid)+`</td>
                                                      <td class="multi_lang"></td>
                                                      <td id="total_in"></td>
                                                      <td class="multi_lang"></td>
                                                      <td id="total_in"></td>
                                                      <td id="total_in"></td>
                                                      
                                                      
                                                      
                                                      
                                                  </tr>
                                          </tfoot>
                                           `;

                      $("#table1").html(attach);
                    
                    }else{

                      attach = attach + `<thead>
                                            <tr class="bg-blue-300">
                                              <th>#</th>
                                              <th class="multi_lang">مجموع دفوعات الموردين</th>
                                              
                                              
                                            </tr>
                                          </thead>
                                          <tbody id="table_report_safe_sum">`;
                      
                      for(var i=0 ; i <=res.data.length-1;i++){
                        

                        attach = attach + `
                                          <tr>
                                              <td>1</td>
                                              <td>`+currency_format_display(res.data[i].total_paid)+`</td>
                                              
                                             
                                              
                                          </tr>
                                          `;
                        // total_inv = +total_inv + +(res.data[i].final_price);

                      }

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

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


    });

});





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

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

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


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

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

  




    // end highlight row and column


    // Single row selection
    
  


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


});



