<?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


if(isset($_REQUEST['id']) && $_REQUEST['id']!=''){		
	$techId = mysqli_real_escape_string($con, trim($_REQUEST['id']));		
	$techType = getTechType($techId);

	//get all payments to LiiMR
	$paymentRs = mysqli_query($con, "SELECT * FROM tbl_admin_tech_incomes WHERE technician_id = '".$techId."' ORDER BY id DESC");
	
	//get all incomes from LiiMR
	$incomeRs = mysqli_query($con, "SELECT * FROM tbl_admin_tech_payments WHERE technician_id = '".$techId."' ORDER BY id DESC");
	
	
	//get total amount (user paid)
	$taskRec = mysqli_fetch_array(mysqli_query($con, "SELECT SUM(task_amount) AS amount FROM `tbl_technician_payments` WHERE `technician_id` = '".$techId."' AND status = 'paid'")); 
	$totalTaskFullAmount = $taskRec['amount'];
	
	//get total amount got from tasks as Cash/Online/Wallet
	$incomesRec = mysqli_fetch_array(mysqli_query($con, "SELECT SUM(user_amount) AS amount FROM `tbl_technician_payments` WHERE `technician_id` = '".$techId."' AND status = 'paid'")); 
	$totalTaskAmount = $incomesRec['amount'];
	
	//get total amount got from tasks as Cash
	$incomesCashRec = mysqli_fetch_array(mysqli_query($con, "SELECT SUM(task_amount) AS amount FROM `tbl_technician_payments` WHERE `technician_id` = '".$techId."' AND payment_method = 'Cash' AND status = 'paid'")); 
	$totalCashAmount = $incomesCashRec['amount'];
	
	//get total amount got from tasks as Onlinw/Wallet
	$incomesWalletRec = mysqli_fetch_array(mysqli_query($con, "SELECT SUM(task_amount) AS amount FROM `tbl_technician_payments` WHERE `technician_id` = '".$techId."' AND payment_method != 'Cash' AND status = 'paid'")); 
	$totalWalletAmount = $incomesWalletRec['amount'];
	
	//total admin fee for LiiMR
	$adminRec = mysqli_fetch_array(mysqli_query($con, "SELECT SUM(admin_fee) AS amount FROM `tbl_technician_payments` WHERE `technician_id` = '".$techId."' AND status = 'paid'")); 
	$totalAdminFee = $adminRec['amount'];
	
	//total operator commission
	//$operatorRec = mysqli_fetch_array(mysqli_query($con, "SELECT SUM(operator_commission) AS amount FROM `tbl_technician_payments` WHERE `technician_id` = '".$techId."'")); 
	//$totalOperatorCommission = $operatorRec['amount'];
	
	//total given to liimr
	$toLiiMRRec = mysqli_fetch_array(mysqli_query($con, "SELECT SUM(amount) AS amount FROM tbl_admin_tech_incomes WHERE technician_id = '".$techId."'"));
	$totAmountGiven = $toLiiMRRec['amount'];
	
	//total received from liimr
	$fromLiiMRRec = mysqli_fetch_array(mysqli_query($con, "SELECT SUM(amount) AS amount FROM tbl_admin_tech_payments WHERE technician_id = '".$techId."'"));
	$totAmountReceived = $fromLiiMRRec['amount'];
	
	//my outstanding balance
	//$myBalance = $totalTaskAmount-(($totalCashAmount+$totAmountReceived)-$totAmountGiven);
	$myBalance = getBalanceTech($techId);
	
	//total extra amount credited from admin
	$totExtraCredits = 0;
	$adminExtraCreditRec = mysqli_fetch_array(mysqli_query($con, "SELECT SUM(amount) AS amount FROM tbl_admin_tech_extra_payments WHERE technician_id = '".$techId."'"));
	$totExtraCredits = $adminExtraCreditRec['amount'];
	
} else {
	header('Location:index.php?page=manage_technician');
	exit();
} 


?>