108 lines
3.9 KiB
PHP
108 lines
3.9 KiB
PHP
<?php
|
|
|
|
namespace App\Jobs\Document;
|
|
|
|
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 implements ShouldUpdate
|
|
{
|
|
use Relationships;
|
|
|
|
public function handle(): Document
|
|
{
|
|
$this->authorize();
|
|
|
|
if (empty($this->request['amount'])) {
|
|
$this->request['amount'] = 0;
|
|
}
|
|
|
|
// Disable this lines for global discount issue fixed ( https://github.com/akaunting/akaunting/issues/2797 )
|
|
$this->request['discount_rate'] = $this->request['discount'] ?? null;
|
|
|
|
event(new DocumentUpdating($this->model, $this->request));
|
|
|
|
// Track original contact_id to sync transactions if it changes
|
|
$originalContactId = $this->model->contact_id;
|
|
|
|
\DB::transaction(function () use ($originalContactId) {
|
|
// Upload attachment
|
|
if ($this->request->file('attachment')) {
|
|
$this->deleteMediaModel($this->model, 'attachment', $this->request);
|
|
|
|
foreach ($this->request->file('attachment') as $attachment) {
|
|
$media = $this->getMedia($attachment, Str::plural($this->model->type));
|
|
|
|
$this->model->attachMedia($media, 'attachment');
|
|
}
|
|
} elseif ($this->request->isNotApi() && ! $this->request->file('attachment') && $this->model->attachment) {
|
|
$this->deleteMediaModel($this->model, 'attachment', $this->request);
|
|
} elseif ($this->request->isApi() && $this->request->has('remove_attachment') && $this->model->attachment) {
|
|
$this->deleteMediaModel($this->model, 'attachment', $this->request);
|
|
}
|
|
|
|
$this->deleteRelationships($this->model, ['items', 'item_taxes', 'totals'], true);
|
|
|
|
$this->dispatch(new CreateDocumentItemsAndTotals($this->model, $this->request));
|
|
|
|
$this->model->paid_amount = $this->model->paid;
|
|
|
|
event(new PaidAmountCalculated($this->model));
|
|
|
|
if ($this->model->paid_amount > 0) {
|
|
if ($this->request['amount'] == $this->model->paid_amount) {
|
|
$this->request['status'] = 'paid';
|
|
}
|
|
|
|
if ($this->request['amount'] > $this->model->paid_amount) {
|
|
$this->request['status'] = 'partial';
|
|
}
|
|
}
|
|
|
|
unset($this->model->reconciled);
|
|
unset($this->model->paid_amount);
|
|
|
|
$this->model->update($this->request->all());
|
|
|
|
// Sync transaction contact_id if document contact changed
|
|
if (isset($this->request['contact_id']) && $originalContactId != $this->request['contact_id']) {
|
|
$this->model->transactions()->update([
|
|
'contact_id' => $this->request['contact_id'],
|
|
]);
|
|
}
|
|
|
|
$this->model->updateRecurring($this->request->all());
|
|
});
|
|
|
|
event(new DocumentUpdated($this->model, $this->request));
|
|
|
|
return $this->model;
|
|
}
|
|
|
|
/**
|
|
* Determine if this action is applicable.
|
|
*/
|
|
public function authorize(): void
|
|
{
|
|
$lockedStatuses = ['sent', 'received', 'viewed', 'partial', 'paid', 'overdue', 'unpaid', 'cancelled'];
|
|
|
|
if (
|
|
isset($this->request['contact_id']) &&
|
|
(int) $this->request['contact_id'] !== (int) $this->model->contact_id &&
|
|
in_array($this->model->status, $lockedStatuses)
|
|
) {
|
|
$type = Str::plural($this->model->type);
|
|
$message = trans('messages.warning.contact_change', ['type' => trans_choice("general.$type", 1)]);
|
|
|
|
throw new \Exception($message);
|
|
}
|
|
}
|
|
}
|