<?php
session_start();
include 'dbc.php';

$userid=$_SESSION['userid'];

$id=$_REQUEST["id"];
?>

<style>
.borderexample {
 border-width:3px;
 border-style:solid;
 border-color:#287EC7;
}
</style>
<?php
  $sqlcus = "select * from invoice_st where id='$id' and user_id='$userid'";
   $result=$conn->query($sqlcus);
$ppp = $result->fetch_assoc();
    $date=$ppp["date"];
	$cell=$ppp["cell"];
	$cname=$ppp["cname"];
	$pname=$ppp["pname"];
	$netamount=$ppp["netamount"];
	$netamount=$ppp["stamount"];
	$tnetamount=$ppp["tnetamount"];
	$discount=$ppp["discount"];
	$gamount=$ppp["gamount"];
	$cashrec=$ppp["cashrec"];
	$balret=$ppp["balret"];
	$disc=$ppp["disc"];
	$dno=$ppp["dno"];
	$pno=$ppp["pno"];
	
	 $sqlcus1 = "select * from feenote";
   $result1=$conn->query($sqlcus1);
$ppp1 = $result1->fetch_assoc();
	 $notice=$ppp1["notice"];
	  $sqlcus2 = "select * from customer where bname='$cname' and user_id='$userid'";
   $result2=$conn->query($sqlcus2);
$ppp2 = $result2->fetch_assoc();
	 $ntnno=$ppp2["ntnno"];
	  $saletaxno=$ppp2["saletaxno"];


 	    
		
echo "<table border=0 width=100% align=center bgcolor=white>";
echo "<tr>";
echo "<td bgcolor=white align=center colspan=9>" ."<img src='logo.jpg' />" . "</td>";
echo "</tr>";
echo "<tr>";
echo "<td bgcolor=white align=center colspan=9><font color=black size=8pt face=Impact>" . "RIGHT WAY TRADER". "</font></td>";
echo "</tr>";
echo "<tr>";
echo "<td bgcolor=white align=center colspan=9><font color=black size=3pt face=Arial> NTN : 3343901 , STR# : 3277876163176</font></td>";
echo "</tr>";
echo "<tr>";
echo "<td bgcolor=white align=center colspan=9><font color=black size=2pt face=Arial>107 Bank Squre Market Model Town Lahore Tel: +92-423-5845106/+92-321-8403888</font></td>";
echo "</tr>";


echo "<tr>";
echo "<td bgcolor=white align=center colspan=9><font color=black size=3pt face=Arial>" . "Sales Tax Invoice" . "</font></td>";
echo "</tr>";




echo "<tr>";
  echo "<td align=left colspan=2><font color=black size=3pt face=Arial>" ."Bill To" . "</font></td>" ;
  echo "<td align=left colspan=3><font color=black size=2pt face=Arial>" .$ppp["cname"] .   "</font></td>" ;
  echo "<td bgcolor=white align=right colspan=4><font color=black size=3pt face=Arial>" . "Invoice No:" .$ppp["id"]  .  "</font></td>";
  echo "<tr>";
  echo "<td align=left colspan=2><font color=black size=3pt face=Arial>" ."Address" . "</font></td>" ;
  echo "<td align=left colspan=3><font color=black size=2pt face=Arial>" .$ppp["address"] .   "</font></td>" ;
  echo "<td bgcolor=white align=right colspan=4><font color=black size=3pt face=Arial>" . "Date:"  .$ppp["date"] . "</font></td>";
  echo "<tr>";
  echo "<td align=left colspan=5><font color=black size=3pt face=Arial>" ."NTN:" . $ntnno . "</font></td>" ;
   echo "<td bgcolor=white align=right colspan=4><font color=black size=3pt face=Arial>"  . "PO No:"  .$ppp["pno"] . "</font></td>";
  echo "</tr>"; 
  echo "<tr>";
  echo "<td align=left colspan=5><font color=black size=3pt face=Arial>" ."STR #:" . $saletaxno . "</font></td>" ;
  echo "</tr>"; 
  
 
 
 
  
 echo "</table>";
$sql1 = "select * from invoice_details_st where inv_no='$id' and user_id='$userid'";
   $result1=$conn->query($sql1);
echo "<table border=1 style=border:solid black width=100% align=center bgcolor=white>";
 echo "<tr>";
 echo "<th bgcolor=white align=center style=border:solid black ><font color=black size=2pt face=Arial> Sr </font></th>";
  echo "<th bgcolor=white align=center style=border:solid black><font color=black size=2pt face=Arial> Item Name </font></th>";
  echo "<th bgcolor=white align=center style=border:solid black><font color=black size=2pt face=Arial> UM Unit </font></th>";
   echo "<th bgcolor=white align=center style=border:solid black><font color=black size=2pt face=Arial> Qty </font></th>";
 echo "<th bgcolor=white align=center style=border:solid black><font color=black size=2pt face=Arial> Unit Price </font></th>";

 echo "<th bgcolor=white align=center style=border:solid black><font color=black size=2pt face=Arial>  Exl(GST) Amount </font></th>";
  echo "<th bgcolor=white align=center style=border:solid black><font color=black size=2pt face=Arial>  S.Tax % </font></th>";
 echo "<th bgcolor=white align=center style=border:solid black><font color=black size=2pt face=Arial> (GST) Amount </font></th>";
 echo "<th bgcolor=white align=center style=border:solid black><font color=black size=2pt face=Arial> Inc(GST) Amount </font></th>";

 
 echo "</tr>";
  $sno=1;
  while($row = $result1->fetch_assoc())

  {
   echo "<tr>";
  
	 echo "<td height=20 align=center><font color=black size=3pt face=Arial>" .$sno . "</font></td>";
	 echo "<td height=20><font color=black size=3pt face=Arial>" . $row['barcode']. ":" .  $row['item_name'] . "</font>  </td>";
	  echo "<td height=20 align=center><font color=black size=3pt face=Arial>" . $row['rfid']. "</font>  </td>";
	  echo "<td height=20 align=center><font color=black size=3pt face=Arial>" .$row['qty'] . "</font></td>";
	 echo "<td  height=20 align=center><font color=black size=3pt face=Arial>" .$row['srate'] . "</font></td>";
	
	 echo "<td  height=20 align=center><font color=black size=3pt face=Arial>" .$row['amount'] . "</font></td>";
	  echo "<td  height=20 align=center><font color=black size=3pt face=Arial>" .$row['st'] . "</font></td>";
	   echo "<td  height=20 align=center><font color=black size=3pt face=Arial>" .$row['stamount'] . "</font></td>";
	    echo "<td  height=20 align=center><font color=black size=3pt face=Arial>" .$row['netamount'] . "</font></td>";
	
		
		
  echo "</tr>";
 
  $sno=$sno + 1;
  $treceived=$treceived + $row['amount'];
   $st=$st + $row['st'];
    $stamount=$stamount + $row['stamount'];
  $tqty=$tqty + $row['qty'];
  
  }
  echo "<tr height=40>";

 
 echo "<th align=right bgcolor=white colspan=3><font color=black size=4pt face=Arial> Total Qty:&nbsp;    </font>  </th>";

 echo "<th align=right><font color=black size=4pt face=Arial>" .$tqty . "</font></th>" ;
 echo "</tr>";
echo "</table>";


echo "<table border=0 width=100% align=center bgcolor=white>";

 echo "<tr height=40>";
  echo "<td align=right colspan=7></td>" ;
 
 echo "<th align=right bgcolor=white><font color=black size=4pt face=Arial> Gross Amount:&nbsp;    </font>  </th>";

 echo "<th align=right class='borderexample'><font color=black size=4pt face=Arial>" .round($treceived,1) . "</font></th>" ;
 echo "</tr>";
 echo "<tr height=40>";
 echo "<td align=left colspan=7></td>" ;
 echo "<th align=right bgcolor=white ><font color=black size=4pt face=Arial> GST Amount:&nbsp;&nbsp;&nbsp;&nbsp;    </font>  </th>";

 echo "<th align=right class='borderexample'><font color=black size=4pt face=Arial>" .round($stamount,1) . "</font></th>" ;
 echo "</tr>";
  echo "<tr height=40>";
   echo "<td align=left colspan=7></td>" ;
 echo "<th align=right bgcolor=white ><font color=black size=4pt face=Arial> Total Amount:&nbsp;&nbsp;&nbsp;    </font>  </th>";

 echo "<th align=right class='borderexample'><font color=black size=4pt face=Arial>" .round($tnetamount,1) . "</font></th>" ;
 echo "</tr>";
 echo "<tr height=40>";
   echo "<td align=left colspan=7></td>" ;
 echo "<th align=right bgcolor=white ><font color=black size=4pt face=Arial> Discount:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;    </font>  </th>";

 echo "<th align=right class='borderexample'><font color=black size=4pt face=Arial>" .round($discount,1) . "</font></th>" ;
 echo "</tr>";
  echo "<tr height=40>";
  echo "<td align=left colspan=7></td>" ;
 echo "<th align=right bgcolor=white ><font color=black size=4pt face=Arial> Net Amount: &nbsp;&nbsp;&nbsp;&nbsp;   </font>  </th>";

 echo "<th align=right class='borderexample'><font color=black size=4pt face=Arial>" .$gamount . "</font></th>" ;
 echo "</tr>";
 $number = $gamount;
$number1 = $number;
$no = floor($number);
$hundred = null;
$digits_1 = strlen($no); //to find lenght of the number
$i = 0;
// Numbers can stored in array format
$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');
//Extract last digit of number and print corresponding number in words till num becomes 0
while ($i < $digits_1)
{
$divider = ($i == 2) ? 10 : 100;
//Round numbers down to the nearest integer
$number =floor($no % $divider);
$no = floor($no / $divider);
$i +=($divider == 10) ? 1 : 2;

if ($number)
{
$plural = (($counter = count($str)) && $number > 9) ? 's' : 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);
$tresult = implode('', $str); //Join array elements with a string

 echo "<tr>";
   
 echo "<th align=right  colspan=8 ><font color=black size=3pt face=Arial>" ."Amounts in Word:-" .$tresult ."Rs Only" . "</font></th>" ;
 echo "</tr>";
 
 
 echo "<tr>";
 echo "</tr>";
 echo "<tr>";

 echo "</tr>";
  echo "<tr>";
 echo "<td align=left bgcolor=white  colspan=8><font color=black size=1pt face=Arial>" .$notice.     "</font></td>";
 echo "</tr>";

 echo "<tr>";
 echo "<td align=center bgcolor=#000066  colspan=9><font color=white size=3pt face=Arial>This is System Genrated invoice and does not riqure any signatures.</font></td>";
 
 echo "</tr>";
 echo "</table>";
  

	 
 
?>



