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

65 lines
2.1 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
*
* @copyright Copyright (c) 2021. Invoice Ninja LLC (https://invoiceninja.com)
2020-10-04 23:55:52 +02:00
*
* @license https://opensource.org/licenses/AAL
*/
namespace App\Services\Recurring;
use App\Services\AbstractService;
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
{
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";
2020-10-05 13:32:56 +02:00
$this->invitation_class = 'App\Models\\' . ucfirst(Str::camel($this->entity_name)) . "Invitation";
2020-10-05 23:46:47 +02:00
$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) {
$invitation = $this->invitation_class::whereCompanyId($this->entity->company_id)
->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);
$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
return $this->entity;
}
2020-10-28 11:10:49 +01:00
}