<meta charset="UTF-8" />

<?php
include_once('db.php');
require_once('convert_marathi.php');

$username = $_REQUEST['login_id'];
$ltr_id = $_REQUEST['ltr_id'];
$ltrid = $ltr_id;
$id = $_REQUEST['desk_ltr_id'];


$dis_type = $_REQUEST['dis_type'];   		///// either 1-update or 2-dispatch



///// value of karyawahi select list ///////////////
$actn = $_REQUEST['actn'];  

if($actn=='other')
{
	$actiontaken = $_REQUEST['actiontaken'];
	convert_marathi();
	$qry=("UPDATE desk_transaction SET action_taken='$actiontaken' WHERE desk_ltr_id='$id'");
}
else
{
	convert_marathi();
	$qry=("UPDATE desk_transaction SET action_taken='$actn' WHERE desk_ltr_id='$id'");
	
	//echo "UPDATE desk_transaction SET action_taken='$actn' WHERE desk_ltr_id='$id'";
}




$qrynm=("Select * FROM incoming_letters_transaction,tapal_to_dpt_transaction,desk_transaction WHERE desk_transaction.desk_ltr_id='$id' AND tapal_to_dpt_transaction.ltr_id=incoming_letters_transaction.ltr_id AND tapal_to_dpt_transaction.dpt_ltr_id=desk_transaction.dpt_ltr_id ");
$rsnm=mysql_query($qrynm)or die("fhfhgh".mysql_error());
$rownm=mysql_fetch_array($rsnm);
		
$curdeptid=$rownm['dept_id'];						
$curdeskid=$rownm['desk_id'];

///////// same user /////////////
$addressed_to=$rownm['sender_name'];
$sameemail=$rownm['email_id'];
$sameaddress=$rownm['address'];


////////// other user ///////////
$addressed_toother=$_REQUEST['othername'];
$email=$_REQUEST['emailid'];
$address=$_REQUEST['add'];





	
//////////////// jatan prakar ////////////////////////
$ltrtype = 	$_REQUEST['ltrtype']; 		
	
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');
}
	





	
/////////////////////////////// update ///////////////////////
if($dis_type==1)
{
		
		convert_marathi();
		$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 "200";
		}
		else
		{   
			echo "300";
		}
}

//////////////////////////////////// dispatch ///////////////////////////////////
else if($dis_type==2)
{
	
	$dispato = $_REQUEST['dispatchmethod'];  // 1-arjdaar, 2-etar, 3-dept, 4-desk
	
	
	$dispa_via=$_REQUEST['dispatchtype'];    // email / post / both
		
	$qry1=("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.ltr_rej_id, de.update_date, de.dispatched_dt, de.ltr_approval,de.vargawari , 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, de.ltr_category, de.assign_no
	FROM desk_transaction de,incoming_letters_transaction i,tapal_to_dpt_transaction t 
	where de.desk_ltr_id='$id' 
	AND t.dpt_ltr_id=de.dpt_ltr_id 
	AND t.ltr_id=i.ltr_id");
	
	$rs1=mysql_query($qry1);
	$rw1=mysql_fetch_array($rs1);
	$deskinward=$rw1['desk_ltr_inword_no'];
	$ltrinwordno=$rw1['ltr_inword_no'];
	
	mysql_query($qry);
	
	
	if($dispato==1)
	{
		$dispato='permanent';// dept or permanent
		$usertype='same';// dept or permanent
		
		$qry2=("INSERT INTO dispatched_transaction (outword_no, desk_inward_no, dispatched_via, dispatched_to, dept_id_from, addressed_to, email_id, address,ltr_id, desk_ltr_id) VALUES('$ltrinwordno','$deskinward','$dispa_via','$dispato','$curdeptid','$addressed_to','$sameemail','$sameaddress','$ltrid','$id')");
				
		$qry3=("UPDATE desk_transaction de,tapal_to_dpt_transaction t SET t.desk_approval='dispatched', de.dispatched_dt=curdate(),de.nasti_nashta_dt='$futureDate', de.ltr_status='dispatched' 
		WHERE de.desk_ltr_id='$id'  
		AND t.dpt_ltr_id=de.dpt_ltr_id");
		
		$qry=mysql_query("UPDATE desk_transaction SET vargawari='$ltrtype' WHERE desk_ltr_id='$id' ");
		
		
		if(mysql_query($qry2))
		{  	
			mysql_query($qry3);
			
			if(mysql_query($qry3))
			{
				$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='$id' ");
								
				while($rw=mysql_fetch_array($qrye))	 
				{	
						$nuk=$rw['desk_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);
						
						$sameemail=$rw['email_id'];
						
						//////////////////////////   eMAIL SENDING ///////////////////////////////////////	
						require_once('../PHPMailer/class.phpmailer.php');
						require_once("../PHPMailer/class.smtp.php");
						require_once("../PHPMailer/class.pop3.php");
						
						
						$mailto=$rw['email_id'];
						$subject='संदर्भ क्र. '. $rw['ref_no'] .'  वर  केलेली कार्यवाही  ';
		
						$uid = md5(uniqid(time()));
						
									
									
						// header
						$header = "From:  पोलीस अधिक्षक कार्यालय, जि.रायगड <splendid@gains.nanodnscloud.com>\r\n";
						$header .= "MIME-Version: 1.0\r\n";
						$header .= "Content-type:text/html;charset=UTF-8" . "\r\n";
									
						$message='<body>
									<table border=1px>
										<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";
						
						//echo "<br>".$mailto."--".$subject."--".$headers;
						//SEND Mail
						if (mail($mailto, $subject, $nmessage, $header))
						{
							echo "200";
						}
						else
						{
							echo "300";
						}
						
			
				}
			}
			
		}
		
		
		
		
	}
	else if($dispato==2)
	{
		$dispato='permanent';// dept or permanent
		$usertype='other';// dept or permanent
				
		$qry2=("INSERT INTO dispatched_transaction(outword_no, desk_inward_no,dispatched_via ,dispatched_to, dept_id_from, addressed_to, email_id, address, ltr_id, desk_ltr_id) VALUES('$ltrinwordno','$deskinward','$dispa_via','$dispato','$curdeptid','$addressed_toother','$email','$address','$ltrid','$id')");
		
		
		$qry3=("UPDATE desk_transaction de,tapal_to_dpt_transaction t SET t.desk_approval='dispatched', de.dispatched_dt=curdate(), de.nasti_nashta_dt='$futureDate', de.ltr_status='dispatched' 
		WHERE de.desk_ltr_id='$id' 
		AND t.dpt_ltr_id=de.dpt_ltr_id");
		
		$qry=("UPDATE desk_transaction SET vargawari='$ltrtype' WHERE desk_ltr_id='$id'  ");
		mysql_query($qry) or die("axcdsw".mysql_error());
		
		
		if(mysql_query($qry2))
		{	
			mysql_query($qry3);
			
			if(mysql_query($qry3))
			{
				$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='$id' ");
					
				while($rw=mysql_fetch_array($qrye))	 
				{
						$nuk=$rw['desk_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);
						
						$sameemail=$rw['email_id'];
						
						//////////////////////////   eMAIL SENDING ///////////////////////////////////////	
						require_once('../PHPMailer/class.phpmailer.php');
						require_once("../PHPMailer/class.smtp.php");
						require_once("../PHPMailer/class.pop3.php");
						
						$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=1px><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 "200";
					}
					else
					{
						echo "300";
					}
													
													
													
								
								
				}
		
			}
		
		
		
		
		
		}
	}
	else if($dispato==3)
	{
		
		
		$dispato='department';// dept or permanent
		
		$deptid = $_REQUEST['deptid'];
		
		$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,desk_ltr_id) VALUES('$ltrinwordno','$deskinward','$dispa_via','$dispato','$curdeptid','$deptid','$addressed_toother','$email','$address','$ltrid','$id')");
		
		$qry=mysql_query("UPDATE desk_transaction SET vargawari='$ltrtype' WHERE desk_ltr_id='$id' ");
		
		if(mysql_query($qry2))
		{
			$qry3=("UPDATE desk_transaction SET dispatched_dt=curdate(),nasti_nashta_dt='$futureDate',ltr_status='dispatched' ,desk_transaction.ltr_approval='transferred' WHERE desk_ltr_id=$id");
			if(mysql_query($qry3)) 
			{
				$qry23=mysql_query("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'");
				
			}
			else
			{
				$qry3=mysql_query("UPDATE desk_transaction SET dispatched_dt='',ltr_status='approve' ,desk_transaction.ltr_approval='' WHERE desk_ltr_id=$id");
				
			}
			
			echo "200";
	
		}
		else
		{
			echo "300";
		}
		
		
		
		
	}
	else if($dispato==4)
	{
		$dispato='desk';// dept or permanent
		
		$deskid = $_REQUEST['deskid'];
		
		$qry2=("INSERT INTO dispatched_transaction(outword_no, desk_inward_no, dispatched_via, dispatched_to, dsk_id_from, dsk_id_to, addressed_to, email_id, address, ltr_id,desk_ltr_id) VALUES('$ltrinwordno','$deskinward','$dispa_via','$dispato','$curdeskid','$deskid','$addressed_toother','$email','$address','$ltrid','$id')");
		
		
		$qry=mysql_query("UPDATE desk_transaction SET vargawari='$ltrtype' WHERE desk_ltr_id='$id' ");
		
		if(mysql_query($qry2))
		{  	
			$qry3=("UPDATE desk_transaction SET dispatched_dt=curdate(),nasti_nashta_dt='$futureDate',ltr_status='dispatched' ,desk_transaction.ltr_approval='transferred_todesk' WHERE desk_ltr_id=$id");
			
			if(mysql_query($qry3)) 
			{
				$qry23=mysql_query("UPDATE incoming_letters_transaction i, desk_transaction d, tapal_to_dpt_transaction t 
				SET t.desk_id='$deskid', t.desk_approval='pending'
				WHERE i.ltr_id=t.ltr_id 
				AND t.dpt_ltr_id=d.dpt_ltr_id 
				AND d.desk_ltr_id='$id'");
				
			}
			else
			{
				$qry3=mysql_query("UPDATE desk_transaction SET dispatched_dt='',ltr_status='approve' ,desk_transaction.ltr_approval='' WHERE desk_ltr_id=$id");
			
			}
	
			echo "200";

		}
		else
		{   
			echo "300";

		}		
		
		
	}
	
	}
	



?>