<?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];$jobid = $_POST[jobid];$srno = $_POST[srno];
			$cat = $_POST[cat];$subcode = $_POST[subcode];$colorcode = $_POST[colorcode];$shortdesc = $_POST[shortdesc];$sizeset = $_POST[sizeset];
			$set = $_POST[set];$ratiotota = $_POST[ratiotota];$qty = $_POST[qty];$fresh = $_POST[fresh];$reprocess = $_POST[reprocess];
			$demage = $_POST[demage]; $prodcode=$_POST[prodcode];
			
			for ($i = 0; $i < count($srno); $i++) 
			  {
$inventoryregdet ="INSERT INTO `inventoryregdet` SET `entryid` = '',  `jobid` = '$jobid[$i]', `srno` = '$srno[$i]', `prodcode` = '$prodcode[$i]', `cat` = '$cat[$i]', `subcode` = '$subcode[$i]', `colorcode` = '$colorcode[$i]' , `shortdesc` = '$shortdesc[$i]', `sizeset` = '$sizeset[$i]', `set` = '$set[$i]',`ratiotot` = '$ratiotota[$i]', `qty` = '$qty[$i]', `fresh` = '$fresh[$i]', `reprocess` = '$reprocess[$i]', `demage` ='$demage[$i]' , `pcost` = '$_POST[rate]', `pamt` = '$_POST[amount]' ";
		  
		  		if(!mysqli_query($con,$inventoryregdet))
		   		{
				die('Error: ' . mysqli_error($con));
				}
				
			  }
			  
	
		
		  {
			  foreach($_POST as $key => $value){
			  if(!is_array($_POST[$key]))
	          $_POST[$key] = mysqli_real_escape_string($con,trim($value));
	          }
			  
			$size=$_POST['size_pv'];
			$qnty=$_POST['qnty_pv'];
			$fresh=$_POST['fresh_pv'];
			$reprocess=$_POST['reprocess_pv'];
			$demage=$_POST['demage_pv'];
			$prodcode_pv=$_POST['prodcode'];
			
			
			for ($i = 0; $i < count($_POST['size_pv']); $i++) 
			  {
				if($size[$i]!='')
				{
					$sql = "INSERT INTO inventoryvar (`entryid`, `jobid`,`srno`, `prodname`, `size`, `qnty`, `fresh`, `reprocess`, `demage`) VALUES (NULL, '$_POST[jobid]','$_POST[srno]','$_POST[prodcode]', '$size[$i]', '$qnty[$i]', '$fresh[$i]', '$reprocess[$i]', '$demage[$i]')";
					echo $sql;
					if(!mysqli_query($con,$sql))
					{
					die('Error: ' . mysqli_error($con));
					}
					
				}
			  } // for ($i = 0; $i < count($_POST['size_pv']); $i++) 

		}
		
		/****** UPDATE STOCK FROM FINISHED PRODUCT START****/
			{
				  foreach($_POST as $key => $value){
				  if(!is_array($_POST[$key]))
				  $_POST[$key] = mysqli_real_escape_string($con,trim($value));
				  }
				$issuedqty = $_POST['issuedqty'];
				$prodcode_pv = $_POST['prodcode_pv'];
				$size_pv = $_POST['size_pv'];
				$qnty_pv = $_POST['qnty_pv'];
				
				
				for ($i = 0; $i < count($size_pv); $i++) 
				{
					
				$qnty_pvsql ="UPDATE prodvar SET  `qnty` = `qnty`-'$qnty_pv[$i]' where `size` = '$size_pv[$i]'  and `prodname` = '$prodcode_pv[$i]'  ";
				  if(!mysqli_query($con,$qnty_pvsql))
				  {
				  die('Error: ' . mysqli_error($con));
				  }
				 
				}
			
		   }
		
		/****** UPDATE STOCK FROM FINISHED PRODUCT END****/
		
		/****** UPDATE IN FINISHED PRODUCT JOB START****/
		{
				  
				  foreach($_POST as $key => $value){
				  if(!is_array($_POST[$key]))
				  $_POST[$key] = mysqli_real_escape_string($con,trim($value));
				  }
				$issuedqty = $_POST['issuedqty'];
				$prodcode_pv = $_POST['prodcode_pv'];
				$size_pv = $_POST['size_pv'];
				$qnty_pv = $_POST['qnty_pv'];
				
				
				for ($i = 0; $i < count($size_pv); $i++) 
				{
					
				$qnty_pvsql ="UPDATE jobvar SET  `qnty` = `qnty`-'$qnty_pv[$i]' where `size` = '$size_pv[$i]' and `job_id` = '$_POST[batchno]'  and `prodname` = '$prodcode_pv[$i]'  ";
				  if(!mysqli_query($con,$qnty_pvsql))
				  {
				  die('Error: ' . mysqli_error($con));
				  }
				 
				}
			
		   }
		/****** UPDATE IN FINISHED PRODUCT JOB END****/
		
		 
}





else if($_GET[mode]=='update')
{
		 
		  $sql="UPDATE `inventoryregdet` SET   `jobid` = '$_POST[jobid]', `srno` = '$_POST[srno]', `prodcode` = '$_POST[prodcode]', `cat` = '$_POST[cat]', `subcode` = '$_POST[subcode]', `colorcode` ='$_POST[colorcode]' , `shortdesc` = '$_POST[shortdesc]', `sizeset` = '$_POST[sizeset]', `set` = '$_POST[set]',`ratiotot` = '$_POST[ratiotota]', `qty` = '$_POST[qty]', `fresh` = '$_POST[fresh]', `reprocess` = '$_POST[reprocess]', `demage` ='$_POST[demage]' , `pcost` = '$_POST[rate]', `pamt` = '$_POST[amount]' WHERE entryid= '$_POST[entryid]'";
		  
		  if(!mysqli_query($con,$sql))
		   {
			die('Error: ' . mysqli_error($con));
			}
			 else
		  	{
	
				
				foreach($_POST as $key => $value){
				if(!is_array($_POST[$key]))
				$_POST[$key] = mysqli_real_escape_string($con,trim($value));
				}	
			$size      =$_POST['size_pv'];
			$qnty      =$_POST['qnty_pv'];
			$fresh     =$_POST['fresh_pv'];
			$reprocess =$_POST['reprocess_pv'];
			$demage    =$_POST['demage_pv'];
			$entryid_pv=$_POST['entryid_pv'];
			$prodcode_pv = $_POST['prodcode_pv'];
			
			for ($i = 0, $ser=0; $i < count($_POST['size_pv']); $i++,$ser++) 
			  {
				
					$sizeSql = "UPDATE inventoryvar SET `srno` = '$ser', `prodname` = '$prodcode_pv[$i]', `size` ='$size[$i]', `qnty` = '$qnty[$i]', `fresh` = '$fresh[$i]', `reprocess` = '$reprocess[$i]', `demage` ='$demage[$i]' where `jobid` = '$_POST[jobid]' and `entryid` = '$entryid_pv[$i]' ";
					if(!mysqli_query($con,$sizeSql))
					{
					die('Error: ' . mysqli_error($con));
					}
				//echo $sizeSql;
		 } 
					
	}
		  
		   
		  
		  	
		      /****** UPDATE IN FINISHED PRODUCT JOB START****/
		{
				  
				  foreach($_POST as $key => $value){
				  if(!is_array($_POST[$key]))
				  $_POST[$key] = mysqli_real_escape_string($con,trim($value));
				  }
				$issuedqty = $_POST['issuedqty'];
				$prodcode_pv = $_POST['prodcode_pv'];
				$size_pv = $_POST['size_pv'];
				$qnty_pv = $_POST['qnty_pv'];
				
				
				for ($i = 0; $i < count($size_pv); $i++) 
				{
					
				$qnty_pvsql ="UPDATE jobvar SET  `qnty` = `qnty`-'$qnty_pv[$i]' where `size` = '$size_pv[$i]' and `job_id` = '$_POST[batchno]'  and `prodname` = '$prodcode_pv[$i]'  ";
				  if(!mysqli_query($con,$qnty_pvsql))
				  {
				  die('Error: ' . mysqli_error($con));
				  }
				 
				}
			
		   }
		/****** UPDATE IN FINISHED PRODUCT JOB END****/
		 
	
		}




else if($_GET[mode]=='delete')
{
  foreach($_POST as $key => $value){
	  if(!is_array($_POST[$key]))
	  $_POST[$key] = mysqli_real_escape_string($con,trim($value));
	  }
	  $entryid = $_POST['entryid'];
			for ($i = 0; $i < count($_POST['entryid']); $i++) 
			  {	
$sql="DELETE from inventoryregdet where entryid=' $entryid[$i]'";

	 if(!mysqli_query($con,$sql))
	  {
	  die('Error: ' . mysqli_error($con));
	  }
  
 else{
			  
			  {
			  	foreach($_POST as $key => $value){
	 			if(!is_array($_POST[$key]))
				$_POST[$key] = mysqli_real_escape_string($con,trim($value));
				}	
			$size=$_POST['size_pv'];
			$qnty=$_POST['qnty_pv'];
			$fresh=$_POST['fresh_pv'];
			$reprocess=$_POST['reprocess_pv'];
			$demage=$_POST['demage_pv'];
			$entryid = $_POST['entryid_pv'];
			
			for ($i = 0; $i < count($_POST['entryid_pv']); $i++) 
			  {
				
					$deletjob = "DELETE from inventoryvar where `entryid` = $entryid[$i] ";
					if(!mysqli_query($con,$deletjob))
					{
					die('Error: ' . mysqli_error($con));
					}
					//echo $deletjob ;
				
			  } 
			  
		}
			
 }
}
			
  
echo "Record Deleted Successfully!!!";

}
mysqli_close($con);
?>
