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

class PaymentMethod
{
	// Properties of the class:
	var $hddnPmId, $edtPmName;
	function __construct(){}
	function __destruct(){}

	function addPaymentMethod()
	{
		if (empty($this->hddnPmId))
		{
			if ($this->checkPaymentMethodExists())
			SableSystem::ajaxResponse(false, "", "This Payment Method has already been added.", 
				5, "edtPmName", false, "edtPmName");
			$this->loadPaymentMethod();
		}
		else
		{
			$this->updatePaymentMethod();
		}
	}

	function loadPaymentMethod()
	{
		global $sableDB;
		$insertQuery = "INSERT INTO payment_method (
						pm_name
						) VALUES (
						'".addslashes($this->edtPmName)."')";
		$res=$sableDB->updateQuery($insertQuery);
		//Check if the Payment Method details were successfully added:
		if ($res)
		{
			$this->setPaymentMethodId();
			SableDBFunctions::loadAuditDetail($_SESSION['user']['user_code'], 'New Payment Method added: '.$this->edtPmName, $this->hddnPmId);
			SableSystem::ajaxResponse(true, '/payment_method/', 'The Payment Method was successfully added.', 0, "", 0, "");
		}
		else
			SableSystem::ajaxResponse(false, "", "Add of a new Payment Method failed. Please try again or contact support.", 
				5, "edtPmName", false, "edtPmName");
	}

	function updatePaymentMethod()
	{
		global $sableDB;
		$code = SableSystem::sableDecode($this->hddnPmId);
		$updateQuery = "UPDATE payment_method set 
						pm_name = '".addslashes($this->edtPmName)."' 
						where pm_id = '".$code."'";
		$res=$sableDB->updateQuery($updateQuery);
		//Check if the details were successfully updated:
		if ($res)
		{
			SableDBFunctions::loadAuditDetail($_SESSION['user']['user_code'], 
				'Payment Method details modified: '.$this->edtPmName, $code);
			SableSystem::ajaxResponse(true, '/payment_method/', 'The Payment Method was successfully updated.', 0, "", 0, "");
		}
		else 
			SableSystem::ajaxResponse(false, "", "Modification of the Payment Method failed. Please try again or contact support.", 
				5, "edtPmName", false, "edtPmName");
	}

	private function getPaymentMethodId()
	{
		global $sableDB;
		$sql = "select pm_id from payment_method where pm_id = '".$this->hddnPmId."'";
		return $sableDB->getValue($sql);
	}

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

	private function setPaymentMethodId()
	{
		$this->hddnPmId = $this->getPaymentMethodId();
	}

	function viewPaymentMethod()
	{
		$url = '/payment_method/view.php?'.SableSystem::sableDecode($this->hddnPmId);
		SableSystem::ajaxPageRedirect($url);
	}

	function editPaymentMethod()
	{
		$url = '/payment_method/edit.php?'.SableSystem::sableDecode($this->hddnPmId);
		SableSystem::ajaxPageRedirect($url);
	}

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

} //classPaymentMethod
?>
