1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-10 13:12:50 +01:00
invoiceninja/app/Services/Quote/QuoteService.php
Shift 19080933b6
Apply Laravel coding style
Shift automatically applies the Laravel coding style - which uses the PSR-2 coding style as a base with some minor additions.

You may customize the code style applied by adding a [PHP CS Fixer][1] or [PHP CodeSniffer][2] ruleset to your project root. Feel free to use [Shift's Laravel ruleset][3] to help you get started.

For more information on customizing the code style applied by Shift, [watch this short video][4].

[1]: https://github.com/FriendsOfPHP/PHP-CS-Fixer
[2]: https://github.com/squizlabs/PHP_CodeSniffer
[3]: https://gist.github.com/laravel-shift/cab527923ed2a109dda047b97d53c200
[4]: https://laravelshift.com/videos/shift-code-style
2022-06-21 09:57:17 +00:00

245 lines
6.0 KiB
PHP

<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2022. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://www.elastic.co/licensing/elastic-license
*/
namespace App\Services\Quote;
use App\Events\Quote\QuoteWasApproved;
use App\Factory\InvoiceInvitationFactory;
use App\Jobs\Entity\CreateEntityPdf;
use App\Jobs\Util\UnlinkFile;
use App\Models\Invoice;
use App\Models\Quote;
use App\Repositories\QuoteRepository;
use App\Services\Quote\TriggeredActions;
use App\Utils\Ninja;
use App\Utils\Traits\MakesHash;
class QuoteService
{
use MakesHash;
public $quote;
public $invoice;
public function __construct($quote)
{
$this->quote = $quote;
}
public function createInvitations()
{
$this->quote = (new CreateInvitations($this->quote))->run();
return $this;
}
public function convert() :self
{
if ($this->quote->invoice_id) {
return $this;
}
$convert_quote = (new ConvertQuote($this->quote->client))->run($this->quote);
$this->invoice = $convert_quote;
$this->quote->fresh();
if ($this->quote->client->getSetting('auto_archive_quote')) {
$quote_repo = new QuoteRepository();
$quote_repo->archive($this->quote);
}
return $this;
}
public function getQuotePdf($contact = null)
{
return (new GetQuotePdf($this->quote, $contact))->run();
}
public function sendEmail($contact = null) :self
{
$send_email = new SendEmail($this->quote, null, $contact);
$send_email->run();
return $this;
}
/**
* Applies the invoice number.
* @return $this InvoiceService object
*/
public function applyNumber() :self
{
$apply_number = new ApplyNumber($this->quote->client);
$this->quote = $apply_number->run($this->quote);
return $this;
}
public function markSent() :self
{
$this->quote = (new MarkSent($this->quote->client, $this->quote))->run();
return $this;
}
public function setStatus($status) :self
{
$this->quote->status_id = $status;
return $this;
}
public function approve($contact = null) :self
{
$this->setStatus(Quote::STATUS_APPROVED)->save();
if (! $contact) {
$contact = $this->quote->invitations->first()->contact;
}
if ($this->quote->client->getSetting('auto_convert_quote')) {
$this->convert();
$this->invoice
->service()
->markSent()
->touchPdf()
->save();
}
event(new QuoteWasApproved($contact, $this->quote, $this->quote->company, Ninja::eventVars()));
return $this;
}
/**
* Sometimes we need to refresh the
* PDF when it is updated etc.
* @return InvoiceService
*/
public function touchPdf($force = false)
{
try {
if ($force) {
$this->quote->invitations->each(function ($invitation) {
CreateEntityPdf::dispatchNow($invitation);
});
return $this;
}
$this->quote->invitations->each(function ($invitation) {
CreateEntityPdf::dispatch($invitation);
});
} catch (\Exception $e) {
nlog('failed creating invoices in Touch PDF');
}
return $this;
}
public function approveWithNoCoversion($contact = null) :self
{
$this->setStatus(Quote::STATUS_APPROVED)->save();
if (! $contact) {
$contact = $this->quote->invitations->first()->contact;
}
event(new QuoteWasApproved($contact, $this->quote, $this->quote->company, Ninja::eventVars()));
return $this;
}
public function convertToInvoice()
{
$this->convert();
$this->invoice->service()->createInvitations();
return $this->invoice;
}
public function isConvertable() :bool
{
if ($this->quote->invoice_id) {
return false;
}
if ($this->quote->status_id == Quote::STATUS_EXPIRED) {
return false;
}
return true;
}
public function fillDefaults()
{
$settings = $this->quote->client->getMergedSettings();
if (! $this->quote->design_id) {
$this->quote->design_id = $this->decodePrimaryKey($settings->quote_design_id);
}
if (! isset($this->quote->footer)) {
$this->quote->footer = $settings->quote_footer;
}
if (! isset($this->quote->terms)) {
$this->quote->terms = $settings->quote_terms;
}
/* If client currency differs from the company default currency, then insert the client exchange rate on the model.*/
if (! isset($this->quote->exchange_rate) && $this->quote->client->currency()->id != (int) $this->quote->company->settings->currency_id) {
$this->quote->exchange_rate = $this->quote->client->currency()->exchange_rate;
}
if (! isset($this->quote->public_notes)) {
$this->quote->public_notes = $this->quote->client->public_notes;
}
return $this;
}
public function triggeredActions($request)
{
$this->quote = (new TriggeredActions($this->quote->load('invitations'), $request))->run();
return $this;
}
public function deletePdf()
{
$this->quote->invitations->each(function ($invitation) {
UnlinkFile::dispatchNow(config('filesystems.default'), $this->quote->client->quote_filepath($invitation).$this->quote->numberFormatter().'.pdf');
});
return $this;
}
/**
* Saves the quote.
* @return Quote|null
*/
public function save() : ?Quote
{
$this->quote->saveQuietly();
return $this->quote;
}
}