<?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 `sorderreg` SET  `ptotal` = '$_POST[tpcost]', `vat` = '$_POST[vat]', `pnamount` = '$_POST[grtotal]' WHERE `orderid` = $_POST[orderid]";
		
		if (mysqli_query($con,$sql)){
		$sql="INSERT INTO `sorderregdet` ( `entryid`, `orderid`, `srno`, `prodcode`, `cat`, `subcode`, `colorcode`, `shortdesc`, `hsncode`, `set`, `qty`, `sordmrp`,`pcost`, `pamt`, `discamt`,`pergst`, `cgst`, `sgst`, `igst`, `gtotal`) VALUES ('', '$_POST[orderid]', '$_POST[srno]', '$_POST[prodcode]', '$_POST[cat]', '$_POST[subcode]', '$_POST[colorcode]', '$_POST[shortdesc]', '$_POST[hsncode]', '$_POST[set]', '$_POST[qty]', '$_POST[sordmrp]','$_POST[rate]', '$_POST[amount]', '$_POST[discount]','$_POST[percgst]', '$_POST[cgst]', '$_POST[sgst]',  '$_POST[igst]', '$_POST[gtotal]')";
		
		//echo $sql;exit;
		
		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 sordervar (`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[prodcode]' and cat ='$_POST[cat]'"))		
	{
		echo "Record Added Successfully!!";
	}
		}

}


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 `sorderreg` SET  `ptotal` = '$_POST[tpcost]', `vat` = '$_POST[vat]', `pnamount` = '$_POST[grtotal]' WHERE `orderid` = $_POST[orderid]";
		
		  if (mysqli_query($con,$sql)){
		  $sql="UPDATE `sorderregdet` SET   `orderid` = '$_POST[orderid]', `srno` = '$_POST[srno]', `prodcode` = '$_POST[prodcode]', `cat` = '$_POST[cat]', `subcode` = '$_POST[subcode]', `colorcode` ='$_POST[colorcode]' , `shortdesc` = '$_POST[shortdesc]', `hsncode` = '$_POST[hsncode]', `set` = '$_POST[set]', `sordmrp` = '$_POST[sordmrp]' , `qty` = '$_POST[qty]',`pcost` = '$_POST[rate]', `pamt` = '$_POST[amount]',`discamt` = '$_POST[discount]', `pergst` = '$_POST[percgst]', `cgst` = '$_POST[cgst]',  `sgst` = '$_POST[sgst]', `igst` = '$_POST[igst]', `gtotal` = '$_POST[gtotal]' 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]!='')
				{
					
					$sizsetSQL = "UPDATE sordervar 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));
					}
					//echo $sizsetSQL;
				}
			  } 
			}
		  echo "Record Updated Successfully!!!";

		}

}


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++) 
			  {
				//if($size[$i]!='')
				//{
					
					$sizsetSQL = "UPDATE sordervar 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));
					}
					//echo $sizsetSQL;
					$qtyval+=$qnty[$i];
				//}
			  } 
			
			$sql="UPDATE `sorderregdet` SET  `qty` = '$qtyval' WHERE entryid= '$_POST[entryid]'";
		
		  if (mysqli_query($con,$sql)){
			  
			  }
			
			
		  echo "Record Updated Successfully!!!";

	

}


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



if(!mysqli_query($con,$sql))
 {
  die('Error: ' . mysqli_error($con));
  }
  else
		  {
			
			
				
					if(!mysqli_query($con,"delete from sordervar where    srno = '$_POST[srno]' and `prodname` = '$_POST[prodcode]' and job_id = '$_POST[orderid]' "))
					{
					die('Error: ' . mysqli_error($con));
					}
					
				
			  
			}
		  
echo "Record Deleted Successfully!!!";

}
 
mysqli_close($con);
?>
