1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-14 07:02:34 +01:00
invoiceninja/app/Services/Quote/QuoteService.php

241 lines
6.2 KiB
PHP
Raw Normal View History

<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
2023-01-28 23:21:40 +01:00
* @copyright Copyright (c) 2023. 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\Quote;
2023-10-26 04:57:44 +02:00
use App\Events\Quote\QuoteWasApproved;
2023-06-26 13:05:15 +02:00
use App\Exceptions\QuoteConversion;
2023-10-26 04:57:44 +02:00
use App\Models\Project;
use App\Models\Quote;
2023-06-26 13:05:15 +02:00
use App\Repositories\QuoteRepository;
2023-10-26 04:57:44 +02:00
use App\Utils\Ninja;
use App\Utils\Traits\MakesHash;
2023-06-30 07:43:28 +02:00
use Illuminate\Support\Facades\Storage;
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 convertToProject(): Project
{
$project = (new ConvertQuoteToProject($this->quote))->run();
return $project;
}
public function convert() :self
{
if ($this->quote->invoice_id) {
2023-06-26 13:05:15 +02:00
throw new QuoteConversion();
}
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();
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);
}
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();
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
if ($this->quote->client->getSetting('auto_convert_quote')) {
$this->convert();
2021-01-19 22:30:04 +01:00
$this->invoice
->service()
->markSent()
2023-09-05 03:54:05 +02:00
// ->deletePdf()
2021-01-19 22:30:04 +01:00
->save();
}
2021-12-17 12:11:36 +01:00
event(new QuoteWasApproved($contact, $this->quote, $this->quote->company, Ninja::eventVars()));
return $this;
}
2022-04-01 04:46:55 +02:00
public function approveWithNoCoversion($contact = null) :self
{
$this->setStatus(Quote::STATUS_APPROVED)->save();
if (! $contact) {
2022-03-17 05:48:50 +01:00
$contact = $this->quote->invitations->first()->contact;
}
2022-03-17 05:48:50 +01:00
event(new QuoteWasApproved($contact, $this->quote, $this->quote->company, Ninja::eventVars()));
return $this;
}
public function convertToInvoice()
{
$this->convert();
2021-01-19 22:30:04 +01:00
$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;
}
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) {
2021-01-18 21:02:32 +01:00
$this->quote->exchange_rate = $this->quote->client->currency()->exchange_rate;
}
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-25 15:19:52 +01:00
return $this;
}
2021-09-15 03:12:36 +02:00
public function triggeredActions($request)
{
2022-04-20 03:55:33 +02:00
$this->quote = (new TriggeredActions($this->quote->load('invitations'), $request))->run();
2021-09-15 03:12:36 +02:00
return $this;
}
2021-03-01 00:40:18 +01:00
public function deletePdf()
{
$this->quote->invitations->each(function ($invitation) {
2023-06-30 07:43:28 +02:00
// (new UnlinkFile(config('filesystems.default'), $this->quote->client->quote_filepath($invitation).$this->quote->numberFormatter().'.pdf'))->handle();
//30-06-2023
try {
// if (Storage::disk(config('filesystems.default'))->exists($this->invoice->client->invoice_filepath($invitation).$this->invoice->numberFormatter().'.pdf')) {
Storage::disk(config('filesystems.default'))->delete($this->quote->client->quote_filepath($invitation).$this->quote->numberFormatter().'.pdf');
// }
// if (Ninja::isHosted() && Storage::disk('public')->exists($this->invoice->client->invoice_filepath($invitation).$this->invoice->numberFormatter().'.pdf')) {
if (Ninja::isHosted()) {
Storage::disk('public')->delete($this->quote->client->quote_filepath($invitation).$this->quote->numberFormatter().'.pdf');
}
} catch (\Exception $e) {
nlog($e->getMessage());
}
2021-06-12 13:50:01 +02:00
});
2021-03-01 00:40:18 +01:00
return $this;
}
/**
* Saves the quote.
* @return Quote|null
*/
public function save() : ?Quote
{
$this->quote->saveQuietly();
return $this->quote;
}
}