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
|
|
|
|
*
|
2021-01-03 22:54:54 +01:00
|
|
|
* @copyright Copyright (c) 2021. 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-06-19 05:09:22 +02:00
|
|
|
use Illuminate\Support\Str;
|
2020-02-11 21:57:25 +01:00
|
|
|
|
|
|
|
class CreateInvitations
|
|
|
|
{
|
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
|
|
|
{
|
2021-01-14 00:00:32 +01:00
|
|
|
|
|
|
|
$contacts = $this->quote->client->contacts;
|
|
|
|
|
|
|
|
if($contacts->count() == 0){
|
|
|
|
$this->createBlankContact();
|
|
|
|
|
|
|
|
$this->quote->refresh();
|
|
|
|
$contacts = $this->quote->client->contacts;
|
|
|
|
}
|
|
|
|
|
|
|
|
$contacts->each(function ($contact){
|
|
|
|
$invitation = QuoteInvitation::whereCompanyId($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);
|
|
|
|
$ii->quote_id = $this->quote->id;
|
2020-02-11 21:57:25 +01:00
|
|
|
$ii->client_contact_id = $contact->id;
|
|
|
|
$ii->save();
|
2020-09-06 11:38:10 +02:00
|
|
|
} elseif ($invitation && ! $contact->send_email) {
|
2020-02-11 21:57:25 +01:00
|
|
|
$invitation->delete();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
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;
|
|
|
|
$new_contact->save();
|
|
|
|
}
|
|
|
|
|
2020-02-11 21:57:25 +01:00
|
|
|
}
|