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



if($_GET[mode]=='insert')
{
	if ($_POST[ordertype] == "readaystock"){
		$sql1="UPDATE `prodreg` SET  `stock` = `stock`+'$_POST[qty]' WHERE `code` = '$_POST[prodcode]';";
		}
	else{
		$sql1="UPDATE `rawmatreg` SET  `qty` = `qty`+'$_POST[qty]' WHERE `code` = '$_POST[prodcode]';";
		}
		mysqli_query($con,$sql1);

	$sql="UPDATE `porderreg` SET  `ptotal` = '$_POST[tpcost]', `vat` = '$_POST[vat]', `pnamount` = '$_POST[grtotal]' WHERE `orderid` = $_POST[orderid]";
		
		if (mysqli_query($con,$sql)){
		$sql="INSERT INTO `porderregdet` ( `entryid`, `orderid`, `srno`, `prodcode`, `cat`, `subcode`, `colorcode`, `shortdesc`, `sizeset`, `set`, `qty`, `pordmrp`,`porddisc`, `fresh`, `reprocess`, `demage`, `pcost`, `pamt`) VALUES ('', '$_POST[orderid]', '$_POST[srno]', '$_POST[prodcode]', '$_POST[cat]', '$_POST[subcode]', '$_POST[colorcode]', '$_POST[shortdesc]', '$_POST[sizeset]', '$_POST[set]', '$_POST[sordmrp]', '$_POST[sorddisc]', '$_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'];
			//echo "test".count($_POST['size_pv']);
			
			for ($i = 0; $i < count($_POST['size_pv']); $i++) 
			  {
				if($size[$i]!='')
				{
					$sql = "INSERT INTO pordervar (`entryid`, `job_id`,`srno`, `prodname`, `size`, `qnty`, `fresh`, `reprocess`, `demage`) VALUES (NULL, '$_POST[orderid]','$_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));
					}
					
				}
			  } 
			}
		 
		  
		 
				//if(mysqli_query($con,"UPDATE `prodreg` SET  `stock` = stock - $_POST[qty] where code='$_POST[code]' and cat ='PRODUCT'"))
			
	{
		echo "Record Added Succesfully!!";
	}
		}

}


else if($_GET[mode]=='update')
{
	
	if ($_POST[ordertype] == "readaystock"){
		$sql1="UPDATE `prodreg` SET  `stock` = `stock`+'$_POST[qty]' WHERE `code` = '$_POST[prodcode]';";
		}
	else{
		$sql1="UPDATE `rawmatreg` SET  `qty` = `qty`+'$_POST[qty]' WHERE `code` = '$_POST[prodcode]';";
		}
		mysqli_query($con,$sql1);
	
		$sql="UPDATE `porderreg` SET  `ptotal` = '$_POST[tpcost]', `vat` = '$_POST[vat]', `pnamount` = '$_POST[grtotal]' WHERE `orderid` = $_POST[orderid];";
		 
			   if (mysqli_query($con,$sql)){
			  
		  $sql="UPDATE `porderregdet` SET   `orderid` = '$_POST[orderid]', `srno` = '$_POST[srno]', `prodcode` = '$_POST[prodcode]', `cat` = '$_POST[cat]', `subcode` = '$_POST[subcode]', `colorcode` ='$_POST[colorcode]' , `shortdesc` = '$_POST[shortdesc]', `sizeset` = '$_POST[sizeset]', `set` = '$_POST[set]', `pordmrp` = '$_POST[sordmrp]' , `porddisc` = '$_POST[sorddisc]' , `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
		  {
			$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,"UPDATE pordervar 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]' "))
					{
					die('Error: ' . mysqli_error($con));
					}
					
				}
			  } 
			}
		  echo "Record Updated Susscessfully!!!".$sql1;

		}

}


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



if(!mysqli_query($con,$sql))
 {
  die('Error: ' . mysqli_error($con));
  }
  
  
  
echo "Record Deleted Susscessfully!!!";

}
mysqli_close($con);
?>
