从Xero API获取所有发票以及它们的明细项目。

huangapple go评论114阅读模式
英文:

Get all Invoices with their LineItems from Xero API

问题

我需要请求一个季度内的所有发票,并返回并汇总每个行项目按行项目账户分组的金额。然而,无论我如何构建请求,行项目数组都为空。

我正在使用 calcinai/xero-php 库,并且有一个可工作的模拟,可以返回我期望的数据:

  private static function getBillsPaid($xeroClient) {
    // ... 一些设置

    $bills = $xero->load(\XeroPHP\Models\Accounting\Invoice::class)
      ->where('Type', \XeroPHP\Models\Accounting\Invoice::INVOICE_TYPE_ACCPAY)
      ->where('Status', \XeroPHP\Models\Accounting\Invoice::INVOICE_STATUS_PAID)
      ->where("Date >= DateTime($start)")
      ->where("Date <= DateTime($end)")
      ->execute();

    $billsByAccount = new Map();
    foreach ($bills as $bill) {
      $invoice = $xero->loadByGUID(\XeroPHP\Models\Accounting\Invoice::class, $bill->getInvoiceID());
      foreach ($invoice->getLineItems() as $lineItem) {
        $billsByAccount->addListItemSum(
          $lineItem->getAccountCode(),
          $invoice->getCurrencyCode(),
          $lineItem->getUnitAmount()
        );
      }
    }

    return $billsByAccount->getAll();
  }

这个方法是有效的,但它必须为每张发票进行新的请求以加载行项目。我需要的是在单个请求中加载所有信息 ($bills),这样它就不会抛出“请求太多”错误。

我查看了 文档 并在理论上 在请求中添加 summaryOnly=True 查询 应该解决我面临的问题。

然而,将 ->setParameter('summaryOnly', 'true') 添加到请求中会导致它抛出“坏请求”错误,除非我从请求中删除日期查询。但即使我删除日期查询,它仍然不包括行项目。

作为参考,当不获取 $invoice 时,我尝试从 $bills->getLineItems() 获取行项目。

我漏掉了什么?

英文:

I need to request all invoices within a quarter, and return and sum the amount of each line item grouped by the line items account. However, no matter how I build the request the line items array is empty.

I am using the calcinai/xero-php library and have a working mockup that returns the data I expect it to:

  private static function getBillsPaid($xeroClient) {
    // ... some setup

    $bills = $xero->load(\XeroPHP\Models\Accounting\Invoice::class)
      ->where('Type', \XeroPHP\Models\Accounting\Invoice::INVOICE_TYPE_ACCPAY)
      ->where('Status', \XeroPHP\Models\Accounting\Invoice::INVOICE_STATUS_PAID)
      ->where("Date >= DateTime($start)")
      ->where("Date <= DateTime($end)")
      ->execute();

    $billsByAccount = new Map();
    foreach ($bills as $bill) {
      $invoice = $xero->loadByGUID(\XeroPHP\Models\Accounting\Invoice::class, $bill->getInvoiceID());
      foreach ($invoice->getLineItems() as $lineItem) {
        $billsByAccount->addListItemSum(
          $lineItem->getAccountCode(),
          $invoice->getCurrencyCode(),
          $lineItem->getUnitAmount()
        );
      }
    }

    return $billsByAccount->getAll();
  }

This works, however, it has to make a new request to load the line items for each invoice. What I need is for all the information to be loaded in a single request ($bills) so that it doesn't throw a "too many requests" error.

I have looked at the documentation and in theory adding a summaryOnly=True query to the request should resolve the issue I am facing.

However, adding ->setParameter('summaryOnly', 'true') to the request causes it throw a "bad request" error unless I remove both date queries from the request. But, even if I do remove the date queries it still doesn't include the line items.

For reference, when not fetching the $invoice, I am trying to get the line items from $bills->getLineItems().

What am I missing?

答案1

得分: 2

你可以尝试添加页面参数,例如 ?page=1。

英文:

Can you try adding the page param e.g. ?page=1

答案2

得分: 1

"SummaryOnly=true" 确保不返回任何明细项,与相反情况相反。正如Doel所提到的,使用分页将为您提供所需的详细响应。

英文:

SummaryOnly=true will ensure that no line items are returned rather than the opposite. As Doel has mentioned, using paging will give you the detailed response you need.

huangapple
  • 本文由 发表于 2023年6月6日 04:31:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/76409809.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定