<?
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'];
$pmode=$_REQUEST['pmode'];
if($savecount==0)
{
	if(($cus=='-select-')||($cus==''))
    	{
    		echo "Data could not saved <br> Invalid branch name";
    	}
	elseif(($pmode=='')||($pmode=='-select-'))
    	{
    		echo "Data could not saved <br> Invalid Payment Mode";
    	}
  	else
  	{
 	$i=0;
	$partname=$_REQUEST['partname'];
	foreach($partname as $a)
	{
 		$partname[$i]=$a; 
		$i++;
	}
 	if(count(array_unique($partname))<count($partname))
	{
    	}  
	$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++;
	}
	$i=0;
	$unitprice=$_REQUEST['unitprice'];
	foreach($unitprice as $a)
	{
 		$unitprice[$i]=$a; 
 		$i++;
	}
	$i=0;
	$tot=$_REQUEST['tot'];
	foreach($tot as $a)
	{
		$tot[$i]=$a; 
		$i++;
	}
	
	$flg=0;
	$spersonflg=0;
	$totflg=0;

$sq="CREATE TABLE IF NOT EXISTS pordersprtmp(
  id int(11) NOT NULL AUTO_INCREMENT,
  invno varchar(11) DEFAULT NULL,
  invdate date DEFAULT NULL,
  customer varchar(100) DEFAULT NULL,
  cusid varchar(50) DEFAULT NULL,
  item varchar(100) DEFAULT NULL,
  item_code varchar(100) DEFAULT NULL,
  qty double(10,2) DEFAULT NULL,
  unitprice double(10,2) DEFAULT NULL,
  total double(10,2) DEFAULT NULL,
  brcode varchar(50) DEFAULT NULL,
  pmode varchar(100) DEFAULT NULL,
  received varchar(10) DEFAULT NULL,
  tobrcode varchar(100) DEFAULT NULL,
  PRIMARY KEY (id)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=latin1";
mysqli_query($db,$sq);



	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++;
    		}
   		if(($tot[$j]=='')||($tot[$j]=='0'))
    		{
    			print "<script>document.getElementById('myTable').rows[$k].cells[0].style.color='red';</script>";
     			$totflg++;
    		}
                 $sql="select partcode from partmaster where partcode='$partcode[$j]'";
                $rsq=mysqli_query($db,$sql);
                $countnm=mysqli_num_rows($rsq);
                if($countnm==0)
                {
                           $spersonflg++;
                }
	}
	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($spersonflg>0)
 	{
  		echo "<small>Item Code dosn't exists</small>";
 	}
	else
 	{
		$re1=mysqli_query($db,"select inv_prefix from branch where brcode='$brcode'");
		$row=mysqli_fetch_array($re1);
		$mprefix = $row[0];
  		$re=mysqli_query($db,"select max(invno) from pordersprtmp where brcode='$brcode' ");
		if(mysqli_num_rows($re)<1)
		{
			$inv=$mprefix.'000001'; 
		}
		else
		{
			$row=mysqli_fetch_array($re);
		       $inv=substr($row[0],-6)+1;
			$inv=substr('000000'.$inv,-6);
			if($inv==0)
			{
				$inv='000001'; 
			}
			$inv=$mprefix.$inv; 
		}
	 	
	 	$flg1=0;
		
		for($m=0;$m<$i;$m++)
       	       {
	  		
			$invdt=$_REQUEST['invdt'];			
			$reg=$_REQUEST['reg'];			
                        $refno=$_REQUEST['refno'];
                        
                  $sql="insert into pordersprtmp(invno,invdate,customer,cusid,item,item_code,qty,unitprice,total,brcode,pmode,received,tobrcode) values('$inv','$invdt','$cus','$reg','$partname[$m]','$partcode[$m]','$qty[$m]','$unitprice[$m]','$tot[$m]','$brcode','$pmode','N','$cus')";
echo $sql;
			mysqli_query($db,"insert into pordersprtmp(invno,invdate,customer,cusid,item,item_code,qty,unitprice,total,brcode,pmode,received,tobrcode) values('$inv','$invdt','$cus','$reg','$partname[$m]','$partcode[$m]','$qty[$m]','$unitprice[$m]','$tot[$m]','$brcode','$pmode','N','$cus')");
			

		       $flg1++;
	      	} 
		
		
		
		
		
		
		if($flg1==$i) 
 		{
			
			echo "Data  saved with invoice number:".$inv;
			echo "<script> document.getElementById('savecount').value=1;document.location='purchsordr.php';</script>";
		}  
 	}
	}
}
elseif($savecount==1)
{
	echo "Data already saved";
  	echo "<script> document.location='purchsordr.php';</script>";
}
?>
