1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-16 16:13:20 +01:00
invoiceninja/app/Services/Invoice/CreateInvitations.php

83 lines
2.5 KiB
PHP
Raw Normal View History

<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2021. Invoice Ninja LLC (https://invoiceninja.com)
*
2021-06-16 08:58:16 +02:00
* @license https://www.elastic.co/licensing/elastic-license
*/
namespace App\Services\Invoice;
2021-01-14 00:00:32 +01:00
use App\Factory\ClientContactFactory;
use App\Factory\InvoiceInvitationFactory;
use App\Models\Invoice;
use App\Models\InvoiceInvitation;
use App\Services\AbstractService;
2021-05-28 03:02:43 +02:00
use Illuminate\Support\Str;
class CreateInvitations extends AbstractService
{
private $invoice;
public function __construct(Invoice $invoice)
{
$this->invoice = $invoice;
}
public function run()
{
2021-01-14 00:00:32 +01:00
$contacts = $this->invoice->client->contacts;
if($contacts->count() == 0){
$this->createBlankContact();
$this->invoice->refresh();
$contacts = $this->invoice->client->contacts;
}
$contacts->each(function ($contact) {
$invitation = InvoiceInvitation::whereCompanyId($this->invoice->company_id)
->whereClientContactId($contact->id)
->whereInvoiceId($this->invoice->id)
2020-07-25 01:02:32 +02:00
->withTrashed()
->first();
if (! $invitation && $contact->send_email) {
$ii = InvoiceInvitationFactory::create($this->invoice->company_id, $this->invoice->user_id);
$ii->invoice_id = $this->invoice->id;
$ii->client_contact_id = $contact->id;
$ii->save();
} elseif ($invitation && ! $contact->send_email) {
$invitation->delete();
}
});
2021-04-30 01:01:56 +02:00
if($this->invoice->invitations()->count() == 0) {
$contact = $this->createBlankContact();
$ii = InvoiceInvitationFactory::create($this->invoice->company_id, $this->invoice->user_id);
$ii->invoice_id = $this->invoice->id;
$ii->client_contact_id = $contact->id;
$ii->save();
}
return $this->invoice;
}
2021-01-14 00:00:32 +01:00
private function createBlankContact()
{
$new_contact = ClientContactFactory::create($this->invoice->company_id, $this->invoice->user_id);
$new_contact->client_id = $this->invoice->client_id;
$new_contact->contact_key = Str::random(40);
$new_contact->is_primary = true;
$new_contact->save();
2021-04-30 01:01:56 +02:00
return $new_contact;
2021-01-14 00:00:32 +01:00
}
}