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



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

	$sql="UPDATE `drcrnotereg` SET  `ptotal` = '$_POST[tpcost]', `vat` = '$_POST[vat]', `pnamount` = '$_POST[grtotal]' WHERE `orderid` = $_POST[orderid]";
		
		if (mysqli_query($con,$sql)){
		$sql="INSERT INTO `drcrnoteregdet` ( `entryid`, `orderid`,`frmdetail`, `srno`, `prodcode`, `cat`, `colorcode`, `shortdesc`, `sizeset`, `set`, `qty`,`mrp`,`pcost`, `pamt`) VALUES ('', '$_POST[orderid]','$_POST[frmdetail]', '$_POST[srno]', '$_POST[prodcode]', '$_POST[cat]', '$_POST[colorcode]', '$_POST[shortdesc]', '$_POST[sizeset]', '$_POST[set]', '$_POST[qty]', '$_POST[mrp]','$_POST[rate]', '$_POST[amount]')";
		
		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'];
			$price=$_POST['price_pv'];
			$mrp=$_POST['mrp_pv'];
			echo "test".count($size);
			
			for ($i = 0; $i < count($size); $i++) 
			  {
				//if($qnty[$i]!=0)
				//{
					$sql = "INSERT INTO drcrnotevar (`entryid`, `job_id`,`srno`, `prodname`, `size`, `qnty`, `fresh`, `reprocess`, `demage`, `price`, `mrp`) VALUES (NULL, '$_POST[orderid]','$_POST[srno]','$_POST[prodcode]', '$size[$i]', '$qnty[$i]', '$fresh[$i]', '$reprocess[$i]', '$demage[$i]', '$price[$i]', '$mrp[$i]')";
					
					if(!mysqli_query($con,$sql))
					{
					die('Error: ' . mysqli_error($con));
					}
					echo $sql; 
				//}
			  } 
		}
		 
		 {
			 
			$size=$_POST['size_pv'];
			$qnty=$_POST['qnty_pv'];
			$fresh=$_POST['fresh_pv'];
			$prodname=$_POST['prodname_pv'];	
						
			for ($i = 0, $ser=0; $i < count($_POST['size_pv']); $i++,$ser++) 
			  {
				if($qnty[$i]!='')
				{
					$sizeSql = "UPDATE prodvar SET    `qnty` = `qnty`-$qnty[$i] where  `size` = '$size[$i]' and prodname = '$prodname[$i]' ";
					if(!mysqli_query($con,$sizeSql))
					{
					die('Error: ' . mysqli_error($con));
					}
					
				}
				
			  } 
			 
		}
		  
		 
				//if(mysqli_query($con,"UPDATE `prodreg` SET  `stock` = stock - $_POST[qty] where code='$_POST[code]' and cat ='PRODUCT'"))
			
	
		
		 $issuedqty = $_POST['issuedqty'];
		$orderidissued = $_POST['orderidissued'];
		if($_POST['frm']=='PKL'){
			
		for ($i = 0; $i < count($_POST['orderidissued']); $i++) 
		{
			
		  if(!mysqli_query($con,"UPDATE spacklistregdet SET  `salesissued` = `salesissued`+'$_POST[issuedqty]' where packingid = '$orderidissued[$i]'"))
		  {
		  die('Error: ' . mysqli_error($con));
		  }
		}
		}
		else{
			 
	    for ($i = 0; $i < count($_POST['orderidissued']); $i++) 
		 {
			
		  if(!mysqli_query($con,"UPDATE sorderregdet SET  `salesissued` = `salesissued`+'$_POST[issuedqty]' where orderid = '$orderidissued[$i]' "))
		   {
		  die('Error: ' . mysqli_error($con));
		   }
		 }
			
	}
		
		echo "Record Added Successfully!!";
	
		}

}


else if($_GET[mode]=='update')
{
	
		$sql="UPDATE `drcrnotereg` SET  `ptotal` = '$_POST[tpcost]', `vat` = '$_POST[vat]', `pnamount` = '$_POST[grtotal]' WHERE `orderid` = $_POST[orderid]";
		
		  if (mysqli_query($con,$sql)){
		  $sql="UPDATE `drcrnoteregdet` SET   `orderid` = '$_POST[orderid]', `frmdetail` = '$_POST[frmdetail]',`srno` = '$_POST[srno]', `prodcode` = '$_POST[prodcode]', `cat` = '$_POST[cat]', `colorcode` ='$_POST[colorcode]' , `shortdesc` = '$_POST[shortdesc]', `sizeset` = '$_POST[sizeset]', `set` = '$_POST[set]', `qty` = '$_POST[qty]', `mrp` = '$_POST[mrp]' , `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];
			$price=$_POST['price_pv'];
			$mrp=$_POST['mrp_pv'];
			
			for ($i = 0; $i < count($_POST['size_pv']); $i++) 
			  {
				if($qnty[$i]!='')
				{
					if(!mysqli_query($con,"UPDATE drcrnotevar SET   `prodname` = '$_POST[prodcode]', `size` = '$size[$i]', `qnty` = '$qnty[$i]', `fresh` = '$fresh[$i]', `reprocess` = '$reprocess[$i]', `demage` = '$demage[$i]',`price` = '$price[$i]', `mrp` = '$mrp[$i]' where `entryid` = $entryid[$i] and `job_id` = '$_POST[orderid]' "))
					{
					die('Error: ' . mysqli_error($con));
					}
					
				}
			  } 
			  
			  
			  /*{
			 
			$size=$_POST['size_pv'];
			$qnty=$_POST['qnty_pv'];
			$fresh=$_POST['fresh_pv'];
			$prodname=$_POST['prodname_pv'];	
						
			for ($i = 0, $ser=0; $i < count($_POST['size_pv']); $i++,$ser++) 
			  {
				if($qnty[$i]!='')
				{
					$sizeSql = "UPDATE prodvar SET    `qnty` = `qnty`-$qnty[$i] where  `size` = '$size[$i]' and prodname = '$prodname[$i]' ";
					if(!mysqli_query($con,$sizeSql))
					{
					die('Error: ' . mysqli_error($con));
					}
					echo $sizeSql;
				}
				
			  } 
			 
		}*/
			  
			}
		  echo "Record Updated Successfully!!!";

		}

}


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



if(!mysqli_query($con,$sql))
 {
  die('Error: ' . mysqli_error($con));
  }
  else{
			
			$entryid =$_POST['entryid_pv'];
			
			for ($i = 0; $i < count($_POST['entryid_pv']); $i++) 
			  {
				
					$deletjob = "DELETE from drcrnotevar where `entryid` = '$entryid[$i]' ";
					if(!mysqli_query($con,$deletjob))
					{
					die('Error: ' . mysqli_error($con));
					}
					echo $deletjob ;
				
			  } 
			  
		}
echo "Record Deleted Successfully!!!";

}
mysqli_close($con);
?>
