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



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

	$sql="UPDATE `sinvoicereg` SET  `ptotal` = '$_POST[tpcost]', `vat` = '$_POST[vat]', `pnamount` = '$_POST[grtotal]' WHERE `orderid` = $_POST[orderid]";
		
		if (mysqli_query($con,$sql)){
		$sql="INSERT INTO `sinvoiceregdet` ( `entryid`, `orderid`, `srno`, `prodcode`, `cat`, `subcode`, `colorcode`, `shortdesc`, `hsncode`, `set`, `qty`,`frmdetail`, `fresh`, `reprocess`, `demage`, `mrp`,`pcost`, `pamt`, `discamt`, `percgst`, `cgst`, `persgst`, `sgst`, `perigst`, `igst`, `gtotal`) VALUES ('', '$_POST[orderid]', '$_POST[srno]', '$_POST[prodcode]', '$_POST[cat]', '$_POST[subcode]', '$_POST[colorcode]', '$_POST[shortdesc]', '$_POST[hsncode]', '$_POST[set]', '$_POST[qty]', '$_POST[frm]','$_POST[fresh]', '$_POST[reprocess]', '$_POST[demage]' ,'$_POST[mrp]','$_POST[rate]', '$_POST[amount]', '$_POST[discount]','$_POST[percgst]', '$_POST[cgst]', '$_POST[persgst]', '$_POST[sgst]', '$_POST[perigst]', '$_POST[igst]', '$_POST[gtotal]')";
		
		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'];
			$price=$_POST['price_pv'];
			$mrp=$_POST['mrp_pv'];
			//echo "test".count($_POST['size_pv']);
			
			for ($i = 0; $i < count($_POST['size_pv']); $i++) 
			{
				if($qnty[$i]!=0)
				{
					$sql = "INSERT INTO sinvoicevar (`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]')";
					echo $sql;
					if(!mysqli_query($con,$sql))
					{
					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));
					}
						
				}
			}  
		}
		  
		 
		if(mysqli_query($con,"UPDATE `prodreg` SET  `stock` = stock - $_POST[qty] WHERE `code` ='$_POST[prodcode]' AND `cat` ='$_POST[cat]'"))
			
	
		
		 $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 `sinvoicereg` SET  `ptotal` = '$_POST[tpcost]', `vat` = '$_POST[vat]', `pnamount` = '$_POST[grtotal]' WHERE `orderid` = $_POST[orderid]";
		
		  if (mysqli_query($con,$sql)){
		  $sql="UPDATE `sinvoiceregdet` 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]', `qty` = '$_POST[qty]', `frmdetail` ='salesinv',`fresh` = '$_POST[fresh]', `reprocess` = '$_POST[reprocess]', `demage` ='$_POST[demage]', `mrp` = '$_POST[mrp]' , `pcost` = '$_POST[rate]', `pamt` = '$_POST[amount]', `discamt` = '$_POST[discount]', `percgst` = '$_POST[percgst]', `cgst` = '$_POST[cgst]', `persgst` = '$_POST[persgst]', `sgst` = '$_POST[sgst]', `perigst` = '$_POST[perigst]', `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];
			$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 sinvoicevar 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 sinvoiceregdet 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 sinvoicevar WHERE `entryid` = '$entryid[$i]' ";
					if(!mysqli_query($con,$deletjob))
					{
					die('Error: ' . mysqli_error($con));
					}
					echo $deletjob ;
				
			  } 
			  
		}
echo "Record Deleted Successfully!!!";

}
mysqli_close($con);
?>
