<?
session_start();
$brcode=$_SESSION['brcode'];
$user=$_SESSION['user'];
if(strlen($brcode)<=0)
{
 print "<script> alert('Session timed out'); parent.document.location='../index.html'; </script>";
 return;
}
include("../opendb.php");
include("../test.php");
if(isset($_POST['show']))
{
?>

<style>
td,th,body{font-size:14;font-family:'arial narrow';letter-spacing:1}
</style>
<center>
<?
$d1=$_REQUEST['d1'];
$m1=$_REQUEST['m1'];
$y1=$_REQUEST['y1'];


$d2=$_REQUEST['d2'];
$m2=$_REQUEST['m2'];
$y2=$_REQUEST['y2'];
 $dfrom=$y1.'-'.$m1.'-'.$d1;
 $dto=$y2.'-'.$m2.'-'.$d2;

$ordby=$_REQUEST['ordby'];

$brres=mysqli_query($db,"select * from branch where brcode='$brcode'");
$brrow=mysqli_fetch_array($brres);
?>
<table>
<TR><TD COLSPAN=2 ALIGN=CENTER><b> PSN AUTOMOTIVE MARKETING P LTD </b><BR>
	 <?print $brrow['braddr'].",".$brrow['brcity'].",<br><u> PH - ".$brrow['brphone']?></u>
TIN: <?print $brrow['tin']?>, GST: <?print $brrow['cstno']?>&nbsp;
</td></tr>
<tr><td COLSPAN=2 ALIGN=CENTER>
<?print "<h4><br>OUTWARD SUPPLY REPORT </H4>";

print " from ".implode('-', array_reverse(explode('-', $dfrom))).' to '.implode('-', array_reverse(explode('-', $dto)));
?>
</td></tr>
</table>

<?
       
    
  
 $tbl="spareinvdw".$brcode.$user;
mysqli_query($db,"drop table $tbl");
mysqli_query($db,"CREATE TABLE $tbl (invno varchar(200),invdate date,cust varchar(400),brcode varchar(22),invtype varchar(33),28b4 double(10,2) default 0, 14sgst double(10,2) default 0, 14cgst double(12,2) default 0,28igst double(12,2) default 0,18b4 double(12,2) default 0,9sgst double(12,2) default 0,9cgst double(12,2) default 0,18igst double(12,2) default 0,12b4 double(12,2) default 0,6sgst double(12,2) default 0,6cgst double(12,2) default 0,12igst double(12,2) default 0,5b4 double(12,2) default 0,2_5sgst double(12,2) default 0,2_5cgst double(12,2) default 0,5igst double(12,2) default 0,0gst double(12,2) default 0,hand double(12,2) default 0,total double(12,2) default 0,salestype varchar(200))");
mysqli_query($db,"delete from $tbl");






//   job card end  ################################################

//   WARANTY start #########################################################

// parts

$sqig=mysqli_query($db,"select invno,invdate,sum(aprice) ap,sum(gstamt) ig,sum(tot) tt from warcouponinvoicepassey  where brcode='$brcode'  and invdate>='$dfrom' and invdate<='$dto' and gstp='28.00' and invtype in('Spare Parts','Lubricants','Local Parts','Outside Material','Service') group by invno");
 while($res2=mysqli_fetch_array($sqig))
{
 $sqig1="insert into $tbl(invno,invdate,brcode,invtype,28b4,28igst,total,salestype) values('$res2[invno]','$res2[invdate]','$brcode','WTY','$res2[ap]','$res2[ig]','$res2[tt]','spare')";
 //print $sqig1;
   mysqli_query($db,$sqig1) or die('saleinv err'.mysqli_error($db));
}

$sqig=mysqli_query($db,"select invno,invdate,sum(aprice) ap,sum(gstamt) ig,sum(tot) tt from warcouponinvoicepassey  where brcode='$brcode'  and invdate>='$dfrom' and invdate<='$dto' and gstp='18.00'  and invtype in('Spare Parts','Lubricants','Local Parts','Outside Material','Service') group by invno");
 while($res2=mysqli_fetch_array($sqig))
{
 $sqig1="insert into $tbl(invno,invdate,brcode,invtype,18b4,18igst,total,salestype) values('$res2[invno]','$res2[invdate]','$brcode','WTY','$res2[ap]','$res2[ig]','$res2[tt]','spare')";
   mysqli_query($db,$sqig1) or die('saleinv err'.mysqli_error($db));
}

$sqig=mysqli_query($db,"select invno,invdate,sum(aprice) ap,sum(gstamt) ig,sum(tot) tt from warcouponinvoicepassey  where brcode='$brcode'  and invdate>='$dfrom' and invdate<='$dto' and gstp='12.00'  and invtype in('Spare Parts','Lubricants','Local Parts','Outside Material','Service') group by invno");
 while($res2=mysqli_fetch_array($sqig))
{
 $sqig1="insert into $tbl(invno,invdate,brcode,invtype,12b4,12igst,total,salestype) values('$res2[invno]','$res2[invdate]','$brcode','WTY','$res2[ap]','$res2[ig]','$res2[tt]','spare')";
   mysqli_query($db,$sqig1) or die('saleinv err'.mysqli_error($db));
}

$sqig=mysqli_query($db,"select invno,invdate,sum(aprice) ap,sum(gstamt) ig,sum(tot) tt from warcouponinvoicepassey  where brcode='$brcode'  and invdate>='$dfrom' and invdate<='$dto' and gstp='5.00'  and invtype in('Spare Parts','Lubricants','Local Parts','Outside Material','Service') group by invno");
 while($res2=mysqli_fetch_array($sqig))
{
 $sqig1="insert into $tbl(invno,invdate,brcode,invtype,5b4,5igst,total,salestype) values('$res2[invno]','$res2[invdate]','$brcode','WTY','$res2[ap]','$res2[ig]','$res2[tt]','spare')";
   mysqli_query($db,$sqig1) or die('saleinv err'.mysqli_error($db));
}

$sqig=mysqli_query($db,"select invno,invdate,sum(aprice) ap,sum(gstamt) ig,sum(tot) tt from warcouponinvoicepassey  where brcode='$brcode'  and invdate>='$dfrom' and invdate<='$dto' and gstp='0.00'  and invtype in('Spare Parts','Lubricants','Local Parts','Outside Material','Service') group by invno");
 while($res2=mysqli_fetch_array($sqig))
{
 $sqig1="insert into $tbl(invno,invdate,brcode,invtype,0gst,total,salestype) values('$res2[invno]','$res2[invdate]','$brcode','WTY','$res2[ap]','$res2[tt]','spare')";
   mysqli_query($db,$sqig1) or die('saleinv err'.mysqli_error($db));
}



// war service
/*

$sqig=mysqli_query($db,"select invno,invdate,sum(aprice) ap,sum(gstamt) ig,sum(tot) tt from warcouponinvoicepassey  where brcode='$brcode'  and invdate>='$dfrom' and invdate<='$dto' and gstp='18.00' and invtype='Service' group by invno");
 while($res2=mysqli_fetch_array($sqig))
{
 $sqig1="insert into $tbl(invno,invdate,brcode,invtype,18b4,18igst,total,salestype) values('$res2[invno]','$res2[invdate]','$brcode','WTY','$res2[ap]','$res2[ig]','$res2[tt]','Labour')";
   mysqli_query($db,$sqig1) or die('saleinv err'.mysqli_error($db));
}


$sqig=mysqli_query($db,"select invno,invdate,sum(aprice) ap,sum(gstamt) ig,sum(tot) tt from warcouponinvoicepassey  where brcode='$brcode'  and invdate>='$dfrom' and invdate<='$dto' and gstp='0.00' and invtype='Service' group by invno");
 while($res2=mysqli_fetch_array($sqig))
{
 $sqig1="insert into $tbl(invno,invdate,brcode,invtype,0gst,total,salestype) values('$res2[invno]','$res2[invdate]','$brcode','WTY','$res2[ap]','$res2[tt]','Labour')";
   mysqli_query($db,$sqig1) or die('saleinv err'.mysqli_error($db));
}

*/
//  Waranty end  #######################################################





//*********** Waranty  print start


//####################################################################


print "<table border=1 cellspacing=1 cellpadding=4 bgcolor='lightsteelblue'>";
print "<tr><th colspan=28><h3>WARANTY INVOICE</th></tr>";
print "<tr><th colspan=7><h3>Invoice Details</th><th colspan=4><h3>28% TAX</th><th colspan=4><h3>18% TAX</th><th colspan=4><h3>12% TAX</th><th colspan=4><h3>5% TAX</th><th colspan=1><h3>0% TAX</th><th colspan=4><h3></th></tr>";

print " <th>Inv No <th>Invoice Date ";
print "<th>GST NO<th>Customer<th>State Code<th>Branch<th>Sales Type<th>Before Value <th>IGST <th>SGST <th>CGST <th>Before Value <th>IGST <th>SGST <th>CGST <th>Before Value <th>IGST <th>SGST <th>CGST <th>Before Value <th>IGST <th>SGST <th>CGST <th>Before Value<th>Hand<th>Total<th>VECV  Ledger Total<th>Difference ";

    $reinvno=mysqli_query($db,"select distinct invno,invdate,cust,salestype from $tbl  where brcode='$brcode' and invtype='WTY' order by invno,invdate")or die('err'.mysqli_error($db));
   $bta5=0;
$cg5=0;
$sg5=0;
$ig5=0;
$bta12=0;
$cg12=0;
$sg12=0;
$ig12=0;
$bta18=0;
$cg18=0;
$sg18=0;
$ig18=0;
$bta28=0;
$cg28=0;
$sg28=0;
$ig28=0;
$bta0=0;
$gtot=0;
    $hand1=0;    
    while($rowinvno=mysqli_fetch_array($reinvno))
{
$re=mysqli_query($db,"select sum(28b4) 28b4,sum(14sgst) 14sgst,sum(14cgst) 14cgst ,sum(28igst) 28igst,sum(18b4) 18b4,sum(9sgst) 9sgst,sum(9cgst) 9cgst ,sum(18igst) 18igst,sum(12b4) 12b4,sum(6sgst) 6sgst,sum(6cgst) 6cgst ,sum(12igst) 12igst,sum(5b4) 5b4,sum(2_5sgst) 2_5sgst,sum(2_5cgst) 2_5cgst ,sum(5igst) 5igst,sum(0gst) 0b4,sum(hand) hand,sum(total) tot from $tbl  where brcode='$brcode'   and invno='$rowinvno[0]' and invtype='WTY' and salestype='$rowinvno[3]' group by invno")or die('err1q'.mysqli_error($db));
   


while($row=mysqli_fetch_array($re))
{
    $cus_result=mysqli_query($db,"select tin from customer where acname = '$row[cust]'")or die  (mysqli_error($db));
	$cus_row=mysqli_fetch_row($cus_result);
	$mtin=$cus_row[0];
	//$btgstamt=$row[3];
	//	$totcs+= $row[tot];
//	$hand1+=$row[hand];
//	$tthand+=$row[hand]; 

	
	
	$s=mysqli_query($db,"select amount from wtyreco where brcode='$brcode'  and invno='$rowinvno[0]' ");
	
     $r=mysqli_fetch_array($s);
	 
	 
    print "\n<tr bgcolor=white><td>$rowinvno[0]</td><td>".implode('-', array_reverse(explode('-', $rowinvno['invdate'])));
    print "<td>".$mtin."</td>";
    print "<td>".strtoupper($rowinvno['cust'])."</td><td></td><td>$brcode</td><td>$rowinvno[3]</td>";
   
    print "<td align=right>".putComa($row[0])."</td><td align=right>".putComa($row[3])."</td>";
    print "<td align=right>".putcoma($row[1])."</td><td align=right>".putComa($row[2])."</td>";
    
    print "<td align=right>".putComa($row[4])."</td><td align=right>".putComa($row[7])."</td>";
    print "<td align=right>".putcoma($row[5])."</td><td align=right>".putComa($row[6])."</td>";
    
    print "<td align=right>".putComa($row[8])."</td><td align=right>".putComa($row[11])."</td>";
    print "<td align=right>".putcoma($row[9])."</td><td align=right>".putComa($row[10])."</td>";
    
    print "<td align=right>".putComa($row[12])."</td><td align=right>".putComa($row[15])."</td>";
    print "<td align=right>".putcoma($row[13])."</td><td align=right>".putComa($row[14])."</td>";
    
    print "<td align=right>".putcoma($row[16])."</td><td align=right>".putComa($row[17])."</td>";
   $diff=$r[0]-round($row[18]);
   $dif+=$diff;
   $ltot+=$r[0];
   print "<td align=right>".putcoma($row[18])."</td><td align=right>".putcoma($r[0])."</td><td align=right>".putcoma($diff)."</td></tr>";
  
   $bta5+=$row[12];
$cg5+=$row[14];
$sg5+=$row[13];
$ig5+=$row[15];

$bta12+=$row[8];
$cg12+=$row[10];
$sg12+=$row[9];
$ig12+=$row[11];

$bta18+=$row[4];
$cg18+=$row[6];
$sg18+=$row[5];
$ig18+=$row[7];

$bta28+=$row[0];
$cg28+=$row[2];
$sg28+=$row[1];
$ig28+=$row[3];

$bta0+=$row[16];
$gtot+=$row[18];
	$hand1+=$row[17]; 
}


}
print "<tr><td colspan=7 align =right>Total</td><td align =right>".putcoma($bta28)."</td><td align =right>".putcoma($ig28)."</td><td align =right>".putcoma($sg28)."</td><td align =right>".putcoma($cg28)."</td>  <td align =right>".putcoma($bta18)."</td><td align =right>".putcoma($ig18)."</td><td align =right>".putcoma($sg18)."</td><td align =right>".putcoma($cg18)."</td>   <td align =right>".putcoma($bta12)."</td><td align =right>".putcoma($ig12)."</td><td align =right>".putcoma($sg12)."</td><td align =right>".putcoma($cg12)."</td>  <td align =right>".putcoma($bta5)."</td><td align =right>".putcoma($ig5)."</td><td align =right>".putcoma($sg5)."</td><td align =right>".putcoma($cg5)."</td> <td align =right>".putcoma($bta0)."</td><td align =right>".putcoma($hand1)."</td><td align =right>".putcoma($gtot)."</td><td align =right>".putcoma($ltot)."</td><td align =right>".putcoma($dif)."</td></td></tr>";
print "</table>";
//   Waranty   print  end



//###########################################################################





//  GST Income print  end

//$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$




return;
}// end if
?>

<link rel="stylesheet" href="../style.css" type="text/css" />
<style>
//	tr{background-color:white}
	th{background-color:steelblue;color:white}
	td{font-size:13}
.right{text-align:right};
</style>

<center>
<h3> Outward Supply Register</H3>
<form action='<?Print $PHP_SELF?>' method='post'>
<table cellspacing=1 cellpadding=1 bgcolor='steelblue'>
<tr><td bgcolor='white' align=center>
<table class=noprint>
<tr><td>Date from </td><td>dd <input type=text name='d1' size=2 value='<?print date("d")?>'>
mm <input type=text name='m1' size=2 value='<?print date("m")?>'>
yy <input type=text name='y1' size=4 value='<?print date("Y")?>'></td></tr>

<tr><td align=right>to </td><td>dd <input type=text name='d2' size=2 value='<?print date("d")?>'>
mm <input type=text name='m2' size=2 value='<?print date("m")?>'>
yy <input type=text name='y2' size=4 value='<?print date("Y")?>'>
</td></tr>
<tr><td colspan=2 align=center bgcolor='lightsteelblue'><input type=submit value='Show' name='show'></td></tr>

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


