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
|
|
|
|
*
|
2022-04-27 05:20:41 +02:00
|
|
|
* @copyright Copyright (c) 2022. 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\InvoiceWasUpdated;
|
2020-07-23 05:55:11 +02:00
|
|
|
use App\Models\Client;
|
2020-02-03 11:33:07 +01:00
|
|
|
use App\Models\Invoice;
|
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-03 11:33:07 +01:00
|
|
|
|
2020-02-17 10:37:44 +01:00
|
|
|
class MarkSent extends AbstractService
|
2020-02-03 11:33:07 +01:00
|
|
|
{
|
2020-07-23 05:55:11 +02:00
|
|
|
public $client;
|
2020-02-03 11:33:07 +01:00
|
|
|
|
2020-07-23 05:55:11 +02:00
|
|
|
public $invoice;
|
2020-02-17 10:37:44 +01:00
|
|
|
|
2020-07-23 05:55:11 +02:00
|
|
|
public function __construct(Client $client, Invoice $invoice)
|
2020-02-03 11:33:07 +01:00
|
|
|
{
|
|
|
|
$this->client = $client;
|
2022-06-21 11:57:17 +02:00
|
|
|
|
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()
|
|
|
|
{
|
2020-02-03 11:33:07 +01:00
|
|
|
|
2022-02-06 03:46:19 +01:00
|
|
|
/* Return immediately if status is not draft or invoice has been deleted */
|
|
|
|
if ($this->invoice && ($this->invoice->fresh()->status_id != Invoice::STATUS_DRAFT || $this->invoice->is_deleted)) {
|
2020-02-17 10:37:44 +01:00
|
|
|
return $this->invoice;
|
2020-02-03 11:33:07 +01:00
|
|
|
}
|
|
|
|
|
2021-11-10 06:41:33 +01:00
|
|
|
$adjustment = $this->invoice->amount;
|
|
|
|
|
2021-10-19 11:04:19 +02:00
|
|
|
/*Set status*/
|
2020-04-15 02:30:52 +02:00
|
|
|
$this->invoice
|
|
|
|
->service()
|
|
|
|
->setStatus(Invoice::STATUS_SENT)
|
2021-11-10 06:41:33 +01:00
|
|
|
->updateBalance($adjustment, true)
|
2021-10-19 11:04:19 +02:00
|
|
|
->save();
|
|
|
|
|
|
|
|
/*Update ledger*/
|
|
|
|
$this->invoice
|
|
|
|
->ledger()
|
2021-11-10 06:41:33 +01:00
|
|
|
->updateInvoiceBalance($adjustment, "Invoice {$this->invoice->number} marked as sent.");
|
|
|
|
|
|
|
|
/* Perform additional actions on invoice */
|
|
|
|
$this->invoice
|
|
|
|
->service()
|
|
|
|
->applyNumber()
|
|
|
|
->setDueDate()
|
2022-04-22 14:01:32 +02:00
|
|
|
->touchPdf()
|
2021-11-10 06:41:33 +01:00
|
|
|
->setReminder()
|
|
|
|
->save();
|
|
|
|
|
2022-03-29 10:57:14 +02:00
|
|
|
/*Adjust client balance*/
|
2022-04-22 14:01:32 +02:00
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
\DB::connection(config('database.default'))->transaction(function () use ($adjustment) {
|
2022-04-22 14:01:32 +02:00
|
|
|
|
|
|
|
/* Get the last record for the client and set the current balance*/
|
2022-04-28 04:40:07 +02:00
|
|
|
$client = Client::withTrashed()->where('id', $this->client->id)->lockForUpdate()->first();
|
2022-04-22 14:01:32 +02:00
|
|
|
$client->balance += $adjustment;
|
|
|
|
$client->save();
|
|
|
|
}, 1);
|
2022-03-29 10:57:14 +02:00
|
|
|
|
2021-11-10 06:41:33 +01:00
|
|
|
$this->invoice->markInvitationsSent();
|
2020-02-20 22:05:01 +01:00
|
|
|
|
2021-04-13 01:52:17 +02:00
|
|
|
event(new InvoiceWasUpdated($this->invoice, $this->invoice->company, Ninja::eventVars(auth()->user() ? auth()->user()->id : null)));
|
2020-09-06 11:38:10 +02:00
|
|
|
|
2020-06-06 03:07:31 +02:00
|
|
|
return $this->invoice->fresh();
|
2020-03-21 06:37:30 +01:00
|
|
|
}
|
2020-02-03 11:33:07 +01:00
|
|
|
}
|