查询客户的示例,如果不存在,则将客户添加到quickbooks

时间:2014-04-30 13:49:34

标签: php magento quickbooks qbxml

请提供一个示例,了解客户是否退出快速簿中的查询,如果客户没有退出则需要将该客户添加到快速手册

同步客户和销售订单时出现的另一个错误

    Message:
    No data exchange required

    Description:
    No data to exchange for this application. Job ending.

我在单独测试了quickbooks示例之后将代码集成到magento站点中,这些示例工作正常且没有错误。但是在整合到magento之后我得到了消息"没有数据可以交换这个应用程序。工作结束。"

用magento控制器文件编写的代码

    public function syncAction() {    

        $orderid =  $id    = $this->getRequest()->getParam('order_id');
        $orderdetails = Mage::getModel('sales/order')->load($id);
        // Get the id of the orders shipping address

        $realorderid = $orderdetails->getRealOrderId();
        $orderdate = $orderdetails->getCreatedAtDate();
        $customerid = $orderdetails->getCustomerId();
        $shippingId = $orderdetails->getShippingAddress()->getId();
        $billingId = $orderdetails->getBillingAddress()->getId();

        $items = $orderdetails->getAllItems();
        $itemcount=count($items);
        $order=array();
        foreach ($items as $itemId => $item) {
            $order['items']['name'][] = $item->getName();
            $order['items']['unitPrice'][]=$item->getPrice();
            $order['items']['sku'][]=$item->getSku();
            $order['items']['qty'][]=$item->getQtyToInvoice();
        }

        // Get shipping address data using the id
        $customer = Mage::getModel('customer/customer')->load($customerid);
        $saddress = Mage::getModel('sales/order_address')->load($shippingId);
        $baddress = Mage::getModel('sales/order_address')->load($billingId);


        foreach ($customer->getAddresses() as $address){
        }               

        $customers['id'] = $customer->getId();
        $customers['name'] = $customer->getName();
        $customers['firstname'] = $customer->getFirstname();
        $customers['lastname']  = $customer->getLastname();
        $customers['email']  = $customer->getEmail();   
        $customers['company'] = $customer->getCompany();            
        $customers['address1']   = $address->getStreet(-1);
        $customers['address2']   = '';
        $customers['postal_code'] = $address->getPostcode();
        $customers['city']        = $address->getCity();
        $customers['state']        = $address->getState();
        $customers['country']     = $address->getCountryModel()->getIso3Code();
        $customers['phone_number'] = $address->getTelephone();
        $customers['altphone']   = '';
        $customers['fax_number'] = $address->getFax();

        $order['customer'] = $customerid;
        $order['customername'] = $customer->getName();
        $order['order_id'] = $orderid;
        $order['realorder_id'] = $realorderid;
        $order['orderdate'] = $orderdate;
        $order['shipaddress']['firstname'] = $saddress->getFirstname();
        $order['shipaddress']['lastname']  = $saddress->getLastname();
        $order['shipaddress']['address1']   = $saddress->getStreet(-1);
        $order['shipaddress']['address2']   = $saddress->getStreet(1);
        $order['shipaddress']['postal_code'] = $saddress->getPostcode();
        $order['shipaddress']['city']        = $saddress->getCity();
        $order['shipaddress']['state']        = $baddress->getState();
        $order['shipaddress']['country']     = $saddress->getCountryModel()->getIso3Code();
        $order['shipaddress']['phone_number'] = $saddress->getTelephone();

        $order['billaddress']['firstname'] = $baddress->getFirstname();
        $order['billaddress']['lastname']  = $baddress->getLastname();
        $order['billaddress']['address1']   = $baddress->getStreet(-1);
        $order['billaddress']['address2']   = $baddress->getStreet(1);
        $order['billaddress']['postal_code'] = $baddress->getPostcode();
        $order['billaddress']['city']        = $baddress->getCity();
        $order['billaddress']['state']        = $baddress->getState();
        $order['billaddress']['country']     = $baddress->getCountryModel()->getIso3Code();
        $order['billaddress']['phone_number'] = $baddress->getTelephone();


        // Display the shipping address data array on screen
        //var_dump($address);
        require_once Mage::getBaseDir().'/wscabinertyqb/config.php';
        $Queue = new QuickBooks_WebConnector_Queue($dsn);
        $Queue->enqueue(QUICKBOOKS_ADD_CUSTOMER,$customerid,'', $customers);
        $Queue->enqueue(QUICKBOOKS_ADD_SALESORDER,$realorderid,'', $order);
        //print_r($orderdetails);
        $this->_redirect('*/*/');
        //echo "Working";
    }

qwc文件代码

    <?xml version="1.0"?>
    <QBWCXML>
        <AppName>Wholesale Cabinetry LLC</AppName>
        <AppID></AppID>
        <AppURL>https://mysiteurl.com/store/wscabinertyqb/wscabinertyqb.php</AppURL>
        <AppDescription></AppDescription>
        <AppSupport>https://mysiteurl.com/store/wscabinertyqb/wscabinertyqb.php?support=1</AppSupport>
        <UserName>wscabinetry</UserName>
        <OwnerID>{90A44FB7-33D9-4818-AC85-AC86A7E7D1EE}</OwnerID>
        <FileID>{57F3B9B6-86F1-4FCE-B1FF-967DE1813D22}</FileID>
        <QBType>QBFS</QBType>
        <Scheduler>
            <RunEveryNMinutes>60</RunEveryNMinutes>
        </Scheduler>
        <IsReadOnly>false</IsReadOnly>
    </QBWCXML>

webconnector文件wscabinertyqb.php

    <?php
    require_once 'config.php';
    require_once 'functions.php';

    // Map QuickBooks actions to handler functions
    $map = array(
        QUICKBOOKS_QUERY_CUSTOMER => array( '_quickbooks_customer_query_request', '_quickbooks_customer_query_response', '_quickbooks_error_handler' ),
        QUICKBOOKS_ADD_CUSTOMER => array( '_quickbooks_customer_add_request', '_quickbooks_customer_add_response' ),
        QUICKBOOKS_ADD_SALESORDER => array( '_quickbooks_salesorder_add_request', '_quickbooks_salesorder_add_response' ),
        );

    $errmap = array(
                3070 =>  '_quickbooks_error_stringtoolong',
                3140 => '_quickbooks_reference_error',
                '*' => '_quickbooks_error_handler',
            );

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

    $hooks = array();
    $log_level = QUICKBOOKS_LOG_DEVELOP;
    $soapserver = QUICKBOOKS_SOAPSERVER_BUILTIN;
    $soap_options = array();
    $handler_options = array(
                'deny_concurrent_logins' => false, 
                'deny_reallyfast_logins' => false, 
            );      
    $soap_options = array();
    $driver_options = array();
    $callback_options = array();    

    // Create a new server and tell it to handle the requests
    // __construct($dsn_or_conn, $map, $errmap = array(), $hooks = array(), $log_level = QUICKBOOKS_LOG_NORMAL, $soap = QUICKBOOKS_SOAPSERVER_PHP, $wsdl = QUICKBOOKS_WSDL, $soap_options = array(), $handler_options = array(), $driver_options = array(), $callback_options = array()
    $Server = new QuickBooks_WebConnector_Server($dsn, $map, $errmap, $hooks, $log_level, $soapserver, QUICKBOOKS_WSDL, $soap_options, $handler_options, $driver_options, $callback_options);

    $response = $Server->handle(true, true);

配置文件代码

    <?php
    error_reporting(E_ALL | E_STRICT);
    ini_set('display_errors', 1);

    if (function_exists('date_default_timezone_set')){  
        date_default_timezone_set('America/New_York'); 
    }

    require_once 'QuickBooks.php';
    //require_once '../app/Mage.php';

    $user = 'wscabinetry';
    $pass = 'wscabinetry@123';


    $dsn = 'mysql://user:root#@localhost/wholecab';

    define('QB_QUICKBOOKS_DSN', $dsn);

    if (!QuickBooks_Utilities::initialized($dsn)){
        // Initialize creates the neccessary database schema for queueing up requests and logging
        QuickBooks_Utilities::initialize($dsn);

        // This creates a username and password which is used by the Web Connector to authenticate
        QuickBooks_Utilities::createUser($dsn, $user, $pass);   
    }

    QuickBooks_WebConnector_Queue_Singleton::initialize($dsn);

功能文件代码

    <?php

    function _quickbooks_salesorder_add_request($requestID, $user, $action, $ID, $extra, &$err, $last_action_time, $last_actionident_time, $version, $locale){

        mysql_query("INSERT INTO quickbooks_salesorder (customer_id,order_id,realorder_id) VALUES ('".$order['customer']."','".$order['order_id']."','".$order['realorder_id']."')");
        $order = $extra;

        // We're just testing, so we'll just use a static test request:
        $xml = '<?xml version="1.0" encoding="utf-8"?>
            <?qbxml version="5.0"?>
            <QBXML>
                <QBXMLMsgsRq onError="stopOnError">
                    <SalesOrderAddRq requestID="'.$requestID.'">
                        <SalesOrderAdd>
                            <CustomerRef>
                                <FullName>'.$order['customername'].'</FullName>
                            </CustomerRef>
                            <TxnDate>'.$order['orderdate'].'</TxnDate>
                            <RefNumber>'.$order['realorder_id'].'</RefNumber>
                            <BillAddress>
                                <Addr1>'.$order['billaddress']['address1'].'</Addr1>
                                <Addr2>'.$order['billaddress']['address2'].'</Addr2>
                                <City>'.$order['billaddress']['city'].'</City>
                                <State>'.$order['billaddress']['state'].'</State>
                                <PostalCode>'.$order['billaddress']['postal_code'].'</PostalCode>
                                <Country>'.$order['billaddress']['country'].'</Country>
                            </BillAddress>
                            <ShipAddress>
                                <Addr1>'.$order['shipaddress']['address1'].'</Addr1>
                                <Addr2>'.$order['shipaddress']['address2'].'</Addr2>
                                <City>'.$order['shipaddress']['city'].'</City>
                                <State>'.$order['shipaddress']['state'].'</State>
                                <PostalCode>'.$order['shipaddress']['postal_code'].'</PostalCode>
                                <Country>'.$order['shipaddress']['country'].'</Country>
                            </ShipAddress>                      
                            <Memo></Memo>';

                        for($i=0;$i < count($order['items']);$i++){

                        $xml .= '<SalesOrderLineAdd>
                                        <ItemRef>
                                        <FullName>'.$order['items']['sku'][$i].'</FullName>
                                        </ItemRef>
                                        <Desc>'.$order['items']['name'][$i].'</Desc>
                                        <Quantity>'.$order['items']['qty'][$i].'</Quantity>
                                        <Amount>'.$order['items']['unitPrice'][$i].'</Amount>
                                </SalesOrderLineAdd>';
                        }


                $xml .= '   </SalesOrderAdd>
                    </SalesOrderAddRq>
                </QBXMLMsgsRq>
            </QBXML>';
        echo $xml;
        exit;
        return $xml;
    }

    function _quickbooks_salesorder_add_response($requestID, $user, $action, $ID, $extra, &$err, $last_action_time, $last_actionident_time, $xml, $idents){
        mysql_query("
                UPDATE 
                    quickbooks_salesorder 
                SET 
                    quickbooks_listid = '" . mysql_real_escape_string($idents['ListID']) . "', 
                    quickbooks_editsequence = '" . mysql_real_escape_string($idents['EditSequence']) . "'
                WHERE 
                    id = " . (int) $ID);

        $content = "##########################################################";
        $content .= $idents;
        $content .= "##########################################################";
        $content .= $xml;
        $fp = fopen($_SERVER['DOCUMENT_ROOT'] . "/store/qbsdkm/docs/myText.txt","a");
        fwrite($fp,$content);
        fclose($fp);
        return; 
    }

    function _quickbooks_customer_add_request($requestID, $user, $action, $ID, $extra, &$err, $last_action_time, $last_actionident_time, $version, $locale){


        mysql_query("INSERT INTO quickbooks_customers (customer_id) VALUES ('".$customer['id']."')");
        $customer = $extra;
        // We're just testing, so we'll just use a static test request:  
        $xml = '<?xml version="1.0" encoding="utf-8"?>
            <?qbxml version="5.0"?>
            <QBXML>
                <QBXMLMsgsRq onError="stopOnError">
                    <CustomerAddRq requestID="' . $requestID . '">
                        <CustomerAdd>
                            <Name>'.$customer['name'].'</Name>
                            <CompanyName>'.$customer['company'].'</CompanyName>
                            <FirstName>'.$customer['firstname'].'</FirstName>
                            <LastName>'.$customer['lastname'].'</LastName>
                            <BillAddress>
                                <Addr1>'.$customer['address1'].'</Addr1>
                                <Addr2>'.$customer['address2'].'</Addr2>
                                <City>'.$customer['city'].'</City>
                                <State>'.$customer['state'].'</State>
                                <PostalCode>'.$customer['postal_code'].'</PostalCode>
                                <Country>'.$customer['country'].'</Country>
                            </BillAddress>
                            <Phone>'.$customer['phone_number'].'</Phone>
                            <AltPhone>'.$customer['altphone'].'</AltPhone>
                            <Fax>'.$customer['fax_number'].'</Fax>
                            <Email>'.$customer['email'].'</Email>
                            <Contact>'.$customer['name'].'</Contact>
                        </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 
                    quickbooks_customers 
                SET 
                    quickbooks_listid = '" . mysql_real_escape_string($idents['ListID']) . "', 
                    quickbooks_editsequence = '" . mysql_real_escape_string($idents['EditSequence']) . "'
                WHERE 
                    id = " . (int) $ID);

        $content = "##########################################################";
        $content .= $idents;
        $content .= "##########################################################";
        $content .= $xml;
        $fp = fopen($_SERVER['DOCUMENT_ROOT'] . "/store/qbsdkm/docs/myText.txt","a");
        fwrite($fp,$content);
        fclose($fp);
        return; 
    }

    function _quickbooks_customer_query_request($requestID , $user, $action, $ID, $extra, &$err, $last_action_time, $last_actionident_time, $version, $locale){
        /*$xml = '<?xml version="1.0" encoding="utf-8"?>
            <?qbxml version="5.0"?>
            <QBXML>
              <QBXMLMsgsRq onError="continueOnError">
                <CustomerQueryRq>
                  <MaxReturned>5</MaxReturned>
                  <FromModifiedDate>1984-01-29T22:03:19</FromModifiedDate>
                  <OwnerID>0</OwnerID>
                </CustomerQueryRq>
              </QBXMLMsgsRq>
            </QBXML>';*/
            $xml = '<?xml version="1.0" encoding="utf-8"?>
                    <?qbxml version="5.0"?>
                    <QBXML>
                        <QBXMLMsgsRq onError="continueOnError">
                            <CustomerQueryRq requestID="' . $requestID . '" iterator="Start">
                                <FullName>'.$extra[''].'</FullName>
                            </CustomerQueryRq>
                        </QBXMLMsgsRq>
                    </QBXML>';
        return $xml;
    }

    function _quickbooks_customer_query_response($requestID, $user, $action, $ID, $extra, &$err, $last_action_time, $last_actionident_time, $xml, $idents){ 
        $content = $xml;
        $fp = fopen($_SERVER['DOCUMENT_ROOT'] . "/store/qbsdkm/docs/myText.txt","wb");
        fwrite($fp,$content);
        fclose($fp);
    }

    function _quickbooks_error_handler($requestID, $user, $action, $ID, $extra, &$err, $xml, $errnum, $errmsg){
        $content = "##########################################################";
        $content .= $idents;
        $content .= "##########################################################";
        $content .= $xml;
        $content .= "##########################################################";
        $content .= $errmsg;
        $fp = fopen($_SERVER['DOCUMENT_ROOT'] . "/store/qbsdkm/docs/myText.txt","a");
        fwrite($fp,$content);
        fclose($fp);
    }

    function _quickbooks_reference_error($requestID, $user, $action, $ID, $extra, &$err, $xml, $errnum, $errmsg){
        $content = "##########################################################";
        $content .= $idents;
        $content .= "##########################################################";
        $content .= $xml;
        $content .= "##########################################################";
        $content .= $errmsg;
        $fp = fopen($_SERVER['DOCUMENT_ROOT'] . "/store/qbsdkm/docs/myText.txt","a");
        fwrite($fp,$content);
        fclose($fp);
    }

    function _quickbooks_invoice_add_request($requestID, $user, $action, $ID, $extra, &$err, $last_action_time, $last_actionident_time, $version, $locale){
        // We're just testing, so we'll just use a static test request:
        $xml = '<?xml version="1.0" encoding="utf-8"?>
            <?qbxml version="5.0"?>
            <QBXML>
              <QBXMLMsgsRq onError="stopOnError">
                <InvoiceAddRq requestID="'.$requestID.'">
                  <InvoiceAdd>
                    <CustomerRef>
                      <FullName>Muralidhar, LLC (1249573828)</FullName>
                    </CustomerRef>
                    <TxnDate>2014-04-23</TxnDate>
                    <RefNumber>9668</RefNumber>
                    <BillAddress>
                      <Addr1>56 Cowles Road</Addr1>
                      <City>Willington</City>
                      <State>CT</State>
                      <PostalCode>06279</PostalCode>
                      <Country>United States</Country>
                    </BillAddress>
                    <PONumber></PONumber>
                    <Memo></Memo>

                    <InvoiceLineAdd>
                      <ItemRef>
                        <FullName>Test Item</FullName>
                      </ItemRef>
                      <Desc>Item 1 Description Goes Here</Desc>
                      <Quantity>1</Quantity>
                      <Rate>295</Rate>
                    </InvoiceLineAdd>

                    <InvoiceLineAdd>
                      <ItemRef>
                        <FullName>Test Item</FullName>
                      </ItemRef>
                      <Desc>Item 2 Description Goes Here</Desc>
                      <Quantity>3</Quantity>
                      <Rate>25</Rate>
                    </InvoiceLineAdd>

                  </InvoiceAdd>
                </InvoiceAddRq>
              </QBXMLMsgsRq>
            </QBXML>';

        return $xml;
    }

    function _quickbooks_invoice_add_response($requestID, $user, $action, $ID, $extra, &$err, $last_action_time, $last_actionident_time, $xml, $idents){
        $content = "##########################################################";
        $content .= $idents;
        $content .= "##########################################################";
        $content .= $xml;
        $fp = fopen($_SERVER['DOCUMENT_ROOT'] . "/store/qbsdkm/docs/myText.txt","wb");
        fwrite($fp,$content);
        fclose($fp);
        return; 
    }

请帮我修复此代码

谢谢, 穆拉利。

1 个答案:

答案 0 :(得分:1)

quickbooks Web连接器的方式基于队列系统(先进先出),因此您当前的实现将无法有效工作,您必须将ListIDFullName存储在magento中执行QUICKBOOKS_QUERY_CUSTOMER之后,因为每个操作(队列/添加客户,添加销售订单)都是从quickbooks到magento webserver的单独请求。

更好的方法是在magento中存储来自ListID方法的客户FullName_quickbooks_customer_add_response

然后

if(ListID or FullName)
  QUICKBOOKS_ADD_SALESORDER
else
  QUICKBOOKS_ADD_CUSTOMER
  QUICKBOOKS_ADD_SALESORDER

_quickbooks_salesorder_add_request方法中,您可以根据订单号或客户ID查找ListIDFullName

如果有人在quickbooks中删除了客户,那么您可能还会遇到问题,然后您可以在quickbooks中添加客户,然后在magento中添加更新FullName