Akaunting/app/Jobs/Common/UpdateItem.php

44 lines
1.4 KiB
PHP
Raw Normal View History

2019-11-16 07:21:14 +00:00
<?php
namespace App\Jobs\Common;
use App\Abstracts\Job;
2023-10-31 14:40:14 +00:00
use App\Events\Common\ItemUpdated;
use App\Events\Common\ItemUpdating;
2021-09-06 08:53:57 +00:00
use App\Interfaces\Job\ShouldUpdate;
2020-12-08 10:30:06 +00:00
use App\Jobs\Common\CreateItemTaxes;
2020-12-08 14:00:50 +00:00
use App\Models\Common\Item;
2019-11-16 07:21:14 +00:00
2021-09-06 08:53:57 +00:00
class UpdateItem extends Job implements ShouldUpdate
2019-11-16 07:21:14 +00:00
{
2021-09-06 08:53:57 +00:00
public function handle(): Item
2019-11-16 07:21:14 +00:00
{
2023-10-31 14:40:14 +00:00
event(new ItemUpdating($this->model, $this->request));
2020-06-26 10:40:19 +00:00
\DB::transaction(function () {
2021-09-06 08:53:57 +00:00
$this->model->update($this->request->all());
2019-11-16 07:21:14 +00:00
2020-06-26 10:40:19 +00:00
// Upload picture
if ($this->request->file('picture')) {
2025-12-07 15:31:41 +00:00
$this->deleteMediaModel($this->model, 'picture', $this->request);
2020-06-26 10:40:19 +00:00
$media = $this->getMedia($this->request->file('picture'), 'items');
2019-11-16 07:21:14 +00:00
2021-09-06 08:53:57 +00:00
$this->model->attachMedia($media, 'picture');
2025-12-07 15:31:41 +00:00
} elseif ($this->request->isNotApi() && ! $this->request->file('picture') && $this->model->picture) {
$this->deleteMediaModel($this->model, 'picture', $this->request);
} elseif ($this->request->isApi() && $this->request->has('remove_picture') && $this->model->picture) {
$this->deleteMediaModel($this->model, 'picture', $this->request);
2020-06-26 10:40:19 +00:00
}
2020-12-08 10:30:06 +00:00
2021-09-06 08:53:57 +00:00
$this->deleteRelationships($this->model, ['taxes']);
2020-12-08 10:30:06 +00:00
2021-09-06 08:53:57 +00:00
$this->dispatch(new CreateItemTaxes($this->model, $this->request));
2020-06-26 10:40:19 +00:00
});
2019-11-16 07:21:14 +00:00
2023-10-31 14:40:14 +00:00
event(new ItemUpdated($this->model, $this->request));
2021-09-06 08:53:57 +00:00
return $this->model;
2019-11-16 07:21:14 +00:00
}
}