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


   elseif($pmode=='')
    {
    echo "Data could not saved <br> Invalid Payment Mode";
    }

 elseif($model=='')
    {
    echo "Data could not saved <br> Invalid Vehicle Model";
    }
  else
  {
 

 
   

$i=0;
foreach($partname as $a)
{
 $partname[$i]=$a; 
 $i++;
}
$i=0;
foreach($npartname as $a)
{
 $npartname[$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($sperson as $a)
{
 $sperson[$i]=$a; 
 $i++;
}
$flg=0;
$spersonflg=0;
for($j=0;$j<$i;$j++)
{
 $k=$j+1;
 $qry=mysqli_query($db,"select * from $tbl2 where partname='$partname[$j]' and type='$typ[$j]' and brcode='$brcode'");
 if(mysqli_num_rows($qry)<=0)
 {
  // echo "select * from $tbl2 where partname='$partname[$j]' and type='$typ[$j]' and brcode='$brcode'"."<br>";
  print "<script>document.getElementById('myTable').rows[$k].cells[0].style.color='red';</script>";
  $flg++;
 }

$qry1=mysqli_query($db,"select * from $tbl2 where partname='$npartname[$j]' and type='$typ[$j]' and brcode='$brcode'");

 if(mysqli_num_rows($qry1)<=0)
 {
  // echo "select * from $tbl2 where partname='$npartname[$j]' and type='$typ[$j]' and brcode='$brcode'"."<br>";
  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($sperson[$j]=='')

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


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

elseif($spersonflg>0)
 {
  echo "<small>Select the SalesPerson of Red marked row(s)</small>";
 }
else
 {

  mysqli_query($db,"delete from invoice where invno='$invno' and brcode='$brcode'");

 

 

      $flg1=0;
	for($m=0;$m<$i;$m++)
       {
   $qry1=mysqli_query($db,"select * from newpartmaster where partname='$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 invoice (invno,invdate,customer,vehno,item,qty,unitprice,dis,total,flag,group1,brcode,salesman,group2,pmode,chasisno,model,org_item,coup_no) values('$invno','$invdt','$cus','$reg','$partname[$m]','$qty[$m]','$unitprice[$m]','$dis[$m]','$tot[$m]','$typ[$m]','$row1[2]','$brcode','$sperson[$m]','$row2[1]','$pmode','$chno','$model','$npartname[$m]','$coup_no')");
 $flg1++;

      }  
 if($flg1==$i) 
 {
  echo "Data Updated";
  echo "<script> document.getElementById('savecount').value=1;document.location='invoicenew.php';</script>";
 }  
  }

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

?>
