<?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['id']) && $_REQUEST['id']!=''){
	
	$isEdit = TRUE;
	$editId = mysqli_real_escape_string($con, trim($_REQUEST['id']));	
	$editRs = mysqli_query($con, "SELECT * FROM tbl_expense_category WHERE id = '".$editId."'");
	if(mysqli_num_rows($editRs)>0){
		$resEdit = mysqli_fetch_array($editRs);
	} else {
		$error = 'The category does not exist!';
	}
	
	if(isset($_REQUEST['action']) && $_REQUEST['action']=='delete'){
		//check if no delete
		$chkRec = mysqli_fetch_array(mysqli_query($con, "SELECT no_delete FROM tbl_expense_category WHERE id = '".$editId."'"));
		if($chkRec['no_delete']=='yes'){
			$error = 'Kidding? You cannot delete this category';
		} else {			
			$delete = mysqli_query($con, "DELETE FROM tbl_expense_category WHERE id = '".$editId."'");
			if($delete){
				$success = 'The category has been deleted';
				header('Location:index.php?page=expense_category&success='.$success);
				
			} else {
				$error = 'The category does not exist!';
			}
		}	
	} 

} 

if(isset($_POST['add'])) {
		
	if(isset($_POST['name']) && strlen($_POST['name'])>0) {
		$name = trim($_POST['name']);
	} else {
		$error = 'Please enter category';
	}
	
	
	if(empty($error)) {			
		$insertQry = "INSERT INTO tbl_expense_category (name) VALUES ('".mysqli_real_escape_string($con, $name)."')";
		$execInsert = mysqli_query($con,$insertQry);
		$insert_id = mysqli_insert_id($con);			
		if($execInsert) {					
			$success = 'Category added successfully';
			header('Location:index.php?page=expense_category&success='.$success);
			exit();			
		}				
	} 
}

if(isset($_POST['update'])) {
	
	if(isset($_POST['name']) && strlen($_POST['name'])>0) {
		$name = trim($_POST['name']);
	} else {
		$error = 'Please enter category';
	}
		
	if(empty($error)) {
				
		$updateQry = "UPDATE tbl_expense_category SET name = '".$name."' WHERE id = '".$editId."'";
		$execUpdate = mysqli_query($con,$updateQry);			
		
		if($execUpdate) {
			$success = 'Category updated successfully';
			header('Location:index.php?page=expense_category&success='.$success);
			exit();			
		}				
	} 
}

$catRs = mysqli_query($con, "SELECT * FROM tbl_expense_category ORDER BY name");



?>