akaunting/app/Jobs/Document/CreateDocumentHistory.php

68 lines
1.9 KiB
PHP
Raw Permalink Normal View History

2020-12-24 01:28:38 +03:00
<?php
namespace App\Jobs\Document;
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\Document\Document;
2020-12-24 01:28:38 +03:00
use App\Models\Document\DocumentHistory;
2021-09-10 01:49:49 +03:00
use Illuminate\Http\Request;
2020-12-24 01:28:38 +03:00
2021-09-07 10:33:34 +03:00
class CreateDocumentHistory extends Job implements HasOwner, HasSource, ShouldCreate
2020-12-24 01:28:38 +03:00
{
protected $document;
protected $notify;
protected $description;
2021-09-10 01:20:20 +03:00
public function __construct(Document $document, $notify = 0, $description = null, $request = null)
2020-12-24 01:28:38 +03:00
{
$this->document = $document;
$this->notify = $notify;
$this->description = $description;
2021-09-10 01:20:20 +03:00
$this->request = $request;
2021-09-06 11:53:57 +03:00
parent::__construct($document, $notify, $description);
2020-12-24 01:28:38 +03:00
}
2021-09-06 11:53:57 +03:00
public function handle(): DocumentHistory
2020-12-24 01:28:38 +03:00
{
$description = $this->description ?: trans_choice('general.payments', 1);
$document_history = DocumentHistory::create([
'company_id' => $this->document->company_id,
'type' => $this->document->type,
'document_id' => $this->document->id,
'status' => $this->document->status,
'notify' => $this->notify,
'description' => $description,
2021-09-10 01:20:20 +03:00
'created_from' => $this->getCustomSourceName(),
2021-09-07 10:33:34 +03:00
'created_by' => user_id(),
2020-12-24 01:28:38 +03:00
]);
return $document_history;
}
2021-09-10 01:20:20 +03:00
public function getCustomSourceName(): string
{
2021-09-10 01:49:49 +03:00
if (empty($this->request)) {
2021-09-10 01:20:20 +03:00
return $this->getSourceName();
}
2021-09-10 01:49:49 +03:00
if (is_array($this->request)) {
if (empty($this->request['created_from'])) {
return $this->getSourceName();
}
} elseif ($this->request instanceof Request) {
if ($this->request->missing('created_from')) {
return $this->getSourceName();
}
}
2021-09-10 01:20:20 +03:00
return $this->request['created_from'];
}
2020-12-24 01:28:38 +03:00
}