improved document events
This commit is contained in:
parent
37c968abf3
commit
6c8e98ee74
@ -3,7 +3,6 @@
|
||||
namespace App\Abstracts;
|
||||
|
||||
use App\Abstracts\Model;
|
||||
use App\Events\Document\PaidAmountCalculated;
|
||||
use App\Models\Setting\Tax;
|
||||
use App\Traits\Currencies;
|
||||
use App\Traits\DateTime;
|
||||
@ -116,13 +115,9 @@ abstract class DocumentModel extends Model
|
||||
|
||||
$this->setAttribute('reconciled', $reconciled);
|
||||
|
||||
// TODO: find a cleaner way compatible with observer pattern
|
||||
$model = clone $this;
|
||||
$model->paid_amount = $paid;
|
||||
event(new PaidAmountCalculated($model));
|
||||
|
||||
return round($model->paid_amount, $precision);
|
||||
return round($paid, $precision);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the status label.
|
||||
*
|
||||
|
@ -6,6 +6,7 @@ use App\Abstracts\Job;
|
||||
use App\Jobs\Banking\CreateTransaction;
|
||||
use App\Jobs\Purchase\CreateBillHistory;
|
||||
use App\Jobs\Sale\CreateInvoiceHistory;
|
||||
use App\Events\Document\PaidAmountCalculated;
|
||||
use App\Models\Banking\Transaction;
|
||||
use App\Models\Sale\Invoice;
|
||||
use App\Traits\Currencies;
|
||||
@ -64,11 +65,17 @@ class CreateDocumentTransaction extends Job
|
||||
|
||||
protected function prepareRequest()
|
||||
{
|
||||
if (!isset($this->request['amount'])) {
|
||||
$this->model->paid_amount = $this->model->paid;
|
||||
event(new PaidAmountCalculated($this->model));
|
||||
|
||||
$this->request['amount'] = $this->model->amount - $this->model->paid_amount;
|
||||
}
|
||||
|
||||
$this->request['company_id'] = session('company_id');
|
||||
$this->request['currency_code'] = isset($this->request['currency_code']) ? $this->request['currency_code'] : $this->model->currency_code;
|
||||
$this->request['type'] = ($this->model instanceof Invoice) ? 'income' : 'expense';
|
||||
$this->request['paid_at'] = isset($this->request['paid_at']) ? $this->request['paid_at'] : Date::now()->format('Y-m-d');
|
||||
$this->request['amount'] = isset($this->request['amount']) ? $this->request['amount'] : ($this->model->amount - $this->model->paid);
|
||||
$this->request['currency_rate'] = config('money.' . $this->request['currency_code'] . '.rate');
|
||||
$this->request['account_id'] = isset($this->request['account_id']) ? $this->request['account_id'] : setting('default.account');
|
||||
$this->request['document_id'] = isset($this->request['document_id']) ? $this->request['document_id'] : $this->model->id;
|
||||
@ -92,8 +99,13 @@ class CreateDocumentTransaction extends Job
|
||||
$amount = round($converted_amount, $precision);
|
||||
}
|
||||
|
||||
$total_amount = round($this->model->amount - $this->model->paid, $precision);
|
||||
$this->model->paid_amount = $this->model->paid;
|
||||
event(new PaidAmountCalculated($this->model));
|
||||
|
||||
$total_amount = round($this->model->amount - $this->model->paid_amount, $precision);
|
||||
|
||||
unset($this->model->reconciled);
|
||||
unset($this->model->paid_amount);
|
||||
|
||||
$compare = bccomp($amount, $total_amount, $precision);
|
||||
|
||||
|
@ -3,6 +3,7 @@
|
||||
namespace App\Jobs\Purchase;
|
||||
|
||||
use App\Abstracts\Job;
|
||||
use App\Events\Document\PaidAmountCalculated;
|
||||
use App\Events\Purchase\BillUpdated;
|
||||
use App\Events\Purchase\BillUpdating;
|
||||
use App\Jobs\Purchase\CreateBillItemsAndTotals;
|
||||
@ -53,14 +54,16 @@ class UpdateBill extends Job
|
||||
|
||||
$this->dispatch(new CreateBillItemsAndTotals($this->bill, $this->request));
|
||||
|
||||
$bill_paid = $this->bill->paid;
|
||||
$this->bill->paid_amount = $this->bill->paid;
|
||||
event(new PaidAmountCalculated($this->bill));
|
||||
|
||||
unset($this->bill->reconciled);
|
||||
|
||||
if (($bill_paid) && $this->request['amount'] > $bill_paid) {
|
||||
if ($this->request['amount'] > $this->bill->paid_amount) {
|
||||
$this->request['status'] = 'partial';
|
||||
}
|
||||
|
||||
unset($this->bill->reconciled);
|
||||
unset($this->bill->paid_amount);
|
||||
|
||||
$this->bill->update($this->request->input());
|
||||
|
||||
$this->bill->updateRecurring();
|
||||
|
@ -3,6 +3,7 @@
|
||||
namespace App\Jobs\Sale;
|
||||
|
||||
use App\Abstracts\Job;
|
||||
use App\Events\Document\PaidAmountCalculated;
|
||||
use App\Events\Sale\InvoiceUpdated;
|
||||
use App\Events\Sale\InvoiceUpdating;
|
||||
use App\Jobs\Sale\CreateInvoiceItemsAndTotals;
|
||||
@ -53,14 +54,16 @@ class UpdateInvoice extends Job
|
||||
|
||||
$this->dispatch(new CreateInvoiceItemsAndTotals($this->invoice, $this->request));
|
||||
|
||||
$invoice_paid = $this->invoice->paid;
|
||||
$this->invoice->paid_amount = $this->invoice->paid;
|
||||
event(new PaidAmountCalculated($this->invoice));
|
||||
|
||||
unset($this->invoice->reconciled);
|
||||
|
||||
if (($invoice_paid) && $this->request['amount'] > $invoice_paid) {
|
||||
if ($this->request['amount'] > $this->invoice->paid_amount) {
|
||||
$this->request['status'] = 'partial';
|
||||
}
|
||||
|
||||
unset($this->invoice->reconciled);
|
||||
unset($this->invoice->paid_amount);
|
||||
|
||||
$this->invoice->update($this->request->all());
|
||||
|
||||
$this->invoice->updateRecurring();
|
||||
|
@ -34,12 +34,12 @@ class Transaction extends Observer
|
||||
{
|
||||
$invoice = $transaction->invoice;
|
||||
|
||||
// TODO: find a cleaner way compatible with observer pattern
|
||||
$model = clone $invoice;
|
||||
$model->transactions_count = $invoice->transactions->count();
|
||||
event(new TransactionsCounted($model));
|
||||
$invoice->transactions_count = $invoice->transactions->count();
|
||||
event(new TransactionsCounted($invoice));
|
||||
|
||||
$invoice->status = ($model->transactions_count > 1) ? 'partial' : 'sent';
|
||||
$invoice->status = ($invoice->transactions_count > 0) ? 'partial' : 'sent';
|
||||
|
||||
unset($invoice->transactions_count);
|
||||
|
||||
$invoice->save();
|
||||
|
||||
@ -50,12 +50,12 @@ class Transaction extends Observer
|
||||
{
|
||||
$bill = $transaction->bill;
|
||||
|
||||
// TODO: find a cleaner way compatible with observer pattern
|
||||
$model = clone $bill;
|
||||
$model->transactions_count = $bill->transactions->count();
|
||||
event(new TransactionsCounted($model));
|
||||
$bill->transactions_count = $bill->transactions->count();
|
||||
event(new TransactionsCounted($bill));
|
||||
|
||||
$bill->status = ($model->transactions_count > 1) ? 'partial' : 'received';
|
||||
$bill->status = ($bill->transactions_count > 0) ? 'partial' : 'received';
|
||||
|
||||
unset($bill->transactions_count);
|
||||
|
||||
$bill->save();
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user