1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-10 21:22:58 +01:00
invoiceninja/app/Services/Quote/QuoteService.php

192 lines
4.3 KiB
PHP
Raw Normal View History

<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2021. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://opensource.org/licenses/AAL
*/
namespace App\Services\Quote;
2020-11-03 13:35:05 +01:00
use App\Events\Quote\QuoteWasApproved;
use App\Models\Invoice;
use App\Models\Quote;
use App\Repositories\QuoteRepository;
2020-11-03 13:35:05 +01:00
use App\Utils\Ninja;
use App\Utils\Traits\MakesHash;
class QuoteService
{
use MakesHash;
2021-01-14 00:00:32 +01:00
public $quote;
public $invoice;
public function __construct($quote)
{
$this->quote = $quote;
}
public function createInvitations()
{
2021-01-14 00:00:32 +01:00
$this->quote = (new CreateInvitations($this->quote))->run();
return $this;
}
public function markApproved()
{
$mark_approved = new MarkApproved($this->quote->client);
$this->quote = $mark_approved->run($this->quote);
if ($this->quote->client->getSetting('auto_convert_quote') === true) {
$this->convert();
}
return $this;
}
public function convert() :self
{
if ($this->quote->invoice_id) {
return $this;
}
2021-01-13 11:12:14 +01:00
$convert_quote = (new ConvertQuote($this->quote->client))->run($this->quote);
2021-01-13 11:12:14 +01:00
$this->invoice = $convert_quote;
$this->quote->fresh();
return $this;
}
public function getQuotePdf($contact = null)
{
2020-07-06 06:16:24 +02:00
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
{
2021-01-13 09:58:01 +01:00
$this->quote = (new MarkSent($this->quote->client, $this->quote))->run();
return $this;
}
public function setStatus($status) :self
{
$this->quote->status_id = $status;
return $this;
}
2020-11-03 13:35:05 +01:00
public function approve($contact = null) :self
{
$this->setStatus(Quote::STATUS_APPROVED)->save();
2020-11-25 15:19:52 +01:00
if (!$contact) {
2020-11-03 13:35:05 +01:00
$contact = $this->quote->invitations->first()->contact;
2020-11-25 15:19:52 +01:00
}
2020-11-03 13:35:05 +01:00
event(new QuoteWasApproved($contact, $this->quote, $this->quote->company, Ninja::eventVars()));
$invoice = null;
if ($this->quote->client->getSetting('auto_convert_quote')) {
$this->convert();
}
if ($this->quote->client->getSetting('auto_archive_quote')) {
$quote_repo = new QuoteRepository();
$quote_repo->archive($this->quote);
}
return $this;
}
public function convertToInvoice()
{
//to prevent circular references we need to explicit call this here.
$mark_approved = new MarkApproved($this->quote->client);
$this->quote = $mark_approved->run($this->quote);
$this->convert();
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();
2020-11-25 15:19:52 +01:00
if (! $this->quote->design_id) {
$this->quote->design_id = $this->decodePrimaryKey($settings->quote_design_id);
2020-11-25 15:19:52 +01:00
}
2020-11-25 15:19:52 +01:00
if (!isset($this->quote->footer)) {
$this->quote->footer = $settings->quote_footer;
2020-11-25 15:19:52 +01:00
}
2020-11-25 15:19:52 +01:00
if (!isset($this->quote->terms)) {
$this->quote->terms = $settings->quote_terms;
2020-11-25 15:19:52 +01:00
}
2021-01-18 12:08:18 +01:00
if (!isset($this->quote->public_notes)) {
$this->quote->public_notes = $this->quote->client->public_notes;
}
2020-11-25 15:19:52 +01:00
return $this;
}
/**
* Saves the quote.
* @return Quote|null
*/
public function save() : ?Quote
{
$this->quote->save();
return $this->quote;
}
}