phpsdkquickbooksqbxml

how to add multiple line items <SalesReceiptLineAdd> dynamically while adding sales receipt using qbxml with php?


I have my own software and want to add the sales receipt from my database to Quickbooks pro 2020. So in that I have sales receipt with more than one line items inside. How can I pass more line items in qbXML using PHP while returning xml request?

This is my code. I am using Keith Palmer's framework..

In this I have added SalesReceiptLineAdd for multiple items statically. But how can it be created dynamically? i.e how do I loop SalesReceiptLineAdd tag multiple times?

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

    $xml = '<?xml version="1.0" encoding="utf-8"?>
        <?qbxml version="7.0"?>
        <QBXML>
            <QBXMLMsgsRq onError="stopOnError">
                <SalesReceiptAddRq requestID="' . $requestID . '">
                    <SalesReceiptAdd>
                        <CustomerRef>
                            <ListID>80000020-1573225608</ListID>
                        </CustomerRef>
                        <TxnDate>2019-11-14</TxnDate>
                        <RefNumber>34</RefNumber>
                        <BillAddress>
                            <Addr1>Keith Palmer Jr.</Addr1>
                            <Addr3>134 Stonemill Road</Addr3>
                            <City>Storrs-Mansfield</City>
                            <State>CT</State>
                            <PostalCode>06268</PostalCode>
                            <Country>United States</Country>
                        </BillAddress>
                        <SalesReceiptLineAdd>
                            <ItemRef>
                                <ListID>80000030-1573662395</ListID>
                            </ItemRef>
                            <Desc>$25.00 gift certificate</Desc>
                            <Quantity>1</Quantity>
                            <Rate>25.00</Rate>
                            <SalesTaxCodeRef>
                                <FullName>tax</FullName>
                            </SalesTaxCodeRef>
                        </SalesReceiptLineAdd>
                        <SalesReceiptLineAdd>
                            <ItemRef>
                                <ListID>80000031-1573810312</ListID>
                            </ItemRef>
                            <Desc>The Hitchhiker\'s Guide to the Galaxy</Desc>
                            <Quantity>1</Quantity>
                            <Rate>25.00</Rate>
                            <SalesTaxCodeRef>
                                <FullName>tax</FullName>
                            </SalesTaxCodeRef>
                        </SalesReceiptLineAdd>
                    </SalesReceiptAdd>
                </SalesReceiptAddRq>
            </QBXMLMsgsRq>
        </QBXML>';

    return $xml;


}

Solution

  • You can use standard string concatenation and a for loop to do this.

    For example, something like this would repeat the line item 5 times (for 5 lines):

    function _quickbooks_salesreceipt_add_request($requestID, $user, $action, $ID, $extra, &$err, $last_action_time, $last_actionident_time, $version, $locale)
    {
        /*
            <CustomerRef>
                <ListID>80003579-1231522938</ListID>
            </CustomerRef>  
        */
    
        $xml = '<?xml version="1.0" encoding="utf-8"?>
            <?qbxml version="7.0"?>
            <QBXML>
                <QBXMLMsgsRq onError="stopOnError">
                    <SalesReceiptAddRq requestID="' . $requestID . '">
                        <SalesReceiptAdd>
                            <CustomerRef>
                                <ListID>80000020-1573225608</ListID>
                            </CustomerRef>
                            <TxnDate>2019-11-14</TxnDate>
                            <RefNumber>34</RefNumber>
                            <BillAddress>
                                <Addr1>Keith Palmer Jr.</Addr1>
                                <Addr3>134 Stonemill Road</Addr3>
                                <City>Storrs-Mansfield</City>
                                <State>CT</State>
                                <PostalCode>06268</PostalCode>
                                <Country>United States</Country>
                            </BillAddress>';
                
        for ($i = 0; $i <= 5; $i++)
        {
            $xml .= '
              <SalesReceiptLineAdd>
                                <ItemRef>
                                    <ListID>80000031-1573810312</ListID>
                                </ItemRef>
                                <Desc>The Hitchhiker\'s Guide to the Galaxy</Desc>
                                <Quantity>1</Quantity>
                                <Rate>25.00</Rate>
                                <SalesTaxCodeRef>
                                    <FullName>tax</FullName>
                                </SalesTaxCodeRef>
                            </SalesReceiptLineAdd>';
        }
                            
        $xml .= '
                        </SalesReceiptAdd>
                    </SalesReceiptAddRq>
                </QBXMLMsgsRq>
            </QBXML>';
    
        return $xml;
    
    
    }

    I would also recommend referring to the PHP documentation on these basic topics: