2020-02-11 21:57:25 +01:00
|
|
|
<?php
|
2020-07-01 03:06:40 +02:00
|
|
|
/**
|
|
|
|
* Invoice Ninja (https://invoiceninja.com)
|
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
|
|
|
* @copyright Copyright (c) 2020. Invoice Ninja LLC (https://invoiceninja.com)
|
|
|
|
*
|
|
|
|
* @license https://opensource.org/licenses/AAL
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2020-02-11 21:57:25 +01:00
|
|
|
namespace App\Services\Quote;
|
|
|
|
|
|
|
|
use App\Factory\QuoteInvitationFactory;
|
|
|
|
use App\Models\QuoteInvitation;
|
|
|
|
|
|
|
|
class CreateInvitations
|
|
|
|
{
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-02-14 04:32:22 +01:00
|
|
|
public function run($quote)
|
2020-02-11 21:57:25 +01:00
|
|
|
{
|
2020-03-21 06:37:30 +01:00
|
|
|
$quote->client->contacts->each(function ($contact) use ($quote) {
|
2020-02-11 21:57:25 +01:00
|
|
|
$invitation = QuoteInvitation::whereCompanyId($quote->company_id)
|
|
|
|
->whereClientContactId($contact->id)
|
|
|
|
->whereQuoteId($quote->id)
|
|
|
|
->first();
|
|
|
|
|
2020-02-17 22:08:27 +01:00
|
|
|
if (!$invitation && $contact->send_email) {
|
2020-02-11 21:57:25 +01:00
|
|
|
$ii = QuoteInvitationFactory::create($quote->company_id, $quote->user_id);
|
|
|
|
$ii->quote_id = $quote->id;
|
|
|
|
$ii->client_contact_id = $contact->id;
|
|
|
|
$ii->save();
|
2020-02-17 22:08:27 +01:00
|
|
|
} elseif ($invitation && !$contact->send_email) {
|
2020-02-11 21:57:25 +01:00
|
|
|
$invitation->delete();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2020-03-06 14:41:15 +01:00
|
|
|
return $quote->fresh();
|
2020-02-11 21:57:25 +01:00
|
|
|
}
|
|
|
|
}
|