1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-16 16:13:20 +01:00
invoiceninja/app/Services/Payment/UpdateInvoicePayment.php

98 lines
3.1 KiB
PHP
Raw Normal View History

<?php
2020-07-01 03:06:40 +02:00
/**
* Invoice Ninja (https://invoiceninja.com).
2020-07-01 03:06:40 +02:00
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2021. 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
*/
namespace App\Services\Payment;
use App\Events\Invoice\InvoiceWasUpdated;
2021-04-02 13:44:44 +02:00
use App\Jobs\Invoice\InvoiceWorkflowSettings;
use App\Models\Invoice;
2020-09-19 03:20:14 +02:00
use App\Models\Payment;
use App\Models\PaymentHash;
use App\Utils\Ninja;
use App\Utils\Traits\MakesHash;
class UpdateInvoicePayment
{
use MakesHash;
public $payment;
public $payment_hash;
2020-09-19 03:20:14 +02:00
public function __construct(Payment $payment, PaymentHash $payment_hash)
{
$this->payment = $payment;
$this->payment_hash = $payment_hash;
}
public function run()
{
$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();
collect($paid_invoices)->each(function ($paid_invoice) use ($invoices) {
2021-01-06 06:14:20 +01: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;
});
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;
} else {
2020-08-31 06:27:47 +02:00
$paid_amount = $paid_invoice->amount;
}
2020-08-31 06:27:47 +02: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 */
if($paid_amount > $invoice->partial && $paid_amount > $invoice->balance)
$paid_amount = $invoice->balance;
/* Updates the company ledger */
$this->payment
->ledger()
->updatePaymentBalance($paid_amount * -1);
$this->payment
->client
->service()
->updateBalance($paid_amount * -1)
2020-08-31 06:27:47 +02:00
->updatePaidToDate($paid_amount)
->save();
$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();
$this->payment->applied += $paid_amount;
$invoice->service() //caution what if we amount paid was less than partial - we wipe it!
2020-09-01 01:28:37 +02:00
->clearPartial()
->updateBalance($paid_amount * -1)
->updatePaidToDate($paid_amount)
2020-09-11 02:10:53 +02:00
->updateStatus()
2021-04-06 00:19:27 +02:00
->deletePdf()
2020-09-01 01:28:37 +02:00
->save();
2021-04-02 13:44:44 +02:00
InvoiceWorkflowSettings::dispatchNow($invoice);
2021-04-20 23:26:04 +02:00
event(new InvoiceWasUpdated($invoice, $invoice->company, Ninja::eventVars(auth()->user() ? auth()->user()->id : null)));
});
$this->payment->save();
return $this->payment;
}
}