Support passing reference with amount_paid and paid

This commit is contained in:
David Bomba 2023-02-10 09:47:22 +11:00
parent 1bae14c337
commit a5f303f338
4 changed files with 10 additions and 27 deletions

View File

@ -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 */

View File

@ -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;
}

View File

@ -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;

View File

@ -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;
}