2020-07-07 14:33:11 +02:00
|
|
|
<?php
|
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
2020-07-07 14:33:11 +02:00
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
2024-04-12 06:15:41 +02:00
|
|
|
* @copyright Copyright (c) 2024. Invoice Ninja LLC (https://invoiceninja.com)
|
2020-07-07 14:33:11 +02:00
|
|
|
*
|
2021-06-16 08:58:16 +02:00
|
|
|
* @license https://www.elastic.co/licensing/elastic-license
|
2020-07-07 14:33:11 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
namespace App\Services\Invoice;
|
|
|
|
|
2024-06-06 23:41:46 +02:00
|
|
|
use Carbon\Carbon;
|
|
|
|
use App\Utils\Ninja;
|
|
|
|
use App\Utils\Number;
|
2023-04-26 22:59:50 +02:00
|
|
|
use App\Models\Client;
|
2020-10-12 06:10:34 +02:00
|
|
|
use App\Models\Credit;
|
2020-07-07 14:33:11 +02:00
|
|
|
use App\Models\Invoice;
|
|
|
|
use App\Models\Payment;
|
2024-06-06 23:41:46 +02:00
|
|
|
use App\Libraries\MultiDB;
|
2020-09-04 00:01:17 +02:00
|
|
|
use App\Models\PaymentHash;
|
2020-10-26 05:06:58 +01:00
|
|
|
use App\Models\PaymentType;
|
2024-06-06 23:41:46 +02:00
|
|
|
use Illuminate\Support\Str;
|
|
|
|
use App\DataMapper\InvoiceItem;
|
2024-08-29 06:36:38 +02:00
|
|
|
use App\Events\Invoice\InvoiceAutoBillFailed;
|
|
|
|
use App\Events\Invoice\InvoiceAutoBillSuccess;
|
2024-06-06 23:41:46 +02:00
|
|
|
use App\Factory\PaymentFactory;
|
|
|
|
use App\Services\AbstractService;
|
|
|
|
use App\Models\ClientGatewayToken;
|
|
|
|
use App\Events\Invoice\InvoiceWasPaid;
|
2024-02-17 09:03:20 +01:00
|
|
|
use App\Repositories\CreditRepository;
|
|
|
|
use App\Repositories\PaymentRepository;
|
2024-06-06 23:41:46 +02:00
|
|
|
use App\Events\Payment\PaymentWasCreated;
|
2020-07-07 14:33:11 +02:00
|
|
|
|
|
|
|
class AutoBillInvoice extends AbstractService
|
|
|
|
{
|
2023-04-26 22:59:50 +02:00
|
|
|
private Client $client;
|
2020-10-07 13:03:53 +02:00
|
|
|
|
2023-04-26 22:59:50 +02:00
|
|
|
private array $used_credit = [];
|
2024-06-14 09:09:44 +02:00
|
|
|
|
2022-09-06 11:18:05 +02:00
|
|
|
/*Specific variable for partial payments */
|
|
|
|
private bool $is_partial_amount = false;
|
|
|
|
|
2023-04-26 22:59:50 +02:00
|
|
|
public function __construct(private Invoice $invoice, protected string $db)
|
2020-07-07 14:33:11 +02:00
|
|
|
{
|
2024-08-22 08:45:06 +02:00
|
|
|
|
2024-07-27 00:48:51 +02:00
|
|
|
$this->client = $this->invoice->client;
|
|
|
|
|
2020-07-07 14:33:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function run()
|
|
|
|
{
|
2021-09-03 14:59:48 +02:00
|
|
|
MultiDB::setDb($this->db);
|
|
|
|
|
2023-04-26 22:59:50 +02:00
|
|
|
/* @var \App\Modesl\Client $client */
|
2024-08-22 08:45:06 +02:00
|
|
|
|
2021-05-26 08:04:38 +02:00
|
|
|
$is_partial = false;
|
|
|
|
|
2020-10-07 13:03:53 +02:00
|
|
|
/* Is the invoice payable? */
|
2022-06-21 11:57:17 +02:00
|
|
|
if (! $this->invoice->isPayable()) {
|
2020-07-08 04:20:44 +02:00
|
|
|
return $this->invoice;
|
2022-06-21 11:57:17 +02:00
|
|
|
}
|
2020-10-28 11:10:49 +01:00
|
|
|
|
2020-10-07 13:03:53 +02:00
|
|
|
/* Mark the invoice as sent */
|
2020-07-15 08:08:57 +02:00
|
|
|
$this->invoice = $this->invoice->service()->markSent()->save();
|
|
|
|
|
2020-10-07 13:03:53 +02:00
|
|
|
/* Mark the invoice as paid if there is no balance */
|
2024-07-16 23:31:56 +02:00
|
|
|
if (floatval($this->invoice->balance) == 0) {
|
2022-03-15 10:20:05 +01:00
|
|
|
return $this->invoice->service()->markPaid()->save();
|
2022-06-21 11:57:17 +02:00
|
|
|
}
|
2021-01-29 12:00:30 +01:00
|
|
|
|
2020-10-09 03:59:59 +02:00
|
|
|
//if the credits cover the payments, we stop here, build the payment with credits and exit early
|
2022-06-21 11:57:17 +02:00
|
|
|
if ($this->client->getSetting('use_credits_payment') != 'off') {
|
2020-10-28 11:10:49 +01:00
|
|
|
$this->applyCreditPayment();
|
2022-06-21 11:57:17 +02:00
|
|
|
}
|
2020-07-07 14:33:11 +02:00
|
|
|
|
2024-02-17 08:41:40 +01:00
|
|
|
if($this->client->getSetting('use_unapplied_payment') != 'off') {
|
|
|
|
$this->applyUnappliedPayment();
|
|
|
|
}
|
|
|
|
|
2022-09-06 11:18:05 +02:00
|
|
|
//If this returns true, it means a partial invoice amount was paid as a credit and there is no further balance payable
|
2024-02-18 05:32:49 +01:00
|
|
|
if (($this->is_partial_amount && $this->invoice->partial == 0) || (int)$this->invoice->balance == 0) {
|
2022-09-06 11:18:05 +02:00
|
|
|
return;
|
2023-02-16 02:36:09 +01:00
|
|
|
}
|
2022-09-06 11:18:05 +02:00
|
|
|
|
2021-09-03 07:38:20 +02:00
|
|
|
$amount = 0;
|
2023-04-26 22:59:50 +02:00
|
|
|
$invoice_total = 0;
|
2021-09-03 07:38:20 +02:00
|
|
|
|
2020-10-07 13:03:53 +02:00
|
|
|
/* Determine $amount */
|
2020-11-25 15:19:52 +01:00
|
|
|
if ($this->invoice->partial > 0) {
|
2021-05-26 08:04:38 +02:00
|
|
|
$is_partial = true;
|
2021-09-22 03:57:03 +02:00
|
|
|
$invoice_total = $this->invoice->balance;
|
2020-09-04 00:01:17 +02:00
|
|
|
$amount = $this->invoice->partial;
|
2020-11-25 15:19:52 +01:00
|
|
|
} elseif ($this->invoice->balance > 0) {
|
2020-09-04 00:01:17 +02:00
|
|
|
$amount = $this->invoice->balance;
|
2020-11-25 15:19:52 +01:00
|
|
|
} else {
|
2020-10-23 06:18:16 +02:00
|
|
|
return $this->invoice;
|
2020-11-25 15:19:52 +01:00
|
|
|
}
|
2020-10-07 13:03:53 +02:00
|
|
|
|
2024-09-09 02:45:30 +02:00
|
|
|
nlog("Auto Bill - balance remains to be paid!! - {$amount}");
|
|
|
|
nlog($this->invoice->amount);
|
|
|
|
nlog($this->invoice->balance);
|
2020-10-12 06:30:53 +02:00
|
|
|
|
2021-01-27 22:31:31 +01:00
|
|
|
/* Retrieve the Client Gateway Token */
|
2023-04-26 22:59:50 +02:00
|
|
|
/** @var \App\Models\ClientGatewayToken $gateway_token */
|
2020-10-07 13:03:53 +02:00
|
|
|
$gateway_token = $this->getGateway($amount);
|
|
|
|
|
|
|
|
/* Bail out if no payment methods available */
|
2022-06-21 11:57:17 +02:00
|
|
|
if (! $gateway_token || ! $gateway_token->gateway || ! $gateway_token->gateway->driver($this->client)->token_billing) {
|
|
|
|
nlog('Bailing out - no suitable gateway token found.');
|
|
|
|
|
2023-04-26 22:59:50 +02:00
|
|
|
throw new \Exception(ctrans('texts.no_payment_method_specified'));
|
|
|
|
// return $this->invoice;
|
2021-09-03 07:38:20 +02:00
|
|
|
}
|
2021-01-29 12:00:30 +01:00
|
|
|
|
2024-09-09 02:45:30 +02:00
|
|
|
nlog("Gateway present - adding gateway fee on {$amount}");
|
2021-09-04 07:51:31 +02:00
|
|
|
|
2020-10-07 13:03:53 +02:00
|
|
|
/* $gateway fee */
|
2021-01-27 22:31:31 +01:00
|
|
|
$this->invoice = $this->invoice->service()->addGatewayFee($gateway_token->gateway, $gateway_token->gateway_type_id, $amount)->save();
|
|
|
|
|
2024-09-09 02:45:30 +02:00
|
|
|
|
|
|
|
nlog($this->invoice->amount);
|
|
|
|
nlog($this->invoice->balance);
|
|
|
|
|
2021-09-22 03:57:03 +02:00
|
|
|
//change from $this->invoice->amount to $this->invoice->balance
|
2022-06-21 11:57:17 +02:00
|
|
|
if ($is_partial) {
|
2021-09-22 03:57:03 +02:00
|
|
|
$fee = $this->invoice->balance - $invoice_total;
|
2022-06-21 11:57:17 +02:00
|
|
|
} else {
|
2021-09-22 03:57:03 +02:00
|
|
|
$fee = $this->invoice->balance - $amount;
|
2022-06-21 11:57:17 +02:00
|
|
|
}
|
2021-09-22 03:57:03 +02:00
|
|
|
|
2024-09-09 02:45:30 +02:00
|
|
|
nlog("fee is {$fee}");
|
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
if ($fee > $amount) {
|
2021-09-22 03:57:03 +02:00
|
|
|
$fee = 0;
|
2022-06-21 11:57:17 +02:00
|
|
|
}
|
2020-07-08 04:20:44 +02:00
|
|
|
|
2020-10-07 13:03:53 +02:00
|
|
|
/* Build payment hash */
|
2022-01-05 05:32:07 +01:00
|
|
|
|
2020-09-04 00:01:17 +02:00
|
|
|
$payment_hash = PaymentHash::create([
|
2023-10-15 22:25:47 +02:00
|
|
|
'hash' => Str::random(32),
|
2023-03-17 02:49:08 +01:00
|
|
|
'data' => [
|
2023-03-18 08:24:56 +01:00
|
|
|
'amount_with_fee' => $amount + $fee,
|
2023-03-17 02:49:08 +01:00
|
|
|
'invoices' => [
|
|
|
|
[
|
2023-03-18 08:24:56 +01:00
|
|
|
'invoice_id' => $this->invoice->hashed_id,
|
|
|
|
'amount' => $amount,
|
|
|
|
'invoice_number' => $this->invoice->number,
|
2023-03-17 02:49:08 +01:00
|
|
|
'pre_payment' => $this->invoice->is_proforma,
|
|
|
|
],
|
|
|
|
],
|
|
|
|
],
|
2020-09-04 00:01:17 +02:00
|
|
|
'fee_total' => $fee,
|
|
|
|
'fee_invoice_id' => $this->invoice->id,
|
|
|
|
]);
|
2020-07-08 04:20:44 +02:00
|
|
|
|
2021-09-04 07:51:31 +02:00
|
|
|
nlog("Payment hash created => {$payment_hash->id}");
|
2024-09-09 02:45:30 +02:00
|
|
|
|
2021-10-04 13:29:23 +02:00
|
|
|
$payment = false;
|
2022-05-17 01:33:52 +02:00
|
|
|
try {
|
|
|
|
$payment = $gateway_token->gateway
|
|
|
|
->driver($this->client)
|
|
|
|
->setPaymentHash($payment_hash)
|
|
|
|
->tokenBilling($gateway_token, $payment_hash);
|
|
|
|
} catch (\Exception $e) {
|
2022-05-17 06:46:03 +02:00
|
|
|
|
2024-04-19 02:32:16 +02:00
|
|
|
nlog('payment NOT captured for '.$this->invoice->number.' with error '.$e->getMessage());
|
2024-08-29 06:36:38 +02:00
|
|
|
event(new InvoiceAutoBillFailed($this->invoice, $this->invoice->company, Ninja::eventVars(), $e->getMessage()));
|
|
|
|
|
2024-04-19 02:32:16 +02:00
|
|
|
}
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2024-09-04 01:17:46 +02:00
|
|
|
$this->invoice = $this->invoice->fresh();
|
2024-04-19 02:32:16 +02:00
|
|
|
$this->invoice->auto_bill_tries += 1;
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2024-04-19 02:32:16 +02:00
|
|
|
if ($this->invoice->auto_bill_tries == 3) {
|
2024-10-21 04:29:59 +02:00
|
|
|
|
|
|
|
\App\Models\Invoice::where('id', $this->invoice->id)->update([
|
|
|
|
'auto_bill_enabled' => false,
|
|
|
|
'auto_bill_tries' => 0
|
|
|
|
]);
|
|
|
|
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
|
|
|
|
\App\Models\Invoice::where('id', $this->invoice->id)->update([
|
|
|
|
'auto_bill_tries' => $this->invoice->auto_bill_tries
|
|
|
|
]);
|
|
|
|
|
2022-05-17 01:33:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($payment) {
|
2022-06-21 11:57:17 +02:00
|
|
|
info('Auto Bill payment captured for '.$this->invoice->number);
|
2024-08-29 06:36:38 +02:00
|
|
|
event(new InvoiceAutoBillSuccess($this->invoice, $this->invoice->company, Ninja::eventVars()));
|
2021-08-05 02:46:03 +02:00
|
|
|
}
|
2020-07-07 14:33:11 +02:00
|
|
|
}
|
|
|
|
|
2020-10-08 02:28:23 +02:00
|
|
|
/**
|
|
|
|
* If the credits on file cover the invoice amount
|
|
|
|
* the we create a matching payment using credits only
|
2020-10-28 11:10:49 +01:00
|
|
|
*
|
2020-10-08 02:28:23 +02:00
|
|
|
* @return Invoice $invoice
|
|
|
|
*/
|
|
|
|
private function finalizePaymentUsingCredits()
|
|
|
|
{
|
|
|
|
$amount = array_sum(array_column($this->used_credit, 'amount'));
|
|
|
|
|
|
|
|
$payment = PaymentFactory::create($this->invoice->company_id, $this->invoice->user_id);
|
2023-11-10 05:01:51 +01:00
|
|
|
|
|
|
|
$payment->amount = 0;
|
|
|
|
$payment->applied = 0;
|
2020-10-08 05:31:02 +02:00
|
|
|
$payment->client_id = $this->invoice->client_id;
|
|
|
|
$payment->currency_id = $this->invoice->client->getSetting('currency_id');
|
2023-11-15 05:36:24 +01:00
|
|
|
$payment->date = now()->addSeconds($this->invoice->company->utc_offset())->format('Y-m-d');
|
2020-10-08 05:31:02 +02:00
|
|
|
$payment->status_id = Payment::STATUS_COMPLETED;
|
2020-10-26 03:06:24 +01:00
|
|
|
$payment->type_id = PaymentType::CREDIT;
|
2020-10-08 05:31:02 +02:00
|
|
|
$payment->service()->applyNumber()->save();
|
2020-10-08 02:28:23 +02:00
|
|
|
|
|
|
|
$payment->invoices()->attach($this->invoice->id, ['amount' => $amount]);
|
|
|
|
|
2021-01-24 07:44:14 +01:00
|
|
|
$this->invoice
|
2022-09-06 11:18:05 +02:00
|
|
|
->service()
|
|
|
|
->setCalculatedStatus()
|
|
|
|
->save();
|
2024-01-14 05:05:00 +01:00
|
|
|
|
2023-04-26 22:59:50 +02:00
|
|
|
$current_credit = false;
|
2020-10-08 02:28:23 +02:00
|
|
|
|
2020-11-25 15:19:52 +01:00
|
|
|
foreach ($this->used_credit as $credit) {
|
2023-08-08 10:56:31 +02:00
|
|
|
$current_credit = Credit::query()->find($credit['credit_id']);
|
2021-01-24 07:44:14 +01:00
|
|
|
$payment->credits()
|
|
|
|
->attach($current_credit->id, ['amount' => $credit['amount']]);
|
2020-10-28 11:10:49 +01:00
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
info("adjusting credit balance {$current_credit->balance} by this amount ".$credit['amount']);
|
2020-10-23 06:18:16 +02:00
|
|
|
|
2024-06-06 23:41:46 +02:00
|
|
|
|
|
|
|
$item_date = Carbon::parse($payment->date)->format($payment->client->date_format());
|
|
|
|
$invoice_numbers = $this->invoice->number;
|
|
|
|
|
|
|
|
$item = new InvoiceItem();
|
|
|
|
$item->quantity = 0;
|
|
|
|
$item->cost = $credit['amount'] * -1;
|
|
|
|
$item->notes = "{$item_date} - " . ctrans('texts.credit_payment', ['invoice_number' => $invoice_numbers]) . " ". Number::formatMoney($credit['amount'], $payment->client);
|
|
|
|
$item->type_id = "1";
|
|
|
|
|
|
|
|
$line_items = $current_credit->line_items;
|
|
|
|
$line_items[] = $item;
|
|
|
|
$current_credit->line_items = $line_items;
|
|
|
|
|
|
|
|
|
2021-01-24 07:44:14 +01:00
|
|
|
$current_credit->service()
|
2022-06-21 11:57:17 +02:00
|
|
|
->adjustBalance($credit['amount'] * -1)
|
2021-01-24 07:44:14 +01:00
|
|
|
->updatePaidToDate($credit['amount'])
|
|
|
|
->setCalculatedStatus()
|
|
|
|
->save();
|
2024-06-06 23:41:46 +02:00
|
|
|
|
2020-11-25 15:19:52 +01:00
|
|
|
}
|
2020-10-08 02:28:23 +02:00
|
|
|
|
2020-11-25 15:19:52 +01:00
|
|
|
$payment->ledger()
|
2024-04-28 05:52:44 +02:00
|
|
|
->updatePaymentBalance($amount * -1, "AutoBill")
|
2022-03-04 23:21:17 +01:00
|
|
|
->save();
|
|
|
|
|
2022-09-06 11:18:05 +02:00
|
|
|
$this->invoice
|
|
|
|
->client
|
|
|
|
->service()
|
|
|
|
->updateBalanceAndPaidToDate($amount * -1, $amount)
|
|
|
|
->adjustCreditBalance($amount * -1)
|
|
|
|
->save();
|
2020-10-08 02:28:23 +02:00
|
|
|
|
2023-04-26 22:59:50 +02:00
|
|
|
$credit_reference = $current_credit ? $current_credit->number : 'unknown';
|
|
|
|
|
2022-03-09 02:31:54 +01:00
|
|
|
$this->invoice->ledger() //09-03-2022
|
2023-04-26 22:59:50 +02:00
|
|
|
->updateCreditBalance($amount * -1, "Credit {$credit_reference} used to pay down Invoice {$this->invoice->number}")
|
2022-09-06 11:18:05 +02:00
|
|
|
->save();
|
2020-10-08 02:28:23 +02:00
|
|
|
|
2021-12-17 12:11:36 +01:00
|
|
|
event('eloquent.created: App\Models\Payment', $payment);
|
2020-11-25 15:19:52 +01:00
|
|
|
event(new PaymentWasCreated($payment, $payment->company, Ninja::eventVars()));
|
2020-10-08 05:31:02 +02:00
|
|
|
|
2023-04-16 08:41:40 +02:00
|
|
|
//if we have paid the invoice in full using credits, then we need to fire the event
|
2023-10-26 04:57:44 +02:00
|
|
|
if($this->invoice->balance == 0) {
|
2023-04-16 08:41:40 +02:00
|
|
|
event(new InvoiceWasPaid($this->invoice, $payment, $payment->company, Ninja::eventVars()));
|
|
|
|
}
|
|
|
|
|
2021-01-24 07:44:14 +01:00
|
|
|
return $this->invoice
|
|
|
|
->service()
|
|
|
|
->setCalculatedStatus()
|
2024-06-06 23:41:46 +02:00
|
|
|
->workFlow() //07-06-2024 - run the workflow if paid!
|
2021-01-24 07:44:14 +01:00
|
|
|
->save();
|
2020-10-08 02:28:23 +02:00
|
|
|
}
|
2024-06-14 09:09:44 +02:00
|
|
|
|
2024-02-17 08:41:40 +01:00
|
|
|
/**
|
|
|
|
* If the client has unapplied payments on file
|
2024-06-14 09:09:44 +02:00
|
|
|
* we will use these prior to charging a
|
2024-02-17 08:41:40 +01:00
|
|
|
* payment method on file.
|
|
|
|
*
|
|
|
|
* This needs to be wrapped in a transaction.
|
|
|
|
*
|
|
|
|
* @return self
|
|
|
|
*/
|
2024-07-27 00:48:51 +02:00
|
|
|
public function applyUnappliedPayment(): self
|
2024-02-17 08:41:40 +01:00
|
|
|
{
|
2024-02-18 05:06:20 +01:00
|
|
|
$unapplied_payments = Payment::query()
|
|
|
|
->where('client_id', $this->client->id)
|
2024-02-17 08:41:40 +01:00
|
|
|
->where('status_id', Payment::STATUS_COMPLETED)
|
|
|
|
->where('is_deleted', false)
|
2024-02-18 05:32:49 +01:00
|
|
|
->whereColumn('amount', '>', 'applied')
|
2024-02-17 08:41:40 +01:00
|
|
|
->where('amount', '>', 0)
|
|
|
|
->orderBy('created_at')
|
|
|
|
->get();
|
2024-06-14 09:09:44 +02:00
|
|
|
|
2024-02-17 08:41:40 +01:00
|
|
|
$available_unapplied_balance = $unapplied_payments->sum('amount') - $unapplied_payments->sum('applied');
|
2024-08-22 08:45:06 +02:00
|
|
|
|
2024-07-27 00:48:51 +02:00
|
|
|
nlog($this->client->id);
|
|
|
|
nlog($this->invoice->id);
|
|
|
|
nlog($unapplied_payments->sum('amount'));
|
|
|
|
nlog($unapplied_payments->sum('applied'));
|
2024-06-14 09:09:44 +02:00
|
|
|
|
2024-02-17 08:41:40 +01:00
|
|
|
nlog("available unapplied balance = {$available_unapplied_balance}");
|
2024-06-14 09:09:44 +02:00
|
|
|
|
2024-02-17 08:41:40 +01:00
|
|
|
if ((int) $available_unapplied_balance == 0) {
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->invoice->partial > 0) {
|
|
|
|
$this->is_partial_amount = true;
|
|
|
|
}
|
|
|
|
|
2024-02-17 09:03:20 +01:00
|
|
|
$payment_repo = new PaymentRepository(new CreditRepository());
|
2024-06-14 09:09:44 +02:00
|
|
|
|
2024-02-17 08:41:40 +01:00
|
|
|
foreach ($unapplied_payments as $key => $payment) {
|
|
|
|
$payment_balance = $payment->amount - $payment->applied;
|
2024-02-17 09:03:20 +01:00
|
|
|
|
2024-02-17 08:41:40 +01:00
|
|
|
if ($this->is_partial_amount) {
|
|
|
|
//more than needed
|
|
|
|
if ($payment_balance > $this->invoice->partial) {
|
2024-02-17 09:04:41 +01:00
|
|
|
$payload = ['client_id' => $this->invoice->client_id, 'invoices' => [['invoice_id' => $this->invoice->id,'amount' => $this->invoice->partial]]];
|
2024-02-17 09:03:20 +01:00
|
|
|
$payment_repo->save($payload, $payment);
|
2024-06-14 09:09:44 +02:00
|
|
|
|
2024-02-18 06:19:09 +01:00
|
|
|
$this->invoice = $this->invoice->fresh();
|
|
|
|
|
|
|
|
return $this;
|
2024-02-17 08:41:40 +01:00
|
|
|
} else {
|
2024-02-17 09:04:41 +01:00
|
|
|
$payload = ['client_id' => $this->invoice->client_id, 'invoices' => [['invoice_id' => $this->invoice->id,'amount' => $payment_balance]]];
|
2024-02-17 09:03:20 +01:00
|
|
|
$payment_repo->save($payload, $payment);
|
2024-02-17 08:41:40 +01:00
|
|
|
}
|
|
|
|
} else {
|
2024-02-17 09:08:20 +01:00
|
|
|
//more than needed
|
2024-02-17 08:41:40 +01:00
|
|
|
if ($payment_balance > $this->invoice->balance) {
|
2024-06-14 09:09:44 +02:00
|
|
|
|
2024-02-17 09:04:41 +01:00
|
|
|
$payload = ['client_id' => $this->invoice->client_id, 'invoices' => [['invoice_id' => $this->invoice->id,'amount' => $this->invoice->balance]]];
|
2024-02-17 09:03:20 +01:00
|
|
|
$payment_repo->save($payload, $payment);
|
2024-02-17 08:41:40 +01:00
|
|
|
|
2024-02-18 06:19:09 +01:00
|
|
|
$this->invoice = $this->invoice->fresh();
|
|
|
|
|
|
|
|
return $this;
|
2024-06-14 09:09:44 +02:00
|
|
|
|
2024-02-17 08:41:40 +01:00
|
|
|
} else {
|
2024-06-14 09:09:44 +02:00
|
|
|
|
2024-02-17 09:04:41 +01:00
|
|
|
$payload = ['client_id' => $this->invoice->client_id, 'invoices' => [['invoice_id' => $this->invoice->id,'amount' => $payment_balance]]];
|
2024-02-17 09:03:20 +01:00
|
|
|
$payment_repo->save($payload, $payment);
|
|
|
|
|
2024-10-16 07:57:37 +02:00
|
|
|
$this->invoice = $this->invoice->fresh();
|
|
|
|
|
2024-02-17 08:41:40 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-17 09:08:20 +01:00
|
|
|
if((int)$this->invoice->balance == 0) {
|
2024-02-18 06:19:09 +01:00
|
|
|
event(new InvoiceWasPaid($this->invoice, $payment, $payment->company, Ninja::eventVars()));
|
2024-02-17 09:08:20 +01:00
|
|
|
return $this;
|
|
|
|
}
|
2024-02-17 09:03:20 +01:00
|
|
|
}
|
2024-02-17 08:41:40 +01:00
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
2020-10-08 02:28:23 +02:00
|
|
|
|
2020-10-07 13:03:53 +02:00
|
|
|
/**
|
|
|
|
* Applies credits to a payment prior to push
|
|
|
|
* to the payment gateway
|
2020-10-28 11:10:49 +01:00
|
|
|
*
|
2020-10-07 13:03:53 +02:00
|
|
|
* @return $this
|
|
|
|
*/
|
2024-07-27 00:48:51 +02:00
|
|
|
public function applyCreditPayment(): self
|
2020-10-07 13:03:53 +02:00
|
|
|
{
|
2023-08-06 09:35:19 +02:00
|
|
|
$available_credits = Credit::query()->where('client_id', $this->client->id)
|
2020-10-07 13:03:53 +02:00
|
|
|
->where('is_deleted', false)
|
|
|
|
->where('balance', '>', 0)
|
2022-09-05 10:15:08 +02:00
|
|
|
->orderBy('created_at')
|
|
|
|
->get();
|
2020-10-07 13:03:53 +02:00
|
|
|
|
|
|
|
$available_credit_balance = $available_credits->sum('balance');
|
|
|
|
|
2024-02-17 08:41:40 +01:00
|
|
|
nlog("available credit balance = {$available_credit_balance}");
|
2020-10-23 06:18:16 +02:00
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
if ((int) $available_credit_balance == 0) {
|
2023-04-26 22:59:50 +02:00
|
|
|
return $this;
|
2020-11-25 15:19:52 +01:00
|
|
|
}
|
2020-10-07 13:03:53 +02:00
|
|
|
|
|
|
|
if ($this->invoice->partial > 0) {
|
2022-09-06 11:18:05 +02:00
|
|
|
$this->is_partial_amount = true;
|
2020-10-07 13:03:53 +02:00
|
|
|
}
|
|
|
|
|
2020-10-08 05:31:02 +02:00
|
|
|
$this->used_credit = [];
|
2020-10-07 13:03:53 +02:00
|
|
|
|
2020-11-25 15:19:52 +01:00
|
|
|
foreach ($available_credits as $key => $credit) {
|
2022-09-06 11:18:05 +02:00
|
|
|
if ($this->is_partial_amount) {
|
2020-10-08 01:36:06 +02:00
|
|
|
//more credit than needed
|
2021-01-24 10:28:18 +01:00
|
|
|
if ($credit->balance > $this->invoice->partial) {
|
2020-10-08 02:28:23 +02:00
|
|
|
$this->used_credit[$key]['credit_id'] = $credit->id;
|
|
|
|
$this->used_credit[$key]['amount'] = $this->invoice->partial;
|
|
|
|
$this->invoice->balance -= $this->invoice->partial;
|
2021-01-24 07:44:14 +01:00
|
|
|
$this->invoice->paid_to_date += $this->invoice->partial;
|
2020-10-08 01:36:06 +02:00
|
|
|
$this->invoice->partial = 0;
|
|
|
|
break;
|
2020-11-25 15:19:52 +01:00
|
|
|
} else {
|
2020-10-08 02:28:23 +02:00
|
|
|
$this->used_credit[$key]['credit_id'] = $credit->id;
|
|
|
|
$this->used_credit[$key]['amount'] = $credit->balance;
|
2020-10-08 01:36:06 +02:00
|
|
|
$this->invoice->partial -= $credit->balance;
|
2020-10-08 02:28:23 +02:00
|
|
|
$this->invoice->balance -= $credit->balance;
|
2021-01-24 07:44:14 +01:00
|
|
|
$this->invoice->paid_to_date += $credit->balance;
|
2020-10-08 01:36:06 +02:00
|
|
|
}
|
2020-11-25 15:19:52 +01:00
|
|
|
} else {
|
2020-10-08 01:36:06 +02:00
|
|
|
//more credit than needed
|
2021-01-24 10:28:18 +01:00
|
|
|
if ($credit->balance > $this->invoice->balance) {
|
2020-10-08 05:31:02 +02:00
|
|
|
$this->used_credit[$key]['credit_id'] = $credit->id;
|
|
|
|
$this->used_credit[$key]['amount'] = $this->invoice->balance;
|
2021-01-24 07:44:14 +01:00
|
|
|
$this->invoice->paid_to_date += $this->invoice->balance;
|
2020-10-08 01:36:06 +02:00
|
|
|
$this->invoice->balance = 0;
|
2022-09-05 13:15:23 +02:00
|
|
|
|
2020-10-08 01:36:06 +02:00
|
|
|
break;
|
2020-11-25 15:19:52 +01:00
|
|
|
} else {
|
2020-10-08 05:31:02 +02:00
|
|
|
$this->used_credit[$key]['credit_id'] = $credit->id;
|
|
|
|
$this->used_credit[$key]['amount'] = $credit->balance;
|
2020-10-08 01:36:06 +02:00
|
|
|
$this->invoice->balance -= $credit->balance;
|
2021-01-24 07:44:14 +01:00
|
|
|
$this->invoice->paid_to_date += $credit->balance;
|
2020-10-08 01:36:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-10-07 13:03:53 +02:00
|
|
|
|
2020-10-23 06:18:16 +02:00
|
|
|
$this->finalizePaymentUsingCredits();
|
2020-10-07 13:03:53 +02:00
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-07-15 07:05:02 +02:00
|
|
|
/**
|
|
|
|
* Harvests a client gateway token which passes the
|
2020-09-06 11:38:10 +02:00
|
|
|
* necessary filters for an $amount.
|
|
|
|
*
|
2020-07-15 07:05:02 +02:00
|
|
|
* @param float $amount The amount to charge
|
2023-04-26 22:59:50 +02:00
|
|
|
* @return ClientGatewayToken | bool The client gateway token
|
2020-07-15 07:05:02 +02:00
|
|
|
*/
|
2021-01-17 11:33:05 +01:00
|
|
|
public function getGateway($amount)
|
2020-07-07 14:33:11 +02:00
|
|
|
{
|
2021-01-17 11:33:05 +01:00
|
|
|
//get all client gateway tokens and set the is_default one to the first record
|
2024-02-10 03:40:12 +01:00
|
|
|
$gateway_tokens = \App\Models\ClientGatewayToken::query()
|
|
|
|
->where('client_id', $this->client->id)
|
|
|
|
->where('is_deleted', 0)
|
|
|
|
->whereHas('gateway', function ($query) {
|
|
|
|
$query->where('is_deleted', 0)
|
|
|
|
->where('deleted_at', null);
|
|
|
|
})->orderBy('is_default', 'DESC')
|
2024-02-13 05:25:18 +01:00
|
|
|
->get();
|
2024-02-10 03:40:12 +01:00
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
$filtered_gateways = $gateway_tokens->filter(function ($gateway_token) use ($amount) {
|
2021-01-17 11:33:05 +01:00
|
|
|
$company_gateway = $gateway_token->gateway;
|
|
|
|
|
|
|
|
//check if fees and limits are set
|
2024-07-14 13:28:54 +02:00
|
|
|
if (isset($company_gateway->fees_and_limits) && ! is_array($company_gateway->fees_and_limits) && property_exists($company_gateway->fees_and_limits, $gateway_token->gateway_type_id)) { //@phpstan-ignore-line
|
2021-01-17 11:33:05 +01:00
|
|
|
//if valid we keep this gateway_token
|
2022-06-21 11:57:17 +02:00
|
|
|
if ($this->invoice->client->validGatewayForAmount($company_gateway->fees_and_limits->{$gateway_token->gateway_type_id}, $amount)) {
|
2021-01-17 11:33:05 +01:00
|
|
|
return true;
|
2022-06-21 11:57:17 +02:00
|
|
|
} else {
|
2021-01-17 11:33:05 +01:00
|
|
|
return false;
|
2022-06-21 11:57:17 +02:00
|
|
|
}
|
2020-07-15 07:05:02 +02:00
|
|
|
}
|
2021-01-17 11:33:05 +01:00
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
return true; //if no fees_and_limits set then we automatically must add this gateway
|
2021-01-17 11:33:05 +01:00
|
|
|
});
|
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
if ($filtered_gateways->count() >= 1) {
|
2021-01-17 11:33:05 +01:00
|
|
|
return $filtered_gateways->first();
|
2022-06-21 11:57:17 +02:00
|
|
|
}
|
2021-01-17 11:33:05 +01:00
|
|
|
|
|
|
|
return false;
|
2020-07-08 02:18:13 +02:00
|
|
|
}
|
2020-07-07 16:50:51 +02:00
|
|
|
|
2020-07-07 14:33:11 +02:00
|
|
|
}
|