Akaunting/app/Jobs/Document/UpdateDocument.php

87 lines
3.2 KiB
PHP
Raw Normal View History

2020-12-23 22:28:38 +00:00
<?php
namespace App\Jobs\Document;
use App\Abstracts\Job;
use App\Events\Document\PaidAmountCalculated;
use App\Events\Document\DocumentUpdated;
use App\Events\Document\DocumentUpdating;
2021-09-06 08:53:57 +00:00
use App\Interfaces\Job\ShouldUpdate;
2020-12-23 22:28:38 +00:00
use App\Jobs\Document\CreateDocumentItemsAndTotals;
use App\Models\Document\Document;
use App\Traits\Relationships;
use Illuminate\Support\Str;
2021-09-06 08:53:57 +00:00
class UpdateDocument extends Job implements ShouldUpdate
2020-12-23 22:28:38 +00:00
{
use Relationships;
2021-09-06 08:53:57 +00:00
public function handle(): Document
2020-12-23 22:28:38 +00:00
{
if (empty($this->request['amount'])) {
$this->request['amount'] = 0;
}
// Disable this lines for global discount issue fixed ( https://github.com/akaunting/akaunting/issues/2797 )
2025-08-09 11:20:45 +00:00
$this->request['discount_rate'] = $this->request['discount'] ?? null;
2021-09-06 08:53:57 +00:00
event(new DocumentUpdating($this->model, $this->request));
2020-12-23 22:28:38 +00:00
// Track original contact_id to sync transactions if it changes
$originalContactId = $this->model->contact_id;
\DB::transaction(function () use ($originalContactId) {
2020-12-23 22:28:38 +00:00
// Upload attachment
if ($this->request->file('attachment')) {
2021-09-06 08:53:57 +00:00
$this->deleteMediaModel($this->model, 'attachment', $this->request);
2020-12-23 22:28:38 +00:00
2021-01-22 09:38:17 +00:00
foreach ($this->request->file('attachment') as $attachment) {
2021-09-06 08:53:57 +00:00
$media = $this->getMedia($attachment, Str::plural($this->model->type));
2021-01-22 09:38:17 +00:00
2021-09-06 08:53:57 +00:00
$this->model->attachMedia($media, 'attachment');
2021-01-22 09:38:17 +00:00
}
2025-12-07 15:31:41 +00:00
} 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) {
2021-09-06 08:53:57 +00:00
$this->deleteMediaModel($this->model, 'attachment', $this->request);
2020-12-23 22:28:38 +00:00
}
$this->deleteRelationships($this->model, ['items', 'item_taxes', 'totals'], true);
2020-12-23 22:28:38 +00:00
2021-09-06 08:53:57 +00:00
$this->dispatch(new CreateDocumentItemsAndTotals($this->model, $this->request));
2020-12-23 22:28:38 +00:00
2021-09-06 08:53:57 +00:00
$this->model->paid_amount = $this->model->paid;
2021-09-06 08:53:57 +00:00
event(new PaidAmountCalculated($this->model));
2020-12-23 22:28:38 +00:00
2021-09-06 08:53:57 +00:00
if ($this->model->paid_amount > 0) {
if ($this->request['amount'] == $this->model->paid_amount) {
$this->request['status'] = 'paid';
}
2021-09-06 08:53:57 +00:00
if ($this->request['amount'] > $this->model->paid_amount) {
$this->request['status'] = 'partial';
}
2020-12-23 22:28:38 +00:00
}
2021-09-06 08:53:57 +00:00
unset($this->model->reconciled);
unset($this->model->paid_amount);
2020-12-23 22:28:38 +00:00
2021-09-06 08:53:57 +00:00
$this->model->update($this->request->all());
2020-12-23 22:28:38 +00:00
// 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'],
]);
}
2021-09-06 08:53:57 +00:00
$this->model->updateRecurring($this->request->all());
2020-12-23 22:28:38 +00:00
});
2021-09-06 08:53:57 +00:00
event(new DocumentUpdated($this->model, $this->request));
2020-12-23 22:28:38 +00:00
2021-09-06 08:53:57 +00:00
return $this->model;
2020-12-23 22:28:38 +00:00
}
}