<?php
$flag=$_REQUEST['flag'];
if($flag=='')
{
include_once('dashboard_department.php');
require_once('convert_marathi.php');
################################## Displaying All Accepted Letters By Department To Return ##########################
$username = $_SESSION['User_Name'];
?>
<!DOCTYPE html>
<html>
<head>
<?php convert_marathi();	//display data in marathi
	$qry1 =("SELECT * FROM incoming_letters_transaction i LEFT OUTER JOIN tapal_to_dpt_transaction t ON i.ltr_id = t.ltr_id  
	WHERE t.dept_id_from= (SELECT dept_id FROM login WHERE login_id ='$username') 
	AND i.status='approve' 
	and (t.desk_id='0' or t.desk_id='') 
	AND t.returned_choice='' 
	ORDER BY i.ltr_id DESC");	
	$rs1=mysql_query($qry1);
	while($row1=mysql_fetch_array($rs1))
		{?>
			<script type="text/javascript">
				$(document).ready(function(){
					var ltr="<?php echo $row1['dpt_ltr_id'];?>";
					$('#dpts'+ltr).fadeOut('fast');
					$('#othdpt'+ltr).change(function(){
						if(this.checked)
						{
							alert("ddddd");
							$('#dpts'+ltr).fadeIn('slow');
						}
						else
						{
							$('#dpts'+ltr).fadeIn('slow');
						}
					});

					$('#tpl'+ltr).change(function(){
					if(this.checked)
					{
						$('#dpts'+ltr).fadeIn('slow');
					}
					else
					{
						$('#dpts'+ltr).fadeOut('slow');
					}
					});

				})
			</script>
<?php 	} ?>  
</head>
<div class="rightpanel">
    <ul class="breadcrumbs">
        <li><a href="dashboard_department.php"><i class="iconfa-home"></i></a> <span class="separator"></span></li>
        <li><a href="dashboard_department.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	
			################################## Updating All Accepted Letters Returned By Department ##########################
			$qry1=("SELECT * FROM incoming_letters_transaction i,tapal_to_dpt_transaction t 
			WHERE i.ltr_id=t.ltr_id");
			$rs1=mysql_query($qry1)or die ("aaa".mysql_error());
			$cnt=mysql_num_rows($rs1);
			while($row1=mysql_fetch_array($rs1))
				{			
					$id=$row1['dpt_ltr_id'];
					$ltrinwardno=$row1['ltr_inword_no'];
					$sentdate= $row1['sent_dt'];
					$sndt = date('Y-m-d', strtotime($sentdate));
					$rtntodpt = $_POST['departmentnm' . $row1['dpt_ltr_id']];
					$dptid=$row1['dept_id_from'];
					if(isset($_POST['choice' . $row1['dpt_ltr_id']])) //if Return Choice 
						{	
							$rtnchoice = $_POST['choice' . $row1['dpt_ltr_id']];
							$rtntodsk = $_POST['desknm' . $rw['dpt_ltr_id']];
							convert_marathi();
							$qry1=mysql_query("INSERT INTO returned_from_dpt(ltr_inword_no, ltr_rec_dt, dpt_rtn_dt, rtn_choice, rtn_from, assign_to) VALUES('$ltrinwardno','$sndt',curdate(),'$rtnchoice','$dptid','$rtntodpt')");
							if($rtnchoice==('Tapal'))	//if Return Choice is Tapal
								{	
									$qry2 =("UPDATE incoming_letters_transaction i, tapal_to_dpt_transaction t SET i.dept_id='0', t.returned_choice='$rtnchoice', t.assign_dpt='$rtntodpt', i.status='closed' 
									WHERE t.dpt_ltr_id='$id' 
									AND i.ltr_id=i.ltr_id 
									AND i.dept_id=(Select dept_id from login where login_id='$username')
									AND i.dpt_inword_no=t.dpt_inword_no ")or die("aaa".mysql_error());
									
									$qry3=mysql_query("UPDATE tapal_to_dpt_transaction set status='closed' where dpt_ltr_id='$id'")or die("bbb".mysql_error());	
									if(mysql_query($qry2))
										{ 
											echo "<script type='text/javascript'>alert('Returned Successfully!')</script>";
											echo '<script language="JavaScript"> window.location.href ="dept_accepted_rtn_table.php" </script>';
										}	
										else
										{   
											echo "<script type='text/javascript'>alert('Returned Unsuccessful!')</script>";
											echo '<script language="JavaScript"> window.location.href ="dept_accepted_rtn_table.php" </script>';
										}
								}
							else if($rtnchoice==('Department')) //if Return Choice is department
								{	
									if( $rtntodpt!='')
									{
										$qry2 =("UPDATE incoming_letters_transaction i, tapal_to_dpt_transaction t SET i.dept_id='$rtntodpt', i.rtn_choice='$rtnchoice', i.status='pending' WHERE t.dpt_ltr_id='$id' 
										AND i.ltr_id=t.ltr_id ") or die("ccc".mysql_error());
									
										$qry6=mysql_query("UPDATE tapal_to_dpt_transaction SET returned_choice='$rtnchoice', assign_dpt='$rtntodpt', status='closed' WHERE dpt_ltr_id='$id'") or die("ddd".mysql_error());
										
										if(mysql_query($qry2))
										{ 
											echo "<script type='text/javascript'>alert('Returned Successfully!')</script>";
											echo '<script language="JavaScript"> window.location.href ="dept_accepted_rtn_table.php" </script>';
										}	
										else
										{   
											echo "<script type='text/javascript'>alert('Returned Unsuccessful!')</script>";
											echo '<script language="JavaScript"> window.location.href ="dept_accepted_rtn_table.php" </script>';
										}	
									}
									else
									{
											echo "<script type='text/javascript'>alert('कृपया पत्र परत करण्यासाठीची माहिती भरा!')</script>";
											echo '<script language="JavaScript"> window.location.href ="dept_accepted_rtn_table.php" </script>';
									}
									
								}//if Return Choice is department
							
						}//if Return Choice 
		        }//while
		?>
		<h4 class="widgettitle">स्वीकृत पत्र परत पाठवा</h4>
            <form action="dept_accepted_rtn_table.php" method="post">
				<table id="dyntable" class="table table-bordered">
                    <thead>
                        <tr>
                            <th class="head0 center">पोलीस अधिक्षक <br>कार्यालय<br>आवक_क्र. </th>
                            <th class="head1 center">संदर्भ क्र. /संदर्भ दिं.</th>
                            <th class="head0 center">विभागीय प्राप्त दिं.</th>
                            <th class="head1 center">कोणाकडून प्राप्त झाले  </th>
							<th class="head0 center">थोडक्यात विषय</th>
							<th class="head0 center">संदर्भाचा प्रकार</th>
                            <th class="head0 center">रिटर्नची माहिती भरा</th>
                        </tr>
                    </thead>
                    <tbody>
					<?php 
					convert_marathi();
					$qry1=("SELECT * FROM incoming_letters_transaction i LEFT OUTER JOIN tapal_to_dpt_transaction t ON i.ltr_id = t.ltr_id  
					WHERE t.dept_id_from= (SELECT dept_id FROM login WHERE login_id ='$username') 
					AND i.status='approve' 
					and (t.desk_id='0' or t.desk_id='') 
					AND t.returned_choice='' 
					ORDER BY i.ltr_id DESC");
					$rs1=mysql_query($qry1)or die ("aaa".mysql_error());
					$cnt=mysql_num_rows($rs1);
					while($row1=mysql_fetch_array($rs1))
					{	
						$num=$row1['dpt_ltr_id'];
						$refdate = strtotime($row1['ref_dt']);
						$ref= date('d-m-Y', $refdate);
						$sentdate = strtotime($row1['sent_dt']);
						$sdt= date('d-m-Y', $sentdate);
						?>
                        <tr class="gradeX unicode" lang="mr">
							<td class="center"><?php echo $row1['ltr_inword_no'];
							$id=$row1['ltr_inword_no'];?></td>
                            <td class="center"><?php echo $row1['ref_no']."<br>".$ref;?></td>
                            <td class="center"><?php echo $sdt;?></td>
                            <td class="center" id="wrapping"><?php echo $row1['sender_name'];?></td>
							<td class="center" id="wrapping"><?php echo $row1['subject'];?></td>
							<?php
								convert_marathi();
								$qry5=("Select * from sandharbh_master sm,incoming_letters_transaction i 
								WHERE sm.sandhrbh_id=i.ltr_type 
								AND i.ltr_inword_no='$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>
							<?php  	 
							echo "<td><div id='radiob". $row1['dpt_ltr_id']."'>
							<div id='tpl". $row1['dpt_ltr_id']."'><label><input type='radio' name='choice". $row1['dpt_ltr_id']."' id='status' value='Tapal'>टपाल</label></div>";
							
						/*	echo "<div id='othdpt". $row1['dpt_ltr_id'] ."'><label><input type='radio' name='choice". $row1['dpt_ltr_id']."' id='status' value='Department'> विभागास </label></div>
							</div>";							
							echo "<div id='dpts". $row1['dpt_ltr_id']."'><select name='departmentnm". $row1['dpt_ltr_id']."'>Dropdown</option><option value=> विभाग निवडा </option>"; 
							$qr=mysql_query("Select * from department_master where dept_id!=(select dept_id from login where login_id='$username')");
							while($row = mysql_fetch_array($qr))
								{
									echo"	{
												('<option id='".$row['dept_id']."' value='".$row['dept_id']."'>".$row['dept_nm']."</option>')
											}";
								}
				  			 
                            echo" </select></div>";*/
							echo "</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 />
     </form>   
 <?php   
  include_once('footer.php');
 ?>
 </div><!--maincontentinner-->
</div><!--maincontent-->   
</div><!--rightpanel-->   
</div><!--mainwrapper-->
</body>
</html>
<?php 
} //if($flag=='')

////////////////////////////////////////////////////

else if($flag=='n')
{
include_once('new_dashboard_department.php');
require_once('convert_marathi.php');
################################## Displaying All Accepted Letters By Department To Return ##########################
$username = $_SESSION['User_Name'];

?>
<!DOCTYPE html>
<html>
<head>
<?php convert_marathi();	//display data in marathi
	/*$qry1 =("SELECT * FROM incoming_letters_transaction,tapal_to_dpt_transaction WHERE incoming_letters_transaction.dept_id = (SELECT dept_id FROM login WHERE login_id ='$username') AND incoming_letters_transaction.status='approve' and (tapal_to_dpt_transaction.desk_id='0' or tapal_to_dpt_transaction.desk_id='') AND tapal_to_dpt_transaction.returned_choice='' AND incoming_letters_transaction.ltr_id=tapal_to_dpt_transaction.ltr_id ORDER BY incoming_letters_transaction.ltr_id DESC");*/
	$qry1 =("SELECT * FROM incoming_letters_transaction i LEFT OUTER JOIN tapal_to_dpt_transaction t ON i.ltr_id = t.ltr_id 
		WHERE i.dept_id = (SELECT dept_id FROM login WHERE login_id ='$username') 
		AND i.status='approve' 
		AND t.ltr_category=''
		AND t.returned_choice='' 
		ORDER BY i.ltr_id DESC");
	$rs1=mysql_query($qry1);
	while($row1=mysql_fetch_array($rs1))
		{?>
			<script type="text/javascript">
				$(document).ready(function(){
					var ltr="<?php echo $row1['dpt_ltr_id']; ?>";
					$('#dpts'+ltr).fadeOut('fast');
					$('#othdpt'+ltr).change(function(){
						if(this.checked)
						{
							alert("ddddd");
							$('#dpts'+ltr).fadeIn('slow');
						}
						else
						{
							$('#dpts'+ltr).fadeIn('slow');
						}
					});

					$('#tpl'+ltr).change(function(){
					if(this.checked)
					{
						$('#dpts'+ltr).fadeIn('slow');
					}
					else
					{
						$('#dpts'+ltr).fadeOut('slow');
					}
					});

				})
			</script>
<?php 	} ?>  
</head>
<div class="rightpanel">
    <ul class="breadcrumbs">
        <li><a href="new_dashboard_department.php"><i class="iconfa-home"></i></a> <span class="separator"></span></li>
        <li><a href="new_dashboard_department.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	
			################################## Updating All Accepted Letters Returned By Department ##########################
			$qry1=("SELECT * FROM incoming_letters_transaction i,tapal_to_dpt_transaction t 
			WHERE i.ltr_id=t.ltr_id");
			$rs1=mysql_query($qry1)or die ("aaa".mysql_error());
			$cnt=mysql_num_rows($rs1);
			while($row1=mysql_fetch_array($rs1))
				{			
					$id=$row1['dpt_ltr_id'];
					$ltrinwardno=$row1['ltr_inword_no'];
					$sentdate= $row1['sent_dt'];
					$sndt = date('Y-m-d', strtotime($sentdate));
					$rtntodpt = $_POST['departmentnm' . $row1['dpt_ltr_id']];
					$dptid=$row1['dept_id_from'];
					if(isset($_POST['choice' . $row1['dpt_ltr_id']])) //if Return Choice 
					{	
							$rtnchoice = $_POST['choice' . $row1['dpt_ltr_id']];
							//$rtntodsk = $_POST['desknm' . $rw['dpt_ltr_id']];
							convert_marathi();
							$qry1=mysql_query("INSERT INTO returned_from_dpt(ltr_inword_no, ltr_rec_dt, dpt_rtn_dt, rtn_choice, rtn_from, assign_to) VALUES('$ltrinwardno','$sndt',curdate(),'$rtnchoice','$dptid','$rtntodpt')")or die ("abcabc".mysql_error());
							if($rtnchoice==('Tapal'))	//if Return Choice is Tapal
								{	
									$qry2 =("UPDATE incoming_letters_transaction i, tapal_to_dpt_transaction t SET i.dept_id='0', t.returned_choice='$rtnchoice', t.assign_dpt='$rtntodpt', i.status='closed' 
									WHERE t.dpt_ltr_id='$id' 
									AND i.ltr_id=i.ltr_id 
									AND i.dept_id=(Select dept_id from login where login_id='$username')
									AND i.dpt_inword_no=t.dpt_inword_no")or die("aaabbb".mysql_error());
									
									$qry3=mysql_query("UPDATE tapal_to_dpt_transaction set status='closed' where dpt_ltr_id='$id'")or die("bbbaaa".mysql_error());	
									if(mysql_query($qry2))
										{ 
											echo "<script type='text/javascript'>alert('Returned Successfully!')</script>";
											echo '<script language="JavaScript"> window.location.href ="dept_accepted_rtn_table.php?flag=n" </script>';
										}	
										else
										{   
											echo "<script type='text/javascript'>alert('Returned Unsuccessful!')</script>";
											echo '<script language="JavaScript"> window.location.href ="dept_accepted_rtn_table.php?flag=n" </script>';
										}
								}
							else if($rtnchoice==('Department')) //if Return Choice is department
								{	
									if( $rtntodpt!='')
									{
										$qry2 =("UPDATE incoming_letters_transaction i, tapal_to_dpt_transaction t SET i.dept_id='$rtntodpt', i.rtn_choice='$rtnchoice', i.status='pending' WHERE t.dpt_ltr_id='$id' 
										AND i.ltr_id=t.ltr_id ") or die("ccczzz".mysql_error());
									
										$qry6=mysql_query("UPDATE tapal_to_dpt_transaction SET returned_choice='$rtnchoice', assign_dpt='$rtntodpt', status='closed' WHERE dpt_ltr_id='$id'") or die("ddd".mysql_error());
										
										if(mysql_query($qry2))
										{ 
											echo "<script type='text/javascript'>alert('Returned Successfully!')</script>";
											echo '<script language="JavaScript"> window.location.href ="dept_accepted_rtn_table.php?flag=n" </script>';
										}	
										else
										{   
											echo "<script type='text/javascript'>alert('Returned Unsuccessful!')</script>";
											echo '<script language="JavaScript"> window.location.href ="dept_accepted_rtn_table.php?flag=n" </script>';
										}	
									}
									else
									{
											echo "<script type='text/javascript'>alert('कृपया पत्र परत करण्यासाठीची माहिती भरा!')</script>";
											echo '<script language="JavaScript"> window.location.href ="dept_accepted_rtn_table.php?flag=n" </script>';
									}
									
								}//if Return Choice is department
							
						}//if Return Choice 
		        }//while
		?>
		<h4 class="widgettitle">स्वीकृत पत्र परत पाठवा</h4>
            <form action="dept_accepted_rtn_table.php?flag=n" method="post">
				<table id="dyntable" class="table table-bordered">
                    <thead>
                        <tr>
                            <th class="head0 center">पोलीस अधिक्षक <br>कार्यालय <br>आवक_क्र. </th>
                            <th class="head1 center">संदर्भ क्र. /संदर्भ दिं.</th>
                            <th class="head0 center">विभागीय प्राप्त दिं.</th>
                            <th class="head1 center">कोणाकडून प्राप्त झाले  </th>
							<th class="head0 center">थोडक्यात विषय</th>
							<th class="head0 center">संदर्भाचा प्रकार</th>
                            <th class="head0 center">रिटर्नची माहिती भरा</th>
                        </tr>
                    </thead>
                    <tbody>
					<?php 
					convert_marathi();
					/*$qry1=("SELECT * FROM incoming_letters_transaction,tapal_to_dpt_transaction WHERE incoming_letters_transaction.dept_id = (SELECT dept_id FROM login WHERE login_id ='$username') AND incoming_letters_transaction.status='approve' and (tapal_to_dpt_transaction.desk_id='0' or tapal_to_dpt_transaction.desk_id='') AND tapal_to_dpt_transaction.returned_choice='' AND incoming_letters_transaction.ltr_id=tapal_to_dpt_transaction.ltr_id ORDER BY incoming_letters_transaction.ltr_id DESC");*/
					$qry1 =("SELECT * FROM incoming_letters_transaction i LEFT OUTER JOIN tapal_to_dpt_transaction t ON i.ltr_id = t.ltr_id 
					WHERE i.dept_id = (SELECT dept_id FROM login WHERE login_id ='$username') 
					AND i.status='approve' 
					AND t.ltr_category=''
					AND t.returned_choice='' 
					ORDER BY i.ltr_id DESC");
					$rs1=mysql_query($qry1)or die ("aaa".mysql_error());
					$cnt=mysql_num_rows($rs1);
					while($row1=mysql_fetch_array($rs1))
					{	
						$num=$row1['dpt_ltr_id'];
						$refdate = strtotime($row1['ref_dt']);
						$ref= date('d-m-Y', $refdate);
						$sentdate = strtotime($row1['sent_dt']);
						$sdt= date('d-m-Y', $sentdate);
						?>
                        <tr class="gradeX unicode" lang="mr">
							<td class="center"><?php echo $row1['ltr_inword_no'];
							$id=$row1['ltr_inword_no'];?></td>
                            <td class="center"><?php echo $row1['ref_no']."<br>".$ref;?></td>
                            <td class="center"><?php echo $sdt;?></td>
                            <td class="center" id="wrapping"><?php echo $row1['sender_name'];?></td>
							<td class="center" id="wrapping"><?php echo $row1['subject'];?></td>
							<?php
								convert_marathi();
								$qry5=("Select * from sandharbh_master sm,incoming_letters_transaction i 
								WHERE sm.sandhrbh_id=i.ltr_type 
								AND i.ltr_inword_no='$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>
							<?php  	 
							echo "<td><div id='radiob". $row1['dpt_ltr_id']."'>
							<div id='tpl". $row1['dpt_ltr_id']."'><label><input type='radio' name='choice". $row1['dpt_ltr_id']."' id='status' value='Tapal'>टपाल</label></div>";
							
						/*	echo "<div id='othdpt". $row1['dpt_ltr_id'] ."'><label><input type='radio' name='choice". $row1['dpt_ltr_id']."' id='status' value='Department'> विभागास </label></div>
							</div>";						
							echo "<div id='dpts". $row1['dpt_ltr_id']."'><select name='departmentnm". $row1['dpt_ltr_id']."'>Dropdown</option><option value=> विभाग निवडा </option>"; 
							$qr=mysql_query("Select * from department_master where dept_id!=(select dept_id from login where login_id='$username')");
							while($row = mysql_fetch_array($qr))
								{
									echo"	{
												('<option id='".$row['dept_id']."' value='".$row['dept_id']."'>".$row['dept_nm']."</option>')
											}";
								}
				  			 
                            echo" </select></div>"; */	
							echo "</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 />
     </form>   
 <?php   
  include_once('footer.php');
 ?>
 </div><!--maincontentinner-->
</div><!--maincontent-->   
</div><!--rightpanel-->   
</div><!--mainwrapper-->
</body>
</html>
<?php 
} //else if($flag=='n')
?>