2019-12-17 23:40:15 +01:00
|
|
|
<?php
|
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* Quote Ninja (https://invoiceninja.com).
|
2019-12-17 23:40:15 +01:00
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
2021-01-03 22:54:54 +01:00
|
|
|
* @copyright Copyright (c) 2021. Quote Ninja LLC (https://invoiceninja.com)
|
2019-12-17 23:40:15 +01:00
|
|
|
*
|
2021-06-16 08:58:16 +02:00
|
|
|
* @license https://www.elastic.co/licensing/elastic-license
|
2019-12-17 23:40:15 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
namespace App\Jobs\Quote;
|
|
|
|
|
|
|
|
use App\Factory\QuoteInvitationFactory;
|
2019-12-27 01:28:36 +01:00
|
|
|
use App\Libraries\MultiDB;
|
|
|
|
use App\Models\Company;
|
2019-12-17 23:40:15 +01:00
|
|
|
use App\Models\Quote;
|
|
|
|
use App\Models\QuoteInvitation;
|
|
|
|
use Illuminate\Bus\Queueable;
|
|
|
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
|
|
|
use Illuminate\Foundation\Bus\Dispatchable;
|
|
|
|
use Illuminate\Queue\InteractsWithQueue;
|
|
|
|
use Illuminate\Queue\SerializesModels;
|
|
|
|
|
|
|
|
class CreateQuoteInvitations implements ShouldQueue
|
|
|
|
{
|
|
|
|
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
|
|
|
|
|
|
|
private $quote;
|
|
|
|
|
2019-12-27 01:28:36 +01:00
|
|
|
private $company;
|
|
|
|
|
2019-12-17 23:40:15 +01:00
|
|
|
/**
|
|
|
|
* Create a new job instance.
|
|
|
|
*
|
2020-10-28 11:10:49 +01:00
|
|
|
* @param Quote $quote
|
|
|
|
* @param Company $company
|
2019-12-17 23:40:15 +01:00
|
|
|
*/
|
2019-12-27 01:28:36 +01:00
|
|
|
public function __construct(Quote $quote, Company $company)
|
2019-12-17 23:40:15 +01:00
|
|
|
{
|
|
|
|
$this->quote = $quote;
|
|
|
|
|
2019-12-27 01:28:36 +01:00
|
|
|
$this->company = $company;
|
2019-12-17 23:40:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function handle()
|
|
|
|
{
|
2019-12-27 01:28:36 +01:00
|
|
|
MultiDB::setDB($this->company->db);
|
|
|
|
|
2019-12-17 23:40:15 +01:00
|
|
|
$contacts = $this->quote->client->contacts;
|
|
|
|
|
|
|
|
$contacts->each(function ($contact) {
|
|
|
|
$invitation = QuoteInvitation::whereCompanyId($this->quote->company_id)
|
|
|
|
->whereClientContactId($contact->id)
|
|
|
|
->whereQuoteId($this->quote->id)
|
|
|
|
->first();
|
|
|
|
|
2020-09-06 11:38:10 +02:00
|
|
|
if (! $invitation && $contact->send) {
|
2019-12-17 23:40:15 +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();
|
2020-09-06 11:38:10 +02:00
|
|
|
} elseif ($invitation && ! $contact->send) {
|
2019-12-17 23:40:15 +01:00
|
|
|
$invitation->delete();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2019-12-30 22:59:12 +01:00
|
|
|
}
|