<?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/functions.php'); # general functions

$isEdit = FALSE;

if(isset($_REQUEST['tech_id']) && $_REQUEST['tech_id']!=''){		
	$techId = mysqli_real_escape_string($con, trim($_REQUEST['tech_id']));	
} else {
	header('Location:index.php?page=manage_technician');
}


if(isset($_REQUEST['id']) && $_REQUEST['id']!=''){	
	$isEdit = TRUE;
	$editId = mysqli_real_escape_string($con, trim($_REQUEST['id']));
	$editRs = mysqli_query($con, "SELECT * FROM tbl_admin_tech_payments WHERE id = '".$editId."'");
	if(mysqli_num_rows($editRs)>0){
		$resEdit = mysqli_fetch_array($editRs);
	} else {
		$error = 'No payment added';
	}	
	

} 

if(isset($_POST['add'])) {
	
	if(strlen(trim($_POST['description'])) > 0) {
		$description = trim($_POST['description']);
	} else {
		$description = '';
	}	
		
	if(strlen(trim($_POST['amount'])) > 0) {
		$amount = trim($_POST['amount']);
		if(is_numeric($amount) && $amount>0){
			//ok
		} else {
			$error = 'Please enter a valid amount';
		}
	} else {
		$error = 'Please enter amount';
	}
	
		
	if(empty($error)) {
		$curDate = date('Y-m-d H:i:s');			
				
		$insertQry = "INSERT INTO tbl_admin_tech_payments(technician_id, amount, description, date, added_by) VALUES (
		'".$techId."',
		 '".mysqli_real_escape_string($con, $amount)."',
		 '".mysqli_real_escape_string($con, $description)."',
		 '".$curDate."',		 
		 '".mysqli_real_escape_string($con, $_SESSION['adminId'])."')";
		
		$execInsert = mysqli_query($con,$insertQry);		
		if($execInsert) {
			
			//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 = 'Payment Added Successfully';
			header('Location:index.php?page=tech_payments&tech_id='.$techId.'&success='.$success);
			exit();			
		}				
	} 
}

if(isset($_POST['update'])) {
	
	if(strlen(trim($_POST['description'])) > 0) {
		$description = trim($_POST['description']);
	} else {
		$description = '';
	}	
		
	if(strlen(trim($_POST['amount'])) > 0) {
		$amount = trim($_POST['amount']);
		if(is_numeric($amount) && $amount>0){
			//ok
		} else {
			$error = 'Please enter a valid amount';
		}
	} else {
		$error = 'Please enter amount';
	}
	
		
	if(empty($error)) {
		$curDate = date('Y-m-d H:i:s');			
				
		$insertQry = "UPDATE tbl_admin_tech_payments SET amount = '".mysqli_real_escape_string($con, $amount)."', 
		description = '".mysqli_real_escape_string($con, $description)."', 
		updated_by = '".mysqli_real_escape_string($con, $_SESSION['adminId'])."', 
		updated_on = '".$curDate."' WHERE id = '".$editId."'";
		
		$execInsert = mysqli_query($con,$insertQry);		
		if($execInsert) {
						
			$success = 'Payment Updated Successfully';
			header('Location:index.php?page=tech_payments&tech_id='.$techId.'&success='.$success);
			exit();			
		}				
	} 
}

//get total amount to be given to tech( from job with payment mode online and wallet)
//$paymentsRec = mysqli_fetch_array(mysqli_query($con, "SELECT SUM(user_amount) AS amount FROM `tbl_technician_payments` WHERE `technician_id` = '".$techId."' AND payment_method != 'Cash'")); 
//$paidRec = mysqli_fetch_array(mysqli_query($con, "SELECT SUM(amount) AS amount FROM `tbl_admin_tech_payments` WHERE `technician_id` = '".$techId."'"));
//$totalAmountPending = $paymentsRec['amount']-$paidRec['amount'];

$techBalance = getBalanceTech($techId); //the amount in tech hand
if($techBalance>0){
	$totalAmountPending = 0; //to tech 
} else {
	$totalAmountPending = abs($techBalance); //to tech as this is a minus values we should make it positive in this page
}



?>