2020-02-15 10:01:15 +01:00
|
|
|
<?php
|
2020-07-01 03:06:40 +02:00
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
2020-07-01 03:06:40 +02:00
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
2021-01-03 22:54:54 +01:00
|
|
|
* @copyright Copyright (c) 2021. Invoice Ninja LLC (https://invoiceninja.com)
|
2020-07-01 03:06:40 +02:00
|
|
|
*
|
|
|
|
* @license https://opensource.org/licenses/AAL
|
|
|
|
*/
|
|
|
|
|
2020-02-15 10:01:15 +01:00
|
|
|
namespace App\Services\Credit;
|
|
|
|
|
2021-01-14 00:00:32 +01:00
|
|
|
use App\Factory\ClientContactFactory;
|
2020-02-15 10:01:15 +01:00
|
|
|
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
|
|
|
|
2021-01-14 00:00:32 +01:00
|
|
|
if($contacts->count() == 0){
|
|
|
|
$this->createBlankContact();
|
|
|
|
|
|
|
|
$this->credit->refresh();
|
|
|
|
$contacts = $this->credit->client->contacts;
|
|
|
|
}
|
|
|
|
|
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();
|
|
|
|
|
2020-09-06 11:38:10 +02:00
|
|
|
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-11-03 14:27:41 +01:00
|
|
|
} elseif (! $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
|
|
|
}
|
2021-01-14 00:00:32 +01:00
|
|
|
|
|
|
|
private function createBlankContact()
|
|
|
|
{
|
|
|
|
$new_contact = ClientContactFactory::create($this->credit->company_id, $this->credit->user_id);
|
|
|
|
$new_contact->client_id = $this->credit->client_id;
|
|
|
|
$new_contact->contact_key = Str::random(40);
|
|
|
|
$new_contact->is_primary = true;
|
|
|
|
$new_contact->save();
|
|
|
|
}
|
2020-02-15 10:01:15 +01:00
|
|
|
}
|