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

class NetcashPayment
{
	// Properties of the class:
	var $hddnNpId, $selNpUser, $edtNpDate, $edtNpDescription, $edtNpAmount, $selNpPaymentType;
	function __construct(){}
	function __destruct(){}

	function addNetcashPayment()
	{
		if (empty($this->hddnNpId))
		{
			if ($this->checkNetcashPaymentExists())
			SableSystem::ajaxResponse(false, "", "This Netcash Payment has already been added.", 
				5, "edtNpDescription", false, "edtNpDescription");
			$this->loadNetcashPayment();
		}
		else
		{
			$this->updateNetcashPayment();
		}
	}

	function loadNetcashPayment()
	{
		global $sableDB;
		$insertQuery = "INSERT INTO netcash_payment (
						np_user, np_date, np_description, np_amount, np_payment_type
						) VALUES (
						'".$this->selNpUser."',
						'".$this->edtNpDate."',
						'".addslashes($this->edtNpDescription)."',
						'".$this->edtNpAmount."',
						'".$this->selNpPaymentType."')";
		$res=$sableDB->updateQuery($insertQuery);
		//Check if the Netcash Payment details were successfully added:
		if ($res)
		{
			$this->setNetcashPaymentId();
			SableDBFunctions::loadAuditDetail($_SESSION['user']['user_code'], 'New Netcash Payment added: '.$this->edtNpDescription, $this->hddnNpId);
			SableSystem::ajaxResponse(true, '/netcash_payment/', 'The Netcash Payment was successfully added.', 0, "", 0, "");
		}
		else
			SableSystem::ajaxResponse(false, "", "Add of a new Netcash Payment failed. Please try again or contact support.", 
				5, "edtNpDescription", false, "edtNpDescription");
	}

	function updateNetcashPayment()
	{
		global $sableDB;
		$code = SableSystem::sableDecode($this->hddnNpId);
		$updateQuery = "UPDATE netcash_payment set 
						np_user = '".$this->selNpUser."', 
						np_date = '".$this->edtNpDate."', 
						np_description = '".addslashes($this->edtNpDescription)."', 
						np_amount = '".$this->edtNpAmount."', 
						np_payment_type = '".$this->selNpPaymentType."' 
						where np_id = '".$code."'";
		$res=$sableDB->updateQuery($updateQuery);
		//Check if the details were successfully updated:
		if ($res)
		{
			SableDBFunctions::loadAuditDetail($_SESSION['user']['user_code'], 
				'Netcash Payment details modified: '.$this->edtNpDescription, $code);
			SableSystem::ajaxResponse(true, '/netcash_payment/', 'The Netcash Payment was successfully updated.', 0, "", 0, "");
		}
		else 
			SableSystem::ajaxResponse(false, "", "Modification of the Netcash Payment failed. Please try again or contact support.", 
				5, "edtNpDescription", false, "edtNpDescription");
	}

	private function getNetcashPaymentId()
	{
		global $sableDB;
		$sql = "select np_id from netcash_payment where np_id = '".$this->hddnNpId."'";
		return $sableDB->getValue($sql);
	}

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

	private function setNetcashPaymentId()
	{
		$this->hddnNpId = $this->getNetcashPaymentId();
	}

	function viewNetcashPayment()
	{
		$url = '/netcash_payment/view.php?'.SableSystem::sableDecode($this->hddnNpId);
		SableSystem::ajaxPageRedirect($url);
	}

	function editNetcashPayment()
	{
		$url = '/netcash_payment/edit.php?'.SableSystem::sableDecode($this->hddnNpId);
		SableSystem::ajaxPageRedirect($url);
	}

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

} //classNetcashPayment
?>
