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

$type=$_REQUEST['type'];
$fromdt=$_REQUEST['fromdt'];
$todt=$_REQUEST['todt'];



?>
<html>
<body >
<title>Demo</title>

<style type="text/css">
table,tr,td{border-collapse:collapse;border: 1px solid black;}
body{font-family:Verdana;font-size:8px;color:black;}
td{font-family:Verdana;font-size:10px;color:black;height:20px;}
th{font-family:Verdana;font-size:11px;color:black;}
select,input[type=text], textarea{  
background: #eeeeee; /* Old browsers */
/* IE9 SVG, needs conditional override of 'filter' to 'none' */
background: url(data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiA/Pgo8c3ZnIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgd2lkdGg9IjEwMCUiIGhlaWdodD0iMTAwJSIgdmlld0JveD0iMCAwIDEgMSIgcHJlc2VydmVBc3BlY3RSYXRpbz0ibm9uZSI+CiAgPGxpbmVhckdyYWRpZW50IGlkPSJncmFkLXVjZ2ctZ2VuZXJhdGVkIiBncmFkaWVudFVuaXRzPSJ1c2VyU3BhY2VPblVzZSIgeDE9IjAlIiB5MT0iMCUiIHgyPSIwJSIgeTI9IjEwMCUiPgogICAgPHN0b3Agb2Zmc2V0PSIwJSIgc3RvcC1jb2xvcj0iI2VlZWVlZSIgc3RvcC1vcGFjaXR5PSIxIi8+CiAgICA8c3RvcCBvZmZzZXQ9IjIzJSIgc3RvcC1jb2xvcj0iI2ZmZmZmZiIgc3RvcC1vcGFjaXR5PSIxIi8+CiAgPC9saW5lYXJHcmFkaWVudD4KICA8cmVjdCB4PSIwIiB5PSIwIiB3aWR0aD0iMSIgaGVpZ2h0PSIxIiBmaWxsPSJ1cmwoI2dyYWQtdWNnZy1nZW5lcmF0ZWQpIiAvPgo8L3N2Zz4=);
background: -moz-linear-gradient(top, #eeeeee 0%, #ffffff 23%); /* FF3.6+ */
background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,#eeeeee), color-stop(23%,#ffffff)); /* Chrome,Safari4+ */
background: -webkit-linear-gradient(top, #eeeeee 0%,#ffffff 23%); /* Chrome10+,Safari5.1+ */
background: -o-linear-gradient(top, #eeeeee 0%,#ffffff 23%); /* Opera 11.10+ */
background: -ms-linear-gradient(top, #eeeeee 0%,#ffffff 23%); /* IE10+ */
background: linear-gradient(top, #eeeeee 0%,#ffffff 23%); /* W3C */
filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#eeeeee', endColorstr='#ffffff',GradientType=0 ); /* IE6-8 */
border:1px solid #999; padding:3px;
-webkit-border-radius: 3px;
-moz-border-radius: 3px;
border-radius: 3px;


}
p{padding:0px;}
em{font-size:14px;}
h2{color:#000;font-size:14px;margin:0px;padding:5px 20px;}

</style>
<body onload='window.print();'>


<?

$df=$fromdt;
$dt=$todt;

if($type=='Cash'){
 $typ='CASH';
 
}
if($type=='Credit'){
 $typ='CREDIT';
 $typ_name='Insurance';
}
print "<table cellspacing=1 cellpadding=5 width='100%' bgcolor='white' border='1' style='border-collapse:collapse;'>";
if($type=='Itemcode')
{
print "<tr ><td align=center colspan='10' ><h2><b><u>".$company."</b> <br>";
}
else if($type=='Salesman')
{
print "<tr ><td align=center colspan='10' ><h2><b><u>".$company."</b> <br>";
}
else
{
print "<tr ><td align=center colspan='6' ><h2><b><u>".$company."</b> <br>";

}
?>
<b>Invoice Register:&nbsp;&nbsp;&nbsp;  <? print datetohtml($df) ?>&nbspto <?print datetohtml($dt)?> </h2></b></td></tr> 
<?
if($type=='Itemcode')
{
print  "<tr><th align=left>Sl no<th align=left>Entry date<th align=left>Invno<th align=left>Customer<th align=left>Partcode<th align=left>PartName<th align=left>Qty<th align=right>Rate<th align=right>Discount<th align=right>Net Amt</tr>";
}
else if($type=='Salesman')
{
print  "<tr><th align=left>Sl no<th align=left>Entry date<th align=left>Invno<th align=left>Customer<th align=left>Partcode<th align=left>PartName<th align=left>Qty<th align=right>Rate<th align=right>Discount<th align=right>Net Amt</tr>";

}
else
{
print  "<tr><th align=left>Sl no<th align=left>Entry date<th align=left>Invno<th align=left>Customer<th align=left>Payment Mode<th align=right>Net Amt</tr>";
}
//$sql=mysqli_query($db,"select distinct(invno),sum(amt),date,inscomp,typ,pmode from jcardinvoice where date between '$df' and '$dt' and brcode='$brcode' and typ='$typ' group by invno order by pmode,invno asc");
if($type=='All'){
//$sql=mysqli_query($db,"select distinct(invno),sum(amt),date,inscomp,typ,pmode from jcardinvoice where date between '$df' and '$dt' and brcode='$brcode' group by invno order by pmode,invno asc");
$sql=mysqli_query($db,"select distinct(invno),sum(total),invdate,customer,pmode,item from workshop_invoice where invdate between '$df' and '$dt' and brcode='$brcode' and pmode='CREDIT' group by invno order by invno asc");
$sql1=mysqli_query($db,"select distinct(invno),sum(total),invdate,customer,pmode,item from workshop_invoicecsh where invdate between '$df' and '$dt' and brcode='$brcode' and pmode='CASH' group by invno order by invno asc");

}
if($type=='Salesman')
{
print "<tr><td colspan='10' align=center><b><font size=14>CREDIT</font></b></td></tr>"; 
$query1=mysqli_query($db,"select distinct(salesman) from workshop_invoice where invdate between '$df' and '$dt' and brcode='$brcode' order by salesman asc");
$query2=mysqli_query($db,"select distinct(salesman) from workshop_invoicecsh where invdate between '$df' and '$dt' and brcode='$brcode' order by salesman asc");
while($row1=mysqli_fetch_array($query1))
{

$i=1;
$sql1=mysqli_query($db,"select invno,total,invdate,customer,pmode,item,item_code,qty,unitprice,dis from workshop_invoice where invdate between '$df' and '$dt' and brcode='$brcode' and pmode='CREDIT' and salesman='$row1[salesman]' order by salesman asc");
while($row=mysqli_fetch_array($sql1))
{
 


$total+=$row[1];

print "<tr><td>".$i."</td><td>".datetoHtml($row[2])."</td>";
print "<td>$row[0]</td><td>$row[3]</td>";
print "<td>$row[item_code]</td><td>$row[item]</td><td align='right'>$row[qty]</td><td align='right'>$row[unitprice]</td><td align='right'>$row[dis]</td>";
print "<td align='right'>$row[1]</td></tr>";
$tot_amt1+=$row[1];
$i++;
$pre_pmode=$row[5];
$tot_amt1=number_format($tot_amt1, 3);
$qtyrt1+=$row[qty]*$row[unitprice];
$qtyrt1=number_format($qtyrt1, 3);
$totdis1+=$row[dis];
$totdis1=number_format($totdis1, 3);
}
//print "<tr><td align='right' colspan='10'><b>".$tot_amt1."</b></td></tr>"; 
print "<tr><td colspan='7'><b>".$row1[0]."</td><td align='right'><b>".$qtyrt1."</b></td><td align='right'><b>".$totdis1."</b></td><td align='right' ><b>".$tot_amt1."</b></td></tr>"; 


}
//print "<br>";
print "<tr><td colspan='10' align=center><b><font size=14>CASH</font></b></td></tr>"; 
while($row2=mysqli_fetch_array($query2))
{
//$qry1="select invno,total,invdate,customer,pmode,item,item_code,qty,unitprice,dis from workshop_invoicecsh where invdate between '$df' and '$dt' and brcode='$brcode' and pmode='CASH' and salesman='$row2[0]' order by salesman asc";

//echo $qry1;
//echo "<br>";
$sql2=mysqli_query($db,"select invno,total,invdate,customer,pmode,item,item_code,qty,unitprice,dis from workshop_invoicecsh where invdate between '$df' and '$dt' and brcode='$brcode' and pmode='CASH' and salesman='$row2[0]' order by salesman asc");




while($row=mysqli_fetch_array($sql2))
{
 


$total+=$row[1];

print "<tr><td>".$i."</td><td>".datetoHtml($row[2])."</td>";
print "<td>$row[0]</td><td>$row[3]</td>";
print "<td>$row[item_code]</td><td>$row[item]</td><td align='right'>$row[qty]</td><td align='right'>$row[unitprice]</td><td align='right'>$row[dis]</td>";
print "<td align='right'>$row[1]</td></tr>";
$tot_amt1+=$row[1];
$i++;
$pre_pmode=$row[5];
$tot_amt1=number_format($tot_amt1, 3);
$qtyrt1+=$row[qty]*$row[unitprice];
$qtyrt1=number_format($qtyrt1, 3);
$totdis1+=$row[dis];
$totdis1=number_format($totdis1, 3);
}
//print "<tr><td align='right' colspan='10'><b>".$tot_amt1."</b></td></tr>"; 
//print "<tr><td align='right' colspan='8'><b>".$qtyrt1."</b></td><td align='right'><b>".$totdis1."</b></td><td align='right' ><b>".$tot_amt1."</b></td></tr>"; 

print "<tr><td colspan='7'><b>".$row2[0]."</td><td align='right'><b>".$qtyrt1."</b></td><td align='right'><b>".$totdis1."</b></td><td align='right' ><b>".$tot_amt1."</b></td></tr>"; 
















}
//$query="select invno,total,invdate,customer,pmode,item,salesman from workshop_invoice where invdate between '$df' and '$dt' and brcode='$brcode' and pmode='CREDIT' order by salesman asc";
//echo $query; 
//$sql1=mysqli_query($db,"select invno,sum(total),invdate,customer,pmode,item,salesman from workshop_invoicecsh where invdate between '$df' and '$dt' and brcode='$brcode' and pmode='CASH' order by salesman asc");


}
if($type=='Itemcode')
{
//$sql=mysqli_query($db,"select distinct(invno),sum(total),invdate,customer,pmode,item from workshop_invoice where invdate between '$df' and '$dt' and brcode='$brcode' group by invno order by invno asc");
$sql=mysqli_query($db,"select invno,total,invdate,customer,pmode,item,item_code,qty,unitprice,dis from workshop_invoice where invdate between '$df' and '$dt' and pmode='CREDIT' and brcode='$brcode' order by invno asc");
$sql1=mysqli_query($db,"select invno,total,invdate,customer,pmode,item,item_code,qty,unitprice,dis from workshop_invoicecsh where invdate between '$df' and '$dt' and pmode='CASH' and brcode='$brcode' order by invno asc");

}
if($type=='Cash')
{
$sql=mysqli_query($db,"select distinct(invno),sum(total),invdate,customer,pmode,item from workshop_invoicecsh where invdate between '$df' and '$dt' and brcode='$brcode' and pmode='CASH' group by invno order by invno asc");
}
if($type=='Credit')
{
$sql=mysqli_query($db,"select distinct(invno),sum(total),invdate,customer,pmode,item from workshop_invoice where invdate between '$df' and '$dt' and brcode='$brcode' and pmode='CREDIT' group by invno order by invno asc");
}
$i=1;
$tot_amt=0;
$total=0;
$pre_pmode='x';
if($type=='All')
{

while($row=mysqli_fetch_array($sql))
{
 


$total+=$row[1];

print "<tr><td>".$i."</td><td>".datetoHtml($row[2])."</td>";
print "<td>$row[0]</td><td>$row[3]</td>";
print "<td>$row[pmode]</td>";
print "<td align='right'>$row[1]</td></tr>";
$tot_amt1+=$row[1];
$i++;
$pre_pmode=$row[5];
$tot_amt1=number_format($tot_amt1, 3);
}
print "<tr><td align='right' colspan='6'><b>".$tot_amt1."</b></td></tr>"; 
while($row=mysqli_fetch_array($sql1))
{
 


$total+=$row[1];
if($row[4]=='cus')
$typ_name='Private';
if($row[4]=='ins')
$typ_name='Insurance';
print "<tr><td>".$i."</td><td>".datetoHtml($row[2])."</td>";
print "<td>$row[0]</td><td>$row[3]</td>";
print "<td>$row[pmode]</td>";
print "<td align='right'>$row[1]</td></tr>";
$tot_amt2+=$row[1];
$i++;
$pre_pmode=$row[5];
$tot_amt2=number_format($tot_amt2, 3);
}
print "<tr><td align='right' colspan='6'><b>".$tot_amt2."</b></td></tr>"; 

$tot_amt=$tot_amt1+$tot_amt2;
$tot_amt=number_format($tot_amt, 3);
}
else if($type=='Itemcode')
{

while($row=mysqli_fetch_array($sql))
{
 


$total+=$row[1];

print "<tr><td>".$i."</td><td>".datetoHtml($row[2])."</td>";
print "<td>$row[0]</td><td>$row[3]</td>";
print "<td>$row[item_code]</td><td>$row[item]</td><td align='right'>$row[qty]</td><td align='right'>$row[unitprice]</td><td align='right'>$row[dis]</td>";
print "<td align='right'>$row[1]</td></tr>";
$tot_amt1+=$row[1];
$i++;
$pre_pmode=$row[5];
$tot_amt1=number_format($tot_amt1, 3);
$qtyrt1+=$row[qty]*$row[unitprice];
$qtyrt1=number_format($qtyrt1, 3);
$totdis1+=$row[dis];
$totdis1=number_format($totdis1, 3);
}
//print "<tr><td align='right' colspan='10'><b>".$tot_amt1."</b></td></tr>"; 
print "<tr><td align='right' colspan='8'><b>".$qtyrt1."</b></td><td align='right'><b>".$totdis1."</b></td><td align='right' ><b>".$tot_amt1."</b></td></tr>"; 
while($row=mysqli_fetch_array($sql1))
{
 


$total+=$row[1];
if($row[4]=='cus')
$typ_name='Private';
if($row[4]=='ins')
$typ_name='Insurance';
print "<tr><td>".$i."</td><td>".datetoHtml($row[2])."</td>";
print "<td>$row[0]</td><td>$row[3]</td>";
print "<td>$row[item_code]</td><td>$row[item]</td><td align='right'>$row[qty]</td><td align='right'>$row[unitprice]</td><td align='right'>$row[dis]</td>";
print "<td align='right'>$row[1]</td></tr>";
$tot_amt2+=$row[1];
$i++;
$pre_pmode=$row[5];
$tot_amt2=number_format($tot_amt2, 3);
$qtyrt2+=$row[qty]*$row[unitprice];
$qtyrt2=number_format($qtyrt2, 3);
$totdis2+=$row[dis];
$totdis2=number_format($totdis2, 3);
}
print "<tr><td align='right' colspan='8'><b>".$qtyrt2."</b></td><td align='right'><b>".$totdis2."</b></td><td align='right' colspan='8'><b>".$tot_amt2."</b></td></tr>"; 

$tot_amt=$tot_amt1+$tot_amt2;
$tot_amt=number_format($tot_amt, 3);
$totqtyrt=$qtyrt1+$qtyrt2;
$totdis=$totdis1+$totdis2;
}
else if($type=='Salesman')
{





}

else
{

while($row=mysqli_fetch_array($sql))
{
 


$total+=$row[1];

print "<tr><td>".$i."</td><td>".datetoHtml($row[2])."</td>";
print "<td>$row[0]</td><td>$row[3]</td>";
print "<td>$row[pmode]</td>";
print "<td align='right'>$row[1]</td></tr>";
$tot_amt+=$row[1];
$i++;
$pre_pmode=$row[5];
$tot_amt=number_format($tot_amt, 3);
}
}
//print "<tr><td align='right' colspan='6'><b>".putcoma($total)."</b></td></tr>"; 
if($type=='Itemcode')
{
print "<tr><td colspan='7' align='right'><b>Grand Total:</b></td><td align='right'><b>".putcoma($totqtyrt)."</b></td><td align='right'><b>".putcoma($totdis)."</b></td><td align='right'><b>".putcoma($tot_amt)."</b></td></tr>";
//print "<tr><td colspan='9' align='right'><b>Grand Total:</b></td><td align='right'><b>".putcoma($tot_amt)."</b></td></tr>";
}
else
{
print "<tr><td colspan='5' align='right'><b>Grand Total:</b></td><td align='right'><b>".putcoma($tot_amt)."</b></td></tr>";
}

print "</table>";
?>

