akaunting/app/Jobs/Banking/CreateTransaction.php

39 lines
1.1 KiB
PHP
Raw Normal View History

2019-11-16 10:21:14 +03:00
<?php
namespace App\Jobs\Banking;
use App\Abstracts\Job;
2020-03-11 22:16:52 +03:00
use App\Events\Banking\TransactionCreated;
use App\Events\Banking\TransactionCreating;
2021-09-06 11:53:57 +03:00
use App\Interfaces\Job\HasOwner;
use App\Interfaces\Job\ShouldCreate;
2020-03-12 14:29:21 +03:00
use App\Models\Banking\Transaction;
2019-11-16 10:21:14 +03:00
2021-09-06 11:53:57 +03:00
class CreateTransaction extends Job implements HasOwner, ShouldCreate
2019-11-16 10:21:14 +03:00
{
2021-09-06 11:53:57 +03:00
public function handle(): Transaction
2019-11-16 10:21:14 +03:00
{
2020-03-11 22:16:52 +03:00
event(new TransactionCreating($this->request));
2020-06-26 13:40:19 +03:00
\DB::transaction(function () {
2021-09-06 11:53:57 +03:00
$this->model = Transaction::create($this->request->all());
2019-11-16 10:21:14 +03:00
2020-06-26 13:40:19 +03:00
// Upload attachment
if ($this->request->file('attachment')) {
foreach ($this->request->file('attachment') as $attachment) {
$media = $this->getMedia($attachment, 'transactions');
2019-11-16 10:21:14 +03:00
2021-09-06 11:53:57 +03:00
$this->model->attachMedia($media, 'attachment');
}
2020-06-26 13:40:19 +03:00
}
2019-11-16 10:21:14 +03:00
2020-06-26 13:40:19 +03:00
// Recurring
2021-09-06 11:53:57 +03:00
$this->model->createRecurring($this->request->all());
2020-06-26 13:40:19 +03:00
});
2020-03-11 22:16:52 +03:00
2021-09-06 11:53:57 +03:00
event(new TransactionCreated($this->model));
2019-11-16 10:21:14 +03:00
2021-09-06 11:53:57 +03:00
return $this->model;
2019-11-16 10:21:14 +03:00
}
}