1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-11 05:32:39 +01:00
invoiceninja/app/Services/Recurring/CreateRecurringInvitations.php

81 lines
2.4 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) 2020. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://opensource.org/licenses/AAL
*/
namespace App\Services\Recurring;
use App\Factory\InvoiceInvitationFactory;
2020-10-05 13:32:56 +02:00
use App\Factory\RecurringInvoiceFactory;
2020-10-04 23:55:52 +02:00
use App\Models\Invoice;
2020-10-05 13:32:56 +02:00
use App\Models\RecurringInvoice;
2020-10-04 23:55:52 +02:00
use App\Models\InvoiceInvitation;
2020-10-05 13:32:56 +02:00
use App\Models\RecurringInvoiceInvitation;
2020-10-04 23:55:52 +02:00
use App\Services\AbstractService;
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-05 23:46:47 +02:00
info($this->entity_name);
info($this->entity_id_name);
info($this->invitation_class);
info($this->invitation_factory);
try {
2020-10-04 23:55:52 +02:00
$this->entity->client->contacts->each(function ($contact) {
2020-10-05 13:32:56 +02:00
2020-10-04 23:55:52 +02:00
$invitation = $this->invitation_class::whereCompanyId($this->entity->company_id)
->whereClientContactId($contact->id)
->where($this->entity_id_name, $this->entity->id)
->withTrashed()
->first();
if (! $invitation && $contact->send_email) {
$ii = $this->invitation_factory::create($this->entity->company_id, $this->entity->user_id);
2020-10-05 13:32:56 +02:00
$ii->{$this->entity_id_name} = $this->entity->id;
2020-10-04 23:55:52 +02:00
$ii->client_contact_id = $contact->id;
$ii->save();
} elseif ($invitation && ! $contact->send_email) {
$invitation->delete();
}
2020-10-05 13:32:56 +02:00
2020-10-04 23:55:52 +02:00
});
2020-10-05 23:46:47 +02:00
}
catch(\Exception $e)
{
info($e->getMessage());
}
info("returning the entity");
2020-10-04 23:55:52 +02:00
return $this->entity;
}
2020-10-05 13:32:56 +02:00
}