<?
session_start();
include("test.php");
include("opendb.php");
include("includes/function.php");

if($savecount==0)
{

$ker=mysqli_query($db,"select brstate from branch where brcode='$brcode'");
$kar=mysqli_fetch_array($ker);
if(strlen($brcode)<=0)
{
 print "<script> alert('Session timed out'); parent.document.location='index.html'; </script>";
 return;
}

if(($cname=='SELECT')||($cname==''))
    {
    echo "Data could not saved <br> Invalid supplier";
    }
  elseif($pbillno=='')
    {
    echo "Data could not saved <br> Invalid Purchase Bill No";
    }

 elseif(($pmode=='')||($pmode=='--select--'))
    {
    echo "Data could not saved <br> Invalid Payment mode";
    }
  else
  {

$flg2=0;

 $pcd_flg=0;
 $pnm_flg=0;

$i=0;
foreach($partcode as $a)
{
 $partcode[$i]=$a; 

 if(strlen($a)<=0)
     $pcd_flg++;

 $i++;
}

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


$i=0;
foreach($partname as $a)
{
 $partname[$i]=$a; 

  if(strlen($a)<=0)
     $pnm_flg++;

 $i++;
}

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

$i=0;
foreach($qty as $a)
{
 $qty[$i]=$a; 
  if($qty[$i]>0){

  $flg2++;
  }
 
 $i++;
}

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

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

foreach($spareengineer as $a)
{
 $spareengineer[$i]=$a; 
 $i++;
}
foreach($engineer as $a)
{
 $engineer[$i]=$a; 
 $i++;
}

$i=0;
foreach($day as $a)
{
 $day[$i]=$a; 
 $i++;
}
$i=0;
foreach($month as $a)
{
 $month[$i]=$a; 
 $i++;
}
$i=0;
foreach($year as $a)
{
 $year[$i]=$a; 
 $i++;
}



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







$entrydate=$entrdate3.'-'.$entrdate2.'-'.$entrdate1;
$prdt=$pdate3.'-'.$pdate2.'-'.$pdate1;

 $re=mysqli_query($db,"select max(prno) from lubricant_purmast where brcode='$brcode' ");

     if(mysqli_num_rows($re)<1)
       {
           $purid=1; 
       }
       else
       {
        $row=mysqli_fetch_array($re);
        
         $purid=$row[0]+1;
        }

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





if($flg2<=0){

print "<script>alert('Invalid Entry');</script>";
exit;
 }


$t=1;
$s=1;

$dt=date('Y-m-d');

for($m=0;$m<$i;$m++)

{

if($qty[$m]>0)
{


 if(strlen($partcode[$m])<=0)

{
   echo 'The Part code must be filled out';
   exit;
}

 if(strlen($partname[$m])<=0)

{
   echo 'The Part name must be filled out';
   exit;
}



	$edate=$year[$j].'-'.$month[$j].'-'.$day[$j];
       $qry1=mysqli_query($db,"select * from newpartmaster where partcode='$partcode[$m]' ");
       $row1=mysqli_fetch_array($qry1);
       $qry2=mysqli_query($db,"select * from subgroupmaster where subname='$row1[2]' ");
       $row2=mysqli_fetch_array($qry2);
       mysqli_query($db,"insert into lubricant_purmast values('$partcode[$m]','$partname[$m]','$row1[2]','$row2[1]','$purid','$entrydate','$qty[$m]','$rate[$m]','$disc[$m]','$gtot[$m]','$brcode','$pbillno','$prdt','$cname','NO','1','$jvno','$spareengineer[$m]','$jcno','$porder[$m]')");


   $r1=mysqli_query($db,"select * from newpartmaster where partcode='$partcode[$m]' ");
   $jr1=mysqli_fetch_array($r1);

}
}



  
$taxdrres=mysqli_query($db,"select sum(tot) from lubricant_purmast  where prno='$purid' and  brcode='$brcode'");

  $taxdrrow=mysqli_fetch_array($taxdrres);
  
    $ledger='Spare Purchase';  
   $sql="insert into journal2 values('$entrydate','$jvno','Dr','Spare Purchase','$taxdrrow[0]','0','Spare purno: $purid,PBill:$pbillno','$brcode','Spare Pur',' ','','0')";
     mysqli_query($db,$sql) or die('Led Post Err1'.mysqli_error($db));
  
   $sql="insert into journal2 values('$prdt','$jvno','Cr','$cname','0','$taxdrrow[0]','Spare purno: $purid,PBill:$pbillno','$brcode','Spare Pur',' ','','0')";
     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)
{
 $sql="insert into journal2 values('$entrydate','$jvno','Dr','Round Off','".abs($dif)."', '0','Round of adjusted JC No: $jcno','$brcode','JC Spare Invoice','jcno','$jcno','$purid')";
   mysqli_query($db,$sql) or die("J V Write error dif1:".mysqli_error($db));
}
else if($dif>0)
{
   $sql="insert into journal2 values('$entrydate','$jvno','Cr','Round Off','0','".abs($dif)."','Round of adjusted JC No: $jcno','$brcode','JC Spare Invoice','jcno','$jcno','$purid')";
    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($pmode=='CASH')
{
$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='$cname' and brcode='$brcode'");
$jpostrow=mysqli_fetch_row($jpostres);
$jvno++;

echo $sql="insert into journal2 values('$entrydate','$jvno','Dr','Cash','$r1[0]','0','Purchase $cname(jcno:$jcno,CR:$crno)','$brcode','CR','jpurchase','$jcno','$crno')";
mysqli_query($db,$sql) or die(mysqli_error($db));

echo  $sql="insert into journal2 values('$entrydate','$jvno','Cr','$cname','0','$r1[0]','Purchase $cname(jcno:$jcno)','$brcode','CR','jpurchase','$jcno','$crno')";
mysqli_query($db,$sql) or die(mysqli_error($db));

 $sql="insert into cshrec values('$entrydate','$crno','Cr','$cname','0','$r1[0]','Purchase $cname(jcno:$jcno)','$brcode','$jvno','jcno','$jcno')";
mysqli_query($db,$sql) or die(mysqli_error($db));
print "<script> window.open('../accounts/payrec/prncshrec.php?no=$crno'); </script>";

} */ 






  
echo "Data saved ";
     echo "<script> document.getElementById('savecount').value=1;document.location='purchase.php';</script>";




}
}

elseif($savecount==1)
{
echo "Data already saved";
  echo "<script> document.location='purchase.php';</script>";
}





?>

