<?php
session_start();
include '../db_connect.php';

$reload=0;
 foreach($_POST as $key => $value){
	 if(!is_array($_POST[$key]))
$_POST[$key] = mysqli_real_escape_string($con,trim($value));
}
if($_GET['mode']=='insert')
{
	$sql="INSERT INTO  `prodreg` SET id = '', `code` = '$_POST[prodcode]', `gencode` = '$_POST[subprdcd]', `color` = '$_POST[color]', `stock` = '$_POST[stock]',  `phystock` = '$_POST[phystock]', `reorder` = '$_POST[reorder]', `cat` = '$_POST[cat]', `subcat` = '$_POST[subcat]', `ratio` = '$_POST[ratio]',`ratiotot` = '$_POST[ratiotot]',`sizesetcode` = '$_POST[sizesetcode]', `desc` = '$_POST[note]', `hsncode` = '$_POST[hsncode]', `unit` = ' ', `ptotal` = '$_POST[ptotal]', `pdiscount` = '$_POST[pdiscount]', `pnamount` = '$_POST[pnamount]', `vat` = '$_POST[vat]', `gstper` = '$_POST[gstper]', `gstamt` = '$_POST[gstamt]', `grtotal` = '$_POST[grtotal]', `diffmrp` = '$_POST[diffmrp]', `diffprice` = '$_POST[diffprice]', `status` = '$_POST[status]', `taxtype` = '$_POST[taxtype]'";


if(!mysqli_query($con,$sql)){
  echo 'Error: ' . mysqli_error($con);
  }
  else{
	  $prodid = mysqli_insert_id($con);
	    if(mysqli_query($con,"UPDATE `prodreg` SET   `subcat` = '$_POST[subcat]', `ratio` = '$_POST[ratio]',`ratiotot` = '$_POST[ratiotot]', `sizesetcode` = '$_POST[sizesetcode]', `desc` = '$_POST[note]', `ptotal` = '$_POST[ptotal]', `pdiscount` = '$_POST[pdiscount]', `pnamount` = '$_POST[pnamount]', `vat` = '$_POST[vat]', `gstper` = '$_POST[gstper]', `gstamt` = '$_POST[gstamt]', `grtotal` = '$_POST[grtotal]', `diffmrp` = '$_POST[diffmrp]', `diffprice` = '$_POST[diffprice]', `taxtype` = '$_POST[taxtype]' WHERE `gencode` = '$_POST[subprdcd]' ;"));
		
		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'];
			
			
			for ($i = 0, $ser=0; $i < count($_POST['size_pv']); $i++,$ser++) 
			  {
				if($size[$i]!='')
				{
					$sizeSql = "INSERT INTO prodvar (`entryid`, `job_id`,`srno`, `prodname`, `size`, `qnty`, `fresh`, `reprocess`, `demage`, `price`, `mrp`) VALUES (NULL, '$_POST[jobid]','$ser','$_POST[prodcode]', '$size[$i]', '$qnty[$i]', '$fresh[$i]', '$reprocess[$i]', '$demage[$i]', '$price[$i]', '$mrp[$i]')";
					if(!mysqli_query($con,$sizeSql))
					{
					die('Error: ' . mysqli_error($con));
					}
					
				}
				
			  } 
		
    echo "Record  Insert!! ###".$prodid;
  }
  
  

}

else if($_GET['mode']=='update'){
	$jobid=$_POST[jobid];
	if(isset($jobid)&&!empty($jobid)){
	$sql="UPDATE `prodreg` SET  `code` = '$_POST[prodcode]', `gencode` = '$_POST[subprdcd]', `color` = '$_POST[color]', `stock` = '$_POST[stock]',  `phystock` = '$_POST[phystock]', `reorder` = '$_POST[reorder]', `cat` = '$_POST[cat]', `subcat` = '$_POST[subcat]', `ratio` = '$_POST[ratio]',`ratiotot` = '$_POST[ratiotot]', `sizesetcode` = '$_POST[sizesetcode]', `desc` = '$_POST[note]', `hsncode` = '$_POST[hsncode]', `unit` = ' ', `ptotal` = '$_POST[ptotal]', `pdiscount` = '$_POST[pdiscount]', `pnamount` = '$_POST[pnamount]', `vat` = '$_POST[vat]', `gstper` = '$_POST[gstper]', `gstamt` = '$_POST[gstamt]', `grtotal` = '$_POST[grtotal]', `diffmrp` = '$_POST[diffmrp]', `diffprice` = '$_POST[diffprice]', `status` = '$_POST[status]', `taxtype` = '$_POST[taxtype]' WHERE `id` = $jobid";
	
	//echo $sql;
if(!mysqli_query($con,$sql)){
  echo 'Error: ' . mysqli_error($con);
  }
  else{
	  
	  $provarSql = "delete from prodvar where `prodname` = '$_POST[prodcode]' ";
	  
	   if(mysqli_query($con,$provarSql))
	   
			{
					
					$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'];
			
			
			
			for ($i = 0, $ser=0; $i < count($_POST['size_pv']); $i++,$ser++) 
			  {
				if($mrp[$i]!='')
				{
					$sizeSql = "INSERT INTO prodvar (`entryid`, `job_id`,`srno`, `prodname`, `size`, `qnty`, `fresh`, `reprocess`, `demage`, `price`, `mrp`) VALUES (NULL, '$_POST[jobid]','$ser','$_POST[prodcode]', '$size[$i]', '$qnty[$i]', '$fresh[$i]', '$reprocess[$i]', '$demage[$i]', '$price[$i]', '$mrp[$i]')";
					if(!mysqli_query($con,$sizeSql))
					{
					die('Error: ' . mysqli_error($con));
					}
					//echo $sizeSql;
				}
				
			  } 
			}
	  
	  
	  echo "Record  Updated!! ###";
  }
  
	}//if jobid set
	}
	
else if($_GET['mode']=='doc_up')
{
$ftp_server = "geeteedecors.com";
$ftp_user_name = "ftpuploads@geeteedecors.com";
$ftp_user_pass = "Info@7401$19";
$destination_file = "/";
$source_file = $_POST['file']['tmp_name'];



// set up basic connection
$conn_id = ftp_connect($ftp_server);
ftp_pasv($conn_id, true); 

// login with username and password
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass); 

// check connection
if ((!$conn_id) || (!$login_result)) { 
    $output['error'] = "FTP connection has failed!";
   	$output['error'] .= "Attempted to connect to $ftp_server for user $ftp_user_name"; 
    exit; 
} else {
    $output['success'] = "Connected to $ftp_server, for user $ftp_user_name";
}

	
	
$output_dir = "ftpdata/";
if(isset($_FILES["myfile"]))
{
	$ret = array();

	$error =$_FILES["myfile"]["error"];
	//You need to handle  both cases
	//If Any browser does not support serializing of multiple files using FormData() 
	if(!is_array($_FILES["myfile"]["name"])) //single file
	{
 	 	$fileName = $_FILES["myfile"]["name"];
		
		$fileLink = $destination_file.$fileName;
		
 		//move_uploaded_file($_FILES["myfile"]["tmp_name"],$output_dir.$fileName);
		$source_file  = $_FILES["myfile"]["tmp_name"];
		$upload = ftp_put($conn_id, $fileLink, $source_file, FTP_BINARY); 
		//$insertSQL = "update  fileupload set`uplink`= CONCAT(uplink,'$fileName,') where id in ($_POST[docid])";
			
		//mysql_query($insertSQL);
    	//$ret[]= $fileName;
	}
	else  //Multiple files, file[]
	{
	  $fileCount = count($_FILES["myfile"]["name"]);
	  for($i=0; $i < $fileCount; $i++)
	  {
	  	$fileName = $_FILES["myfile"]["name"][$i];
		//move_uploaded_file($_FILES["myfile"]["tmp_name"][$i],$output_dir.$fileName);
		//$insertSQL = "update  fileupload set`uplink`= CONCAT(uplink,'$fileName,')";
			
		//mysql_query($insertSQL);
	  	//$ret[]= $fileName;
		$source_file  = $_FILES["myfile"]["tmp_name"][$i];
		$fileLink .= $destination_file.'/'.$fileName.',';
		$upload = ftp_put($conn_id, $fileLink, $source_file, FTP_BINARY); 
		
		$output['error'] .=  "$upload aaa";
	  }
	
	}
	
	// check upload status
if (!$upload) { 
	$output['error'] .=  "FTP upload has failed!";
} 
else {
	$EntryID = clean($con,$_POST[EntryID]);
	$imgtype = clean($con,$_POST[typeval]);
	if($imgtype=="photoone"){
	$sqlQry = "update  prodreg set `img1`= CONCAT('$fileName') where id = '$EntryID' ";
	}
	else if($imgtype=="phototwo"){
	$sqlQry = "update  prodreg set `img2`= CONCAT('$fileName') where id = '$EntryID' ";
	}
	else if($imgtype=="photothree"){
	$sqlQry = "update  prodreg set `img3`= CONCAT('$fileName') where id = '$EntryID' ";
	}
	else if($imgtype=="photofour"){
	$sqlQry = "update  prodreg set `img4`= CONCAT('$fileName') where id = '$EntryID' ";
	}
	mysqli_query($con,$sqlQry);
	$output['success'] = "Uploaded $fileName to $ftp_server as $destination_file";
}

	
    echo json_encode($output);
	
	// close the FTP stream 
ftp_close($conn_id);
 }

}

?>