<?php
include_once('dashboard_tapal.php');
require_once('convert_marathi.php');
$username = $_SESSION['User_Name'];
?>
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="js/adduser.js"></script> 
</head>
<body>
<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">
				<?php 
					convert_marathi();
					$qrye=("SELECT * FROM incoming_letters_transaction WHERE dept_id = (SELECT dept_id FROM login WHERE login_id ='$username') 
					AND dept_id='0' 
					AND status!='approve' 
					ORDER BY incoming_letters_transaction.ltr_id DESC");
					$rsult=mysql_query($qrye) or die("".mysql_error());
					while($rw=mysql_fetch_array($rsult))
					 {	
						$refno=$rw['ref_no'];
						$refdt =$rw['ref_dt'];
						$recdt=$rw['receive_dt'];
						$sendernm=$rw['sender_name'];
						$mobno=$rw['mobile_no'];
						$email=$rw['email_id'];
						$add=$rw['address'];
						$sandh=$rw['ltr_type'];
						$sub=$rw['subject'];
						$prio=$rw['	priority_days'];
						$path=$rw['document'];
						if(isset($_POST['departmentnm' . $rw['ltr_inword_no']]))
							{
								$selectedDepertmentNumber = $_POST['departmentnm' . $rw['ltr_inword_no']];
								#########################################################
															#if Assgin To all Department
								#########################################################
								if($selectedDepertmentNumber=='222')//if Assgin To all Department
									{	
										if($selectedDepertmentNumber!="Select Department") //if department name is selected
											{
												$qryrs=mysql_query("Select * from department_master");
												$cnt=1;
												while($dept=mysql_fetch_array($qryrs))
													{	
														$deptid=$dept['dept_id'];
														if($cnt==1)
															{
																$sql1 = mysql_query("UPDATE incoming_letters_transaction SET dept_id = '$deptid', sent_dt=curdate(),status='pending' WHERE ltr_inword_no = '$rw[ltr_inword_no]'");
																$cnt=$cnt+1;
															}
														else if($cnt!=1)
															{
																############################################################
																	# Generating Inward no and adding record no of times that is equal to no of departments available in dept_master
																#############################################################
																$year = date('y');
																//echo $year;
																$r = mysql_query("SELECT MAX(ltr_inword_no)AS id FROM incoming_letters_transaction WHERE ltr_inword_no like '$year%'");
																$row1 = mysql_fetch_array($r);
																$Auto_increment = $row1['id'];
																$start='000001';
																if ($Auto_increment != '')
																	{
																		list($a,$b) = Explode("-",$Auto_increment);
																		if ($a==$year)
																		{
																			$c= str_pad($b + 1, 6, 0, STR_PAD_LEFT);
																			$n = $year ."-". $c;
																		}
																		else
																		{
																			$c= str_pad($b + 1, 6, 0, STR_PAD_LEFT);
																			$n =  $year."-". $c;
																		}
																	}
																else
																	{
																		$n =$year."-". $start;
																	}
																$padtalni=mt_rand();
																$qry13=mysql_query("insert into incoming_letters_transaction(ltr_inword_no, ref_no, ref_dt, receive_dt, sender_name, mobile_no, email_id, address, ltr_type, subject,priority_days, document,status,sent_dt,dept_id,padtalni_no,first_flag) values('$n','$refno','$refdt' ,'$recdt','$sendernm','$mobno','$email','$add','$sandh','$sub','$prio','$path', 'pending',curdate(),'$deptid','$padtalni','Y')") or die("<br>Error".mysql_error());
															}//if cnt
													}//while
											}//if select department
									}//if select dept==222
										################## End Of If Assign To All Department Coding #########################
										
										########################################################################################
															// if assigned to single department
										########################################################################################
									else if($selectedDepertmentNumber!='222')	
									{
										$selectedDepertmentNumber = $_POST['departmentnm' . $rw['ltr_inword_no']];
										if($selectedDepertmentNumber!="Select Department")
											{
												$sql1 = ("UPDATE incoming_letters_transaction SET dept_id = '$selectedDepertmentNumber', sent_dt=curdate(),status='pending' WHERE ltr_inword_no = '$rw[ltr_inword_no]'");
												if(mysql_query($sql1))
													{
														echo "<script type='text/javascript'>alert('Assigned Department Successfully!')</script>";
														echo '<script language="JavaScript"> window.location.href ="assign_dept.php" </script>';
													}
												else
													{
														echo "<script type='text/javascript'>alert('Assigned Department Unsuccessful!')</script>";
														echo '<script language="JavaScript"> window.location.href ="assign_dept.php" </script>';	
													}
												
											}//select no
									}//else if($selectedDepertmentNumber!='222')
							}//if
					}//while			
				?>
				<h4 class="widgettitle">विभाग लावा </h4>
				<form action="assign_dept.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="head1 center">थोडक्यात विषय </th>                         
								<th class="head0 center">विभाग लावा</th>
								<th class="head1 center">View Document</th>  
							</tr>
						</thead>
						<tbody>
						<?php 
							#########################################################################################
											//Fetching All Data In Datatable
							#########################################################################################
							convert_marathi();	
							$qry1 =("SELECT * FROM incoming_letters_transaction WHERE dept_id = (SELECT dept_id FROM login WHERE login_id ='$username') AND status!='approve' ORDER BY incoming_letters_transaction.ltr_id DESC");
							$rs1=mysql_query($qry1);
							while($rws=mysql_fetch_array($rs1))
								{
									$refdate = strtotime($row1['ref_dt']);
									$ref= date('d-m-Y', $refdate);
									//sent_dt
									$sentdate = strtotime($row1['sent_dt']);
									$sdt= date('d-m-Y', $sentdate);
									//ref dt
									$refdate = strtotime($rws['ref_dt']);
									$ref= date('d-m-Y', $refdate);
									//rec dt
									$recdt = strtotime($rws['receive_dt']);
									$rec= date('d-m-Y', $recdt);
									convert_marathi();//display in marathi
									$query = "SELECT  * FROM department_master"; 
									$result = mysql_query ($query);
									
									echo "<tr>
									<td class=center>".$rws['ltr_inword_no']."</td>
									<td class=center>". $rws['ref_no']." </td>
									<td class=center>".$ref."</td>
									<td class=center>".$rec."</td>
									<td class=center>".$rws['sender_name']."</td>
									<td class=center>".$rws['subject']."</td>";
									echo "<td class=center><select name='departmentnm".$rws['ltr_inword_no']."' id ='department'>Dropdown</option><option selected='selected'>विभागाचे नाव </option><option value='222'>All</option>";
									while($row = mysql_fetch_array($result))
										{
											echo "<option id='".$row['dept_id']."' value='".$row['dept_id']."'>".$row['dept_nm']."</option>";
										}
									echo" </select></td>";
									echo '<td class="center"><a href="view_avak.php?user=3&ltrid='.$rws['ltr_id'].'">View</a></td>	
									</tr>';
								} 
						?>
						</tbody>
					</table>
				<br>
				<p class="stdformbutton">
					<center>
						<button class="btn btn-primary">बदल करा </button>
						<button type="reset" class="btn">रद्द करा </button>
					</center>
                </p>
            </form> 
            <?php   
             include_once('footer.php');
            ?>
           </div><!--maincontentinner-->
        </div><!--maincontent-->
    </div><!--rightpanel-->
</div><!--mainwrapper-->
</body>
</html>
