2020-02-15 09:01:15 +00:00
|
|
|
<?php
|
|
|
|
|
namespace App\Services\Credit;
|
|
|
|
|
|
|
|
|
|
use App\Factory\CreditInvitationFactory;
|
|
|
|
|
use App\Models\CreditInvitation;
|
2020-02-17 20:37:44 +11:00
|
|
|
use App\Services\AbstractService;
|
2020-02-15 09:01:15 +00:00
|
|
|
|
2020-02-17 20:37:44 +11:00
|
|
|
class CreateInvitations extends AbstractService
|
2020-02-15 09:01:15 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
|
|
public function __construct()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-17 20:37:44 +11:00
|
|
|
public function run($credit)
|
2020-02-15 09:01:15 +00:00
|
|
|
{
|
|
|
|
|
|
2020-02-17 20:37:44 +11:00
|
|
|
$contacts = $credit->client->contacts;
|
2020-02-15 09:01:15 +00:00
|
|
|
|
|
|
|
|
$contacts->each(function ($contact) use($credit){
|
|
|
|
|
$invitation = CreditInvitation::whereCompanyId($credit->account_id)
|
|
|
|
|
->whereClientContactId($contact->id)
|
|
|
|
|
->whereCreditId($credit->id)
|
|
|
|
|
->first();
|
|
|
|
|
|
|
|
|
|
if (!$invitation) {
|
2020-02-17 20:37:44 +11:00
|
|
|
$ii = CreditInvitationFactory::create($credit->company_id, $credit->user_id);
|
2020-02-15 09:01:15 +00:00
|
|
|
$ii->credit_id = $credit->id;
|
|
|
|
|
$ii->client_contact_id = $contact->id;
|
|
|
|
|
$ii->save();
|
2020-02-17 20:37:44 +11:00
|
|
|
} elseif ($invitation && !$contact->send_email) {
|
2020-02-15 09:01:15 +00:00
|
|
|
$invitation->delete();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return $credit;
|
|
|
|
|
}
|
|
|
|
|
}
|