1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-18 09:04:35 +01:00
invoiceninja/app/Services/Invoice/MarkPaid.php

100 lines
3.1 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)
*
* @license https://opensource.org/licenses/AAL
*/
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\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;
class MarkPaid extends AbstractService
{
2020-02-26 11:06:08 +01:00
use GeneratesCounter;
private $client_service;
private $invoice;
public function __construct(ClientService $client_service, Invoice $invoice)
{
$this->client_service = $client_service;
$this->invoice = $invoice;
}
public function run()
{
if ($this->invoice->status_id == Invoice::STATUS_DRAFT) {
$this->invoice->service()->markSent();
}
2020-07-15 08:08:57 +02:00
/*Don't double pay*/
if ($this->invoice->statud_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;
$payment->number = $this->getNextPaymentNumber($this->invoice->client);
$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;
/* Create a payment relationship to the invoice entity */
$payment->save();
$payment->invoices()->attach($this->invoice->id, [
'amount' => $payment->amount,
]);
2020-11-04 10:32:49 +01:00
$this->invoice->next_send_date = null;
$this->invoice->service()
->updateBalance($payment->amount * -1)
->updatePaidToDate($payment->amount)
->setStatus(Invoice::STATUS_PAID)
->applyNumber()
2021-03-10 03:28:35 +01:00
->deletePdf()
->save();
if ($this->invoice->client->getSetting('client_manual_payment_notification'))
$payment->service()->sendEmail();
2020-11-09 03:57:34 +01:00
/* Update Invoice balance */
2020-07-08 14:02:16 +02:00
event(new PaymentWasCreated($payment, $payment->company, Ninja::eventVars()));
2021-01-24 21:55:04 +01:00
event(new InvoiceWasPaid($this->invoice, $payment, $payment->company, Ninja::eventVars()));
$payment->ledger()
->updatePaymentBalance($payment->amount * -1);
$this->client_service
->updateBalance($payment->amount * -1)
->updatePaidToDate($payment->amount)
->save();
2021-02-05 06:02:38 +01:00
InvoiceWorkflowSettings::dispatchNow($this->invoice);
return $this->invoice;
}
}