<?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['project_id']) && $_REQUEST['project_id']!=''){		
	$projectId = mysqli_real_escape_string($con, trim($_REQUEST['project_id']));	

} else {
	header('Location:index.php?page=manage_projects');
}


if(isset($_REQUEST['id']) && $_REQUEST['id']!=''){	
	$isEdit = TRUE;
	$editId = mysqli_real_escape_string($con, trim($_REQUEST['id']));
	$editRs = mysqli_query($con, "SELECT *, DATE(date) AS date FROM tbl_project_income WHERE id = '".$editId."'");
	if(mysqli_num_rows($editRs)>0){
		$resEdit = mysqli_fetch_array($editRs);
		$expDateArr = explode('-',$resEdit['date']);
		$incDate = $expDateArr['2'].'-'.$expDateArr['1'].'-'.$expDateArr['0'];
	} 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(isset($_POST['inc_date']) && strlen($_POST['inc_date'])>0) {
		$expDate = trim($_POST['inc_date']);
	} else {
		$error = 'Please select Date';
	}
	
		
	if(empty($error)) {
		$curDate = date('Y-m-d H:i:s');			
				
		$insertQry = "INSERT INTO tbl_project_income(project_id, amount, date, description, added_by) VALUES (
		'".$projectId."',
		 '".mysqli_real_escape_string($con, $amount)."',
		 '".$expDate."',
		 '".mysqli_real_escape_string($con, $description)."',
		 '".mysqli_real_escape_string($con, $_SESSION['adminId'])."')";
		
		$execInsert = mysqli_query($con,$insertQry);		
		if($execInsert) {
						
			$success = 'Income Added Successfully';
			header('Location:index.php?page=project_incomes&id='.$projectId.'&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(isset($_POST['inc_date']) && strlen($_POST['inc_date'])>0) {
		$expDate = trim($_POST['inc_date']);
	} else {
		$error = 'Please select Date';
	}
	
		
	if(empty($error)) {
		$curDate = date('Y-m-d H:i:s');			
				
		$insertQry = "UPDATE tbl_project_income 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 = '".$expDate."' WHERE id = '".$editId."'";
		
		$execInsert = mysqli_query($con,$insertQry);		
		if($execInsert) {
						
			$success = 'Income Updated Successfully';
			header('Location:index.php?page=project_incomes&id='.$projectId.'&success='.$success);
			exit();			
		}				
	} 
}



?>