2020-02-04 08:51:44 +01: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\Invoice;
|
|
|
|
|
|
|
|
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()
|
|
|
|
{
|
|
|
|
$this->invoice->client->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-02-04 08:51:44 +01:00
|
|
|
->first();
|
|
|
|
|
2020-02-17 22:08:27 +01: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-02-17 22:08:27 +01:00
|
|
|
} elseif ($invitation && !$contact->send_email) {
|
2020-02-04 08:51:44 +01:00
|
|
|
$invitation->delete();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2020-02-17 10:37:44 +01:00
|
|
|
return $this->invoice;
|
2020-03-21 06:37:30 +01:00
|
|
|
}
|
2020-02-14 04:32:22 +01:00
|
|
|
}
|