<?php

$flag=$_REQUEST['flag'];
if($flag=='')
{
	############################################### Letter Entry Database ##############################################
	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 #########################################
							$year = date('y');
							/*$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']; //got auto inc in d form of 15-0049       */
							$r = mysql_query("SELECT ltr_inword_no FROM incoming_letters_transaction WHERE ltr_inword_no  like '$year%'");
				
							while($row1 = mysql_fetch_array($r))
							{
							 $Auto_increment =$row1['ltr_inword_no'];
							}
							
							$start='0001';
							if ($Auto_increment != '')
							{
								list($a,$b) = Explode("-",$Auto_increment); // specifies to break d string at '-'
								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;
							}
							
							
							$r1 = mysql_query("select max(t.dpt_inword_no) as id from tapal_to_dpt_transaction t, incoming_letters_transaction i
							where  t.dpt_inword_no like '$year%' 
							AND t.dept_id_from=(SELECT dept_id from login where login_id = '$username')
							AND t.ltr_id=i.ltr_id ") ;
							$row11 = mysql_fetch_array($r1);
							$Auto_increment1 = $row11['id'];
							$start1='0001';
							################################################################################
												// Generating Inward No
							################################################################################
							if ($Auto_increment1 != '')
							{	
								list($a1,$b1) = Explode("-",$Auto_increment1);
								if ($a1==$year)
								{
									$c1= str_pad($b1 + 1, 4, 0, STR_PAD_LEFT);
									$n1 = $year ."-". $c1;
								}
								else
								{
									$c1= str_pad($b1 + 1, 4, 0, STR_PAD_LEFT);
									$n1=  $year."-". $c1;
								}
							}
							else
							{
								$n1 =$year."-". $start1;
							}
							################################# End OF Generating Inward Number ######################################
							
							
							
	 	$ltrinwrdno=$n;
		$refno=$_POST['refno'];
		
		$rfsdt=$_POST['refdate'];

        $refdt = date('Y-m-d', strtotime($rfsdt));
		$redt=$_POST['recdate'];
		 $recdt = date('Y-m-d', strtotime($redt));
		$sendernm=$_POST['sendernm'];
		$sub=$_POST['sub'];
		################################ 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 ="../letterentry1.php" </script>';
				}
				else
				{
					move_uploaded_file($temp_name, $target_path);
				}
			}
		}
		else
		{
			$target_path = "../uploads/file_nt_uploaded.jpg";
		}
		################################ end of file upload #######################################
		
		
		//$sandh=$_POST['ltrtypec'];
		
		$msp=$_POST['sp'];
		
		if($msp=='sthanik')
		{
			$sandh=27;
		}
		else
		{
			$sandh=$_POST['ltrtypec'];
		}
		
		$prio=$_POST['priodays'];
		if($prio=='' or $prio=='0')
		{
			$prio='0';
		}
		$mobno=$_POST['mobno'];
		$add=$_POST['add'];
		$email=$_POST['email'];
		$padtalni=mt_rand();
		$dptinwrdno=$n1;
		
       convert_marathi();
	  
		
		if($refno!='' && $refdt!='' && $recdt!='' && $sendernm!='' && $add!='' && $sandh!='' && $sub!='' && $padtalni!='')
		{	
			$q=mysql_query("SELECT dept_id from login where login_id='$username' ");
			$r=mysql_fetch_array($q);
			$deptid=$r['dept_id'];
			$curdt=date("Y-m-d");
			$qry=("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,padtalni_no,dept_id,sent_dt,dpt_inword_no,first_flag) values('$ltrinwrdno','$refno','$refdt' ,'$recdt','$sendernm','$mobno','$email','$add','$sandh','$sub','$prio','$target_path','approve','$padtalni','$deptid','$curdt','$dptinwrdno','Y')");
			
			if(mysql_query($qry))
			{
				$qr=mysql_query("SELECT ltr_id from incoming_letters_transaction where dpt_inword_no='$dptinwrdno' AND dept_id ='$deptid' ");
				$rs=mysql_fetch_array($qr);
				$ltrid=$rs['ltr_id'];
				
				$q=("INSERT into tapal_to_dpt_transaction (dpt_inword_no, dpt_ltr_rec_dt, dpt_ltr_aprv_dt, status, dept_id_from, ltr_id) VALUES('$dptinwrdno', '$curdt', '$curdt', 'approve', '$deptid', '$ltrid')");
				if(mysql_query($q))
				{
					echo "<script type='text/javascript'>alert('Inserted Successfully!')</script>";
				
					function sendSMS($mobno,$sendernm,$refno,$padtalni) 
					{
						$url = 'http://104.155.195.12/API/WebSMS/Http/v1.0a/index.php';
						$customer = 'osmanabadT';
						$senderid="OTapal";
						$key = 'tapal@123';
						
						$request = $url.'?username='.$customer.'&password='.$key.'&sender='.$senderid.'&to='.urlencode($mobno).'&message='.urlencode('Congratulation '.$sendernm.'    संदर्भ क्रमांक: '.$refno.'      पडताळणी क्रमांक : '.$padtalni.'   - जिल्हा पोलिस अधीक्षक, बीड. ').'&msgtype='.unicode;
						$ch = curl_init();
						curl_setopt($ch, CURLOPT_URL, $request);
						curl_setopt($ch, CURLOPT_HEADER, 0);
						curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
						$response = curl_exec($ch);
						curl_close($ch);
						return split(',',$response);
					}
				
					// Integration in your project
				//	$sms_api_result = sendSMS($mobno,$sendernm,$refno,$padtalni);

					// Check if SMS was received by the API or not
					if ($sms_api_result[0] == 'OK') {
						// Ok, SMS received by the API
						echo "The SMS was sent.";
					}
					else {
						// Failure, SMS was not received by the API
						// I this example we display the response to identify the error
						//print_r($sms_api_result);
					}
					//echo '<script language="JavaScript"> window.location.href ="print_invoice.php?id='.$id.'"</script>';
			
					echo "<script language='JavaScript'> window.location.href ='../reciet11.php?ltrid=".$ltrid."' </script>";
				}
				else
				{
					mysql_query("DELETE from incoming_letters_transaction where ltr_id='$ltrid' ");
					echo "<script type='text/javascript'>alert('Inserted unsuccessfully!')</script>";
					echo '<script language="JavaScript"> window.location.href ="../letterentry1.php" </script>';
				}
				
			}
			else
			{
				echo "<script type='text/javascript'>alert('Inserted unsuccessfully!')</script>";
				echo '<script language="JavaScript"> window.location.href ="../letterentry1.php" </script>';
			
			}
		}
		else
		{
				echo "<script type='text/javascript'>alert('Please fill the data !')</script>";
				echo '<script language="JavaScript"> window.location.href ="../letterentry1.php" </script>';
		
		}
}

else
{
	############################################### Letter Entry Database ##############################################
	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 #########################################
	$year = date('y');
	$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']; //got auto inc in d form of 15-0049
	$start='0001';
	if ($Auto_increment != '')
	{
		list($a,$b) = Explode("-",$Auto_increment); // specifies to break d string at '-'
		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;
	}
	
	
	$r1 = mysql_query("select max(t.dpt_inword_no) as id from tapal_to_dpt_transaction t, incoming_letters_transaction i
	where  t.dpt_inword_no like '$year%' 
	AND t.dept_id_from=(SELECT dept_id from login where login_id = '$username')
	AND t.ltr_id=i.ltr_id ") ;
	$row11 = mysql_fetch_array($r1);
	$Auto_increment1 = $row11['id'];
	$start1='0001';
	################################################################################
						// Generating Inward No
	################################################################################
	if ($Auto_increment1 != '')
	{	
		list($a1,$b1) = Explode("-",$Auto_increment1);
		if ($a1==$year)
		{
			$c1= str_pad($b1 + 1, 4, 0, STR_PAD_LEFT);
			$n1 = $year ."-". $c1;
		}
		else
		{
			$c1= str_pad($b1 + 1, 4, 0, STR_PAD_LEFT);
			$n1 =  $year."-". $c1;
		}
	}
	else
	{
		$n1 =$year."-". $start1;
	}
	################################# End OF Generating Inward Number ######################################
	
							
	 	$ltrinwrdno=$n;
		$refno=$_POST['refno'];
		
		$rfsdt=$_POST['refdate'];

        $refdt = date('Y-m-d', strtotime($rfsdt));
		$redt=$_POST['recdate'];
		 $recdt = date('Y-m-d', strtotime($redt));
		$sendernm=$_POST['sendernm'];
		$sub=$_POST['sub'];
		################################ 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;
	    $target_path = "../uploads/".$imagename;
			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 ="../letterentry1.php?flag=n" </script>';
				}
				else
				{
					move_uploaded_file($temp_name, $target_path);
				}
			}
		}
		else
		{
			$target_path = "../uploads/file_nt_uploaded.jpg";
		}
		################################ end of file upload #######################################
		
		
		//$sandh=$_POST['ltrtypec'];
		
		$msp=$_POST['sp'];
		
		if($msp=='sthanik')
		{
			$sandh=27;
		}
		else
		{
			$sandh=$_POST['ltrtypec'];
		}
		
		$prio=$_POST['priodays'];
		if($prio=='' or $prio=='0')
		{
			$prio='0';
		}
		$mobno=$_POST['mobno'];
		$add=$_POST['add'];
		$email=$_POST['email'];
		$padtalni=mt_rand();
		$dptinwrdno=$n1;
       convert_marathi();
	  
		if($refno!='' && $refdt!='' && $recdt!='' && $sendernm!='' && $add!='' && $sandh!='' && $sub!='' && $padtalni!='')
		{	
			$q=mysql_query("SELECT dept_id from login where login_id='$username' ");
			$r=mysql_fetch_array($q);
			$deptid=$r['dept_id'];
			$curdt=date("Y-m-d");
			$qry=("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,padtalni_no,dept_id,sent_dt,dpt_inword_no,first_flag) values('$ltrinwrdno','$refno','$refdt' ,'$recdt','$sendernm','$mobno','$email','$add','$sandh','$sub','$prio','$target_path','approve','$padtalni','$deptid','$curdt','$dptinwrdno','Y')");
			
			if(mysql_query($qry))
			{
				$qr=mysql_query("SELECT ltr_id from incoming_letters_transaction where dpt_inword_no='$dptinwrdno' AND dept_id ='$deptid' ");
				$rs=mysql_fetch_array($qr);
				$ltrid=$rs['ltr_id'];
				
				$q=("INSERT into tapal_to_dpt_transaction (dpt_inword_no, dpt_ltr_rec_dt, dpt_ltr_aprv_dt, status, dept_id_from, ltr_id) VALUES('$dptinwrdno', '$curdt', '$curdt', 'approve', '$deptid', '$ltrid')");
				if(mysql_query($q))
				{
					echo "<script type='text/javascript'>alert('Inserted Successfully!')</script>";
				
					function sendSMS($mobno,$sendernm,$refno,$padtalni) 
					{
						$url = 'http://104.155.195.12/API/WebSMS/Http/v1.0a/index.php';
						$customer = 'osmanabadT';
						$senderid="OTapal";
						$key = 'tapal@123';
						
						$request = $url.'?username='.$customer.'&password='.$key.'&sender='.$senderid.'&to='.urlencode($mobno).'&message='.urlencode('Congratulation '.$sendernm.'    संदर्भ क्रमांक: '.$refno.'      पडताळणी क्रमांक : '.$padtalni.'  - जिल्हा पोलिस अधीक्षक, बीड. ').'&msgtype='.unicode;
						
						$ch = curl_init();
						curl_setopt($ch, CURLOPT_URL, $request);
						curl_setopt($ch, CURLOPT_HEADER, 0);
						curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
						$response = curl_exec($ch);
						curl_close($ch);
						return split(',',$response);
					}
				
					// Integration in your project
					//$sms_api_result = sendSMS($mobno,$sendernm,$refno,$padtalni);

					// Check if SMS was received by the API or not
					if ($sms_api_result[0] == 'OK') {
						// Ok, SMS received by the API
						echo "The SMS was sent.";
					}
					else {
						// Failure, SMS was not received by the API
						// I this example we display the response to identify the error
						//print_r($sms_api_result);
					}
					//echo '<script language="JavaScript"> window.location.href ="print_invoice.php?id='.$id.'"</script>';
			
					echo "<script language='JavaScript'> window.location.href ='../reciet11.php?flag=".$flag."&ltrid=".$ltrid."' </script>";
				}
				else
				{
					mysql_query("DELETE from incoming_letters_transaction where ltr_id='$ltrid' ");
					echo "<script type='text/javascript'>alert('Inserted unsuccessfully!')</script>";
					echo '<script language="JavaScript"> window.location.href ="../letterentry1.php?flag=n" </script>';
				}
				
			}
			else
			{
				echo "<script type='text/javascript'>alert('Inserted unsuccessfully!')</script>";
				echo '<script language="JavaScript"> window.location.href ="../letterentry1.php?flag=n" </script>';
			
			}
		}
		else
		{
				echo "<script type='text/javascript'>alert('Please fill the data !')</script>";
				echo '<script language="JavaScript"> window.location.href ="../letterentry1.php?flag=n" </script>';
		
		}
}	

	
	
?>
																					