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

class Client
{
	// Properties of the class:
	var $hddnClientId, $edtClientName, $edtClientSurname, $hddnIdTypeId, $edtClientIdNo, $hddnClientTypeId, $hddnAddressId, $edtClientAddressNote, 
		$edtClientDistance, $cbClientAddrBill, $edtClientCell, $edtClientPhone, $edtClientEmail, $txtClientNotes, $txtClientNote, $hddnGenderId, 
		$edtClientBusinessName, $edtClientVatNo, $edtClientRegNo, $cbClientContract, $hddnClientParentId, $hddnRewardLevelId, $hddnClientDeleted;

	private $sableDB, $uc, $ts, $ip;

	function __construct()
	{
		$this->sableDB = new SableDB;
	}

	function __destruct()
	{
		unset($this->sableDB);
	}

	function addClient()
	{
		$this->uc = $_SESSION['user']['user_code'];
		$this->ts = date('Y-m-d H:i:s');
		$this->ip = SableGeneralFunctions::getRealIpAddr();

	if (!SableValidationFunctions::validateEmail($this->edtClientEmail)) SableSystem::ajaxResponse(false, "", "The email address provided does not seem to be valid.", 5, "edtClientEmail", false, "edtClientEmail");
		if (empty($this->hddnClientId))
		{
			if ($this->checkClientExists())
			SableSystem::ajaxResponse(false, "", "This Client has already been added.", 
				5, "hddnRewardLevelId", false, "hddnRewardLevelId");
			$this->loadClient();
		}
		else
		{
			$this->updateClient();
		}
	}

	function loadClient()
	{
		$insertQuery = "INSERT INTO client (
						client_name, client_surname, id_type_id, client_id_no, client_type_id, address_id, client_address_note, client_addr_bill, client_cell, client_phone, client_email, client_notes, client_booking_note, gender_id, client_business_name, client_vat_no, client_reg_no, client_contract, client_parent_id, reward_level_id, client_deleted, last_update
						) VALUES (
						'".SableGeneralFunctions::formatName($this->edtClientName)."',
						'".SableGeneralFunctions::formatName($this->edtClientSurname)."',
						'{$this->hddnIdTypeId}',
						'{$this->edtClientIdNo}',
						'{$this->hddnClientTypeId}',
						'{$this->hddnAddressId}',
						'".SableGeneralFunctions::formatName($this->edtClientAddressNote)."',
						'{$this->edtClientDistance}',
						'{$this->cbClientAddrBill}',
						'".SableGeneralFunctions::formatName($this->edtClientCell)."',
						'".SableGeneralFunctions::formatName($this->edtClientPhone)."',
						'{$this->edtClientEmail}',
						'{$this->txtClientNotes}',
						'{$this->txtClientNote}',
						'{$this->hddnGenderId}',
						'".SableGeneralFunctions::formatName($this->edtClientBusinessName)."',
						'{$this->edtClientVatNo}',
						'{$this->edtClientRegNo}',
						{$this->cbClientContract},
						'{$this->hddnClientParentId}',
						'{$this->hddnRewardLevelId}',
						'{$this->hddnClientDeleted}',
						'{$this->ts}')";
		$res=$this->sableDB->updateQuery($insertQuery);
		//Check if the Client details were successfully added:
		if ($res)
		{
			$this->setClientId();
			SableDBFunctions::loadAuditDetail($this->uc, 'New Client added: '.$this->hddnRewardLevelId, $this->hddnClientId);
			SableSystem::ajaxResponse(true, '/client/', 'The Client was successfully added.', 0, "", 0, "");
		}
		else
			SableSystem::ajaxResponse(false, "", "Add of a new Client failed. Please try again or contact support.", 
				5, "hddnRewardLevelId", false, "hddnRewardLevelId");
	}

	function updateClient()
	{
		$code = SableSystem::sableDecode($this->hddnClientId);
		$updateQuery = "UPDATE client set 
						client_name = '".SableGeneralFunctions::formatName($this->edtClientName)."', 
						client_surname = '".SableGeneralFunctions::formatName($this->edtClientSurname)."', 
						id_type_id = '{$this->hddnIdTypeId}', 
						client_id_no = '{$this->edtClientIdNo}', 
						client_type_id = '{$this->hddnClientTypeId}', 
						address_id = '{$this->hddnAddressId}', 
						client_address_note = '".SableGeneralFunctions::formatName($this->edtClientAddressNote)."', 
						client_addr_bill = '{$this->cbClientAddrBill}', 
						client_cell = '".SableGeneralFunctions::formatName($this->edtClientCell)."', 
						client_phone = '".SableGeneralFunctions::formatName($this->edtClientPhone)."', 
						client_email = '{$this->edtClientEmail}', 
						client_notes = '{$this->txtClientNotes}', 
						client_booking_note = '{$this->txtClientNote}', 
						gender_id = '{$this->hddnGenderId}', 
						client_business_name = '".SableGeneralFunctions::formatName($this->edtClientBusinessName)."', 
						client_vat_no = '{$this->edtClientVatNo}', 
						client_reg_no = '{$this->edtClientRegNo}', 
						client_contract = {$this->cbClientContract}, 
						client_parent_id = '{$this->hddnClientParentId}', 
						reward_level_id = '{$this->hddnRewardLevelId}', 
						client_deleted = '{$this->hddnClientDeleted}', 
						last_update = '{$this->ts}' 
						where client_id = '{$code}'";
		$res=$this->sableDB->updateQuery($updateQuery);
		//Check if the details were successfully updated:
		if ($res)
		{
			SableDBFunctions::loadAuditDetail($this->uc, 
				'Client details modified: '.$this->hddnRewardLevelId, $code);
			SableSystem::ajaxResponse(true, '/client/', 'The Client was successfully updated.', 0, "", 0, "");
		}
		else 
			SableSystem::ajaxResponse(false, "", "Modification of the Client failed. Please try again or contact support.", 
				5, "hddnRewardLevelId", false, "hddnRewardLevelId");
	}

	private function getClientId()
	{
		$sql = "select client_id from client where client_id = '{$this->hddnClientId}'";
		return $this->sableDB->getValue($sql);
	}

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

	private function setClientId()
	{
		$this->hddnClientId = $this->getClientId();
	}

	function viewClient()
	{
		$url = '/client/view.php?'.SableSystem::sableDecode($this->hddnClientId);
		SableSystem::ajaxPageRedirect($url);
	}

	function editClient()
	{
		$url = '/client/edit.php?'.SableSystem::sableDecode($this->hddnClientId);
		SableSystem::ajaxPageRedirect($url);
	}

	function deleteClient()
	{
		$code = SableSystem::sableDecode($this->hddnClientId);
		$cnt = $this->sableDB->getValue("select count(*) from client where client_id = {$code}");
		if ($cnt <= 0)
			SableSystem::ajaxResponse(false, "", "Delete failed - invalid Client selected.", 
				5, "hddnRewardLevelId", false, "hddnRewardLevelId");
		else
		{
			$sql = "delete from client where client_id = '{$code}'";
			$res = $this->sableDB->updateQuery($sql);
			if ($res)
			{
				SableDBFunctions::loadAuditDetail($this->uc, 'Client record deleted.', $code);
				SableSystem::ajaxPageRedirect('/client/','The Client record was successfully deleted.');
			}
			else
			SableSystem::ajaxResponse(false, "", "
			System error: The Client failed to delete. Please try again later or contact support.", 
				5, "hddnRewardLevelId", false, "hddnRewardLevelId");
		}
	}

} //classClient
