<?php
session_start();
error_reporting(E_ALL);
ini_set('display_errors', 1);

global $ROOT, $PATH, $phpself;
$ROOT = $_SERVER['DOCUMENT_ROOT'];
$PATH = '../../';  
$phpself = basename(__FILE__);
require_once($PATH  .'globals/globals.php');
$adminAct = $_REQUEST['adminType']; 
$admin = 'ongoing';
$updateString = ''; 
$udID = ''; 

//$fieldArray = array('feeID','feeDescID','clientID','feeAmt','feeDesc','feeStartDate','feeRecID','feeLastRec','feeNextRec','projectID','feeActive','feeDisplay');
//$excludeArray = array('submit','projectDisp','adminType','page');

$page = (isset($_POST['page']) ? $_POST['page'] : '');
$section = (isset($_POST['section']) ? $_POST['section'] : '');
$variables = getVariables('ongoing',$page);

if (!empty($_POST['op'])) {
	$op = $_POST['op'];
	
	if ($op == 'delete') {
		$editID = $_POST['editID'];
		$query = 'UPDATE ongoingFees SET display=0 WHERE feeID='. $editID;
		$result = $con->query($query);
	} else {
		$fieldString = '';
		$valueString = '';
		foreach($_POST as $key => $value) {
			//if (($key == 'submit')||($key == 'feeActive')||($key == 'display')||($key == 'adminType')||($key == 'activateFee')) {
			if (in_array($key,$variables['excludeArray'])) {
				// Do Not Include in query
			} elseif ($key == 'op') {
				if ($op == "deact") {
					$fieldString .= 'feeActive,';
					$valueString .= '"0",';
					$fieldString .= 'display,';
					$valueString .= '"'. $_POST['display'] .'",';
				} elseif  ($op == "delete") {
					$fieldString .= 'feeActive,';
					$valueString .= '"0",';
					$fieldString .= 'display,';
					$valueString .= '"0",';
				} else  {
					if ($op == "react") {
						$fieldString .= 'feeActive,';
						$valueString .= '"1",';
						$fieldString .= 'display,';
						$valueString .= '"1",';
					} else {
						$fieldString .= 'feeActive,';
						$valueString .= '"'. $_POST['feeActive'] .'",';
						$fieldString .= 'display,';
						$valueString .= '"'. $_POST['display'] .'",';
					}
				}
			} elseif ($key == 'editID') {
				$fieldString .= 'feeID,';
				$valueString .= '"'. $value .'",';
				$udID = $value;
			} elseif ($key == 'compID') {
				$fieldString .= 'clientID,';
				$valueString .= '"'. $value .'",';
			} else {	
				$fieldString .= $key .',';
				$valueString .= '"'. $value .'",';
			}
		}
		// Remove Last comma from string to avoid mySQL errors
		$fieldString = substr($fieldString,0,strlen($fieldString)-1);
		$valueString = substr($valueString,0,strlen($valueString)-1);
		
		$query = 'REPLACE INTO ongoingFees ('. $fieldString .') VALUES ('. $valueString .')';
		$result = $con->query($query);
		$editID = (!empty($udID) ? $udID : $con->insert_id);
		//echo $query .'<br>';
		
		// Add Ongoing Fee Billing Records
		if ($_REQUEST['activateFee']) {
			if (isset($_REQUEST['editID']))
				$lastID = $_REQUEST['editID'];
			else
				$lastID = $con->insert_id;
			
			$ogfRec = getOGFrec($_REQUEST['feeRecID'],$con);
			
			$query = "INSERT INTO ongoingFeesPaid (ogfpmtID,ogfeeID,invDate,pmtDate,ppInvDate,ppInv,ppPaidDate,ppPaid) VALUES ('','". $lastID ."','". $_REQUEST['feeStartDate'] ."','0000-00-00','0000-00-00',0,'0000-00-00','0000-00-00')"; 
			$result = $con->query($query);
			//echo $query .'<br>';
			
			if ($_REQUEST['feeRecID'] == 6)
				$feeNext = addMonths($_REQUEST['feeStartDate'], 12);
			else 
				$feeNext = addMonths($_REQUEST['feeStartDate'], $ogfRec);
			
			$query = "UPDATE ongoingFees set feeLastRec='". $_REQUEST['feeStartDate'] ."', feeNextRec='". $feeNext ."', feeActive=1 WHERE feeID=". $lastID;
			$result = $con->query($query);
			//echo $query .'<br>';
		}
	}
	$updateID = (($page == 'dash') ? 'ongoing-'. $editID : 'record-'. $editID);
	$output = (($result) ? array('update'=>$updateID,'updateOP'=>$op,'updateString'=>$updateString) : 'UPDATE ERROR'); 
} else {
	$output = 'ERROR'; 	
}

if ($con) $con->close();
header('Content-Type: application/json; charset=utf-8', true); 
echo json_encode(array("output"=>$output));
	
?>