<?php
include '../../db_connect.php';

if($_GET[mode]=='insert')
{
	
			foreach($_POST as $key => $value)
			{
	        	if(!is_array($_POST[$key]))
            	$_POST[$key] = mysqli_real_escape_string($con,trim($value));
            }
			
			{
			
			$fresh 	= $_POST[fresh];
			$reprocess  = $_POST[reprocess];
			$demage = $_POST[reprocess];
			
			
			for ($i = 0; $i < count($_POST[jobid]); $i++) 
			  {
			$jobregdet="UPDATE `jobregdet` SET    `fresh` = '$_POST[fresh]', `reprocess` = '$_POST[reprocess]', `demage` ='$_POST[demage]'  WHERE entryid= '$_POST[entryid]' and `jobid` = '$_POST[jobid]' and `srno` = '$_POST[srno]' and `prodcode` = '$_POST[prodcode]'";
		  
		  		if(!mysqli_query($con,$jobregdet))
		   		{
				die('Error: ' . mysqli_error($con));
				}
				//echo $jobregdet;
			  }
			  
			  
			}
				
		    $srno_pv = $_POST['srno_pv'];
			$product_pv = $_POST['prodcode_pv'];
			$size_pv = $_POST['size_pv'];
			$issued = $_POST['issued_pv'];
			$qnty_pv = $_POST['qnty_pv'];
			$fresh_pv = $_POST['fresh_pv'];
			$reprocess_pv = $_POST['reprocess_pv'];
			$demage_pv = $_POST['demage_pv']; 


			for ($i = 0; $i < count($size_pv); $i++) 
			  {
				  
				if($size_pv[$i]!='')
				{
					$trckSql = "INSERT INTO `jobrecvar` SET `entryid` = NULL, `srno` = '$srno_pv[$i]', `refno` = '$_POST[refno]', `jobid` = '$_POST[jobid]', `stylecode` = '$product_pv[$i]', `size` = '$size_pv[$i]', `issued` = '$issued[$i]', `qty` = '$qnty_pv[$i]', `fresh_pv` = '$fresh_pv[$i]', `reprocess_pv` = '$reprocess_pv[$i]', `demage_pv` = '$demage_pv[$i]';";
					if(!mysqli_query($con,$trckSql))
					{
					die('Error: ' . mysqli_error($con));
					}
					else{
				    
					$sizeSql = "UPDATE `prodvar` SET `qnty` = 'qnty+$fresh_pv[$i]' where size = '$size_pv[$i]' and prodname = '$product_pv[$i]';";
					if(!mysqli_query($con,$sizeSql))
					{
					die('Error: ' . mysqli_error($con));
					}
					
					
					$jobdetSql = "UPDATE `jobvar` SET `fresh` = '$fresh_pv[$i]', `reprocess` = '$reprocess_pv[$i]', `demage` = '$demage_pv[$i]' where size = '$size_pv[$i]' and prodname = '$product_pv[$i]' and  `job_id` = '$_POST[jobid]';";
					if(!mysqli_query($con,$jobdetSql))
					{
					die('Error: ' . mysqli_error($con));
					}
				  
			     
					}
				}
			  } //for ($i = 0; $i < count($_POST['rmcode_pv']); $i++) 

		
		echo "Record Added Successfully!!";

}


else if($_GET[mode]=='update')
{
	         
				
			$entryid_pv = $_POST['entryid_pv'];	
		    $srno_pv = $_POST['srno_pv'];
			$product_pv = $_POST['prodcode_pv'];
			$size_pv = $_POST['size_pv'];
			$qnty_pv = $_POST['qnty_pv'];
			$fresh_pv = $_POST['fresh_pv'];
			$reprocess_pv = $_POST['reprocess_pv'];
			$demage_pv = $_POST['demage_pv']; 
	        $issued = $_POST['issued_pv'];
			
			
			
			for ($i = 0; $i < count($size_pv); $i++) 
			  {
				 
				
					$trckSql = "UPDATE `jobrecvar` SET  `srno` = '$srno_pv[$i]', `refno` = '$_POST[refno]', `jobid` = '$_POST[jobid]', `stylecode` = '$product_pv[$i]', `size` = '$size_pv[$i]', `issued` = '$issued[$i]', `qty` = '$qnty_pv[$i]', `fresh_pv` = '$fresh_pv[$i]', `reprocess_pv` = '$reprocess_pv[$i]', `demage_pv` = '$demage_pv[$i]' where `entryid` = '$entryid_pv[$i]'";
					if(!mysqli_query($con,$trckSql))
					{
					die('Error: ' . mysqli_error($con));
					}
					else{
				
					$sizeSql = "UPDATE `prodvar` SET `qnty` = 'qnty+$fresh_pv[$i]' where size = '$size_pv[$i]' and prodname = '$product_pv[$i]';";
					if(!mysqli_query($con,$sizeSql))
					{
					die('Error: ' . mysqli_error($con));
					}
					
					
					$jobdetSql = "UPDATE `jobvar` SET `fresh` = '$fresh_pv[$i]', `reprocess` = '$reprocess_pv[$i]', `demage` = '$demage_pv[$i]' where size = '$size_pv[$i]' and prodname = '$product_pv[$i]' and  `job_id` = '$_POST[jobid]';";
					if(!mysqli_query($con,$jobdetSql))
					{
					die('Error: ' . mysqli_error($con));
					}
				}
			  } //for ($i = 0; $i < count($_POST['rmcode_pv']); $i++) 
				  

		  echo "Record Updated Successfully!!!";
}


else if($_GET[mode]=='singleupdate')
{
	foreach($_POST as $key => $value){
		foreach($_POST[$key] as $key1 => $value1){
    	$_POST[$key]= $value1 ;
		}
	}
				
		$trckSql = "Update `jobrecvar` SET  `srno` = '$_POST[srno_pv]', `refno` = '$_POST[refno]', `jobid` = '$_POST[jobid]', `stylecode` = '$_POST[prodcode_pv]', `size` = '$_POST[size_pv]', `issued` = '$_POST[issued_pv]', `qty` = '$_POST[qnty_pv]', `fresh_pv` = '$_POST[fresh_pv]', `reprocess_pv` = '$_POST[reprocess_pv]', `demage_pv` = '$_POST[demage_pv]' where `entryid` = '$_POST[entryid_pv]'";
					if(!mysqli_query($con,$trckSql))
					{
					die('Error: ' . mysqli_error($con));
					}
					else{
						
					$entryid_pv = $_POST['entryid_pv'];	
					$srno_pv = $_POST['srno_pv'];
					$product_pv = $_POST['prodcode_pv'];
					$size_pv = $_POST['size_pv'];
					$qnty_pv = $_POST['qnty_pv'];
					$fresh_pv = $_POST['fresh_pv'];
					$reprocess_pv = $_POST['reprocess_pv'];
					$demage_pv = $_POST['demage_pv']; 
					 $issued = $_POST['issued_pv'];
			
			
			
			for ($i = 0; $i < count($size_pv); $i++) 
			  {
				 
				
					$trckSql = "UPDATE `jobrecvar` SET  `srno` = '$srno_pv[$i]', `refno` = '$_POST[refno]', `jobid` = '$_POST[jobid]', `stylecode` = '$product_pv[$i]', `size` = '$size_pv[$i]', `issued` = '$issued[$i]', `qty` = '$qnty_pv[$i]', `fresh_pv` = '$fresh_pv[$i]', `reprocess_pv` = '$reprocess_pv[$i]', `demage_pv` = '$demage_pv[$i]' where `entryid` = '$entryid_pv[$i]'";
					if(!mysqli_query($con,$trckSql))
					{
					die('Error: ' . mysqli_error($con));
					}
					else{
				
				//comment for confirmation
					/*$sizeSql = "UPDATE `prodvar` SET `qnty` = 'qnty+$fresh_pv[$i]' where size = '$size_pv[$i]' and prodname = '$product_pv[$i]';";
					if(!mysqli_query($con,$sizeSql))
					{
					die('Error: ' . mysqli_error($con));
					}
					
					
					$jobdetSql = "UPDATE `jobvar` SET `fresh` = '$fresh_pv[$i]', `reprocess` = '$reprocess_pv[$i]', `demage` = '$demage_pv[$i]' where size = '$size_pv[$i]' and prodname = '$product_pv[$i]' and  `job_id` = '$_POST[jobid]';";
					if(!mysqli_query($con,$jobdetSql))
					{
					die('Error: ' . mysqli_error($con));
					}*/
					
					
				}
			  } //for ($i = 0; $i < count($_POST['rmcode_pv']); $i++) 
						
						
						}
                // updatestock($con,$_POST[size_pv],$_POST[qnty_pv],$_POST[prodcode_pv]);
		
		  echo "Record Updated Successfully!!!";
			
		

}

/*else if($_GET[mode]=='delete')
{	
$sql="DELETE from jobrecvar where entryid='$_POST[entryid_pv]'";
if(!mysqli_query($con,$sql))
{
  die('Error: ' . mysqli_error($con));
}
else
echo "Record Deleted Successfully!!!";

}*/


function updatestock($con,$size_pv,$qnty,$prodname)
{
	
	for ($i = 0; $i < count($size_pv); $i++) 
			  {
					$sizeSql = "UPDATE `prodvar` SET `qnty` = 'qnty+$qnty[$i]' where size = '$size_pv[$i]' and prodname = '$prodname[$i]';";
					if(!mysqli_query($con,$sizeSql))
					{
					die('Error: ' . mysqli_error($con));
					}
					
				echo $sizeSql;
			  }
			 
	
}
mysqli_close($con);
?>
