<?
session_start();
/* if(strlen($brcode)<=0)
{
 print "<script> alert('Session timed out'); parent.document.location='../ind.php'; </script>";
 return;
} */
if($savecount==0)
{
$tbl2="stocksummary1".$brcode.$user;
include ("opendb.php");
  if(($cus=='Customer name')||($reg=='Register number'))
    {
    echo "Data could not saved <br> Invalid customer name/register number";
    }
  else
  {
 

 
   $prbldt = date("Y-m-d", strtotime($prbldt));
 $prdt = date("Y-m-d", strtotime($prdt)); 
$i=0;
foreach($partcode as $a)
{
 $partcode[$i]=$a; 
 $i++;
}
$i=0;
foreach($partname as $a)
{
 $partname[$i]=$a; 
 $i++;
}
$i=0;
foreach($qty as $a)
{
 $qty[$i]=$a; 
 $i++;
}
$i=0;
foreach($unitprice as $a)
{
 $unitprice[$i]=$a; 
 $i++;
}
$i=0;
foreach($tot as $a)
{
 $tot[$i]=$a; 
 $i++;
}
$i=0;
foreach($typ as $a)
{
 $typ[$i]=$a; 
 $i++;
}
$i=0;
foreach($dis as $a)
{
 $dis[$i]=$a; 
 $i++;
}
$i=0;
foreach($unit as $a)
{
 $unit[$i]=$a; 
 $i++;
}
$i=0;
foreach($conv as $a)
{
 $conv[$i]=$a; 
 $i++;
}
$i=0;
foreach($part_type as $a)
{
 $part_type[$i]=$a; 
 $i++;
}
$i=0;
foreach($purord as $a)
{
 $purord[$i]=$a; 
 $i++;
}
$flg=0;
for($j=0;$j<$i;$j++)
{
 $k=$j+1;

 $qry=mysqli_query($db,"select * from spareprmast where partcode='$partcode[$j]' and brcode='$brcode'");
 if(mysqli_num_rows($qry)<=0)
 {
  
  print "<script>document.getElementById('myTable').rows[$k].cells[0].style.color='red';</script>";
  $flg++;
 }

  if(($qty[$j]=='')||($qty[$j]=='0'))

    {
    print "<script>document.getElementById('myTable').rows[$k].cells[0].style.color='red';</script>";
  $flg++;
    }

}
if($flg>0)
 {
  echo "<small>Check the  partname/Qty of Red marked row(s)</small>";
 }
else
 {

  $find_jv=mysqli_query($db,"select * from lubricant_purmast where prno='$prno' and brcode='$brcode'");
  $f_j=mysqli_fetch_row($find_jv);
  $j_no=$f_j[16];
 
   
 mysqli_query($db,"delete from lubricant_purmast where prno='$prno' and brcode='$brcode'");

   if($j_no >0){
 mysqli_query($db,"delete from journal2 where jvno='$j_no' and brcode='$brcode'");

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




 
 
      $flg1=0;
	for($m=0;$m<$i;$m++)
       {
    $qry1=mysqli_query($db,"select * from newpartmaster where partcode='$partname[$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]','$prno','$prdt','$qty[$m]','$unitprice[$m]','$dis[$m]','$tot[$m]','$brcode','$prbil','$prbldt','$sup','$unit[$m]','$conv[$m]','$jvno','$part_type[$m]','$jcno','$purord[$m]')");


 $flg1++;

      }  
 if($flg1==$i) 
 {

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

  $taxdrrow=mysqli_fetch_array($taxdrres);
  
    $ledger='Spare Purchase';  
    $sql="insert into journal2 values('$prdt','$jvno','Dr','$ledger','$taxdrrow[0]','0','Spare purno: $prno,PBill:$prbil','$brcode','Spare Pur',' ','','0')";
     mysqli_query($db,$sql) or die('Led Post Err1'.mysqli_error($db));

  
   $sql="insert into journal2 values('$prdt','$jvno','Cr','$sup','0','$taxdrrow[0]','Spare purno: $prno,PBill:$prbil','$brcode','Spare Pur',' ','','0')";
     mysqli_query($db,$sql) or die('Led Post Err3'.mysqli_error($db));


  



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

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

?>
