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

71 lines
1.9 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
*
* @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
*/
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-06-19 05:09:22 +02:00
use Illuminate\Support\Str;
class CreateInvitations
{
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()
{
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)
->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);
$ii->quote_id = $this->quote->id;
$ii->client_contact_id = $contact->id;
$ii->save();
} elseif ($invitation && ! $contact->send_email) {
$invitation->delete();
}
});
2021-01-14 00:00:32 +01:00
return $this->quote->fresh();
}
2021-01-14 00:00:32 +01:00
private function createBlankContact()
{
$new_contact = ClientContacstFactory::create($this->quote->company_id, $this->quote->user_id);
$new_contact->client_id = $this->quote->client_id;
$new_contact->contact_key = Str::random(40);
$new_contact->is_primary = true;
$new_contact->save();
}
}