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
|
|
|
|
*
|
2021-01-03 22:54:54 +01:00
|
|
|
* @copyright Copyright (c) 2021. Invoice Ninja LLC (https://invoiceninja.com)
|
2020-02-03 11:33:07 +01:00
|
|
|
*
|
|
|
|
* @license https://opensource.org/licenses/AAL
|
|
|
|
*/
|
|
|
|
|
|
|
|
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-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;
|
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-02-03 11:33:07 +01:00
|
|
|
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;
|
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;
|
|
|
|
|
2020-02-03 11:33:07 +01:00
|
|
|
private $client_service;
|
|
|
|
|
2020-02-17 10:37:44 +01:00
|
|
|
private $invoice;
|
|
|
|
|
|
|
|
public function __construct(ClientService $client_service, Invoice $invoice)
|
2020-02-03 11:33:07 +01:00
|
|
|
{
|
|
|
|
$this->client_service = $client_service;
|
2020-02-17 10:37:44 +01:00
|
|
|
|
|
|
|
$this->invoice = $invoice;
|
2020-02-03 11:33:07 +01:00
|
|
|
}
|
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
public function run()
|
|
|
|
{
|
|
|
|
if ($this->invoice->status_id == Invoice::STATUS_DRAFT) {
|
2020-02-17 10:37:44 +01:00
|
|
|
$this->invoice->service()->markSent();
|
2020-03-21 06:37:30 +01:00
|
|
|
}
|
2020-02-03 11:33:07 +01:00
|
|
|
|
2020-07-15 08:08:57 +02:00
|
|
|
/*Don't double pay*/
|
2020-09-06 11:38:10 +02:00
|
|
|
if ($this->invoice->statud_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
|
|
|
|
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
|
|
|
|
2020-02-17 10:37:44 +01:00
|
|
|
$payment->amount = $this->invoice->balance;
|
2020-02-27 07:29:40 +01:00
|
|
|
$payment->applied = $this->invoice->balance;
|
2020-02-26 11:46:35 +01:00
|
|
|
$payment->number = $this->getNextPaymentNumber($this->invoice->client);
|
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;
|
2020-02-03 11:33:07 +01:00
|
|
|
$payment->transaction_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;
|
2020-02-03 11:33:07 +01:00
|
|
|
/* Create a payment relationship to the invoice entity */
|
|
|
|
$payment->save();
|
|
|
|
|
2020-02-17 10:37:44 +01:00
|
|
|
$payment->invoices()->attach($this->invoice->id, [
|
2020-09-06 11:38:10 +02:00
|
|
|
'amount' => $payment->amount,
|
2020-02-03 11:33:07 +01:00
|
|
|
]);
|
|
|
|
|
2020-11-04 10:32:49 +01:00
|
|
|
$this->invoice->next_send_date = null;
|
|
|
|
|
2020-02-17 10:37:44 +01:00
|
|
|
$this->invoice->service()
|
2020-09-06 11:38:10 +02:00
|
|
|
->updateBalance($payment->amount * -1)
|
2021-01-24 07:44:14 +01:00
|
|
|
->updatePaidToDate($payment->amount)
|
2020-02-04 08:51:44 +01:00
|
|
|
->setStatus(Invoice::STATUS_PAID)
|
2020-05-28 02:59:33 +02:00
|
|
|
->applyNumber()
|
2020-02-04 08:51:44 +01:00
|
|
|
->save();
|
2020-02-03 11:33:07 +01:00
|
|
|
|
2021-01-17 22:31:49 +01:00
|
|
|
if ($this->invoice->client->getSetting('client_manual_payment_notification'))
|
|
|
|
$payment->service()->sendEmail();
|
2020-11-09 03:57:34 +01:00
|
|
|
|
2020-02-03 11:33:07 +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()));
|
2020-02-03 11:33:07 +01:00
|
|
|
|
2020-02-20 22:05:01 +01:00
|
|
|
$payment->ledger()
|
2020-09-06 11:38:10 +02:00
|
|
|
->updatePaymentBalance($payment->amount * -1);
|
2020-02-14 04:32:22 +01:00
|
|
|
|
2020-02-03 11:33:07 +01:00
|
|
|
$this->client_service
|
2020-09-06 11:38:10 +02:00
|
|
|
->updateBalance($payment->amount * -1)
|
2020-02-03 11:33:07 +01:00
|
|
|
->updatePaidToDate($payment->amount)
|
|
|
|
->save();
|
|
|
|
|
2021-02-05 06:02:38 +01:00
|
|
|
InvoiceWorkflowSettings::dispatchNow($this->invoice);
|
|
|
|
|
2020-02-17 10:37:44 +01:00
|
|
|
return $this->invoice;
|
2020-03-21 06:37:30 +01:00
|
|
|
}
|
2020-02-03 11:33:07 +01:00
|
|
|
}
|