<?
session_start();
$brcode=$_SESSION['brcode'];
$user=$_SESSION['user'];
$partcode=$_REQUEST['partcode'];
$partname=$_REQUEST['partname'];
if(strlen($brcode)<=0)
{
 print "<script> alert('Session timed out'); parent.document.location='../index.html'; </script>";
 return;
}

include_once('includes/top.php');

include("../opendb.php");
include("../test.php");
include("../util/exqty.php");
$ker=mysqli_query($db,"select brstate from branch where brcode='$brcode'");
$kar=mysqli_fetch_array($ker);
$k=$kar[0];
?>
<link href="../css/form.css" type="text/css" rel="stylesheet" media="screen"  />
<html>
<body >
<script>
var msn=[];
msn[0]='';
function getmsno()
{
document.f1.msno.value=msn[document.f1.to.selectedIndex];
}
function valid()
{
 if(confirm('Do you want to save?'))
   return true;
 else
   return false;
}
</script>
<?
?>
<style>
	tr{background-color:white}
	th{background-color:steelblue;color:white}
</style>
<center>
<form name='f1' method='post' action='<?print $PHP_SELF?>' onSubmit='return valid()'>
<input type=hidden name='code' value='<?print $partname?>'>
<table border=1 width=100%>
<tr><td><center>Customer : 
<?
?>
&nbsp;MSNO:
<input type=text name="msno" size=35 value='<?print $code?>'> <br>
<input type=submit value='Save & Print' name='save'>
</td></tr></table>
</form>

<br>
<?
$code=$_REQUEST['code'];
$msno=$_REQUEST['msno'];
if(isset($_POST['save']))
{

$ca=$code;

if(strlen($ca)<=0)
{
//print 'invalid customer';
//return;
}

$tbl='invotmpdc'.$brcode.$user;
$re=mysqli_query($db,"select * from $tbl");
$dt=date("Y-m-d");
$pres=mysqli_query($db,"select max(orderno) from saleorder_sparedc where brcode='$brcode'");
$prow=mysqli_fetch_row($pres);
if($prow[0]<=0)
 $orderno=1;
else
 $orderno=($prow[0]+1);

while($ro=mysqli_fetch_array($re))
{
  $sql="insert into saleorder_sparedc (partcode,partname,qty,date,orderto,brcode,orderno,sqty,msno,closed,reas) values('$ro[0]','$ro[1]','$ro[2]','$dt','$ro[5]','$brcode','$orderno','$ro[3]','$ro[6]','N','-')";


 mysqli_query($db,$sql) or die(mysqli_error($db));
}
mysqli_query($db,"delete from $tbl");
//print "<script> window.open('prnsaleorderspare1dc.php?orderno=$orderno'); </script>";
print "Data Saved";
 //print "<script> alert('Data Saved....!!!');document.location='invindexnew1dc.html';</script>";

}




$tbl='invotmpdc'.$brcode.$user;
$sql="create table $tbl (partcode varchar(100),prtname varchar(200), qty double(14,2), rate double(14,2), brcode varchar(100), cust varchar(100), hsn varchar(100))";
//print "sql:".$sql;
//$sql="create table $tbl (partcode varchar(100),prtname varchar(200), qty double(14,2),sqty double(14,2), price double(14,2), discount double(14,2), hand double(14,2), cst double(14,2), tot double(14,2), brcode varchar(100))";

mysqli_query($db,$sql);
if(isset($_POST['add']))
{
$partcode=$_REQUEST['partcode'];
$sqty=$_REQUEST['sqty'];
$cust=$_REQUEST['cust'];
$partname=$_REQUEST['partname'];
$rate=$_REQUEST['rate'];
$hsn=$_REQUEST['hsn'];

  $res1=mysqli_query($db,"select * from $tbl where partcode='$partcode'");
  if(mysqli_num_rows($res1)>0)
  {
    print "<script> alert('Duplicate Entry')</script>";
    return;
  }

/*$res2=mysqli_query($db,"select * from partmaster where partcode='$partcode' and hsncode!='NULL'");
$r=mysqli_fetch_array($res2);
$prtname=$r[partname];

$qty=exqty1($partcode,$brcode,$user);
*/
// $qty='1';

//if(mysqli_num_rows($res2)>0)
  //{
  $sql="insert into $tbl values('$partcode','$partname','$sqty','$rate','$brcode','$cust','$hsn')";
//print $sql;
 mysqli_query($db,$sql) or die(mysqli_error($db));
  //}
 /* else
  {
     print "<script> alert('hsn code and tax not updated')</script>";
  
  }*/
}

$res=mysqli_query($db,"select * from $tbl order by partcode");
if(mysqli_num_rows($res)>0)
{
  $i=1;
  print "<table border=1 cellpadding=3> <th>No <th>Customer<th>Item Code <th>Item Name <th>HSN<th>Qty <th>Rate <th>Amount<th>Tax <th>Cess <th>Net <th> Del";
  while($row=mysqli_fetch_array($res))
  {
    $locres=mysqli_query($db,"select location from sparelocmast where brcode='$brcode' and itemcode='$row[partcode]'")or die(mysqli_error($db));
    $locrow=mysqli_fetch_array($locres);
//$rate=getPrice($row[0],$k);
    print "\n<form action='$PHP_SELF' method='POST'> <input type=hidden name='partcode' value='$row[0]'><tr><td>";
    print $i++."</td><td>$row[cust]</td><td>$row[0]</td><td>$row[1]</td><td>$row[6]</td><td>$row[qty]</td><td>$row[rate]</td>";
    //print "<td>$rate</td>";
    $tqty+=$row[sqty];
    $amt=$row[rate]*$row[sqty];
    print "<td align=right>$amt</td>";



/* $prres1=mysqli_query($db,"select kar_tax_sales from partmaster where partcode='$row[0]' ")or die('err'.mysqli_error($db));
 $prrow=mysqli_fetch_array($prres1);
    $tx=$prrow[0]*2;
    $tax=($amt*$tx/100);
    print "<td align=right>$tax</td>";
    $cess_amt=($tax*0/100);


    print "<td align=right>".putComa(round($cess_amt,3))."</td>";
    $netamt_1=$amt+$tax+$cess_amt;
    print "<td align=right>".putComa(round($netamt_1,2))."</td>";*/

    print "<td><input type=submit value='Del' name='Del'></td></tr></form>";
    $totamt+=$netamt_1;
  }
 print "<tr><td colspan=5><center> Total</td>";
 print "<td>".putComa(round($tqty,2))."</td><td>&nbsp;</td>";

 print "<td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td>";
 print "<td align=right>".putComa(round($totamt,2))."</td></tr>";
 print "</table>";
}
$tbl='invotmpdc'.$brcode.$user;

if(isset($_POST['Del']))
{

$partcode=$_REQUEST['partcode'];

 mysqli_query($db,"delete from $tbl where partcode='$partcode'")or die('err'.mysqli_error($db));
 print "<script>document.location='invtempnewdc.php'</script>";
}

function getPrice($code,$kk)
{
if($kk=='Kerala') 
{
 $prres=mysqli_query($db,"select lstprice from spareprmast where partcode='$code' and brcode='ERA153' order by effectfrom desc")or die('err'.mysqli_error($db));
 $prrow=mysqli_fetch_array($prres);
 return $prrow[0];
}
else
{
 $prres=mysqli_query($db,"select lstprice from spareprmast where partcode='$code' and brcode='BAN682' order by effectfrom desc")or die('err'.mysqli_error($db));
 $prrow=mysqli_fetch_array($prres);
 return $prrow[0];
}
}
?>
