1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-14 07:02:34 +01:00
invoiceninja/app/Services/Recurring/CreateRecurringInvitations.php

81 lines
2.7 KiB
PHP
Raw Normal View History

2020-10-04 23:55:52 +02:00
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
2023-01-28 23:21:40 +01:00
* @copyright Copyright (c) 2023. Invoice Ninja LLC (https://invoiceninja.com)
2020-10-04 23:55:52 +02:00
*
2021-06-16 08:58:16 +02:00
* @license https://www.elastic.co/licensing/elastic-license
2020-10-04 23:55:52 +02:00
*/
namespace App\Services\Recurring;
use App\Services\AbstractService;
2021-10-14 07:25:09 +02:00
use App\Utils\Traits\MakesHash;
2020-10-28 11:10:49 +01:00
use Exception;
2020-10-04 23:55:52 +02:00
use Illuminate\Support\Str;
class CreateRecurringInvitations extends AbstractService
{
2021-10-14 07:25:09 +02:00
use MakesHash;
2020-10-04 23:55:52 +02:00
private $entity;
private $entity_name;
private $entity_id_name;
private $invitation_class;
private $invitation_factory;
public function __construct($entity)
{
$this->entity = $entity;
$this->entity_name = lcfirst(Str::snake(class_basename($entity)));
$this->entity_id_name = $this->entity_name.'_id';
$this->invitation_class = 'App\Models\\'.ucfirst(Str::camel($this->entity_name)).'Invitation';
$this->invitation_factory = 'App\Factory\\'.ucfirst(Str::camel($this->entity_name)).'InvitationFactory';
2020-10-04 23:55:52 +02:00
}
public function run()
{
2020-10-06 02:52:16 +02:00
try {
$this->entity->client->contacts->each(function ($contact) {
2023-08-06 09:35:19 +02:00
$invitation = $this->invitation_class::query()->whereCompanyId($this->entity->company_id)
2020-10-06 02:52:16 +02:00
->whereClientContactId($contact->id)
->where($this->entity_id_name, $this->entity->id)
->withTrashed()
->first();
2020-10-04 23:55:52 +02:00
2020-10-06 02:52:16 +02:00
if (! $invitation && $contact->send_email) {
$ii = $this->invitation_factory::create($this->entity->company_id, $this->entity->user_id);
2021-11-06 01:46:12 +01:00
$ii->key = $this->createDbHash($this->entity->company->db);
2020-10-06 02:52:16 +02:00
$ii->{$this->entity_id_name} = $this->entity->id;
$ii->client_contact_id = $contact->id;
$ii->save();
} elseif ($invitation && ! $contact->send_email) {
$invitation->delete();
}
});
2020-11-25 15:19:52 +01:00
} catch (Exception $e) {
2020-10-06 02:52:16 +02:00
info($e->getMessage());
}
2020-10-04 23:55:52 +02:00
if ($this->entity->invitations()->count() == 0) {
2023-08-06 09:35:19 +02:00
$invitation = $this->invitation_class::query()->where('company_id', $this->entity->company_id)
->where($this->entity_id_name, $this->entity->id)
->withTrashed()
->first();
2023-02-16 02:36:09 +01:00
if ($invitation) {
$invitation->restore();
2023-02-16 02:36:09 +01:00
}
}
2020-10-04 23:55:52 +02:00
return $this->entity;
}
2020-10-28 11:10:49 +01:00
}