<?php
/*
* date last modified 28-11-2017
* Edited by PI-Digi Logical Solutions
* HTML Files - add_user.html.php
* Mysql Tables - tbl_designation, tbl_staff
*/
include('includes/imgFunctions.php');
include('includes/functions.php'); # general functions


$debug = true;

if(isset($_REQUEST['p'])){
	$p = trim($_REQUEST['p']);
} else {
	$p = 1;
}

if(isset($_REQUEST['tech_id']) && $_REQUEST['tech_id']!=''){
	$techId = trim($_REQUEST['tech_id']);
	$chRs = mysqli_query($con, "SELECT id FROM tbl_technician WHERE id = '".$techId."' AND status = 'Active'");
	if(mysqli_num_rows($chRs)>0){
		//ok
	} else {
		$error = 'Sorry, you can credit amount to an active technician only';
	}
	
	$curDate = date('Y-m-d H:i:s');	

	if(isset($_POST['submit'])) {
		
		if(isset($_POST['amount']) && strlen($_POST['amount'])>0) {
			$amount = trim($_POST['amount']);
			if(is_numeric($amount) && $amount>0 && $amount<1000000){
				//ok
			} else {
				$error = 'Please enter a valid amount';
			}
		} else {
			$error = 'Please enter amount';
		}	
							
		if(empty($error)) {			
			$adminId = $_SESSION['adminId'];
			$insert = mysqli_query($con, "INSERT INTO tbl_admin_tech_extra_payments(technician_id, amount, date, added_by) VALUES ('".$techId."', '".$amount."', '".$curDate."', '".$adminId."')");
			if($insert){
				
				//send sms to tech
				$techRec = mysqli_fetch_array(mysqli_query($con, "SELECT name, phone FROM tbl_technician WHERE id = '".$techId."'"));
				$techName = $techRec['name'];
				$techPhone = $techRec['phone'];
				$smsMessage = urlencode('Hi '.$techName.', your account has been credited with Rs.'.$amount.'.');
				sendSMSP($techPhone, $smsMessage);						
												
				$success = 'The amount of Rs.'.$amount.' has been credited to technician';
			}
					
		} 
	}



} else {
	header('Location:index.php?page=manage_technician&p='.$p);
	exit();
}

?>