2020-02-11 21:57:25 +01:00
|
|
|
<?php
|
2020-05-09 00:35:49 +02:00
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
2020-05-09 00:35:49 +02:00
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
2021-01-03 22:54:54 +01:00
|
|
|
* @copyright Copyright (c) 2021. Invoice Ninja LLC (https://invoiceninja.com)
|
2020-05-09 00:35:49 +02:00
|
|
|
*
|
2021-06-16 08:58:16 +02:00
|
|
|
* @license https://www.elastic.co/licensing/elastic-license
|
2020-05-09 00:35:49 +02:00
|
|
|
*/
|
|
|
|
|
2020-02-11 21:57:25 +01:00
|
|
|
namespace App\Services\Quote;
|
|
|
|
|
2020-11-03 13:35:05 +01:00
|
|
|
use App\Events\Quote\QuoteWasApproved;
|
2021-10-18 04:31:21 +02:00
|
|
|
use App\Factory\InvoiceInvitationFactory;
|
2021-03-01 00:40:18 +01:00
|
|
|
use App\Jobs\Util\UnlinkFile;
|
2020-03-07 13:46:45 +01:00
|
|
|
use App\Models\Invoice;
|
2020-02-11 21:57:25 +01:00
|
|
|
use App\Models\Quote;
|
2020-03-07 13:46:45 +01:00
|
|
|
use App\Repositories\QuoteRepository;
|
2021-09-15 03:12:36 +02:00
|
|
|
use App\Services\Quote\TriggeredActions;
|
2020-11-03 13:35:05 +01:00
|
|
|
use App\Utils\Ninja;
|
2020-11-04 08:57:16 +01:00
|
|
|
use App\Utils\Traits\MakesHash;
|
2020-02-11 21:57:25 +01:00
|
|
|
|
|
|
|
class QuoteService
|
|
|
|
{
|
2020-11-04 08:57:16 +01:00
|
|
|
use MakesHash;
|
|
|
|
|
2021-01-14 00:00:32 +01:00
|
|
|
public $quote;
|
2020-02-11 21:57:25 +01:00
|
|
|
|
2020-05-27 06:46:19 +02:00
|
|
|
public $invoice;
|
|
|
|
|
2020-02-11 21:57:25 +01:00
|
|
|
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();
|
2020-02-11 21:57:25 +01:00
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2020-09-06 11:38:10 +02:00
|
|
|
public function convert() :self
|
2020-05-27 06:46:19 +02:00
|
|
|
{
|
2020-09-06 11:38:10 +02:00
|
|
|
if ($this->quote->invoice_id) {
|
2020-05-27 06:46:19 +02:00
|
|
|
return $this;
|
2020-09-06 11:38:10 +02:00
|
|
|
}
|
2020-05-27 06:46:19 +02:00
|
|
|
|
2021-01-13 11:12:14 +01:00
|
|
|
$convert_quote = (new ConvertQuote($this->quote->client))->run($this->quote);
|
2020-05-28 02:04:48 +02:00
|
|
|
|
2021-01-13 11:12:14 +01:00
|
|
|
$this->invoice = $convert_quote;
|
2020-05-27 06:46:19 +02:00
|
|
|
|
|
|
|
$this->quote->fresh();
|
|
|
|
|
2021-07-08 03:48:11 +02:00
|
|
|
if ($this->quote->client->getSetting('auto_archive_quote')) {
|
|
|
|
$quote_repo = new QuoteRepository();
|
|
|
|
$quote_repo->archive($this->quote);
|
|
|
|
}
|
|
|
|
|
2020-05-27 06:46:19 +02:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2020-05-09 00:35:49 +02:00
|
|
|
public function getQuotePdf($contact = null)
|
2020-02-15 10:01:15 +01:00
|
|
|
{
|
2020-07-06 06:16:24 +02:00
|
|
|
return (new GetQuotePdf($this->quote, $contact))->run();
|
2020-02-15 10:01:15 +01:00
|
|
|
}
|
|
|
|
|
2020-09-06 11:38:10 +02:00
|
|
|
public function sendEmail($contact = null) :self
|
2020-02-15 10:01:15 +01:00
|
|
|
{
|
2020-05-09 00:35:49 +02:00
|
|
|
$send_email = new SendEmail($this->quote, null, $contact);
|
2020-02-15 10:01:15 +01:00
|
|
|
|
2020-05-09 00:35:49 +02:00
|
|
|
$send_email->run();
|
2020-03-07 13:46:45 +01:00
|
|
|
|
|
|
|
return $this;
|
2020-02-15 10:01:15 +01:00
|
|
|
}
|
|
|
|
|
2020-02-11 21:57:25 +01:00
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* Applies the invoice number.
|
2020-02-11 21:57:25 +01:00
|
|
|
* @return $this InvoiceService object
|
|
|
|
*/
|
2020-09-06 11:38:10 +02:00
|
|
|
public function applyNumber() :self
|
2020-02-11 21:57:25 +01:00
|
|
|
{
|
|
|
|
$apply_number = new ApplyNumber($this->quote->client);
|
|
|
|
|
2020-02-14 04:32:22 +01:00
|
|
|
$this->quote = $apply_number->run($this->quote);
|
2020-02-11 21:57:25 +01:00
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2020-09-06 11:38:10 +02:00
|
|
|
public function markSent() :self
|
2020-02-11 21:57:25 +01:00
|
|
|
{
|
2021-01-13 09:58:01 +01:00
|
|
|
$this->quote = (new MarkSent($this->quote->client, $this->quote))->run();
|
2020-02-11 21:57:25 +01:00
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2020-09-06 11:38:10 +02:00
|
|
|
public function setStatus($status) :self
|
2020-02-11 21:57:25 +01:00
|
|
|
{
|
|
|
|
$this->quote->status_id = $status;
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2020-11-03 13:35:05 +01:00
|
|
|
public function approve($contact = null) :self
|
2020-03-07 13:46:45 +01:00
|
|
|
{
|
|
|
|
$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()));
|
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
if ($this->quote->client->getSetting('auto_convert_quote')) {
|
2020-05-27 06:46:19 +02:00
|
|
|
$this->convert();
|
2021-01-19 22:30:04 +01:00
|
|
|
|
|
|
|
$this->invoice
|
|
|
|
->service()
|
|
|
|
->markSent()
|
2021-03-10 11:00:18 +01:00
|
|
|
->deletePdf()
|
2021-01-19 22:30:04 +01:00
|
|
|
->save();
|
|
|
|
|
2020-03-07 13:46:45 +01:00
|
|
|
}
|
|
|
|
|
2021-01-19 22:30:04 +01:00
|
|
|
|
2021-11-30 21:22:17 +01:00
|
|
|
// if ($this->quote->client->getSetting('auto_archive_quote')) {
|
|
|
|
// $quote_repo = new QuoteRepository();
|
|
|
|
// $quote_repo->archive($this->quote);
|
|
|
|
// }
|
2020-03-07 13:46:45 +01:00
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2020-05-28 02:04:48 +02:00
|
|
|
public function convertToInvoice()
|
2020-03-07 13:46:45 +01:00
|
|
|
{
|
|
|
|
|
2020-05-27 06:46:19 +02:00
|
|
|
$this->convert();
|
2020-03-07 13:46:45 +01:00
|
|
|
|
2021-01-19 22:30:04 +01:00
|
|
|
$this->invoice->service()->createInvitations();
|
|
|
|
|
2020-05-27 06:46:19 +02:00
|
|
|
return $this->invoice;
|
2020-03-07 13:46:45 +01:00
|
|
|
}
|
|
|
|
|
2020-05-28 02:04:48 +02:00
|
|
|
public function isConvertable() :bool
|
|
|
|
{
|
2020-09-06 11:38:10 +02:00
|
|
|
if ($this->quote->invoice_id) {
|
2020-05-28 02:04:48 +02:00
|
|
|
return false;
|
2020-09-06 11:38:10 +02:00
|
|
|
}
|
2020-05-28 02:04:48 +02:00
|
|
|
|
2020-09-06 11:38:10 +02:00
|
|
|
if ($this->quote->status_id == Quote::STATUS_EXPIRED) {
|
2020-05-28 02:04:48 +02:00
|
|
|
return false;
|
2020-09-06 11:38:10 +02:00
|
|
|
}
|
2020-05-28 02:04:48 +02:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-11-04 07:02:15 +01:00
|
|
|
public function fillDefaults()
|
|
|
|
{
|
|
|
|
$settings = $this->quote->client->getMergedSettings();
|
|
|
|
|
2021-01-18 21:02:32 +01:00
|
|
|
if (! $this->quote->design_id)
|
2020-11-04 07:02:15 +01:00
|
|
|
$this->quote->design_id = $this->decodePrimaryKey($settings->quote_design_id);
|
|
|
|
|
2021-01-18 21:02:32 +01:00
|
|
|
if (!isset($this->quote->footer))
|
2020-11-04 07:02:15 +01:00
|
|
|
$this->quote->footer = $settings->quote_footer;
|
2021-01-18 21:02:32 +01:00
|
|
|
|
|
|
|
if (!isset($this->quote->terms))
|
2020-11-04 07:02:15 +01:00
|
|
|
$this->quote->terms = $settings->quote_terms;
|
2021-01-18 21:02:32 +01:00
|
|
|
|
|
|
|
/* 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;
|
2020-11-04 07:02:15 +01:00
|
|
|
|
2021-01-18 21:04:13 +01:00
|
|
|
if (!isset($this->quote->public_notes))
|
2021-01-18 12:08:18 +01:00
|
|
|
$this->quote->public_notes = $this->quote->client->public_notes;
|
2020-11-04 07:02:15 +01:00
|
|
|
|
2020-11-25 15:19:52 +01:00
|
|
|
return $this;
|
2020-11-04 07:02:15 +01:00
|
|
|
}
|
|
|
|
|
2021-09-15 03:12:36 +02:00
|
|
|
public function triggeredActions($request)
|
|
|
|
{
|
|
|
|
$this->quote = (new TriggeredActions($this->quote, $request))->run();
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2021-03-01 00:40:18 +01:00
|
|
|
public function deletePdf()
|
|
|
|
{
|
2021-06-12 13:50:01 +02:00
|
|
|
$this->quote->invitations->each(function ($invitation){
|
|
|
|
|
|
|
|
UnlinkFile::dispatchNow(config('filesystems.default'), $this->quote->client->quote_filepath($invitation) . $this->quote->numberFormatter().'.pdf');
|
|
|
|
|
|
|
|
});
|
2021-03-01 00:40:18 +01:00
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2020-02-11 21:57:25 +01:00
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* Saves the quote.
|
2020-02-11 21:57:25 +01:00
|
|
|
* @return Quote|null
|
|
|
|
*/
|
|
|
|
public function save() : ?Quote
|
|
|
|
{
|
2021-10-10 11:56:05 +02:00
|
|
|
$this->quote->saveQuietly();
|
2020-05-27 06:46:19 +02:00
|
|
|
|
2020-02-11 21:57:25 +01:00
|
|
|
return $this->quote;
|
|
|
|
}
|
|
|
|
}
|