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


<?
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'];

$engineer = $_REQUEST['engineer'];
$msno = $_REQUEST['msno'];
$ref = $_REQUEST['ref'];
$vatorcst = $_REQUEST['vatorcst'];
$invtype = $_REQUEST['invtype'];
$hand = $_REQUEST['hnd'];
$hand_per = $_REQUEST['hndtax'];
$hand_taxamt=($hand*$hand_per)/100;

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

//print "<script>alert(hi);</script>";
$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;
$price = $_REQUEST['price'];
foreach($price as $a)
{
 $price[$i]=$a;
 $i++;
}

$i=0;
$supqty = $_REQUEST['supqty'];
foreach($supqty as $a)
{
 $supqty1[$i]=$a;
if($a<=0)
{
  print "<script> alert('Invalid Quantity'); </script>";
  return false;
}
 $i++;
}

$i=0;
$aprice = $_REQUEST['aprice'];
foreach($aprice as $a)
{
         $aprice[$i]=$a;
         //$tamt+=$aprice[$i];
         $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++;
}



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


$i=0;
$fcessa = $_REQUEST['fcessa'];
foreach($fcessa as $a)
{
 $fcessa[$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++;
}

$tax=0;
$customer = $_REQUEST['customer'];
$addr = $_REQUEST['addr'];
$tin = $_REQUEST['tin'];
$mnfr = $_REQUEST['mnfr'];
$paytype = $_REQUEST['paytype'];

//include ("calcsparestock.php");




$invres_sp=mysqli_query($db,"select max(invno) from spareinvoice_labret where brcode='$brcode'");
$invrow_sp=mysqli_fetch_row($invres_sp);
if($invrow_sp[0]>0)
{
    
    $invno_sp=($invrow_sp[0]);
    $invno=$invno_sp+1;
    
}
else
{
    $invno='25'.$brcode.'110001'; ;
}    

$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($price[$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;
 if($vatorcst=='INTRASTATE')   
    {
        $sqlqry="insert into spareinvoice_labret(invno,invtype,invdate,entrydate,saleordno,customer,addr,tin,partcode,partname,qty,price,disc,aprice,hand,tax,taxamt,tot,brcode,paytype,focqty,warqty,cessp,cessamt,vatorcst,jvno,msno,ref,engineer,ac_postdt,itemtype,cgstp,cgstamt,sgstp,sgstamt,igstp,igstamt,gstin,hsncode,fcessp,fcessamt) 
values('$invno','$invtype','$invdate','$entrydate','0', '$customer','$addr','$tin','$partcode[$j]','$partname1[$j]','$supqty1[$j]','$price[$j]','0',
'$aprice[$j]','0','0','$taxamt[$j]','$tot[$j]', '$brcode','$paytype', '0','0',
'0','0', '$vatorcst', '$jvno','$msno','$ref','$engineer','0000-00-00','0','$taxcgst[$j]','$taxamtcgst[$j]','$taxsgst[$j]','$taxamtsgst[$j]','0', '0','','','$fcessp[$j]','$fcessa[$j]')";
//echo $sqlqry;  
    }
    else
    {
        
        $sqlqry="insert into spareinvoice_labret(invno,invtype,invdate,entrydate,saleordno,customer,addr,tin,partcode,partname,qty,price,disc,aprice,hand,tax,taxamt,tot,brcode,paytype,focqty,warqty,cessp,cessamt,vatorcst,jvno,msno,ref,engineer,ac_postdt,itemtype,cgstp,cgstamt,sgstp,sgstamt,igstp,igstamt,gstin,hsncode,fcessp,fcessamt) 
values('$invno','$invtype','$invdate','$entrydate','0', '$customer','$addr','$tin','$partcode[$j]','$partname1[$j]','$supqty1[$j]','$price[$j]','0',
'$aprice[$j]','0','0','$taxamt[$j]','$tot[$j]', '$brcode','$paytype', '0','0',
'0','0', '$vatorcst', '$jvno','$msno','$ref','$engineer','0000-00-00','0','0','0','0','0','$taxigst[$j]', '$taxamtigst[$j]','0','0','0','0')";
        
     //echo $sqlqry;    
    }

         mysqli_query($db,$sqlqry)or die('Invoice Err:'.mysqli_error($db));

//mysqli_query($db,"update saleorder_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'");

     }
}



//********** 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_labret where invno='$invno' and vatorcst='INTRASTATE' and brcode='$brcode' group by invno")or die('Led Post Err3'.mysqli_error($db));
while($taxdrrow=mysqli_fetch_array($taxdrres))
{
    $ledger=$engineer;
    $sql="insert into journal2(date,jvno,type,partic,dramt,cramt,narr,brcode,j_type,against,no,tr_no) values('$entrydate','$jvno','Dr','$ledger','$taxdrrow[0]','0','Service invno: $invno $msno','$brcode','Service Invoice ret','Service Order','$saleordno','$invno')";
    mysqli_query($db,$sql) or die('Led Post Err1'.mysqli_error($db));
}
$taxres=mysqli_query($db,"select cgstp,sgstp,sum(cgstamt),sum(sgstamt),sum(fcessamt) from spareinvoice_labret where invno='$invno' and vatorcst='INTRASTATE' and brcode='$brcode' group by invno")or die('Led Post Err3'.mysqli_error($db));
while($taxrow=mysqli_fetch_array($taxres))
{
    $gsta=$taxrow[2]+$taxrow[3];
    $ledger='Output Tax';
    $sql="insert into journal2(date,jvno,type,partic,dramt,cramt,narr,brcode,j_type,against,no,tr_no) values('$entrydate','$jvno','Dr','$ledger','$gsta','0','Service invno: $invno $msno','$brcode','Service Invoice ret','Service Order','$saleordno','$invno')";
    mysqli_query($db,$sql) or die('Led Post Err2'.mysqli_error($db));
     if($taxrow[4]>0)
    {
   $ledger='Flood Cess';
    $sql="insert into journal2 (date,jvno,type,partic,dramt,cramt,narr,brcode,j_type,against,no,tr_no) values('$entrydate','$jvno','Dr','$ledger','$taxrow[4]','0','Service invno: $invno $msno','$brcode','Service Invoice ret','Service Order','$saleordno','$invno')";
    mysqli_query($db,$sql) or die('Led Post Err2fcess'.mysqli_error($db));
    }
}


$taxdrres22=mysqli_query($db,"select sum(aprice),igstp from spareinvoice_labret where invno='$invno' and vatorcst='INTERSTATE' and brcode='$brcode' group by invno")or die('Led Post Err3'.mysqli_error($db));
while($taxdrrow22=mysqli_fetch_array($taxdrres22))
{
    //$ledger='Outward Supply - Labour';
   $ledger= $engineer;
    $sql22="insert into journal2(date,jvno,type,partic,dramt,cramt,narr,brcode,j_type,against,no,tr_no) values('$entrydate','$jvno','Dr','$ledger','$taxdrrow22[0]','0','Service invno: $invno $msno','$brcode','Service Invoice ret','Service Order','$saleordno','$invno')";
    mysqli_query($db,$sql22) or die('Led Post Err1'.mysqli_error($db));
}

$taxres=mysqli_query($db,"select igstp, sum(igstamt) from spareinvoice_labret where invno='$invno' and vatorcst='INTERSTATE' and brcode='$brcode' group by invno")or die('Led Post Err3'.mysqli_error($db));
while($taxrow=mysqli_fetch_array($taxres))
{
    $ledger='Output Tax';
    $sql="insert into journal2 (date,jvno,type,partic,dramt,cramt,narr,brcode,j_type,against,no,tr_no) values('$entrydate','$jvno','Dr','$ledger','$taxrow[1]','0','Service invno: $invno $msno','$brcode','Service Invoice ret','Service Order','$saleordno','$invno')";
    mysqli_query($db,$sql) or die('Led Post Err2'.mysqli_error($db));
}
$fr=mysqli_query($db,"select sum(hand) from spareinvoice_labret where invno='$invno' and brcode='$brcode' group by invno")or die('cess err.'.mysqli_error($db));
while($fr1=mysqli_fetch_array($fr))
{
    $ledger='FREIGHT CHARGES';
    $sql="insert into journal2 (date,jvno,type,partic,dramt,cramt,narr,brcode,j_type,against,no,tr_no) values('$entrydate','$jvno','Dr','$ledger','$fr1[0]','0','Service invno: $invno $msno','$brcode','Service Invoice ret','Service Order','$saleordno','$invno')";
   // mysqli_query($db,$sql) or die('Led Post Err7'.mysqli_error($db));
}
$sq="select sum(tot) from spareinvoice_labret 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','Service invno: $invno $msno','$brcode','Service Invoice ret','Service Order','$saleordno','$invno')";
    //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','Cr','$customer','0','".round($taxcrrow[0])."','Service invno: $invno $msno','$brcode','Service Invoice ret','Service Order','$saleordno','$invno')";
    mysqli_query($db,$sql) or die('Led Post Err3'.mysqli_error($db));
    }
}
$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 $msno','$brcode','Service Invoice ret','S ORd no','$saleordno','$invno')";
    echo $sql;
    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','Service Invoice ret','S ORd no','$saleordno','$invno')";
     echo $sql;
     mysqli_query($db,$sql) or die("J V Write error dif:".mysqli_error($db));
}
$ip_address = "$jvno|Service Invoice|Add";
log_entry($brcode,$user,$ip_address,"serviceinvoice:$invno jvno: $jvno",$db);

?>
<script>
//alert("Data Saved...!");
alert("Data Saved with invoice no <? echo $invno ?>");
window.open('serviceinvoiceret_dup.php?invno=<?print $invno?>&mnfr=<?print $mnfr?>');
document.location='serviceinvret.php';
</script>