<?php
	function GetImageExtension($imagetype)
    {
      if(empty($imagetype)) return false;
       switch($imagetype)
       {
           case 'image/bmp': return '.bmp';
           case 'image/gif': return '.gif';
           case 'image/jpeg': return '.jpg';
           case 'image/png': return '.png';
		   case 'image/pdf': return '.pdf';
		   case 'image/BMP': return '.BMP';
           case 'image/GIF': return '.GIF';
           case 'image/JPEG': return '.JPG';
           case 'image/PNG': return '.PNG';
		   case 'image/PDF': return '.PDF';
           default: return false;
       }
    }
	$username=$_GET['username'];
     require_once('db.php');
	 require_once('../convert_marathi.php');
	 
	 ################################# Generating Inward Number #########################################
							
		
		################################ file upload#######################################
		if (!empty($_FILES["uploadedimage"]["name"])) 
		{
			$file_name=$_FILES["uploadedimage"]["name"];
			
			$temp_name=$_FILES["uploadedimage"]["tmp_name"];
			$imgtype=$_FILES["uploadedimage"]["type"];
			$ext= GetImageExtension($imgtype);
			$imagename=$file_name;
			
			$temp = explode(".", $_FILES["uploadedimage"]["name"]);
			$newfilename =$recdt.round(microtime(true)) . '.' . end($temp);
			$target_path = "../uploads/".$newfilename;
			
			if($target_path!='')
			{
				if(!move_uploaded_file($temp_name, $target_path))
				 {
						echo "<script type='text/javascript'>alert('Unable to move file!')</script>";
						echo '<script language="JavaScript"> window.location.href ="../direct_javak_dpt.php" </script>';
				}
				else
				{
					move_uploaded_file($temp_name, $target_path);
				}
			}
		}
		else
		{
			$target_path = "../uploads/file_nt_uploaded.jpg";
		}
		################################ end of file upload #######################################
		$javak=$_POST['javak'];
		$jdate=$_POST['jdate'];
        $jdate = date('Y-m-d', strtotime($jdate));
		$sp=$_POST['sp'];
		//$vibhag=$_POST['vibhag'];
		$othern=$_POST['othern'];
		$sakalan=$_POST['sakalan'];
		$sub=$_POST['sub'];
		
		convert_marathi();	
			$q=mysql_query("SELECT * from login where login_id='$username' ");
			$r=mysql_fetch_array($q);
			$deptid=$r['dept_id'];
			$desk_id=$r['desk_id'];
			$curdt=date("Y-m-d");
			
		if($sp=='dpt')
		{
			$othern='';
			$chkk = $_POST['chkk'];
			$str   = "";
			foreach ($chkk as $val) 
			{
				if($str=='')
				{
					$str = $val;
				}
				else 
				{
					$str = $str.",".$val;
				}
			}
			
			$arr   = explode(",",$str);
	        $cnt   = count($arr);
		    for($i=0;$i<$cnt;$i++)
			{
				$vibhag = $arr[$i];
				$othern='';
				            $year = date('y');
				            convert_marathi();
							$r = mysql_query("SELECT MAX(javak_no) AS id FROM direct_javak WHERE javak_no like '$year%' and dept_id='$deptid'");
							$row1 = mysql_fetch_array($r);
							$Auto_increment = $row1['id']; //got auto inc in d form of 15-0049
							$start='1';
							if ($Auto_increment != '')
							{
								list($a,$b) = Explode("-",$Auto_increment); // specifies to break d string at '-'
								if ($a==$year)
								{
									$c= str_pad($b + 1,1, 0, STR_PAD_LEFT);
								
									$n = $year ."-". $c;
								}
								else
								{
									$c= str_pad($b + 1,1, 0, STR_PAD_LEFT);
									$n =$year."-". $c;
								}
							}
							else
							{
								$n =$year."-". $start;
							}
				convert_marathi();	
				$qry=mysql_query("INSERT into direct_javak(javak_no, javak_dt, actoin, dpt_id, other_nm, sakalan, subject, document,dept_id,desk_id) values('$javak','$jdate','$sp' ,'$vibhag','$othern','$sakalan','$sub','$target_path','$deptid','$desk_id')");
				                                                                   
			}
		
		}
		else if($sp=='other')
		{
			$vibhag='';
			//echo $othern;
			                convert_marathi();
							$r = mysql_query("SELECT MAX(javak_no) AS id FROM direct_javak WHERE javak_no like '$year%' and dept_id='$deptid'");
							$row1 = mysql_fetch_array($r);
							$Auto_increment = $row1['id']; //got auto inc in d form of 15-0049
							$start='1';
							if ($Auto_increment != '')
							{
								list($a,$b) = Explode("-",$Auto_increment); // specifies to break d string at '-'
								if ($a==$year)
								{
									$c= str_pad($b + 1,1, 0, STR_PAD_LEFT);
								
									$n = $year ."-". $c;
								}
								else
								{
									$c= str_pad($b + 1,1, 0, STR_PAD_LEFT);
									$n =$year."-". $c;
								}
							}
							else
							{
								$n =$year."-". $start;
							}
			convert_marathi();
			$qry=mysql_query("INSERT into direct_javak(javak_no, javak_dt, actoin, dpt_id, other_nm, sakalan, subject, document,dept_id,desk_id) values('$javak','$jdate','$sp' ,'$vibhag','$othern','$sakalan','$sub','$target_path','$deptid' ,'$desk_id')");
		}
		
			if($qry)
			{
				echo "<script type='text/javascript'>alert('Inserted successfully!')</script>";
				echo '<script language="JavaScript"> window.location.href ="../direct_javak_dpt.php" </script>';
			}
			else
			{
			
				echo "<script type='text/javascript'>alert('Inserted unsuccessfully!')</script>";
				echo '<script language="JavaScript"> window.location.href ="../direct_javak_dpt.php" </script>';
			}
				
?>