<?php
/*
* date last modified 28-11-2017
* Edited by PI-Digi Logical Solutions
* HTML Files - add_user.html.php
* Mysql Tables - tbl_designation, tbl_user
*/
include('includes/imgFunctions.php');
include('includes/functions.php'); # general functions

if(isset($_REQUEST['p']) && $_REQUEST['p']!=''){
	$p = trim($_REQUEST['p']);
} else {
	$p = 1;
}

$totalPayments = 0;
$totalExpense = 0;

if(isset($_REQUEST['id']) && $_REQUEST['id']!=''){		
	$editId = mysqli_real_escape_string($con, trim($_REQUEST['id']));			
	$editRs = mysqli_query($con, "SELECT * FROM tbl_project WHERE id = '".$editId."'");
	if(mysqli_num_rows($editRs)>0){
		$resEdit = mysqli_fetch_array($editRs);
		
		//find total income
		$incomeRs = mysqli_query($con, "SELECT sum(amount) AS amount FROM tbl_project_income WHERE project_id = '".$editId."'");
		if(mysqli_num_rows($incomeRs)>0){
			$incomeRec = mysqli_fetch_array($incomeRs);
			$totalPayments = $incomeRec['amount'];
		} 
				
		//find total expense
		$expenseRs = mysqli_query($con, "SELECT sum(amount) AS amount FROM tbl_project_expense WHERE project_id = '".$editId."'");		
		if(mysqli_num_rows($expenseRs)>0){
			$expenseRec = mysqli_fetch_array($expenseRs);
			$totalExpense = $expenseRec['amount'];
		}		
		
	} else {
		$error = 'Project does not found!';
	}
	

	if(isset($_POST['upload'])){	
		
		if(isset($_FILES['quotation']) && $_FILES['quotation']['size'] > 0) {		
			if($_FILES['quotation']['type'] == 'application/pdf') {
				$quotation_name = $_FILES['quotation']['name'];			
				$quotation_temp_name = $_FILES['quotation']['tmp_name'];
				$quotation_extension = pathinfo($quotation_name, PATHINFO_EXTENSION);
				
				$photo_dir = QUOTE_FOLDER;
				$quotation_name = $editId.'_'.time().'.'.$quotation_extension; 
				if(move_uploaded_file($quotation_temp_name, $photo_dir.$quotation_name)){								
					mysqli_query($con, "UPDATE tbl_project SET quotation = '".$quotation_name."' WHERE id = ".$editId);	
					$success = 'Uploaded the quotation';				
				}				
			} else {				
				$error = "Invalid File type. Only pdf allowed";
			}
		}
		
		
		
	}
	
} 


?>