phpinvoicexero-api

Get all Invoices with their LineItems from Xero API


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?


Solution

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