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

class TransactionType
{
	// Properties of the class:
	var $hddnTxnTypeId, $edtTxnTypeName, $cbTxnDebit;
	function __construct(){}
	function __destruct(){}

	function addTransactionType()
	{
		if (empty($this->edtTxnTypeName))
		{
			if ($this->checkTransactionTypeExists())
			SableSystem::ajaxResponse(false, "", "This Transaction Type has already been added.", 
				5, "edtTxnTypeName", false, "edtTxnTypeName");
			$this->loadTransactionType();
		}
		else
		{
			$this->updateTransactionType();
		}
	}

	function loadTransactionType()
	{
		global $sableDB;
		$insertQuery = "INSERT INTO txn_type (
						txn_type_id, txn_debit, txn_type_ip, txn_type_timestamp, txn_type_user, last_update
						) VALUES (
						'".$this->hddnTxnTypeId."',
						".$this->cbTxnDebit.",
						'".SableGeneralFunctions::getRealIpAddr()."',
						'".date('Y-m-d H:i:s')."',
						'".$_SESSION['user']['user_code']."',
						'".date('Y-m-d H:i:s')."')";
		$res=$sableDB->updateQuery($insertQuery);
		//Check if the Transaction Type details were successfully added:
		if ($res)
		{
			$this->setTransactionTypeId();
			SableDBFunctions::loadAuditDetail($_SESSION['user']['user_code'], 'New Transaction Type added: '.$this->edtTxnTypeName, $this->edtTxnTypeName);
			SableSystem::ajaxResponse(true, '/transaction_type/', 'The Transaction Type was successfully added.', 0, "", 0, "");
		}
		else
			SableSystem::ajaxResponse(false, "", "Add of a new Transaction Type failed. Please try again or contact support.", 
				5, "edtTxnTypeName", false, "edtTxnTypeName");
	}

	function updateTransactionType()
	{
		global $sableDB;
		$code = SableSystem::sableDecode($this->edtTxnTypeName);
		$updateQuery = "UPDATE txn_type set 
						txn_type_id = '".$this->hddnTxnTypeId."', 
						txn_debit = ".$this->cbTxnDebit.", 
						txn_type_ip = '".SableGeneralFunctions::getRealIpAddr()."', 
						txn_type_timestamp = '".date('Y-m-d H:i:s')."', 
						txn_type_user = '".$_SESSION['user']['user_code']."', 
						last_update = '".date('Y-m-d H:i:s')."' 
						where txn_type_name = '".$code."'";
		$res=$sableDB->updateQuery($updateQuery);
		//Check if the details were successfully updated:
		if ($res)
		{
			SableDBFunctions::loadAuditDetail($_SESSION['user']['user_code'], 
				'Transaction Type details modified: '.$this->edtTxnTypeName, $code);
			SableSystem::ajaxResponse(true, '/transaction_type/', 'The Transaction Type was successfully updated.', 0, "", 0, "");
		}
		else 
			SableSystem::ajaxResponse(false, "", "Modification of the Transaction Type failed. Please try again or contact support.", 
				5, "edtTxnTypeName", false, "edtTxnTypeName");
	}

	private function getTransactionTypeId()
	{
		global $sableDB;
		$sql = "select txn_type_name from txn_type where txn_type_name = '".$this->edtTxnTypeName."'";
		return $sableDB->getValue($sql);
	}

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

	private function setTransactionTypeId()
	{
		$this->edtTxnTypeName = $this->getTransactionTypeId();
	}

	function viewTransactionType()
	{
		$url = '/transaction_type/view.php?'.SableSystem::sableDecode($this->edtTxnTypeName);
		SableSystem::ajaxPageRedirect($url);
	}

	function editTransactionType()
	{
		$url = '/transaction_type/edit.php?'.SableSystem::sableDecode($this->edtTxnTypeName);
		SableSystem::ajaxPageRedirect($url);
	}

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

} //classTransactionType
?>
