akaunting/app/Observers/Transaction.php

68 lines
1.6 KiB
PHP
Raw Normal View History

2019-11-16 10:21:14 +03:00
<?php
namespace App\Observers;
2019-12-31 15:49:09 +03:00
use App\Jobs\Purchase\CreateBillHistory;
use App\Jobs\Sale\CreateInvoiceHistory;
2019-11-16 10:21:14 +03:00
use App\Models\Banking\Transaction as Model;
2019-12-22 15:58:48 +03:00
use App\Traits\Jobs;
2019-11-16 10:21:14 +03:00
class Transaction
{
2019-12-22 15:58:48 +03:00
use Jobs;
2019-11-16 10:21:14 +03:00
/**
* Listen to the deleted event.
*
* @param Model $transaction
* @return void
*/
public function deleted(Model $transaction)
{
2019-12-22 15:58:48 +03:00
if (empty($transaction->document_id)) {
return;
2019-11-16 10:21:14 +03:00
}
2019-12-22 15:58:48 +03:00
$function = ($transaction->type == 'income') ? 'updateInvoice' : 'updateBill';
$this->$function($transaction);
2019-11-16 10:21:14 +03:00
}
protected function updateInvoice($transaction)
{
if (session('deleting_invoice')) {
return;
}
2019-11-16 10:21:14 +03:00
$invoice = $transaction->invoice;
2020-01-11 16:57:32 +03:00
$invoice->status = ($invoice->transactions->count() > 1) ? 'partial' : 'sent';
2019-11-16 10:21:14 +03:00
$invoice->save();
2019-12-22 15:58:48 +03:00
$this->dispatch(new CreateInvoiceHistory($invoice, 0, $this->getDescription($transaction)));
2019-11-16 10:21:14 +03:00
}
protected function updateBill($transaction)
{
if (session('deleting_bill')) {
return;
}
2019-11-16 10:21:14 +03:00
$bill = $transaction->bill;
2020-01-11 16:57:32 +03:00
$bill->status = ($bill->transactions->count() > 1) ? 'partial' : 'received';
2019-11-16 10:21:14 +03:00
$bill->save();
2019-12-22 15:58:48 +03:00
$this->dispatch(new CreateBillHistory($bill, 0, $this->getDescription($transaction)));
}
2019-11-16 10:21:14 +03:00
2019-12-22 15:58:48 +03:00
protected function getDescription($transaction)
{
$amount = money((double) $transaction->amount, (string) $transaction->currency_code, true)->format();
2019-11-16 10:21:14 +03:00
2019-12-22 15:58:48 +03:00
return trans('messages.success.deleted', ['type' => $amount . ' ' . trans_choice('general.payments', 1)]);
2019-11-16 10:21:14 +03:00
}
2019-12-22 15:58:48 +03:00
}