<?
session_start();
$brcode=$_SESSION['brcode'];
$user=$_SESSION['user'];

if(strlen($user)<=0)
{
  print "<script> alert('Invalid User'); document.location='inde.php' </script>";
  return;
}
include_once('opendb.php');
$savecount=$_REQUEST['savecount'];
$cus=$_REQUEST['cus'];

if($savecount==0)
{
	if($cus=='')
    	{
    		print "<script>aler('select a customer')</script>";
    	}
	
  	else
  	{
            $i=0;
            $partname=$_REQUEST['partname'];
            foreach($partname as $a)
                {
                    $partname[$i]=$a;
                    $i++;
                }
            $i=0;
            $partcode=$_REQUEST['partcode'];
            foreach($partcode as $a)
                {
                    $partcode[$i]=$a;
                    $i++;
                }
            $i=0;
            $qty=$_REQUEST['qty'];
            foreach($qty as $a)
                {
                    $qty[$i]=$a;
                    $i++;
                }
     
          

            $flg=0;
            $spflg=0;
            $totflg=0;
            for($j=0;$j<$i;$j++)
                {
                    $k=$j+1;
                    if(($qty[$j]=='')||($qty[$j]=='0'))
                        {
                            print "<script>document.getElementById('myTable').rows[$k].cells[0].style.color='red';</script>";
                            $flg++;
                        }
                   
                    $qry=mysqli_query($db,"select * from partmaster where partcode ='$partcode[$j]'");
                    if(mysqli_num_rows($qry)<=0)
                        {
                            print "<script>document.getElementById('myTable').rows[$k].cells[0].style.color='red';</script>";
                            $spflg++;
                        }
                }
            if($flg>0)
                {
                    echo "<small>Check the itemname/Qty of Red marked row(s)</br></small>";
                }
            elseif($totflg>0)
                {
                    echo "<small>Enter Total of Red marked row(s)</small>";
                }
            elseif($spflg>0)
                {
                    echo "<small>Check the itemcode of Red marked row(s)</br></small>";
                }
            else
                {
                     $pres=mysqli_query($db,"select max(orderno) from saleorder_spare where brcode='$brcode'");
                      if(mysqli_num_rows($pres)<1)
                      {
	                 $orderno='000001'; 
                      }
                      else
                      {
	                $row=mysqli_fetch_array($pres);
                        $orderno=substr($row[0],-6)+1;
	                $orderno=substr('000000'.$orderno,-6);
	                  if($orderno==0)
	                   {
		            $orderno='000001'; 
	                   }
	              $orderno=$orderno; 
                       }


                    $flg1=0;
                    
                    for($m=0;$m<$i;$m++)
                        {
                            $msno=$_REQUEST['msno'];
                            $invdt=$_REQUEST['invdt'];
		
                            mysqli_query($db,"insert into saleorder_spare (partcode,partname,qty,date,orderto,brcode,orderno,sqty,msno,closed,reas) values('$partcode[$m]','$partname[$m]','$qty[$m]','$invdt','$cus','$brcode','$orderno','$qty[$m]','$msno','N','-')");
                            

                            $flg1++;
                        }

            


                    if($flg1==$i)
                        {
                           
                            
                                   

			
		echo "<script>alert('Data saved with invoice number:".$orderno."')</script>";
		echo "<script> document.getElementById('savecount').value=1;document.location='invoice_sale.php';</script>";
                        }
                }
	}
}
elseif($savecount==1)
{
	echo "Data already saved";
  	echo "<script> document.location='invoice_sale.php';</script>";
}
?>
