1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-13 22:54:25 +01:00
invoiceninja/app/Services/Quote/CreateInvitations.php

100 lines
3.1 KiB
PHP
Raw Normal View History

<?php
2020-07-01 03:06:40 +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
*/
namespace App\Services\Quote;
2021-01-14 00:00:32 +01:00
use App\Factory\ClientContactFactory;
use App\Factory\QuoteInvitationFactory;
2021-01-14 00:00:32 +01:00
use App\Models\Quote;
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;
class CreateInvitations
{
2021-10-14 07:25:09 +02:00
use MakesHash;
2021-01-14 00:00:32 +01:00
public $quote;
public function __construct(Quote $quote)
{
2021-01-14 00:00:32 +01:00
$this->quote = $quote;
}
2021-01-14 00:00:32 +01:00
public function run()
{
$contacts = $this->quote->client->contacts;
2021-01-14 00:00:32 +01:00
if ($contacts->count() == 0) {
2021-01-14 00:00:32 +01:00
$this->createBlankContact();
$this->quote->refresh();
$contacts = $this->quote->client->contacts;
}
$contacts->each(function ($contact) {
2023-08-06 09:03:12 +02:00
$invitation = QuoteInvitation::query()
->where('company_id',$this->quote->company_id)
->whereClientContactId($contact->id)
2021-01-14 00:00:32 +01:00
->whereQuoteId($this->quote->id)
->withTrashed()
->first();
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;
$ii->client_contact_id = $contact->id;
2023-01-31 15:06:21 +01:00
$ii->saveQuietly();
} elseif ($invitation && ! $contact->send_email) {
$invitation->delete();
}
});
if ($this->quote->invitations()->count() == 0) {
if ($contacts->count() == 0) {
2021-11-09 11:15:08 +01:00
$contact = $this->createBlankContact();
} else {
2021-11-09 11:15:08 +01:00
$contact = $contacts->first();
2023-08-06 09:35:19 +02:00
$invitation = QuoteInvitation::query()->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();
if ($invitation) {
$invitation->restore();
return $this->quote;
}
2021-11-09 11:15:08 +01: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();
}
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
}
}