<?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";
 }
 document.getElementById("btno").style.display = "none";
 document.getElementById("btnd").style.display = "none";
 window.print();
 document.body.innerHTML=restore;
 

}



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

<?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 saleheader where id='".$invoice_id."' "); 
//$re=mysqli_query($sql);
 if($row = mysqli_fetch_array($re))
 {
    $invoice_no= $row['invoice_no'];
	
	$ename= $row['ename'];
	$vehicleno= $row['vehicleno'];
   $tamount = round($row['tamount']);
   $customer_code = $row['customer_id'];
   $date1 = $row['sale_date'];
    $date = date("d-m-Y", strtotime($date1)); 
	
	$date1 = $row['sale_date'];
	
	 $pending_amount=$row['pending_amount'];
	  $collection_amount=$row['collection_amount'];
	   $discount=$row['discount'];
	   
	   if($discount=='0.00')
	   {
		   
		   $display="style=display:none";
		   
	   }
	   
	  else
	   {
		   
		   $display="";
		   
	   }
	   
	   
	    $pending_tray=$row['pending_tray'];
		 $in_tray=$row['in_tray'];
		  $out_tray=$row['out_tray']; 
   
  
  // $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_tamil'];
	   $address= $row2['caddress'];
  	   $address2= $row2['area_tamil'];
	   $pincode= $row2['pincode'];
	   $area= $row2['area'];
   	  $area_tamil= $row2['area_tamil'];
   	  $mobile=$row2['mobile'];
	   
    }
    
   //$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.qty_type,pm.product_tamil  FROM saledetails as sii LEFT JOIN product_master as pm on pm.id=sii.productID  WHERE sii.saleheader_id = '$invoice_id'";
	//echo $sql3;

    $re3 = mysqli_query($conn,$sql3); 
   
            
 }
?>
<button onClick="printin('p1','cpy','o','amd','<?php echo number_format((float)$tamount, 2, '.', '');?>')" id="btno">O print</button>
<button onClick="printin('p1','cpy','d','amd','<?php echo number_format((float)$tamount, 2, '.', '');?>')" id="btnd">D print</button>
<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;">
     
     <thead>
     
       <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"> RK Fruits</font></B>
       <br /><u><strong>பழ கமிஷன் மண்டி</strong></u>
            <br><font style="font-size: 14px;"><strong>காய்கறி  மார்க்கெட் பிள்ளையார் கோவில் எதிரில் , <br>கள்ளக்குறிச்சி  -606202.</strong></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 : 98948 17332 & 73588 75353</b></td><td align="left"  valign="center" width="255"><B></B></td>
      </tr>
      </table>
      </center> 
      
       </td></tr>
       
       <tr >
      <td colspan="6" rowspan="4" style="border-right:2pt solid black; border-bottom:2pt solid black;" valign="top" >
      <font size="3"><U><B>Buyer Name & Address</B></U>
      <br><?php echo $client_name; ?>
      <br><?php echo nl2br($address); ?>
      <br><?php echo $pincode; ?>  
      
       <br>Mobile:<?php echo $mobile; ?>
      
      </font>
      <br></td>
      <td colspan="4" style="border-right:2pt solid black;" rowspan="4">
       <center><b>  INVOICE</b></center>
      <table width="100%" border="1">
      <tr>
         <td colspan="2" >Invoice&nbsp;No.</td><td colspan="2" >: <?php echo $invoice_no; ?> </td> </tr>
          
        <tr><td colspan="2" >Date.</td><td colspan="2"  >: <?php echo $date;  ?></td> </tr>
        <tr><td colspan="2" >Area</td><td colspan="2" >:  <?php echo $area; ?>/     (<?php echo $area_tamil;?>)</td> </tr>
      </table>
      </td>
      
      
      
      </tr>
       </thead>
       <tbody>
      
        

    <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="7%" style="border-right:2pt solid black;"><center><b>Qty</b></center></td>
    <td width="20%" style="border-right:2pt solid black;"><center><b>UOM</b></center></td>
    <td width="7%" style="border-right:2pt solid black;"><center><b>Rate</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['product_tamil'].')';
	   $price= $row3['sale_prate'];
	   $qty = $row3['sale_eqty'];
       $gst_per = 0;
	   $qty_type=$row3['qty_type'];
       //$hsncode = $row3['hsn_code'];  
       
       $amount = $price * $qty;


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

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

      <td  align="center" style="font-size:20px;"><?php echo $i;?></td>
      <td colspan="5" style="font-size:20px;"><?php echo $item;?></td>
     
      <td  align="center" style="font-size:20px;"><?php echo $qty;?></td>
       <td  align="center" style="font-size:20px;"><?php echo $qty_type;?></td>
      <td  align="right" style="font-size:20px;"><?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;font-size:20px;" 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);

    $logsttotal = number_format((float)$gstby2total, 2, '.', '');
    $igstDisptotal = number_format((float)00, 2, '.', '');



?>
    
    </tbody>
    <tfoot>
  
    <tr style="border-top:2pt solid black; border-bottom: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" style="border-bottom:2pt solid black; font-size:20px;" ><b>Bill Amount </b></td><td rowspan="2" align="right" style="border-right:2pt solid black;border-bottom:2pt solid black;font-size:20px;"><strong><?php echo number_format((float)$totaloftotal, 2, '.', '');  ?></strong></td></tr>
    

    <tr> 
    <td colspan="6" rowspan="6" align="left" valign="top" style="border-top:2pt solid black;">
        
   <table >
 
     <tr><td align="right" style="font-size:20px;"><strong>கிரேடு  பாக்கி:</strong></td><td align="right" style="font-size:20px;"> <strong><?php echo number_format($pending_tray,2,'.','');?></strong> </td></tr>
     
      <tr><td align="right" style="font-size:20px;"><strong>கிரேடு :</strong></td><td align="right" style="font-size:20px;">  <strong><?php echo number_format(($out_tray),2,'.','');?></strong></td></tr>
     
     <tr><td align="right" style="font-size:20px;"><strong>Empty கிரேடு :</strong></td><td align="right" style="font-size:20px;"> <strong> <?php echo number_format($in_tray,2,'.','');?></strong></td></tr>
    
     <tr><td align="right" style="font-size:20px;"><strong>பாக்கி  கிரேடு  :</strong></td><td align="right" style="font-size:20px;">  <strong><?php echo number_format((($pending_tray+$out_tray)-$in_tray),2,'.','');?></strong></td></tr>
    
     
      </table>   </td>
    </tr>
    
    <tr style="border-bottom:2pt solid black; font-size:20px;"> 
    <td colspan="3" align="right"><strong>முன் பாக்கி :</strong></td>
    <td align="right" style="font-size:20px;"> <strong><?php echo number_format($pending_amount,2,'.','');?></strong></td>
    </tr>
    <tr style="border-bottom:2pt solid black;font-size:20px;">
      <td colspan="3" align="right"><strong>Total Amount:</strong></td>
      <td align="right"> <strong> <?php echo number_format(($roundtotal+$pending_amount),2,'.','');?></strong></td>
    </tr>
    <tr style="border-bottom:2pt solid black;font-size:20px;">
      <td colspan="3" align="right"><strong>வரவு :</strong></td>
      <td align="right" style="font-size:20px;"> <strong> <?php echo number_format($collection_amount,2,'.','');?></strong></td>
    </tr>
    <tr style="border-bottom:2pt solid black; font-size:20px;">
      <td colspan="3" align="right"><strong><span <?php echo $display;?> >Discount Amt:</span></strong></td>
      <td align="right" style="font-size:20px;"> <strong> <span <?php echo $display;?> ><?php echo number_format($discount,2,'.','');?></span></strong></td>
    </tr>
    <tr style="border-bottom:2pt solid black;font-size:20px;">
      <td colspan="3" align="right"><strong>மொத்த பாக்கி:</strong></td>
      <td align="right" style="font-size:20px;">  <strong><?php echo number_format((($pending_amount+$roundtotal)-($collection_amount+$discount)),2,'.','');?></strong></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 />     </td> 
     <td style="border-right:2pt solid black;" colspan="5" align="right"  valign="top"><b><font size="3">For RK Fruits</font> </b><br />
  <img src="        "  />
<br />
<br>Authorized signatory</td>
</tr></tfoot>
   </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>