1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-09-21 17:01:33 +02:00
invoiceninja/app/Services/Invoice/MarkPaid.php

153 lines
4.9 KiB
PHP
Raw Normal View History

<?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-06-16 08:58:16 +02:00
* @license https://www.elastic.co/licensing/elastic-license
*/
namespace App\Services\Invoice;
2020-08-12 03:45:40 +02:00
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;
2020-07-08 14:02:16 +02:00
use App\Utils\Ninja;
2020-02-26 11:06:08 +01:00
use App\Utils\Traits\GeneratesCounter;
use Illuminate\Support\Carbon;
class MarkPaid extends AbstractService
{
2020-02-26 11:06:08 +01:00
use GeneratesCounter;
2022-09-05 09:18:08 +02:00
private $payable_balance;
2023-02-16 02:36:09 +01:00
public function __construct(private Invoice $invoice, private ?string $reference)
{
}
public function run()
{
2020-07-15 08:08:57 +02:00
/*Don't double pay*/
2021-10-11 08:05:14 +02:00
if ($this->invoice->status_id == Invoice::STATUS_PAID) {
2020-07-15 08:08:57 +02:00
return $this->invoice;
}
2020-07-15 08:08:57 +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();
}
2022-09-05 09:18:08 +02:00
\DB::connection(config('database.default'))->transaction(function () {
2022-09-12 05:03:39 +02:00
$this->invoice = Invoice::withTrashed()->where('id', $this->invoice->id)->lockForUpdate()->first();
2023-02-16 02:36:09 +01:00
if ($this->invoice) {
2023-01-15 04:59:42 +01:00
$this->payable_balance = $this->invoice->balance;
$this->invoice
->service()
->setExchangeRate()
->clearPartial()
2023-01-15 04:59:42 +01:00
->updateBalance($this->payable_balance * -1)
->updatePaidToDate($this->payable_balance)
->setStatus(Invoice::STATUS_PAID)
->save();
}
}, 1);
/* Create Payment */
$payment = PaymentFactory::create($this->invoice->company_id, $this->invoice->user_id);
2022-09-05 09:18:08 +02:00
$payment->amount = $this->payable_balance;
$payment->applied = $this->payable_balance;
$payment->status_id = Payment::STATUS_COMPLETED;
$payment->client_id = $this->invoice->client_id;
$payment->transaction_reference = $this->reference ?: ctrans('texts.manual_entry');
$payment->currency_id = $this->invoice->client->getSetting('currency_id');
$payment->is_manual = true;
if ($this->invoice->company->timezone()) {
2023-11-15 05:36:24 +01:00
$payment->date = now()->addSeconds($this->invoice->company->utc_offset())->format('Y-m-d');
}
2022-04-05 12:47:44 +02:00
$payment_type_id = $this->invoice->client->getSetting('payment_type_id');
if ((int) $payment_type_id > 0) {
$payment->type_id = (int) $payment_type_id;
}
2021-10-15 09:47:41 +02:00
$payment->saveQuietly();
2022-08-05 00:08:19 +02:00
$payment->service()->applyNumber()->save();
2024-01-14 05:05:00 +01:00
/* Create a payment relationship to the invoice entity */
$payment->invoices()->attach($this->invoice->id, [
2022-09-05 09:18:08 +02:00
'amount' => $this->payable_balance,
]);
2023-11-20 03:53:39 +01:00
if ($payment->client->getSetting('send_email_on_mark_paid')) {
$payment->service()->sendEmail();
2023-02-16 02:36:09 +01:00
}
$this->setExchangeRate($payment);
2021-10-15 09:47:41 +02:00
event('eloquent.created: App\Models\Payment', $payment);
2020-11-04 10:32:49 +01:00
$this->invoice->next_send_date = null;
2021-10-22 01:55:58 +02:00
$this->invoice
->service()
->applyNumber()
2023-09-05 03:54:05 +02:00
// ->deletePdf()
->save();
$payment->ledger()
2022-09-05 09:18:08 +02:00
->updatePaymentBalance($this->payable_balance * -1);
2022-09-06 12:51:42 +02:00
//06-09-2022
$this->invoice
->client
->service()
2024-01-14 05:05:00 +01:00
->updateBalanceAndPaidToDate($payment->amount * -1, $payment->amount)
2022-09-06 12:51:42 +02:00
->save();
2022-03-29 05:13:11 +02:00
2022-03-15 10:20:05 +01:00
$this->invoice = $this->invoice
->service()
->workFlow()
->save();
2021-02-05 06:02:38 +01:00
2021-12-17 12:11:36 +01:00
/* Update Invoice balance */
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)));
2022-11-08 22:09:42 +01:00
event('eloquent.updated: App\Models\Invoice', $this->invoice);
2024-01-14 05:05:00 +01:00
return $this->invoice;
}
private function setExchangeRate(Payment $payment)
{
if ($payment->exchange_rate != 1) {
return;
}
$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));
//$payment->exchange_currency_id = $client_currency; // 23/06/2021
$payment->exchange_currency_id = $company_currency;
$payment->saveQuietly();
}
}
}