Akaunting/app/Jobs/Sale/CreateInvoice.php

62 lines
1.4 KiB
PHP
Raw Normal View History

2018-11-04 22:25:43 +00:00
<?php
2019-12-31 12:49:09 +00:00
namespace App\Jobs\Sale;
2018-11-04 22:25:43 +00:00
2019-11-16 07:21:14 +00:00
use App\Abstracts\Job;
2019-12-31 12:49:09 +00:00
use App\Events\Sale\InvoiceCreated;
use App\Events\Sale\InvoiceCreating;
2020-07-06 06:54:18 +00:00
use App\Jobs\Sale\CreateInvoiceItemsAndTotals;
2019-12-31 12:49:09 +00:00
use App\Models\Sale\Invoice;
2018-11-04 22:25:43 +00:00
2019-11-16 07:21:14 +00:00
class CreateInvoice extends Job
2018-11-04 22:25:43 +00:00
{
2019-12-07 09:54:13 +00:00
protected $invoice;
2020-06-26 10:40:19 +00:00
protected $request;
2018-11-04 22:25:43 +00:00
/**
* Create a new job instance.
*
* @param $request
*/
public function __construct($request)
{
2019-11-16 07:21:14 +00:00
$this->request = $this->getRequestInstance($request);
2018-11-04 22:25:43 +00:00
}
/**
* Execute the job.
*
* @return Invoice
*/
public function handle()
{
2019-11-16 07:21:14 +00:00
if (empty($this->request['amount'])) {
$this->request['amount'] = 0;
}
event(new InvoiceCreating($this->request));
2020-06-26 10:40:19 +00:00
\DB::transaction(function () {
$this->invoice = Invoice::create($this->request->all());
2018-11-04 22:25:43 +00:00
2020-06-26 10:40:19 +00:00
// Upload attachment
if ($this->request->file('attachment')) {
$media = $this->getMedia($this->request->file('attachment'), 'invoices');
2018-11-04 22:25:43 +00:00
2020-06-26 10:40:19 +00:00
$this->invoice->attachMedia($media, 'attachment');
}
2018-11-04 22:25:43 +00:00
2020-07-06 06:54:18 +00:00
$this->dispatch(new CreateInvoiceItemsAndTotals($this->invoice, $this->request));
2018-11-04 22:25:43 +00:00
2020-06-26 10:40:19 +00:00
$this->invoice->update($this->request->all());
2018-11-04 22:25:43 +00:00
2020-06-26 10:40:19 +00:00
$this->invoice->createRecurring();
});
2018-11-04 22:25:43 +00:00
2019-12-07 09:54:13 +00:00
event(new InvoiceCreated($this->invoice));
2018-11-04 22:25:43 +00:00
2019-12-07 09:54:13 +00:00
return $this->invoice;
2018-11-04 22:25:43 +00:00
}
2018-11-06 14:55:31 +00:00
}