<?php 
$nwords = array(  "", "one", "two", "three", "four", "five", "six", 
            "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", 
            "fourteen", "fifteen", "sixteen", "seventeen", "eightteen", 
          "nineteen", "twenty", 30 => "thirty", 40 => "fourty",
                     50 => "fifty", 60 => "sixty", 70 => "seventy", 80 => "eigthy",
                     90 => "ninety" );
function decimal_to_words($x)
{
  $x = str_replace(',','',$x);
  $pos = strpos((string)$x, ".");
  if ($pos !== false) { $decimalpart= substr($x, $pos+1, 2); $x = substr($x,0,$pos); }
  $tmp_str_rtn = number_to_words ($x);
  if(!empty($decimalpart))
    $tmp_str_rtn .= ' and '  . number_to_words ($decimalpart) . ' paise';
  return   $tmp_str_rtn;
} 

function number_to_words ($x)
{
      $nwords = array(  "", "one", "two", "three", "four", "five", "six", 
            "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", 
            "fourteen", "fifteen", "sixteen", "seventeen", "eightteen", 
          "nineteen", "twenty", 30 => "thirty", 40 => "fourty",
                     50 => "fifty", 60 => "sixty", 70 => "seventy", 80 => "eigthy",
                     90 => "ninety" ); 
           
     if(!is_numeric($x))
     {
         $w = '#';
     }else if(fmod($x, 1) != 0)
     {
         $w = '#';
     }else{
         if($x < 0)
         {
             $w = 'minus ';
             $x = -$x;
         }else{
             $w = '';
         }
         if($x < 21)
         {
             $w .= $nwords[$x];
         }else if($x < 100)
         {
             $w .= $nwords[10 * floor($x/10)];
             $r = fmod($x, 10);
             if($r > 0)
             {
                 $w .= ' '. $nwords[$r];
             }
         } else if($x < 1000)
         {
    
             $w .= $nwords[floor($x/100)] .' hundred';
             $r = fmod($x, 100);
             if($r > 0)
             {
                 $w .= ' '. number_to_words($r);
             }
         } else if($x < 100000)
         {
          $w .= number_to_words(floor($x/1000)) .' thousand';
             $r = fmod($x, 1000);
             if($r > 0)
             {
                 $w .= ' ';
                 if($r < 100)
                 {
                     $w .= ' ';
                 }
                 $w .= number_to_words($r);
             }
         } else {
             $w .= number_to_words(floor($x/100000)) .' lacs';
             $r = fmod($x, 100000);
             if($r > 0)
             {
                 $w .= ' ';
                 if($r < 100)
                 {
                     $word .= ' ';
                 }
                 $w .= number_to_words($r);
             }
         }
     }
     return $w;
} 
// demonstration 

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Print Invoice</title>
  </head>

  <body>
    <div>
      <!-- <button type="button" class="edit_bill" data-bill="<?php //echo !empty($dbtnote_data['devid_note_no']) ? $dbtnote_data['devid_note_no'] : '' ;?>">EDIT</button> --> 
      <button type="button" class="print_bill">PRINT</button>
    </div>
    <?php $state_array = array('-','Jammu & Kashmir','Himachal Pradesh','Punjab','Chandigarh','Uttarakhand','Haryana','Delhi','Rajasthan','Uttar Pradesh','Bihar','Sikkim','Arunachal Pradesh','Nagaland','Manipur','Mizoram','Tripura','Meghalaya','Assam','West Bengal','Jharkhand','Odisha','Chhattisgarh','Madhya Pradesh','Gujarat','Daman & Diu','Dadra & Nagar Haveli','Maharashtra','Karnataka','Goa','Lakshdweep','Kerala','Tamil Nadu','Pondicherry','Andaman & Nicobar Islands','Telengana','Andhra Pradesh','Other Territory');?>
    <div  class="wrap" id="DivIdToPrint">
    <style>
        .wrap{ width:96%; padding:4%;}
        table td{ border:1px solid #09C; font-size:11px;}
        
    </style>
    <table width="100%" border="0" cellspacing="0" cellpadding="0">
      <tr>
        <td height="27" colspan="5" align="center" valign="top" style="font-size:16px;"><h2 style="margin-bottom:5px;"><?php echo !empty($setings->company_name) ? $setings->company_name : '';?></h2>
          <?php echo !empty($setings->company_address) ? $setings->company_address : '';?><br />
          GSTIN: <?php echo !empty($setings->gst_no) ? $setings->gst_no : '';?></td>
        </tr>
      <tr>
        <td colspan="4" align="center" valign="top"><lable style="font-size: 30px;color: #09C;"><strong>DEBIT NOTE</strong> ( BILL : <?php echo $dbtnote_data['bill_no'];?> )</lable></td>
        <td width="30%" align="left" valign="top"><table width="100%" border="0" cellspacing="0" cellpadding="0">
          <tr>
            <td width="19%">&nbsp;</td>
            <td width="81%"><lable style="color: red;">Original for Receipient</lable></td>
          </tr>
          <tr>
            <td>&nbsp;</td>
            <td><lable style="color: red;">Duplicate for Supplier/Transporter</lable></td>
          </tr>
          <tr>
            <td>&nbsp;</td>
            <td><lable style="color: red;">Triplicate for Supplier</lable></td>
          </tr>
        </table></td>
      </tr>
      <tr>
        <td colspan="5" align="left" valign="bottom" style="padding:0">
    <table width="100%" border="0" cellspacing="0" cellpadding="0">
    <tr><td width="50%">
    <table width="100%" border="0" cellspacing="0" cellpadding="0">
          <tr>
            <td width="26%" style="border:none;" >Reverse Charge</td>
            <td width="2%" style="border:none;">:</td>
            <td width="45%" style="border:none;">NO</td>
            <td width="27%" style="border:none;">&nbsp;</td>
            
          </tr>
          <tr>
            <td style="border:none;">Invoice No.</td>
            <td style="border:none;">:</td>
            <td style="border:none;"><?php echo !empty($dbtnote_data['devid_note_no']) ? $dbtnote_data['devid_note_no'] : '' ;?></td>
            <td style="border:none;">&nbsp;</td>
          </tr>
          <tr>
            <td style="border:none;">Invoice Date</td>
            <td style="border:none;">:</td>
            <td style="border:none;"><?php echo !empty($dbtnote_data['devid_date']) ? date('d/m/Y',strtotime(str_replace('-', '/', $dbtnote_data['devid_date']))) : '' ;?></td>
            <td style="border:none;">&nbsp;</td>
          </tr>
          <tr>
            <td style="border:none;"><strong>State</strong></td>
            <td style="border:none;">:</td>
            <td style="border:none;"><?php echo !empty($state_array[19]) ? $state_array[19] : '' ;?></td>
            <td valign="bottom" style="border:none; padding:0"><table width="90%" border="0" cellspacing="0" cellpadding="0">
              <tr>
                <td>State Code</td>
                <td>19</td>
              </tr>
            </table></td>
            
          </tr>
        </table>
    </td><td width="50%">
          <table width="100%" border="0" cellspacing="0" cellpadding="0">
            <tr>
              <td width="38%"  style="border:none;">Transportation Mode</td>
              <td width="3%"  style="border:none;">:</td>
              <td width="59%"  style="border:none;">&nbsp;</td>
            </tr>
            <tr>
              <td style="border:none;">Vehicle Number</td>
              <td style="border:none;">:</td>
              <td style="border:none;">&nbsp;</td>
            </tr>
            <tr>
              <td style="border:none;">Date of Supply</td>
              <td style="border:none;">:</td>
              <td style="border:none;">&nbsp;</td>
            </tr>
            <tr>
              <td style="border:none;"><strong>Place of Supply</strong></td>
              <td style="border:none;">:</td>
              <td style="border:none;">&nbsp;</td>
            </tr>
          </table>
      </td></tr></table>
        </td>
      </tr>
      <tr>
        <td colspan="5" align="left" valign="top">&nbsp;</td>
        </tr>
      <tr>
        <td colspan="5" align="center" valign="top">
    <table width="100%" border="0" cellspacing="0" cellpadding="0">
    <tr><td width="50%"><strong>Details of Receiver | Billed to :</strong></td>
        <td width="50%"><strong>Details of Consignee | Shipped to :</strong></td>
        </tr>
    </table>
    </td></tr>
      <tr>
        <td colspan="5" align="left" valign="bottom" style="padding:0">
    <table width="100%" border="0" cellspacing="0" cellpadding="0">
    <tr><td width="50%">
    <table width="100%" border="0" cellspacing="0" cellpadding="0">
          <tr>
            <td width="26%" style="border:none;" >Name</td>
            <td width="2%" style="border:none;">:</td>
            <td width="45%" style="border:none;"><?php echo !empty($customer_details->name) ? $customer_details->name : '' ;?></td>
            <td width="27%" style="border:none;">&nbsp;</td>
          </tr>
          <tr>
            <td style="border:none;">Address</td>
            <td style="border:none;">:</td>
            <td style="border:none;"><?php echo !empty($customer_details->billing_address) ? $customer_details->billing_address : '' ;?></td>
            <td style="border:none;">&nbsp;</td>
          </tr>
          <tr>
            <td style="border:none;">GSTIN</td>
            <td style="border:none;">:</td>
            <td style="border:none;"><?php echo !empty($customer_details->gstn) ? $customer_details->gstn : '' ;?></td>
            <td style="border:none;">&nbsp;</td>
          </tr>
          <tr>
            <td style="border:none;">State</td>
            <td style="border:none;">:</td>
            <td style="border:none;"><?php echo !empty($customer_details->billing_state) ? $state_array[(int)$customer_details->billing_state] : '' ;?></td>
            <td valign="bottom" style="border:none; padding:0">
              <table width="90%" border="0" cellspacing="0" cellpadding="0">
                <tr>
                  <td>State Code</td>
                  <td><?php echo !empty($customer_details->billing_state) ? $customer_details->billing_state : '' ;?></td>
                </tr>
              </table>
            </td>
            
          </tr>
        </table></td>
        <td align="left" width="50%" valign="bottom" style="padding:0;">
          <table width="100%" border="0" cellspacing="0" cellpadding="0">
            <tr>
              <td width="38%"  style="border:none;">Name</td>
              <td width="3%"  style="border:none;">:</td>
              <td width="59%"  style="border:none;"><?php echo !empty($customer_details->name) ? $customer_details->name : '' ;?></td>
            </tr>
            <tr>
              <td style="border:none;">Address</td>
              <td style="border:none;">:</td>
              <td style="border:none;"><?php echo !empty($customer_details->shipping_address) ? $customer_details->shipping_address : '' ;?></td>
            </tr>
            <tr>
              <td style="border:none;">GSTIN</td>
              <td style="border:none;">:</td>
              <td style="border:none;"><?php echo !empty($customer_details->gstn) ? $customer_details->gstn : '' ;?></td>
            </tr>
            <tr>
              <td style="border:none;">State</td>
              <td style="border:none;">:</td>
              <td style="border:none;"><?php echo !empty($customer_details->shipping_state) ? $state_array[(int)$customer_details->shipping_state] : '' ;?>
                <table style="float: right;" width="50%" border="0" cellspacing="0" cellpadding="0">
                  <tr>
                    <td>State Code</td>
                    <td><?php echo !empty($customer_details->shipping_state) ? $customer_details->shipping_state : '' ;?></td>
                  </tr>
                </table>
              </td>
            </tr>
          </table>
        </td>
        </tr>
    </table>
    </td>
        </tr>
      <tr>
        <td colspan="5" align="left" valign="top">&nbsp;</td>
        </tr>
      <tr>
        <td colspan="5" align="left" valign="top"><table width="100%" border="0" cellspacing="0" cellpadding="0">
          <tr>
            <td width="4%" rowspan="2" align="center" bgcolor="#A3EBF1">Sr. No.</td>
            <td width="21%" rowspan="2" align="center" bgcolor="#A3EBF1">Name Of Product / Service</td>
            <td width="4%" rowspan="2" align="center" bgcolor="#A3EBF1">HSN ACS</td>
            <td width="4%" rowspan="2" align="center" bgcolor="#A3EBF1">UOM</td>
            <td width="4%" rowspan="2" align="center" bgcolor="#A3EBF1">Qty</td>
            <td width="6%" rowspan="2" align="center" bgcolor="#A3EBF1">Rate</td>
            <td width="8%" rowspan="2" align="center" bgcolor="#A3EBF1">Amount</td>
            <td width="8%" rowspan="2" align="center" bgcolor="#A3EBF1">Less/Discount</td>
            <td width="6%" rowspan="2" align="center" bgcolor="#A3EBF1">Taxable Value</td>
            <td colspan="2" align="center" bgcolor="#A3EBF1">CGST</td>
            <td colspan="2" align="center" bgcolor="#A3EBF1">SGST</td>
            <td colspan="2" align="center" bgcolor="#A3EBF1">IGST</td>
            <td width="6%" rowspan="2" align="center" bgcolor="#A3EBF1">Total</td>
          </tr>
          <tr>
            <td width="4%" align="center" bgcolor="#A3EBF1">Rate</td>
            <td width="6%" align="center" bgcolor="#A3EBF1">Amount</td>
            <td width="4%" align="center" bgcolor="#A3EBF1">Rate</td>
            <td width="6%" align="center" bgcolor="#A3EBF1">Amount</td>
            <td width="3%" align="center" bgcolor="#A3EBF1">Rate</td>
            <td width="6%" align="center" bgcolor="#A3EBF1">Amount</td>
          </tr>
          <?php if(count($item_details) > 0){
            $i =1;
            $total_qty = 0; 
            $total_amount = 0; 
            $total_discount = 0;
            $total_rate = 0; 
            $total_taxable_amount = 0;
            
            foreach($item_details as $key => $value){ 
              $total_qty += $value->qty; 
              $total_amount += $value->amount; 
              $total_discount += isset($value->discount) ? $value->discount : 0;
              $total_taxable_amount += ($value->qty * $value->rate); 
            ?> 
            <tr height="50">
              <td align="center"><?php echo $i;?></td>
              <td align="center" style="border-bottom:none; border-top:none;"><?php echo !empty($value->item) ? $value->item : '' ;?></td>
              <td align="center" style="border-bottom:none; border-top:none;"><?php echo !empty($value->hsn_code) ? $value->hsn_code : '' ;?></td>
              <td align="center" style="border-bottom:none; border-top:none;"><?php echo !empty($value->uom) ? $value->uom : '' ;?></td>
              <td align="center" style="border-bottom:none; border-top:none;"><?php echo !empty($value->qty) ? $value->qty : '' ;?></td>
              <td align="center" style="border-bottom:none; border-top:none;"><?php echo !empty($value->rate) ? number_format((float)$value->rate, 2, '.', '') : '' ;?></td>
              <td align="center" style="border-bottom:none; border-top:none;"><?php echo number_format((float)($value->qty * $value->rate), 2, '.', '');?></td>
              <td align="center" style="border-bottom:none; border-top:none;"><?php echo !empty($value->discount) ? $value->discount : '-' ;?></td>
              <td align="center" style="border-bottom:none; border-top:none;"><?php echo number_format((float)($value->qty * $value->rate), 2, '.', '') ;?></td>

              <td align="center" style="border-bottom:none; border-top:none;"><?php echo (!empty($customer_details->shipping_state) && ($customer_details->shipping_state == 19)) ? ($value->gst_rate/2).' %' : '-';?></td>
              <td align="center" style="border-bottom:none; border-top:none;"><?php echo !empty($value->cgst) ? number_format((float)$value->cgst, 2, '.', '') : '-' ;?></td>
              <td align="center" style="border-bottom:none; border-top:none;"><?php echo (!empty($customer_details->shipping_state) && ($customer_details->shipping_state == 19)) ? ($value->gst_rate/2).' %' : '-';?></td>
              <td align="center" style="border-bottom:none; border-top:none;"><?php echo !empty($value->sgst) ? number_format((float)$value->sgst, 2, '.', '') : '-' ;?></td>

              <td align="center" style="border-bottom:none; border-top:none;"><?php echo (!empty($customer_details->shipping_state) && ($customer_details->shipping_state != 19)) ? $value->gst_rate.' %' : '-';?></td>
              <td align="center" style="border-bottom:none; border-top:none;"><?php echo !empty($value->igst) ? number_format((float)$value->igst, 2, '.', '') : '-' ;?></td>
              <td align="center" style="border-bottom:none; border-top:none;" bgcolor="#A3EBF1"><?php echo number_format((float)($value->amount + $value->cgst + $value->sgst + $value->igst), 2, '.', '');?></td>
            </tr>
          <?php $i++;}
          }?>
          
          <tr>
            <td colspan="4" align="right">Total</td>
            <td align="center"><?php echo $total_qty;?></td>
            <td align="center">&nbsp;</td>
            <td align="center"><?php echo !empty($total_taxable_amount) ? number_format((float)$total_taxable_amount, 2, '.', '') : '0.00';?></td>
            <td align="center"><?php echo !empty($total_discount) ? number_format((float)$total_discount, 2, '.', '') : '';?></td>
            <td align="center"><?php echo !empty($total_taxable_amount) ? number_format((float)$total_taxable_amount, 2, '.', '') : '';?></td>
            <td colspan="2" align="center"><?php echo !empty($dbtnote_data['cgst_total']) ? number_format((float)$dbtnote_data['cgst_total'], 2, '.', '') : '';?></td>
            <td colspan="2" align="center"><?php echo !empty($dbtnote_data['sgst_total']) ? number_format((float)$dbtnote_data['sgst_total'], 2, '.', '') : '';?></td>
            <td colspan="2" align="center"><?php echo !empty($dbtnote_data['igst_total']) ? number_format((float)$dbtnote_data['igst_total'], 2, '.', '') : '';?></td>
            <td align="center" bgcolor="#A3EBF1"><?php !empty($dbtnote_data['total']) ? number_format((float)$dbtnote_data['total'], 2, '.', '') : '';?></td>
          </tr>
        </table></td>
        </tr>
      <tr>
        <td colspan="5" align="left" valign="top">&nbsp;</td>
        </tr>
      <tr>
        <td height="42" colspan="3" align="center" valign="top">Total Invoice Amount in Words<br/><strong><?php echo ucwords(strtolower(decimal_to_words((int)$dbtnote_data['total']))); ?> Only..</strong></td>
        <td colspan="2" rowspan="2" align="left" valign="top"><table width="100%" border="0" cellspacing="0" cellpadding="0">
          <tr>
            <td width="71%" align="left" valign="top">Total Amount Before Tax</td>
            <td width="29%" align="right" valign="top"><?php echo number_format((float)$total_taxable_amount, 2, '.', '');?></td>
          </tr>
          <tr>
            <td align="left" valign="top">Add : CGST</td>
            <td align="right" valign="top"><?php echo number_format((float)$dbtnote_data['cgst_total'], 2, '.', '');?></td>
          </tr>
          <tr>
            <td align="left" valign="top">Add : SGST</td>
            <td align="right" valign="top"><?php echo number_format((float)$dbtnote_data['sgst_total'], 2, '.', '');?></td>
          </tr>
          <tr>
            <td align="left" valign="top">Add : IGST</td>
            <td align="right" valign="top"><?php echo number_format((float)$dbtnote_data['igst_total'], 2, '.', '');?></td>
          </tr>
          <tr>
            <td align="left" valign="top">Tax Amount : GST</td>
            <td align="right" valign="top"><?php echo number_format((float)($dbtnote_data['igst_total'] + $dbtnote_data['sgst_total'] + $dbtnote_data['cgst_total']), 2, '.', '');?></td>
          </tr>
          <tr>
            <td align="left" valign="top">Total Amount After Tax</td>
            <td align="right" valign="top"><?php echo number_format((float)$dbtnote_data['total'], 2, '.', '');?></td>
          </tr>
          <tr>
            <td colspan="2" align="left" valign="top">&nbsp;</td>
            </tr>
          <tr>
            <td align="left" valign="top">GST Payable on Reverse Charge :</td>
            <td align="left" valign="top">&nbsp;N.A.</td>
          </tr>
          <tr>
            <td height="72" colspan="2" align="left" valign="top">Certified that the particulars given above are true and correct. For,<?php echo !empty($setings->company_name) ? $setings->company_name : '';?></td>
            </tr>
          <tr>
            <td colspan="2" align="center" valign="top">Authorised Signatory</td>
            </tr>
        </table></td>
        </tr>
      <tr>
        <td colspan="3" align="left" valign="top"><table width="100%" border="0" cellspacing="0" cellpadding="0">
          <tr>
            <td width="59%" height="42" align="left" valign="top">: Bank Details : A/C No  - <?php echo !empty($setings->bank_account) ? $setings->bank_account : '';?><br/> IFS Code - <?php echo !empty($setings->ifsc_code) ? $setings->ifsc_code : '';?></td>
            <td width="41%" rowspan="2" align="center" valign="bottom">(Common Seal)</td>
            </tr>
          <tr>
            <td height="84" align="left" valign="top">: Terms and Conditions :</td>
            </tr>
        </table></td>
        </tr>
      <tr>
        <td width="31%" align="left" valign="top">&nbsp;</td>
        <td width="28%" align="left" valign="top">&nbsp;</td>
        <td width="9%" align="left" valign="top">&nbsp;</td>
        <td align="left" valign="top">&nbsp;</td>
        <td align="right" valign="top">EOL</td>
      </tr>
      
    </table>
    </div>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
  <script type="text/javascript">
    $(document).ready(function(){
      $('.print_bill').on('click',function(){
        var divToPrint=document.getElementById('DivIdToPrint');
        var newWin=window.open('','Print-Window');
        newWin.document.open();
        newWin.document.write('<html><body onload="window.print()">'+divToPrint.innerHTML+'</body></html>');
        newWin.document.close();
        setTimeout(function(){newWin.close();},10);
      });
      
      // $('.edit_bill').on('click',function(){
      //   var bill = $(this).data('bill');
      //   window.location.href = '<?php //echo site_url("purchase/newpurchase/'+bill+'");?>';
      // });
    });
  </script>
  </body>
</html>
