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

153 lines
4.5 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
*
* @copyright Copyright (c) 2020. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://opensource.org/licenses/AAL
*/
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;
/* Check invoice partial for amount to be cleared first */
2020-10-13 14:28:30 +02:00
if($this->invoice->partial > 0){
$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;
// $this->credit->balance -= $partial_payment;
$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 */
if($this->amount > 0 && $applicable_amount > 0 && $invoice_balance > 0){
2020-10-13 14:28:30 +02:00
2020-10-14 01:53:20 +02:00
$balance_payment = min($invoice_balance, $this->amount);
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->credit->balance -= $balance_payment;
$this->amount_applied += $balance_payment;
}
2020-10-14 02:11:24 +02:00
$this->applyPaymentToCredit();
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
->invoices()
->attach($this->invoice->id, ['amount' => $this->amount_applied]);
$this->payment
->credits()
->attach($this->credit->id, ['amount' => $this->amount_applied]);
$this->payment
->ledger()
->updatePaymentBalance($this->amount_applied * -1);
$this->payment
->client
->service()
->updateBalance($this->amount_applied * -1)
->adjustCreditBalance($this->amount_applied * -1)
->updatePaidToDate($this->amount_applied)
->save();
$this->invoice
->service()
->updateBalance($this->amount_applied * -1)
->updateStatus()
->save();
$this->credit
->ledger()
->updateCreditBalance(($this->amount_applied * -1), "Credit payment applied to Invoice {$this->invoice->number}");
2020-10-13 14:28:30 +02:00
2020-10-14 01:53:20 +02:00
event(new InvoiceWasUpdated($this->invoice, $this->invoice->company, Ninja::eventVars()));
2020-10-14 02:11:24 +02:00
if((int)$this->invoice->balance == 0){
$this->invoice->service()->deletePdf();
2020-10-14 01:53:20 +02:00
event(new InvoiceWasPaid($this->invoice, $this->payment->company, Ninja::eventVars()));
2020-10-14 02:11:24 +02:00
}
2020-10-14 01:53:20 +02:00
}
2020-10-13 14:28:30 +02:00
}