akaunting/app/Jobs/Sale/CreateInvoice.php

62 lines
1.4 KiB
PHP
Raw Normal View History

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