<?php
	############################################### 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 = "17-10041";//$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='00001';
	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;
	}
	################################# 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"];
			//echo  $file_name."<br>";
			$temp_name=$_FILES["uploadedimage"]["tmp_name"];
			$imgtype=$_FILES["uploadedimage"]["type"];
			$ext= GetImageExtension($imgtype);
			$imagename=$file_name;
			//$target_path = "../uploads/".$imagename;
			 
			 
			$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 ="../letterentry.php" </script>';
				}
				else
				{
					move_uploaded_file($temp_name, $target_path);
				}
			}
		}
		else
		{
			$target_path = "../uploads/file_nt_uploaded.jpg";
		}
		################################ end of file upload #######################################
		
		$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();
		
       convert_marathi();
	  
		if($refno!='' && $refdt!='' && $recdt!='' && $sendernm!='' && $add!='' && $sandh!='' && $sub!='' && $padtalni!='')
		{
			
			
			$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,first_flag) values('$ltrinwrdno','$refno','$refdt' ,'$recdt','$sendernm','$mobno','$email','$add','$sandh','$sub','$prio','$target_path','pending','$padtalni','Y')");
			if(mysql_query($qry))
			{
				$qr=mysql_query("SELECT ltr_id from incoming_letters_transaction where ltr_inword_no='$ltrinwrdno' ");
				$rs=mysql_fetch_array($qr);
				$ltrid=$rs['ltr_id'];
			
				echo "<script type='text/javascript'>alert('Inserted Successfully!')</script>";
				
				
				function sendSMS($mobno,$sendernm,$refno) 
				{
					$url = 'otapal.cwsdomians.in/submitsms.jsp';
					$customer = 'otapal';
					$senderid="OTapal";
					$key = '25d9958eacXX';
					
					$request = $url.'?user='.$customer.'&key='.$key.'&senderid='.$senderid.'&mobile='.urlencode($mobno).'&message='.urlencode('मा.महोदय,  '.$sendernm.' आपले पत्र   संदर्भ क्रमांक: '.$refno.'  अन्वये जिल्हा पोलीस अधीक्षक कार्यालय,रायगड यांस प्राप्त झाले. ').'&accusage=1&unicode=1';
					
				// 	echo $request;
					$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);

				// 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 
				{
				// 	echo "Failure";
					// 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 ="../reciet.php?ltrid='.$ltrid.'" </script>';
			}
			else
			{
				echo "<script type='text/javascript'>alert('Inserted unsuccessfully!')</script>";
				echo '<script language="JavaScript"> window.location.href ="../letterentry.php" </script>';
			
			}
		}
		else
		{
				echo "<script type='text/javascript'>alert('Please fill the data !')</script>";
				echo '<script language="JavaScript"> window.location.href ="../letterentry.php" </script>';
		
		}
	

	
	
?>
																					