<?php
include('dashboard_bdo.php');
require_once('convert_marathi.php');		
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Tapal Admin System</title>
<script type="text/javascript">  
  function MyAlert(x)  
    {  
		var k=x;
		document.getElementById(k).style.visibility = "visible";
	}
	
	function MyAlerthide(x)  
    {  
		var k=x;
		document.getElementById(k).style.visibility = "hidden";
	}
</script> 
</head>
<div class="rightpanel">
    <ul class="breadcrumbs">
        <li><a href="dashboard_tapal.php"><i class="iconfa-home"></i></a> <span class="separator"></span></li>
        <li><a href="dashboard_tapal.php">Tables</a> <span class="separator"></span></li>
        <li>जावक बारनिशी  </li>       
   </ul>
    <div class="pageheader"> 
       <div class="pageicon"><span class="iconfa-table"></span></div>
        <div class="pagetitle">
            <h5>Tables</h5>
            <h1>जावक बारनिशी  </h1>
        </div>
    </div><!--pageheader-->
    <div class="maincontent">
       <div class="maincontentinner">
			<h4 class="widgettitle">जावक बारनिशी  </h4> 
				<?php	
					convert_marathi();
					$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") or die("error".mysql_error());
					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);
							##############################################################################
													// if taken action is approved assign flag=true
							###############################################################################
							 if(isset($_POST['allocate_status' . $rw['desk_ltr_id']]))
								{ 	
									$path=$rw['scan_doc'];
									//echo "sssssssssss".$rw['email_id'];
									$str2 = substr($path, 1);
									//echo "path is".$str2;
									$aaaa = 'allocate_status'."".$nuk;
									$selectedoption=$_POST[$aaaa];
									if($selectedoption==('true'))
									{	
										$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 desk_transaction SET flag='true' WHERE desk_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 desk_transaction SET flag='true' WHERE desk_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($selectedoption==('true'))
								/***************************************************************
								// if taken action is not approved assign flag=false	
								*****************************************************************/
								if($selectedoption==('false'))
									{
										$bbbb = 'reason'."".$nuk;
										$resn=$_POST[$bbbb];
										$dt=$rw['dispatched_dt'];
										//echo "reason".$resn;
										$action=$rw['action_taken'];
										if($resn!='')
										{
											###################### Add Record In ltr_rejection Table #############
											$sql13=mysql_query("INSERT INTO  ltr_rejection(ltr_rec_dt, reason, ltr_sent_date, desk_ltr_id, performed_action) VALUES('$dt','$resn',curdate(),'$nuk','$action')");  
											######################## To get max no of ltr_rej_id ###############
											$sql14=mysql_query("select max(ltr_rej_id) as id from ltr_rejection");
											$row1 = mysql_fetch_array($sql14);
											$ltr_rej_id = $row1['id'];
											########################## Update Desk Transaction Table ############
											$sql2=("UPDATE desk_transaction SET flag='false',ltr_rej_id='$ltr_rej_id' WHERE desk_ltr_id='$nuk'") or die("ccccccccc".mysql_error());		
											if(mysql_query($sql2))
											   {
												echo "<script type='text/javascript'>alert('Returned Successfully!')</script>";
												echo '<script language="JavaScript"> window.location.href ="" </script>';	
											   }
											   else
											   {
												echo "<script type='text/javascript'>alert('Returned Unsuccessful!')</script>";
												echo '<script language="JavaScript"> window.location.href ="" </script>';	
											  }
										}
										else
										{
											echo "<script type='text/javascript'>alert('कृपया रिजेक्शनचे कारण टाका !')</script>";
											echo '<script language="JavaScript"> window.location.href ="" </script>';	
										}
									}///if($selectedoption==('false')
								} //if(isset($_POST['allocate_status' . $rw['desk_ltr_id']]))
						}//while?>
						
					<form action="bdo_javakbarnishi.php" method="post">
						<table id="dyntable" class="table table-bordered">
							<thead>
								<tr>
									<th class='head1 center'>आवक_क्र.<br>आवक_दिनांक</th>
									<th class='head0 center'>विभाग_आ_क्र.<br>आ. दि.</th>
									<th class='head1 center'>डेस्क_आ_क्र. <br>आ दि</th>
									<th class="center center">जावक_क्रमांक <br> जावक दि.</th>
									<th class='head0 center'>संदर्भ_क्रमांक <br>संदर्भ दि. </th>
									<th class='head1 center'>संदर्भाचा <br>प्रकार</th>
									<th class='head0 center'>विषय</th>
									<th class='head1 center'>कोणाकडून <br>प्राप्त_झाला</th>
									<th class='head0 center'>केलेली <br>कार्यवाही</th>
									<th class='head1 center'>View</th>
									<th class='head0 center'>पर्याय_निवडा </th>
								</tr>
							</thead>
							<tbody>	
								<?php	
									convert_marathi();
									$qry=mysql_query("SELECT * FROM  desk_transaction, incoming_letters_transaction, dispatched_transaction, tapal_to_dpt_transaction WHERE desk_transaction.ltr_status='dispatched'  AND 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.flag='' and dispatched_transaction.dispatched_to='permanent' )AND desk_transaction.desk_ltr_id=dispatched_transaction.desk_ltr_id AND dispatched_transaction.ltr_id=incoming_letters_transaction.ltr_id ORDER BY dispatched_transaction.dispatched_id DESC") or die("Error is".mysql_error());
									while($test = mysql_fetch_array($qry))
										{		
											$ltrinward=$test['ltr_inword_no'];
											$desk_ltr_rec_dt = date('d-m-Y',strtotime($test['desk_ltr_rec_dt']));
											$dept_rec_date 	= date('d-m-Y',strtotime($test['sent_dt']));
											$receive_dt	= date('d-m-Y',strtotime($test['receive_dt']));
											$refdt	= date('d-m-Y',strtotime($test['ref_dt']));
											$deskrec_dt	= date('d-m-Y',strtotime($test['sent_to_desk_dt']));
											$m=$test['ltr_type'];
											$deskid=$test['desk_id'];
											$desdte=date('d-m-Y',strtotime($test['dispatched_dt']));
											$dpt=$test['dept_id_from'];
											$dsk=$test['desk_id_from'];
											$deskltrid=$test['desk_ltr_id'];
											echo "</thead>
											<tbody>
											<tr class='gradeX'>
												<td class='center'>".$ltrinward."<br>".$receive_dt."</td>";
												##################### fetch department name ##############################
												$dept=mysql_query("Select dept_nm from department_master where dept_id='$dpt'");
												$dptnm=mysql_fetch_array($dept);
												echo "<td class='center'>".$dptnm[dept_nm]."/<br>".$test['dpt_inword_no']."<br>".$dept_rec_date ."</td>";
												############################## fetch desk name ###################################
												$desk=mysql_query("Select desk_nm from desk_master where desk_id='$dsk'");
												$dsknm=mysql_fetch_array($desk);
												echo "<td class='center'>".$dsknm['desk_nm']."/<br>".$test['desk_ltr_inword_no']."<br>".$deskrec_dt."</td>";
												echo "<td class='center'>".$test['outword_no']."<br>".$desdte." </td>";
												echo "<td class='center'>".$test['ref_no']."<br>".$refdt."</td>";
												############################## fetch sandharbh name ###############################
												$ltrtype=mysql_query("select sandharbh_nm FROM sandharbh_master WHERE sandhrbh_id='$m'");
												$s=mysql_fetch_array($ltrtype);
												echo "<td class='center'>".$s['sandharbh_nm']."</td>
												<td class='center'>".$test['subject']."</td>
												<td class='center'>".$test['sender_name']."</td>";
												echo "<td class='center'>".$test['action_taken']."</td>
												<td class='center'><a href='view_document.php?deskltrid=$deskltrid&no=1'>View</a></td>";
												################################ Selecting radio button ##########################
												echo "<td><div><input type='radio' name='allocate_status". $test['desk_ltr_id']."' id='status' value=true onclick='MyAlerthide($deskltrid)'>Ok<br><input type='radio' name='allocate_status". $test['desk_ltr_id']."' id='status' value=false onclick='MyAlert($deskltrid)'>Reject</div>
												<div id='$deskltrid' style=visibility:hidden;><textarea style='width:80px;' class=unicode lang=mr name='reason".$test['desk_ltr_id']."'></textarea></div></td>
											</tr>";
										}?>	
							</tbody>
						</table>
						<br>
						<p class="stdformbutton">
							<center>
								<button class="btn btn-primary">बदल करा</button>
                                <button type="reset" class="btn">रद्द करा </button>
							</center>
						</p>
						<br/><br/>
                <?php include_once('footer.php');?>
            </div><!--maincontentinner-->
        </div><!--maincontent-->
    </div><!--rightpanel-->
</div><!--mainwrapper-->
</body>
</html>
