From 7cd632764924076744960daca64bf3e5ac757e63 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Thu, 10 Mar 2022 13:27:31 +1100 Subject: [PATCH] Transaction logs --- app/Jobs/Ninja/TransactionLog.php | 12 +++---- app/Services/Payment/RefundPayment.php | 47 ++++++++++++++++++++------ 2 files changed, 42 insertions(+), 17 deletions(-) diff --git a/app/Jobs/Ninja/TransactionLog.php b/app/Jobs/Ninja/TransactionLog.php index f5873a1c8a..b0247db6f3 100644 --- a/app/Jobs/Ninja/TransactionLog.php +++ b/app/Jobs/Ninja/TransactionLog.php @@ -50,19 +50,19 @@ class TransactionLog implements ShouldQueue private $event_transformer; private array $transformer_array = [ - TransactionEvent::INVOICE_MARK_PAID => MarkPaidTransaction::class, - TransactionEvent::INVOICE_UPDATED => InvoiceUpdatedTransaction::class, - TransactionEvent::INVOICE_DELETED => InvoiceDeletedTransaction::class, + TransactionEvent::INVOICE_MARK_PAID => MarkPaidTransaction::class, // + TransactionEvent::INVOICE_UPDATED => InvoiceUpdatedTransaction::class, // + 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::INVOICE_REVERSED => InvoiceReversalTransaction::class, // + TransactionEvent::INVOICE_FEE_APPLIED => InvoiceFeeAppliedTransaction::class, // TransactionEvent::PAYMENT_MADE => PaymentMadeTransaction::class, TransactionEvent::GATEWAY_PAYMENT_MADE => GatewayPaymentMadeTransaction::class, TransactionEvent::PAYMENT_APPLIED => PaymentAppliedTransaction::class, TransactionEvent::PAYMENT_REFUND => PaymentRefundedTransaction::class, TransactionEvent::PAYMENT_FAILED => PaymentFailedTransaction::class, - TransactionEvent::PAYMENT_DELETED => PaymentDeletedTransaction::class, + TransactionEvent::PAYMENT_DELETED => PaymentDeletedTransaction::class, // TransactionEvent::CLIENT_STATUS => ClientStatusTransaction::class, ]; diff --git a/app/Services/Payment/RefundPayment.php b/app/Services/Payment/RefundPayment.php index 0e8dd1a877..d846989cf8 100644 --- a/app/Services/Payment/RefundPayment.php +++ b/app/Services/Payment/RefundPayment.php @@ -53,7 +53,7 @@ class RefundPayment public function run() { - return $this->calculateTotalRefund() //sets amount for the refund (needed if we are refunding multiple invoices in one payment) + $this->payment = $this->calculateTotalRefund() //sets amount for the refund (needed if we are refunding multiple invoices in one payment) ->setStatus() //sets status of payment //->reversePayment() //->buildCreditNote() //generate the credit note @@ -64,7 +64,6 @@ class RefundPayment ->processGatewayRefund() //process the gateway refund if needed ->save(); - $transaction = [ 'invoice' => [], 'payment' => $this->payment->transaction_event(), @@ -74,6 +73,8 @@ class RefundPayment ]; TransactionLog::dispatch(TransactionEvent::PAYMENT_REFUND, $transaction, $this->payment->company->db); + + return $this->payment; } /** @@ -269,27 +270,51 @@ class RefundPayment $adjustment_amount += $refunded_invoice['amount']; $client->balance += $refunded_invoice['amount']; - //$client->paid_to_date -= $refunded_invoice['amount'];//todo refund balancing $client->save(); - //todo adjust ledger balance here? or after and reference the credit and its total + + $transaction = [ + 'invoice' => $invoice->transaction_event(), + 'payment' => [], + 'client' => $client->transaction_event(), + 'credit' => [], + 'metadata' => [], + ]; + + TransactionLog::dispatch(TransactionEvent::PAYMENT_REFUND, $transaction, $invoice->company->db); + } - // $ledger_string = "Refund for Invoice {$invoice->number} for amount " . $refunded_invoice['amount']; //todo - - // $this->credit_note->ledger()->updateCreditBalance($adjustment_amount, $ledger_string); - - $client = $this->payment->client->fresh(); - + $client = $this->payment->client->fresh(); $client->service()->updatePaidToDate(-1 * $refunded_invoice['amount'])->save(); + + + $transaction = [ + 'invoice' => [], + 'payment' => [], + 'client' => $client->transaction_event(), + 'credit' => [], + 'metadata' => [], + ]; + + TransactionLog::dispatch(TransactionEvent::PAYMENT_REFUND, $transaction, $client->company->db); + } else{ //if we are refunding and no payments have been tagged, then we need to decrement the client->paid_to_date by the total refund amount. $client = $this->payment->client->fresh(); - $client->service()->updatePaidToDate(-1 * $this->total_refund)->save(); + $transaction = [ + 'invoice' => [], + 'payment' => [], + 'client' => $client->transaction_event(), + 'credit' => [], + 'metadata' => [], + ]; + + TransactionLog::dispatch(TransactionEvent::PAYMENT_REFUND, $transaction, $client->company->db); } return $this;