1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-15 23:52:33 +01:00
invoiceninja/app/Services/Invoice/MarkSent.php

70 lines
2.0 KiB
PHP
Raw Normal View History

<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
2024-04-12 06:15:41 +02:00
* @copyright Copyright (c) 2024. 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\InvoiceWasUpdated;
2020-07-23 05:55:11 +02:00
use App\Models\Client;
use App\Models\Invoice;
use App\Models\Webhook;
use App\Services\AbstractService;
2020-07-08 14:02:16 +02:00
use App\Utils\Ninja;
class MarkSent extends AbstractService
{
2023-02-22 20:22:20 +01:00
public function __construct(public Client $client, public Invoice $invoice)
2023-03-18 08:24:56 +01:00
{
}
2022-12-08 01:17:18 +01:00
public function run($fire_webhook = false)
{
/* 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)) {
return $this->invoice;
}
2021-11-10 06:41:33 +01:00
$adjustment = $this->invoice->amount;
/*Set status*/
$this->invoice
->service()
->setStatus(Invoice::STATUS_SENT)
2021-11-10 06:41:33 +01:00
->updateBalance($adjustment, true)
->save();
/*Update ledger*/
2023-11-07 11:58:52 +01:00
$this->invoice
->ledger()
->updateInvoiceBalance($adjustment, "Invoice {$this->invoice->number} marked as sent.");
2023-11-07 04:54:44 +01:00
2023-11-08 05:19:30 +01:00
$this->invoice->client->service()->calculateBalance();
2021-11-10 06:41:33 +01:00
/* Perform additional actions on invoice */
$this->invoice
->service()
->applyNumber()
->setDueDate()
->setReminder()
->save();
$this->invoice->markInvitationsSent();
event(new InvoiceWasUpdated($this->invoice, $this->invoice->company, Ninja::eventVars(auth()->user() ? auth()->user()->id : null)));
2023-02-16 02:36:09 +01:00
if ($fire_webhook) {
2022-12-08 01:17:18 +01:00
event('eloquent.updated: App\Models\Invoice', $this->invoice);
2023-12-12 08:43:55 +01:00
$this->invoice->sendEvent(Webhook::EVENT_SENT_INVOICE, "client");
2023-02-16 02:36:09 +01:00
}
2020-06-06 03:07:31 +02:00
return $this->invoice->fresh();
}
}