<?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
*/
require_once('./classes/class.noseo_pagination.php'); # pagination class
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_incomes WHERE id = '".$editId."'");
	if(mysqli_num_rows($editRs)>0){
		$resEdit = mysqli_fetch_array($editRs);
	} else {
		$error = 'No income added';
	}	
	

}

if(isset($_REQUEST['pay_id']) && $_REQUEST['pay_id']!=''){		
	$payId = mysqli_real_escape_string($con, trim($_REQUEST['pay_id']));
	$payRs = mysqli_query($con, "SELECT * FROM tbl_tech_sub_incomes WHERE id = '".$payId."'");
	if(mysqli_num_rows($payRs)>0){
		$resPay = mysqli_fetch_array($payRs);
		$techId = $resPay['operator_id'];
		$subId = $resPay['sub_id'];
		$subAmt = $resPay['amount'];
		$dateCur = date('Y-m-d H:i:s');
		
		mysqli_query($con, "INSERT INTO tbl_admin_tech_incomes (technician_id, operator_id, amount, description, date) VALUES ('".$subId."', '".$techId."', '".$subAmt."', 'from operator', '".$dateCur."')");
		mysqli_query($con, "UPDATE tbl_tech_sub_incomes SET status = 'paid' WHERE id = '".$payId."'");
				
		$success = 'The payment has been received'; 
		header('Location:index.php?page=tech_add_income&tech_id='.$techId.'&success='.$success);
		exit();	
		
	} else {
		$error = 'No income 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_incomes(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 payment of Rs.'.$amount.' to '.SITE_NAME.' has been received.');						
			sendSMSP($techPhone, $smsMessage);
						
			$success = 'Income Added Successfully';
			header('Location:index.php?page=tech_incomes&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_incomes 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 = 'Income Updated Successfully';
			header('Location:index.php?page=tech_incomes&tech_id='.$techId.'&success='.$success);
			exit();			
		}				
	} 
}
//total income
//$incomesTotRec = mysqli_fetch_array(mysqli_query($con, "SELECT SUM(user_amount) AS amount FROM `tbl_technician_payments` WHERE `technician_id` = '".$techId."'"));
//get total amount received from tech( from job with payment mode cash)
//for staff tech all amount should be given to LiiMR
//for others they can take their commission and the rest goes to admin
$techRec = mysqli_fetch_array(mysqli_query($con, "SELECT type FROM tbl_technician WHERE id = '".$techId."'"));
$techType = $techRec['type'];
$totalAmountPending = getBalanceTech($techId);

$pageVars = '';

// Get All User details to list 
$where = ' id>0 ';

$select = "SELECT * FROM tbl_tech_sub_incomes WHERE operator_id = '".$techId."' AND status = 'pending' ORDER BY id DESC";
//echo $select;
// Using pagination class
$pagination = new pagination_noseo($select);
$execDetails = $pagination->createPages(700);

$pageLnk = 'index.php?page='.$page.'&'.$pageVars;
$pageNums = $pagination->pageNumber(5, $pageLnk);



?>