invoiceninja/app/Services/Invoice/MarkSent.php

74 lines
1.9 KiB
PHP
Raw Normal View History

<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
2022-04-27 13:20:41 +10:00
* @copyright Copyright (c) 2022. Invoice Ninja LLC (https://invoiceninja.com)
*
2021-06-16 16:58:16 +10:00
* @license https://www.elastic.co/licensing/elastic-license
*/
namespace App\Services\Invoice;
2020-08-12 11:45:40 +10:00
use App\Events\Invoice\InvoiceWasUpdated;
2020-07-23 13:55:11 +10:00
use App\Models\Client;
use App\Models\Invoice;
use App\Services\AbstractService;
2020-07-08 22:02:16 +10:00
use App\Utils\Ninja;
class MarkSent extends AbstractService
{
2020-07-23 13:55:11 +10:00
public $client;
2020-07-23 13:55:11 +10:00
public $invoice;
2020-07-23 13:55:11 +10:00
public function __construct(Client $client, Invoice $invoice)
{
$this->client = $client;
$this->invoice = $invoice;
}
public function run()
{
/* 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 16:41:33 +11:00
$adjustment = $this->invoice->amount;
/*Set status*/
$this->invoice
->service()
->setStatus(Invoice::STATUS_SENT)
2021-11-10 16:41:33 +11:00
->updateBalance($adjustment, true)
->save();
/*Update ledger*/
$this->invoice
->ledger()
2021-11-10 16:41:33 +11:00
->updateInvoiceBalance($adjustment, "Invoice {$this->invoice->number} marked as sent.");
/* Perform additional actions on invoice */
$this->invoice
->service()
->applyNumber()
->setDueDate()
->touchPdf()
2021-11-10 16:41:33 +11:00
->setReminder()
->save();
2022-03-29 19:57:14 +11:00
/*Adjust client balance*/
2022-09-05 17:18:08 +10:00
$this->invoice->client->service()->updateBalance($adjustment)->save();
2022-03-29 19:57:14 +11:00
2021-11-10 16:41:33 +11:00
$this->invoice->markInvitationsSent();
event(new InvoiceWasUpdated($this->invoice, $this->invoice->company, Ninja::eventVars(auth()->user() ? auth()->user()->id : null)));
2020-06-06 11:07:31 +10:00
return $this->invoice->fresh();
}
}