calcinai / xero-php

A php library for the Xero API, with a cleaner OAuth interface and ORM-like abstraction.
MIT License
359 stars 261 forks source link

Saving Multiple invoices #845

Closed CarlAtIssl closed 2 years ago

CarlAtIssl commented 2 years ago

Hi

I've a PHP routine that creates an invoice, sends the invoice to xero, pause for a second and then repeats.

In the test data I have, there are over 300 invoices, my system timesout (laravel/vuejs) with a 504 error with no response to the user, it just dies.

But if I wait for 15 minutes or so, I receive the email I create in the routine to inform me that the invoice has been sent.

But, is there a way to create a batch of invoices so I only send to Xero at the end of the invoice build routine

I currently send each invoice one at a time. Simply put with lots of code missing:-

foreach($transactions as $transaction) {
  $new_invoice = new Invoice($xero);
  $new_invoice->setDescription('etc etc')
  try {
    $new_invoice->save();
  }
} 

How do I create a variable to store each $new_invoice in, then send them all in one call?

Regards Carl.

rodjsta commented 2 years ago

Hi Carl, rather than using the save() method, have you thought about using the saveAll() method?

rodjsta commented 2 years ago

Hi Carl, the other thing you could do if you're using Laravel is catch the exception and dispatch the export/saving back to the queue with a delay. Interesting that you're getting a 504, sounds like more of a 503 rate limit problem.

CarlAtIssl commented 2 years ago

Hi Carl, rather than using the save() method, have you thought about using the saveAll() method?

Hi, but how do I populate a variable with multiple invoices? The line $new_invoice = new Invoice($xero) only allows for 1 invoice. Is there a wrapper for multiple invoices?

CarlAtIssl commented 2 years ago

Hi Carl, the other thing you could do if you're using Laravel is catch the exception and dispatch the export/saving back to the queue with a delay. Interesting that you're getting a 504, sounds like more of a 503 rate limit problem.

Hi, I think I might get all the invoices to process by doing the following:-

When I press the button process the following js code:-

do an axios call to get all the invoices that require transferring (in a php routine), return the results back to js, then process each invoice by doing a js call to a php routine to process each invoice, that way, I can interact with the screen by updating the user (I think). That might get around the issue I'm having!

ebebbington commented 2 years ago

@CarlAtIssl just to help out regarding saveAll()


$invoices = []
foreach($transactions as $transaction) {
  $new_invoice = new Invoice($xero);
  $new_invoice->setDescription('etc etc')
  $invoices[] = $new_invoice;
} 
try {
  $xero->saveAll($invoices);
} catch (Exception $e) { ... }
CarlAtIssl commented 2 years ago

@ebebbington thanks for that, I was thinking that it would need to be $all_invoices->saveAll(), not the way you've described, I'll try that now!

Thanks to both of you