<?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['user_id']) && $_REQUEST['user_id']!=''){
	$userId = trim($_REQUEST['user_id']);
	$chRs = mysqli_query($con, "SELECT id FROM tbl_user WHERE id = '".$userId."' AND status = 'Active'");
	if(mysqli_num_rows($chRs)>0){
		//ok
	} else {
		$error = 'Sorry, you can credit amount to an active user 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_user_payments(user_id, amount, type, payment_method, payment_status, added_date, added_by) VALUES ('".$userId."', '".$amount."', 'Deposit', 'Admin', 'Success', '".$curDate."', '".$adminId."')");
			if($insert){
				//add as expense
				mysqli_query($con, "INSERT INTO tbl_expenses (cat_id, amount, exp_date, date) VALUES ('8', '".$amount."', '".$curDate."', '".$curDate."')");
									
				$success = 'The user account has been credited with Rs.'.$amount;
			}
					
		} 
	}



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

?>