2019-11-16 04:12:29 +01:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Invoice Ninja (https://invoiceninja.com)
|
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
2020-01-07 01:13:47 +01:00
|
|
|
* @copyright Copyright (c) 2020. Invoice Ninja LLC (https://invoiceninja.com)
|
2019-11-16 04:12:29 +01:00
|
|
|
*
|
|
|
|
* @license https://opensource.org/licenses/AAL
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace App\Jobs\Invoice;
|
|
|
|
|
|
|
|
use App\Factory\InvoiceInvitationFactory;
|
2019-12-27 01:28:36 +01:00
|
|
|
use App\Libraries\MultiDB;
|
|
|
|
use App\Models\Company;
|
2019-11-16 04:12:29 +01:00
|
|
|
use App\Models\Invoice;
|
|
|
|
use App\Models\InvoiceInvitation;
|
|
|
|
use Illuminate\Bus\Queueable;
|
|
|
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
|
|
|
use Illuminate\Foundation\Bus\Dispatchable;
|
|
|
|
use Illuminate\Queue\InteractsWithQueue;
|
|
|
|
use Illuminate\Queue\SerializesModels;
|
|
|
|
use Symfony\Component\Debug\Exception\FatalThrowableError;
|
|
|
|
|
|
|
|
class CreateInvoiceInvitations implements ShouldQueue
|
|
|
|
{
|
|
|
|
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
|
|
|
|
2019-12-17 23:40:15 +01:00
|
|
|
private $invoice;
|
2019-11-16 04:12:29 +01:00
|
|
|
|
2019-12-27 01:28:36 +01:00
|
|
|
private $company;
|
|
|
|
|
2019-11-16 04:12:29 +01:00
|
|
|
/**
|
|
|
|
* Create a new job instance.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2019-12-27 01:28:36 +01:00
|
|
|
public function __construct(Invoice $invoice, Company $company)
|
2019-11-16 04:12:29 +01:00
|
|
|
{
|
|
|
|
$this->invoice = $invoice;
|
2019-12-27 01:28:36 +01:00
|
|
|
$this->company = $company;
|
2019-11-16 04:12:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function handle()
|
|
|
|
{
|
2019-12-27 01:28:36 +01:00
|
|
|
MultiDB::setDB($this->company->db);
|
|
|
|
|
2019-11-16 04:12:29 +01:00
|
|
|
$contacts = $this->invoice->client->contacts;
|
|
|
|
|
|
|
|
$contacts->each(function ($contact) {
|
|
|
|
$invitation = InvoiceInvitation::whereCompanyId($this->invoice->company_id)
|
|
|
|
->whereClientContactId($contact->id)
|
|
|
|
->whereInvoiceId($this->invoice->id)
|
|
|
|
->first();
|
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
if (!$invitation && $contact->send_invoice) {
|
2019-11-16 04:12:29 +01:00
|
|
|
$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();
|
2019-12-30 22:59:12 +01:00
|
|
|
} elseif ($invitation && !$contact->send_invoice) {
|
2019-11-16 04:12:29 +01:00
|
|
|
$invitation->delete();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2019-12-30 22:59:12 +01:00
|
|
|
}
|