2020-12-24 01:28:38 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Jobs\Document;
|
|
|
|
|
|
|
|
use App\Abstracts\Job;
|
|
|
|
use App\Events\Document\DocumentCreated;
|
|
|
|
use App\Events\Document\DocumentCreating;
|
2021-09-06 11:53:57 +03:00
|
|
|
use App\Interfaces\Job\HasOwner;
|
2021-09-07 10:33:34 +03:00
|
|
|
use App\Interfaces\Job\HasSource;
|
2021-09-06 11:53:57 +03:00
|
|
|
use App\Interfaces\Job\ShouldCreate;
|
2020-12-24 01:28:38 +03:00
|
|
|
use App\Jobs\Document\CreateDocumentItemsAndTotals;
|
|
|
|
use App\Models\Document\Document;
|
|
|
|
use Illuminate\Support\Str;
|
|
|
|
|
2021-09-07 10:33:34 +03:00
|
|
|
class CreateDocument extends Job implements HasOwner, HasSource, ShouldCreate
|
2020-12-24 01:28:38 +03:00
|
|
|
{
|
2021-09-06 11:53:57 +03:00
|
|
|
public function handle(): Document
|
2020-12-24 01:28:38 +03:00
|
|
|
{
|
|
|
|
if (empty($this->request['amount'])) {
|
|
|
|
$this->request['amount'] = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
event(new DocumentCreating($this->request));
|
|
|
|
|
|
|
|
\DB::transaction(function () {
|
2021-09-06 11:53:57 +03:00
|
|
|
$this->model = Document::create($this->request->all());
|
2020-12-24 01:28:38 +03:00
|
|
|
|
|
|
|
// Upload attachment
|
|
|
|
if ($this->request->file('attachment')) {
|
2021-01-22 12:38:17 +03:00
|
|
|
foreach ($this->request->file('attachment') as $attachment) {
|
2021-09-06 11:53:57 +03:00
|
|
|
$media = $this->getMedia($attachment, Str::plural($this->model->type));
|
2020-12-24 01:28:38 +03:00
|
|
|
|
2021-09-06 11:53:57 +03:00
|
|
|
$this->model->attachMedia($media, 'attachment');
|
2021-01-22 12:38:17 +03:00
|
|
|
}
|
2020-12-24 01:28:38 +03:00
|
|
|
}
|
|
|
|
|
2021-09-06 11:53:57 +03:00
|
|
|
$this->dispatch(new CreateDocumentItemsAndTotals($this->model, $this->request));
|
2020-12-24 01:28:38 +03:00
|
|
|
|
2021-09-06 11:53:57 +03:00
|
|
|
$this->model->update($this->request->all());
|
2020-12-24 01:28:38 +03:00
|
|
|
|
2021-09-06 11:53:57 +03:00
|
|
|
$this->model->createRecurring($this->request->all());
|
2020-12-24 01:28:38 +03:00
|
|
|
});
|
|
|
|
|
2021-09-06 11:53:57 +03:00
|
|
|
event(new DocumentCreated($this->model, $this->request));
|
2020-12-24 01:28:38 +03:00
|
|
|
|
2021-09-06 11:53:57 +03:00
|
|
|
return $this->model;
|
2020-12-24 01:28:38 +03:00
|
|
|
}
|
|
|
|
}
|