<?php
require_once(__DIR__."/../includes/sysinit.php");

class Finance
{
	// Properties of the class:
	var $hddnFinanceId, $edtTxnDate, $selClientId, $selTxnTypeId, $selPmId, $edtTxnAmount, $cbTxnPaid, $txtTxnNotes, 
		$selBsId;
	function __construct(){}
	function __destruct(){}

	function addFinance()
	{
		global $sableDB;
		$debit = $sableDB->getValue('select txn_debit from txn_type where txn_type_id = '.$this->selTxnTypeId);
		if (!$debit) $this->edtTxnAmount = ($this->edtTxnAmount * -1); //credit value must be negative.
		if (empty($this->hddnFinanceId))
		{
			if ($this->checkFinanceExists())
			SableSystem::ajaxResponse(false, "", "This Finance has already been added.", 
				5, "hddnFinanceId", false, "hddnFinanceId");
			$this->loadFinance();
		}
		else
		{
			$this->updateFinance();
		}
	}

	function loadFinance()
	{
		global $sableDB;
		$insertQuery = "INSERT INTO finance (
						txn_date, client_id, txn_type_id, pm_id, txn_amount, txn_paid, txn_notes, bs_id, last_update, txn_timestamp, txn_user, txn_ip
						) VALUES (
						'".$this->edtTxnDate."',
						'".$this->selClientId."',
						'".$this->selTxnTypeId."',
						'".$this->selPmId."',
						'".$this->edtTxnAmount."',
						".$this->cbTxnPaid.",
						'".$this->txtTxnNotes."',
						'".$this->selBsId."',
						'".date('Y-m-d H:i:s')."',
						'".date('Y-m-d H:i:s')."',
						'".$_SESSION['user']['user_code']."',
						'".SableGeneralFunctions::getRealIpAddr()."')";
		$res=$sableDB->updateQuery($insertQuery);
		//Check if the Finance details were successfully added:
		if ($res)
		{
			$this->setFinanceId();
			SableDBFunctions::loadAuditDetail($_SESSION['user']['user_code'], 'New Finance added: '.$this->hddnFinanceId, $this->hddnFinanceId);
			SableSystem::ajaxResponse(true, '/finance/', 'The Finance was successfully added.', 0, "", 0, "");
		}
		else
			SableSystem::ajaxResponse(false, "", "Add of a new Finance failed. Please try again or contact support.", 
				5, "hddnFinanceId", false, "hddnFinanceId");
	}

	function updateFinance()
	{
		global $sableDB;
		$code = SableSystem::sableDecode($this->hddnFinanceId);
		$updateQuery = "UPDATE finance set 
						txn_date = '".$this->edtTxnDate."', 
						client_id = '".$this->selClientId."', 
						txn_type_id = '".$this->selTxnTypeId."', 
						pm_id = '".$this->selPmId."', 
						txn_amount = '".$this->edtTxnAmount."', 
						txn_paid = ".$this->cbTxnPaid.", 
						txn_notes = '".$this->txtTxnNotes."', 
						bs_id = '".$this->selBsId."', 
						last_update = '".date('Y-m-d H:i:s')."', 
						txn_timestamp = '".date('Y-m-d H:i:s')."', 
						txn_user = '".$_SESSION['user']['user_code']."', 
						txn_ip = '".SableGeneralFunctions::getRealIpAddr()."' 
						where finance_id = '".$code."'";
		$res=$sableDB->updateQuery($updateQuery);
		//Check if the details were successfully updated:
		if ($res)
		{
			SableDBFunctions::loadAuditDetail($_SESSION['user']['user_code'], 
				'Finance details modified: '.$this->hddnFinanceId, $code);
			SableSystem::ajaxResponse(true, '/finance/', 'The Finance was successfully updated.', 0, "", 0, "");
		}
		else 
			SableSystem::ajaxResponse(false, "", "Modification of the Finance failed. Please try again or contact support.", 
				5, "hddnFinanceId", false, "hddnFinanceId");
	}

	private function getFinanceId()
	{
		global $sableDB;
		$sql = "select finance_id from finance where finance_id = '".$this->hddnFinanceId."'";
		return $sableDB->getValue($sql);
	}

	private function checkFinanceExists()
	{
		$cid = $this->getFinanceId();
		if (!empty($cid)) return true; else return false;
	}

	private function setFinanceId()
	{
		$this->hddnFinanceId = $this->getFinanceId();
	}

	function viewFinance()
	{
		$url = '/finance/view.php?'.SableSystem::sableDecode($this->hddnFinanceId);
		SableSystem::ajaxPageRedirect($url);
	}

	function editFinance()
	{
		$url = '/finance/edit.php?'.SableSystem::sableDecode($this->hddnFinanceId);
		SableSystem::ajaxPageRedirect($url);
	}

	function copyFinance()
	{
		global $sableDB;
		$code = SableSystem::sableDecode($this->hddnFinanceId);
		$newId = $sableDB->duplicateRow("finance", "finance_id", $code);
		if($newId)
		{
			SableDBFunctions::loadAuditDetail($_SESSION["user"]["user_code"], "
				'Finance record duplicated.", $code);
			SableSystem::ajaxPageRedirect("/finance/", "The Finance record was successfully duplicated.");
		}
		else
			SableSystem::ajaxResponse(false, "", "
			System error: The Finance failed to duplicate. Please try again later or contact support.", 
				5, "hddnFinanceId", false, "hddnFinanceId");
	}

	function deleteFinance()
	{
		global $sableDB;
		$code = SableSystem::sableDecode($this->hddnFinanceId);
		$cnt = $sableDB->getValue("select count(*) from finance where finance_id = ".$code);
		if ($cnt <= 0)
			SableSystem::ajaxResponse(false, "", "Delete failed - invalid Finance selected.", 
				5, "hddnFinanceId", false, "hddnFinanceId");
		else
		{
			$sql = "delete from finance where finance_id = '".$code."'";
			$res = $sableDB->updateQuery($sql);
			if ($res)
			{
				SableDBFunctions::loadAuditDetail($_SESSION["user"]["user_code"], 'Finance record deleted.', $code);
				SableSystem::ajaxPageRedirect('/finance/','The Finance record was successfully deleted.');
			}
			else
			SableSystem::ajaxResponse(false, "", "
			System error: The Finance failed to delete. Please try again later or contact support.", 
				5, "hddnFinanceId", false, "hddnFinanceId");
		}
	}

} //classFinance
?>
