<?php
session_start();
include 'connect.php';
if(isset($_SESSION['adminemail']))
{
$user=$_SESSION['adminemail'];

echo '<link href="css/print.css" rel="stylesheet" type="text/css" />';

echo"<style>
@page  
{ 
    size: auto;
    margin: 5mm 0mm 5mm 0mm;
} 
.main
{
min-height:auto;
}
.bottom
{
height:50px;
text-align:center;
}
table
{
font-size:11px;
font-family:arial;
}
table tr,th,td
{
border-color:#999;
}
table tr,td
{
	padding:0 5px;
}
@media print {
    p{page-break-inside: avoid;}	
}

th
{
text-align:center;
vertical-align:middle;
}

</style>";

if(isset($_GET['id']))
	{
		$id=$_GET['id'];
		$digital=$_GET['digital'];
		$copy=$_GET['copy'];
		$check=$_GET['check'];
		
		$gtp=mysql_query("select * from settings");
		$gtp1=mysql_fetch_array($gtp);

		$get=mysql_query("select *,sum(total) as total from invoice where invoiceno='$id'");
		$get1=mysql_fetch_array($get);
		
		$exc=mysql_query("select * from employee where ID='$get1[executive]'");
		$exc1=mysql_fetch_array($exc);
		
		$cus=mysql_query("select * from customer where ID='$get1[customer]'");
		$cus1=mysql_fetch_array($cus);

$vill=mysql_query("select * from villages where ID='$cus1[village]'");
$vill1=mysql_fetch_array($vill);

$tlk=mysql_query("select * from taluk where ID='$cus1[taluk]'");
$tlk1=mysql_fetch_array($tlk);

$dis=mysql_query("select * from districts where ID='$cus1[district]'");
$dis1=mysql_fetch_array($dis);
		
		$dte=$get1['date'];
		
		$d=date("m",strtotime($dte));
		if($d>03)
		{
		$y=date("Y",strtotime($dte));
		$y1=$y+1;		
		
		$dd=date($y);
		$dd1=date($y1);	
		}
		elseif($d<=03)
		{
		$y1=date("Y",strtotime($dte));
		$y=$y1-1;	
		
		$dd=date($y);
		$dd1=date($y1);	
		}
		
		$fin=$dd.'-'.$dd1;
		
		echo'<table border="1" style="width:99%; border-color:#999; border:1px solid #999; margin:17px auto; border-collapse:collapse;">
		
		<tr><td style="padding:0;">
		
		<table style="width:100%; border-collapse:collapse;">
		
		<tr><td style="width:45%; border-right:1px solid #999;">
		
		
		<table style="width:100%;">
		
		<tr><td style="padding:0;">';
		
		$sett=mysql_query("select * from settings");
		$sett1=mysql_fetch_array($sett);
		
		if($sett1['logo']!='')
		{
			echo'<img src="'.$sett1['logo'].'" width="80" height="80" style="margin-right:5px; margin-left:5px;" />';
		}
		
		echo'</td>
		
		<td>
		
		<table style="width:100%;">
		<tr><td><b style="font-size:14px;">GROTEC AGRO PRODUCTS</b></td></tr>';
		
		if($sett1['address']!='')
		{
        echo'<tr><td>'.str_replace("\n","<br />",$sett1['address']).'</td></tr>';
		}

		if($sett1['contact']!='')
		{
		echo'<tr><td>Contact: '.$sett1['contact'].'</td></tr>';
		}
		
		echo'</table>
		
		</td></tr>
		
		</table>
		
		
		</td>
		
		
		<td style="text-align:center; width:25%; border-right:1px solid #999;">
		
		<table style="width:100%; border-collapse:collapse;">
		
		<tr><td style="text-align:center; width:100%;"><b style="font-size:15px;">RETAIL INVOICE</b></td></tr>
		
		</table>
		
		</td>
		
		
		<td style="text-align:center; width:30%;">
		
		<table style="width:100%; border-collapse:collapse;">
		
		<tr><td style="padding-left:5px; padding-top:7px;">GST IN</td><td style="padding-top:7px;">:</td><td style="padding-top:7px;"><b>'.$sett1['gst'].'</b></td></tr>
		
		<tr><td style="padding-left:5px;">Date</td><td>:</td><td><b>'.date('d-m-Y',strtotime($get1['date'])).'</b></td></tr>

		</table>
		
		</td></tr>
		
		</table></td></tr>
		
		
		

		<tr><td style="padding:0; border-bottom:none;">

		<table style="width:100%; border-collapse:collapse;">

		<tr><td style="width:33%;">

		<table style="line-height:18px;">
		
		<tr><td style="padding-left:5px;">Customer Name</td><td style="padding:0 5px 0 0;">:</td><td><b style="font-size:12px;">'.$cus1['name'].'</b></td></tr>
		
		<tr><td style="padding-left:5px;" valign="top">Address</td><td valign="top" style="padding:0 5px 0 0;">:</td><td>';
		
		echo''.$cus1['doorno'].' '.$cus1['street'].'';
		
		if($cus1['doorno']!='' || $cus1['street']!='')
		{
			echo'<br />';
		}
		
		echo''.str_replace("\n","<br />",$vill1['village']).'<br />'.str_replace("\n","<br />",$tlk1['taluk']).'<br />'.str_replace("\n","<br />",$dis1['district']).'';
		
		echo'</td></tr>
		
		<tr><td style="padding-left:5px;" valign="top">Mobile1</td><td valign="top" style="padding:0 5px 0 0;">:</td><td>'.$cus1['mobile1'].'</td></tr>
		
		<tr><td style="padding-left:5px;" valign="top">Mobile 2</td><td valign="top" style="padding:0 5px 0 0;">:</td><td>'.$cus1['mobile2'].'</td></tr>
		
		</table>

		</td>
		
		<td style="width:33%;"></td>
		
		<td style="width:33%; text-align:right; padding:5px 7px 5px 0px;" valign="top">
		
		<table style="border-collapse:collapse; line-height:18px;">
		
		<tr><td style="padding-left:5px; padding-top:7px;">Invoice No</td><td style="padding:0 5px 0 0;">:</td><td style="padding-top:7px;"><b>GT/'.$fin.'/IV'.$get1['invno'].'</b></td></tr>
		
		<tr><td style="padding-left:5px;">Po.No. &amp; Date</td><td style="padding:0 5px 0 0;">:</td><td>'.$get1['pono'].'</td></tr>
		
		<tr><td style="padding-left:5px;">Vehicle No.</td><td style="padding:0 5px 0 0;">:</td><td>'.$get1['vehicleno'].'</td></tr>

		</table>
		
		</td></tr>
		
		</table>
		
		</td></tr>';


		echo'<tr><td style="padding:0; border-top:none; border-bottom:none;">
		
		<table style="width:100%; border-collapse:collapse; border:none; border-color:#999; line-height:27px;" border="1">
		<tr><th style="width:3%; border-left:none;">S.No</th><th style="width:15.5%; border-left:none;">Product</th>
		<th valign="middle" style="width:5%; border-left:none; vertical-align:middle; padding-top:23px; padding-left:5px;">HSN SAC</th><th style="width:8%;">Qty</th>
		<th style="width:5%;">Rate</th><th style="width:7%;">Amount</th>
		<th style="width:5.4%;">Discount</th><th style="width:7%; border-right:none; padding-top:23px; padding-left:5px;">Taxable Amt</th></tr>';
		
		$list=mysql_query("select * from invoice where invoiceno='$id' order by ID ASC") or die("unable to get");
		$sno=0;
		$rate=0;
		$qty=0;
		$tvat=0;
		$cgt=0;
		$sgt=0;
		$igt=0;
		$wtt=0;
		while($list1=mysql_fetch_array($list))
		{
			$sno=$sno+1;
			$total=$list1['total']-$list1['cstval'];
			
			$rt=$list1['rate']*$list1['qty'];
			
			$dis=$list1['discount'];
			
			$rowtotal=$rt-$dis;
			
			$pnm=mysql_query("select * from product where ID='$list1[product]'");
			$pnm1=mysql_fetch_array($pnm);
			
			echo '<tr><td style="border-bottom:none; border-top:none; border-left:none; width:10%;">'.$sno.'.</td>
			<td style="border-bottom:none; border-top:none; width:25%;">'.$pnm1['product'].'</td>
			<td style="border-bottom:none; border-top:none; width:10%;">'.$list1['hsncode'].'</td>
			<td style="border-bottom:none; border-top:none; width:10%; padding-top:17px;">'.$list1['qty'].' <b>'.$pnm1['unit'].'</b></td>
			<td style="border-bottom:none; border-top:none; width:10%; text-align:right;">'.number_format($list1['rate'],2,'.','').'</td>
			<td style="border-bottom:none; border-top:none; width:10%; text-align:right;">'.number_format($rt,2,'.','').'</td>
			<td style="border-bottom:none; border-top:none; width:10%; text-align:right;">'.$list1['discount'].'</td>
			<td style="border-bottom:none; border-top:none; width:12%; text-align:right; border-right:none; padding-right:5px;">'.number_format($rowtotal,2,'.','').'</td></tr>';
			
			$rowt=$rowt+$rt;
			$qty=$qty+$list1['qty'];
			$cgt=$cgt+$list1['cgst_amt'];
			$sgt=$sgt+$list1['sgst_amt'];
			$igt=$igt+$list1['igst_amt'];
			$wtt=$wtt+$rowtotal;
		}
		
		$mx=7;
		
		while($sno<=$mx)
		{

		echo"<tr><td style='height:27px; border-left:none;'></td><td style='line-height:27px;'></td><td style='line-height:27px;'></td><td style='line-height:27px;'></td><td style='line-height:27px;'></td><td style='line-height:27px;'></td><td style='line-height:27px;'></td><td style='line-height:27px; border-right:none;'></td></tr>";

		$sno=$sno+1;

		}

		
		$sub=$get1['total'];
		
		$totgst=$cgt+$sgt+$igt;
		
		echo'<tr style="font-weight:bold;"><td colspan="3" style="height:30px; text-align:right; border-left:none;">Total</td>
		
		<td style="text-align:right; padding-right:5px;">'.$qty.'</td>
		
		<td style="text-align:right;"></td>
		
		<td style="text-align:right;">'.number_format($rowt,2,'.','').'</td>
		
		<td style="text-align:right;"></td><td style="text-align:right; border-right:none; padding-right:5px;">'.number_format($wtt,2,'.','').'</td></tr>';
		
		
		$tot=$get1['total'];

		if($totgst!=0)
		{
		
		$jj=mysql_query("select * from invoice where invoiceno='$id' and (cgst!='0.00' OR sgst!='0.00' OR igst!='0.00')");
		$jj1=mysql_fetch_array($jj);
		
		$sugst=$jj1['cgst']+$jj1['sgst']+$jj1['igst'];
		}
		
		
		echo'<tr><td colspan="4">
		
		
		<table style="width:100%; border:none; height:35px; border-collapse:collapse;">
		
		<tr><td style="padding:11px 3px;"><b>Amount in words :</b> '; $total=number_format($tot,0,'.',''); include 'num_words.php'; echo'</td></tr>
		
		<tr><td style="padding:7px 3px; border-top:1px solid #999;">GST Exempted goods HSN Code-3101</td></tr>';
		
		if($get1['narration']!='')
		{
		echo'<tr><td style="padding:3px 0 0 2px; font-size:10px;"><b>Narration:</b> '.$get1['narration'].'</td></tr>';
		}
			
			
		if($sett1['terms']!='')
		{

		echo'<tr><td style="padding:3px 0 0 2px; font-size:10px; width:70%;"><b>Terms:</b> '.$sett1['terms'].'</td></tr>';
		
		}
		
		echo'</table>
		
		</td><td rowspan="2" colspan="4" style="border-right:none;">
		
		
		
		
		
		<table style="width:50%; border:none;">
		
		<tr><td style="width:50%;">Total Taxable Value : </td><td valign="top">'.number_format($wtt,2,'.','').'</td></tr>';
		
		if($cgt!=0 && $sgt!=0)
		{
		echo"<tr><td style='width:50%;'>
		
		<table style='font-size:9px; border:none; border-collapse:collape;'>
		
		<tr><td>CGST (".number_format($jj1['cgst'],0,'.','')."%) : ".$cgt."</td>
		<td rowspan='2'><img src='assets/images/cl.png' /></td>
		<td rowspan='2'><b style='font-size:10px;'>GST: ".$sugst."% : </b></td></tr>
		
		<tr><td>SGST (".number_format($jj1['sgst'],0,'.','')."%) : ".$sgt."</td></tr>
		
		</table>
		
		</td><td>".number_format($totgst,2,'.','')."</td></tr>";
		}
		elseif($igt!=0)
		{
		echo"<tr><td style='padding-top:7px;'>IGST (".number_format($jj1['igst'],0,'.','')."%) : &nbsp; ".$igt."</td><td style='text-align:right;'>".number_format($totgst,2,'.','')."</td></tr>";
		}
		

		echo'</table>

		<table style="width:70%; border:none; margin-bottom:7px;">
		
		<tr><td style="padding-top:15px; padding-left:5px;"><b style="font-size:13px;">Grand Total : </b></td>
		
		<td style="padding-top:11px; text-align:right;"><img src="assets/images/rupee.png" width="12" height="17" />.&nbsp;<b style="font-size:25px;">'.number_format($tot,0,'.','').'.00</b></td></tr>		
		
		<tr><td></td><td style="padding:5px 3px 2px 0; text-align:right;">E&O.E</td></tr>
		
		</table>
		
		
		</td></tr>
		
		
		
		<tr><td colspan="4" style="width:58%;">
		
		
		<b>Terms &amp; Conditions :</b><br /> ';
		
		$trm=mysql_query("select * from terms order by ID asc limit 1");
		$trm1=mysql_fetch_array($trm);
		
		echo'<span style="line-height:18px;">'.str_replace("\n","<br />",$trm1['terms']).'</span>
		
		
		</td></tr>
		
		
		<tr><td colspan="4">
		
		</td><td colspan="4" style="border-right:none;">
		
		
		<table style="width:100%; border:none;">

		<tr><td style="text-align:center;">For <b>GROTEC AGRO PRODUCTS</b></td></tr>';
		
		if($check==1)
		{

		echo'<tr><td style="text-align:center;">';

		echo'<img src="assets/images/sign.png" width="60" height="82" />';

		echo'</td></tr>';

		}
		
		else
		{
			echo'<tr><td style="height:45px;"></td></tr>';
		}
		
		echo'
		
		<tr><td style="text-align:center;">Authorised Signatory</td></tr>
		
		</table>
		
		
		</td></tr>
		
		
		</table>

		</td></tr>
		
		</table>';

	}
	
	
}
else
{ // Not logged in
  print "<META HTTP-EQUIV = 'Refresh' Content = '1; URL =login.php'>";
}
?>