2014-01-16 26 views
2

我正在使用QuickBooks PHP DevKit爲每個客戶獲取發票。這工作正常,但我需要從發票中的每一行中獲取每個項目以簡單顯示它,並且由於某種原因我很難獲取此信息。如何使用QuickBooks_IPP從發票中顯示一行中的每個項目?

我可以

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

得到的itemref但它拋出一個致命的錯誤:「調用一個非對象的成員函數getItemRef()」,並殺死了腳本。

這是我的代碼。

//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'); 
    } 

總之,在發票的每一行中查詢項目的正確方法是什麼?

回答

3

有幾件事情要注意在這裏,它是一個有點分不清到底發生了從剛纔你的代碼怎麼回事......所以這裏有一些事情要檢查:

1.確保您正在提取訂單項:

默認情況下,Intuit的某些查詢不會返回訂單項。這並不是針對PHP或任何其他東西,它只是Intuit的API的工作原理。

如果你想行項目,你可能需要做一個查詢這樣的:

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

2.不是每個行項目都將有一個SalesItemLineDetail節點。

訂單項可以有多種不同的詳細節點。某些訂單項可能有SalesItemLineDetail節點,而其他訂單項可能有DiscountLineDetail節點,而其他訂單項可能有其他類型。

見準備在這裏:

所以,像這樣聲明是不是安全做

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

因爲有一種可能性,即SalesItemLineDetail您試圖獲得的節點可能不存在然後導致$ItemNULL,這將導致錯誤。

3.調試!

有一件事可能對您有所幫助(當您發佈尋找幫助時當然會有所幫助)將看看您從Intuit獲得的實際XML響應。

您可以通過執行此操作:

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

這將轉儲一些調試輸出,這將是有益的。您也可以打印出對象:

print_r($Line); 

而且這可能會幫助您查找問題。

4.最後 - 一個工作示例!

下面是應該爲你工作的例子:

總之,你想要做的是:

$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"); 
    } 
} 
+0

非常感謝你,基思,爲您的快速解決方案!這絕對有助於理解我想要做的事情。我查看了示例文件(example_invoice_w_lines_query.php),但它只包含了$ Line = $ Invoice-> getLine(0),我已經擁有這個文件,並且不確定如何獲取對象的更深層數據,儘管我是在我的select語句中缺少「Line。*」,這可能是它的很大一部分。無論如何,非常感謝您的幫助! – cellen

+0

NP。我更新了GitHub上的例子,專門回答你的問題,這就是爲什麼它之前沒有展示過這些東西。 –

相關問題