<?php 

require_once("config/configDB.php");

//header('Content-disposition: attachment; ');
//header('Content-type: application/pdf');


?>
<!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>Invoice-<?php echo $_GET['saleID']; ?></title>

<script type="text/javascript">

 function printin(el,cpy,c,amd,valam)
{
 var restore =  document.body.innerHTML;
 var pricn = document.getElementById(el).innerHTML;
 document.body.innerHTML=pricn;
 document.getElementById(amd).innerHTML = valam;
 if(c=='o')
 {
   document.getElementById(cpy).innerHTML = "Original Copy";
 }
 else
 {
  document.getElementById(cpy).innerHTML = "Duplicate Copy";
 }
 window.print();
 document.body.innerHTML=restore;

}

</script>
</head>
<body onload="btnd.click(" >

<?php


function convertNumberToWord($number = false)
{
  
   $no = round($number);
   $point = round($number - $no, 2) * 100;
   $hundred = null;
   $digits_1 = strlen($no);
   $i = 0;
   $str = array();
   $words = array('0' => '', '1' => 'one', '2' => 'two',
    '3' => 'three', '4' => 'four', '5' => 'five', '6' => 'six',
    '7' => 'seven', '8' => 'eight', '9' => 'nine',
    '10' => 'ten', '11' => 'eleven', '12' => 'twelve',
    '13' => 'thirteen', '14' => 'fourteen',
    '15' => 'fifteen', '16' => 'sixteen', '17' => 'seventeen',
    '18' => 'eighteen', '19' =>'nineteen', '20' => 'twenty',
    '30' => 'thirty', '40' => 'forty', '50' => 'fifty',
    '60' => 'sixty', '70' => 'seventy',
    '80' => 'eighty', '90' => 'ninety');
   $digits = array('', 'hundred', 'thousand', 'lakh', 'crore');
   while ($i < $digits_1) {
     $divider = ($i == 2) ? 10 : 100;
     $number = floor($no % $divider);
     $no = floor($no / $divider);
     $i += ($divider == 10) ? 1 : 2;
     if ($number) {
        $plural = (($counter = count($str)) && $number > 9) ? 's' : null;
	    $plural =null;
        $hundred = ($counter == 1 && $str[0]) ? ' and ' : null;
        $str [] = ($number < 21) ? $words[$number] .
            " " . $digits[$counter] . $plural . " " . $hundred
            :
            $words[floor($number / 10) * 10]
            . " " . $words[$number % 10] . " "
            . $digits[$counter] . $plural . " " . $hundred;
     } else $str[] = null;
  }
  $str = array_reverse($str);
  $result = implode('', $str);
  $points = ($point) ?
    "." . $words[$point / 10] . " " . 
          $words[$point = $point % 10] : '';
  //echo $result . "Rupees  " . $points . " Paise";
    return  $result;
}


  $invoice_id  = $_GET['saleID'];
  
$re = mysqli_query($conn,"select * from saleheaderwo where id='".$invoice_id."' "); 
//$re=mysqli_query($sql);
 if($row = mysqli_fetch_array($re))
 {
    $invoice_no= $row['invoice_no'];
    
   $customer_code = $row['customer_id'];
   $date1 = $row['sale_date'];
    $date = date("d-m-Y", strtotime($date1));  
   
   $po = $row['po_num'];
    $delivery_address = $row['delivery_address'];
   
  // $status = $row[6]; 
   // $others = $row[5];
//	$others_amount = $row[6];

   //$sql2="select * from customer_master where id='".$customer_code."' ";
   $re2 = mysqli_query($conn,"select * from customer_master where id='".$customer_code."' "); 
   //$re2=mysqli_query($sql2);
   if($row2 = mysqli_fetch_array($re2))
    {

	   $client_name= $row2['cname'];
	   $address= $row2['caddress'];
  	   
   	   $address3= $row2['customer_state'];
	   $gstin= $row2['gstin'];
       $state_code = $row2['customer_state'];
	   
    }
    
   //$sql3="SELECT sii.*,pro.code,pro.name,pro.    FROM `saleiteminfo` as sii left join product as pro ON pro.id = sii.product_id  where invoice_id='$invoice_id'";

    $sql3 ="SELECT sii.*,pm.productcode,pm.productname,pm.hsn  FROM saledetailswo as sii LEFT JOIN product_master as pm on pm.id=sii.productID  WHERE sii.saleheader_id = '$invoice_id'";

    $re3 = mysqli_query($conn,$sql3); 
   
            
 }
?>
<div id="p1">
<center>
    <table width="690">
    <tr><td><div align="left" id="cpy">Original Copy</div></td><td align="right"><b>Bill Amount :</b><b  id="amd"></b></td></tr>
    </table>
   <table border='3' width="760"  cellpadding="5" style="border-collapse:collapse;">
     
     
     
       <tr style="border-bottom:2pt solid black;"><td style="border-right:2pt solid black;" colspan="10"  >
       <center>
       
   <table width="100%" >
  <tr>
       <td width="21%" align="right" >
       <!--<img src="images/logo.png" width="150" height="100"  />-->       </td>
       <td width="61%" align="left"> <center><B><font size="6dp"> RAVI JWELLERY</font></B>
       
            <br><font style="font-size: 14px;">BO:72/F1,Nandhavanam Street,Chinna Kadaiveethi <br>Namakkal-1.</font></center>
        <br>      </td>
      <td width="18%" align="center" >
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;       </td> 
      </tr>
</table>
    <table border="0" width="100%">
      <tr>
      <td align="left"><b>Phone No : 94437 29521/99944 22805</b></td><td align="left"  valign="center" width="255"><B><b>GSTIN &nbsp;&nbsp;: 33AELPR1612J1ZJ.</b></B></td>
      </tr>
      <!--<tr>
      <td align="left"><b>E-mail : info@cambersystems.in</b></td>
      <td align="left"><b>Website:www.cambersystems.in</b></td>
      </tr>-->
      </table>
      </center> 
      
       </td></tr>
      <tr >
      <td colspan="6" rowspan="5" style="border-right:2pt solid black; border-bottom:2pt solid black;" valign="top" >
      <U><B><font size="3">Buyer Name & Address</B></U>
      <br><?php echo $client_name; ?>
      <br><?php echo nl2br($address); ?>
      <br />
      <br>
      <U><B><font size="3">Delivery Address</B></U>
      <br /><?php echo $delivery_address; ?>
      <br></td>
      <td colspan="4" style="border-right:2pt solid black;"><center><b>  ESTIMATION</b></center></td></tr>
         <td colspan="2">Invoice No.</td><td colspan="2" style="border-right:2pt solid black;">: <?php echo $invoice_no; ?> </td> </tr>
         <!--<td colspan="2"><b>Bill Amount</b></td><td colspan="2" style="border-right:2pt solid black;">: <b  id="amd"></b> </td> </tr>-->    
        <tr><td colspan="2">Date.</td><td colspan="2"  style="border-right:2pt solid black;">: <?php echo $date;  ?></td> </tr>
        <tr><td colspan="2">PO No.</td><td colspan="2" style="border-right:2pt solid black;">: <?php echo $po; ?></td> </tr>
        
        <tr><td colspan="2">State Code</td><td  colspan="2" style="border-right:2pt solid black;">: TN - 33</td> </tr>
      
    <tr style="border-bottom:2pt solid black; border-top:2pt solid black;">
    <td width="5%" style="border-right:2pt solid black;" ><center><b>Sno.</b></center></td>
    <td width="50%" style="border-right:2pt solid black;" colspan="5"><center><b>Description of Goods</b></center></td>
    <td width="20%" style="border-right:2pt solid black;"><center><b>HSN</b></center></td>
    <td width="7%" style="border-right:2pt solid black;"><center><b>Rate</b></center></td>
    <td  width="7%" style="border-right:2pt solid black;"><center><b>Qty</b></center></td>
    
    <!--<td width="1%" style="border-right:2pt solid black;"><center><b>Amount</b></center></td>
    <td width="12%"   style="border-right:2pt solid black;"><center><b>CGST</b></center></td>
    <td width="12%" style="border-right:2pt solid black;"><center><b>SGST</b></center></td>
    <td width="12%"  style="border-right:2pt solid black;"><center><b>IGST</b></center></td>-->
    <td width="1%" style="border-right:2pt solid black;"><center><b>Total</b></center></td></tr>
    
    <?php
	$i = 0;
	$total = 0;
    $gstby2total =0;
    $totaloftotal =0;
        $igsttotal=0;
	 while($row3 = mysqli_fetch_array($re3))
     {
	   $item = $row3['productname'];//.'('.$row3['productcode'].')';
	   $price= $row3['sale_prate'];
	   $qty = $row3['sale_eqty'];
       $gst_per = $row3['sale_gst'];
       $hsncode = $row3['hsn'];  
       
       $amount = $price * $qty;


       $gst = ($amount * $gst_per) / 100 ;

       $gstby2 = $gst / 2 ;
       $amountgsttotal = $amount + $gst ;
	   $total+=$amount;
       $gstby2total+=$gstby2;
        $igsttotal+=$gst;
       $totaloftotal+= $amountgsttotal;
       
       if($state_code == 'Tamil Nadu')
       {
          $igst_per = "-";
          $igst = number_format((float)(00), 2, '.', '');
          $logst_per = number_format((float)($gst_per/2), 2, '.', '')."%";
          $logst =  number_format((float)$gstby2, 2, '.', '');
       }
       else
       {
         $igst_per = number_format((float)($gst_per), 2, '.', '')."%";
         $igst = number_format((float)($gst), 2, '.', '');
         $logst = number_format((float)(00), 2, '.', '');
         $logst_per ="-";
         
       }
       
       $i++;
	?>
     
     <tr style="border-bottom:0pt solid white;"> 

      <td  align="center" ><?php echo $i;?></td>
      <td colspan="5"><?php echo $item;?></td>
      <td  align="center"><?php echo $hsncode;?></td>
      <td  align="center"><?php echo $qty;?></td>
      <td  align="right"><?php echo number_format((float)$price, 2, '.', '');?></td>
      
<!--      <td align="right"><?php echo number_format((float)$amount, 2, '.', ''); ?></td>
      <td width="6%" align="right"><?php echo $logst; ?><br /><font size="2">(<?php echo $logst_per; ?>)</font></td>
      <td width="6%" align="right"><?php echo $logst; ?><br /><font size="2">(<?php echo $logst_per; ?>)</font></td>
      
      <td  width="6%" align="right"><?php echo $igst; ?><br /><font size="2">(<?php echo $igst_per; ?>)</font></td>-->
      
      <td style="border-right:2pt solid black;" align="right"><?php echo number_format((float)$amountgsttotal, 2, '.', ''); ?></td></tr>
  
<?php
    }
    
	for($ii = $i ;$ii<9 ; $ii++)
	{
	?>
       <tr style="border-bottom:0pt solid white;" height="30">
       <td  align="center" ></td>
       <td  colspan="5"></td>
       <td ></td>
       <td  align="center" ></td>
       <td  align="center"></td>
   <!--    <td  align="center"></td>
       <td align="right"></td>
      
       <td align="right"></td>
       <td align="right"></td>-->
       <td style="border-right:2pt solid black;" align="right"></td></tr>
  <?php
	}

$vat = ($total*5.0)/100.0;
$gtotal = $total + $vat;
$roundtotal = round($totaloftotal);
if($state_code == 'Tamil Nadu')
{
    $logsttotal = number_format((float)$gstby2total, 2, '.', '');
    $igstDisptotal = number_format((float)00, 2, '.', '');
}
else
{
    $logsttotal = number_format((float)00, 2, '.', '');
    $igstDisptotal = number_format((float)$igsttotal, 2, '.', '');
}

?>
    
    
  
    <tr style="border-top:2pt solid black;">
    <td colspan="6" align="left" valign="top" style="border-top:2pt solid black;">
    <b>Total Amount in words : </b> <br>   
    <?php echo ucfirst(convertNumberToWord($roundtotal)).'rupees only'; ?></td>
    <td colspan="3" rowspan="2" ><b>Total Amount </b></td><td rowspan="2" align="right" style="border-right:2pt solid black;"><?php echo number_format((float)$totaloftotal, 2, '.', '');  ?></td></tr>
    

    <tr> 
    <td colspan="6" rowspan="2" align="left" valign="top" style="border-top:2pt solid black;">
        
    <table >
     <tr><td>Account NO.</td><td> : </td></tr>
     <tr><td>Bank Name.</td><td> : </td></tr>
     <tr><td>Bank Branch.</td><td> : </td></tr>
     <tr><td>IFSC Code.</td><td> : </td></tr>
     <!--<tr><td>Company's PAN.</td><td> : AKJPN9602Q</td></tr>-->
      </table>    </td>
    </tr>
    
    <tr style="border-bottom:2pt solid black;"> <td colspan="3"><b>Total(Rounded Off)<br>E. & O.E.</td><td style="border-right:2pt solid black;" align="right"><b><?php echo  number_format((float)$roundtotal, 2, '.', ''); ?></b></td></tr>
     <tr style="border-bottom:2pt solid black;border-top:2pt solid black; ">
     <td colspan="5" style="font-size: 13px;" >
     
     
     1) Goods once sold will not be taken back.<br />
    2) If any damage caused on transport, We are not reposponsible. </td> 
     <td style="border-right:2pt solid black;" colspan="5" align="right"  valign="top"><b><font size="3">For RAVI JWELLERY</font> </b><br />
  <img src="        "  />
<br />
<br>Authorized signatory</td>
   </table>
   <table width="700">
    <tr><td><center> This is Computer Generated Invoice</center></td></tr>
    </table>
</center>   
</div>
<button onClick="printin('p1','cpy','o','amd','<?php echo number_format((float)$roundtotal, 2, '.', '');?>')" id="btno">O print</button>
<button onClick="printin('p1','cpy','d','amd','<?php echo number_format((float)$roundtotal, 2, '.', '');?>')" id="btnd">D print</button>

<?php

//echo "<a href='salebill.php?saleID=$invoice_id'  target='_blank' class='btn btn-primary'>Generate PDF</a>";
?>



</body>
</html>