我正在使用 QuickBooks PHP DevKit 获取每位客户的发票。这工作正常,但我需要从发票中的每一行获取每个项目才能简单地显示它,并且由于某种原因我很难获取此信息。
我可以通过
获取 ItemRef$Item = $Line->getSalesItemLineDetail();
$itemRef = $Item->getItemRef();
但它会抛出一个 fatal error :“调用非对象上的成员函数 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(' This customer has no invoices.<br>');
}
}
}
else
{
print('There are no customers with the provided ID');
}
简而言之,查询发票每一行中的项目的正确方法是什么?
最佳答案
这里有一些事情需要注意,并且很难仅从您的代码中准确判断发生了什么......所以这里有一些需要检查的事情:
<强>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
节点可能可能不存在,这会导致 $Item
为NULL
这会导致错误。
<强>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");
}
}
关于php - 如何使用 QuickBooks_IPP 显示发票中一行中的每个项目?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21169282/