<?
session_start();

include_once('includes/top.php');
include_once('opendb.php');
include_once('test.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;
}

$a=0;

//include("../util/exqty.php");
//include ("calcsparestock.php");

$result=mysqli_query($db,"select * from ledger where grp='branches'");

$row=mysqli_fetch_array($result); 


$ker=mysqli_query($db,"select brstate from branch where brcode='$brcode'");
$kar=mysqli_fetch_array($ker);
?>
<style>
element {
    
}
</style>
<title>Spare Order </title>
<script>
function valid()
{

 if(document.f1.customer.selectedIndex<=0)
 {
  alert('Select Branch');
  document.f1.customer.focus();
//  return false;
 }
 if(confirm('Do you want to save?'))
   return true;
 else
   return false;
}
</script>

<form name="f1" action="svinvoice_new.php" method=post onSubmit="return valid()">
<input type=hidden name='saleordno' value='<?print $pono?>'>
<input type=hidden name='invtype' value='<?print $taxtype?>'>
<input type=hidden name='vatorcst' value='<?print $vatorcst?>'>
<center>
<?
$sq="select max(invno) from spareinvoice where brcode='$brcode' ";
//and invtype='$taxtype' and vatorcst='$vatorcst'
$re=mysqli_query($db,$sq);
$prow=mysqli_fetch_row($re);
if($prow[0]<=0)
 $invno=1;
else
 $invno=($prow[0]+1);
$tbl="sparetmp".$brcode.$user;







?>
<link rel="stylesheet" href="../style.css" type="text/css" />

<table border=0 cellspacing=1 cellpadding=3 bgcolor='silver' class="tble1" style="font-family :Calibri;" >
<th>No<th>Branches 

<?
  $ta=mysqli_query($db,"select distinct(led) from ledger  where grp='branches' order by led");
    while($head=mysqli_fetch_row($ta))
    {
$brna=$head[0];


   print "<th >$brna";
}

  
$i=1;
$result=mysqli_query($db,"select distinct(led) from ledger  where grp='branches' order by led");
while($row=mysqli_fetch_array($result))
{


 print "\n<tr bgcolor='white'; >\n<td>".$i."</td><td><input type=text size=50 name='partcode[]' value='$row[0]' size=5></td><td>";

/*
 $result1=mysqli_query($db,"select distinct(led) from ledger  where grp='branches' order by led limit 5");
while($row1=mysqli_fetch_array($result1))
{

$result12=mysqli_query($db,"select sum(dramt)-sum(cramt) from journal2  where brcode='$brcode' and partic='$row1[0]' ");

    
}

*/
  
 print "</tr>";
$i++;
}
print "\n<tr><td colspan=2> <center><b>Total </td><td align=center><input type=text class=right name='totsupqty[]' id='totsupqty' size=5></td>";
print "\n<td align=center>";
print "<input type=text class=right id='tottx' size=5></td>";
print "<td></td><td><input type=text name=ggtot id='ggtot' size=6></td></tr>";

?>
<tr>
<th colspan=18><input type=submit value="Save" name="sv">
<input type=button value='Cancel' onClick=window.close()>
</table>
</form>

<script>
 function calc(x)
 {
 var ele='price'+x; 
 var disele='dis'+x;
 var actele='ap'+x;
 var hndele='hnd'+x;
 var taxele='tax'+x;
 var totele='tot'+x;
 var txamtele='taxamt'+x;
 var focele='foc'+x;
 var warele='war'+x;
 var totsupq='totsupqty';
 var tx='tottx';
 var avail='availqty'+x;

 var cesspele='cessp'+x;
 var cessele='cessamt'+x;

 if(parseFloat(document.getElementById(x).value)>parseFloat(document.getElementById(avail).value) || document.getElementById(avail).value=='')
 {
  // alert('Stock only '+document.getElementById(avail).value);
  //document.getElementById(x).value=0;
  //document.getElementById(x).focus();
  // return false;
 }
 var amt=parseFloat(document.getElementById(ele).value)*(parseFloat(document.getElementById(x).value)); //+parseFloat(document.getElementById(warele).value));
 var disc=amt*parseFloat(document.getElementById(disele).value)/100;
 var actamt=amt-disc;
//document.getElementById(disele).value=parseFloat(disc);

document.getElementById(actele).value=parseFloat(actamt);
<?
if($kar[0]=='Kerala') 
{
?>
var hnd=parseFloat(document.getElementById(hndele).value);
<?
}
else
{
?>
var hnd=parseFloat(document.getElementById(hndele).value);
<?
}
?>
var tx=parseFloat(document.getElementById(taxele).value);
var tax=(actamt*tx)/100;
document.getElementById(txamtele).value=tax.toFixed(2);

var cessp1=document.getElementById(cesspele).value;
var cessam=(cessp1*tax)/100;
document.getElementById(cessele).value=cessam.toFixed(2);
var cs1=cessam; 
document.getElementById(totele).value=(actamt+hnd+tax+cs1).toFixed(2);

tot=0;
for(j=1;j<<?print $i?>;j++)
 {
   var focele1='foc'+j;
   var warele1='war'+j;
//   f=parseFloat(document.getElementById(focele1).value); 
//   w=parseFloat(document.getElementById(warele1).value); 

   t=document.getElementById(j).value; 
   if(t.length<=0) 
     t=0;
   tot=parseFloat(tot)+parseFloat(t); //+f+w;
}

tottax1=0;
gt=0;
for(j=1;j<<?print $i?>;j++)
 {
 var tx='taxamt'+j; 
 var gtot='tot'+j
   t=document.getElementById(tx).value; 
   if(parseFloat(document.getElementById(gtot).value)>0)
   gt=parseFloat(gt)+parseFloat(document.getElementById(gtot).value); 

   if(t.length<=0) 
     t=0;
   tottax1=parseFloat(tottax1)+parseFloat(t);
}

document.getElementById(totsupq).value=parseFloat(tot).toFixed(2);
document.getElementById('tottx').value=parseFloat(tottax1).toFixed(2);
document.getElementById('ggtot').value=parseFloat(gt).toFixed(2);

 }
</script>