<?php 
    require_once('connection/db.php');
	require_once('convert_marathi.php');
	######################################### Fetch Selected Record In Form Field And Perform Action #########################
	session_start();
	$username = $_SESSION['User_Name']; 
	$flag=$_REQUEST['flag'];


if($flag=='')
{
	$id=$_GET['num'];
	//echo "id is".$id;
	$target_path = "";
	################################ file upload#######################################
	if (!empty($_FILES["uploadedimage"]["name"])) 
	{
		$file_name=$_FILES["uploadedimage"]["name"];
		//echo  $file_name."<br>";
		$temp_name=$_FILES["uploadedimage"]["tmp_name"];
		$imgtype=$_FILES["uploadedimage"]["type"];
		$ext= GetImageExtension($imgtype);
		$imagename=$file_name;
		//$target_path = "../uploads/".$imagename;
		 
		 
		$temp = explode(".", $_FILES["uploadedimage"]["name"]);
		$newfilename =$recdt.round(microtime(true)) . '.' . end($temp);
		$target_path = "uploads/".$newfilename;
		
		if($target_path!='')
		{
			if(!move_uploaded_file($temp_name, $target_path))
			{
				//	echo "<script type='text/javascript'>alert('Unable to move file!')</script>";
				//	echo '<script language="JavaScript"> window.location.href ="desk_action_perfom_form.php?no='.$id.'" </script>';
					break;
			}
			else
			{
				move_uploaded_file($temp_name, $target_path);
			}
		}
	}
	else
	{
		$target_path = "uploads/file_nt_uploaded.jpg";
	}
	################################ end of file upload #######################################

	$target_path = "../".$target_path;

	$ltrtype=$_POST['ltrtype'];
	$actiontaken=$_POST['actiontake'];
	$actiontaken1=$_POST['actiontake1'];
	$dptinwardno=$_POST['dptinwardno'];
	//echo $dptinwardno."hiii222";
	convert_marathi();
	if($actiontaken1=='other')
	{
		$qrym=mysql_query("UPDATE incoming_letters_transaction SET desk_doument='$target_path',first_flag='N' WHERE ltr_inword_no='$dptinwardno'");
     
		$qry=("UPDATE desk_transaction SET action_taken='$actiontaken',scan_doc='$target_path' WHERE desk_ltr_id='$id'");
	}
	else
	{
		$qrym=mysql_query("UPDATE incoming_letters_transaction SET desk_doument='$target_path' ,first_flag='N' WHERE ltr_inword_no='$dptinwardno'");
		$qry=("UPDATE desk_transaction SET action_taken='$actiontaken1',scan_doc='$target_path' WHERE desk_ltr_id='$id'");
	}
	/************************************************
	action performed if update button is selected
	*************************************************/
    if (isset($_POST['update']))
	{
		if($ltrtype=='ब')
		{
			//$futureDate=date('Y-m-d', strtotime('+30 year'));
			$now23 =date('Y')+30;
			$futureDate =$now23.'-'.date('m-d');
		}
		else if($ltrtype=='क')
		{
			$now23 =date('Y')+10;
			$futureDate =$now23.'-'.date('m-d');
		}
		else if($ltrtype=='क१')
		{
			$now23 =date('Y')+5;
			$futureDate =$now23.'-'.date('m-d');
		}
		else if($ltrtype=='ड')
		{
			$now23 =date('Y')+1;
			$futureDate =$now23.'-'.date('m-d');
		}
		
		$qry2=("UPDATE desk_transaction dt, tapal_to_dpt_transaction t 
		SET dt.update_date=curdate(), 
		t.desk_approval='updated', dt.ltr_status='updated' , dt.vargawari='$ltrtype', dt.nasti_nashta_dt='$futureDate'
		WHERE desk_ltr_id='$id' AND t.dpt_ltr_id=dt.dpt_ltr_id");

		if(mysql_query($qry2)) 
		{ 
			mysql_query($qry);
	    	echo "<script type='text/javascript'>alert('Action Performed Successfully!')</script>";
			echo '<script language="JavaScript"> window.location.href ="desk_action_perform.php" </script>';
		}
		else
		{   
			echo "<script type='text/javascript'>alert('Problem to perform action!111111111111111dfsdf')</script>";
        echo '<script language="JavaScript"> window.location.href ="desk_action_perform.php" </script>';
		}
	}
	/************************************************
		action performed if dispatch button is selected
	*************************************************/
	if (isset($_POST['dispatch']))
	{		
		require_once('dispatch.php');// Calling dispatch.php File
		
		mysql_query($qry);
	}
	/************************************************
		Rejected letter's dispatch button is selected
		
	*************************************************/
	if (isset($_POST['rejdispatch']))
	{ 
		$recdt = date('Y-m-d', strtotime($redt));
		$rejid=$_POST['rejid'];
		require_once('dispatch.php');// Calling dispatch.php File
		if(mysql_query($qry))
		{
			$qry10=mysql_query("UPDATE desk_transaction,ltr_rejection SET desk_transaction.action_taken='$actiontaken', desk_transaction.dispatched_dt=curdate(),desk_transaction.flag='',desk_transaction.vargawari='$ltrtype' WHERE desk_transaction.desk_ltr_id='$id' AND ltr_rejection.ltr_rej_id='$rejid' AND desk_transaction.desk_ltr_id=ltr_rejection.desk_ltr_id AND desk_transaction.ltr_rej_id=ltr_rejection.ltr_rej_id") or ("qqqqqqq".mysql_error());
			
		}
		else
		{
			echo "<script type='text/javascript'>alert('Problem to perform action!')</script>";
			echo '<script language="JavaScript"> window.location.href ="desk_action_perform.php" </script>';
		}
	}
} //if($flag=='')

else if($flag=='n')
{
	$id=$_GET['num'];
	//echo "id is".$id;
	$target_path = "";
	################################ file upload#######################################
	if (!empty($_FILES["uploadedimage"]["name"])) 
	{
		$file_name=$_FILES["uploadedimage"]["name"];
		//echo  $file_name."<br>";
		$temp_name=$_FILES["uploadedimage"]["tmp_name"];
		$imgtype=$_FILES["uploadedimage"]["type"];
		$ext= GetImageExtension($imgtype);
		$imagename=$file_name;
		//$target_path = "../uploads/".$imagename;
		 
		 
		$temp = explode(".", $_FILES["uploadedimage"]["name"]);
		$newfilename =$recdt.round(microtime(true)) . '.' . end($temp);
		$target_path = "uploads/".$newfilename;
		
		if($target_path!='')
		{
			if(!move_uploaded_file($temp_name, $target_path))
			 {
				echo "<script type='text/javascript'>alert('Unable to move file!')</script>";
				echo '<script language="JavaScript"> window.location.href ="desk_action_perfom_form.php?flag=n&no='.$id.'" </script>';
					break;

			}
			else
			{
				move_uploaded_file($temp_name, $target_path);
			}
		}
	}
	else
	{
		$target_path = "uploads/file_nt_uploaded.jpg";
	}
	################################ end of file upload #######################################
	$target_path = "../".$target_path;
	
	$ltrtype=$_POST['ltrtype'];
     $dptinwardno=$_POST['dptinwardno'];
	$actiontaken1=$_POST['actiontake1'];
	convert_marathi();
	if($actiontaken1!='other')
	{
		$qrym=mysql_query("UPDATE incoming_letters_transaction SET desk_doument='$target_path',first_flag='N' WHERE ltr_inword_no='$dptinwardno'");
	
		$qry=("UPDATE tapal_to_dpt_transaction SET  action_taken='$actiontaken1',scan_doc='$target_path' WHERE dpt_ltr_id='$id'");
	}
	else
	{
		$ationtaken=$_POST['actiontake'];
		$qrym=mysql_query("UPDATE incoming_letters_transaction SET desk_doument='$target_path' ,first_flag='N' WHERE ltr_inword_no='$dptinwardno'");
			
		$qry=("UPDATE tapal_to_dpt_transaction SET  action_taken='$ationtaken',scan_doc='$target_path' WHERE dpt_ltr_id='$id'");
	}
	/************************************************
	action performed if update button is selected
	*************************************************/
    if (isset($_POST['update']))
	{
		/*$qry2=("UPDATE desk_transaction, tapal_to_dpt_transaction SET update_date=curdate(), desk_approval='updated', ltr_status='updated' WHERE desk_ltr_id='$id' AND tapal_to_dpt_transaction.dpt_ltr_id=desk_transaction.dpt_ltr_id");*/
		if($ltrtype=='ब')
		{
			//$futureDate=date('Y-m-d', strtotime('+30 year'));
			$now23 =date('Y')+30;
			$futureDate =$now23.'-'.date('m-d');
		}
		else if($ltrtype=='क')
		{
			$now23 =date('Y')+10;
			$futureDate =$now23.'-'.date('m-d');
		}
		else if($ltrtype=='क१')
		{
			$now23 =date('Y')+5;
			$futureDate =$now23.'-'.date('m-d');
		}
		else if($ltrtype=='ड')
		{
			$now23 =date('Y')+1;
			$futureDate =$now23.'-'.date('m-d');
		}
		$qry2=("UPDATE tapal_to_dpt_transaction 
				SET update_date=curdate(), ltr_status='updated' ,vargawari='$ltrtype', nasti_nashta_dt='$futureDate'
				WHERE dpt_ltr_id='$id' ") or die("hhhhh".mysql_error());
		
		if(mysql_query($qry2)) 
		{ 
		mysql_query($qry);
			echo "<script type='text/javascript'>alert('Action Performed Successfully!')</script>";
			echo '<script language="JavaScript"> window.location.href ="desk_action_perform.php?flag=n" </script>';
		}
		else
		{   
		echo "<script type='text/javascript'>alert('Problem to perform action!')</script>";
		echo '<script language="JavaScript"> window.location.href ="desk_action_perform.php?flag=n" </script>';
		}
	}
	/************************************************
		action performed if dispatch button is selected
	*************************************************/
	if (isset($_POST['dispatch']))
	{		
		require_once('dispatch_dpt.php');// Calling dispatch_dpt.php File
		mysql_query($qry);
	}
	/************************************************
		Rejected letter's dispatch button is selected
		
	*************************************************/
 /*	if (isset($_POST['rejdispatch']))
	{ 
		$recdt = date('Y-m-d', strtotime($redt));
		$rejid=$_POST['rejid'];
		require_once('dispatch.php');// Calling dispatch.php File
		if(mysql_query($qry))
		{
			$qry10=mysql_query("UPDATE desk_transaction,ltr_rejection SET desk_transaction.action_taken='$actiontaken', desk_transaction.dispatched_dt=curdate(),desk_transaction.flag='',desk_transaction.vargawari='$ltrtype' WHERE desk_transaction.desk_ltr_id='$id' AND ltr_rejection.ltr_rej_id='$rejid' AND desk_transaction.desk_ltr_id=ltr_rejection.desk_ltr_id AND desk_transaction.ltr_rej_id=ltr_rejection.ltr_rej_id") or ("qqqqqqq".mysql_error());
			
		}
		else
		{
			echo "<script type='text/javascript'>alert('Problem to perform action!')</script>";
			echo '<script language="JavaScript"> window.location.href ="desk_action_perform.php" </script>';
		}
	}*/

}// else if($flag=='n')

?>

<?php
function GetImageExtension($imagetype)
{
  if(empty($imagetype)) return false;
   switch($imagetype)
   {
       case 'image/bmp': return '.bmp';
       case 'image/gif': return '.gif';
       case 'image/jpeg': return '.jpg';
       case 'image/png': return '.png';
	   case 'image/pdf': return '.pdf';
	   case 'image/BMP': return '.BMP';
       case 'image/GIF': return '.GIF';
       case 'image/JPEG': return '.JPG';
       case 'image/PNG': return '.PNG';
	   case 'image/PDF': return '.PDF';
       default: return false;
   }
}
?>
