Created
July 11, 2019 07:16
-
-
Save lukecurtis93/dfa695009a356cc82c8972d99cbd6477 to your computer and use it in GitHub Desktop.
Send an invoice to Xero in Laravel Job
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Jobs\Xero; | |
use File; | |
use DateTime; | |
use App\Models\Invoice; | |
use Illuminate\Bus\Queueable; | |
use Spatie\Browsershot\Browsershot; | |
use Illuminate\Queue\SerializesModels; | |
use Illuminate\Queue\InteractsWithQueue; | |
use Illuminate\Contracts\Queue\ShouldQueue; | |
use Illuminate\Foundation\Bus\Dispatchable; | |
use XeroPHP\Application\PrivateApplication; | |
use App\Repositories\Backend\InvoiceRepository; | |
use App\Repositories\Backend\LineItemRepository; | |
use XeroPHP\Models\Accounting\Account as XeroAccount; | |
use XeroPHP\Models\Accounting\Contact as XeroContact; | |
use XeroPHP\Models\Accounting\Invoice as XeroInvoice; | |
use XeroPHP\Models\Accounting\Payment as XeroPayment; | |
use XeroPHP\Models\Accounting\Attachment as XeroAttachment; | |
use XeroPHP\Models\Accounting\Invoice\LineItem as XeroLineItem; | |
class SendXeroInvoice implements ShouldQueue | |
{ | |
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; | |
protected $invoice; | |
protected $xero; | |
protected $invoiceRepository; | |
/** | |
* Create a new job instance. | |
* | |
* @return void | |
*/ | |
public function __construct(Invoice $invoice) | |
{ | |
// | |
$this->xero = new PrivateApplication(config('xero')); | |
$this->invoiceRepository = new InvoiceRepository(new LineItemRepository); | |
$this->invoice = $invoice; | |
} | |
/** | |
* Execute the job. | |
* | |
* @return void | |
*/ | |
public function handle() | |
{ | |
// | |
$xeroInvoice = new XeroInvoice($this->xero); | |
$xeroInvoice->setContact($this->xero->loadByGUID(XeroContact::class, $this->invoice->company->xeroContact->xero_id)) | |
->setReference($this->invoice->name) | |
->setType($this->invoice->type) | |
->setCurrencyCode('AUD') | |
->setDueDate(new DateTime($this->invoice->due_date)) | |
->setStatus($this->invoice->status); | |
foreach($this->invoice->lineItems as $item) { | |
$xeroLineItem = new XeroLineItem($this->xero); | |
$xeroLineItem->setDescription($item->description) | |
->setQuantity($item->quantity) | |
->setUnitAmount($item->dollar_unit_price) | |
->setItemCode($item->xeroItem->code) | |
->setAccountCode($item->xeroAccount->code); | |
$xeroInvoice->addLineItem($xeroLineItem); | |
} | |
if ($xeroInvoice->save()) { | |
//Create the invoice PDF and save | |
$name = 'invoice-'.$this->invoice->id.'.pdf'; | |
$invoice = $this->invoice; | |
$view = view('backend.invoice.pdf', compact('invoice')); | |
Browsershot::html($view)->margins(20, 0, 20, 0)->showBackground()->waitUntilNetworkIdle()->save(storage_path('app/'.($name))); | |
$xeroAttachment = XeroAttachment::createFromLocalFile(storage_path('app/'.$name)); | |
$xeroInvoice->addAttachment($xeroAttachment); | |
$xeroInvoice->save(); | |
File::delete(storage_path('app/'.$name)); | |
// Successfully saved to Xero with attachment | |
$this->invoiceRepository->update($this->invoice, [ | |
'xero_id' => $xeroInvoice->getInvoiceNumber(), | |
'xero_guid' => $xeroInvoice->getGuid() | |
]); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
For the namespace of Xero (use XeroPHP\Models\Accounting), Which package you used ?