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

$username = $_SESSION['User_Name'];

?>
<!DOCTYPE html>
<html>
<head>
<link type="text/css" rel="stylesheet" href="style2.css" />
<script type="text/javascript" src="js/adduser.js"></script> 
<script type="text/javascript">
<?php
	$qry1=("SELECT * FROM tapal_to_dpt_transaction t, incoming_letters_transaction i
	WHERE t.desk_id=(SELECT desk_id FROM login WHERE login_id='$username') 
	AND ( t.desk_approval='pending'  )
	AND  t.ltr_id=i.ltr_id 
	ORDER BY i.ltr_id DESC");
	
	$rs1=mysql_query($qry1)or die ("aaa".mysql_error());
	$count=mysql_num_rows($rs1);
	while($row1=mysql_fetch_array($rs1))
	{ ?>
		$(document).ready(function(){
			var ltr="<?php echo $row1['dpt_ltr_id'];?>";
			$('#radiob'+ltr).fadeOut('fast');
			$('#dptlist'+ltr).fadeOut('fast');
			
			$('#checkbox2'+ltr).change(function(){
				if($(this).is(':checked')){
					$('#checkbox1'+ltr).attr('disabled',true);
					$('#radiob'+ltr).fadeIn('slow');
				}
				else
				{
					$('#radiob'+ltr).fadeOut('slow');
					$('#dptlist'+ltr).fadeOut('slow');
					$('#checkbox1'+ltr).attr('disabled',false);
				}
			});
			$('#otherdpt'+ltr).change(function(){
				if(this.checked)
					{
						$('#dptlist'+ltr).fadeOut('slow');
					}
				else
					{
						$('#dptlist'+ltr).fadeIn('slow');
					}
			});

			$('#owndpt'+ltr).change(function(){
				if(this.checked)
				{
					$('#dptlist'+ltr).fadeIn('slow');
				}
				else
				{
					$('#dptlist'+ltr).fadeOut('slow');
				}
			});
			$('#checkbox1'+ltr).change(function(){
				if(this.checked)
					{
						var chkb = document.getElementById('checkbox2'+ltr);
						chkb.checked = false;
						$('#radiob'+ltr).fadeOut('slow');
						$('#dptlist'+ltr).fadeOut('slow');
						$('#checkbox2'+ltr).attr('disabled',true);
					}
				else
					{
						$('#checkbox2'+ltr).attr('disabled',false);
					}
			});
		})
<?php 
	}
?>
</script>

 
</head>

<body onload="fun()">
    <div class="rightpanel">
        <ul class="breadcrumbs">
            <li><a href="dashboard_desk.php"><i class="iconfa-home"></i></a> <span class="separator"></span></li>
            <li><a href="dashboard_desk.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">
			
			 	<?php	
					convert_marathi();
					/*$qrye=("SELECT * FROM tapal_to_dpt_transaction,incoming_letters_transaction  WHERE  tapal_to_dpt_transaction.desk_id=(SELECT desk_id FROM login WHERE login_id='$username') AND tapal_to_dpt_transaction.desk_id!='0' AND tapal_to_dpt_transaction.desk_approval='pending' AND (tapal_to_dpt_transaction.desk_ltr_inword_no='' OR tapal_to_dpt_transaction.desk_ltr_inword_no='0') AND  incoming_letters_transaction.ltr_id=tapal_to_dpt_transaction.ltr_id ORDER BY incoming_letters_transaction.ltr_id DESC ") or die("abc".mysql_error());*/
					
					$qrye=mysql_query("SELECT * FROM tapal_to_dpt_transaction t, incoming_letters_transaction i
					WHERE t.desk_id=(SELECT desk_id FROM login WHERE login_id='$username') 
					AND ( t.desk_approval='pending'  )
					AND  t.ltr_id=i.ltr_id 
					ORDER BY i.ltr_id DESC ") ;	
					
					while($rw=mysql_fetch_array($qrye))	 
					{  
						$year = date('y');
						$dptltrid=$rw['dpt_ltr_id'];
					
						################################## Generating Desk Letter Inward No ##############################
						$r = mysql_query("select max(desk_transaction.desk_ltr_inword_no) as id from tapal_to_dpt_transaction,desk_transaction where  desk_transaction.desk_ltr_inword_no like '$year%' and desk_transaction.desk_id_from=(SELECT desk_id from login where login_id = '$username') AND tapal_to_dpt_transaction.dpt_ltr_id=desk_transaction.dpt_ltr_id") ;
						
						$row1 = mysql_fetch_array($r) or die("<br>fffffff".mysql_error());
						$Auto_increment = $row1['id'];
						$start='0001';
						if ($Auto_increment != '')
						{
							list($a,$b) = Explode("-",$Auto_increment);
							if ($a==$year)
							{
								$c= str_pad($b + 1, 4, 0, STR_PAD_LEFT);
								$n = $year ."-". $c;
							}
							else
							{
								$c= str_pad($b + 1, 4, 0, STR_PAD_LEFT);
								//echo " C is ".$c;
								$n =  $year."-". $c;
							}
						}
						else
						{
						      $n =$year."-". $start;
						}
						################################## Generating Desk Letter Inward No ##############################
							
						// fetching sent date from incoming ltr tran and save it as received dt in tapl to dpt trn
							$dt=$rw['sent_dt'];
						// fetching sent date from incoming ltr tran and save it as received dt in tapl to dpt trn
							$dt=$rw['sent_to_desk_dt'];
						$deskid=$rw['desk_id'];
						if(isset($_POST['allocate_status' . $rw['ltr_inword_no']]))
							{
								$selectedoption = $_POST['allocate_status' . $rw['ltr_inword_no']];
								##################### If Letter Approve #############################
								if($selectedoption=='approve')
									{	
										$sql1 = ("INSERT into desk_transaction (desk_ltr_inword_no, desk_ltr_rec_dt, desk_apprv_dt ,ltr_approval, dpt_ltr_id,desk_id_from) VALUES('$n','$dt',curdate(), 'Approve','$dptltrid','$deskid')");
										if(mysql_query($sql1))
										{
										$sql2=("UPDATE tapal_to_dpt_transaction SET desk_approval='approve',desk_ltr_inword_no='$n' WHERE ltr_id='$rw[ltr_id]'
										 AND tapal_to_dpt_transaction.ltr_category='' ") or die("error in $sql12".mysql_error());
											if(mysql_query($sql2))
											{
												echo "<script type='text/javascript'>alert('Approved Successfully!')</script>";
												echo '<script language="JavaScript"> window.location.href ="deskacceptletter.php" </script>';		
											}
											else
											{
												echo "<script type='text/javascript'>alert('Approve Unsuccessful!')</script>";
												echo '<script language="JavaScript"> window.location.href ="deskacceptletter.php" </script>';	
											}
										}
										else
										{
											echo "<script type='text/javascript'>alert('Unable to insert record!')</script>";
											echo '<script language="JavaScript"> window.location.href ="deskacceptletter.php" </script>';
										}
									}
								##################### End Of If Letter Approve #############################

								##################### If Letter Return ######################										
								else if($selectedoption=='return')
								{	

									$category=$_POST['choice'];
									$ltrid=$rw['ltr_id'];
									$dptltrid=$rw['dpt_ltr_id'];
									$ltrinwardno=$rw['ltr_inword_no'];	
									$sentdate=$rw['sent_to_desk_dt'];	
									$rtntodsk = $_POST['desktnm' . $rw['dpt_ltr_id']];
									//echo $rtntodsk."--".$category;
									$sndt = date('Y-m-d', strtotime($sentdate));
									$us=$rw['dept_id'];
									$dptin=$rw['dpt_inword_no'];
									if($category=='owndpt' || ( $rtntodsk!='' && $category=='otherdpt'))
									{
										convert_marathi();
										$qry1=("INSERT INTO returned_from_desk(ltr_inword_no, dpt_inword_no, rec_dt, returned_dt, rtn_choice, returned_from, rtn_to_dsk) VALUES('$ltrinwardno','$dptin','$sndt',curdate(),'$category','$deskid','$rtntodsk')");
										
										if(mysql_query($qry1))
										{ 
											if($category=='owndpt')
											{	
												$qry22=mysql_query("UPDATE tapal_to_dpt_transaction set desk_approval='returned' , desk_id='$rtntodsk', desk_approval='returned' where dpt_ltr_id='$dptltrid'");
											}
											else if($category=='otherdpt')
											{
												$qry3=mysql_query("UPDATE tapal_to_dpt_transaction SET desk_id='$rtntodsk', desk_approval='pending', status='returned' WHERE dpt_ltr_id='$dptltrid'");	
											}
											echo "<script type='text/javascript'>alert('Returned Successfully!')</script>";
											echo '<script language="JavaScript"> window.location.href ="deskacceptletter.php" </script>';
										}	
										else
										{   
											echo "<script type='text/javascript'>alert('Returned Unsuccessful!')</script>";
											echo '<script language="JavaScript"> window.location.href ="deskacceptletter.php" </script>';
										}
									}//if($category!='' && $rtntodsk!='')
									else
									{
										echo "<script type='text/javascript'>alert('कृपया पत्र परत करण्यासाठीची माहिती भरा!..')</script>";
										echo '<script language="JavaScript"> window.location.href ="deskacceptletter.php" </script>';
									}
								}//else if($selectedoption=='return')
												
							}//if(isset($_POST['allocate_status' . $rw['ltr_inword_no']]))
									##################### End Of If Letter Return ######################	
					}?>
			<!--#################################### Displaying Letters TO Approve Or Return ###################-->
               
			   
			   
			<!--############################### POPUP CODE ###################################-->

							<div id="overlay"></div>
							<div id="popup" style=" width:600px;overflow-y:scroll;overflow-x:scroll;">
								<div class="popupcontrols popupHeader" >
									<span class="header_title"> अलर्ट </span>
									<span id="popupclose">X</span>
								</div>
								<div class="popupcontent">
									<div id="parent">
										
										<center>	
										<table style="width:450px;">
                        <?php 
						
						convert_marathi();
						//not accepted by desk
						$result2221 = mysql_query("SELECT d.desk_ltr_id, d.desk_id_from, d.action_taken, d.desk_ltr_rec_dt, d.desk_ltr_inword_no, 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, d. update_date, d.dispatched_dt, d.ltr_approval, di.addressed_to, d.ltr_status, i.sender_name, i.ltr_id, i.ltr_inword_no,i.priority_days, i.receive_dt, i.ref_dt, i.ref_no, i.dept_id, i.document, d.scan_doc, di.outword_no, di.dispatched_to, d.ltr_category, d.assign_no 
						FROM  incoming_letters_transaction i LEFT OUTER JOIN  tapal_to_dpt_transaction t ON i.ltr_id = t.ltr_id 
						LEFT OUTER JOIN desk_transaction d ON d.dpt_ltr_id = t.dpt_ltr_id 
						LEFT OUTER JOIN dispatched_transaction di ON di.desk_ltr_id = d.desk_ltr_id 
						WHERE t.desk_approval='pending'
						AND t.desk_id = ( SELECT desk_id from login where login_id = '$username' )") or die("bbbb".mysql_error());
						
						$n1=mysql_num_rows($result2221);
						
						convert_marathi();
						//accepted by desk
						$result222 = mysql_query("SELECT d.desk_ltr_id, d.desk_id_from, d.action_taken, d.desk_ltr_rec_dt, d.desk_ltr_inword_no, 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, d. update_date, d.dispatched_dt, d.ltr_approval, di.addressed_to, d.ltr_status, i.sender_name, i.ltr_id, i.ltr_inword_no, i.priority_days, i.receive_dt, i.ref_dt, i.ref_no, i.dept_id, i.document, d.scan_doc, di.outword_no, di.dispatched_to, d.ltr_category, d.assign_no
						FROM incoming_letters_transaction i LEFT OUTER JOIN tapal_to_dpt_transaction t ON i.ltr_id = t.ltr_id LEFT OUTER JOIN desk_transaction d ON d.dpt_ltr_id = t.dpt_ltr_id LEFT OUTER JOIN dispatched_transaction di ON di.desk_ltr_id = d.desk_ltr_id 
						WHERE d.desk_id_from=(select desk_id from login where login_id='$username') 
						AND d.ltr_status = ''") or die("".mysql_error());
						
						$n2=mysql_num_rows($result222);
						
						if($n1!=0 || $n2!=0)
						{
								
								echo "<tr><td> &nbsp&nbsp&nbsp&nbsp <b> आवक_क्र. </b></td>
									<td>&nbsp&nbsp&nbsp&nbsp <b> संदर्भ_क्र. </b></td>
									<td>&nbsp&nbsp&nbsp&nbsp <b> विषय </b></td>
									<td>&nbsp&nbsp&nbsp&nbsp <b> पत्राची स्थिती </b></td>
									<td>&nbsp&nbsp&nbsp&nbsp <b> मुदत </b></td>
									</tr>
									<tr><td colspan=3>&nbsp;</td></tr>";
								
								
								while($row211=mysql_fetch_array($result2221))
								{ 
									$priority_days=$row211['priority_days'];
									$receive_dt	=strtotime($row211['receive_dt']);
									
									$to_dt=strtotime("+".$priority_days." days",$receive_dt);
									//echo date("d-m-Y",$to_dt)."aa";
									
									$frm_dt=strtotime("-2 days",$to_dt);
									
									if(strtotime($date123)>=$frm_dt && strtotime($date123)<=$to_dt)
									{
										echo "<tr style=height:20px;><td>&nbsp&nbsp&nbsp&nbsp".$row211['ltr_inword_no']."</td>
										</td><td>&nbsp&nbsp&nbsp&nbsp".$row211['ref_no']."</td>
										<td>&nbsp&nbsp&nbsp&nbsp".$row211['subject']."</td>
										<td>&nbsp&nbsp&nbsp&nbsp डेस्कने स्वीकारणे बाकी </td>
										<td> &nbsp&nbsp&nbsp&nbsp ".date('d-m-Y',$to_dt)." </td>
										</tr>";
									}
									
								//echo date("d-m-Y",$frm_dt)."<br>";
								
									
								}
								
								
								
								while($row21=mysql_fetch_array($result222))
								{ 
									$priority_days=$row21['priority_days'];
									$receive_dt	=strtotime($row21['receive_dt']);

									$to_dt=strtotime("+".$priority_days." days",$receive_dt);
									//echo date("d-m-Y",$to_dt)."aa";
									
									$frm_dt=strtotime("-2 days",$to_dt);
									if(strtotime($date123)>=$frm_dt && strtotime($date123)<=$to_dt)
									{
										echo "<tr style=height:20px;><td>&nbsp&nbsp&nbsp&nbsp".$row21['ltr_inword_no']."</td>
										</td><td>&nbsp&nbsp&nbsp&nbsp".$row21['ref_no']."</td>
										<td>&nbsp&nbsp&nbsp&nbsp".$row21['subject']."</td>
										<td>&nbsp&nbsp&nbsp&nbsp डेस्क कार्यवाही करणे बाकी </td>
										<td> &nbsp&nbsp&nbsp&nbsp ".date('d-m-Y',$to_dt)." </td>
										</tr>";
									}
									
								//echo date("d-m-Y",$frm_dt)."<br>";
								
									
								}
						}
						else
						{
							echo "<h3> No records found....</h3>";
						}
						
						
						?>
						</table>
									</center>
									
									</div>
								</div>
							</div>

			<!--##################################################################-->		
							
							
			   <h4 class="widgettitle">पत्र स्वीकार करा </h4>
                <form action="deskacceptletter.php" method="post" name="deskaccept">
					<table id="dyntable" class="table table-bordered">
						<thead>
							<tr>
								<th class="head0 center">पोलीस अधिक्षक <br> कार्यालय <br>आवक_क्रमांक  <br> आवक_दिनांक</th>
								<th class="head1 center">संदर्भ_क्रमांक <br>संदर्भ_दिनांक.</th>
								<th class="head0 center">विभाग आवक_क्रमांक <br> आवक_दिनांक</th>
								<th class="head1 center">डेस्कला <br> प्राप्त_दिनांक </th>
								<th class="head0 center">कोणाकडून <br>प्राप्त झाले  </th>
								<th class="head1 center">थोडक्यात <br>विषय</th>
								<th class="head0 center">संदर्भाचा प्रकार</th>
								<th class="head1 center">View Document</th>
								<th class="head0 center">स्वीकृत करा</th>
								<th class="head1 center">परत करा</th>
							</tr>
						</thead>
						<tbody>
							<?php 
							convert_marathi();
							$qry1=("SELECT * FROM tapal_to_dpt_transaction t, incoming_letters_transaction i
									WHERE t.desk_id=(SELECT desk_id FROM login WHERE login_id='$username') 
									AND ( t.desk_approval='pending'  )
									AND  t.ltr_id=i.ltr_id 
									ORDER BY i.ltr_id DESC");
			
							$rs1=mysql_query($qry1)or die ("aaa".mysql_error());
							$count=mysql_num_rows($rs1);
							while($row1=mysql_fetch_array($rs1))
							{	
								$recdate = strtotime($row1['receive_dt']);
								$rec= date('d-m-Y', $recdate);
								$refdate = strtotime($row1['ref_dt']);
								$ref= date('d-m-Y', $refdate);
								//sent_to_desk_dt
								$sentdate = strtotime($row1['sent_to_desk_dt']);
								$sdt= date('d-m-Y', $sentdate);
								//sent_dt
								$sentdt = strtotime($row1['sent_dt']);
								$sndt= date('d-m-Y', $sentdt);
								$id=$row1['ltr_id'];
								$deptid=$row1['dept_id'];
								?>
								<tr class="gradeX unicode" lang="mr">
									<td class="center"><?php echo $row1['ltr_inword_no'];?><br><?php echo $rec;?></td>
									<td class="center"><?php echo $row1['ref_no'];?><br><?php echo $ref;?></td>
									<td class="center"><?php echo $row1['dpt_inword_no'];?><br><?php echo $sndt;?></td>
									<td class="center"><?php echo $sdt;?></td>
									<td class="center" ><?php echo $row1['sender_name'];?></td>
									<td class="center"><?php echo $row1['subject'];?></td>
									<?php
									convert_marathi();
									$qry5=("Select * from sandharbh_master,incoming_letters_transaction WHERE sandharbh_master.sandhrbh_id=incoming_letters_transaction.ltr_type AND incoming_letters_transaction.ltr_id='$id'");
									$rs5=mysql_query($qry5) or die ("mmmm". mysql_error());
									while($rw5=mysql_fetch_array($rs5))
									{
										$ltr=$rw5['sandharbh_nm'];
									}
									?>
									<td class="center"><?php echo $ltr; ?></td>
									<td class="center"><a href="view_avak.php?user=2&ltrid=<?php echo $row1['ltr_id'];?>">View</a></td>		
									<td class="center">
									<?php  
									echo" <label style='width:100px;'><input type=checkbox name='allocate_status". $row1['ltr_inword_no']."' value=approve id='checkbox1". $row1['dpt_ltr_id']."'> 
									स्वीकृत करा</label>
									</td>
									<td>
									<label style='width:100px;'>
									<input type='checkbox' name='allocate_status". $row1['ltr_inword_no']."' value='return' id='checkbox2". $row1['dpt_ltr_id']."'> परत करा</label>"; 
									echo "<div id='radiob". $row1['dpt_ltr_id']."'>
									<div id='owndpt'><label><input type=radio name='choice' id='status' value='owndpt'>स्व-विभागास </label></div>
									<div id='otherdpt". $row1['dpt_ltr_id']."'><label><input type=radio name='choice' value='otherdpt'> इतर डेस्कला </label></div></div>";							
									echo "<br><div id='dptlist". $row1['dpt_ltr_id']."' style=float:right;><select name='desktnm".$row1['dpt_ltr_id']."'>Dropdown</option><option value=>डेस्क निवडा </option>";
									$qr=mysql_query("Select * from desk_master WHERE desk_id!=(select desk_id From login WHERE login_id='$username') AND dept_id='$deptid'");
										while($row = mysql_fetch_array($qr))
											{
												echo "'<option id='".$row['desk_id']."' value='".$row['desk_id']."'>".$row['desk_nm']."</option>'";
											}
											echo "</select></div>";
											
				  			 
									echo" </td></tr>";
							}?>
						</tbody>
					</table><br>
					<!--#################################### End OF Displaying Letters TO Approve Or Return ###################-->
					<p class="stdformbutton">
                        <center> 
							<button class="btn btn-primary">बदल करा </button>
							<button type="reset" class="btn">रद्द करा </button>
						</center>
                    </p>
					
					
					
					
                <br /><br />
				
				
				  
               </form> 
			   
			 <script>

		function fun()
		{

			// Initialize Variables
			var closePopup = document.getElementById("popupclose");
			
			var overlay = document.getElementById("overlay");
			var popup = document.getElementById("popup");
			var button = document.getElementById("button");
		   
			// Show Overlay and Popup

				overlay.style.display = 'block';
				popup.style.display = 'block';

		}

		


		  // Initialize Variables
			var closePopup = document.getElementById("popupclose");
			var overlay = document.getElementById("overlay");
			var popup = document.getElementById("popup");
			var button = document.getElementById("button");
			// Close Popup Event
		   
			   // Close Popup Event
			closePopup.onclick = function() {
				overlay.style.display = 'none';
				popup.style.display = 'none';
			};

		</script>	  
			   
			
             <?php   
             include_once('footer.php');
            ?>
            </div><!--maincontentinner-->
        </div><!--maincontent-->
    </div><!--rightpanel-->
</div><!--mainwrapper-->
</body>
</html>


