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

$username = $_REQUEST['login_id'];
$ltr_id = $_REQUEST['ltr_id'];

// letter accept/ return flag..... 1=accept, 2=return
$acc_ret_flag = $_REQUEST['acc_ret_flag'];

if($acc_ret_flag==2)
{
	// letter self_dept / othr_dept flag..... 1=self_dept, 2=othr_desk
	$dep_desk_flag = $_REQUEST['dep_desk_flag'];
	
	if($dep_desk_flag==2)
	{
		$desk_id = $_REQUEST['desk_id'];
	}
}
	echo "bbbb";
					convert_marathi();
					$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 
					AND  i.ltr_id = '$ltr_id'
					ORDER BY i.ltr_id DESC ") ;	
					
					while($rw=mysql_fetch_array($qrye))	 
					{  
						echo "aaaaa";
						
						
						$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'];
						
						
						
								$selectedoption = $acc_ret_flag;
								##################### If Letter Approve #############################
								if($selectedoption==1)
								{	
									$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='' ");
										 
										if(mysql_query($sql2))
										{
											echo "200";		
										}
										else
										{
											echo "300";
										}
									}
									else
									{
										echo "400";
									}
								}
								##################### End Of If Letter Approve #############################

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

									$category=$dep_desk_flag;
									
									$ltrid=$rw['ltr_id'];
									$dptltrid=$rw['dpt_ltr_id'];
									$ltrinwardno=$rw['ltr_inword_no'];	
									$sentdate=$rw['sent_to_desk_dt'];
									
									$rtntodsk = $desk_id;
									
									//echo $rtntodsk."--".$category;
									$sndt = date('Y-m-d', strtotime($sentdate));
									$us=$rw['dept_id'];
									$dptin=$rw['dpt_inword_no'];
									
									if($category==1 || ( $rtntodsk!='' && $category==2))
									{
										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==1)
											{	
												$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==2)
											{
												$qry3=mysql_query("UPDATE tapal_to_dpt_transaction SET desk_id='$rtntodsk', desk_approval='pending', status='returned' WHERE dpt_ltr_id='$dptltrid'");	
											}
											echo "2001";
											
										}	
										else
										{   
											echo "3001";
											
										}
									}//if($category!='' && $rtntodsk!='')
									else
									{
										echo "4001";
										
									}
								}//else if($selectedoption=='return')
												
							
									##################### End Of If Letter Return ######################	
					}
					
					
					
					
					
					?>
					
					
					
					
					
					
					
					
					
					
					
					
			

