Aaron Dressler Aaron Dressler - 1 year ago 101
PHP Question

Consolibyte Quickbooks PHP SDK (web connector) - Repeated Tasks

I'm using the Consolibyte QB SDK found here, and for the most part is working very well.

However, I have a situation where I need to check if any customers, estimates, and sales orders have been created or updated in Quickbooks each time QB reports to my server. If so, upload the new/updated ones to the server.

I remember seeing something about scheduled tasks (or repeated tasks, or something of that nature) in the docs, but I cannot find it now.

I don't think I want to bother with Quickbooks SQL Mirroring... seems like overkill. Can someone point me in the right direction?

Answer Source

Here is an example of how to do what you're describing:

In summary:

Register a login success hook - this is a function that gets called every time the Web Connector starts a new sync session.

// An array of callback hooks
$hooks = array(
    QuickBooks_WebConnector_Handlers::HOOK_LOGINSUCCESS => '_quickbooks_hook_loginsuccess',     // call this whenever a successful login occurs
    );

https://github.com/consolibyte/quickbooks-php/blob/master/docs/web_connector/example_web_connector_import.php#L126

Use the function to queue up a request to query for anything new.

function _quickbooks_hook_loginsuccess($requestID, $user, $hook, &$err, $hook_data, $callback_config)
{
    // For new users, we need to set up a few things
    // Fetch the queue instance
    $Queue = QuickBooks_WebConnector_Queue_Singleton::getInstance();
    $date = '1983-01-02 12:01:01';

    // Do the same for customers
    if (!_quickbooks_get_last_run($user, QUICKBOOKS_IMPORT_CUSTOMER))
    {
        _quickbooks_set_last_run($user, QUICKBOOKS_IMPORT_CUSTOMER, $date);
    }

    $Queue->enqueue(QUICKBOOKS_IMPORT_CUSTOMER, 1, QB_PRIORITY_CUSTOMER);
}

https://github.com/consolibyte/quickbooks-php/blob/master/docs/web_connector/example_web_connector_import.php#L219

Write your request/response handlers to create a qbXML query that asks for modified objects:

function _quickbooks_customer_import_request($requestID, $user, $action, $ID, $extra, &$err, $last_action_time, $last_actionident_time, $version, $locale)
{
    // Iterator support (break the result set into small chunks)
    $attr_iteratorID = '';
    $attr_iterator = ' iterator="Start" ';
    if (empty($extra['iteratorID']))
    {
        // This is the first request in a new batch
        $last = _quickbooks_get_last_run($user, $action);
        _quickbooks_set_last_run($user, $action);           // Update the last run time to NOW()

        // Set the current run to $last
        _quickbooks_set_current_run($user, $action, $last);
    }
    else
    {
        // This is a continuation of a batch
        $attr_iteratorID = ' iteratorID="' . $extra['iteratorID'] . '" ';
        $attr_iterator = ' iterator="Continue" ';

        $last = _quickbooks_get_current_run($user, $action);
    }

    // Build the request
    $xml = '<?xml version="1.0" encoding="utf-8"?>
        <?qbxml version="' . $version . '"?>
        <QBXML>
            <QBXMLMsgsRq onError="stopOnError">
                <CustomerQueryRq ' . $attr_iterator . ' ' . $attr_iteratorID . ' requestID="' . $requestID . '">
                    <MaxReturned>20</MaxReturned>
                    <FromModifiedDate>' . $last . '</FromModifiedDate>
                    <OwnerID>0</OwnerID>
                </CustomerQueryRq>  
            </QBXMLMsgsRq>
        </QBXML>';

    return $xml;
}
/** 
 * Handle a response from QuickBooks 
 */
function _quickbooks_customer_import_response($requestID, $user, $action, $ID, $extra, &$err, $last_action_time, $last_actionident_time, $xml, $idents)
{   
    if (!empty($idents['iteratorRemainingCount']))
    {
        // Queue up another request

        $Queue = QuickBooks_WebConnector_Queue_Singleton::getInstance();
        $Queue->enqueue(QUICKBOOKS_IMPORT_CUSTOMER, null, QB_PRIORITY_CUSTOMER, array( 'iteratorID' => $idents['iteratorID'] ));
    }

    ... handle the XML blob from QuickBooks here ... 

    return true;
}

https://github.com/consolibyte/quickbooks-php/blob/master/docs/web_connector/example_web_connector_import.php#L472