1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-06 11:12:43 +01:00
invoiceninja/app/Services/Credit/CreateInvitations.php

68 lines
1.9 KiB
PHP
Raw Normal View History

<?php
2020-07-01 03:06:40 +02:00
/**
* Invoice Ninja (https://invoiceninja.com).
2020-07-01 03:06:40 +02:00
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2021. Invoice Ninja LLC (https://invoiceninja.com)
2020-07-01 03:06:40 +02:00
*
* @license https://opensource.org/licenses/AAL
*/
namespace App\Services\Credit;
2021-01-14 00:00:32 +01:00
use App\Factory\ClientContactFactory;
use App\Factory\CreditInvitationFactory;
2020-02-18 21:56:21 +01:00
use App\Models\Credit;
use App\Models\CreditInvitation;
use App\Services\AbstractService;
class CreateInvitations extends AbstractService
{
2020-02-18 21:56:21 +01:00
private $credit;
2020-02-18 21:56:21 +01:00
public function __construct(Credit $credit)
{
2020-02-18 21:56:21 +01:00
$this->credit = $credit;
}
2020-02-18 21:56:21 +01:00
public function run()
{
2020-02-18 21:56:21 +01:00
$contacts = $this->credit->client->contacts;
2021-01-14 00:00:32 +01:00
if($contacts->count() == 0){
$this->createBlankContact();
$this->credit->refresh();
$contacts = $this->credit->client->contacts;
}
$contacts->each(function ($contact) {
2020-02-18 21:56:21 +01:00
$invitation = CreditInvitation::whereCompanyId($this->credit->company_id)
->whereClientContactId($contact->id)
2020-02-18 21:56:21 +01:00
->whereCreditId($this->credit->id)
->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;
$ii->client_contact_id = $contact->id;
$ii->save();
2020-11-03 14:27:41 +01:00
} elseif (! $contact->send_email) {
$invitation->delete();
}
});
return $this->credit;
}
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();
}
}