akaunting/app/Jobs/Banking/CreateTransaction.php

46 lines
1.4 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;
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-03-12 14:29:21 +03:00
use App\Models\Banking\Transaction;
2019-11-16 10:21:14 +03:00
2021-09-07 10:33:34 +03:00
class CreateTransaction extends Job implements HasOwner, HasSource, 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));
if (! array_key_exists($this->request->get('type'), config('type.transaction'))) {
$type = (empty($this->request->get('recurring_frequency')) || ($this->request->get('recurring_frequency') == 'no')) ? Transaction::INCOME_TYPE : Transaction::INCOME_RECURRING_TYPE;
$this->request->merge(['type' => $type]);
}
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
}
}