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



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

	$sql="UPDATE `spacklistreg` SET  `ptotal` = '$_POST[tpcost]', `vat` = '$_POST[vat]', `pnamount` = '$_POST[grtotal]' WHERE `packingid` = $_POST[packingid]";
		
		if (mysqli_query($con,$sql)){
		$sql="INSERT INTO `spacklistregdet` ( `entryid`, `packingid`, `srno`, `prodcode`, `cat`, `subcode`, `colorcode`, `shortdesc`, `sizeset`, `set`, `qty`, `fresh`, `reprocess`, `demage`, `sordmrp`,`sorddisc`, `pcost`, `pamt`) VALUES ('', '$_POST[packingid]', '$_POST[srno]', '$_POST[prodcode]', '$_POST[cat]', '$_POST[subcode]', '$_POST[colorcode]', '$_POST[shortdesc]', '$_POST[sizeset]', '$_POST[set]', '$_POST[qty]', '$_POST[fresh]', '$_POST[reprocess]', '$_POST[demage]' , '$_POST[sordmrp]', '$_POST[sorddisc]', '$_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]!='')
				{ 
				
					$rmSql = "INSERT INTO spacklistvar (`entryid`, `job_id`,`srno`, `prodname`, `size`, `qnty`, `fresh`, `reprocess`, `demage`) VALUES (NULL, '$_POST[packingid]','$_POST[srno]','$_POST[prodcode]', '$size[$i]', '$qnty[$i]', '$fresh[$i]', '$reprocess[$i]', '$demage[$i]')";
					echo $rmSql;
					if(!mysqli_query($con,$rmSql))
					{
					die('Error: ' . mysqli_error($con));
					}
					
				}
			  }
			   
			}//else
		 
		  
		 
				//if(mysqli_query($con,"UPDATE `prodreg` SET  `stock` = stock - $_POST[qty] where code='$_POST[code]' and cat ='PRODUCT'"))
			
	{
		$issuedqty = $_POST['issuedqty'];
		$orderidissued = $_POST['orderidissued'];
		for ($i = 0; $i < count($_POST['orderidissued']); $i++) 
		{
			//$outputsql.="UPDATE sorderregdet SET  `issued` = '$issuedqty[$i]' where orderid = '$orderidissued[$i]'";
		if(!mysqli_query($con,"UPDATE sorderregdet SET  `salesissued` = `salesissued`+'$issuedqty[$i]' where orderid = '$orderidissued[$i]' "))
		{
		die('Error: ' . mysqli_error($con));
		}
		}
		
		echo "record added succesfully!!";
	}
		}

}


else if($_GET[mode]=='update')
{
	
		$sql="UPDATE `spacklistreg` SET  `ptotal` = '$_POST[tpcost]', `vat` = '$_POST[vat]', `pnamount` = '$_POST[grtotal]' WHERE `packingid` = $_POST[packingid]";
		
		  if (mysqli_query($con,$sql)){
		  $sql="UPDATE `spacklistregdet` SET   `packingid` = '$_POST[packingid]', `srno` = '$_POST[srno]', `prodcode` = '$_POST[prodcode]', `cat` = '$_POST[cat]', `subcode` = '$_POST[subcode]', `colorcode` ='$_POST[colorcode]' , `shortdesc` = '$_POST[shortdesc]', `sizeset` = '$_POST[sizeset]', `set` = '$_POST[set]', `qty` = '$_POST[qty]', `fresh` = '$_POST[fresh]', `reprocess` = '$_POST[reprocess]', `demage` ='$_POST[demage]' , `sordmrp` = '$_POST[sordmrp]' , `sorddisc` = '$_POST[sorddisc]' , `pcost` = '$_POST[rate]', `pamt` = '$_POST[amount]' WHERE entryid= '$_POST[entryid]'";
		  
		  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'];
			$entryid = $_POST[entryid_pv];
			
			for ($i = 0; $i < count($_POST['size_pv']); $i++) 
			  {
				if($size[$i]!='')
				{
					$rmSql = "UPDATE spacklistvar SET   `prodname` = '$_POST[prodcode]', `size` = '$size[$i]', `qnty` = '$qnty[$i]', `fresh` = '$fresh[$i]', `reprocess` = '$reprocess[$i]', `demage` = '$demage[$i]' where `entryid` = '$entryid[$i]' and `job_id` = '$_POST[packingid]' ";
					//echo $rmSql ;
					if(!mysqli_query($con,$rmSql))
					{
					die('Error: ' . mysqli_error($con));
					}
					
				}
			  
			 }
			 //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++) 
				{
				if($qnty_pv[$i]<=0){	
				$qnty_pvsql ="UPDATE spacklistvar SET  `qnty` = '$issuedqty[$i]' where entryid = '$entryid_pv[$i]'  ";
				  if(!mysqli_query($con,$qnty_pvsql))
				  {
				  die('Error: ' . mysqli_error($con));
				  }
				}
				 echo $qnty_pvsql.'<br/>';
				}
			
		 }
			 
			 
			{
			  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  `issued` =  '$issuedqty[$i]' where entryid = $entryid[$i] "))
				{
				die('Error: ' . mysqli_error($con));
				}
			  
			  }
			}
			  
			}
		  echo "Record Updated Susscessfully!!!";

		}

}


else if($_GET[mode]=='updatesize')
{
	
		
			$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['size_pv']); $i++) 
			  {
				
					
					$sizsetSQL = "UPDATE spacklistvar SET   `prodname` = '$_POST[prodcode]', `size` = '$size[$i]', `qnty` = '$qnty[$i]', `fresh` = '$fresh[$i]', `reprocess` = '$reprocess[$i]', `demage` = '$demage[$i]' where `entryid` = '$entryid[$i]' and `job_id` = '$_POST[orderid]'  ";
					if(!mysqli_query($con,$sizsetSQL))
					{
					die('Error: ' . mysqli_error($con));
					}
					
					$qtyval+=$qnty[$i];
				
			  } 
			
			$sql="UPDATE `spacklistregdet` SET  `qty` = '$qtyval' WHERE entryid= '$_POST[entryid]'";
		
		  if (mysqli_query($con,$sql)){
			  
			  }
			
			
		  echo "Record Updated Successfully!!!";

	

}


else if($_GET[mode]=='delete')
{
$sql="DELETE from spacklistregdet where entryid='$_POST[entryid]'";



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'];
			$entryid = $_POST['entryid_pv'];
			
			for ($i = 0; $i < count($_POST['size_pv']); $i++) 
			  {
				if($size[$i]!='')
				{
					if(!mysqli_query($con,"DELETE from spacklistvar where `entryid` = $entryid[$i] and `job_id` = '$_POST[packingid]' and `srno` = '$_POST[srno]'"))
					{
					die('Error: ' . mysqli_error($con));
					}
					
				}
			  } 
			}
  
echo "Record Deleted Susscessfully!!!";

}
mysqli_close($con);
?>
