在使用Consolibyte / QB PHP集成更新mysql表时,是否可以跳过已在QuickBooks数据库中添加的现有客户?

时间:2014-02-19 14:59:52

标签: php mysql sql quickbooks

我正在使用Consolibyte的PHP / QB集成在本地计算机上插入/更新客户到QB。

当我最初运行PHP脚本以将信息从mysql DB插入QB时, 一切都经过 - 太棒了。

但是,当我将新客户添加到mysql数据库并再次运行consolibyte脚本时,我收到以下错误:

描述: 通过getLastError()从应用程序收到的错误消息:3100:列表元素的名称“John Smith”已在使用中。

我知道我不能插入重复的名称,但有没有办法告诉脚本跳过QB数据库中已有的名称,而不是因为重复错误而中止脚本?

根据文档,我可以将唯一标识符传递给QB并使其唯一,但我的目标是跳过整个过程

http://www.consolibyte.com/wiki/doku.php/quickbooks_error_codes

FUNCTION.PHP

function _quickbooks_customer_add_request($requestID, $user, $action, $ID, $extra, &$err, $last_action_time, $last_actionident_time, $version, $locale)
{
    /*
        <CustomerRef>
            <ListID>80003579-1231522938</ListID>
        </CustomerRef>  
    */

    $customer = mysql_fetch_assoc(mysql_query("SELECT * FROM qb_cust_test WHERE cust_id = " . (int) $ID));

    $xml = '<?xml version="1.0" encoding="utf-8"?>
        <?qbxml version="2.0"?>
        <QBXML>
            <QBXMLMsgsRq onError="stopOnError">
                <CustomerAddRq requestID="' . $requestID . '">
                    <CustomerAdd>
                        <Name>'.$customer['f_name'].' '.$customer['l_name'].'</Name>
                        <FirstName>'.$customer['f_name'].'</FirstName>
                        <LastName>'.$customer['l_name'].'</LastName>
                        <Phone>'.$customer['phone'].'</Phone>
                        <Email>'.$customer['email'].'</Email>
                    </CustomerAdd>
                </CustomerAddRq>
            </QBXMLMsgsRq>
        </QBXML>';

    return $xml;
}

/**
 * Receive a response from QuickBooks 
 */
function _quickbooks_customer_add_response($requestID, $user, $action, $ID, $extra, &$err, $last_action_time, $last_actionident_time, $xml, $idents)
{   
    mysql_query("
        UPDATE 
            qb_cust_test 
        SET 
            quickbooks_listid = '" . mysql_real_escape_string($idents['ListID']) . "', 
            quickbooks_editsequence = '" . mysql_real_escape_string($idents['EditSequence']) . "'
        WHERE 
            id = " . (int) $ID);
}

//continue if error is 3100
$errmap = array(
        '*' => 'catch_all_errors'
);


function catch_all_errors($requestID, $user, $action, $ID, $extra, &$err, $last_action_time, $last_actionident_time, $version, $locale)
{
    if ($action == QUICKBOOKS_ADD_CUSTOMER and $errnum == 3100)
    {
        return true; // Ignore this error, all is OK - customer already exists
    }

    // Some other error occurred, stop processing

return false;

}

ADD_CUSTOMER.php

<?php
/**
 * Require some configuration stuff
 */ 
require_once dirname(__FILE__) . '/config.php';
// Queue up the customer add 

// Select all customers first
$customers = mysql_query("SELECT * FROM qb_cust_test");

while($customer = mysql_fetch_assoc($customers)) {
    $Queue = new QuickBooks_WebConnector_Queue($dsn);
    $Queue->enqueue(QUICKBOOKS_ADD_CUSTOMER, $customer['cust_id']);
}

    die('Customer submitted!');

1 个答案:

答案 0 :(得分:0)

框架/ Web连接器的默认行为是报告错误,并在发生错误时停止进程。

但是,您当然可以调整默认行为并忽略/处理遇到的任何错误。

设置错误处理程序(documentation here)来处理您看到的错误(3100,...)。确保错误处理程序在其末尾有return true;,以便从错误处理程序返回true

返回true告诉Web连接器继续处理,即使发生错误/警告也是如此。返回false(默认值)会使其停止处理。

所以你应该得到类似的东西:

$errmap = array(
    '*' => 'catch_all_errors' 
);

...

function catch_all_errors($requestID, $user, $action, $ID, $extra, &$err, $xml, $errnum, $errmsg)
{
    if ($action == QUICKBOOKS_ADD_CUSTOMER and $errnum == 3100)
    {
        return true; // Ignore this error, all is OK - customer already exists
    }

    // Some other error occurred, stop processing 
    return false;
}