1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-10 21:22:58 +01:00
invoiceninja/app/Services/Credit/CreateInvitations.php

100 lines
3.1 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
*
2022-04-27 05:20:41 +02:00
* @copyright Copyright (c) 2022. Invoice Ninja LLC (https://invoiceninja.com)
2020-07-01 03:06:40 +02:00
*
2021-06-16 08:58:16 +02:00
* @license https://www.elastic.co/licensing/elastic-license
2020-07-01 03:06:40 +02:00
*/
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;
2021-10-14 07:25:09 +02:00
use App\Utils\Traits\MakesHash;
use Illuminate\Support\Str;
class CreateInvitations extends AbstractService
{
2021-10-14 07:25:09 +02:00
use MakesHash;
2021-11-09 11:15:08 +01:00
public $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;
if ($contacts->count() == 0) {
2021-01-14 00:00:32 +01:00
$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)
2021-11-09 11:15:08 +01:00
->withTrashed()
->first();
if (! $invitation) {
2020-02-18 21:56:21 +01:00
$ii = CreditInvitationFactory::create($this->credit->company_id, $this->credit->user_id);
2021-11-06 01:46:12 +01:00
$ii->key = $this->createDbHash($this->credit->company->db);
2020-02-18 21:56:21 +01:00
$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();
}
});
if ($this->credit->invitations()->count() == 0) {
if ($contacts->count() == 0) {
2021-11-09 11:15:08 +01:00
$contact = $this->createBlankContact();
} else {
2021-11-09 11:15:08 +01:00
$contact = $contacts->first();
$invitation = CreditInvitation::where('company_id', $this->credit->company_id)
2021-11-09 11:15:08 +01:00
->where('client_contact_id', $contact->id)
->where('credit_id', $this->credit->id)
->withTrashed()
->first();
if ($invitation) {
$invitation->restore();
return $this->credit;
}
2021-11-09 11:15:08 +01:00
}
$ii = CreditInvitationFactory::create($this->credit->company_id, $this->credit->user_id);
$ii->key = $this->createDbHash($this->credit->company->db);
$ii->credit_id = $this->credit->id;
$ii->client_contact_id = $contact->id;
$ii->save();
2021-11-09 11:15:08 +01:00
}
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();
}
}