diff --git a/app/Console/Commands/CheckData.php b/app/Console/Commands/CheckData.php index aacec46b0573..138486fdaea8 100644 --- a/app/Console/Commands/CheckData.php +++ b/app/Console/Commands/CheckData.php @@ -500,9 +500,11 @@ class CheckData extends Command { $client = Client::withTrashed()->find($_client->client_id); + $credits_from_reversal = Credit::withTrashed()->where('client_id', $client->id)->where('is_deleted', 0)->whereNotNull('invoice_id')->sum('amount'); + $credits_used_for_payments = $this->clientCreditPaymentables($client); - $total_paid_to_date = $_client->payments_applied + $credits_used_for_payments[0]->credit_payment; + $total_paid_to_date = $_client->payments_applied + $credits_used_for_payments[0]->credit_payment - $credits_from_reversal; if(round($total_paid_to_date,2) != round($_client->client_paid_to_date,2)){ diff --git a/app/Jobs/Ninja/TransactionLog.php b/app/Jobs/Ninja/TransactionLog.php index cdf113a61b5f..f5873a1c8a6e 100644 --- a/app/Jobs/Ninja/TransactionLog.php +++ b/app/Jobs/Ninja/TransactionLog.php @@ -17,6 +17,7 @@ use App\DataMapper\Transactions\InvoiceCancelledTransaction; use App\DataMapper\Transactions\InvoiceDeletedTransaction; use App\DataMapper\Transactions\InvoiceFeeAppliedTransaction; use App\DataMapper\Transactions\InvoicePaymentTransaction; +use App\DataMapper\Transactions\InvoiceReversalTransaction; use App\DataMapper\Transactions\InvoiceUpdatedTransaction; use App\DataMapper\Transactions\MarkPaidTransaction; use App\DataMapper\Transactions\PaymentAppliedTransaction; @@ -54,6 +55,7 @@ class TransactionLog implements ShouldQueue TransactionEvent::INVOICE_DELETED => InvoiceDeletedTransaction::class, TransactionEvent::INVOICE_PAYMENT_APPLIED => InvoicePaymentTransaction::class, TransactionEvent::INVOICE_CANCELLED => InvoiceCancelledTransaction::class, + TransactionEvent::INVOICE_REVERSED => InvoiceReversalTransaction::class, TransactionEvent::INVOICE_FEE_APPLIED => InvoiceFeeAppliedTransaction::class, TransactionEvent::PAYMENT_MADE => PaymentMadeTransaction::class, TransactionEvent::GATEWAY_PAYMENT_MADE => GatewayPaymentMadeTransaction::class, diff --git a/app/Models/Client.php b/app/Models/Client.php index b4005ca44a6d..71317443deb3 100644 --- a/app/Models/Client.php +++ b/app/Models/Client.php @@ -843,13 +843,13 @@ class Client extends BaseModel implements HasLocalePreference public function transaction_event() { - $this->fresh(); + $client = $this->fresh(); return [ - 'client_id' => $this->id, - 'client_balance' => $this->balance ?: 0, - 'client_paid_to_date' => $this->paid_to_date ?: 0, - 'client_credit_balance' => $this->credit_balance ?: 0 + 'client_id' => $client->id, + 'client_balance' => $client->balance ?: 0, + 'client_paid_to_date' => $client->paid_to_date ?: 0, + 'client_credit_balance' => $client->credit_balance ?: 0 ]; } diff --git a/app/Models/Credit.php b/app/Models/Credit.php index fbecaca907c7..310041dbefd4 100644 --- a/app/Models/Credit.php +++ b/app/Models/Credit.php @@ -305,13 +305,13 @@ class Credit extends BaseModel public function transaction_event() { - $this->fresh(); + $credit = $this->fresh(); return [ - 'credit_id' => $this->id, - 'credit_amount' => $this->amount ?: 0, - 'credit_balance' => $this->balance ?: 0, - 'credit_status' => $this->status_id ?: 1, + 'credit_id' => $credit->id, + 'credit_amount' => $credit->amount ?: 0, + 'credit_balance' => $credit->balance ?: 0, + 'credit_status' => $credit->status_id ?: 1, ]; } } diff --git a/app/Models/Invoice.php b/app/Models/Invoice.php index 66917320695c..3af186248df8 100644 --- a/app/Models/Invoice.php +++ b/app/Models/Invoice.php @@ -539,15 +539,15 @@ class Invoice extends BaseModel public function transaction_event() { - $this->fresh(); + $invoice = $this->fresh(); return [ - 'invoice_id' => $this->id, - 'invoice_amount' => $this->amount ?: 0, - 'invoice_partial' => $this->partial ?: 0, - 'invoice_balance' => $this->balance ?: 0, - 'invoice_paid_to_date' => $this->paid_to_date ?: 0, - 'invoice_status' => $this->status_id ?: 1, + 'invoice_id' => $invoice->id, + 'invoice_amount' => $invoice->amount ?: 0, + 'invoice_partial' => $invoice->partial ?: 0, + 'invoice_balance' => $invoice->balance ?: 0, + 'invoice_paid_to_date' => $invoice->paid_to_date ?: 0, + 'invoice_status' => $invoice->status_id ?: 1, ]; } } diff --git a/app/Models/Payment.php b/app/Models/Payment.php index fa033cbaf583..113d9a1cde4c 100644 --- a/app/Models/Payment.php +++ b/app/Models/Payment.php @@ -326,15 +326,15 @@ class Payment extends BaseModel public function transaction_event() { - $this->fresh(); + $payment = $this->fresh(); return [ - 'payment_id' => $this->id, - 'payment_amount' => $this->amount ?: 0, - 'payment_applied' => $this->applied ?: 0, - 'payment_refunded' => $this->refunded ?: 0, - 'payment_status' => $this->status_id ?: 1, - 'paymentables' => $this->paymentables->toArray(), + 'payment_id' => $payment->id, + 'payment_amount' => $payment->amount ?: 0, + 'payment_applied' => $payment->applied ?: 0, + 'payment_refunded' => $payment->refunded ?: 0, + 'payment_status' => $payment->status_id ?: 1, + 'paymentables' => $payment->paymentables->toArray(), 'payment_request' => request() ? request()->all() : [], ]; } diff --git a/app/Models/TransactionEvent.php b/app/Models/TransactionEvent.php index cfba96915bd6..1df68c0f1995 100644 --- a/app/Models/TransactionEvent.php +++ b/app/Models/TransactionEvent.php @@ -33,7 +33,8 @@ class TransactionEvent extends StaticModel public const INVOICE_PAYMENT_APPLIED = 4; public const INVOICE_CANCELLED = 5; public const INVOICE_FEE_APPLIED = 6; - + public const INVOICE_REVERSED = 7; + public const PAYMENT_MADE = 100; public const PAYMENT_APPLIED = 101; public const PAYMENT_REFUND = 102; diff --git a/app/Services/Invoice/HandleReversal.php b/app/Services/Invoice/HandleReversal.php index fe9297350b06..053b2650b0b8 100644 --- a/app/Services/Invoice/HandleReversal.php +++ b/app/Services/Invoice/HandleReversal.php @@ -15,11 +15,13 @@ use App\Events\Invoice\InvoiceWasReversed; use App\Factory\CreditFactory; use App\Factory\InvoiceItemFactory; use App\Helpers\Invoice\InvoiceSum; +use App\Jobs\Ninja\TransactionLog; use App\Models\Client; use App\Models\Credit; use App\Models\Invoice; use App\Models\Payment; use App\Models\Paymentable; +use App\Models\TransactionEvent; use App\Services\AbstractService; use App\Utils\Ninja; use App\Utils\Traits\GeneratesCounter; @@ -136,6 +138,16 @@ class HandleReversal extends AbstractService event(new InvoiceWasReversed($this->invoice, $this->invoice->company, Ninja::eventVars())); + $transaction = [ + 'invoice' => $this->invoice->transaction_event(), + 'payment' => $this->payment->transaction_event(), + 'client' => $this->invoice->client->transaction_event(), + 'credit' => [], + 'metadata' => [], + ]; + + TransactionLog::dispatch(TransactionEvent::PAYMENT_REVERSED, $transaction, $this->invoice->company->db); + return $this->invoice; //create a ledger row for this with the resulting Credit ( also include an explanation in the notes section ) }