<?
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");
include("../util/exqty.php");
$tbl="sparetmp".$brcode;



$invdate=$y.'-'.$m.'-'.$d;
$entrydate=$y1.'-'.$m1.'-'.$d1;
$sinvdate=$sy.'-'.$sm.'-'.$sd;


$i=0;
foreach($partcode as $a)
{
 $partcode[$i]=$a; 
 $i++;
}
$i=0;
foreach($partname as $a)
{
 $partname1[$i]=$a; 
 $i++;
}
$i=0;
foreach($supqty as $a)
{
 $supqty1[$i]=$a; 
/*if($a<=0)
{
  print "<script> alert('Invalid Quantity'); </script>";
  return false;
}*/
 $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; 
$tamt+=$aprice[$i];
 $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; 
$txamt+=$taxamt[$i];

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

$i=0;
foreach($focqty as $a)
{
 $focqty[$i]=$a; 
 $i++;
}
$i=0;
foreach($warqty as $a)
{
 $warqty[$i]=$a; 
 $i++;
}
$i=0;
foreach($cessp as $a)
{
 $cessp[$i]=$a; 
 $i++;
}
$i=0;
foreach($cessamt as $a)
{
 $cessamt[$i]=$a;
$csamt+=$cessamt[$i];
 
 $i++;
}


$tot5=$tamt+$txamt+$csamt;
if($paytype=='Credit')
{
$re=mysqli_query($db,"select sum(op) from ledger where grp='Sundry Debtors' and led='$customer' ")or die(mysqli_error($db));
$srow=mysqli_fetch_array($re);
$opbal=$srow[0];

 $sql="select sum(dramt)-sum(cramt) from journal2 where partic='$customer'";
 $balres=mysqli_query($db,$sql)or die('err1'.mysqli_error($db));
  $balrow=mysqli_fetch_row($balres)or die('err2'.mysqli_error($db));
 $balance=$opbal+$balrow[0];

$tot1=$tot5+$balance;
  $chstkre1=mysqli_query($db,"select cramt,crdays from customer where acname='$customer'");
  $chstkrow1=mysqli_fetch_array($chstkre1);
   if($tot1>$chstkrow1[0])
    {
      print "exceeds limit ".$tot1;
      return;
    }


$sql="select sum(dramt) from journal2 where partic='$customer' and (to_days(curdate())-to_days(date))<=$chstkrow1[1]";

 
 $res1=mysqli_query($db,$sql);
  $row=mysqli_fetch_array($res1);
   $r3=$row[0];
$r3=$r3+$opbal;
if ($r3>0)
{
if ($balance>$r3)
{ 
print  "Sorry, I can't bill,Payment outstanding ".$balance;
return;
}
}
}

//include ("calcsparestock.php");
for($j1=0;$j1<$i;$j1++)
{

if($supqty1[$j1]>0)
{
$availqty=exqty1($partcode[$j1],$brcode,$user);
   if($supqty1[$j1]>$availqty)
    {
      print "<script> alert('Insufficient Quantity');history.back(1); </script>";
      print "Insufficient Quantity for".$partcode[$j1];
      return;
    }
/*  $chstkre=mysqli_query($db,"select * from $tbl where partcode='$partcode[$j1]'");
  $chstkrow=mysqli_fetch_array($chstkre);
   if($supqty1[$j1]>$chstkrow[supqty])
    {
      print "Insufficient Quantity ".$partcode[$j1];
      return;
    }
*/
}
}

$re=mysqli_query($db,"select max(invno) from transoutnew where brcode='$brcode'");
$prow=mysqli_fetch_row($re);
if($prow[0]<=0)
 $invno=1;
else
 $invno=($prow[0]+1);


$re1=mysqli_query($db,"select max(jvno) from journal2 where brcode='$brcode'");
$jvrow=mysqli_fetch_row($re1);
if($jvrow[0]<=0)
 $jvno=1;
else
 $jvno=($jvrow[0]+1);

for($j=0;$j<$i;$j++)
{
if($supqty1[$j]>0)
{
  $sql="insert into transoutnew 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]','$cessp[$j]','$cessamt[$j]', '$vatorcst', '$jvno','$msno','$ref', '$engineer','$sinvno','$sinvdate')";
 mysqli_query($db,$sql)or die('Invoice Err:'.mysqli_error($db));

$sql="update saleorder_sparetrfr set sup_qty=(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));
}
}

//********** chk ledger

$chre=mysqli_query($db,"select * from ledger where led='$customer' and brcode='$brcode'");
if(mysqli_num_rows($chre)<=0)
{
$code=substr($customer,0,3).date(ymds);
$code=str_replace(' ','-',$code);

$sql="insert into ledger values('$customer','Sundry Debtors','0','Dr','$code','$brcode')";
mysqli_query($db,$sql);
}



//********************* a/c posting
$sql="select sum(qty*price) from transoutnew where invno='$invno' and brcode='$brcode'";
$postres=mysqli_query($db,$sql) or die('post err'.mysqli_error($db));

$postrow=mysqli_fetch_array($postres);
mysqli_query($db,"insert into journal2 values('$entrydate','$jvno','Cr','Spare transfer out','0','$postrow[0]','Spare Transfer out:$tr_no','$brcode','Spare trans out','Trans no','$invno','$invno')");

mysqli_query($db,"insert into journal2 values('$entrydate','$jvno','Dr','$customer','$postrow[0]','0','Spare Transfer out:$tr_no','$brcode','Spare trans out','Trans no','$invno','$invno')");

log_entry($brcode,$user,$REMOTE_ADDR,"sparetrans out:$invno, Journal:$jvno");


$jdres=mysqli_query($db,"select sum(dramt),sum(cramt) from journal2 where jvno='$jvno' and brcode='$brcode'");
$jdiff=mysqli_fetch_row($jdres);
if($jdiff[0]!=$jdiff[1]||$jdiff[0]==0)
{
 print "<sctipt> alert('Error in posting. Please check the data now');</script>";
}


log_entry($brcode,$user,$REMOTE_ADDR,"transoutnew:$invno jvno: $jvno");
?>
<script>
document.location='prnsptrout.php?invno=<?print $invno?>&invtype=<?print $invtype?>'; 
</script>
