Question

I'm using the QuickBooks PHP DevKit to get the Invoice for each Customer. This works fine, but I need to get each Item from each Line in the invoice to simply display it and I am having difficulty getting this information for some reason.

I can get the ItemRef with

$Item = $Line->getSalesItemLineDetail();
$itemRef = $Item->getItemRef();

But it throws a fatal error: "Call to a member function getItemRef() on a non-object" and kills the script.

This is the code I have.

    //get QuickBooks customer ID    
    $customers = $CustomerService->query($Context, $realm, "SELECT * FROM Customer WHERE id = '18' ");

    if (count($customers))
    {
        foreach ($customers as $Customer)
        {

            $invoices = $InvoiceService->query($Context, $realm, "SELECT * FROM Invoice WHERE CustomerRef = '" . QuickBooks_IPP_IDS::usableIDType($Customer->getId()) . "' ");

            if (count($invoices))
            {
                foreach ($invoices as $Invoice)
                {
                    echo $Invoice->getTotalAmt();

                    $line_count = $Invoice->countLine();

                    for ($i = 0; $i < $line_count; $i++)
                    {
                      $Line = $Invoice->getLine($i);
                      //print_r($Line);

                      $Item = $Line->getSalesItemLineDetail();
                      $itemRef = $Item->getItemRef();

                      $item = $ItemService->query($Context, $realm, "SELECT * FROM Item WHERE id = '" . QuickBooks_IPP_IDS::usableIDType($itemRef) . "' ");

                      foreach($item as $Item){
                          echo $Item->getName();
                      }

                    }
                }
            }
            else
            {
                print(' &nbsp; &nbsp; This customer has no invoices.<br>');
            }
        }
    }
    else
    {
        print('There are no customers with the provided ID');
    }

In short, what is the proper way to query for the Item in each Line of an Invoice?

Was it helpful?

Solution

There's a few things to watch out for here, and it's a little hard to tell exactly what's going on from just your code... so here are some things to check on:

1. Make sure you're fetching the line items:

By default, some of Intuit's queries do not return line items. This isn't specific really to PHP or anything, it's just how Intuit's APIs work.

If you want the line items, you may have to do a query like this:

SELECT *, Line.* FROM Invoice WHERE Id = '5'

2. Not every line item is going to have a SalesItemLineDetail node.

Line items can have a variety of different detail nodes. Some line items might have a SalesItemLineDetail node, while others might have a DiscountLineDetail node, while still others could have other types.

See the possibilities here:

So, a statement like this is not safe to do:

$Line = $Invoice->getLine(0);
$Item = $Line->getSalesItemLineDetail();
$itemRef = $Item->getItemRef();

Because there's a possibility that the SalesItemLineDetail node you're trying to get might not exist which then would result in $Item being NULL which would cause an error.

3. Debugging!

One thing that might be helpful to you (and certainly would be helpful when you're posting looking for help) would be to look at the actual XML response you got back from Intuit.

You can do this by doing:

print($IPP->lastRequest());
print($IPP->lastResponse());

That will dump some debug output which will be helpful. You can also print out the objects:

print_r($Line);

And that might help you track down problems too.

4. Finally - a working example!

Here's an example which should work for you:

In short, what you want to do is:

$num_lines = $Invoice->countLine();                 // How many line items are there?
for ($i = 0; $i < $num_lines; $i++)
{
    $Line = $Invoice->getLine(0);

    // Let's find out what detail type this uses - only fetching item lines here 
    if ($Line->getDetailType() == 'SalesItemLineDetail')
    {
            $Detail = $Line->getSalesItemLineDetail();

            $item_id = $Detail->getItemRef();

            print('Item id is: ' . $item_id . "\n");

            // Query for the item 
            $items = $ItemService->query($Context, $realm, "SELECT * FROM Item WHERE Id = '" . QuickBooks_IPP_IDS::usableIDType($item_id) . "' ");
            print('   That item is named: ' . $items[0]->getName() . "\n");
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top