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