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;
|
|
|
|
|
2021-01-14 00:00:32 +01:00
|
|
|
use App\Factory\ClientContactFactory;
|
2020-02-11 21:57:25 +01:00
|
|
|
use App\Factory\QuoteInvitationFactory;
|
2021-01-14 00:00:32 +01:00
|
|
|
use App\Models\Quote;
|
2020-02-11 21:57:25 +01:00
|
|
|
use App\Models\QuoteInvitation;
|
2021-10-14 07:25:09 +02:00
|
|
|
use App\Utils\Traits\MakesHash;
|
2021-06-19 05:09:22 +02:00
|
|
|
use Illuminate\Support\Str;
|
2020-02-11 21:57:25 +01:00
|
|
|
|
|
|
|
class CreateInvitations
|
|
|
|
{
|
2021-10-14 07:25:09 +02:00
|
|
|
use MakesHash;
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2021-01-14 00:00:32 +01:00
|
|
|
public $quote;
|
|
|
|
|
|
|
|
public function __construct(Quote $quote)
|
2020-02-11 21:57:25 +01:00
|
|
|
{
|
2021-01-14 00:00:32 +01:00
|
|
|
$this->quote = $quote;
|
2020-02-11 21:57:25 +01:00
|
|
|
}
|
|
|
|
|
2021-01-14 00:00:32 +01:00
|
|
|
public function run()
|
2020-02-11 21:57:25 +01:00
|
|
|
{
|
2022-06-21 11:57:17 +02:00
|
|
|
$contacts = $this->quote->client->contacts;
|
2021-01-14 00:00:32 +01:00
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
if ($contacts->count() == 0) {
|
2021-01-14 00:00:32 +01:00
|
|
|
$this->createBlankContact();
|
|
|
|
|
|
|
|
$this->quote->refresh();
|
|
|
|
$contacts = $this->quote->client->contacts;
|
|
|
|
}
|
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
$contacts->each(function ($contact) {
|
2023-08-06 09:03:12 +02:00
|
|
|
$invitation = QuoteInvitation::query()
|
|
|
|
->where('company_id',$this->quote->company_id)
|
2020-02-11 21:57:25 +01:00
|
|
|
->whereClientContactId($contact->id)
|
2021-01-14 00:00:32 +01:00
|
|
|
->whereQuoteId($this->quote->id)
|
2021-01-25 11:54:36 +01:00
|
|
|
->withTrashed()
|
2020-02-11 21:57:25 +01:00
|
|
|
->first();
|
|
|
|
|
2020-09-06 11:38:10 +02:00
|
|
|
if (! $invitation && $contact->send_email) {
|
2021-01-14 00:00:32 +01:00
|
|
|
$ii = QuoteInvitationFactory::create($this->quote->company_id, $this->quote->user_id);
|
2021-11-06 01:46:12 +01:00
|
|
|
$ii->key = $this->createDbHash($this->quote->company->db);
|
2021-01-14 00:00:32 +01:00
|
|
|
$ii->quote_id = $this->quote->id;
|
2020-02-11 21:57:25 +01:00
|
|
|
$ii->client_contact_id = $contact->id;
|
2023-01-31 15:06:21 +01:00
|
|
|
$ii->saveQuietly();
|
2020-09-06 11:38:10 +02:00
|
|
|
} elseif ($invitation && ! $contact->send_email) {
|
2020-02-11 21:57:25 +01:00
|
|
|
$invitation->delete();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
if ($this->quote->invitations()->count() == 0) {
|
|
|
|
if ($contacts->count() == 0) {
|
2021-11-09 11:15:08 +01:00
|
|
|
$contact = $this->createBlankContact();
|
2022-06-21 11:57:17 +02:00
|
|
|
} else {
|
2021-11-09 11:15:08 +01:00
|
|
|
$contact = $contacts->first();
|
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
$invitation = QuoteInvitation::where('company_id', $this->quote->company_id)
|
2021-11-09 11:15:08 +01:00
|
|
|
->where('client_contact_id', $contact->id)
|
|
|
|
->where('quote_id', $this->quote->id)
|
|
|
|
->withTrashed()
|
|
|
|
->first();
|
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
if ($invitation) {
|
|
|
|
$invitation->restore();
|
|
|
|
|
|
|
|
return $this->quote;
|
|
|
|
}
|
2021-11-09 11:15:08 +01:00
|
|
|
}
|
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
$ii = QuoteInvitationFactory::create($this->quote->company_id, $this->quote->user_id);
|
|
|
|
$ii->key = $this->createDbHash($this->quote->company->db);
|
|
|
|
$ii->quote_id = $this->quote->id;
|
|
|
|
$ii->client_contact_id = $contact->id;
|
2023-01-31 15:06:21 +01:00
|
|
|
$ii->saveQuietly();
|
2021-11-09 11:15:08 +01:00
|
|
|
}
|
|
|
|
|
2021-01-14 00:00:32 +01:00
|
|
|
return $this->quote->fresh();
|
2020-02-11 21:57:25 +01:00
|
|
|
}
|
2021-01-14 00:00:32 +01:00
|
|
|
|
|
|
|
private function createBlankContact()
|
|
|
|
{
|
2021-08-24 07:58:43 +02:00
|
|
|
$new_contact = ClientContactFactory::create($this->quote->company_id, $this->quote->user_id);
|
2021-01-14 00:00:32 +01:00
|
|
|
$new_contact->client_id = $this->quote->client_id;
|
|
|
|
$new_contact->contact_key = Str::random(40);
|
|
|
|
$new_contact->is_primary = true;
|
2023-01-31 15:06:21 +01:00
|
|
|
$new_contact->saveQuietly();
|
2021-01-14 00:00:32 +01:00
|
|
|
}
|
2020-02-11 21:57:25 +01:00
|
|
|
}
|