akaunting/app/Jobs/Common/CreateItem.php

49 lines
1.0 KiB
PHP
Raw Normal View History

2019-11-16 10:21:14 +03:00
<?php
namespace App\Jobs\Common;
use App\Abstracts\Job;
2020-12-08 13:30:06 +03:00
use App\Jobs\Common\CreateItemTaxes;
2020-12-08 17:00:50 +03:00
use App\Models\Common\Item;
2019-11-16 10:21:14 +03:00
class CreateItem extends Job
{
2020-06-26 13:40:19 +03:00
protected $item;
2019-11-16 10:21:14 +03:00
protected $request;
/**
* Create a new job instance.
*
* @param $request
*/
public function __construct($request)
{
$this->request = $this->getRequestInstance($request);
2021-06-17 10:59:07 +03:00
$this->request->merge(['created_by' => user_id()]);
2019-11-16 10:21:14 +03:00
}
/**
* Execute the job.
*
* @return Item
*/
public function handle()
{
2020-06-26 13:40:19 +03:00
\DB::transaction(function () {
$this->item = Item::create($this->request->all());
2019-11-16 10:21:14 +03:00
2020-06-26 13:40:19 +03:00
// Upload picture
if ($this->request->file('picture')) {
$media = $this->getMedia($this->request->file('picture'), 'items');
2019-11-16 10:21:14 +03:00
2020-06-26 13:40:19 +03:00
$this->item->attachMedia($media, 'picture');
}
2020-12-08 13:30:06 +03:00
$this->dispatch(new CreateItemTaxes($this->item, $this->request));
2020-06-26 13:40:19 +03:00
});
2019-11-16 10:21:14 +03:00
2020-06-26 13:40:19 +03:00
return $this->item;
2019-11-16 10:21:14 +03:00
}
}