2020-02-04 08:51:44 +01:00
|
|
|
<?php
|
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
2020-02-04 08:51:44 +01:00
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
2021-01-03 22:54:54 +01:00
|
|
|
* @copyright Copyright (c) 2021. Invoice Ninja LLC (https://invoiceninja.com)
|
2020-02-04 08:51:44 +01:00
|
|
|
*
|
|
|
|
* @license https://opensource.org/licenses/AAL
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace App\Services\Invoice;
|
|
|
|
|
2021-01-14 00:00:32 +01:00
|
|
|
use App\Factory\ClientContactFactory;
|
2020-02-04 08:51:44 +01:00
|
|
|
use App\Factory\InvoiceInvitationFactory;
|
|
|
|
use App\Models\Invoice;
|
|
|
|
use App\Models\InvoiceInvitation;
|
2020-02-17 10:37:44 +01:00
|
|
|
use App\Services\AbstractService;
|
2020-02-04 08:51:44 +01:00
|
|
|
|
2020-02-17 10:37:44 +01:00
|
|
|
class CreateInvitations extends AbstractService
|
2020-02-04 08:51:44 +01:00
|
|
|
{
|
2020-02-17 10:37:44 +01:00
|
|
|
private $invoice;
|
|
|
|
|
|
|
|
public function __construct(Invoice $invoice)
|
2020-02-04 08:51:44 +01:00
|
|
|
{
|
2020-02-17 10:37:44 +01:00
|
|
|
$this->invoice = $invoice;
|
2020-02-04 08:51:44 +01:00
|
|
|
}
|
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
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) {
|
2020-02-17 10:37:44 +01:00
|
|
|
$invitation = InvoiceInvitation::whereCompanyId($this->invoice->company_id)
|
2020-02-04 08:51:44 +01:00
|
|
|
->whereClientContactId($contact->id)
|
2020-02-17 10:37:44 +01:00
|
|
|
->whereInvoiceId($this->invoice->id)
|
2020-07-25 01:02:32 +02:00
|
|
|
->withTrashed()
|
2020-02-04 08:51:44 +01:00
|
|
|
->first();
|
|
|
|
|
2020-09-06 11:38:10 +02:00
|
|
|
if (! $invitation && $contact->send_email) {
|
2020-02-17 10:37:44 +01:00
|
|
|
$ii = InvoiceInvitationFactory::create($this->invoice->company_id, $this->invoice->user_id);
|
|
|
|
$ii->invoice_id = $this->invoice->id;
|
2020-02-04 08:51:44 +01:00
|
|
|
$ii->client_contact_id = $contact->id;
|
|
|
|
$ii->save();
|
2020-09-06 11:38:10 +02:00
|
|
|
} elseif ($invitation && ! $contact->send_email) {
|
2020-02-04 08:51:44 +01:00
|
|
|
$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();
|
|
|
|
}
|
|
|
|
|
2020-02-17 10:37:44 +01:00
|
|
|
return $this->invoice;
|
2020-03-21 06:37:30 +01:00
|
|
|
}
|
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
|
|
|
}
|
2020-02-14 04:32:22 +01:00
|
|
|
}
|