$(document).ready(function(){    

   $("#invoice_pidetails").hide();

   $('#pi_selectinvoice').select2({
      ajax: {
        url: url_getpiforinvoice,
        type: "get",
        dataType: 'json',
        delay: 100,
        data: function (params) {
           return {
              searchTerm: params.term // search term
           };
        },
        processResults: function (response) {
           return {
              results: response
                   
           };		   
		 },
        cache: true
      }
 });
 
 $('#pi_selectinvoice').on('select2:select', function (e) {
   var pi_id = e.params.data.id;
   var option="";
    
   $.ajax(url_getpidetailforinvoice,{
      type : 'get',
      data :{'pi_id' : pi_id} ,
      success : function(data){

         $("#invoicetablebody").empty();
         $("#invoicetablefoot").empty();

         piaddresses(data);
         pidetails(data);
         bankdetailsforinvoice(data.bank);
         $(document).find('#invoice_piquotationno').val(data.quotation.quotation_no);
         $(document).find('#invoice_picompanycode').val(data.company.company_code);
         $(document).find('#invoice_picompanyname').val(data.company.name);
         $(document).find('#invoice_pigstno').val(data.company.gstin);
         
         $('#invoice_pidetails').show();
         
      },
      error: function (jqXhr, textStatus, errorMessage) {
         console.log('Error' + errorMessage);
      }
   });
});

});

   function piaddresses(data)
   {
      $('#invoice_crepino').val(data.data.pi_no);
      $('#invoice_crequotationno').val(data.quotation.quotation_no);
      $('#invoice_crecompanycode').val(data.company.company_code);
      $('#invoice_crecompanyname').val(data.company.name);

      $('#invoice_quotationno').val(data.quotation.quotation_no);
      $('#invoice_companyid').val(data.quotation.company_id);
 
      $('#invoice_crebilladd1').val(data.billing.address_line1);
      $('#invoice_crebilladd2').val(data.billing.address_line2);
      $('#invoice_crebilladd3').val(data.billing.address_line3);
      $('#invoice_crebillcity').val(data.billing.name);
      $('#invoice_crebillstate').val(data.billing.state_title);
      $('#invoice_crebillcountry').val(data.billing.country);
      $('#invoice_crebillpincode').val(data.billing.pincode);
      $('#invoice_crebillmobile').val(data.billing.mobile);

      $('#invoice_creshipadd1').val(data.shipping.address_line1);
      $('#invoice_creshipadd2').val(data.shipping.address_line2);
      $('#invoice_creshipadd3').val(data.shipping.address_line3);
      $('#invoice_creshipcity').val(data.shipping.name);
      $('#invoice_creshipstate').val(data.shipping.state_title);
      $('#invoice_creshipcountry').val(data.shipping.country);
      $('#invoice_creshippincode').val(data.shipping.pincode);
      $('#invoice_creshipmobile').val(data.shipping.mobile);
      $('#invoice_creshipperson').val(data.shipping.person_name);

      $('#invoice_crenotetext').val(data.quotation.note);
      $('#invoice_creponumber').val(data.data.po_number);
      $('#invoice_crepaymentterm').val(data.data.paymnet_term);

      // CKEDITOR.instances.editor1.setData( '<p>This is the editor data.</p>' );

      CKEDITOR.instances['invoice_crenotetext'].setData(data.quotation.note);

      $('#invoice_crebankname').val();
      $('#invoice_crebankifsc').val();
      $('#invoice_crebankbranch').val();
      $('#invoice_crebankbranchcode').val();
   }

   function pidetails(data)
   {
      var total=0;
      var subttl=0;
      var gstttl=0;
      var grndttl=0;
      var text="";

      var tax25=0;
      var tax6=0;
      var tax9=0;
      var tax14=0;

      var tax5=0;
      var tax12=0;
      var tax18=0;
      var tax28=0;

      var i_show=0;
      var cs_show=0;

      var countofproduct=(data.product).length;
      var count=1;

      for(var i=0;i<countofproduct;i++)
         {		
            var section=""	;
            if(data.product[i]['c_s_gst']=='null')
            {
                  var test1=(data.product[i]['i_gst']).replace(/[^a-z0-9\s]/gi, '');
                  $('#invoice_tblcs').hide();
                  i_show=1;
                  
                  total=(data.product[i]['unit_cost']*data.product[i]['quantity']);
                  gst=(((data.product[i]['unit_cost']/100)*(data.product[i]['i_gst'].replace(/[\*\^\'\!\%\+\.]/g, '')))*data.product[i]['quantity']);
                  subttl=subttl+total;
                  gstttl=gstttl+gst;
                  

                  section=section+'<tr>'+'<td>'+count+'</td>'+'<td class="preview_item" style="max-width: 40ch !important; word-break: break-word !important; hyphens: auto !important;" data-id="'+data.product[i]['item_id']+'">'+data.product[i]['name']+'</td>'+'<td class="preview_HSN">'+data.product[i]['HSN']+'</td>'+'<td class="preview_unitcost">₹'+data.product[i]['unit_cost']+'</td>'+'<td class="preview_quantity">'+data.product[i]['quantity']+'</td>'+'<td id="finalthcs" class="preview_csgst" style="display:none;">'+data.product[i]['c_s_gst']+'</td>'+'<td id="finalthi" class="preview_igst">'+data.product[i]['i_gst']+'</td>'+'<td class="preview_rowtotal amountalign">₹'+total+'</td>'+'</tr>';

                  if(test1==5)
                  {
                  tax5=tax5+gst;
                  }
                  if(test1==12)
                  {
                  tax12=tax12+gst;
                  }
                  if(test1==18)
                  {
                  tax18=tax18+gst;
                  }
                  if(test1==28)
                  {
                  tax28=tax28+gst;
                  }

            }
            else if(data.product[i]['i_gst']=='null')
            {
                  var test=(data.product[i]['c_s_gst']).replace(/[\*\^\'\!\%\+\.]/g, '').split(' ')[0];
                  $('#invoice_tbli').hide(); 
                  cs_show=1;

                  total=(data.product[i]['unit_cost']*data.product[i]['quantity']);
                  gst=((((data.product[i]['unit_cost']/100)*((data.product[i]['c_s_gst']).replace(/[\*\^\'\!\%\+\.]/g, '').split(' ')[0]))*data.product[i]['quantity'])*2);
                  subttl=subttl+total;
                  gstttl=gstttl+gst;
                  
                  section=section+'<tr>'+'<td>'+count+'</td>'+'<td class="preview_item" style="max-width: 40ch !important; word-break: break-word !important; hyphens: auto !important;" data-id="'+data.product[i]['item_id']+'">'+data.product[i]['name']+'</td>'+'<td class="preview_HSN">'+data.product[i]['HSN']+'</td>'+'<td class="preview_unitcos">₹'+data.product[i]['unit_cost']+'</td>'+'<td class="preview_quantity">'+data.product[i]['quantity']+'</td>'+'<td id="finalthcs" class="preview_csgst">'+data.product[i]['c_s_gst']+'</td>'+'<td id="finalthi" class="preview_igst" style="display:none;">'+data.product[i]['i_gst']+'</td>'+'<td class="preview_rowtotal amountalign">₹'+total+'</td>'+'</tr>';

                  if(test==25)
                  {
                  tax25=tax25+gst;
                  }
                  if(test==6)
                  {
                  tax6=tax6+gst;
                  }
                  if(test==9)
                  {
                  tax9=tax9+gst;
                  }
                  if(test==14)
                  {
                  tax14=tax14+gst; 
                  }

            }
            $("#invoicetablebody").append(section);
         count++;
         }
         grndttl=subttl+gstttl;

         if(i_show==1)
         {
            text=text+
            
            '<tr>'+
               '<td colspan="4"></td>'+
               '<td></td>'+
               '<td><h4>Sub Total</h4></td>'+
               '<td class="amountalign">₹'+((subttl).toFixed(0))+'</td>'+
            '</tr>'+
            '<tr>'+
               '<td><h4>GST(5%)</h4></td>'+
               '<td class="amountalign">₹'+((tax5).toFixed(0))+'</td>'+
               '<td><h4>GST(18%)</h4></td>'+
               '<td class="amountalign">₹'+((tax18).toFixed(0))+'</td>'+
               '<td></td>'+
               '<td><h4>GST Total</h4></td>'+
               '<td class="amountalign">₹'+((gstttl).toFixed(0))+'</td>'+
            '</tr>'+
            '<tr>'+
               '<td><h4>GST(12%)</h4></td>'+
               '<td class="amountalign">₹'+((tax12).toFixed(0))+'</td>'+
               '<td><h4>GST(28%)</h4></td>'+
               '<td class="amountalign">₹'+((tax28).toFixed(0))+'</td>'+
               '<td></td>'+
               '<td><h4>Grand Total</h4></td>'+
               '<td class="amountalign">₹'+((grndttl).toFixed(0))+'</td>'+
            '</tr>'
         
            $("#invoicetablefoot").append(text);
         }
         else if(cs_show==1)
         {
            text=text+
            
            '<tr>'+
               '<td colspan="4"></td>'+
               '<td></td>'+
               '<td><h4>Sub Total</h4></td>'+
               '<td class="amountalign">₹'+((subttl).toFixed(0))+'</td>'+
            '</tr>'+
            '<tr>'+
               '<td><h4>GST(2.5%+2.5%)</h4></td>'+
               '<td class="amountalign">₹'+((tax25).toFixed(0))+'</td>'+
               '<td><h4>GST(9%+9%)</h4></td>'+
               '<td class="amountalign">₹'+((tax9).toFixed(0))+'</td>'+
               '<td></td>'+
               '<td><h4>GST Total</h4></td>'+
               '<td class="amountalign">₹'+((gstttl).toFixed(0))+'</td>'+
            '</tr>'+
            '<tr>'+
               '<td><h4>GST(6%+6%)</h4></td>'+
               '<td class="amountalign">₹'+((tax6).toFixed(0))+'</td>'+
               '<td><h4>GST(14%+14%)</h4></td>'+
               '<td class="amountalign">₹'+((tax14).toFixed(0))+'</td>'+
               '<td></td>'+
               '<td><h4>Grand Total</h4></td>'+
               '<td class="amountalign">₹'+((grndttl).toFixed(0))+'</td>'+
            '</tr>'
         
            $("#invoicetablefoot").append(text);
            

         }
   }

  function bankdetailsforinvoice(data)
  {
   $(document).find('#invoice_crebankname').val(data.bank_name);
   $(document).find('#invoice_crebankbranch').val(data.bank_branch);
   $(document).find('#invoice_crebankbranchcode').val(data.bank_ifsc);
   $(document).find('#invoice_crebankifsc').val(data.ac_no);
  }   
 
$(document).ready(function(){
 
  $('#inv_without_pi_bank_select').select2({
	  
 ajax: {
        url: bank_select,
        type: "get",
		 dataType: 'json',
		data: function (params) {
           return {
              searchTerm: params.term // search term
           };
        },
        processResults: function (response) {
			console.log(response.bank);
           return {
              results: response
           };		   
		 },
        cache: true
 }
      
 });
});