2020-02-11 21:57:25 +01:00
|
|
|
<?php
|
|
|
|
namespace App\Services\Quote;
|
|
|
|
|
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;
|
2020-03-03 10:44:26 +01:00
|
|
|
use App\Services\Quote\CreateInvitations;
|
2020-02-11 21:57:25 +01:00
|
|
|
|
|
|
|
class QuoteService
|
|
|
|
{
|
|
|
|
protected $quote;
|
|
|
|
|
|
|
|
public function __construct($quote)
|
|
|
|
{
|
|
|
|
$this->quote = $quote;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function createInvitations()
|
|
|
|
{
|
|
|
|
$create_invitation = new CreateInvitations();
|
|
|
|
|
2020-02-14 04:32:22 +01:00
|
|
|
$this->quote = $create_invitation->run($this->quote);
|
2020-02-11 21:57:25 +01:00
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function markApproved()
|
|
|
|
{
|
|
|
|
$mark_approved = new MarkApproved($this->quote->client);
|
|
|
|
|
2020-02-14 04:32:22 +01:00
|
|
|
$this->quote = $mark_approved->run($this->quote);
|
2020-02-11 21:57:25 +01:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
if ($this->quote->client->getSetting('auto_convert_quote') === true) {
|
2020-02-11 21:57:25 +01:00
|
|
|
$convert_quote = new ConvertQuote($this->quote->client);
|
2020-02-14 04:32:22 +01:00
|
|
|
$this->quote = $convert_quote->run($this->quote);
|
2020-02-11 21:57:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2020-02-15 10:01:15 +01:00
|
|
|
public function getQuotePdf($contact)
|
|
|
|
{
|
|
|
|
$get_invoice_pdf = new GetQuotePdf();
|
|
|
|
|
|
|
|
return $get_invoice_pdf($this->quote, $contact);
|
|
|
|
}
|
|
|
|
|
2020-03-07 13:46:45 +01:00
|
|
|
public function sendEmail($contact) :QuoteService
|
2020-02-15 10:01:15 +01:00
|
|
|
{
|
|
|
|
$send_email = new SendEmail($this->quote);
|
|
|
|
|
2020-03-07 13:46:45 +01:00
|
|
|
$send_email->run(null, $contact);
|
|
|
|
|
|
|
|
return $this;
|
2020-02-15 10:01:15 +01:00
|
|
|
}
|
|
|
|
|
2020-02-11 21:57:25 +01:00
|
|
|
/**
|
|
|
|
* Applies the invoice number
|
|
|
|
* @return $this InvoiceService object
|
|
|
|
*/
|
2020-03-07 13:46:45 +01:00
|
|
|
public function applyNumber() :QuoteService
|
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-03-07 13:46:45 +01:00
|
|
|
public function markSent() :QuoteService
|
2020-02-11 21:57:25 +01:00
|
|
|
{
|
2020-03-10 13:54:20 +01:00
|
|
|
$mark_sent = new MarkSent($this->quote->client, $this->quote);
|
2020-02-11 21:57:25 +01:00
|
|
|
|
2020-03-10 13:54:20 +01:00
|
|
|
$this->quote = $mark_sent->run();
|
2020-02-11 21:57:25 +01:00
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2020-03-07 13:46:45 +01:00
|
|
|
public function setStatus($status) :QuoteService
|
2020-02-11 21:57:25 +01:00
|
|
|
{
|
|
|
|
$this->quote->status_id = $status;
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2020-03-07 13:46:45 +01:00
|
|
|
public function approve() :QuoteService
|
|
|
|
{
|
|
|
|
$this->setStatus(Quote::STATUS_APPROVED)->save();
|
|
|
|
|
|
|
|
$invoice = null;
|
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
if ($this->quote->client->getSetting('auto_convert_quote')) {
|
2020-03-07 13:46:45 +01:00
|
|
|
$invoice = $this->convertToInvoice();
|
|
|
|
$this->linkInvoiceToQuote($invoice)->save();
|
|
|
|
}
|
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
if ($this->quote->client->getSetting('auto_archive_quote')) {
|
2020-03-07 13:46:45 +01:00
|
|
|
$quote_repo = new QuoteRepository();
|
|
|
|
$quote_repo->archive($this->quote);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Where we convert a quote to an invoice we link the two entities via the invoice_id parameter on the quote table
|
|
|
|
* @param object $invoice The Invoice object
|
|
|
|
* @return object QuoteService
|
|
|
|
*/
|
|
|
|
public function linkInvoiceToQuote($invoice) :QuoteService
|
|
|
|
{
|
|
|
|
$this->quote->invoice_id = $invoice->id;
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function convertToInvoice() :Invoice
|
|
|
|
{
|
|
|
|
Invoice::unguard();
|
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
$invoice = new Invoice((array) $this->quote);
|
|
|
|
$invoice->status_id = Invoice::STATUS_SENT;
|
|
|
|
$invoice->due_date = null;
|
|
|
|
$invoice->invitations = null;
|
|
|
|
$invoice->number = null;
|
|
|
|
$invoice->save();
|
2020-03-07 13:46:45 +01:00
|
|
|
|
|
|
|
Invoice::reguard();
|
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
$invoice->service()->markSent()->createInvitations()->save();
|
2020-03-07 13:46:45 +01:00
|
|
|
|
|
|
|
return $invoice;
|
|
|
|
}
|
|
|
|
|
2020-02-11 21:57:25 +01:00
|
|
|
/**
|
|
|
|
* Saves the quote
|
|
|
|
* @return Quote|null
|
|
|
|
*/
|
|
|
|
public function save() : ?Quote
|
|
|
|
{
|
|
|
|
$this->quote->save();
|
|
|
|
return $this->quote;
|
|
|
|
}
|
|
|
|
}
|