<?
session_start();
if(strlen($brcode)<=0)
{
 print "<script> alert('Session timed out'); parent.document.location='../index.html'; </script>";
 return;
}
?>
<script>
 alert('Cash will be posted automatically');
</script>
<?
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 warspareinvoice 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 warspareinvoice 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 warorder_spare 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
if($vatorcst=='VAT')
{
 $taxdrres=mysqli_query($db,"select sum(aprice+((aprice*hand)/100)),tax from warspareinvoice where invno='$invno' and vatorcst='VAT' and brcode='$brcode' group by tax")or die('Led Post Err3'.mysqli_error($db));
  while($taxdrrow=mysqli_fetch_array($taxdrres))
  {
    $ledger='Warr invoice '.$taxdrrow[tax];  
    $sql="insert into journal2 values('$entrydate','$jvno','Cr','$ledger','0','$taxdrrow[0]','Spare invno: $invno','$brcode','Spare Invoice','Sale Order','$saleordno','0')";
     mysqli_query($db,$sql) or die('Led Post Err1'.mysqli_error($db));
  }


 $taxres=mysqli_query($db,"select tax, sum(taxamt) from warspareinvoice where invno='$invno' and vatorcst='VAT' and brcode='$brcode' group by tax")or die('Led Post Err3'.mysqli_error($db));
  while($taxrow=mysqli_fetch_array($taxres))
  {
    $ledger='VAT Collected '.$taxrow[tax];  
    $sql="insert into journal2 values('$entrydate','$jvno','Cr','$ledger','0','$taxrow[1]','Spare invno: $invno','$brcode','Spare Invoice','Sale Order','$saleordno','0')";
     mysqli_query($db,$sql) or die('Led Post Err2'.mysqli_error($db));
  }

//*************** cess
 $taxcessres=mysqli_query($db,"select sum(cessamt),cessp from warspareinvoice where invno='$invno' and vatorcst='VAT' and brcode='$brcode' group by invno")or die('cess err.'.mysqli_error($db));
  while($taxcessrow=mysqli_fetch_array($taxcessres))
  {
    $ledger='Output Cess '.$taxcessrow[1].'%';
    $sql="insert into journal2 values('$entrydate','$jvno','Cr','$ledger','0','$taxcessrow[0]','Spare invno: $invno','$brcode','Spare Invoice','Sale Order','$saleordno','0')";
     mysqli_query($db,$sql) or die('Led Post Err6'.mysqli_error($db));
  }
//************************
$sq="select sum(tot) from warspareinvoice where invno='$invno' and vatorcst='VAT' and brcode='$brcode'";
 $taxcrres=mysqli_query($db,$sq)or die('Led Post Err3'.mysqli_error($db));
  while($taxcrrow=mysqli_fetch_array($taxcrres))
  {
    $sql="insert into journal2 values('$entrydate','$jvno','Dr','Telcon warranty','".round($taxcrrow[0])."','0','Spare invno: $invno','$brcode','Spare Invoice','Sale Order','$saleordno','0')";
print 'Rounding '.$sql;
     mysqli_query($db,$sql) or die('Led Post Err3'.mysqli_error($db));
/*  
$rnd=round($taxcrrow[0]);
$rnd2=round(($taxcrrow[0]-$rnd),2);
if($rnd2<0)
{
$sql="insert into journal2 values('$entrydate','$jvno','Dr','Round Off','".abs($rnd2)."', '0','Round ofSINV No: $invno','$brcode','Spare sale','invno','$invno','$invno')";
 print $sql;
mysqli_query($db,$sql) or die("J V Write error dif1:".mysqli_error($db));
}
else if($rnd2>0)
{
 $sql="insert into journal2 values('$entrydate','$jvno','Cr','Round Off','0','".abs($rnd2)."','Round of adjusted SINV No: $invno','$brcode','Spare sale','invno','$invno','$invno')";
 print $sql;
mysqli_query($db,$sql) or die("J V Write error dif:".mysqli_error($db));
}*/
} // end while
} // end if
else
{
//********************* CST
 $taxdrres=mysqli_query($db,"select sum(aprice+((aprice*hand)/100)),tax from warspareinvoice where invno='$invno' and vatorcst='CST' and brcode='$brcode' group by tax")or die('Led Post Err3'.mysqli_error($db));
  while($taxdrrow=mysqli_fetch_array($taxdrres))
  {
    $ledger='warSpare Invoice CST '.$taxdrrow[tax];  
    $sql="insert into journal2 values('$entrydate','$jvno','Cr','$ledger','0','$taxdrrow[0]','Spare invno: $invno','$brcode','Spare Invoice','Sale Order','$saleordno','0')";
     mysqli_query($db,$sql) or die('Led Post Err4'.mysqli_error($db));
  }


 $taxres=mysqli_query($db,"select tax, sum(taxamt) from warspareinvoice where invno='$invno' and vatorcst='CST' and brcode='$brcode' group by tax")or die('Led Post Err3'.mysqli_error($db));
  while($taxrow=mysqli_fetch_array($taxres))
  {
    $ledger='warCST Collected '.$taxrow[tax];  
    $sql="insert into journal2 values('$entrydate','$jvno','Cr','$ledger','0','$taxrow[1]','Spare invno: $invno','$brcode','Spare Invoice','Sale Order','$saleordno','0')";
     mysqli_query($db,$sql) or die('Led Post Err5'.mysqli_error($db));
  }
//************************
 $taxcrres=mysqli_query($db,"select sum(tot) from warspareinvoice where invno='$invno' and vatorcst='CST' and brcode='$brcode'")or die('Led Post Err3'.mysqli_error($db));
  while($taxcrrow=mysqli_fetch_array($taxcrres))
  {
    $sql="insert into journal2 values('$entrydate','$jvno','Dr','$customer','".round($taxcrrow[0])."','0','Spare Invno: $invno','$brcode','Spare Invoice','Sale Order','$saleordno','0')";
     mysqli_query($db,$sql) or die('Led Post Err6'.mysqli_error($db));
  
/*  $rnd=round($taxcrrow[0]);
  $rnd2=round(($taxcrrow[0]-$rnd),2);
  if($rnd2<0)
  {
    $sql="insert into journal2 values('$entrydate','$jvno','Dr','Round Off','".abs($rnd2)."', '0','Round  ofSINV No: $invno','$brcode','Spare sale','invno','$invno','$invno')";
   mysqli_query($db,$sql) or die("J V Write error dif1:".mysqli_error($db));
  }
  else if($rnd2>0)
   {
     $sql="insert into journal2 values('$entrydate','$jvno','Cr','Round Off','0','".abs($rnd2)."','Round of adjusted SINV No: $invno','$brcode','Spare sale','invno','$invno','$invno')";
   mysqli_query($db,$sql) or die("J V Write error dif:".mysqli_error($db));
   }
*/
 } // end while
}// end else
$jvdifres=mysqli_query($db,"select sum(dramt)-sum(cramt) from journal2 where jvno='$jvno' and brcode='$brcode'");
$jvdifrow=mysqli_fetch_row($jvdifres);
$dif=$jvdifrow[0];

if($dif<0)
{
 $xx=abs($dif);
$sql="insert into journal2 values('$entrydate','$jvno','Dr','Round Off','".$xx."', '0','Round ofSINV No: $invno','$brcode','Spare Invoice','S ORd no','$saleordno','$invno')";
mysqli_query($db,$sql) or die("J V Write error dif1:".mysqli_error($db));
}
else if($dif>0)
{
 $xx=abs($dif);
 $sql="insert into journal2 values('$entrydate','$jvno','Cr','Round Off','0','".$xx."','Round of adjusted SINV No: $invno','$brcode','Spare Invoice','S ORd no','$saleordno','$invno')";
mysqli_query($db,$sql) or die("J V Write error dif:".mysqli_error($db));
}

$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>";
}

if($paytype=='Casssh')
{
$rs=mysqli_query($db,"select max(crno) from cshrec where brcode='$brcode'");
$cr_row=mysqli_fetch_row($rs);
if($cr_row[0]<=0)
   $crno=1;
 else   
   $crno=$cr_row[0]+1;


$jpostres=mysqli_query($db,"select sum(dramt) from journal2 where jvno='$jvno' and partic='$customer' and brcode='$brcode'");
$jpostrow=mysqli_fetch_row($jpostres);
$jvno=$jvno+1;
$sql="insert into journal2 values('$entrydate','$jvno','Dr','Cash','$jpostrow[0]','0','Spare Invoice of $customer(invno:$invno)','$brcode','CR','sp invoice','$invno','$crno')";
mysqli_query($db,$sql) or die(mysqli_error($db));

 $sql="insert into journal2 values('$entrydate','$jvno','Cr','$customer','0','$jpostrow[0]','Spare Invoice of $customer(invno:$invno)','$brcode','CR','sp invoice','$invno','$crno')";
mysqli_query($db,$sql) or die(mysqli_error($db));

 $sql="insert into cshrec values('$entrydate','$crno','Cr','$customer','0','$jpostrow[0]','Spare invoice (invno:$invno)','$brcode','$jvno','invno','$invno')";
mysqli_query($db,$sql) or die(mysqli_error($db));
print "<script> window.open('../accounts/payrec/prncshrec.php?no=$crno'); </script>";
}
log_entry($brcode,$user,$REMOTE_ADDR,"warspareinvoice:$invno jvno: $jvno");
?>
<script>
document.location='prnwarinvoice.php?invno=<?print $invno?>&invtype=<?print $invtype?>'; 
</script>
