1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-14 15:13:29 +01:00
invoiceninja/app/Services/Invoice/ApplyPayment.php

121 lines
4.1 KiB
PHP
Raw Normal View History

<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
2022-04-27 05:20:41 +02:00
* @copyright Copyright (c) 2022. Invoice Ninja LLC (https://invoiceninja.com)
*
2021-06-16 08:58:16 +02:00
* @license https://www.elastic.co/licensing/elastic-license
*/
namespace App\Services\Invoice;
2022-03-10 01:32:04 +01:00
use App\Jobs\Ninja\TransactionLog;
use App\Models\Invoice;
use App\Models\Payment;
2022-03-10 01:32:04 +01:00
use App\Models\TransactionEvent;
use App\Services\AbstractService;
class ApplyPayment extends AbstractService
{
private $invoice;
private $payment;
private $payment_amount;
public function __construct($invoice, $payment, $payment_amount)
{
$this->invoice = $invoice;
$this->payment = $payment;
$this->payment_amount = $payment_amount;
}
2021-01-20 11:59:24 +01:00
/* Apply payment to a single invoice */
public function run()
{
2021-01-20 11:59:24 +01:00
$this->invoice->fresh('client');
2021-01-20 11:59:24 +01:00
$amount_paid = 0;
if ($this->invoice->hasPartial()) {
if ($this->invoice->partial == $this->payment_amount) {
2021-01-20 11:59:24 +01:00
//is partial and amount is exactly the partial amount
$amount_paid = $this->payment_amount * -1;
2021-11-15 23:50:07 +01:00
$this->invoice->service()->clearPartial()->setDueDate()->setStatus(Invoice::STATUS_PARTIAL)->updateBalance($amount_paid)->save();
} elseif ($this->invoice->partial > 0 && $this->invoice->partial > $this->payment_amount) {
2021-01-20 11:59:24 +01:00
//partial amount exists, but the amount is less than the partial amount
2021-01-20 11:59:24 +01:00
$amount_paid = $this->payment_amount * -1;
2021-11-15 23:50:07 +01:00
$this->invoice->service()->updatePartial($amount_paid)->updateBalance($amount_paid)->save();
} elseif ($this->invoice->partial > 0 && $this->invoice->partial < $this->payment_amount) {
2021-01-20 11:59:24 +01:00
//partial exists and the amount paid is GREATER than the partial amount
$amount_paid = $this->payment_amount * -1;
2021-11-15 23:50:07 +01:00
$this->invoice->service()->clearPartial()->setDueDate()->setStatus(Invoice::STATUS_PARTIAL)->updateBalance($amount_paid)->save();
2021-01-20 11:59:24 +01:00
}
} else {
if ($this->payment_amount == $this->invoice->balance) {
2021-01-20 11:59:24 +01:00
$amount_paid = $this->payment_amount * -1;
2021-11-15 23:50:07 +01:00
$this->invoice->service()->clearPartial()->setStatus(Invoice::STATUS_PAID)->updateBalance($amount_paid)->save();
} elseif ($this->payment_amount < $this->invoice->balance) {
2021-01-20 11:59:24 +01:00
//partial invoice payment made
$amount_paid = $this->payment_amount * -1;
2020-06-06 03:07:31 +02:00
2021-11-15 23:50:07 +01:00
$this->invoice->service()->clearPartial()->setStatus(Invoice::STATUS_PARTIAL)->updateBalance($amount_paid)->save();
} elseif ($this->payment_amount > $this->invoice->balance) {
2021-01-20 11:59:24 +01:00
//partial invoice payment made
$amount_paid = $this->invoice->balance * -1;
2021-11-15 23:50:07 +01:00
$this->invoice->service()->clearPartial()->setStatus(Invoice::STATUS_PAID)->updateBalance($amount_paid)->save();
2021-01-20 11:59:24 +01:00
}
}
2020-06-06 03:07:31 +02:00
2021-01-20 11:59:24 +01:00
$this->payment
->ledger()
->updatePaymentBalance($amount_paid);
2022-02-03 07:38:23 +01:00
// nlog("updating client balance by amount {$amount_paid}");
$this->invoice
->client
->service()
->updateBalance($amount_paid)
->save();
/* Update Pivot Record amount */
$this->payment->invoices->each(function ($inv) use ($amount_paid) {
if ($inv->id == $this->invoice->id) {
$inv->pivot->amount = ($amount_paid * -1);
$inv->pivot->save();
$inv->paid_to_date += floatval($amount_paid * -1);
$inv->save();
}
});
2021-08-08 00:40:04 +02:00
$this->invoice->service()->applyNumber()->workFlow()->save();
2022-03-10 01:32:04 +01:00
$transaction = [
'invoice' => $this->invoice->transaction_event(),
'payment' => $this->payment->transaction_event(),
'client' => $this->invoice->client->transaction_event(),
'credit' => [],
'metadata' => [],
];
TransactionLog::dispatch(TransactionEvent::INVOICE_PAYMENT_APPLIED, $transaction, $this->invoice->company->db);
return $this->invoice;
}
}