From a5f303f338436bd9ad64819a6e963482d3e6d3dd Mon Sep 17 00:00:00 2001 From: David Bomba Date: Fri, 10 Feb 2023 09:47:22 +1100 Subject: [PATCH] Support passing reference with amount_paid and paid --- app/Services/Invoice/ApplyPaymentAmount.php | 16 ++-------------- app/Services/Invoice/InvoiceService.php | 8 ++++---- app/Services/Invoice/MarkPaid.php | 9 ++------- app/Services/Invoice/TriggeredActions.php | 4 ++-- 4 files changed, 10 insertions(+), 27 deletions(-) diff --git a/app/Services/Invoice/ApplyPaymentAmount.php b/app/Services/Invoice/ApplyPaymentAmount.php index 8079278a5c53..ef7524e807d8 100644 --- a/app/Services/Invoice/ApplyPaymentAmount.php +++ b/app/Services/Invoice/ApplyPaymentAmount.php @@ -14,14 +14,10 @@ namespace App\Services\Invoice; use App\Events\Invoice\InvoiceWasPaid; use App\Events\Payment\PaymentWasCreated; use App\Factory\PaymentFactory; -use App\Jobs\Invoice\InvoiceWorkflowSettings; -use App\Jobs\Payment\EmailPayment; use App\Libraries\Currency\Conversion\CurrencyApi; -use App\Models\Client; use App\Models\Invoice; use App\Models\Payment; use App\Services\AbstractService; -use App\Services\Client\ClientService; use App\Utils\Ninja; use App\Utils\Traits\GeneratesCounter; use Illuminate\Support\Carbon; @@ -30,15 +26,7 @@ class ApplyPaymentAmount extends AbstractService { use GeneratesCounter; - private $invoice; - - private $amount; - - public function __construct(Invoice $invoice, $amount) - { - $this->invoice = $invoice; - $this->amount = $amount; - } + public function __construct(private Invoice $invoice, private float $amount, private ?string $reference){} public function run() { @@ -63,7 +51,7 @@ class ApplyPaymentAmount extends AbstractService $payment->number = $this->getNextPaymentNumber($this->invoice->client, $payment); $payment->status_id = Payment::STATUS_COMPLETED; $payment->client_id = $this->invoice->client_id; - $payment->transaction_reference = ctrans('texts.manual_entry'); + $payment->transaction_reference = $this->reference ?: ctrans('texts.manual_entry'); $payment->currency_id = $this->invoice->client->getSetting('currency_id'); $payment->is_manual = true; /* Create a payment relationship to the invoice entity */ diff --git a/app/Services/Invoice/InvoiceService.php b/app/Services/Invoice/InvoiceService.php index 74812486a980..fa1149d281eb 100644 --- a/app/Services/Invoice/InvoiceService.php +++ b/app/Services/Invoice/InvoiceService.php @@ -42,18 +42,18 @@ class InvoiceService * and executes child sub functions. * @return $this InvoiceService object */ - public function markPaid() + public function markPaid(?string $reference = null) { $this->removeUnpaidGatewayFees(); - $this->invoice = (new MarkPaid($this->invoice))->run(); + $this->invoice = (new MarkPaid($this->invoice, $reference))->run(); return $this; } - public function applyPaymentAmount($amount) + public function applyPaymentAmount($amount, ?string $reference = null) { - $this->invoice = (new ApplyPaymentAmount($this->invoice, $amount))->run(); + $this->invoice = (new ApplyPaymentAmount($this->invoice, $amount, $reference))->run(); return $this; } diff --git a/app/Services/Invoice/MarkPaid.php b/app/Services/Invoice/MarkPaid.php index 756ec69d8030..b4f69a6f3e30 100644 --- a/app/Services/Invoice/MarkPaid.php +++ b/app/Services/Invoice/MarkPaid.php @@ -26,14 +26,9 @@ class MarkPaid extends AbstractService { use GeneratesCounter; - private $invoice; - private $payable_balance; - public function __construct(Invoice $invoice) - { - $this->invoice = $invoice; - } + public function __construct(private Invoice $invoice, private ?string $reference){} public function run() { @@ -73,7 +68,7 @@ class MarkPaid extends AbstractService $payment->applied = $this->payable_balance; $payment->status_id = Payment::STATUS_COMPLETED; $payment->client_id = $this->invoice->client_id; - $payment->transaction_reference = ctrans('texts.manual_entry'); + $payment->transaction_reference = $this->reference ?: ctrans('texts.manual_entry'); $payment->currency_id = $this->invoice->client->getSetting('currency_id'); $payment->is_manual = true; diff --git a/app/Services/Invoice/TriggeredActions.php b/app/Services/Invoice/TriggeredActions.php index 0404f17025cb..82f5b580af95 100644 --- a/app/Services/Invoice/TriggeredActions.php +++ b/app/Services/Invoice/TriggeredActions.php @@ -43,7 +43,7 @@ class TriggeredActions extends AbstractService } if ($this->request->has('paid') && $this->request->input('paid') == 'true') { - $this->invoice = $this->invoice->service()->markPaid()->save(); //update notification sends automatically for this. + $this->invoice = $this->invoice->service()->markPaid($this->request->input('reference'))->save(); //update notification sends automatically for this. } if ($this->request->has('mark_sent') && $this->request->input('mark_sent') == 'true' && $this->invoice->status_id == Invoice::STATUS_DRAFT) { @@ -52,7 +52,7 @@ class TriggeredActions extends AbstractService } if ($this->request->has('amount_paid') && is_numeric($this->request->input('amount_paid'))) { - $this->invoice = $this->invoice->service()->applyPaymentAmount($this->request->input('amount_paid'))->save(); + $this->invoice = $this->invoice->service()->applyPaymentAmount($this->request->input('amount_paid'), $this->request->input('reference'))->save(); $this->updated = false; }