1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-14 07:02:34 +01:00
invoiceninja/app/Services/Invoice/ApplyPaymentAmount.php

135 lines
4.3 KiB
PHP
Raw Normal View History

2021-08-13 03:30:48 +02:00
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
2023-01-28 23:21:40 +01:00
* @copyright Copyright (c) 2023. Invoice Ninja LLC (https://invoiceninja.com)
2021-08-13 03:30:48 +02:00
*
* @license https://www.elastic.co/licensing/elastic-license
*/
namespace App\Services\Invoice;
use App\Events\Invoice\InvoiceWasPaid;
use App\Events\Payment\PaymentWasCreated;
use App\Factory\PaymentFactory;
use App\Libraries\Currency\Conversion\CurrencyApi;
use App\Models\Invoice;
use App\Models\Payment;
use App\Services\AbstractService;
use App\Utils\Ninja;
use App\Utils\Traits\GeneratesCounter;
use Illuminate\Support\Carbon;
class ApplyPaymentAmount extends AbstractService
{
use GeneratesCounter;
2023-02-16 02:36:09 +01:00
public function __construct(private Invoice $invoice, private float $amount, private ?string $reference)
{
}
2021-08-13 03:30:48 +02:00
public function run()
{
2021-08-13 03:30:48 +02:00
if ($this->invoice->status_id == Invoice::STATUS_DRAFT) {
2022-09-07 00:35:19 +02:00
$this->invoice = $this->invoice->service()->markSent()->save();
2021-08-13 03:30:48 +02:00
}
/*Don't double pay*/
2021-10-11 08:05:14 +02:00
if ($this->invoice->status_id == Invoice::STATUS_PAID) {
2021-08-13 03:30:48 +02:00
return $this->invoice;
}
if ($this->amount == 0) {
2021-08-13 03:30:48 +02:00
return $this->invoice;
}
2021-08-13 03:30:48 +02:00
/* Create Payment */
$payment = PaymentFactory::create($this->invoice->company_id, $this->invoice->user_id);
$payment->amount = $this->amount;
$payment->applied = min($this->amount, $this->invoice->balance);
2021-10-17 05:21:13 +02:00
$payment->number = $this->getNextPaymentNumber($this->invoice->client, $payment);
2021-08-13 03:30:48 +02:00
$payment->status_id = Payment::STATUS_COMPLETED;
$payment->client_id = $this->invoice->client_id;
$payment->transaction_reference = $this->reference ?: ctrans('texts.manual_entry');
2021-08-13 03:30:48 +02:00
$payment->currency_id = $this->invoice->client->getSetting('currency_id');
$payment->is_manual = true;
/* Create a payment relationship to the invoice entity */
2021-10-15 09:47:41 +02:00
$payment->saveQuietly();
2021-08-13 03:30:48 +02:00
$this->setExchangeRate($payment);
$payment->invoices()->attach($this->invoice->id, [
'amount' => $payment->amount,
]);
$has_partial = $this->invoice->hasPartial();
$invoice_service = $this->invoice->service()
2021-08-13 03:30:48 +02:00
->setExchangeRate()
->updateBalance($payment->amount * -1)
->updatePaidToDate($payment->amount)
->setCalculatedStatus()
->applyNumber();
if ($has_partial) {
$this->invoice->partial = max(0, $this->invoice->partial - $payment->amount);
$invoice_service->checkReminderStatus();
}
2023-10-26 04:57:44 +02:00
if($this->invoice->balance == 0) {
$this->invoice->next_send_date = null;
}
$this->invoice = $invoice_service->save();
2021-08-13 03:30:48 +02:00
2022-03-23 05:51:32 +01:00
$this->invoice
->client
->service()
2022-09-07 00:35:19 +02:00
->updateBalanceAndPaidToDate($payment->amount * -1, $payment->amount)
2022-03-23 05:51:32 +01:00
->save();
2022-09-02 06:22:04 +02:00
if ($this->invoice->client->getSetting('client_manual_payment_notification')) {
2021-08-13 03:30:48 +02:00
$payment->service()->sendEmail();
}
2021-08-13 03:30:48 +02:00
/* Update Invoice balance */
$payment->ledger()
->updatePaymentBalance($payment->amount * -1);
$this->invoice->service()->workFlow()->save();
2021-10-15 09:47:41 +02:00
event('eloquent.created: App\Models\Payment', $payment);
2021-12-17 12:11:36 +01:00
event(new PaymentWasCreated($payment, $payment->company, Ninja::eventVars(auth()->user() ? auth()->user()->id : null)));
event(new InvoiceWasPaid($this->invoice, $payment, $payment->company, Ninja::eventVars(auth()->user() ? auth()->user()->id : null)));
2021-08-13 03:30:48 +02:00
return $this->invoice;
}
private function setExchangeRate(Payment $payment)
{
if ($payment->exchange_rate != 1) {
return;
}
2021-08-13 03:30:48 +02:00
$client_currency = $payment->client->getSetting('currency_id');
$company_currency = $payment->client->company->settings->currency_id;
if ($company_currency != $client_currency) {
$exchange_rate = new CurrencyApi();
$payment->exchange_rate = $exchange_rate->exchangeRate($client_currency, $company_currency, Carbon::parse($payment->date));
2023-02-22 10:13:50 +01:00
2021-08-13 03:30:48 +02:00
$payment->exchange_currency_id = $company_currency;
$payment->saveQuietly();
2021-08-13 03:30:48 +02:00
}
}
}