【问题标题】:How do I display each Item in a Line from an Invoice using the QuickBooks_IPP?如何使用 QuickBooks_IPP 从发票中显示一行中的每个项目?
【发布时间】:2014-01-16 17:54:55
【问题描述】:

我正在使用 QuickBooks PHP DevKit 来获取每个客户的发票。这很好用,但我需要从发票中的每一行获取每个项目以简单地显示它,并且由于某种原因我难以获取此信息。

我可以用

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

但它会引发致命错误:“调用非对象上的成员函数 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');
    }

简而言之,在发票的每一行中查询项目的正确方法是什么?

【问题讨论】:

    标签: php quickbooks quickbooks-online


    【解决方案1】:

    这里有几件事情需要注意,而且很难从您的代码中准确判断发生了什么......所以这里有一些事情需要检查:

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

    【讨论】:

    • 非常感谢 Keith,您的快速解决方案!这绝对有助于理解我试图做的事情。我确实查看了示例文件(example_invoice_w_lines_query.php),但它只包含我已经拥有的 $Line = $Invoice->getLine(0),并且不确定如何获取对象的更深层次的数据,虽然我是我的选择语句中缺少“Line.*”,这可能是其中的很大一部分。无论如何,非常感谢您的帮助!
    • NP。我专门针对您的问题更新了 GitHub 上的示例,这就是它之前没有显示这些内容的原因。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-09
    • 2018-07-05
    • 2019-06-17
    • 2013-05-09
    • 2020-06-26
    • 1970-01-01
    相关资源
    最近更新 更多