1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-14 15:13:29 +01:00
invoiceninja/app/Services/Invoice/MarkPaid.php

136 lines
4.2 KiB
PHP
Raw Normal View History

<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2021. 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;
2021-02-05 06:02:38 +01:00
use App\Jobs\Invoice\InvoiceWorkflowSettings;
2020-11-09 03:57:34 +01:00
use App\Jobs\Payment\EmailPayment;
use App\Libraries\Currency\Conversion\CurrencyApi;
use App\Models\Invoice;
use App\Models\Payment;
use App\Services\AbstractService;
use App\Services\Client\ClientService;
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;
private $invoice;
public function __construct(Invoice $invoice)
{
$this->invoice = $invoice;
}
public function run()
{
if ($this->invoice->status_id == Invoice::STATUS_DRAFT) {
2021-10-22 01:55:58 +02:00
$this->invoice->service()->markSent()->save();
}
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
/* Create Payment */
$payment = PaymentFactory::create($this->invoice->company_id, $this->invoice->user_id);
$payment->amount = $this->invoice->balance;
$payment->applied = $this->invoice->balance;
2021-10-17 05:21:13 +02:00
$payment->number = $this->getNextPaymentNumber($this->invoice->client, $payment);
$payment->status_id = Payment::STATUS_COMPLETED;
$payment->client_id = $this->invoice->client_id;
$payment->transaction_reference = ctrans('texts.manual_entry');
$payment->currency_id = $this->invoice->client->getSetting('currency_id');
$payment->is_manual = true;
$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();
$this->setExchangeRate($payment);
/* Create a payment relationship to the invoice entity */
$payment->invoices()->attach($this->invoice->id, [
'amount' => $payment->amount,
]);
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()
->setExchangeRate()
->updateBalance($payment->amount * -1)
->updatePaidToDate($payment->amount)
->setStatus(Invoice::STATUS_PAID)
2021-10-20 08:16:22 +02:00
->save();
2021-10-22 01:55:58 +02:00
$this->invoice
->service()
->applyNumber()
2022-01-10 02:47:16 +01:00
->touchPdf()
->save();
$payment->ledger()
->updatePaymentBalance($payment->amount * -1);
$this->invoice
->client
->service()
->updateBalance($payment->amount * -1)
->updatePaidToDate($payment->amount)
->save();
2021-10-20 08:16:22 +02:00
$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)));
return $this->invoice;
}
private function setExchangeRate(Payment $payment)
{
$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->save();
}
}
}