<?
session_start();
$brcode = $_SESSION['brcode'];
$user = $_SESSION['user'];
if(strlen($brcode)<=0)
{
           print "<script> alert('Session timed out'); parent.document.location='../index.php'; </script>";
           return;
}
?>
<script>
alert('Cash will be posted automatically');
</script>
<?
include("../opendb.php");
include("../test.php");
//include("../util/exqty.php");
$tbl="sparetmp".$brcode;
$y = $_REQUEST['y'];
$m = $_REQUEST['m'];
$d = $_REQUEST['d'];
$y1 = $_REQUEST['y1'];
$m1 = $_REQUEST['m1'];
$d1 = $_REQUEST['d1'];
$saleordno = $_REQUEST['saleordno'];
$engineer = $_REQUEST['engineer'];
$msno = $_REQUEST['msno'];
$ref = $_REQUEST['ref'];
$vatorcst = $_REQUEST['vatorcst'];
$invtype = $_REQUEST['invtype'];

$invdate=$y.'-'.$m.'-'.$d;
$entrydate=$y1.'-'.$m1.'-'.$d1;
$i=0;
$partcode = $_REQUEST['partcode'];
foreach($partcode as $a)
{
         $partcode[$i]=$a;
         $i++;
}
$i=0;
$partname = $_REQUEST['partname'];
foreach($partname as $a)
{
         $partname1[$i]=$a;
         $i++;
}
$i=0;
$supqty = $_REQUEST['supqty'];
foreach($supqty as $a)
{
 $supqty[$i]=$a;
/*if($a<=0)
{
  print "<script> alert('Invalid Quantity'); </script>";
  return false;
}*/
 $i++;
}
$i=0;
$price = $_REQUEST['price'];
foreach($price as $a)
{
 $price[$i]=$a;
 $i++;
}

$i=0;
$aprice = $_REQUEST['aprice'];
foreach($aprice as $a)
{
         $aprice[$i]=$a;
         $tamt+=$aprice[$i];
         $i++;
}

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

$i=0;
$taxamt = $_REQUEST['taxamt'];
foreach($taxamt as $a)
{
 $taxamt[$i]=$a;
$txamt+=$taxamt[$i];

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

$i=0;
$taxcgst = $_REQUEST['taxcgst'];
foreach($taxcgst as $a)
{
 $taxcgst[$i]=$a;
 $i++;
}
$i=0;
$taxsgst = $_REQUEST['taxsgst'];
foreach($taxsgst as $a)
{
 $taxsgst[$i]=$a;
 $i++;
}

$i=0;
$taxigst = $_REQUEST['taxigst'];
foreach($taxigst as $a)
{
 $taxigst[$i]=$a;
 $i++;
}

$i=0;
$taxamtcgst = $_REQUEST['taxamtcgst'];
foreach($taxamtcgst as $a)
{
 $taxamtcgst[$i]=$a;
//$txamtcgst+=$taxamtcgst[$i];
 $i++;
}

$i=0;
$taxamtsgst = $_REQUEST['taxamtsgst'];
foreach($taxamtsgst as $a)
{
 $taxamtsgst[$i]=$a;
//$txamtsgst+=$taxamtsgst[$i];

 $i++;
}

$i=0;
$taxamtigst = $_REQUEST['taxamtigst'];
foreach($taxamtigst as $a)
{
 $taxamtigst[$i]=$a;
//$txamtigst+=$taxamtigst[$i];

 $i++;
}

$customer = $_REQUEST['customer'];
$addr = $_REQUEST['addr'];
$tin = $_REQUEST['tin'];
$paytype = $_REQUEST['paytype'];
$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];
    $r2=$r3;
    if($opbal>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;
         }
       
     }
}
*/
$re=mysqli_query($db,"select max(invno) from spareinvoice 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($supqty[$j]>0)
     {
         if(is_numeric($focqty[$j]))
            $focqty[$j]=$focqty[$j];
         else
             $focqty[$j]=0;
         if(is_numeric($warqty[$j]))
             $warqty[$j]=$warqty[$j];
         else
             $warqty[$j]=0;
             $hand=0;
             $floodcs[$j]=0;
         $sql="insert into spareinvoice(invno,invtype ,invdate ,entrydate ,saleordno,customer ,addr , tin ,partcode ,partname ,qty ,price ,disc ,aprice ,hand ,tax ,cgstp ,sgstp ,igstp ,cgstamt ,sgstamt ,igstamt ,taxamt ,tot ,brcode ,paytype ,focqty ,
warqty ,cessp ,cessamt ,vatorcst ,jvno ,msno ,ref ,engineer,itemtype,fcessa)  values('$invno','$invtype','$invdate','$entrydate','0', '$customer','$addr','$tin','$partcode[$j]','$partname1[$j]','$supqty[$j]','$price[$j]','0',
'$aprice[$j]','$hand','$floodcs[$j]','$taxcgst[$j]','$taxsgst[$j]','0', '$taxamtcgst[$j]', '$taxamtsgst[$j]','0', '$taxamt[$j]','$tot[$j]', '$brcode','$paytype', '0','0',
'0','0', '$vatorcst', '$jvno','$msno','$ref','$engineer','spares',0)";
echo $sql;
         mysqli_query($db,$sql)or die('Invoice 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);
   $sql11="insert into ledger values('$customer','Sundry Debtors','0','Dr','$code','$brcode')";
   mysqli_query($db,$sql11);
}
//********************* a/c posting


$taxdrres=mysqli_query($db,"select sum(aprice),cgstp+sgstp from spareinvoice where invno='$invno' and vatorcst='INTRASTATE' and brcode='$brcode' group by cgstp")or die('Led Post Err3'.mysqli_error($db));
while($taxdrrow=mysqli_fetch_array($taxdrres))
{
    $ledger='Spare invoice GST '.$taxdrrow[1].'%';
    $sql="insert into journal2(date,jvno,type,partic,dramt,cramt,narr,brcode,j_type,against,no,tr_no) 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 cgstp,sgstp,sum(cgstamt),sum(sgstamt) from spareinvoice where invno='$invno' and vatorcst='INTRASTATE' and brcode='$brcode' group by cgstp")or die('Led Post Err3'.mysqli_error($db));
while($taxrow=mysqli_fetch_array($taxres))
{
    $ledger='Output CGST '.$taxrow[0].'%';
    $sql="insert into journal2(date,jvno,type,partic,dramt,cramt,narr,brcode,j_type,against,no,tr_no) values('$entrydate','$jvno','Cr','$ledger','0','$taxrow[2]','Spare invno: $invno','$brcode','Spare Invoice','Sale Order','$saleordno','0')";
    mysqli_query($db,$sql) or die('Led Post Err2'.mysqli_error($db));
    $ledger='Output SGST '.$taxrow[1].'%';
    $sql="insert into journal2(date,jvno,type,partic,dramt,cramt,narr,brcode,j_type,against,no,tr_no) values('$entrydate','$jvno','Cr','$ledger','0','$taxrow[3]','Spare invno: $invno','$brcode','Spare Invoice','Sale Order','$saleordno','0')";
    mysqli_query($db,$sql) or die('Led Post Err2'.mysqli_error($db));
}
$taxdrres=mysqli_query($db,"select sum(aprice),igstp from spareinvoice where invno='$invno' and vatorcst='INTERSTATE' and brcode='$brcode' group by igstp")or die('Led Post Err3'.mysqli_error($db));
while($taxdrrow=mysqli_fetch_array($taxdrres))
{
    $ledger='Spare invoice IGST '.$taxdrrow[1].'%';
    $sql="insert into journal2(date,jvno,type,partic,dramt,cramt,narr,brcode,j_type,against,no,tr_no) 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 igstp, sum(igstamt) from spareinvoice where invno='$invno' and vatorcst='INTERSTATE' and brcode='$brcode' group by igstp")or die('Led Post Err3'.mysqli_error($db));
while($taxrow=mysqli_fetch_array($taxres))
{
    $ledger='Output IGST '.$taxrow[0].'%';
    $sql="insert into journal2(date,jvno,type,partic,dramt,cramt,narr,brcode,j_type,against,no,tr_no) 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));
}

$fr=mysqli_query($db,"select sum(hand) from spareinvoice where invno='$invno' and brcode='$brcode' group by invno")or die('cess err.'.mysqli_error($db));
while($fr1=mysqli_fetch_array($fr))
{
    $ledger='FRIEGHT CHARGES';
    $sql="insert into journal2(date,jvno,type,partic,dramt,cramt,narr,brcode,j_type,against,no,tr_no) values('$entrydate','$jvno','Cr','$ledger','0','$fr1[0]','Spare invno: $invno','$brcode','Spare Invoice','Sale Order','$saleordno','0')";
    mysqli_query($db,$sql) or die('Led Post Err7'.mysqli_error($db));
}
$sq="select sum(tot) from spareinvoice where invno='$invno' and brcode='$brcode'";
$taxcrres=mysqli_query($db,$sq)or die('Led Post Err3'.mysqli_error($db));
while($taxcrrow=mysqli_fetch_array($taxcrres))
{
        if($paytype=='Cash')
    {
    $sql="insert into journal2(date,jvno,type,partic,dramt,cramt,narr,brcode,j_type,against,no,tr_no) values('$entrydate','$jvno','Dr','Cash','".round($taxcrrow[0])."','0','Spare invno: $invno','$brcode','Spare Invoice','Sale Order','$saleordno','0')";
    mysqli_query($db,$sql) or die('Led Post Err3'.mysqli_error($db));
    }
    else
    {
    $sql="insert into journal2(date,jvno,type,partic,dramt,cramt,narr,brcode,j_type,against,no,tr_no) 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 Err3'.mysqli_error($db));
    }
    $totval+=$taxcrrow[0];
}
$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(date,jvno,type,partic,dramt,cramt,narr,brcode,j_type,against,no,tr_no) 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(date,jvno,type,partic,dramt,cramt,narr,brcode,j_type,against,no,tr_no) 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));
}


log_entry($brcode,$user,$REMOTE_ADDR,"spareinvoice:$invno jvno: $jvno");
?>
<script>
alert("Data Saved with invoice no <? echo $invno ?>");
window.open('prnspareinvoice_dup.php?invno=<?print $invno?>');
document.location='spareinvoice.php';
//document.location='prnspareinvoice2.php?invno=<?print $invno?>&invtype=<?print $invtype?>';
</script>