1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-09-21 08:51:34 +02:00
invoiceninja/app/Services/Credit/ApplyPayment.php

156 lines
5.1 KiB
PHP
Raw Normal View History

2020-10-13 14:28:30 +02:00
<?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)
2020-10-13 14:28:30 +02:00
*
2021-06-16 08:58:16 +02:00
* @license https://www.elastic.co/licensing/elastic-license
2020-10-13 14:28:30 +02:00
*/
namespace App\Services\Credit;
use App\DataMapper\InvoiceItem;
2020-10-14 01:53:20 +02:00
use App\Events\Invoice\InvoiceWasPaid;
use App\Events\Invoice\InvoiceWasUpdated;
2020-10-13 14:28:30 +02:00
use App\Models\Credit;
use App\Models\Invoice;
use App\Models\Payment;
2020-10-14 01:53:20 +02:00
use App\Utils\Ninja;
2020-10-13 14:28:30 +02:00
class ApplyPayment
{
private $credit;
private $invoice;
private $amount;
private $amount_applied;
private $payment;
public function __construct(Credit $credit, Invoice $invoice, float $amount, Payment $payment)
{
$this->credit = $credit;
$this->invoice = $invoice;
$this->amount = $amount;
$this->amount_applied = 0;
$this->payment = $payment->fresh();
}
2020-10-14 01:53:20 +02:00
public function run() :Credit
2020-10-13 14:28:30 +02:00
{
//$available_credit_balance = $this->credit->balance;
$applicable_amount = min($this->amount, $this->credit->balance);
2020-10-14 01:53:20 +02:00
$invoice_balance = $this->invoice->balance;
2020-10-15 06:07:42 +02:00
$credit_balance = $this->credit->balance;
2020-10-14 01:53:20 +02:00
/* Check invoice partial for amount to be cleared first */
2020-11-25 15:19:52 +01:00
if ($this->invoice->partial > 0) {
2020-10-13 14:28:30 +02:00
$partial_payment = min($this->invoice->partial, $applicable_amount);
$this->invoice->partial -= $partial_payment;
2020-10-14 01:53:20 +02:00
$invoice_balance -= $partial_payment;
2020-10-13 14:28:30 +02:00
$this->amount -= $partial_payment;
2020-10-15 06:07:42 +02:00
$credit_balance -= $partial_payment;
2020-10-13 14:28:30 +02:00
$applicable_amount -= $partial_payment;
$this->amount_applied += $partial_payment;
}
2020-10-14 01:53:20 +02:00
/* If there is remaining amount use it on the balance */
2020-11-25 15:19:52 +01:00
if ($this->amount > 0 && $applicable_amount > 0 && $invoice_balance > 0) {
2020-10-15 06:07:42 +02:00
$balance_payment = min($invoice_balance, min($this->amount, $credit_balance));
2020-10-13 14:28:30 +02:00
2020-10-14 01:53:20 +02:00
$invoice_balance -= $balance_payment;
2020-10-13 14:28:30 +02:00
$this->amount -= $balance_payment;
$this->amount_applied += $balance_payment;
}
2020-10-20 04:23:48 +02:00
$this->credit->balance -= $this->amount_applied;
$this->credit->paid_to_date += $this->amount_applied;
2020-10-20 04:23:48 +02:00
if ((int) $this->credit->balance == 0) {
2020-10-20 04:23:48 +02:00
$this->credit->status_id = Credit::STATUS_APPLIED;
2020-11-25 15:19:52 +01:00
} else {
2020-10-20 04:23:48 +02:00
$this->credit->status_id = Credit::STATUS_PARTIAL;
2020-11-25 15:19:52 +01:00
}
2020-10-20 04:23:48 +02:00
$this->credit->save();
2020-10-14 01:53:20 +02:00
$this->addPaymentToLedger();
return $this->credit;
2020-10-13 14:28:30 +02:00
}
private function applyPaymentToCredit()
{
$credit_item = new InvoiceItem;
$credit_item->type_id = '1';
$credit_item->product_key = ctrans('texts.credit');
$credit_item->notes = ctrans('texts.credit_payment', ['invoice_number' => $this->invoice->number]);
$credit_item->quantity = 1;
$credit_item->cost = $this->amount_applied * -1;
2020-10-14 02:11:24 +02:00
$credit_items = $this->credit->line_items;
2020-10-13 14:28:30 +02:00
$credit_items[] = $credit_item;
$this->credit->line_items = $credit_items;
$this->credit = $this->credit->calc()->getCredit();
$this->credit->save();
}
2020-10-14 01:53:20 +02:00
private function addPaymentToLedger()
{
$this->payment->amount += $this->amount_applied;
$this->payment->applied += $this->amount_applied;
$this->payment->status_id = Payment::STATUS_COMPLETED;
$this->payment->currency_id = $this->credit->client->getSetting('currency_id');
$this->payment->save();
$this->payment->service()->applyNumber()->save();
2020-10-14 01:53:20 +02:00
$this->payment
->invoices()
->attach($this->invoice->id, ['amount' => $this->amount_applied]);
2020-10-14 01:53:20 +02:00
$this->payment
->credits()
->attach($this->credit->id, ['amount' => $this->amount_applied]);
2020-11-25 15:19:52 +01:00
$this->payment
2020-10-14 01:53:20 +02:00
->ledger()
->updatePaymentBalance($this->amount_applied * -1);
2020-11-25 15:19:52 +01:00
$this->payment
2020-10-14 01:53:20 +02:00
->client
->service()
->updateBalance($this->amount_applied * -1)
->adjustCreditBalance($this->amount_applied * -1)
->updatePaidToDate($this->amount_applied)
->save();
2020-11-25 15:19:52 +01:00
$this->invoice
->service()
2020-10-14 01:53:20 +02:00
->updateBalance($this->amount_applied * -1)
2021-01-24 10:28:18 +01:00
->updatePaidToDate($this->amount_applied)
2020-10-14 01:53:20 +02:00
->updateStatus()
->save();
2020-11-25 15:19:52 +01:00
$this->credit
2020-10-14 01:53:20 +02:00
->ledger()
->updateCreditBalance(($this->amount_applied * -1), "Credit payment applied to Invoice {$this->invoice->number}");
2020-10-13 14:28:30 +02:00
2021-04-20 23:26:04 +02:00
event(new InvoiceWasUpdated($this->invoice, $this->invoice->company, Ninja::eventVars(auth()->user() ? auth()->user()->id : null)));
2020-10-14 01:53:20 +02:00
if ((int) $this->invoice->balance == 0) {
2021-02-16 14:31:00 +01:00
$this->invoice->service()->deletePdf();
2022-03-04 23:21:17 +01:00
$this->invoice = $this->invoice->fresh();
2021-04-20 23:26:04 +02:00
event(new InvoiceWasPaid($this->invoice, $this->payment, $this->payment->company, Ninja::eventVars(auth()->user() ? auth()->user()->id : null)));
2020-11-25 15:19:52 +01:00
}
2020-10-14 01:53:20 +02:00
}
2020-10-13 14:28:30 +02:00
}