Question

Please provide me a example how query if a customer exits in the quickbooks or not, if the customer doesn't exits then need to add that customer to the quickbook

Another error am getting when syncing the customer and sales orders

    Message:
    No data exchange required

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

i integrated the code in a magento site after testing the quickbooks examples separately which worked fine without error. but after integrating in the magento am getting the message "No data to exchange for this application. Job ending."

code written in the magento controller file

    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 file code

    <?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 file 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);

Config file code

    <?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);

function files code

    <?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; 
    }

Please help me in fixing this code

Thanks, Murali.

Was it helpful?

Solution

The way quickbooks web connector is base off of a queue system (first in first out), so your current implementation will not work efficiently and you will have to storing the ListID or FullName in magento after doing QUICKBOOKS_QUERY_CUSTOMER since each action (queue/add customer, add sales order) is a separate request from quickbooks to magento webserver.

A better way to do this is in magento store the customer ListID and FullName from your _quickbooks_customer_add_response method.

Then

if(ListID or FullName)
  QUICKBOOKS_ADD_SALESORDER
else
  QUICKBOOKS_ADD_CUSTOMER
  QUICKBOOKS_ADD_SALESORDER

In your _quickbooks_salesorder_add_request method you look up the ListID or FullName base on order number or customer id

You may also run in to issue if someone delete the customer in quickbooks then you could just add the customer in quickbooks and then add update FullName in magento.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top