/**
  * ReceiveResponseXML() method for the QuickBooks Web Connector - Receive and handle a resonse form QuickBooks 
  * 
  * The stdClass object passed as a parameter will have the following members: 
  * 	- ->ticket 		The QuickBooks Web Connector ticket
  * 	- ->response	An XML response message
  * 	- ->hresult		Error code
  * 	- ->message		Error message
  * 
  * The sole data member of the returned object should be an integer. 
  * 	- The data member should be -1 if an error occured and QBWC should call ->getLastError()
  * 	- Should be an integer 0 <= x < 100 to indicate success *and* that the application should continue to call ->sendRequestXML() at least one more time (more queued items still in the queue, the integer represents the percentage complete the total batch job is)
  * 	- Should be 100 to indicate success *and* that the queue has been exhausted
  * 
  * The following user-defined hooks are invoked:
  * 	- QUICKBOOKS_HANDLERS_HOOK_RECEIVERESPONSEXML
  * 
  * @param stdClass $obj
  * @return QuickBooks_Result_ReceiveResponseXML
  */
 public function receiveResponseXML($obj)
 {
     $this->_driver->log('receiveResponseXML()', $obj->ticket, QUICKBOOKS_LOG_VERBOSE);
     if ($this->_driver->authCheck($obj->ticket)) {
         $user = $this->_driver->authResolve($obj->ticket);
         $hookdata = array('username' => $user, 'ticket' => $obj->ticket);
         $hookerr = '';
         $this->_callHook($obj->ticket, QUICKBOOKS_HANDLERS_HOOK_RECEIVERESPONSEXML, null, null, null, null, $hookerr, null, array(), $hookdata);
         $this->_driver->log('Incoming XML response: ' . $obj->response, $obj->ticket, QUICKBOOKS_LOG_DEBUG);
         // Check if we got a error message...
         if (strlen($obj->message) or $this->_extractStatusCode($obj->response)) {
             if ($requestID = $this->_extractRequestID($obj->response)) {
                 $errnum = $this->_extractStatusCode($obj->response);
                 //$action = current(explode('|', $requestID));
                 //$ident = next(explode('|', $requestID));
                 $action = '';
                 $ident = '';
                 $this->_parseRequestID($requestID, $action, $ident);
                 // Fetch the request that was processed and EXPERIENCED AN ERROR!
                 $extra = '';
                 if ($current = $this->_driver->queueFetch($user, $action, $ident, QUICKBOOKS_STATUS_PROCESSING)) {
                     if ($current['extra']) {
                         $extra = unserialize($current['extra']);
                     }
                 }
                 if ($obj->message) {
                     $errmsg = $obj->message;
                 } else {
                     if ($status = $this->_extractStatusMessage($obj->response)) {
                         $errmsg = $status;
                     }
                 }
                 $errerr = '';
                 $continue = $this->_handleError($obj->ticket, $errnum, $errmsg, $requestID, $action, $ident, $extra, $errerr, $obj->response, array());
                 //					$errnum, $errmsg, $requestID, $action, $ident, $extra, &$err, $xml, $qb_identifiers = array()
                 if ($errerr) {
                     // The error handler returned an error too...
                     $this->_driver->log('An error occured while handling quickbooks error ' . $errnum . ': ' . $errmsg . ': ' . $errerr, $obj->ticket, QUICKBOOKS_LOG_NORMAL);
                 }
             } else {
                 $errerr = '';
                 $continue = $this->_handleError($obj->ticket, $obj->hresult, $obj->message, null, null, null, null, $errerr, $obj->response, array());
                 if ($errerr) {
                     // The error handler returned an error too...
                     $this->_driver->log('An error occured while handling generic error ' . $obj->hresult . ': ' . $obj->message . ': ' . $errerr, $obj->ticket, QUICKBOOKS_LOG_NORMAL);
                 }
             }
             // Calculate the percentage done
             $progress = $this->_calculateProgress($obj->ticket);
             if (!$continue) {
                 $progress = -1;
             }
             $this->_driver->log('Transaction error at ' . $progress . '% complete... ', $obj->ticket, QUICKBOOKS_LOG_VERBOSE);
             return new QuickBooks_Result_ReceiveResponseXML($progress);
         }
         $action = null;
         $ident = null;
         $requestID = null;
         if ($requestID = $this->_extractRequestID($obj->response)) {
             //$action = current(explode('|', $requestID));
             //$ident = end(explode('|', $requestID));
             $action = '';
             $ident = '';
             $this->_parseRequestID($requestID, $action, $ident);
             // Fetch the request that's being processed
             $extra = '';
             if ($current = $this->_driver->queueFetch($user, $action, $ident, QUICKBOOKS_STATUS_PROCESSING)) {
                 if ($current['extra']) {
                     $extra = unserialize($current['extra']);
                 }
             }
             // Update the status to success (no error occured)
             $this->_driver->queueStatus($obj->ticket, $action, $ident, QUICKBOOKS_STATUS_SUCCESS);
         }
         // Extract ListID, TxnID, etc. from the response
         $identifiers = $this->_extractIdentifiers($obj->response);
         //$this->_driver->log(var_export($identifiers, true), $obj->ticket, QUICKBOOKS_LOG_VERBOSE);
         // Auto-map $ident unique identifier from web application to the QuickBooks ListID or TxnID
         if ($this->_config['map_application_identifiers']) {
             $adds = QuickBooks_Utilities::listActions('*Add*');
             $mods = QuickBooks_Utilities::listActions('*Mod*');
             $qbkey = QuickBooks_Utilities::keyForAction($action);
             $type = QuickBooks_Utilities::actionToObject($action);
             $EditSequence = '';
             if (isset($identifiers['EditSequence'])) {
                 $EditSequence = $identifiers['EditSequence'];
             }
             if (in_array($action, $adds) and isset($identifiers[$qbkey]) and $type) {
                 // Try to map the $ident to the QuickBooks identifier
                 $this->_driver->identMap($user, $type, $ident, $identifiers[$qbkey], $EditSequence);
             } else {
                 if (in_array($action, $mods) and isset($identifiers[$qbkey]) and $type) {
                     // Try to map the $ident to the QuickBooks identifier
                     $this->_driver->identMap($user, $type, $ident, $identifiers[$qbkey], $EditSequence);
                 }
             }
         }
         $err = null;
         $last_action_time = $this->_driver->queueActionLast($user, $action);
         $last_actionident_time = $this->_driver->queueActionIdentLast($user, $action, $ident);
         $this->_callMappedFunction(1, $user, $action, $ident, $extra, $err, $last_action_time, $last_actionident_time, $obj->response, $identifiers);
         // Calculate the percentage done
         $progress = $this->_calculateProgress($obj->ticket);
         if ($err) {
             $errerr = '';
             $continue = $this->_handleError($obj->ticket, QUICKBOOKS_ERROR_HANDLER, $err, $requestID, $action, $ident, $extra, $errerr, $obj->response, $identifiers);
             if (!$continue) {
                 $progress = -1;
             }
         }
         $this->_driver->log($progress . '% complete... ', $obj->ticket, QUICKBOOKS_LOG_VERBOSE);
         return new QuickBooks_Result_ReceiveResponseXML($progress);
     }
     return new QuickBooks_Result_ReceiveResponseXML(-1);
 }