QuickBooks/Driver/Sql.php
protected function _queueStatus($ticket, $requestID, $new_status, $msg = null)
	{
		$errnum = 0;
		$errmsg = '';

		if ($ticket_id = $this->_ticketResolve($ticket))
		{
			$user = $this->authResolve($ticket);

			//print('action: ' . $action . ', ident: ' . $ident . ', new status: ' . $new_status . ', ticket_id: ' . $ticket_id . ', user: ' . $user . ', msg: ' . $msg);

			if ($new_status == QUICKBOOKS_STATUS_SUCCESS)
			{
				$this->_query("
					UPDATE
						" . $this->_mapTableName(QUICKBOOKS_DRIVER_SQL_TICKETTABLE) . "
					SET
						processed = processed + 1,
						lasterror_num = NULL,
						lasterror_msg = NULL
					WHERE
						quickbooks_ticket_id = " . (int) $ticket_id . " ", $errnum, $errmsg);
			}

			if ($new_status == QUICKBOOKS_STATUS_PROCESSING)
			{
				/*
				$this->_query("
					UPDATE
						" . $this->_mapTableName(QUICKBOOKS_DRIVER_SQL_QUEUETABLE) . "
					SET
						qb_status = '" . $this->_escape($new_status) . "',
						msg = '" . $this->_escape($msg) . "',
						quickbooks_ticket_id = " . (int) $ticket_id . ",
						dequeue_datetime = '" . date('Y-m-d H:i:s') . "'
					WHERE
						qb_username = '" . $this->_escape($user) . "' AND
						qb_action = '" . $this->_escape($action) . "' AND
						ident = '" . $this->_escape($ident) . "' AND
						qb_status = '" . QUICKBOOKS_STATUS_QUEUED . "' ", $errnum, $errmsg, 0, 1);
				*/

				$this->_query("
					UPDATE
						" . $this->_mapTableName(QUICKBOOKS_DRIVER_SQL_QUEUETABLE) . "
					SET
						qb_status = '" . $this->_escape($new_status) . "',
						msg = '" . $this->_escape($msg) . "',
						quickbooks_ticket_id = " . (int) $ticket_id . ",
						dequeue_datetime = '" . date('Y-m-d H:i:s') . "'
					WHERE
						quickbooks_queue_id = " . (int) $requestID . " AND
						qb_username = '" . $this->_escape($user) . "' AND
						qb_status = '" . $this->_escape(QUICKBOOKS_STATUS_QUEUED) . "' ", $errnum, $errmsg, null, null);

				//print('running processing status query! ' . $user . ', ' . $action . ', ' . $ident . ', new: ' . $new_status);

				// If we're currently processing, then no error is occuring...
				$errnum = null;
				$errmsg = null;
				$this->_query("
					UPDATE
						" . $this->_mapTableName(QUICKBOOKS_DRIVER_SQL_TICKETTABLE) . "
					SET
						lasterror_num = NULL,
						lasterror_msg = NULL
					WHERE
						quickbooks_ticket_id = " . (int) $ticket_id, $errnum, $errmsg, null, null);
			}
			else if ($new_status == QUICKBOOKS_STATUS_SUCCESS)
			{
				// You can only update to a SUCCESS status if you're currently
				//	in a PROCESSING status

				/*
				$sql = "
					UPDATE
						" . $this->_mapTableName(QUICKBOOKS_DRIVER_SQL_QUEUETABLE) . "
					SET
						qb_status = '" . $this->_escape($new_status) . "',
						msg = '" . $this->_escape($msg) . "'
					WHERE
						quickbooks_ticket_id = " . (int) $ticket_id . " AND
						qb_username = '" . $this->_escape($user) . "' AND
						qb_action = '" . $this->_escape($action) . "' AND
						ident = '" . $this->_escape($ident) . "' AND
						qb_status = '" . QUICKBOOKS_STATUS_PROCESSING . "' ";
				*/

				$sql = "
					UPDATE
						" . $this->_mapTableName(QUICKBOOKS_DRIVER_SQL_QUEUETABLE) . "
					SET
						qb_status = '" . $this->_escape($new_status) . "',
						msg = '" . $this->_escape($msg) . "'
					WHERE
						quickbooks_ticket_id = " . (int) $ticket_id . " AND
						qb_username = '" . $this->_escape($user) . "' AND
						quickbooks_queue_id = " . (int) $requestID . " AND
						qb_status = '" . QUICKBOOKS_STATUS_PROCESSING . "' ";

				$this->_query($sql, $errnum, $errmsg, null, null);
			}
			else
			{
				// There are some statuses which *can not be updated* because
				//	they're already removed from the queue. These are listed in
				//	the NOT IN section

				/*
				$sql = "
					UPDATE
						" . $this->_mapTableName(QUICKBOOKS_DRIVER_SQL_QUEUETABLE) . "
					SET
						qb_status = '" . $this->_escape($new_status) . "',
						msg = '" . $this->_escape($msg) . "'
					WHERE
						quickbooks_ticket_id = " . (int) $ticket_id . " AND
						qb_username = '" . $this->_escape($user) . "' AND
						qb_action = '" . $this->_escape($action) . "' AND
						ident = '" . $this->_escape($ident) . "' AND
						qb_status NOT IN (
							'" . QUICKBOOKS_STATUS_SUCCESS . "',
							'" . QUICKBOOKS_STATUS_HANDLED . "',
							'" . QUICKBOOKS_STATUS_CANCELLED . "',
							'" . QUICKBOOKS_STATUS_REMOVED . "' ) ";
				*/

				$sql = "
					UPDATE
						" . $this->_mapTableName(QUICKBOOKS_DRIVER_SQL_QUEUETABLE) . "
					SET
						qb_status = '" . $this->_escape($new_status) . "',
						msg = '" . $this->_escape($msg) . "'
					WHERE
						quickbooks_ticket_id = " . (int) $ticket_id . " AND
						qb_username = '" . $this->_escape($user) . "' AND
						quickbooks_queue_id = " . (int) $requestID . " AND
						qb_status NOT IN (
							'" . QUICKBOOKS_STATUS_SUCCESS . "',
							'" . QUICKBOOKS_STATUS_HANDLED . "',
							'" . QUICKBOOKS_STATUS_CANCELLED . "',
							'" . QUICKBOOKS_STATUS_REMOVED . "' ) ";

				$this->_query($sql, $errnum, $errmsg, null, null);

				// If that got marked as a NoOp, we should also remove the NoOp
				//	status from the quickbooks_ticket table, or we can get stuck
				//	in an infinite loop (we're all done, last request returns a
				//	no op, get last error is called, returns no op, send request
				//	is called and returns a no op because there's nothing to do,
				//	get last error is called and retuns a no op, etc. etc. etc.
				/*
				if ($new_status == QUICKBOOKS_STATUS_NOOP)
				{
					$errnum = null;
					$errmsg = null;
					$this->_query("
						UPDATE
							" . $this->_mapTableName(QUICKBOOKS_DRIVER_SQL_TICKETTABLE) . "
						SET
							lasterror_num = NULL,
							lasterror_msg = NULL
						WHERE
							quickbooks_ticket_id = " . (int) $ticket_id, $errnum, $errmsg, 0, 1);
				}*/
			}

			return true;
		}

		return false;
	}
protected function _queueStatus($ticket, $requestID, $new_status, $msg = null)
	{
		$errnum = 0;
		$errmsg = '';

		if ($ticket_id = $this->_ticketResolve($ticket))
		{
			$user = $this->authResolve($ticket);

			//print('action: ' . $action . ', ident: ' . $ident . ', new status: ' . $new_status . ', ticket_id: ' . $ticket_id . ', user: ' . $user . ', msg: ' . $msg);

			if ($new_status == QUICKBOOKS_STATUS_SUCCESS)
			{
				$this->_query("
					UPDATE
						" . $this->_mapTableName(QUICKBOOKS_DRIVER_SQL_TICKETTABLE) . "
					SET
						processed = processed + 1,
						lasterror_num = NULL,
						lasterror_msg = NULL
					WHERE
						quickbooks_ticket_id = " . (int) $ticket_id . " ", $errnum, $errmsg);
			}

			if ($new_status == QUICKBOOKS_STATUS_PROCESSING)
			{
				/*
				$this->_query("
					UPDATE
						" . $this->_mapTableName(QUICKBOOKS_DRIVER_SQL_QUEUETABLE) . "
					SET
						qb_status = '" . $this->_escape($new_status) . "',
						msg = '" . $this->_escape($msg) . "',
						quickbooks_ticket_id = " . (int) $ticket_id . ",
						dequeue_datetime = '" . date('Y-m-d H:i:s') . "'
					WHERE
						qb_username = '" . $this->_escape($user) . "' AND
						qb_action = '" . $this->_escape($action) . "' AND
						ident = '" . $this->_escape($ident) . "' AND
						qb_status = '" . QUICKBOOKS_STATUS_QUEUED . "' ", $errnum, $errmsg, 0, 1);
				*/

				$this->_query("
					UPDATE
						" . $this->_mapTableName(QUICKBOOKS_DRIVER_SQL_QUEUETABLE) . "
					SET
						qb_status = '" . $this->_escape($new_status) . "',
						msg = '" . $this->_escape($msg) . "',
						quickbooks_ticket_id = " . (int) $ticket_id . ",
						dequeue_datetime = '" . date('Y-m-d H:i:s') . "'
					WHERE
						quickbooks_queue_id = " . (int) $requestID . " AND
						qb_username = '" . $this->_escape($user) . "' AND
						qb_status = '" . $this->_escape(QUICKBOOKS_STATUS_QUEUED) . "' ", $errnum, $errmsg, null, null);

				//print('running processing status query! ' . $user . ', ' . $action . ', ' . $ident . ', new: ' . $new_status);

				// If we're currently processing, then no error is occuring...
				$errnum = null;
				$errmsg = null;
				$this->_query("
					UPDATE
						" . $this->_mapTableName(QUICKBOOKS_DRIVER_SQL_TICKETTABLE) . "
					SET
						lasterror_num = NULL,
						lasterror_msg = NULL
					WHERE
						quickbooks_ticket_id = " . (int) $ticket_id, $errnum, $errmsg, null, null);
			}
			else if ($new_status == QUICKBOOKS_STATUS_SUCCESS)
			{
				// You can only update to a SUCCESS status if you're currently
				//	in a PROCESSING status

				/*
				$sql = "
					UPDATE
						" . $this->_mapTableName(QUICKBOOKS_DRIVER_SQL_QUEUETABLE) . "
					SET
						qb_status = '" . $this->_escape($new_status) . "',
						msg = '" . $this->_escape($msg) . "'
					WHERE
						quickbooks_ticket_id = " . (int) $ticket_id . " AND
						qb_username = '" . $this->_escape($user) . "' AND
						qb_action = '" . $this->_escape($action) . "' AND
						ident = '" . $this->_escape($ident) . "' AND
						qb_status = '" . QUICKBOOKS_STATUS_PROCESSING . "' ";
				*/

				$sql = "
					UPDATE
						" . $this->_mapTableName(QUICKBOOKS_DRIVER_SQL_QUEUETABLE) . "
					SET
						qb_status = '" . $this->_escape($new_status) . "',
						msg = '" . $this->_escape($msg) . "'
					WHERE
						quickbooks_ticket_id = " . (int) $ticket_id . " AND
						qb_username = '" . $this->_escape($user) . "' AND
						quickbooks_queue_id = " . (int) $requestID . " AND
						qb_status = '" . QUICKBOOKS_STATUS_PROCESSING . "' ";

				$this->_query($sql, $errnum, $errmsg, null, null);
			}
			else
			{
				// There are some statuses which *can not be updated* because
				//	they're already removed from the queue. These are listed in
				//	the NOT IN section

				/*
				$sql = "
					UPDATE
						" . $this->_mapTableName(QUICKBOOKS_DRIVER_SQL_QUEUETABLE) . "
					SET
						qb_status = '" . $this->_escape($new_status) . "',
						msg = '" . $this->_escape($msg) . "'
					WHERE
						quickbooks_ticket_id = " . (int) $ticket_id . " AND
						qb_username = '" . $this->_escape($user) . "' AND
						qb_action = '" . $this->_escape($action) . "' AND
						ident = '" . $this->_escape($ident) . "' AND
						qb_status NOT IN (
							'" . QUICKBOOKS_STATUS_SUCCESS . "',
							'" . QUICKBOOKS_STATUS_HANDLED . "',
							'" . QUICKBOOKS_STATUS_CANCELLED . "',
							'" . QUICKBOOKS_STATUS_REMOVED . "' ) ";
				*/

				$sql = "
					UPDATE
						" . $this->_mapTableName(QUICKBOOKS_DRIVER_SQL_QUEUETABLE) . "
					SET
						qb_status = '" . $this->_escape($new_status) . "',
						msg = '" . $this->_escape($msg) . "'
					WHERE
						quickbooks_ticket_id = " . (int) $ticket_id . " AND
						qb_username = '" . $this->_escape($user) . "' AND
						quickbooks_queue_id = " . (int) $requestID . " AND
						qb_status NOT IN (
							'" . QUICKBOOKS_STATUS_SUCCESS . "',
							'" . QUICKBOOKS_STATUS_HANDLED . "',
							'" . QUICKBOOKS_STATUS_CANCELLED . "',
							'" . QUICKBOOKS_STATUS_REMOVED . "' ) ";

				$this->_query($sql, $errnum, $errmsg, null, null);

				// If that got marked as a NoOp, we should also remove the NoOp
				//	status from the quickbooks_ticket table, or we can get stuck
				//	in an infinite loop (we're all done, last request returns a
				//	no op, get last error is called, returns no op, send request
				//	is called and returns a no op because there's nothing to do,
				//	get last error is called and retuns a no op, etc. etc. etc.
				/*
				if ($new_status == QUICKBOOKS_STATUS_NOOP)
				{
					$errnum = null;
					$errmsg = null;
					$this->_query("
						UPDATE
							" . $this->_mapTableName(QUICKBOOKS_DRIVER_SQL_TICKETTABLE) . "
						SET
							lasterror_num = NULL,
							lasterror_msg = NULL
						WHERE
							quickbooks_ticket_id = " . (int) $ticket_id, $errnum, $errmsg, 0, 1);
				}*/
			}

			return true;
		}

		return false;
	}
protected function _queueStatus($ticket, $action, $ident, $new_status, $msg = null)