2020-02-03 11:33:07 +01:00
|
|
|
<?php
|
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
2020-02-03 11:33:07 +01:00
|
|
|
*
|
|
|
|
* @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)
|
2020-02-03 11:33:07 +01:00
|
|
|
*
|
2021-06-16 08:58:16 +02:00
|
|
|
* @license https://www.elastic.co/licensing/elastic-license
|
2020-02-03 11:33:07 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
namespace App\Services\Invoice;
|
|
|
|
|
2020-08-12 03:45:40 +02:00
|
|
|
use App\Events\Invoice\InvoiceWasPaid;
|
2020-02-03 11:33:07 +01:00
|
|
|
use App\Events\Payment\PaymentWasCreated;
|
|
|
|
use App\Factory\PaymentFactory;
|
2021-06-22 00:39:08 +02:00
|
|
|
use App\Libraries\Currency\Conversion\CurrencyApi;
|
2020-02-03 11:33:07 +01:00
|
|
|
use App\Models\Invoice;
|
|
|
|
use App\Models\Payment;
|
2020-02-17 10:37:44 +01:00
|
|
|
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;
|
2021-06-22 00:39:08 +02:00
|
|
|
use Illuminate\Support\Carbon;
|
2020-02-03 11:33:07 +01:00
|
|
|
|
2020-02-17 10:37:44 +01:00
|
|
|
class MarkPaid extends AbstractService
|
2020-02-03 11:33:07 +01:00
|
|
|
{
|
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)
|
|
|
|
{
|
|
|
|
}
|
2020-02-03 11:33:07 +01:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
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-09-06 11:38:10 +02:00
|
|
|
}
|
2020-07-15 08:08:57 +02:00
|
|
|
|
2022-08-29 10:15:50 +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-08-29 10:15:50 +02:00
|
|
|
}
|
|
|
|
|
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();
|
2022-08-29 10:15:50 +02:00
|
|
|
|
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()
|
2023-09-26 23:30:35 +02:00
|
|
|
->clearPartial()
|
2023-01-15 04:59:42 +01:00
|
|
|
->updateBalance($this->payable_balance * -1)
|
|
|
|
->updatePaidToDate($this->payable_balance)
|
|
|
|
->setStatus(Invoice::STATUS_PAID)
|
|
|
|
->save();
|
|
|
|
}
|
2022-08-29 10:15:50 +02:00
|
|
|
}, 1);
|
|
|
|
|
2020-02-03 11:33:07 +01:00
|
|
|
/* Create Payment */
|
2020-02-17 10:37:44 +01:00
|
|
|
$payment = PaymentFactory::create($this->invoice->company_id, $this->invoice->user_id);
|
2020-02-03 11:33:07 +01:00
|
|
|
|
2022-09-05 09:18:08 +02:00
|
|
|
$payment->amount = $this->payable_balance;
|
|
|
|
$payment->applied = $this->payable_balance;
|
2020-02-03 11:33:07 +01:00
|
|
|
$payment->status_id = Payment::STATUS_COMPLETED;
|
2020-02-17 10:37:44 +01:00
|
|
|
$payment->client_id = $this->invoice->client_id;
|
2023-02-09 23:47:22 +01:00
|
|
|
$payment->transaction_reference = $this->reference ?: ctrans('texts.manual_entry');
|
2020-03-08 06:59:06 +01:00
|
|
|
$payment->currency_id = $this->invoice->client->getSetting('currency_id');
|
2020-07-29 00:20:40 +02:00
|
|
|
$payment->is_manual = true;
|
2022-05-11 07:25:33 +02:00
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
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-06-21 11:57:17 +02:00
|
|
|
}
|
2022-04-05 12:47:44 +02:00
|
|
|
|
2021-10-12 12:48:07 +02:00
|
|
|
$payment_type_id = $this->invoice->client->getSetting('payment_type_id');
|
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
if ((int) $payment_type_id > 0) {
|
|
|
|
$payment->type_id = (int) $payment_type_id;
|
|
|
|
}
|
2021-10-12 12:48:07 +02:00
|
|
|
|
2021-10-15 09:47:41 +02:00
|
|
|
$payment->saveQuietly();
|
2020-02-03 11:33:07 +01:00
|
|
|
|
2022-08-05 00:08:19 +02:00
|
|
|
$payment->service()->applyNumber()->save();
|
|
|
|
|
2021-10-12 12:48:07 +02:00
|
|
|
/* Create a payment relationship to the invoice entity */
|
2020-02-17 10:37:44 +01:00
|
|
|
$payment->invoices()->attach($this->invoice->id, [
|
2022-09-05 09:18:08 +02:00
|
|
|
'amount' => $this->payable_balance,
|
2020-02-03 11:33:07 +01:00
|
|
|
]);
|
|
|
|
|
2023-02-16 02:36:09 +01:00
|
|
|
if ($payment->company->getSetting('send_email_on_mark_paid')) {
|
2023-01-22 08:25:04 +01:00
|
|
|
$payment->service()->sendEmail();
|
2023-02-16 02:36:09 +01:00
|
|
|
}
|
2023-01-22 08:25:04 +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;
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2021-10-22 01:55:58 +02:00
|
|
|
$this->invoice
|
|
|
|
->service()
|
2020-05-28 02:59:33 +02:00
|
|
|
->applyNumber()
|
2023-09-05 03:54:05 +02:00
|
|
|
// ->deletePdf()
|
2020-02-04 08:51:44 +01:00
|
|
|
->save();
|
2020-02-03 11:33:07 +01:00
|
|
|
|
2020-02-20 22:05:01 +01:00
|
|
|
$payment->ledger()
|
2022-09-05 09:18:08 +02:00
|
|
|
->updatePaymentBalance($this->payable_balance * -1);
|
2020-02-14 04:32:22 +01:00
|
|
|
|
2022-09-06 12:51:42 +02:00
|
|
|
//06-09-2022
|
|
|
|
$this->invoice
|
|
|
|
->client
|
|
|
|
->service()
|
|
|
|
->updateBalanceAndPaidToDate($payment->amount*-1, $payment->amount)
|
|
|
|
->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);
|
|
|
|
|
2020-02-17 10:37:44 +01:00
|
|
|
return $this->invoice;
|
2020-03-21 06:37:30 +01:00
|
|
|
}
|
2021-06-22 00:39:08 +02:00
|
|
|
|
|
|
|
private function setExchangeRate(Payment $payment)
|
|
|
|
{
|
2022-06-21 11:57:17 +02:00
|
|
|
if ($payment->exchange_rate != 1) {
|
2022-02-08 10:36:07 +01:00
|
|
|
return;
|
2022-06-21 11:57:17 +02:00
|
|
|
}
|
2022-02-08 10:36:07 +01:00
|
|
|
|
2021-06-22 00:39:08 +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));
|
2021-06-23 08:49:40 +02:00
|
|
|
//$payment->exchange_currency_id = $client_currency; // 23/06/2021
|
|
|
|
$payment->exchange_currency_id = $company_currency;
|
2021-06-22 00:39:08 +02:00
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
$payment->saveQuietly();
|
2021-06-22 00:39:08 +02:00
|
|
|
}
|
|
|
|
}
|
2020-02-03 11:33:07 +01:00
|
|
|
}
|