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

include("../opendb.php");
include("../test.php");
$re=mysqli_query($db,"select max(invno) from spareinvoicefoc where brcode='$brcode' and invtype='$invtype'");
$prow=mysqli_fetch_row($re)or die('err'.mysqli_error($db));
if($prow[0]<=0)
 $invno=1;
else
 $invno=($prow[0]+1);

$invdate=$y.'-'.$m.'-'.$d;
$entrydate=$y1.'-'.$m1.'-'.$d1;
print $invdate;

$i=0;
foreach($partcode as $a)
{
 $partcode[$i]=$a; 
 $i++;
}
$i=0;
foreach($partname as $a)
{
 $partname1[$i]=$a; 
 $i++;
}

$i=0;
foreach($price as $a)
{
 $price[$i]=$a; 
 $i++;
}
$i=0;
foreach($disc as $a)
{
 $disc[$i]=$a; 
 $i++;
}
$i=0;
foreach($aprice as $a)
{
 $aprice[$i]=$a; 
 $i++;
}
$i=0;
foreach($hand as $a)
{
 $hand[$i]=$a; 
 $i++;
}

$i=0;
foreach($tax as $a)
{
 $tax[$i]=$a; 
 $i++;
}

$i=0;
foreach($taxamt as $a)
{
 $taxamt[$i]=$a; 
 $i++;
}
$i=0;
foreach($tot as $a)
{
 $tot[$i]=$a; 
 $i++;
}

$i=0;
foreach($supqty as $a)
{
 $supqty1[$i]=$a; 
 $i++;
}

for($j=0;$j<$i;$j++)
{
if($supqty1[$j]>0)
{
  $sql="insert into spareinvoicefoc values('$invno','$invtype','$invdate','$entrydate','$saleordno', '$customer','$addr','$tin','$partcode[$j]','$partname1[$j]','$supqty1[$j]','$price[$j]','$disc[$j]','$aprice[$j]','$hand[$j]','$tax[$j]', '$taxamt[$j]','$tot[$j]', '$brcode','$paytype', '$focqty[$j]','$warqty[$j]','$msno')";
print $sql;
mysqli_query($db,$sql)or die(mysqli_error($db));

$sql="update saleorder_spare set sup_qty='$supqty1[$j]',sup_purno='$invno',sup_date='$entrydate' , trans_type='$invtype' where orderno='$saleordno' and partcode='$partcode[$j]' and brcode='$brcode'";
mysqli_query($db,$sql)or die('err'.mysqli_error($db));
}
}
log_entry($brcode,$user,$REMOTE_ADDR,"spareinvoiceFOC:$invno , Journal: $jvno");

?>

<script>
document.location='prnspareinvoice.php?invno=<?print $invno?>&invtype=<?print $invtype?>&foc=foc'; 
</script>
