1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-14 23:22:52 +01:00
invoiceninja/app/Services/Credit/CreateInvitations.php

43 lines
1.1 KiB
PHP
Raw Normal View History

<?php
2020-02-18 21:56:21 +01:00
namespace App\Services\Credit;
use App\Factory\CreditInvitationFactory;
2020-02-18 21:56:21 +01:00
use App\Models\Credit;
use App\Models\CreditInvitation;
use App\Services\AbstractService;
class CreateInvitations extends AbstractService
{
2020-02-18 21:56:21 +01:00
private $credit;
2020-02-18 21:56:21 +01:00
public function __construct(Credit $credit)
{
2020-02-18 21:56:21 +01:00
$this->credit = $credit;
}
2020-02-18 21:56:21 +01:00
public function run()
{
2020-02-18 21:56:21 +01:00
$contacts = $this->credit->client->contacts;
2020-02-18 21:56:21 +01:00
$contacts->each(function ($contact){
$invitation = CreditInvitation::whereCompanyId($this->credit->company_id)
->whereClientContactId($contact->id)
2020-02-18 21:56:21 +01:00
->whereCreditId($this->credit->id)
->first();
if (!$invitation) {
2020-02-18 21:56:21 +01:00
$ii = CreditInvitationFactory::create($this->credit->company_id, $this->credit->user_id);
$ii->credit_id = $this->credit->id;
$ii->client_contact_id = $contact->id;
$ii->save();
} elseif ($invitation && !$contact->send_email) {
$invitation->delete();
}
});
return $this->credit;
}
}