added pointer interfaces for jobs
This commit is contained in:
@@ -7,34 +7,26 @@ use App\Models\Document\Document;
|
||||
|
||||
class CancelDocument extends Job
|
||||
{
|
||||
protected $document;
|
||||
protected $model;
|
||||
|
||||
/**
|
||||
* Create a new job instance.
|
||||
*
|
||||
* @param $document
|
||||
*/
|
||||
public function __construct($document)
|
||||
public function __construct(Document $model)
|
||||
{
|
||||
$this->document = $document;
|
||||
$this->model = $model;
|
||||
|
||||
parent::__construct($model);
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the job.
|
||||
*
|
||||
* @return Document
|
||||
*/
|
||||
public function handle()
|
||||
public function handle(): Document
|
||||
{
|
||||
\DB::transaction(function () {
|
||||
$this->deleteRelationships($this->document, [
|
||||
$this->deleteRelationships($this->model, [
|
||||
'transactions', 'recurring'
|
||||
]);
|
||||
|
||||
$this->document->status = 'cancelled';
|
||||
$this->document->save();
|
||||
$this->model->status = 'cancelled';
|
||||
$this->model->save();
|
||||
});
|
||||
|
||||
return $this->document;
|
||||
return $this->model;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,33 +5,15 @@ namespace App\Jobs\Document;
|
||||
use App\Abstracts\Job;
|
||||
use App\Events\Document\DocumentCreated;
|
||||
use App\Events\Document\DocumentCreating;
|
||||
use App\Interfaces\Job\HasOwner;
|
||||
use App\Interfaces\Job\ShouldCreate;
|
||||
use App\Jobs\Document\CreateDocumentItemsAndTotals;
|
||||
use App\Models\Document\Document;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
class CreateDocument extends Job
|
||||
class CreateDocument extends Job implements HasOwner, ShouldCreate
|
||||
{
|
||||
protected $document;
|
||||
|
||||
protected $request;
|
||||
|
||||
/**
|
||||
* Create a new job instance.
|
||||
*
|
||||
* @param $request
|
||||
*/
|
||||
public function __construct($request)
|
||||
{
|
||||
$this->request = $this->getRequestInstance($request);
|
||||
$this->request->merge(['created_by' => user_id()]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the job.
|
||||
*
|
||||
* @return Document
|
||||
*/
|
||||
public function handle()
|
||||
public function handle(): Document
|
||||
{
|
||||
if (empty($this->request['amount'])) {
|
||||
$this->request['amount'] = 0;
|
||||
@@ -40,26 +22,26 @@ class CreateDocument extends Job
|
||||
event(new DocumentCreating($this->request));
|
||||
|
||||
\DB::transaction(function () {
|
||||
$this->document = Document::create($this->request->all());
|
||||
$this->model = Document::create($this->request->all());
|
||||
|
||||
// Upload attachment
|
||||
if ($this->request->file('attachment')) {
|
||||
foreach ($this->request->file('attachment') as $attachment) {
|
||||
$media = $this->getMedia($attachment, Str::plural($this->document->type));
|
||||
$media = $this->getMedia($attachment, Str::plural($this->model->type));
|
||||
|
||||
$this->document->attachMedia($media, 'attachment');
|
||||
$this->model->attachMedia($media, 'attachment');
|
||||
}
|
||||
}
|
||||
|
||||
$this->dispatch(new CreateDocumentItemsAndTotals($this->document, $this->request));
|
||||
$this->dispatch(new CreateDocumentItemsAndTotals($this->model, $this->request));
|
||||
|
||||
$this->document->update($this->request->all());
|
||||
$this->model->update($this->request->all());
|
||||
|
||||
$this->document->createRecurring($this->request->all());
|
||||
$this->model->createRecurring($this->request->all());
|
||||
});
|
||||
|
||||
event(new DocumentCreated($this->document, $this->request));
|
||||
event(new DocumentCreated($this->model, $this->request));
|
||||
|
||||
return $this->document;
|
||||
return $this->model;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,9 +3,11 @@
|
||||
namespace App\Jobs\Document;
|
||||
|
||||
use App\Abstracts\Job;
|
||||
use App\Interfaces\Job\ShouldCreate;
|
||||
use App\Models\Document\Document;
|
||||
use App\Models\Document\DocumentHistory;
|
||||
|
||||
class CreateDocumentHistory extends Job
|
||||
class CreateDocumentHistory extends Job implements ShouldCreate
|
||||
{
|
||||
protected $document;
|
||||
|
||||
@@ -13,26 +15,16 @@ class CreateDocumentHistory extends Job
|
||||
|
||||
protected $description;
|
||||
|
||||
/**
|
||||
* Create a new job instance.
|
||||
*
|
||||
* @param $document
|
||||
* @param $notify
|
||||
* @param $description
|
||||
*/
|
||||
public function __construct($document, $notify = 0, $description = null)
|
||||
public function __construct(Document $document, $notify = 0, $description = null)
|
||||
{
|
||||
$this->document = $document;
|
||||
$this->notify = $notify;
|
||||
$this->description = $description;
|
||||
|
||||
parent::__construct($document, $notify, $description);
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the job.
|
||||
*
|
||||
* @return DocumentHistory
|
||||
*/
|
||||
public function handle()
|
||||
public function handle(): DocumentHistory
|
||||
{
|
||||
$description = $this->description ?: trans_choice('general.payments', 1);
|
||||
|
||||
|
||||
@@ -3,35 +3,28 @@
|
||||
namespace App\Jobs\Document;
|
||||
|
||||
use App\Abstracts\Job;
|
||||
use App\Interfaces\Job\ShouldCreate;
|
||||
use App\Models\Document\Document;
|
||||
use App\Models\Document\DocumentItem;
|
||||
use App\Models\Document\DocumentItemTax;
|
||||
use App\Models\Setting\Tax;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
class CreateDocumentItem extends Job
|
||||
class CreateDocumentItem extends Job implements ShouldCreate
|
||||
{
|
||||
protected $document;
|
||||
|
||||
protected $request;
|
||||
|
||||
/**
|
||||
* Create a new job instance.
|
||||
*
|
||||
* @param $document
|
||||
* @param $request
|
||||
*/
|
||||
public function __construct($document, $request)
|
||||
public function __construct(Document $document, $request)
|
||||
{
|
||||
$this->document = $document;
|
||||
$this->request = $request;
|
||||
|
||||
parent::__construct($document, $request);
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the job.
|
||||
*
|
||||
* @return DocumentItem
|
||||
*/
|
||||
public function handle()
|
||||
public function handle(): DocumentItem
|
||||
{
|
||||
$item_id = !empty($this->request['item_id']) ? $this->request['item_id'] : 0;
|
||||
$precision = config('money.' . $this->document->currency_code . '.precision');
|
||||
|
||||
@@ -3,36 +3,27 @@
|
||||
namespace App\Jobs\Document;
|
||||
|
||||
use App\Abstracts\Job;
|
||||
use App\Traits\DateTime;
|
||||
use App\Traits\Currencies;
|
||||
use App\Interfaces\Job\ShouldCreate;
|
||||
use App\Jobs\Common\CreateItem;
|
||||
use App\Models\Document\Document;
|
||||
use App\Models\Document\DocumentTotal;
|
||||
use App\Traits\Currencies;
|
||||
use App\Traits\DateTime;
|
||||
|
||||
class CreateDocumentItemsAndTotals extends Job
|
||||
class CreateDocumentItemsAndTotals extends Job implements ShouldCreate
|
||||
{
|
||||
use Currencies, DateTime;
|
||||
|
||||
protected $document;
|
||||
|
||||
protected $request;
|
||||
|
||||
/**
|
||||
* Create a new job instance.
|
||||
*
|
||||
* @param $request
|
||||
*/
|
||||
public function __construct($document, $request)
|
||||
public function __construct(Document $document, $request)
|
||||
{
|
||||
$this->document = $document;
|
||||
$this->request = $this->getRequestInstance($request);
|
||||
|
||||
parent::__construct($request);
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the job.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function handle()
|
||||
public function handle(): void
|
||||
{
|
||||
$precision = config('money.' . $this->document->currency_code . '.precision');
|
||||
|
||||
@@ -152,7 +143,7 @@ class CreateDocumentItemsAndTotals extends Job
|
||||
]);
|
||||
}
|
||||
|
||||
protected function createItems()
|
||||
protected function createItems(): array
|
||||
{
|
||||
$sub_total = $discount_amount = $discount_amount_total = 0;
|
||||
|
||||
|
||||
@@ -3,40 +3,24 @@
|
||||
namespace App\Jobs\Document;
|
||||
|
||||
use App\Abstracts\Job;
|
||||
use App\Interfaces\Job\ShouldDelete;
|
||||
use App\Observers\Transaction;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
class DeleteDocument extends Job
|
||||
class DeleteDocument extends Job implements ShouldDelete
|
||||
{
|
||||
protected $document;
|
||||
|
||||
/**
|
||||
* Create a new job instance.
|
||||
*
|
||||
* @param $document
|
||||
*/
|
||||
public function __construct($document)
|
||||
{
|
||||
$this->document = $document;
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the job.
|
||||
*
|
||||
* @return boolean|Exception
|
||||
*/
|
||||
public function handle()
|
||||
public function handle(): bool
|
||||
{
|
||||
$this->authorize();
|
||||
|
||||
\DB::transaction(function () {
|
||||
Transaction::mute();
|
||||
|
||||
$this->deleteRelationships($this->document, [
|
||||
$this->deleteRelationships($this->model, [
|
||||
'items', 'item_taxes', 'histories', 'transactions', 'recurring', 'totals'
|
||||
]);
|
||||
|
||||
$this->document->delete();
|
||||
$this->model->delete();
|
||||
|
||||
Transaction::unmute();
|
||||
});
|
||||
@@ -46,13 +30,11 @@ class DeleteDocument extends Job
|
||||
|
||||
/**
|
||||
* Determine if this action is applicable.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function authorize()
|
||||
public function authorize(): void
|
||||
{
|
||||
if ($this->document->transactions()->isReconciled()->count()) {
|
||||
$type = Str::plural($this->document->type);
|
||||
if ($this->model->transactions()->isReconciled()->count()) {
|
||||
$type = Str::plural($this->model->type);
|
||||
$message = trans('messages.warning.reconciled_doc', ['type' => trans_choice("general.$type", 1)]);
|
||||
|
||||
throw new \Exception($message);
|
||||
|
||||
@@ -8,29 +8,19 @@ use App\Models\Document\Document;
|
||||
|
||||
class DuplicateDocument extends Job
|
||||
{
|
||||
protected $document;
|
||||
|
||||
protected $clone;
|
||||
|
||||
/**
|
||||
* Create a new job instance.
|
||||
*
|
||||
* @param $document
|
||||
*/
|
||||
public function __construct($document)
|
||||
public function __construct(Document $model)
|
||||
{
|
||||
$this->document = $document;
|
||||
$this->model = $model;
|
||||
|
||||
parent::__construct($model);
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the job.
|
||||
*
|
||||
* @return Document
|
||||
*/
|
||||
public function handle()
|
||||
public function handle(): Document
|
||||
{
|
||||
\DB::transaction(function () {
|
||||
$this->clone = $this->document->duplicate();
|
||||
$this->clone = $this->model->duplicate();
|
||||
});
|
||||
|
||||
event(new DocumentCreated($this->clone, request()));
|
||||
|
||||
@@ -6,85 +6,66 @@ use App\Abstracts\Job;
|
||||
use App\Events\Document\PaidAmountCalculated;
|
||||
use App\Events\Document\DocumentUpdated;
|
||||
use App\Events\Document\DocumentUpdating;
|
||||
use App\Interfaces\Job\ShouldUpdate;
|
||||
use App\Jobs\Document\CreateDocumentItemsAndTotals;
|
||||
use App\Models\Document\Document;
|
||||
use App\Traits\Relationships;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
class UpdateDocument extends Job
|
||||
class UpdateDocument extends Job implements ShouldUpdate
|
||||
{
|
||||
use Relationships;
|
||||
|
||||
protected $document;
|
||||
|
||||
protected $request;
|
||||
|
||||
/**
|
||||
* Create a new job instance.
|
||||
*
|
||||
* @param $request
|
||||
*/
|
||||
public function __construct($document, $request)
|
||||
{
|
||||
$this->document = $document;
|
||||
$this->request = $this->getRequestInstance($request);
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the job.
|
||||
*
|
||||
* @return Document
|
||||
*/
|
||||
public function handle()
|
||||
public function handle(): Document
|
||||
{
|
||||
if (empty($this->request['amount'])) {
|
||||
$this->request['amount'] = 0;
|
||||
}
|
||||
|
||||
event(new DocumentUpdating($this->document, $this->request));
|
||||
event(new DocumentUpdating($this->model, $this->request));
|
||||
|
||||
\DB::transaction(function () {
|
||||
// Upload attachment
|
||||
if ($this->request->file('attachment')) {
|
||||
$this->deleteMediaModel($this->document, 'attachment', $this->request);
|
||||
$this->deleteMediaModel($this->model, 'attachment', $this->request);
|
||||
|
||||
foreach ($this->request->file('attachment') as $attachment) {
|
||||
$media = $this->getMedia($attachment, Str::plural($this->document->type));
|
||||
$media = $this->getMedia($attachment, Str::plural($this->model->type));
|
||||
|
||||
$this->document->attachMedia($media, 'attachment');
|
||||
$this->model->attachMedia($media, 'attachment');
|
||||
}
|
||||
} elseif (!$this->request->file('attachment') && $this->document->attachment) {
|
||||
$this->deleteMediaModel($this->document, 'attachment', $this->request);
|
||||
} elseif (!$this->request->file('attachment') && $this->model->attachment) {
|
||||
$this->deleteMediaModel($this->model, 'attachment', $this->request);
|
||||
}
|
||||
|
||||
$this->deleteRelationships($this->document, ['items', 'item_taxes', 'totals']);
|
||||
$this->deleteRelationships($this->model, ['items', 'item_taxes', 'totals']);
|
||||
|
||||
$this->dispatch(new CreateDocumentItemsAndTotals($this->document, $this->request));
|
||||
$this->dispatch(new CreateDocumentItemsAndTotals($this->model, $this->request));
|
||||
|
||||
$this->document->paid_amount = $this->document->paid;
|
||||
$this->model->paid_amount = $this->model->paid;
|
||||
|
||||
event(new PaidAmountCalculated($this->document));
|
||||
event(new PaidAmountCalculated($this->model));
|
||||
|
||||
if ($this->document->paid_amount > 0) {
|
||||
if ($this->request['amount'] == $this->document->paid_amount) {
|
||||
if ($this->model->paid_amount > 0) {
|
||||
if ($this->request['amount'] == $this->model->paid_amount) {
|
||||
$this->request['status'] = 'paid';
|
||||
}
|
||||
|
||||
if ($this->request['amount'] > $this->document->paid_amount) {
|
||||
if ($this->request['amount'] > $this->model->paid_amount) {
|
||||
$this->request['status'] = 'partial';
|
||||
}
|
||||
}
|
||||
|
||||
unset($this->document->reconciled);
|
||||
unset($this->document->paid_amount);
|
||||
unset($this->model->reconciled);
|
||||
unset($this->model->paid_amount);
|
||||
|
||||
$this->document->update($this->request->all());
|
||||
$this->model->update($this->request->all());
|
||||
|
||||
$this->document->updateRecurring($this->request->all());
|
||||
$this->model->updateRecurring($this->request->all());
|
||||
});
|
||||
|
||||
event(new DocumentUpdated($this->document, $this->request));
|
||||
event(new DocumentUpdated($this->model, $this->request));
|
||||
|
||||
return $this->document;
|
||||
return $this->model;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user