akaunting/app/Jobs/Common/CreateItemTaxes.php

58 lines
1.4 KiB
PHP
Raw Normal View History

2020-12-08 13:30:06 +03:00
<?php
namespace App\Jobs\Common;
use App\Abstracts\Job;
2021-09-07 10:33:34 +03:00
use App\Interfaces\Job\HasOwner;
use App\Interfaces\Job\HasSource;
2021-09-06 11:53:57 +03:00
use App\Interfaces\Job\ShouldCreate;
use App\Models\Common\Item;
2020-12-08 13:30:06 +03:00
use App\Models\Common\ItemTax;
2021-09-07 10:33:34 +03:00
class CreateItemTaxes extends Job implements HasOwner, HasSource, ShouldCreate
2020-12-08 13:30:06 +03:00
{
protected $item;
protected $request;
2021-09-06 11:53:57 +03:00
public function __construct(Item $item, $request)
2020-12-08 13:30:06 +03:00
{
$this->item = $item;
$this->request = $request;
2021-09-06 11:53:57 +03:00
parent::__construct($item, $request);
2020-12-08 13:30:06 +03:00
}
/**
* Execute the job.
*
2020-12-14 16:04:54 +03:00
* @return mixed
2021-09-06 11:53:57 +03:00
* @todo type hint after upgrading to PHP 8
2020-12-08 13:30:06 +03:00
*/
public function handle()
{
2020-12-14 16:04:54 +03:00
// BC for 2.0 version
if (!empty($this->request['tax_id'])) {
$this->request['tax_ids'][] = $this->request['tax_id'];
}
if (empty($this->request['tax_ids'])) {
return false;
}
2020-12-08 13:30:06 +03:00
\DB::transaction(function () {
2020-12-14 16:04:54 +03:00
foreach ($this->request['tax_ids'] as $tax_id) {
2020-12-08 13:30:06 +03:00
ItemTax::create([
'company_id' => $this->item->company_id,
'item_id' => $this->item->id,
2020-12-14 16:04:54 +03:00
'tax_id' => $tax_id,
2021-09-07 10:33:34 +03:00
'created_from' => $this->request['created_from'],
'created_by' => $this->request['created_by'],
2020-12-08 13:30:06 +03:00
]);
}
});
return $this->item->taxes;
}
}