2019-10-03 14:02:31 +02:00
|
|
|
<?php
|
2020-07-01 03:06:40 +02:00
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
2020-07-01 03:06:40 +02:00
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
2023-01-28 23:21:40 +01:00
|
|
|
* @copyright Copyright (c) 2023. Invoice Ninja LLC (https://invoiceninja.com)
|
2020-07-01 03:06:40 +02:00
|
|
|
*
|
2021-06-16 08:58:16 +02:00
|
|
|
* @license https://www.elastic.co/licensing/elastic-license
|
2020-07-01 03:06:40 +02:00
|
|
|
*/
|
|
|
|
|
2020-02-20 22:05:01 +01:00
|
|
|
namespace App\Services\Payment;
|
|
|
|
|
2020-09-03 13:37:02 +02:00
|
|
|
use App\Events\Invoice\InvoiceWasUpdated;
|
2021-04-02 13:44:44 +02:00
|
|
|
use App\Jobs\Invoice\InvoiceWorkflowSettings;
|
2022-03-10 01:32:04 +01:00
|
|
|
use App\Jobs\Ninja\TransactionLog;
|
2022-07-11 10:24:49 +02:00
|
|
|
use App\Models\Client;
|
2020-02-20 22:05:01 +01:00
|
|
|
use App\Models\Invoice;
|
2020-09-19 03:20:14 +02:00
|
|
|
use App\Models\Payment;
|
|
|
|
use App\Models\PaymentHash;
|
2022-03-10 01:32:04 +01:00
|
|
|
use App\Models\TransactionEvent;
|
2020-09-03 13:37:02 +02:00
|
|
|
use App\Utils\Ninja;
|
2020-08-31 04:00:43 +02:00
|
|
|
use App\Utils\Traits\MakesHash;
|
2019-10-03 14:02:31 +02:00
|
|
|
|
2020-02-20 22:05:01 +01:00
|
|
|
class UpdateInvoicePayment
|
|
|
|
{
|
2020-08-31 04:00:43 +02:00
|
|
|
use MakesHash;
|
|
|
|
|
2019-10-03 14:02:31 +02:00
|
|
|
public $payment;
|
2019-12-27 01:28:36 +01:00
|
|
|
|
2020-08-31 04:00:43 +02:00
|
|
|
public $payment_hash;
|
|
|
|
|
2020-09-19 03:20:14 +02:00
|
|
|
public function __construct(Payment $payment, PaymentHash $payment_hash)
|
2019-10-03 14:02:31 +02:00
|
|
|
{
|
|
|
|
$this->payment = $payment;
|
2020-08-31 04:00:43 +02:00
|
|
|
$this->payment_hash = $payment_hash;
|
2019-10-03 14:02:31 +02:00
|
|
|
}
|
|
|
|
|
2020-02-20 22:05:01 +01:00
|
|
|
public function run()
|
2019-10-03 14:02:31 +02:00
|
|
|
{
|
2020-08-31 04:00:43 +02:00
|
|
|
$paid_invoices = $this->payment_hash->invoices();
|
|
|
|
|
2021-06-20 00:14:56 +02:00
|
|
|
$invoices = Invoice::whereIn('id', $this->transformKeys(array_column($paid_invoices, 'invoice_id')))->withTrashed()->get();
|
2022-03-28 07:36:00 +02:00
|
|
|
|
|
|
|
$client = $this->payment->client;
|
2020-08-31 04:00:43 +02:00
|
|
|
|
2022-03-28 07:36:00 +02:00
|
|
|
if($client->trashed())
|
|
|
|
$client->restore();
|
2021-01-06 06:14:20 +01:00
|
|
|
|
2022-03-28 07:36:00 +02:00
|
|
|
collect($paid_invoices)->each(function ($paid_invoice) use ($invoices, $client) {
|
2022-03-26 03:58:49 +01:00
|
|
|
|
2020-09-06 11:38:10 +02:00
|
|
|
$invoice = $invoices->first(function ($inv) use ($paid_invoice) {
|
2020-08-31 06:27:47 +02:00
|
|
|
return $paid_invoice->invoice_id == $inv->hashed_id;
|
2020-08-31 04:00:43 +02:00
|
|
|
});
|
|
|
|
|
2022-03-26 03:58:49 +01:00
|
|
|
if($invoice->trashed())
|
|
|
|
$invoice->restore();
|
|
|
|
|
2020-09-06 11:38:10 +02:00
|
|
|
if ($invoice->id == $this->payment_hash->fee_invoice_id) {
|
2020-08-31 06:27:47 +02:00
|
|
|
$paid_amount = $paid_invoice->amount + $this->payment_hash->fee_total;
|
2020-09-06 11:38:10 +02:00
|
|
|
} else {
|
2020-08-31 06:27:47 +02:00
|
|
|
$paid_amount = $paid_invoice->amount;
|
2020-09-06 11:38:10 +02:00
|
|
|
}
|
2020-08-31 06:27:47 +02:00
|
|
|
|
2022-11-22 00:46:42 +01:00
|
|
|
$client->service()->updatePaidToDate($paid_amount); //always use the payment->amount
|
2022-03-18 02:27:27 +01:00
|
|
|
|
2021-01-06 06:14:20 +01:00
|
|
|
/* Need to determine here is we have an OVER payment - if YES only apply the max invoice amount */
|
2021-12-17 12:11:36 +01:00
|
|
|
if($paid_amount > $invoice->partial && $paid_amount > $invoice->balance)
|
|
|
|
$paid_amount = $invoice->balance;
|
2021-01-06 06:14:20 +01:00
|
|
|
|
2022-11-22 00:46:42 +01:00
|
|
|
$client->service()->updateBalance($paid_amount*-1); //only ever use the amount applied to the invoice
|
|
|
|
|
2022-01-26 13:01:54 +01:00
|
|
|
/*Improve performance here - 26-01-2022 - also change the order of events for invoice first*/
|
2022-03-28 07:36:00 +02:00
|
|
|
//caution what if we amount paid was less than partial - we wipe it!
|
2022-04-04 21:23:39 +02:00
|
|
|
$invoice->balance -= $paid_amount;
|
|
|
|
$invoice->paid_to_date += $paid_amount;
|
|
|
|
$invoice->save();
|
|
|
|
|
2022-03-28 07:36:00 +02:00
|
|
|
$invoice = $invoice->service()
|
|
|
|
->clearPartial()
|
|
|
|
->updateStatus()
|
|
|
|
->touchPdf()
|
2022-12-20 11:46:20 +01:00
|
|
|
->workFlow()
|
2022-03-28 07:36:00 +02:00
|
|
|
->save();
|
2022-04-02 07:13:31 +02:00
|
|
|
|
2022-12-20 11:46:20 +01:00
|
|
|
if($invoice->is_proforma)
|
|
|
|
{
|
2023-02-07 12:25:40 +01:00
|
|
|
|
|
|
|
if(strlen($invoice->number) > 1 && str_starts_with($invoice->number,"####"))
|
|
|
|
$invoice->number = '';
|
|
|
|
|
|
|
|
|
2022-12-20 11:46:20 +01:00
|
|
|
$invoice->is_proforma = false;
|
|
|
|
|
|
|
|
$invoice->service()
|
|
|
|
->applyNumber()
|
|
|
|
->save();
|
|
|
|
}
|
|
|
|
|
2022-01-26 13:01:54 +01:00
|
|
|
|
2021-01-06 06:14:20 +01:00
|
|
|
/* Updates the company ledger */
|
2020-08-31 04:00:43 +02:00
|
|
|
$this->payment
|
|
|
|
->ledger()
|
2020-09-06 11:38:10 +02:00
|
|
|
->updatePaymentBalance($paid_amount * -1);
|
2020-08-31 04:00:43 +02:00
|
|
|
|
2020-09-06 11:38:10 +02:00
|
|
|
$pivot_invoice = $this->payment->invoices->first(function ($inv) use ($paid_invoice) {
|
|
|
|
return $inv->hashed_id == $paid_invoice->invoice_id;
|
2020-09-01 01:28:37 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
/*update paymentable record*/
|
|
|
|
$pivot_invoice->pivot->amount = $paid_amount;
|
2020-09-19 03:20:14 +02:00
|
|
|
$pivot_invoice->pivot->save();
|
2019-11-19 11:23:56 +01:00
|
|
|
|
2020-09-19 12:33:59 +02:00
|
|
|
$this->payment->applied += $paid_amount;
|
|
|
|
|
2020-08-31 04:00:43 +02:00
|
|
|
});
|
2020-09-19 12:33:59 +02:00
|
|
|
|
2021-12-17 08:57:26 +01:00
|
|
|
/* Remove the event updater from within the loop to prevent race conditions */
|
|
|
|
|
2021-12-17 12:11:36 +01:00
|
|
|
$this->payment->saveQuietly();
|
|
|
|
|
2021-12-17 08:57:26 +01:00
|
|
|
$invoices->each(function ($invoice) {
|
2022-03-04 23:21:17 +01:00
|
|
|
|
2021-12-17 08:57:26 +01:00
|
|
|
event(new InvoiceWasUpdated($invoice, $invoice->company, Ninja::eventVars(auth()->user() ? auth()->user()->id : null)));
|
2021-12-17 12:11:36 +01:00
|
|
|
|
2021-12-17 08:57:26 +01:00
|
|
|
});
|
|
|
|
|
2022-03-04 23:21:17 +01:00
|
|
|
return $this->payment->fresh();
|
2019-10-03 14:02:31 +02:00
|
|
|
}
|
2020-03-21 06:37:30 +01:00
|
|
|
}
|