<?php 
session_start();

	require_once('db.php');
	 require_once('../convert_marathi.php');

	$username = $_SESSION['User_Name'];
	$m1    = mysql_query("SELECT * FROM login WHERE login_id='$username'");
	$rm1   = mysql_fetch_array($m1);
	$logid = $rm1['id'];
	 ################################################### Dispatch DB ###############################################################
	
	 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';
           default: return false;
       }
    }
	
	$id=$_GET['no'];
	$num1=$_REQUEST['num'];
	
	$usertype=$_POST['usertype']; //same or other user
	
	$dispa_via=$_POST['dispatchtype'];	//email or post or email/post
	
	$dispato=$_POST['dispatchmethod'];// dept or permanent
	
	if($dispato=='same')	
	{		
		$dispato='permanent';// dept or permanent	
		$usertype='same';// dept or permanent	
	}	
	else if($dispato=='other')	
	{		
		$dispato='permanent';// dept or permanent		
		$usertype='other';// dept or permanent
	}
	//same
	$addressed_to=$_POST['name'];
	$sameemail=$_POST['sameemailid'];
	$ltrid=$_POST['ltrid'];
	$sameaddress=$_POST['sameadd'];
	$path=$_POST['path'];
	
	//other
	$addressed_toother=$_POST['othername'];
	$email=$_POST['emailid'];
	$address=$_POST['add'];
	$deptid=$_POST['deptid'];
	$curdeptid=$_POST['curdeptid'];
	
	$vargawariii=$_POST['ltrtype'];
	
	$name6=$_POST['name6'];//by hand
	################################ file upload #######################################
		if (!empty($_FILES["uploadedimage"]["name"])) 
		{
			$file_name=$_FILES["uploadedimage"]["name"];
			//echo $file_name;
			$temp_name=$_FILES["uploadedimage"]["tmp_name"];
			$imgtype=$_FILES["uploadedimage"]["type"];
			$ext= GetImageExtension($imgtype);
			$imagename=$file_name;
			$target_path = "../uploads/".$imagename;
			if(!move_uploaded_file($temp_name, $target_path))//if file moved successfully	
			{
				echo "<script type='text/javascript'>alert('Unable to move file!')</script>";
				echo '<script language="JavaScript"> window.location.href ="../desk_action_perform.php?flag=n" </script>';	
			}
			else
			{
				move_uploaded_file($temp_name, $target_path);
			}
		}
		else{
			$target_path = "";
		}
		################################ end of file upload #######################################
		
	
				/*********************************************
						fetching records			
				**********************************************/
				/*$qry1=("SELECT * FROM desk_transaction,incoming_letters_transaction,tapal_to_dpt_transaction where desk_transaction.desk_ltr_id='$id' AND tapal_to_dpt_transaction.dpt_ltr_id=desk_transaction.dpt_ltr_id AND tapal_to_dpt_transaction.ltr_id=incoming_letters_transaction.ltr_id");*/
				$rs1=mysql_query("SELECT * FROM incoming_letters_transaction i,tapal_to_dpt_transaction t
								where t.dpt_ltr_id='$id'
								AND t.ltr_id=i.ltr_id") or die("abababab".mysql_error());
				$rw1=mysql_fetch_array($rs1);
				//$deskinward=$rw1['desk_ltr_inword_no'];
				$ltrinwordno=$rw1['ltr_inword_no'];		//incoming ltr inward no
				$dptltr_inward_no=$rw1['dpt_inword_no'];	//dept_ltr_inward_no
				
			//	echo $vargawariii."xxxxxxxxxxxx";
				/**********************************************
							Insert Query for same user name 
				***********************************************/
				if($usertype=='same' && $dispato=='permanent' && $rw1['ltr_rej_id']=='0')
				{	
					if($addressed_to!='')
					{
						if(($dispa_via=='email' && $sameemail!='' ) || ($dispa_via=='post' && $sameaddress!='') || ($dispa_via=='emailpost' && $sameaddress!='' && $sameemail!='') || ($dispa_via=='byhand'))
						{
							$ab=strtotime(date("Y-m-d"));
							convert_marathi();
							if($vargawariii=='ब')
							{
								//$futureDate=date('Y-m-d', strtotime('+30 year'));
								$now23 =date('Y')+30;
								$futureDate =$now23.'-'.date('m-d');
								
							}
							else if($vargawariii=='क')
							{
								$now23 =date('Y')+10;
								$futureDate =$now23.'-'.date('m-d');
							}
							else if($vargawariii=='क१')
							{
								$now23 =date('Y')+5;
								$futureDate =$now23.'-'.date('m-d');
							}
							else if($vargawariii=='ड')
							{
								$now23 =date('Y')+1;
								$futureDate =$now23.'-'.date('m-d');
							}
							
							$qry2=("INSERT INTO dispatched_transaction (outword_no,dpt_inword_no, dispatched_via,name_byhand, dispatched_to, dept_id_from, addressed_to, email_id, address,ltr_id) VALUES('$ltrinwordno','$dptltr_inward_no','$dispa_via','$name6','$dispato','$curdeptid','$addressed_to','$sameemail','$sameaddress','$ltrid')");
							
							
							$qry3=("UPDATE tapal_to_dpt_transaction t
								SET t.status='dispatched',t.ltr_status='dispatched' ,t.dispatched_date=curdate(),t.nasti_nashta_dt='$futureDate',t.scan_doc='$target_path'
								WHERE t.dpt_ltr_id='$id' ");
							
							$qry=("UPDATE tapal_to_dpt_transaction SET vargawari='$vargawariii' WHERE dpt_ltr_id='$id'");
							mysql_query($qry) or die("axcdsw".mysql_error());
							
							if(mysql_query($qry2))
							{  	
								
								mysql_query($qry3) or die("error".mysql_error());		
								echo "<script type='text/javascript'>alert('Action Performed Successfully!')</script>";
								
								
								//////////
								if(mysql_query($qry3))
								{
								
								
								
								$qrye =mysql_query("SELECT t.dpt_ltr_id, t.dept_id_from, t.action_taken, t.dpt_ltr_rec_dt, t.dpt_inword_no, i.ltr_type, t.dpt_inword_no, t.dpt_ltr_id, t.dpt_ltr_rec_dt, t.dpt_inword_no, t.desk_id, t.dept_id_from, i.subject, t.update_date, t.dispatched_date, t.status, di.addressed_to, t.ltr_status, i.sender_name, i.ltr_id, i.ltr_inword_no, i.receive_dt, t.sent_to_desk_dt, i.ref_dt, i.ref_no, i.dept_id, i.document, t.scan_doc, di.outword_no, di.dispatched_to, i.sent_dt, di.dispatched_to, di.email_id, t.ltr_category, t.assign_no
								FROM dispatched_transaction AS di, incoming_letters_transaction AS i, tapal_to_dpt_transaction AS t
								WHERE di.dispatched_to = 'permanent'
								AND i.ltr_id = t.ltr_id
								AND t.ltr_id = di.ltr_id
								AND t.dpt_ltr_id = '$id' ") or die("error".mysql_error());
								while($rw=mysql_fetch_array($qrye))	 
							{	
							
							
							
								$nuk=$rw['dpt_ltr_id'];
								$refdt = strtotime($rw['ref_dt']);
								$refdt1= date('d-m-Y', $refdt);
								$recdt = strtotime($rw['receive_dt']);
								$recdt1= date('d-m-Y', $recdt);
								$outdt = strtotime($rw['dispatched_dt']);
								$dispatchdt= date('d-m-Y', $outdt);
								$did=$rw['dept_id_from'];
								$dpt=mysql_query("SELECT dept_nm from department_master WHERE dept_id='$did'");
								$dptnm=mysql_fetch_array($dpt);
								##############################################################################
									// if taken action is approved assign flag=true
								###############################################################################
															
								$path=$rw['scan_doc'];
								//echo "sssssssssss".$rw['email_id'];
								$str2 = substr($path, 1);
								//echo "path is".$str2;
																
								$sameemail=$rw['email_id'];
								//////////////////////////   eMAIL SENDING ///////////////////////////////////////	
								require_once('../PHPMailer/class.phpmailer.php');
								require_once("../PHPMailer/class.smtp.php");
								require_once("../PHPMailer/class.pop3.php");
								//$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
								// in sms sending put path from /../..
								
								
								
								/*
								if($rw['scan_doc']!='')
								{	
									$path=$rw['scan_doc'];// full path of file 
									$str3 = substr($path, 3);// necssary path of file 
									$filename = basename($path); 			
									$ext1 = end(explode('.', $filename));	
									$ext1 = substr(strrchr($filename, '.'), 1);
									$ext1 = substr($filename, strrpos($filename, '.') + 1);
									$ext1 = preg_replace('/^.*\.([^.]+)$/D', '$1', $filename);
									$exts1 = split("[/\\.]", $filename);
									$n1    = count($exts1)-1;
									$outext  = $exts1[$n1];
									$file_size = filesize($str3);													
									$handle = fopen($str3, "r");	
									$content = fread($handle, $file_size);
									fclose($handle);
									$mailto=$rw['email_id'];
									$subject='संदर्भ क्र. '. $rw['ref_no'] .'  वर  केलेली कार्यवाही  ';
									$content = chunk_split(base64_encode($content));

											// a random hash will be necessary to send mixed content
											$separator = md5(time());

											// carriage return type (we use a PHP end of line constant)
											$eol = PHP_EOL;

											// main header (multipart mandatory)
											$headers = "From: पोलीस अधिक्षक कार्यालय, जि.रायगड <splendid@gains.nanodnscloud.com	>" . $eol;
											$headers .= "MIME-Version: 1.0" . $eol;
											
											$headers .= "Content-Type: multipart/mixed; boundary=\"" . $separator . "\"" . $eol . $eol;
											$headers .= "Content-Transfer-Encoding: 7bit" . $eol;
											$headers .= "This is a MIME encoded message." . $eol . $eol;

											// message
											
											$headers .= "--" . $separator . $eol;
											$headers .= "Content-Type: text/html; charset=\"iso-8859-1\"" . $eol;
											$headers .= "Content-Transfer-Encoding: 8bit" . $eol . $eol;
											$headers .="<body><table border='1'>
											<tr>
												<th>नाव </th>
												<th>आवक क्रमांक</th>
												<th>आवक दिनांक</th>
												<th>संदर्भ क्रमांक </th>
												<th>संदर्भ दिनांक </th>
												<th>विषय</th>
												<th>विभागाचे नाव</th>
												<th>केलेली कार्यवाही</th>
												<th>जावक क्रमांक </th>
												<th>जावक दिनांक </th>
											</tr>
													<tr>
														<td>".$rw['sender_name']."</td>
														<td>".$rw['ref_no']."</td>
														<td>".$refdt1."</td>
														<td>".$rw['ltr_inword_no']."</td>
														<td>".$recdt1."</td>
														<td>".$rw['subject']."</td>
														<td>".$dptnm['dept_nm']."</td>
														<td>".$rw['action_taken']."</td>
														<td>".$rw['outword_no']."</td>
														<td>".$dispatchdt."</td>
													</tr>
													</table>
													</body>".$eol . $eol;

													// attachment
													$headers .= "--" . $separator. $eol;
													if($outext=='pdf' || $outext=='PDF')
													{
														$headers .= "Content-Type: application/pdf; name=\"" . $filename . "\"" . $eol;										
													}
													else if($outext=='jpg' || $outext=='JPG' ||$outext=='jpeg'  || $outext=='JPEG' )
													{
														$headers .= "Content-Type: image/jpeg; name=\"" . $filename . "\"" . $eol;
													}
													else if($outext=='gif' || $outext=='GIF' )
													{
														$headers .= "Content-Type: image/gif; name=\"" . $filename . "\"" . $eol;
													}
													else if($outext=='PNG' || $outext=='png')
													{
														$headers .= "Content-Type: image/png; name=\"" . $filename . "\"" . $eol;
																
													}
													else if($outext=='bmp' || $outext=='BMP')
													{
														$headers .= "Content-Type: 	image/bmp; name=\"" . $filename . "\"" . $eol;	
													}
													$headers .= "Content-Transfer-Encoding: base64" . $eol;
													$headers .= "Content-Disposition: attachment"  . $eol . $eol;
													$headers .= $content . $path. $eol . $eol;
													$headers .= "--" . $separator . "--";

													//SEND Mail
													 if (mail($mailto, $subject,"", $headers))
													 {
														//echo "<br>mail send ... OK"; // or use booleans here
														
														$sql2=("UPDATE  tapal_to_dpt_transaction SET flag='true' WHERE dpt_ltr_id='$nuk'");
														if(mysql_query($sql2))
														{
															echo "<script type='text/javascript'>alert('Approved Successfully!')</script>";
															echo '<script language="JavaScript"> window.location.href ="" </script>';
														}
														else
														{
															echo "<script type='text/javascript'>alert('Approve Unsuccessful!')</script>";
															echo '<script language="JavaScript"> window.location.href ="" </script>';	
														}	
													} 
													else 
													{
														echo "<script type='text/javascript'>alert('Approve Unsuccessful!')</script>";
														echo '<script language="JavaScript"> window.location.href ="" </script>';	
													}
												}//if scan_doc!=''
												else 
												{	
													$mailto=$rw['email_id'];
													$subject=' संदर्भ क्र.  '. $rw['ref_no'] .'  वर  केलेली कार्यवाही  ';

													// a random hash will be necessary to send mixed content
													$separator = md5(time());

													// carriage return type (we use a PHP end of line constant)
													$eol = PHP_EOL;
													// main header (multipart mandatory)
													$headers = "From:  पोलीस अधिक्षक कार्यालय, जि.रायगड <splendid@gains.nanodnscloud.com	>" . $eol;
													$headers .= "MIME-Version: 1.0" . $eol;
													
													$headers .= "Content-Type: multipart/mixed; boundary=\"" . $separator . "\"" . $eol . $eol;
													$headers .= "Content-Transfer-Encoding: 7bit" . $eol;
													$headers .= "This is a MIME encoded message." . $eol . $eol;
													// message
													
													$headers .= "--" . $separator . $eol;
													$headers .= "Content-Type: text/html; charset=\"iso-8859-1\"" . $eol;
													$headers .= "Content-Transfer-Encoding: 8bit" . $eol . $eol;
													$headers .="<body><table border='1'>
													<tr>
														<th>नाव </th>
														<th>आवक क्रमांक</th>
														<th>आवक दिनांक</th>
														<th> संदर्भ क्रमांक </th>
														<th>संदर्भ दिनांक </th>
														<th>विषय</th>
														<th>विभागाचे नाव</th>
														<th>केलेली कार्यवाही</th>
														<th> जावक क्रमांक </th>
														<th> जावक दिनांक </th>
													</tr>
													<tr>
														<td>".$rw['sender_name']."</td>
														<td>".$rw['ref_no']."</td>
														<td>".$refdt1."</td>
														<td>".$rw['ltr_inword_no']."</td>
														<td>".$recdt1."</td>
														<td>".$rw['subject']."</td>			
														<td>".$dptnm['dept_nm']."</td>
														<td>".$rw['action_taken']."</td>
														<td>".$rw['outword_no']."</td>
														<td>".$dispatchdt."</td>
													</tr>
													</table>
													</body>".$eol . $eol;
													//SEND Mail
													 if (mail($mailto, $subject,"" ,$headers))
													 {
														//echo "<br>mail send ... OK"; // or use booleans here
														$sql2=("UPDATE  tapal_to_dpt_transaction SET flag='true' WHERE dpt_ltr_id='$nuk'");
														if(mysql_query($sql2))
														{
															echo "<script type='text/javascript'>alert('Approved Successfully!')</script>";
															echo '<script language="JavaScript"> window.location.href ="" </script>';
														}
														else
														{
															echo "<script type='text/javascript'>alert('Approve Unsuccessful!')</script>";
															echo '<script language="JavaScript"> window.location.href ="" </script>';	
														}	
													} 	
													else 
													{
														//echo "<br>mail send ... ERROR!";
														echo "<script type='text/javascript'>alert('Approve Unsuccessful!')</script>";
														echo '<script language="JavaScript"> window.location.href ="" </script>';
													 }
												}
												*/
												
												
												
												
												
												
												
										
												
												
												
												
												if($rw['scan_doc']!='')
								{	
									$path=$rw['scan_doc'];// full path of file 
									$str3 = substr($path, 3);// necssary path of file 
									$filename = basename($path); 			
									$ext1 = end(explode('.', $filename));	
									$ext1 = substr(strrchr($filename, '.'), 1);
									$ext1 = substr($filename, strrpos($filename, '.') + 1);
									$ext1 = preg_replace('/^.*\.([^.]+)$/D', '$1', $filename);
									$exts1 = split("[/\\.]", $filename);
									$n1    = count($exts1)-1;
									$outext  = $exts1[$n1];
									$file_size = filesize($str3);													
									$handle = fopen($str3, "r");	
									$content = fread($handle, $file_size);
									
									fclose($handle);
									
									$mailto=$rw['email_id'];
									$subject='संदर्भ क्र. '. $rw['ref_no'] .'  वर  केलेली कार्यवाही  ';
									
									
									
									$file = $path;
									$file_size = filesize($file);
									$handle = fopen($file, "r");
									$content = fread($handle, $file_size);
									fclose($handle);
									$content = chunk_split(base64_encode($content));
									$uid = md5(uniqid(time()));
									$name = basename($file);
									
									
									// header
									$header = "From:  पोलीस अधिक्षक कार्यालय, जि.रायगड <splendid@gains.nanodnscloud.com>\r\n";
									$header .= "MIME-Version: 1.0\r\n";
									$header .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n";

									
									$message="<body><table border='1'>
											<tr>
												<th>नाव </th>
												<th>आवक क्रमांक</th>
												<th>आवक दिनांक</th>
												<th>संदर्भ क्रमांक </th>
												<th>संदर्भ दिनांक </th>
												<th>विषय</th>
												<th>विभागाचे नाव</th>
												<th>केलेली कार्यवाही</th>
												<th>जावक क्रमांक </th>
												<th>जावक दिनांक </th>
											</tr>
													<tr>
														<td>".$rw['sender_name']."</td>
														<td>".$rw['ref_no']."</td>
														<td>".$refdt1."</td>
														<td>".$rw['ltr_inword_no']."</td>
														<td>".$recdt1."</td>
														<td>".$rw['subject']."</td>
														<td>".$dptnm['dept_nm']."</td>
														<td>".$rw['action_taken']."</td>
														<td>".$rw['outword_no']."</td>
														<td>".$dispatchdt."</td>
													</tr>
													</table>
													</body>";
									
									// message & attachment
									$nmessage = "--".$uid."\r\n";
									$nmessage .= "Content-type:text/html; charset=iso-8859-1\r\n";
									$nmessage .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
									$nmessage .= $message."\r\n\r\n";
									$nmessage .= "--".$uid."\r\n";
									$nmessage .= "Content-Type: application/octet-stream; name=\"".$filename."\"\r\n";
									$nmessage .= "Content-Transfer-Encoding: base64\r\n";
									$nmessage .= "Content-Disposition: attachment; filename=\"".$filename."\"\r\n\r\n";
									$nmessage .= $content."\r\n\r\n";
									$nmessage .= "--".$uid."--";
									
									
									
													//echo "<br>".$mailto."--".$subject."--".$headers;
													//SEND Mail
													if (mail($mailto, $subject, $nmessage, $header))
													{
														//echo "<br>mail send ... OK"; // or use booleans here
														
														$sql2=("UPDATE  tapal_to_dpt_transaction SET flag='true' WHERE dpt_ltr_id='$nuk'");
														if(mysql_query($sql2))
														{
															//echo "okkk2123";
															echo "<script type='text/javascript'>alert('Approved Successfully!')</script>";
															//echo '<script language="JavaScript"> window.location.href ="" </script>';
														}
														else
														{
															//echo "fail11";
															
															echo "<script type='text/javascript'>alert('Approve Unsuccessful!')</script>";
															//echo '<script language="JavaScript"> window.location.href ="" </script>';	
														}	
													} 
													else 
													{
														//echo "fail22";

														echo "<script type='text/javascript'>alert('Approve Unsuccessful!')</script>";
														//echo '<script language="JavaScript"> window.location.href ="" </script>';	
													}
												}//if scan_doc!=''
												else 
												{	
													$mailto=$rw['email_id'];
													$subject=' संदर्भ क्र.  '. $rw['ref_no'] .'  वर  केलेली कार्यवाही  ';

													// carriage return type (we use a PHP end of line constant)
													$eol = PHP_EOL;
									
													$headers = "From:  पोलीस अधिक्षक कार्यालय, जि.रायगड <splendid@gains.nanodnscloud.com	>" . $eol;
													$headers .= "MIME-Version: 1.0" . "\r\n";
													$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
													
													$headers .="<body><table border='1'>
													<tr>
														<th>नाव </th>
														<th>आवक क्रमांक</th>
														<th>आवक दिनांक</th>
														<th> संदर्भ क्रमांक </th>
														<th>संदर्भ दिनांक </th>
														<th>विषय</th>
														<th>विभागाचे नाव</th>
														<th>केलेली कार्यवाही</th>
														<th> जावक क्रमांक </th>
														<th> जावक दिनांक </th>
													</tr>
													<tr>
														<td>".$rw['sender_name']."</td>
														<td>".$rw['ref_no']."</td>
														<td>".$refdt1."</td>
														<td>".$rw['ltr_inword_no']."</td>
														<td>".$recdt1."</td>
														<td>".$rw['subject']."</td>			
														<td>".$dptnm['dept_nm']."</td>
														<td>".$rw['action_taken']."</td>
														<td>".$rw['outword_no']."</td>
														<td>".$dispatchdt."</td>
													</tr>
													</table>
													</body>";
													
													
													
													//SEND Mail
													if (mail($mailto, $subject, "", $headers ))
													 {
														//echo "<br>mail send ... OK"; // or use booleans here
														$sql2=("UPDATE  tapal_to_dpt_transaction SET flag='true' WHERE dpt_ltr_id='$nuk'");
														if(mysql_query($sql2))
														{
															//echo "okkkk";
															echo "<script type='text/javascript'>alert('Approved Successfully !')</script>";
															//echo '<script language="JavaScript"> window.location.href ="" </script>';
														}
														else
														{
															//echo "fail";
															echo "<script type='text/javascript'>alert('Approve Unsuccessful !')</script>";
															//echo '<script language="JavaScript"> window.location.href ="" </script>';	
														}	
													} 	
													else 
													{
														//echo "<br>mail send ... ERROR!";
														
														echo "<script type='text/javascript'>alert('Approve Unsuccessful !')</script>";
														//echo '<script language="JavaScript"> window.location.href ="" </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 action123!')</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 action2!')</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 action3!')</script>";
						echo '<script language="JavaScript"> window.location.href ="../desk_action_perform.php?flag=n" </script>';
					}
				}
				
				
				#################################################################################################
				
				
				#################################################################################################
				if($usertype=='same' && $dispato=='permanent' && $rw1['ltr_rej_id']!='0')
				{	
					if($addressed_to!='')
					{
						if(($dispa_via=='email' && $sameemail!='' ) || ($dispa_via=='post' && $sameaddress!='') || ($dispa_via=='emailpost' && $sameaddress!='' && $sameemail!='') || ($dispa_via=='byhand'))
						{
						convert_marathi();
						if($vargawariii=='ब')
							{
								//$futureDate=date('Y-m-d', strtotime('+30 year'));
								$now23 =date('Y')+30;
								$futureDate =$now23.'-'.date('m-d');
								
							}
							else if($vargawariii=='क')
							{
								$now23 =date('Y')+10;
								$futureDate =$now23.'-'.date('m-d');
							}
							else if($vargawariii=='क१')
							{
								$now23 =date('Y')+5;
								$futureDate =$now23.'-'.date('m-d');
							}
							else if($vargawariii=='ड')
							{
								$now23 =date('Y')+1;
								$futureDate =$now23.'-'.date('m-d');
							}
							
						$qry2=mysql_query("UPDATE dispatched_transaction SET dispatched_via='$dispa_via',name_byhand='$name6',dispatched_to='$dispato',dept_id_from='$curdeptid',addressed_to='$addressed_to',email_id='$sameemail', address='$sameaddress' WHERE desk_ltr_id='$id'") or die("".mysql_error());
						 
					
						$qry3=("UPDATE tapal_to_dpt_transaction SET tapal_to_dpt_transaction.desk_approval='dispatched', tapal_to_dpt_transaction.dispatched_dt=curdate(),t.nasti_nashta_dt='$futureDate', tapal_to_dpt_transaction.scan_doc='$target_path', tapal_to_dpt_transaction.ltr_status='dispatched' WHERE tapal_to_dpt_transaction.dpt_ltr_id='$id' ");
						
						$qry=("UPDATE tapal_to_dpt_transaction SET vargawari='$vargawariii' WHERE dpt_ltr_id='$id'");
						mysql_query($qry) or die("axcdsw".mysql_error());
						if(mysql_query($qry2))
							{  	
								mysql_query($qry3) or die("error".mysql_error());
								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 action1!')</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 action2!')</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 action3!')</script>";
						echo '<script language="JavaScript"> window.location.href ="../desk_action_perform.php?flag=n" </script>';
					}
					
				}
				
				######################################################################################################
				
				#######################################################################################################
				
				else if($usertype=='other' && $dispato=='permanent' && $rw1['ltr_rej_id']!='0')
				{
					if($addressed_toother!='')
					{
						if(($dispa_via=='email' && $email!='' ) || ($dispa_via=='post' && $address!='') || ($dispa_via=='emailpost' && $address!='' && $email!='') || ($dispa_via=='byhand'))
						{
						convert_marathi();
						if($vargawariii=='ब')
							{
								//$futureDate=date('Y-m-d', strtotime('+30 year'));
								$now23 =date('Y')+30;
								$futureDate =$now23.'-'.date('m-d');
								
							}
							else if($vargawariii=='क')
							{
								$now23 =date('Y')+10;
								$futureDate =$now23.'-'.date('m-d');
							}
							else if($vargawariii=='क१')
							{
								$now23 =date('Y')+5;
								$futureDate =$now23.'-'.date('m-d');
							}
							else if($vargawariii=='ड')
							{
								$now23 =date('Y')+1;
								$futureDate =$now23.'-'.date('m-d');
							}
							
						$qry2=("UPDATE dispatched_transaction SET dispatched_via='$dispa_via', dispatched_to='$dispato',name_byhand='$name6', dept_id_from='$curdeptid', addressed_to='$addressed_toother', email_id='$email', address='$address' WHERE desk_ltr_id='$id'") or die("".mysql_error());		
					
						$qry3=("UPDATE tapal_to_dpt_transaction SET tapal_to_dpt_transaction.status='dispatched', tapal_to_dpt_transaction.scan_doc='$target_path', tapal_to_dpt_transaction.dispatched_date=curdate(),tapal_to_dpt_transaction.nasti_nashta_dt='$futureDate',  tapal_to_dpt_transaction.ltr_status='dispatched' WHERE tapal_to_dpt_transaction.dpt_ltr_id='$id' ");
						
						$qry=("UPDATE tapal_to_dpt_transaction SET vargawari='$vargawariii' WHERE dpt_ltr_id='$id'");
						mysql_query($qry) or die("axcdsw".mysql_error());
						
						if(mysql_query($qry2))
						{
							mysql_query($qry3) or die("Error:101".mysql_error());
							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 action1!')</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 action2!')</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>';
					}
					
				}
				
				
				
		
					/**********************************************
							Insert Query for other user name 
				***********************************************/
				else if($usertype=='other' && $dispato=='permanent' && $rw1['ltr_rej_id']=='0')
				{
					if($addressed_toother!='')
					{
						if(($dispa_via=='email' && $email!='' ) || ($dispa_via=='post' && $address!='') || ($dispa_via=='emailpost' && $address!='' && $email!='') || ($dispa_via=='byhand'))
						{
							convert_marathi();
								
							if($vargawariii=='ब')
							{
								//$futureDate=date('Y-m-d', strtotime('+30 year'));
								$now23 =date('Y')+30;
								$futureDate =$now23.'-'.date('m-d');
								
							}
							else if($vargawariii=='क')
							{
								$now23 =date('Y')+10;
								$futureDate =$now23.'-'.date('m-d');
							}
							else if($vargawariii=='क१')
							{
								$now23 =date('Y')+5;
								$futureDate =$now23.'-'.date('m-d');
							}
							else if($vargawariii=='ड')
							{
								$now23 =date('Y')+1;
								$futureDate =$now23.'-'.date('m-d');
							}
						
							
							$qry2=("INSERT INTO dispatched_transaction(outword_no, dpt_inword_no,dispatched_to, dispatched_via ,name_byhand,dept_id_from, addressed_to, email_id, address, ltr_id, desk_ltr_id) VALUES('$ltrinwordno','$dptltr_inward_no ','$dispato','$dispa_via','$name6','$curdeptid','$addressed_toother','$email','$address','$ltrid','$id')") or die("".mysql_error());
					
					
							/*$qry4=("UPDATE desk_transaction,tapal_to_dpt_transaction SET tapal_to_dpt_transaction.status='dispatched', desk_transaction.scan_doc='$target_path', desk_transaction.dispatched_dt=curdate(), desk_transaction.ltr_status='dispatched' WHERE desk_transaction.desk_ltr_id='$id'  AND tapal_to_dpt_transaction.dpt_ltr_id=desk_transaction.dpt_ltr_id");*/
							$qry4=("UPDATE tapal_to_dpt_transaction t
								SET t.status='dispatched',t.ltr_status='dispatched' ,dispatched_date=curdate(),nasti_nashta_dt='$futureDate',scan_doc='$target_path'
								WHERE dpt_ltr_id='$id'");
							$qry=("UPDATE tapal_to_dpt_transaction SET vargawari='$vargawariii' WHERE dpt_ltr_id='$id' ");
							mysql_query($qry) or die("axcdsw".mysql_error());
								
							if(mysql_query($qry2))
							{	
								
								mysql_query($qry4);
								echo "<script type='text/javascript'>alert('Action Performed Successfully!')</script>";
									///////////////
							if(mysql_query($qry4))
							{
							/*$qrye =mysql_query("SELECT de.desk_ltr_id, de.desk_id_from, de.action_taken, de.desk_ltr_rec_dt, de.desk_ltr_inword_no, i.ltr_type, t.dpt_inword_no, t.dpt_ltr_id, t.dpt_ltr_rec_dt, t.dpt_inword_no, t.desk_id, t.dept_id_from, i.subject, de.update_date, de.dispatched_dt, de.ltr_approval, di.addressed_to, de.ltr_status, i.sender_name, i.ltr_id, i.ltr_inword_no, i.receive_dt, t.sent_to_desk_dt, i.ref_dt, i.ref_no, i.dept_id, i.document, de.scan_doc, di.outword_no,  di.dispatched_to, i.sent_dt, di.dispatched_to, di.email_id, de.ltr_category, de.assign_no FROM dispatched_transaction as di, desk_transaction as de, incoming_letters_transaction as i, tapal_to_dpt_transaction AS t WHERE di.dispatched_to='permanent' and i.ltr_id=t.ltr_id AND t.dpt_ltr_id=de.dpt_ltr_id AND de.desk_ltr_id=di.desk_ltr_id AND di.desk_ltr_id=de.desk_ltr_id AND di.desk_ltr_id='$num1' ") or die("error".mysql_error());*/
							
							$qrye =mysql_query("SELECT t.dpt_ltr_id, t.dept_id_from, t.action_taken, t.dpt_ltr_rec_dt, t.dpt_inword_no, i.ltr_type, t.dpt_inword_no, t.dpt_ltr_id, t.dpt_ltr_rec_dt, t.dpt_inword_no, t.desk_id, t.dept_id_from, i.subject, t.update_date, t.dispatched_date, t.status, di.addressed_to, t.ltr_status, i.sender_name, i.ltr_id, i.ltr_inword_no, i.receive_dt, t.sent_to_desk_dt, i.ref_dt, i.ref_no, i.dept_id, i.document, t.scan_doc, di.outword_no, di.dispatched_to, i.sent_dt, di.dispatched_to, di.email_id, t.ltr_category, t.assign_no
								FROM dispatched_transaction AS di, incoming_letters_transaction AS i, tapal_to_dpt_transaction AS t
								WHERE di.dispatched_to = 'permanent'
								AND i.ltr_id = t.ltr_id
								AND t.ltr_id = di.ltr_id
								AND t.dpt_ltr_id = '$num1' ") or die("error".mysql_error());
			
			while($rw=mysql_fetch_array($qrye))	 
			{	
				$nuk=$rw['dpt_ltr_id'];
				$refdt = strtotime($rw['ref_dt']);
				$refdt1= date('d-m-Y', $refdt);
				$recdt = strtotime($rw['receive_dt']);
				$recdt1= date('d-m-Y', $recdt);
				$outdt = strtotime($rw['dispatched_dt']);
				$dispatchdt= date('d-m-Y', $outdt);
				$did=$rw['dept_id_from'];
				$dpt=mysql_query("SELECT dept_nm from department_master WHERE dept_id='$did'");
				$dptnm=mysql_fetch_array($dpt);
				##############################################################################
					// if taken action is approved assign flag=true
				###############################################################################
											
				$path=$rw['scan_doc'];
				//echo "sssssssssss".$rw['email_id'];
				$str2 = substr($path, 1);
				//echo "path is".$str2;
												
				$sameemail=$rw['email_id'];
				//////////////////////////   eMAIL SENDING ///////////////////////////////////////	
				require_once('../PHPMailer/class.phpmailer.php');
				require_once("../PHPMailer/class.smtp.php");
				require_once("../PHPMailer/class.pop3.php");
				//$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
				// in sms sending put path from /../..
				
				
				
				
				
				/*
				if($rw['scan_doc']!='')
				{	
					$path=$rw['scan_doc'];// full path of file 
					$str3 = substr($path, 3);// necssary path of file 
					$filename = basename($path); 			
					$ext1 = end(explode('.', $filename));	
					$ext1 = substr(strrchr($filename, '.'), 1);
					$ext1 = substr($filename, strrpos($filename, '.') + 1);
					$ext1 = preg_replace('/^.*\.([^.]+)$/D', '$1', $filename);
					$exts1 = split("[/\\.]", $filename);
					$n1    = count($exts1)-1;
					$outext  = $exts1[$n1];
					$file_size = filesize($str3);													
					$handle = fopen($str3, "r");	
					$content = fread($handle, $file_size);
					fclose($handle);
					$mailto=$rw['email_id'];
					$subject='संदर्भ क्र. '. $rw['ref_no'] .'  वर  केलेली कार्यवाही  ';
					$content = chunk_split(base64_encode($content));

											// a random hash will be necessary to send mixed content
											$separator = md5(time());

											// carriage return type (we use a PHP end of line constant)
											$eol = PHP_EOL;

											// main header (multipart mandatory)
											$headers = "From:  पोलीस अधिक्षक कार्यालय, जि.रायगड <splendid@gains.nanodnscloud.com	>" . $eol;
											$headers .= "MIME-Version: 1.0" . $eol;
											
											$headers .= "Content-Type: multipart/mixed; boundary=\"" . $separator . "\"" . $eol . $eol;
											$headers .= "Content-Transfer-Encoding: 7bit" . $eol;
											$headers .= "This is a MIME encoded message." . $eol . $eol;

											// message
											
											$headers .= "--" . $separator . $eol;
											$headers .= "Content-Type: text/html; charset=\"iso-8859-1\"" . $eol;
											$headers .= "Content-Transfer-Encoding: 8bit" . $eol . $eol;
											$headers .="<body><table border='1'>
											<tr>
												<th>नाव </th>
												<th>आवक क्रमांक</th>
												<th>आवक दिनांक</th>
												<th> संदर्भ क्रमांक </th>
												<th>संदर्भ दिनांक </th>
												<th>विषय</th>
												<th>विभागाचे नाव</th>
												<th>केलेली कार्यवाही</th>
												<th> जावक क्रमांक </th>
												<th> जावक दिनांक </th>
											</tr>
													<tr>
														<td>".$addressed_toother."</td>
														<td>".$rw['ref_no']."</td>
														<td>".$refdt1."</td>
														<td>".$rw['ltr_inword_no']."</td>
														<td>".$recdt1."</td>
														<td>".$rw['subject']."</td>
														<td>".$dptnm['dept_nm']."</td>
														<td>".$rw['action_taken']."</td>
														<td>".$rw['outword_no']."</td>
														<td>".$dispatchdt."</td>
													</tr>
													</table>
													</body>".$eol . $eol;

													// attachment
													$headers .= "--" . $separator. $eol;
													if($outext=='pdf' || $outext=='PDF')
													{
														$headers .= "Content-Type: application/pdf; name=\"" . $filename . "\"" . $eol;										
													}
													else if($outext=='jpg' || $outext=='JPG' ||$outext=='jpeg'  || $outext=='JPEG' )
													{
														$headers .= "Content-Type: image/jpeg; name=\"" . $filename . "\"" . $eol;
													}
													else if($outext=='gif' || $outext=='GIF' )
													{
														$headers .= "Content-Type: image/gif; name=\"" . $filename . "\"" . $eol;
													}
													else if($outext=='PNG' || $outext=='png')
													{
														$headers .= "Content-Type: image/png; name=\"" . $filename . "\"" . $eol;
																
													}
													else if($outext=='bmp' || $outext=='BMP')
													{
														$headers .= "Content-Type: 	image/bmp; name=\"" . $filename . "\"" . $eol;	
													}
													$headers .= "Content-Transfer-Encoding: base64" . $eol;
													$headers .= "Content-Disposition: attachment"  . $eol . $eol;
													$headers .= $content . $path. $eol . $eol;
													$headers .= "--" . $separator . "--";

													//SEND Mail
													 if (mail($email, $subject,"", $headers))
													 {
														//echo "<br>mail send ... OK"; // or use booleans here
														
														$sql2=("UPDATE tapal_to_dpt_transaction SET flag='true' WHERE dpt_ltr_id='$nuk'");
														if(mysql_query($sql2))
														{
															echo "<script type='text/javascript'>alert('Approved Successfully!')</script>";
															echo '<script language="JavaScript"> window.location.href ="" </script>';
														}
														else
														{
															echo "<script type='text/javascript'>alert('Approve Unsuccessful!')</script>";
															echo '<script language="JavaScript"> window.location.href ="" </script>';	
														}	
													} 
													else 
													{
														echo "<script type='text/javascript'>alert('Approve Unsuccessful!')</script>";
														echo '<script language="JavaScript"> window.location.href ="" </script>';	
													}
												}//if scan_doc!=''
												else 
												{	
													$mailto=$rw['email_id'];
													$subject=' संदर्भ क्र.  '. $rw['ref_no'] .'  वर  केलेली कार्यवाही  ';

													// a random hash will be necessary to send mixed content
													$separator = md5(time());

													// carriage return type (we use a PHP end of line constant)
													$eol = PHP_EOL;
													// main header (multipart mandatory)
													$headers = "From:  पोलीस अधिक्षक कार्यालय, जि.रायगड <splendid@gains.nanodnscloud.com	>" . $eol;
													$headers .= "MIME-Version: 1.0" . $eol;
													
													$headers .= "Content-Type: multipart/mixed; boundary=\"" . $separator . "\"" . $eol . $eol;
													$headers .= "Content-Transfer-Encoding: 7bit" . $eol;
													$headers .= "This is a MIME encoded message." . $eol . $eol;
													// message
													
													$headers .= "--" . $separator . $eol;
													$headers .= "Content-Type: text/html; charset=\"iso-8859-1\"" . $eol;
													$headers .= "Content-Transfer-Encoding: 8bit" . $eol . $eol;
													$headers .="<body><table border='1'>
													<tr>
														<th>नाव </th>
														<th>आवक क्रमांक</th>
														<th>आवक दिनांक</th>
														<th> संदर्भ क्रमांक </th>
														<th>संदर्भ दिनांक </th>
														<th>विषय</th>
														<th>विभागाचे नाव</th>
														<th>केलेली कार्यवाही</th>
														<th> जावक क्रमांक </th>
														<th> जावक दिनांक </th>
													</tr>
													<tr>
														<td>".$rw['sender_name']."</td>
														<td>".$rw['ref_no']."</td>
														<td>".$refdt1."</td>
														<td>".$rw['ltr_inword_no']."</td>
														<td>".$recdt1."</td>
														<td>".$rw['subject']."</td>			
														<td>".$dptnm['dept_nm']."</td>
														<td>".$rw['action_taken']."</td>
														<td>".$rw['outword_no']."</td>
														<td>".$dispatchdt."</td>
													</tr>
													</table>
													</body>".$eol . $eol;
													//SEND Mail
													 if (mail($email, $subject,"" ,$headers))
													 {
														//echo "<br>mail send ... OK"; // or use booleans here
														$sql2=("UPDATE tapal_to_dpt_transaction SET flag='true' WHERE dpt_ltr_id='$nuk'");
														if(mysql_query($sql2))
														{
															echo "<script type='text/javascript'>alert('Approved Successfully!')</script>";
															echo '<script language="JavaScript"> window.location.href ="" </script>';
														}
														else
														{
															echo "<script type='text/javascript'>alert('Approve Unsuccessful!')</script>";
															echo '<script language="JavaScript"> window.location.href ="" </script>';	
														}	
													} 	
													else 
													{
														//echo "<br>mail send ... ERROR!";
														echo "<script type='text/javascript'>alert('Approve Unsuccessful!')</script>";
														echo '<script language="JavaScript"> window.location.href ="" </script>';
													 }
												}
												*/
												
												
												
												
												
												
												if($rw['scan_doc']!='')
				{	
					$path=$rw['scan_doc'];// full path of file 
					$str3 = substr($path, 3);// necssary path of file 
					$filename = basename($path); 			
					$ext1 = end(explode('.', $filename));	
					$ext1 = substr(strrchr($filename, '.'), 1);
					$ext1 = substr($filename, strrpos($filename, '.') + 1);
					$ext1 = preg_replace('/^.*\.([^.]+)$/D', '$1', $filename);
					$exts1 = split("[/\\.]", $filename);
					$n1    = count($exts1)-1;
					$outext  = $exts1[$n1];
					$file_size = filesize($str3);													
					$handle = fopen($str3, "r");	
					$content = fread($handle, $file_size);
					fclose($handle);
					$mailto=$rw['email_id'];
					$subject='संदर्भ क्र. '. $rw['ref_no'] .'  वर  केलेली कार्यवाही  ';
											
											
											
											
											
											
											
									$file = $path;
									$file_size = filesize($file);
									$handle = fopen($file, "r");
									$content = fread($handle, $file_size);
									fclose($handle);
									$content = chunk_split(base64_encode($content));
									$uid = md5(uniqid(time()));
									$name = basename($file);
									
									
									// header
									$header = "From:  पोलीस अधिक्षक कार्यालय, जि.रायगड<splendid@gains.nanodnscloud.com>\r\n";
									$header .= "MIME-Version: 1.0\r\n";
									$header .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n";

									
									$message="<body><table border='1'>
											<tr>
												<th>नाव </th>
												<th>आवक क्रमांक</th>
												<th>आवक दिनांक</th>
												<th> संदर्भ क्रमांक </th>
												<th>संदर्भ दिनांक </th>
												<th>विषय</th>
												<th>विभागाचे नाव</th>
												<th>केलेली कार्यवाही</th>
												<th> जावक क्रमांक </th>
												<th> जावक दिनांक </th>
											</tr>
													<tr>
														<td>".$addressed_toother."</td>
														<td>".$rw['ref_no']."</td>
														<td>".$refdt1."</td>
														<td>".$rw['ltr_inword_no']."</td>
														<td>".$recdt1."</td>
														<td>".$rw['subject']."</td>
														<td>".$dptnm['dept_nm']."</td>
														<td>".$rw['action_taken']."</td>
														<td>".$rw['outword_no']."</td>
														<td>".$dispatchdt."</td>
													</tr>
													</table>
													</body>";
									
									// message & attachment
									$nmessage = "--".$uid."\r\n";
									$nmessage .= "Content-type:text/html; charset=iso-8859-1\r\n";
									$nmessage .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
									$nmessage .= $message."\r\n\r\n";
									$nmessage .= "--".$uid."\r\n";
									$nmessage .= "Content-Type: application/octet-stream; name=\"".$filename."\"\r\n";
									$nmessage .= "Content-Transfer-Encoding: base64\r\n";
									$nmessage .= "Content-Disposition: attachment; filename=\"".$filename."\"\r\n\r\n";
									$nmessage .= $content."\r\n\r\n";
									$nmessage .= "--".$uid."--";
									
									
									
													//echo "<br>".$mailto."--".$subject."--".$headers;
													//SEND Mail
													if (mail($mailto, $subject, $nmessage, $header))
													{

														//echo "<br>mail send ... OK"; // or use booleans here

														$sql2=("UPDATE tapal_to_dpt_transaction SET flag='true' WHERE dpt_ltr_id='$nuk'");
														if(mysql_query($sql2))
														{
															echo "<script type='text/javascript'>alert('Approved Successfully!')</script>";
															//echo '<script language="JavaScript"> window.location.href ="" </script>';
														}
														else
														{
															echo "<script type='text/javascript'>alert('Approve Unsuccessful!')</script>";
															//echo '<script language="JavaScript"> window.location.href ="" </script>';	
														}	
													} 
													else 
													{
														echo "<script type='text/javascript'>alert('Approve Unsuccessful!')</script>";
														//echo '<script language="JavaScript"> window.location.href ="" </script>';	
													}
												}//if scan_doc!=''
												else 
												{	
													$mailto=$rw['email_id'];
													$subject=' संदर्भ क्र.  '. $rw['ref_no'] .'  वर  केलेली कार्यवाही  ';

													
													
													
													
													// carriage return type (we use a PHP end of line constant)
													$eol = PHP_EOL;
									
													$headers = "From:  पोलीस अधिक्षक कार्यालय, जि.रायगड <splendid@gains.nanodnscloud.com	>" . $eol;
													$headers .= "MIME-Version: 1.0" . "\r\n";
													$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
													
													$headers .="<body><table border='1'>
													<tr>
														<th>नाव </th>
														<th>आवक क्रमांक</th>
														<th>आवक दिनांक</th>
														<th> संदर्भ क्रमांक </th>
														<th>संदर्भ दिनांक </th>
														<th>विषय</th>
														<th>विभागाचे नाव</th>
														<th>केलेली कार्यवाही</th>
														<th> जावक क्रमांक </th>
														<th> जावक दिनांक </th>
													</tr>
													<tr>
														<td>".$rw['sender_name']."</td>
														<td>".$rw['ref_no']."</td>
														<td>".$refdt1."</td>
														<td>".$rw['ltr_inword_no']."</td>
														<td>".$recdt1."</td>
														<td>".$rw['subject']."</td>			
														<td>".$dptnm['dept_nm']."</td>
														<td>".$rw['action_taken']."</td>
														<td>".$rw['outword_no']."</td>
														<td>".$dispatchdt."</td>
													</tr>
													</table>
													</body>";
													
													
													
													
													//SEND Mail
													if (mail($mailto, $subject, "", $headers ))
													{
													
														//echo "<br>mail send ... OK"; // or use booleans here
														$sql2=("UPDATE tapal_to_dpt_transaction SET flag='true' WHERE dpt_ltr_id='$nuk'");
														if(mysql_query($sql2))
														{
															echo "<script type='text/javascript'>alert('Approved Successfully!')</script>";
															//echo '<script language="JavaScript"> window.location.href ="" </script>';
														}
														else
														{
															echo "<script type='text/javascript'>alert('Approve Unsuccessful!')</script>";
															//echo '<script language="JavaScript"> window.location.href ="" </script>';	
														}	
													} 	
													else 
													{
														//echo "<br>mail send ... ERROR!";
														echo "<script type='text/javascript'>alert('Approve Unsuccessful!')</script>";
														//echo '<script language="JavaScript"> window.location.href ="" </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 action6!')</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 action7!')</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 action8!')</script>";
						echo '<script language="JavaScript"> window.location.href ="../desk_action_perform.php?flag=n" </script>';
					}
					
				}
				
				/**********************************************
							Insert Query for dept name 
				***********************************************/
				else if($dispato=='department')
				{		
					convert_marathi();
					
					if($vargawariii=='ब')
							{
								//$futureDate=date('Y-m-d', strtotime('+30 year'));
								$now23 =date('Y')+30;
								$futureDate =$now23.'-'.date('m-d');
								
							}
							else if($vargawariii=='क')
							{
								$now23 =date('Y')+10;
								$futureDate =$now23.'-'.date('m-d');
							}
							else if($vargawariii=='क१')
							{
								$now23 =date('Y')+5;
								$futureDate =$now23.'-'.date('m-d');
							}
							else if($vargawariii=='ड')
							{
								$now23 =date('Y')+1;
								$futureDate =$now23.'-'.date('m-d');
							}
					
				/*	$qry2=("INSERT INTO dispatched_transaction(outword_no, desk_inward_no, dispatched_via, dispatched_to, dept_id_from, dept_id_to, addressed_to, email_id, address, ltr_id) VALUES('$ltrinwordno','$deskinward','$dispa_via','$dispato','$curdeptid','$deptid','$addressed_toother','$email','$address','$ltrid')") or die("".mysql_error());*/
				
					$qry2=("INSERT INTO dispatched_transaction(outword_no, dpt_inword_no , dispatched_via, dispatched_to, dept_id_from, dept_id_to, addressed_to, email_id, address, ltr_id) VALUES('$ltrinwordno','$dptltr_inward_no','$dispa_via','$dispato','$curdeptid','$deptid','$addressed_toother','$email','$address','$ltrid')");
					 if(mysql_query($qry2))
						{  	
							/*$qry3=("UPDATE desk_transaction SET dispatched_dt=curdate(),ltr_status='dispatched' ,desk_transaction.ltr_approval='transferred' WHERE desk_ltr_id=$id");*/
							
							$qry3=("UPDATE  tapal_to_dpt_transaction t
									SET t.dispatched_date=curdate(),t.nasti_nashta_dt='$futureDate',t.ltr_status='dispatched' ,t.status='transferred' 
									WHERE t.dpt_ltr_id=$id");
							
							$qry=("UPDATE tapal_to_dpt_transaction SET vargawari='$vargawariii' WHERE dpt_ltr_id='$id' ");
							mysql_query($qry) or die("axcdsw".mysql_error());
							if(mysql_query($qry3)) 
							{
								/*$qry23=("UPDATE incoming_letters_transaction, desk_transaction, tapal_to_dpt_transaction SET incoming_letters_transaction.dept_id='$deptid', tapal_to_dpt_transaction.status='approve', incoming_letters_transaction.status='pending' WHERE incoming_letters_transaction.ltr_id=tapal_to_dpt_transaction.ltr_id AND tapal_to_dpt_transaction.dpt_ltr_id=desk_transaction.dpt_ltr_id AND desk_transaction.desk_ltr_id='$id'") or die("".mysql_error());*/
								
							$qry23=mysql_query("UPDATE incoming_letters_transaction i, tapal_to_dpt_transaction t
										SET i.dept_id='$deptid', i.status='pending'
										WHERE i.ltr_id=t.ltr_id 
										AND t.dpt_ltr_id='$id' ") or die("asasdsda".mysql_error());
								
							//	mysql_query($qry23) or die("error".mysql_error());
							}
							else
							{
							/*	$qry3=("UPDATE desk_transaction SET dispatched_dt='',ltr_status='approve' ,desk_transaction.ltr_approval='' WHERE desk_ltr_id=$id");
								mysql_query($qry3) or die("error".mysql_error());*/
								
							/*$qry3=(" UPDATE tapal_to_dpt_transaction SET dispatched_date='',ltr_status='approve' ,status='' WHERE dpt_ltr_id=$id");
								mysql_query($qry3) or die("error".mysql_error());	*/
								
							$qry3=(" UPDATE tapal_to_dpt_transaction SET dispatched_date='',ltr_status='approve'  WHERE dpt_ltr_id=$id");
								mysql_query($qry3) or die("error".mysql_error());	
							}
					
							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>';
			
						}
				}

				/**********************************************
							Insert Query for Wait Register 
				***********************************************/
				else if($dispato=='wait')
				{
					$rwait = $_POST['rwait'];
					if($rwait=='SD')
					{
						$ddnm = $_POST['wait_dept'];
					}
					else if($rwait=='OD')
					{
						$ddnm = $_POST['wait_otherdept'];
					}
					// echo "Waiting ".$id." : ".$rwait." : ".$ddnm;

					date_default_timezone_set('Asia/Kolkata');
					$dt = date("Y-m-d");

					$q1 = mysql_query("UPDATE tapal_to_dpt_transaction SET waitflag='W',wait_kar_flag='N' WHERE dpt_ltr_id='$id'");
					if($q1)
					{
						
						$q2 = mysql_query("INSERT INTO wait_register(tbnm,iid,rwait,ddnm,dt,login_id)VALUES('N','$id','$rwait','$ddnm','$dt','$logid')");
						if($q2)
						{
							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
						{
							$q3 = mysql_query("UPDATE tapal_to_dpt_transaction SET waitflag='',wait_kar_flag='' WHERE dpt_ltr_id='$id'");

							echo "<script type='text/javascript'>alert('Error in Performing Action!')</script>";
							echo '<script language="JavaScript"> window.location.href ="../desk_action_perform.php?flag=n" </script>';
						}
					}
				}
				
			?>