<?
session_start();

include_once('includes/top.php');
include_once('opendb.php');
$brcode=$_SESSION['brcode'];
$user=$_SESSION['user'];
$level=$_SESSION['level'];
if(strlen($user)<=0)
{
 print "<script> alert('You are not logged in') </script>";
 include("logout.php");
 return;
}
?>
	<link type="text/css" href="includes/style1.css" rel="stylesheet" />
<link href="css/form.css" type="text/css" rel="stylesheet" media="screen" />
<script>
function valid()
{ 
 subOK="false";
 
if(document.f1.state1.selectedIndex<1)
  {subOK="false"; alert('Invalid state');
      
    return false;  
  }




 x=confirm("Do you want to save?");
 if(x)
  subOK="true";
   if(subOK=="true")
     return true;
   else
     return false;
} 

function chkey(abc)
{
 yy=abc.value
 if(yy.length>35)
 { 
//   alert('Name exeeds the limit');
//   return;
 }

 if(yy.indexOf('  ')>-1) 
  {
    alert('Multiple space is not allowed');
    abc.value='';
  }
 if(yy.indexOf("'")>-1) 
  {
    alert("\' is not allowed");
    abc.value='';
  }
 if(yy.indexOf('"')>-1) 
  {
    alert("\" is not allowed");
    abc.value='';
  }

if(yy.indexOf("&")>-1) 
  {
    alert("& is not allowed");
    abc.value='';
  }

}
</script>

<?
?>


<center>
<form name='f1' method='post' action='<?print $PHP_SELF?>' onSubmit="handleSelectClick();return true;">
<table>
<th colspan=4>LIMIT</th>
<tr><td>LIMIT </td><td><input type="text" name="pcode" VALUE="<?print $pcode?>" style="font-size:10pt;width:34ex;"><br>

</td></tr>
<!-- <tr><td>Part Name </td>
    <td><input type=text name='pname' size=32></td></tr> -->
<th colspan=3><input type=submit value='Search' name='sea'>
</td></tr>
</table>


</form>

<table cellspacing=1 cellpadding=3 class='report'>
<tr><td>Checking with Customer Name</td></tr>    
<th>No. <th>Name <th>total B4amount cs<th>Total bill amt cs<th>branch
<?
$pcode=$_REQUEST['pcode'];
if(isset($_POST['sea']))
{
    $tbl12 = 'tcscheck1'.$brcode;
    mysqli_query($db,"CREATE TABLE $tbl12 (customer varchar(200),pan varchar(20),bef_tax double(10,2) default 0, totamt double(10,2) default 0)");
    mysqli_query($db,"delete from $tbl12");
    mysqli_query($db,"insert into $tbl12 select customer,engineer,sum(aprice),sum(tot) from psn_ampleicher.spareinvoice group by customer");
    mysqli_query($db,"insert into $tbl12 select invno,pan,sum(amt),sum(gtot) from psn_ampleicher.jcardparts group by invno");
    mysqli_query($db,"insert into $tbl12 select custname,remark,amt2,netamt from psn_amplvehicle.veh_invoice, psn_amplvehicle.cust_master where psn_amplvehicle.cust_master.code = psn_amplvehicle.veh_invoice.cust");

    mysqli_query($db,"insert into $tbl12 select customer,msno,sum(aprice),sum(tot) from psn_forcedb2021.spareinvoice where length(msno) > 3 group by msno");
    mysqli_query($db,"insert into $tbl12 select customer,msno,sum(aprice),sum(tot) from psn_forcedb2021.spareinvoice_oil where length(msno) > 3 group by msno");

    $re=mysqli_query($db,"SELECT customer FROM $tbl12 where customer < 'AAAAAAAA' ")or die(mysqli_error($db));
    while($row=mysqli_fetch_array($re))
    {
        $brre=mysqli_query($db,"select cname from psn_ampleicher.jobcard where invno = '$row[0]' limit 1");
        $brrow=mysqli_fetch_row($brre);
        mysqli_query($db,"update $tbl1 set customer = '$brrow[0]' where invno = '$row[0]' ");
    }
    

    $re=mysqli_query($db,"SELECT  customer,sum(bef_tax),sum(totamt) FROM $tbl12 group by customer HAVING sum(totamt)>'$pcode'")or die(mysqli_error($db));
    $i=1;
    while($row=mysqli_fetch_array($re))
    {
        print "\n<tr bgcolor=aliceblue><td>".$i++."</td><td>$row[0]</td><td>$row[1]</td><td>$row[2]</td>";
        print"</tr>";
    
    }
    print   "</table><br>";
    print "<tr><td>Checking with PAN</td></tr>";
    print "<table cellspacing=1 cellpadding=3 class='report'>";
    print "<th>No. <th>Name <th>PAN<th>B4Tax Total<th>Total Bill Amt cs<th>branch";
    
    $tbl1 = 'tcscheck'.$brcode;
    mysqli_query($db,"CREATE TABLE $tbl1 (customer varchar(200),pan varchar(20),bef_tax double(10,2) default 0, totamt double(10,2) default 0)");
    mysqli_query($db,"delete from $tbl1");
    mysqli_query($db,"insert into $tbl1 select customer,engineer,sum(aprice),sum(tot) from psn_ampleicher.spareinvoice where length(engineer) > 3 group by engineer");
    mysqli_query($db,"insert into $tbl1 select pan,pan,sum(amt),sum(gtot) from psn_ampleicher.jcardparts where length(pan) > 3 group by pan ");
    mysqli_query($db,"insert into $tbl1 select custname,remark,amt2,netamt from psn_amplvehicle.veh_invoice, psn_amplvehicle.cust_master where psn_amplvehicle.cust_master.code = psn_amplvehicle.veh_invoice.cust");
    mysqli_query($db,"insert into $tbl1 select customer,msno,sum(aprice),sum(tot) from psn_forcedb2021.spareinvoice where length(msno) > 3 group by msno");
    mysqli_query($db,"insert into $tbl1 select customer,msno,sum(aprice),sum(tot) from psn_forcedb2021.spareinvoice_oil where length(msno) > 3 group by msno");

    $re=mysqli_query($db,"SELECT  customer,pan,sum(bef_tax),sum(totamt) from $tbl1 group by pan HAVING sum(totamt)>'$pcode' ")or die(mysqli_error($db));
    $i=1;
    while($row=mysqli_fetch_array($re))
    {
        print "\n<tr bgcolor=aliceblue><td>".$i++."</td><td>$row[customer]</td><td>$row[1]</td><td>$row[2]</td><td>$row[3]</td>";
        print"</tr>";
    }
    print   "</table><br>";
}// end if sea
function getBranch($cde)
{
 $brre=mysqli_query($db,"select brname from branch where brcode='$cde'");
 $brrow=mysqli_fetch_row($brre);
 return $brrow[0];
}
?>

<?

?>
</table>

<script>


function del(gstcode)
{
if(confirm("Do you want to delete GST "+gstcode+" ?"))
 document.location="delgstmas.php?gstcode="+gstcode;

}
</script>


