2020-02-03 11:33:07 +01:00
|
|
|
<?php
|
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
2020-02-03 11:33:07 +01: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-02-03 11:33:07 +01:00
|
|
|
*
|
2021-06-16 08:58:16 +02:00
|
|
|
* @license https://www.elastic.co/licensing/elastic-license
|
2020-02-03 11:33:07 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
namespace App\Services\Invoice;
|
|
|
|
|
|
|
|
use App\Models\Invoice;
|
|
|
|
use App\Models\Payment;
|
2020-02-17 10:37:44 +01:00
|
|
|
use App\Services\AbstractService;
|
2020-02-03 11:33:07 +01:00
|
|
|
|
2020-02-17 10:37:44 +01:00
|
|
|
class ApplyPayment extends AbstractService
|
2020-02-03 11:33:07 +01:00
|
|
|
{
|
2023-06-03 14:49:48 +02:00
|
|
|
public function __construct(private Invoice $invoice, private Payment $payment, private float $payment_amount)
|
2020-02-03 11:33:07 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2021-01-20 11:59:24 +01:00
|
|
|
/* Apply payment to a single invoice */
|
2020-03-21 06:37:30 +01:00
|
|
|
public function run()
|
|
|
|
{
|
2021-01-20 11:59:24 +01:00
|
|
|
$this->invoice->fresh('client');
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2021-01-20 11:59:24 +01:00
|
|
|
$amount_paid = 0;
|
|
|
|
|
|
|
|
if ($this->invoice->hasPartial()) {
|
2022-06-21 11:57:17 +02:00
|
|
|
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;
|
|
|
|
|
2024-01-14 05:05:00 +01:00
|
|
|
$this->invoice->service()->clearPartial()->setDueDate()->setStatus(Invoice::STATUS_PARTIAL)->updateBalance($amount_paid)->updatePaidToDate($amount_paid * -1)->save();
|
2022-06-21 11:57:17 +02:00
|
|
|
} 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
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2021-01-20 11:59:24 +01:00
|
|
|
$amount_paid = $this->payment_amount * -1;
|
|
|
|
|
2024-01-14 05:05:00 +01:00
|
|
|
$this->invoice->service()->updatePartial($amount_paid)->updateBalance($amount_paid)->updatePaidToDate($amount_paid * -1)->save();
|
2022-06-21 11:57:17 +02:00
|
|
|
} 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;
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2024-01-14 05:05:00 +01:00
|
|
|
$this->invoice->service()->clearPartial()->setDueDate()->setStatus(Invoice::STATUS_PARTIAL)->updateBalance($amount_paid)->updatePaidToDate($amount_paid * -1)->save();
|
2021-01-20 11:59:24 +01:00
|
|
|
}
|
2023-09-24 06:54:24 +02:00
|
|
|
|
|
|
|
$this->invoice->service()->checkReminderStatus()->save();
|
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
} else {
|
|
|
|
if ($this->payment_amount == $this->invoice->balance) {
|
2021-01-20 11:59:24 +01:00
|
|
|
$amount_paid = $this->payment_amount * -1;
|
|
|
|
|
2024-01-14 05:05:00 +01:00
|
|
|
$this->invoice->service()->clearPartial()->setStatus(Invoice::STATUS_PAID)->updateBalance($amount_paid)->updatePaidToDate($amount_paid * -1)->save();
|
2022-06-21 11:57:17 +02:00
|
|
|
} 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
|
|
|
|
2024-01-14 05:05:00 +01:00
|
|
|
$this->invoice->service()->clearPartial()->setStatus(Invoice::STATUS_PARTIAL)->updateBalance($amount_paid)->updatePaidToDate($amount_paid * -1)->save();
|
2022-06-21 11:57:17 +02:00
|
|
|
} 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;
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2024-01-14 05:05:00 +01:00
|
|
|
$this->invoice->service()->clearPartial()->setStatus(Invoice::STATUS_PAID)->updateBalance($amount_paid)->updatePaidToDate($amount_paid * -1)->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()
|
2024-04-28 05:52:44 +02:00
|
|
|
->updatePaymentBalance($amount_paid, "ApplyPaymentInvoice");
|
2021-01-20 11:59:24 +01:00
|
|
|
|
2021-01-24 07:44:14 +01:00
|
|
|
$this->invoice
|
|
|
|
->client
|
|
|
|
->service()
|
|
|
|
->updateBalance($amount_paid)
|
|
|
|
->save();
|
2020-02-03 11:33:07 +01:00
|
|
|
|
2023-09-05 03:54:05 +02:00
|
|
|
$this->invoice
|
|
|
|
->service()
|
|
|
|
->applyNumber()
|
|
|
|
->workFlow()
|
|
|
|
->save();
|
2020-05-28 06:18:34 +02:00
|
|
|
|
2020-02-03 11:33:07 +01:00
|
|
|
return $this->invoice;
|
2020-03-21 06:37:30 +01:00
|
|
|
}
|
2020-02-14 04:32:22 +01:00
|
|
|
}
|