<?
if(!defined('IN_BANDTOPIA')) {
	exit ;
}

class AccountObj {

	/*************************************************************/
	/* List Users
	 /*************************************************************/
	public function AccountList() {
		//////////////////////////////////////////////////////////
		//  Todo:
		// 		Add dynamic sorting
		//		Add dynamic filtering
		//////////////////////////////////////////////////////////
		$query = <<<EOD
			SELECT
				*
			FROM
				Accounts a
				INNER JOIN AccountTypes at
					on at.AccountTypeId = a.AccountTypeId
				INNER JOIN Statuses s
					on s.StatusId = a.StatusId
EOD;
		$result = mysql_query($query) or die(mysql_error() . "<br>$query");
		while($row = mysql_fetch_assoc($result)) {
			$tempObj = new AccountObj();
			$tempObj -> AccountId = $row['AccountId'];
			$tempObj -> AccountName = $row['AccountName'];
			$tempObj -> AccountTypeId = $row['AccountTypeId'];
			$tempObj -> BillingName = $row['BillingName'];
			$tempObj -> BillingAddr = $row['BillingAddr'];
			$tempObj -> BillingCity = $row['BillingCity'];
			$tempObj -> BillingSt = $row['BillingSt'];
			$tempObj -> BillingZip = $row['BillingZip'];
			$tempObj -> BillingPhone = $row['BillingPhone'];
			$tempObj -> BillingEmail = $row['BillingEmail'];
			$tempObj -> StatusId = $row['StatusId'];
			$tempObj -> StatusDetail = $row['StatusDetail'];
			$tempObj -> Notes = $row['Notes'];
			$tempObj -> DateAdded = strtotime($row['DateAdded']);
			$tempObj -> AddedByUserId = $row['AddedByUserId'];
			$tempObj -> DateLastModified = strtotime($row['DateLastModified']);
			$tempObj -> LastModifiedByUserId = $row['LastModifiedByUserId'];

			$accountArray[] = $tempObj;
		}
		return $accountArray;
	}

	/*************************************************************/
	/* Get Account
	 /*************************************************************/
	public function GetAccount($AccountId) {
		$query = <<<EOD
			SELECT
				a.*
			FROM
				Accounts a
				INNER JOIN AccountTypes at
					ON at.AccountTypeId = a.AccountTypeId
				INNER JOIN Statuses s
					ON s.StatusId = a.StatusId
			WHERE
				a.AccountId = {$AccountId}
EOD;
		$result = mysql_query($query) or die(mysql_error() . "<br>$query");
		$row = mysql_fetch_assoc($result);
		foreach ($row as $key => $value)
		{
			$row[$key] = stripslashes($value);
		}
		$this -> AccountId = $row['AccountId'];
		$this -> AccountName = $row['AccountName'];
		$this -> AccountTypeId = $row['AccountTypeId'];
		$this -> BillingName = $row['BillingName'];
		$this -> BillingAddr = $row['BillingAddr'];
		$this -> BillingCity = $row['BillingCity'];
		$this -> BillingSt = $row['BillingSt'];
		$this -> BillingZip = $row['BillingZip'];
		$this -> BillingPhone = $row['BillingPhone'];
		$this -> BillingEmail = $row['BillingEmail'];
		$this -> StatusId = $row['StatusId'];
		$this -> StatusDetail = $row['StatusDetail'];
		$this -> Notes = $row['Notes'];
		$this -> DateAdded = strtotime($row['DateAdded']);
		$this -> AddedByUserId = $row['AddedByUserId'];
		$this -> DateLastModified = strtotime($row['DateLastModified']);
		$this -> LastModifiedByUserId = $row['LastModifiedByUserId'];
	}

	/*************************************************************/
	/* Update Account
	 /*************************************************************/
	public function updateAccount() {
		if($this->AccountId != '') {
			$query = <<<EOD
				UPDATE
					Accounts
				SET
					AccountTypeId = {$this->AccountTypeId},
					AccountName = '{$this->AccountName}',
					BillingName = '{$this->BillingName}',
					BillingAddr = '{$this->BillingAddr}',
					BillingCity = '{$this->BillingCity}',
					BillingSt = '{$this->BillingSt}',
					BillingZip = '{$this->BillingZip}',
					BillingPhone = '{$this->BillingPhone}',
					BillingEmail = '{$this->BillingEmail}',
					StatusId = $this->StatusId,
					StatusDetail = '{$this->StatusDetail}',
					Notes = '{$this->Notes}',
					DateLastModified = Now(),
					LastModifiedByUserId = {$_SESSION['UserId']}
				WHERE
					AccountId = {$this->AccountId}
EOD;
			$result = mysql_query($query);
			$accountMsg = "Account information has successfully updated.";
		} else {
			$crypt = new cryptRoutineObj();
			$password = $crypt->EncryptPass($this->int_Password);
			$query = <<<EOD
				INSERT INTO
					Accounts
					(AccountTypeId,
					AccountName,
					BillingName,
					BillingAddr,
					BillingCity,
					BillingSt,
					BillingZip,
					BillingPhone,
					BillingEmail,
					StatusId,
					StatusDetail,
					Notes,
					DateAdded,
					AddedByUserId,
					DateLastModified,
					LastModifiedByUserId)
				VALUES
					({$this->AccountTypeId},
					'{$this->AccountName}',
					'{$this->BillingName}',
					'{$this->BillingAddr}',
					'{$this->BillingCity}',
					'{$this->BillingSt}',
					'{$this->BillingZip}',
					'{$this->BillingPhone}',
					'{$this->BillingEmail}',
					$this->StatusId,
					'{$this->StatusDetail}',
					'{$this->Notes}',
					Now(),
					{$_SESSION['UserId']},
					Now(),
					{$_SESSION['UserId']})
EOD;
			$result = mysql_query($query);
			$this -> AccountId = mysql_insert_id();
			$accountMsg = "Account has been added successfully.";
		}
		if(mysql_error() != '') {
			$accountMsg = "An error has occurred while attempting your request.  Please try again.  If you continue to experience problems, please contact the site administrator<br>";
			$accountMsg .= mysql_error() . "<br>$query";
		}
		return $accountMsg;
	}

	/*************************************************************/
	/* Delete(Deactivate) User
	 /*************************************************************/
	public function delUser() {
		$query = <<<EOD
			UPDATE
				users
			SET
				user_active = 0
			WHERE
				UserId = {$this->int_UserId}
EOD;
		$userMsg = "User has successfully been removed.  If this was done in error, please contact your website administrator.";
		$result = mysql_query($query);
		if(mysql_error() != '') {
			$userMsg = "An error has occurred while attempting your request.  Please try again.  If you continue to experience problems, please contact the site administrator<br>";
			$userMsg .= mysql_error() . "<br>$query";
		}
		return $userMsg;
	}
}
?>