<?
session_start();
$brcode = $_SESSION['brcode'];
if(strlen($brcode)<=0)
{
 print "<script> alert('Session timed out'); parent.document.location='../ind.php'; </script>";
 return;
}
include("../opendb.php");
include("../test.php");
?>
<style>
body,td{font-family:'arial narrow';letter-spacing:1;font-size:13}
th{background-color:c1d799}
.mnu{margin-left:5}
body{BACKGROUND-COLOR:fcfff1}
</style>
<H3>GST SALES REGISTER</H3>
<form action='<?print $PHP_SELF?>' method='POST'>
<table bgcolor='silver'>
<tr><td> Date from </td><td>dd<input type=text name='d' size=2 value='<?print date("d",time())?>'>
				mm<input type=text name='m' size=2 value='<?print date("m",time())?>'>
				yy<input type=text name='y' size=4 value='<?print date("Y",time())?>'>
			</td></tr>

<tr><td> To </td><td>dd<input type=text name='d1' size=2 value='<?print date("d",time())?>'>
				mm<input type=text name='m1' size=2 value='<?print date("m",time())?>'>
				yy<input type=text name='y1' size=4 value='<?print date("Y",time())?>'>
			</td>
</tr>
<tr><td>Type</td><td><select name='tp'><option>All</option>
<option>INTRASTATE</option>
<option>INTERSTATE</option>
</select>
</td></tr>
<tr><td colspan=2 align=center><input type=submit name='show' value='Show'>
<input type=submit name='prn' value='Print'>
</td></tr>
</table>
</form>
<?
if(isset($_POST['prn']))
{
        $d = $_REQUEST['d'];
$m = $_REQUEST['m'];
$y = $_REQUEST['y'];
$d1 = $_REQUEST['d1'];
$m1 = $_REQUEST['m1'];
$y1 = $_REQUEST['y1'];
$tp = $_REQUEST['tp'];
  	$df=$y.'-'.$m.'-'.$d;
	$dt=$y1.'-'.$m1.'-'.$d1;
       print "<script> window.open('salereg_newgstprn.php?df=$df&dt=$dt&tp=$tp');</script>";
}
if(isset($_POST['show']))
{
    $d = $_REQUEST['d'];
$m = $_REQUEST['m'];
$y = $_REQUEST['y'];
$d1 = $_REQUEST['d1'];
$m1 = $_REQUEST['m1'];
$y1 = $_REQUEST['y1'];
$tp = $_REQUEST['tp'];
   	$df=$y.'-'.$m.'-'.$d;
   	$dt=$y1.'-'.$m1.'-'.$d1;
	if($tp=='All')
	{
   		$result=mysqli_query($db,"select * from veh_invoice where invdate>='$df' and invdate<='$dt' and brcode='$brcode' and type!='trans-out' order by taxp,invno") or die(mysqli_error());
   		//print "select * from veh_invoice where invdate>='$df' and invdate<='$dt' and brcode='$brcode' and type!='trans-out' order by taxp,invno";
	}
	else
	{
   		$result=mysqli_query($db,"select * from veh_invoice where invdate>='$df' and invdate<='$dt' and brcode='$brcode' and type!='trans-out' and tp='$tp' order by taxp,invno") or die(mysqli_error());
	}
   	if(mysqli_num_rows($result)>0)
   	{
 		print "<b>Type = ".$tp;
		?>
		<table bgcolor=silver cellspacing=1 cellpadding=1>
		<th>No.<th>Ledger</th><th>To <th>Finance<th>GSTIN<th>PAN No.<th>Inv NO.<th>Inv. Date <th>DA <th>Eng.No<th>Ch.No<th>HSNCode <th>Config<th>Before Tax<th>CGST%<th>CGST Amt<th>SGST%<th>SGST Amt<th>IGST%<th>IGST Amt<th>Tax Type<th>Net Amount<th>Cess Amt<th>TCS<th>Total
		<?
		$i=1;
		while($row=mysqli_fetch_array($result))
     		{
 			if($row['type']=='-' || $row['type']=='sale-ret' || strlen($row['da'])<=0 )
  			{ 
   				$cus_result=mysqli_query($db,"select custname,tin,addr1,kgst,acname from cust_master where code='$row[cust]' or custname='$row[cust]'")or die  (mysqli_error());
				$cus_row=mysqli_fetch_row($cus_result);
    				$cust=$cus_row[0].", ".$cus_row[2];
				if($cust==', ')
					$cust = $row['cust'];				
				$ledger = $cus_row[4];
  			}
	 		else if($row['type']=='trans-out')
 			{
   				$cus_result=mysqli_query($db,"select brname from br_master where code='$row[cust]'")or die  (mysqli_error());
    				$cus_row=mysqli_fetch_row($cus_result);
    				$cust="<font color='brown'>".$cus_row[0];
			}  
			else
 				$cust="<font color='green'>".$row[0];
			if($row['area']=='DEALER')
			{
				$cus_result=mysqli_query($db,"select supname,tin,addr1 from sup_master where code='$row[cust]' ")or die  (mysqli_error());
				$cus_row=mysqli_fetch_row($cus_result);
    				$cust=$cus_row[0].", ".$cus_row[2];
				if($cust==', ')
					$cust = $row['cust'];				
			}
			$gst_no = $cus_row[1];
			if($gst_no<='')
			{
				//$cust = "<font color='green'>".$row[cust];
			       $r=mysqli_query($db,"select * from sup_master where supname='$row[cust]'")or die(mysqli_error());
				//print "select * from sup_master where supname='$row[cust]'";
			       $cus_row12=mysqli_fetch_array($r);
				$gst_no = $cus_row12[10];
			}
			$rrcat=mysqli_query($db,"select cat from veh_master where config='$row[config]' and brcode='$brcode'");
			$rowcat=mysqli_fetch_row($rrcat);			
			if(strlen($row['da'])<=0)
			{
			    print "<tr bgcolor='#fee0da'>";
			}
			else
			{
			    print "<tr bgcolor=white>";
			}
      			print "<td align=center>".$i++."</td><td>$ledger</td><td>$cust</td><td>$row[fin]</td><td>$gst_no</td><td>$cus_row[3]</td>";
			print "<td align=center>$row[invno]</td>";

      			print "<td>".implode('-', array_reverse(explode('-', $row['invdate'])))."</td><td>$row[da]</td><td>$row[engno]</td><td>$row[chasis]</td><td>$row[hsncode]</td><td>$row[config]</td><td align=right>$row[amt2]</td>";
			$bef_tcs = $row['amt2']+$row['cgstamt']+$row['sgstamt']+$row['igstamt'];
      			print "<td align=right>$row[cgstp]</td><td align=right>$row[cgstamt]</td><td align=right>$row[sgstp]</td><td align=right>$row[sgstamt]</td><td align=right>$row[igstp]</td><td align=right>$row[igstamt]</td><td align=right>$row[taxtype]</td><td align=right>$bef_tcs</td><td>$row[cessamt]</td><td>$row[tcsamt]</td><td>$row[netamt]</td></tr>";
      			
    		}
	}
	else
	 	print "Record does not exist";
	print "</table>";
	$result=mysqli_query($db,"select * from invoice_item where invdate>='$df' and invdate<='$dt' and brcode='$brcode' order by taxp,invno") or die(mysqli_error());
   	if(mysqli_num_rows($result)>0)
   	{
		?>
		<table bgcolor=silver cellspacing=1 cellpadding=1>
		<th>No.<th>Customer<th>GSTIN<th>Inv NO.<th>Inv. Date <th>HSNCode <th>Before Tax<th>CGST%<th>CGST Amt<th>SGST%<th>SGST Amt<th>IGST%<th>IGST Amt<th>TOTAL
		<?
		$i=1;
		while($row=mysqli_fetch_array($result))
     		{
			$cus_result=mysqli_query($db,"select custname,tin,addr1,kgst from cust_master where code='$row[cust]' or custname='$row[soldto]'")or die  (mysqli_error());
			$cus_row=mysqli_fetch_row($cus_result);
			$gst_no = $cus_row[1];
			$cgstp=$row['taxp']/2;
			$sgstp=$row['taxp']/2;
			$cgstamt=$row['taxamt']/2;
			$sgstamt=$row['taxamt']/2;
			$mtot = round($row['net']+$cgstamt+$sgstamt,2);
			print "<tr><td>".$i++."<td>$row[soldto]</td><td>".$gst_no."</td><td>T0000".$row['invno']."</td><td>".implode('-', array_reverse(explode('-', $row['invdate'])))."</td><td>90318000</td><td align='right'>".$row['net']."</td>";
			print "<td>".$cgstp."</td><td>".$cgstamt."</td><td>".$sgstp."</td><td>".$sgstamt."</td><td></td><td></td><td>".$mtot."</td>";
		}
	}
}
?>
</table>