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

if($_GET[mode]=='insert')
{

	$sql="UPDATE `jobreg` SET  `ptotal` = '$_POST[tpcost]', `vat` = '$_POST[vat]', `pnamount` = '$_POST[grtotal]' WHERE `jobid` = $_POST[jobid]";
		
		if (mysqli_query($con,$sql)){
		$sql="INSERT INTO `jobregdet` ( `entryid`, `jobid`, `srno`, `prodcode`, `cat`, `subcode`, `colorcode`, `shortdesc`, `sizeset`, `set`, `ratiotot`, `qty`, `fresh`, `reprocess`, `demage`, `pcost`, `pamt`) VALUES ('', '$_POST[jobid]', '$_POST[srno]', '$_POST[prodcode]', '$_POST[cat]', '$_POST[subcode]', '$_POST[colorcode]', '$_POST[shortdesc]', '$_POST[sizeset]', '$_POST[set]','$_POST[ratiotota]', '$_POST[qty]', '$_POST[fresh]', '$_POST[reprocess]', '$_POST[demage]' ,'$_POST[rate]', '$_POST[amount]')";
		
		if (!mysqli_query($con,$sql))
		  {
		  die('Error: ' . mysqli_error($con));
		  }
		  else
		  {
			$size=$_POST['size_pv'];
			$qnty=$_POST['qnty_pv'];
			$fresh=$_POST['fresh_pv'];
			$reprocess=$_POST['reprocess_pv'];
			$demage=$_POST['demage_pv'];
			
			
			for ($i = 0; $i < count($_POST['size_pv']); $i++) 
			  {
				if($size[$i]!='')
				{
					$sql = "INSERT INTO jobvar (`entryid`, `job_id`,`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++) 
			  
			  
			  
			  {
				  
		  	 foreach($_POST as $key => $value){
	           if(!is_array($_POST[$key]))
              $_POST[$key] = mysqli_real_escape_string($con,trim($value));
                }	
		    $jobid=$_POST['jobid'];
			$issuedsize=$_POST['issuedsize'];
			$orderidissued=$_POST['orderidissued'];
			$totalqty_pis=$_POST['totalqty_pis'];
			$prodname_pis=$_POST['prodname_pis'];
			$issuedqty=$_POST['issuedqty'];
			$orderdate = date("Y-m-d");
			
			
			for ($i = 0; $i < count($issuedsize); $i++) 
			  {
				  
				if($issuedsize[$i]!='')
				{
					$trckSql = "INSERT INTO `tracking` SET `entryid` = NULL , `jobid` = '$_POST[jobid]', `orderid` = '$orderidissued[$i]', `prodname` = '$prodname_pis[$i]' , `tracdate` = '".date("Y-m-d")."',`size` = '$issuedsize[$i]', `type` = 'JOBREG', `total` = '$totalqty_pis[$i]', `issue` = '$issuedqty[$i]' ";
					if(!mysqli_query($con,$trckSql))
					{
					die('Error: ' . mysqli_error($con));
					}
					//echo $trckSql;
				}
			  } //for ($i = 0; $i < count($_POST['rmcode_pv']); $i++) 
				  
			  }
			  
			$rmentryid=$_POST['rmentryid_pv'];
			$rmcode=$_POST['rmcode_pv'];
			$rmunit=$_POST['rmunit_pv'];
			$rmrequired=$_POST['rmrequired_pv'];
			$rmissued=$_POST['rmissued_pv'];
			$rmconsumed=$_POST['rmconsumed_pv'];
			$rmreturned = $_POST['rmreturned_pv'];
			
			
			for ($i = 0; $i < count($_POST['rmcode_pv']); $i++) 
			  {
				if($rmcode[$i]!='')
				{
					$rmSql = "INSERT INTO `rawmatdet` SET `entryid` = $rmentryid[$i] , `jobid` = '$_POST[jobid]', `prodcode` = '$_POST[prodcode]',`rmcode` = '$rmcode[$i]',`required` = '$rmrequired[$i]', `issued` = '$rmissued[$i]', `consumed` = '$rmconsumed[$i]', `returned` = '$rmreturned[$i]' ";
					if(!mysqli_query($con,$rmSql))
					{
					die('Error: ' . mysqli_error($con));
					}
					
				}
			  } //for ($i = 0; $i < count($_POST['rmcode_pv']); $i++) 
			  
			  
			  
			}
		 
		  
		 
	//if(mysqli_query($con,"UPDATE `prodreg` SET  `stock` = stock - $_POST[qty] where code='$_POST[code]' and cat ='PRODUCT'"))
			
	{
		$issuedqty = $_POST['issuedqty'];
		$entryid = $_POST['entryid'];
		for ($i = 0; $i < count($_POST['orderidissued']); $i++) 
		{
			$outputsql ="UPDATE sordervar SET  `pissued` = '$issuedqty' where entryid = '$entryid[$i]' ";
		if(!mysqli_query($con,$outputsql))
			{
			die('Error: ' . mysqli_error($con));
			}
		}
		echo "Record Added Successfully!!";
	}
}


	
}

/*
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];
			for ($i = 0; $i < count($_POST[jobid]); $i++) 
			  {
			$jobregdet="INSERT INTO `jobregdet`  `entryid` = '',  `jobid` = '$jobid[$i]', `srno` = '$srno[$i]', `prodcode` = '$_POST[prodcode]', `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,$jobregdet))
		   		{
				die('Error: ' . mysqli_error($con));
				}
				//echo $jobregdet;
			  }
			  
			  
			}
	
	
		
		  {
			$size=$_POST['size_pv'];
			$qnty=$_POST['qnty_pv'];
			$fresh=$_POST['fresh_pv'];
			$reprocess=$_POST['reprocess_pv'];
			$demage=$_POST['demage_pv'];
			
			
			for ($i = 0; $i < count($_POST['size_pv']); $i++) 
			  {
				if($size[$i]!='')
				{
					$sql = "INSERT INTO jobvar (`entryid`, `job_id`,`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++) 
			  
			  
			  
			  {
				  
		   foreach($_POST as $key => $value){
	           if(!is_array($_POST[$key]))
              $_POST[$key] = mysqli_real_escape_string($con,trim($value));
                }	
		    $jobid=$_POST['jobid'];
			$issuedsize=$_POST['issuedsize'];
			$orderidissued=$_POST['orderidissued'];
			$totalqty_pis=$_POST['totalqty_pis'];
			$prodname_pis=$_POST['prodname_pis'];
			$issuedqty=$_POST['issuedqty'];
			$orderdate = date("Y-m-d");
			
			
			for ($i = 0; $i < count($issuedsize); $i++) 
			  {
				  
				if($issuedsize[$i]!='')
				{
					$trckSql = "INSERT INTO `tracking` SET `entryid` = NULL , `jobid` = '$_POST[jobid]', `orderid` = '$orderidissued[$i]', `prodname` = '$prodname_pis[$i]' , `tracdate` = '".date("Y-m-d")."',`size` = '$issuedsize[$i]', `type` = 'JOBREG', `total` = '$totalqty_pis[$i]', `issue` = '$issuedqty[$i]' ";
					if(!mysqli_query($con,$trckSql))
					{
					die('Error: ' . mysqli_error($con));
					}
					//echo $trckSql;
				}
			  } //for ($i = 0; $i < count($_POST['rmcode_pv']); $i++) 
				  
			  }
			  
			$rmentryid=$_POST['rmentryid_pv'];
			$rmcode=$_POST['rmcode_pv'];
			$rmunit=$_POST['rmunit_pv'];
			$rmrequired=$_POST['rmrequired_pv'];
			$rmissued=$_POST['rmissued_pv'];
			$rmconsumed=$_POST['rmconsumed_pv'];
			$rmreturned = $_POST['rmreturned_pv'];
			
			
			for ($i = 0; $i < count($_POST['rmcode_pv']); $i++) 
			  {
				if($rmcode[$i]!='')
				{
					$rmSql = "INSERT INTO `rawmatdet` SET `entryid` = $rmentryid[$i] , `jobid` = '$_POST[jobid]', `prodcode` = '$_POST[prodcode]',`rmcode` = '$rmcode[$i]',`required` = '$rmrequired[$i]', `issued` = '$rmissued[$i]', `consumed` = '$rmconsumed[$i]', `returned` = '$rmreturned[$i]' ";
					if(!mysqli_query($con,$rmSql))
					{
					die('Error: ' . mysqli_error($con));
					}
					
				}
			  } //for ($i = 0; $i < count($_POST['rmcode_pv']); $i++) 
			  
			  
			  
			}
		 
		  
		 
	//if(mysqli_query($con,"UPDATE `prodreg` SET  `stock` = stock - $_POST[qty] where code='$_POST[code]' and cat ='PRODUCT'"))
			
	{
		$issuedqty = $_POST['issuedqty'];
		$entryid = $_POST['entryid'];
		for ($i = 0; $i < count($_POST['orderidissued']); $i++) 
		{
			$outputsql ="UPDATE sordervar SET  `pissued` = '$issuedqty' where entryid = '$entryid[$i]' ";
		if(!mysqli_query($con,$outputsql))
			{
			die('Error: ' . mysqli_error($con));
			}
		}
		echo "Record Added Succesfully!!";
	}


}*/


else if($_GET[mode]=='update')
{
	
		$sql="UPDATE `jobreg` SET  `ptotal` = '$_POST[tpcost]', `vat` = '$_POST[vat]', `pnamount` = '$_POST[grtotal]' WHERE `jobid` = $_POST[jobid]";
		
		  if (mysqli_query($con,$sql)){
		  $sql="UPDATE `jobregdet` 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
		  {
			  
			  if(mysqli_query($con,"delete from tracking where `jobid` = '$_POST[jobid]' "))
		 
			{
				
				foreach($_POST as $key => $value){
	           	  if(!is_array($_POST[$key]))
             	  $_POST[$key] = mysqli_real_escape_string($con,trim($value));
                }	
		    $jobid=$_POST['jobid'];
			$issuedsize=$_POST['issuedsize'];
			$orderidissued=$_POST['orderidissued'];
			$totalqty_pis=$_POST['totalqty_pis'];
			$prodname_pis=$_POST['prodname_pis'];
			$buyercode_pis=$_POST['buyercode_pis'];
			$orderdate=$_POST['orderdate_pis']; 
			$issuedqty=$_POST['issuedqty'];
			
			
			
			for ($i = 0; $i < count($issuedsize); $i++) 
			  {
				  
				if($issuedsize[$i]!='')
				{
					$trckSql = "INSERT INTO `tracking` SET `entryid` = NULL , `jobid` = '$_POST[jobid]', `orderid` = '$orderidissued[$i]' , `prodname` = '$prodname_pis[$i]' ,`tracdate` = '".date("Y-m-d")."',`size` = '$issuedsize[$i]', `type` = 'JOBREG', `total` = '$totalqty_pis[$i]', `issue` = '$issuedqty[$i]', `buyer` = '$buyercode_pis[$i]', `orderdate` = STR_TO_DATE('$orderdate[$i]','%d/%m/%Y') ";
					if(!mysqli_query($con,$trckSql))
					{
					die('Error: ' . mysqli_error($con));
					}
					//echo $trckSql;
				}
			  } //for ($i = 0; $i < count($_POST['rmcode_pv']); $i++) 
				  
			}
			  
			  
			/* if(mysqli_query($con,"delete from jobvar where `job_id` = '$_POST[jobid]' and  `prodname` = '$_POST[prodcode]' "))
	   
			{
				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'];
			
			
			for ($i = 0, $ser=0; $i < count($_POST['size_pv']); $i++,$ser++) 
			  {
				if($size[$i]!='')
				{
					$sizeSql = "INSERT INTO jobvar (`entryid`, `job_id`,`srno`, `prodname`, `size`, `qnty`, `fresh`, `reprocess`, `demage`) VALUES (NULL, '$_POST[jobid]','$ser','$_POST[prodcode]', '$size[$i]', '$qnty[$i]', '$fresh[$i]', '$reprocess[$i]', '$demage[$i]')";
					if(!mysqli_query($con,$sizeSql))
					{
					die('Error: ' . mysqli_error($con));
					}
					
				}
				
			  } 
			}*/
			
			{  
			 //if(mysqli_query($con,"delete from jobvar where `job_id` = '$_POST[jobid]' and  `prodname` = '$_POST[prodcode]' "))
	   
			//{
				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'];
			
			for ($i = 0, $ser=0; $i < count($_POST['size_pv']); $i++,$ser++) 
			  {
				if($size[$i]!='')
				{
					
					$sizeSql = "update  jobvar set  `job_id` = '$_POST[jobid]', `srno` = '$ser', `prodname` = '$_POST[prodcode]', `size` = '$size[$i]', `qnty` = '$qnty[$i]', `fresh` = '$fresh[$i]', `reprocess` = '$reprocess[$i]', `demage` ='$demage[$i]' where `entryid` = '$entryid_pv[$i]';";
					if(!mysqli_query($con,$sizeSql))
					{
					die('Error: ' . mysqli_error($con));
					}
					//echo $sizeSql;
					
				}
				
			  } 
		//	}
			}
			
			
			
			
			 { 
			$rmentryid=$_POST['rmentryid_pv'];
			$rmcode=$_POST['rmcode_pv'];
			$rmunit=$_POST['rmunit_pv'];
			$rmrequired=$_POST['rmrequired_pv'];
			$rmissued=$_POST['rmissued_pv'];
			$rmconsumed=$_POST['rmconsumed_pv'];
			$rmreturned = $_POST['rmreturned_pv'];
			$msrmissued = $_POST['msrmissued_pv'];
			
			
			for ($i = 0; $i < count($_POST['rmcode_pv']); $i++) 
			  {
				if($rmcode[$i]!='')
				{
					$rmSql = "UPDATE `rawmatdet` SET  `required` = '$rmrequired[$i]', `issued` = '$rmissued[$i]', `consumed` = '$rmconsumed[$i]', `returned` = '$rmreturned[$i]' WHERE  `entryid` = $rmentryid[$i] and `jobid` = '$_POST[jobid]' and `prodcode` = '$_POST[prodcode]' and `rmcode` = '$rmcode[$i]'";
					if(!mysqli_query($con,$rmSql))
					{
					die('Error: ' . mysqli_error($con));
					}
					else
					{
						//echo $rmSql;
							$sqlinner = "update rawmatreg set qty = qty + '$msrmissued[$i]' where  `code` = '$rmcode[$i]'";
							if(!mysqli_query($con,$sqlinner))
							{
									die('Error: ' . mysqli_error($con));
							}
					}
					
				}
			  } //for ($i = 0; $i < count($_POST['rmcode_pv']); $i++) 
			}
		  }
		  
		   
		  
		  	if($_POST['qnty_pv']=='')
		      {
				  foreach($_POST as $key => $value){
				  if(!is_array($_POST[$key]))
				  $_POST[$key] = mysqli_real_escape_string($con,trim($value));
				  }
				$issuedqty = $_POST['issuedqty'];
				$entryid_pv = $_POST['entryid_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` = '$issuedqty[$i]' where `size` = '$size_pv[$i]' and `job_id` = '$_POST[jobid]'  and `prodname` = '$_POST[prodcode]'  ";
				  if(!mysqli_query($con,$qnty_pvsql))
				  {
				  die('Error: ' . mysqli_error($con));
				  }
				 
				}
			
		 }
		 
		{
			foreach($_POST as $key => $value){
	 		if(!is_array($_POST[$key]))
			$_POST[$key] = mysqli_real_escape_string($con,trim($value));
			}	
		 $entryid = $_POST['entryid_pis'];
		$issuedqty = $_POST['issuedqty'];
		$prodname_pis = $_POST['prodname_pis'];
		  for ($i = 0; $i < count($entryid); $i++) 
		  {
			  if(!mysqli_query($con,"UPDATE sordervar SET  `pissued` =  '$issuedqty[$i]' where entryid = $entryid[$i] "))
			  {
			  die('Error: ' . mysqli_error($con));
			  }
		  
		  }
		
		  echo "Record Updated Susscessfully!!!";
			}
		}

}


else if($_GET[mode]=='delete')
{
$sql="DELETE from jobregdet 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 = $_POST['entryid_pv'];
			
			for ($i = 0; $i < count($_POST['entryid_pv']); $i++) 
			  {
				
					$deletjob = "DELETE from jobvar where `entryid` = $entryid[$i] ";
					if(!mysqli_query($con,$deletjob))
					{
					die('Error: ' . mysqli_error($con));
					}
					//echo $deletjob ;
				
			  } 
			  
		}
			
		{	
		$entryid = $_POST['entryid_pis'];
		$issuedqty = $_POST['issuedqty'];
		$prodname_pis = $_POST['prodname_pis'];
		  for ($i = 0; $i < count($entryid); $i++) 
		  {
		  if(!mysqli_query($con,"UPDATE sordervar SET  `pissued` =  `pissued`-'$issuedqty[$i]' where entryid = $entryid[$i] "))
			{
			  die('Error: ' . mysqli_error($con));
			}
		  
		  }
		}

}
			
  
echo "Record Deleted Susscessfully!!!";

}
mysqli_close($con);
?>
