2020-02-11 21:57:25 +01:00
|
|
|
<?php
|
2020-07-01 03:06:40 +02:00
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
2020-07-01 03:06:40 +02:00
|
|
|
*
|
|
|
|
* @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)
|
2020-07-01 03:06:40 +02:00
|
|
|
*
|
2021-06-16 08:58:16 +02:00
|
|
|
* @license https://www.elastic.co/licensing/elastic-license
|
2020-07-01 03:06:40 +02:00
|
|
|
*/
|
|
|
|
|
2020-02-11 21:57:25 +01:00
|
|
|
namespace App\Services\Quote;
|
|
|
|
|
|
|
|
use App\Events\Quote\QuoteWasMarkedSent;
|
2023-02-03 12:54:34 +01:00
|
|
|
use App\Jobs\Util\WebhookHandler;
|
2020-02-11 21:57:25 +01:00
|
|
|
use App\Models\Quote;
|
2023-02-03 12:54:34 +01:00
|
|
|
use App\Models\Webhook;
|
2020-07-08 14:02:16 +02:00
|
|
|
use App\Utils\Ninja;
|
2021-04-01 10:07:32 +02:00
|
|
|
use Carbon\Carbon;
|
2020-02-11 21:57:25 +01:00
|
|
|
|
|
|
|
class MarkSent
|
|
|
|
{
|
|
|
|
private $client;
|
|
|
|
|
2020-03-10 13:54:20 +01:00
|
|
|
private $quote;
|
|
|
|
|
|
|
|
public function __construct($client, $quote)
|
2020-02-11 21:57:25 +01:00
|
|
|
{
|
|
|
|
$this->client = $client;
|
2020-03-10 13:54:20 +01:00
|
|
|
$this->quote = $quote;
|
2020-02-11 21:57:25 +01:00
|
|
|
}
|
|
|
|
|
2020-03-10 13:54:20 +01:00
|
|
|
public function run()
|
2020-02-11 21:57:25 +01:00
|
|
|
{
|
|
|
|
|
|
|
|
/* Return immediately if status is not draft */
|
2020-03-10 13:54:20 +01:00
|
|
|
if ($this->quote->status_id != Quote::STATUS_DRAFT) {
|
2020-04-16 10:41:25 +02:00
|
|
|
return $this->quote;
|
2020-02-11 21:57:25 +01:00
|
|
|
}
|
|
|
|
|
2020-03-10 13:54:20 +01:00
|
|
|
$this->quote->markInvitationsSent();
|
2020-02-11 21:57:25 +01:00
|
|
|
|
2021-04-01 10:07:32 +02:00
|
|
|
if ($this->quote->due_date != '' || $this->quote->client->getSetting('valid_until') == '') {
|
2022-06-21 11:57:17 +02:00
|
|
|
} else {
|
2021-04-01 10:07:32 +02:00
|
|
|
$this->quote->due_date = Carbon::parse($this->quote->date)->addDays($this->quote->client->getSetting('valid_until'));
|
|
|
|
}
|
|
|
|
|
2020-05-19 00:22:18 +02:00
|
|
|
$this->quote
|
|
|
|
->service()
|
|
|
|
->setStatus(Quote::STATUS_SENT)
|
|
|
|
->applyNumber()
|
2022-04-01 04:46:55 +02:00
|
|
|
->touchPdf()
|
2020-05-19 00:22:18 +02:00
|
|
|
->save();
|
2020-02-11 21:57:25 +01:00
|
|
|
|
2021-12-17 12:11:36 +01:00
|
|
|
event(new QuoteWasMarkedSent($this->quote, $this->quote->company, Ninja::eventVars(auth()->user() ? auth()->user()->id : null)));
|
|
|
|
|
2023-02-03 12:54:34 +01:00
|
|
|
$subscriptions = Webhook::where('company_id', $this->quote->company_id)
|
|
|
|
->where('event_id', Webhook::EVENT_SENT_QUOTE)
|
|
|
|
->exists();
|
|
|
|
|
|
|
|
if ($subscriptions)
|
|
|
|
WebhookHandler::dispatch(Webhook::EVENT_SENT_QUOTE, $this->quote, $this->quote->company, 'client')->delay(0);
|
|
|
|
|
2020-03-10 13:54:20 +01:00
|
|
|
return $this->quote;
|
2020-02-11 21:57:25 +01:00
|
|
|
}
|
|
|
|
}
|