2019-05-09 07:29:31 +02:00
|
|
|
<?php
|
2019-05-11 05:32:07 +02:00
|
|
|
/**
|
|
|
|
* 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-05-11 05:32:07 +02:00
|
|
|
*
|
|
|
|
* @license https://opensource.org/licenses/AAL
|
|
|
|
*/
|
2019-05-09 07:29:31 +02:00
|
|
|
|
|
|
|
namespace App\Jobs\Invoice;
|
|
|
|
|
2019-05-09 23:50:21 +02:00
|
|
|
use App\Jobs\Invoice\InvoiceNotification;
|
|
|
|
use App\Jobs\Payment\PaymentNotification;
|
2019-12-27 01:28:36 +01:00
|
|
|
use App\Libraries\MultiDB;
|
|
|
|
use App\Models\Company;
|
2019-05-09 23:50:21 +02:00
|
|
|
use App\Models\Invoice;
|
2019-05-09 07:29:31 +02:00
|
|
|
use App\Repositories\InvoiceRepository;
|
|
|
|
use Illuminate\Bus\Queueable;
|
|
|
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
|
|
|
use Illuminate\Foundation\Bus\Dispatchable;
|
|
|
|
use Illuminate\Queue\InteractsWithQueue;
|
|
|
|
use Illuminate\Queue\SerializesModels;
|
|
|
|
|
|
|
|
class StoreInvoice implements ShouldQueue
|
|
|
|
{
|
|
|
|
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
|
|
|
|
|
|
|
public $invoice;
|
|
|
|
|
|
|
|
protected $data;
|
|
|
|
|
2019-12-27 01:28:36 +01:00
|
|
|
private $company;
|
2019-05-09 07:29:31 +02:00
|
|
|
/**
|
|
|
|
* Create a new job instance.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2019-12-27 01:28:36 +01:00
|
|
|
public function __construct(Invoice $invoice, array $data, Company $company)
|
2019-05-09 07:29:31 +02:00
|
|
|
{
|
|
|
|
$this->invoice = $invoice;
|
|
|
|
|
|
|
|
$this->data = $data;
|
|
|
|
|
2019-12-27 01:28:36 +01:00
|
|
|
$this->company = $company;
|
2019-05-09 07:29:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Execute the job.
|
|
|
|
*
|
|
|
|
* We expect the Invoice object along with
|
|
|
|
* the request in array form
|
|
|
|
*
|
2019-12-27 20:30:22 +01:00
|
|
|
* Embedded in the request may be additional
|
|
|
|
* attributes which require additional work to be
|
2019-05-09 07:29:31 +02:00
|
|
|
* done in this job, these include - but are not limited to:
|
|
|
|
*
|
|
|
|
* 1. email_invoice - Email the Invoice
|
|
|
|
* 2. mark_paid - Mark the invoice as paid (Generates a payment against the invoice)
|
|
|
|
* 3. ......
|
2019-12-27 20:30:22 +01:00
|
|
|
*
|
2019-05-09 07:29:31 +02:00
|
|
|
* @return NULL|Invoice
|
|
|
|
*/
|
|
|
|
public function handle(InvoiceRepository $invoice_repo) : ?Invoice
|
|
|
|
{
|
|
|
|
$payment = false;
|
|
|
|
|
2019-11-10 13:06:30 +01:00
|
|
|
// /* Test if we should auto-bill the invoice */
|
|
|
|
// if(property_exists($this->invoice->client->getSetting('auto_bill')) && (bool)$this->invoice->client->getSetting('auto_bill'))
|
|
|
|
// {
|
2019-05-09 07:29:31 +02:00
|
|
|
|
2019-11-10 13:06:30 +01:00
|
|
|
// $this->invoice = $invoice_repo->markSent($this->invoice);
|
2019-05-09 07:29:31 +02:00
|
|
|
|
2019-11-10 13:06:30 +01:00
|
|
|
// //fire autobill - todo - the PAYMENT class will update the INVOICE status.
|
2019-12-27 20:30:22 +01:00
|
|
|
// // $payment =
|
|
|
|
|
2019-11-10 13:06:30 +01:00
|
|
|
// }
|
2019-05-09 07:29:31 +02:00
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
if (isset($this->data['email_invoice']) && (bool)$this->data['email_invoice']) {
|
2019-05-09 07:29:31 +02:00
|
|
|
$this->invoice = $invoice_repo->markSent($this->invoice);
|
|
|
|
|
|
|
|
//fire invoice job (the job performs the filtering logic of the email recipients... if any.)
|
2019-12-27 01:28:36 +01:00
|
|
|
InvoiceNotification::dispatch($invoice, $invoice->company);
|
2019-05-09 07:29:31 +02:00
|
|
|
}
|
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
if (isset($this->data['mark_paid']) && (bool)$this->data['mark_paid']) {
|
2019-05-09 07:29:31 +02:00
|
|
|
$this->invoice = $invoice_repo->markSent($this->invoice);
|
|
|
|
|
|
|
|
// generate a manual payment against the invoice
|
|
|
|
// the PAYMENT class will update the INVOICE status.
|
2019-12-27 20:30:22 +01:00
|
|
|
//$payment =
|
2019-05-09 07:29:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Payment Notifications */
|
2019-12-30 22:59:12 +01:00
|
|
|
if ($payment) {
|
2019-05-09 07:29:31 +02:00
|
|
|
//fire payment notifications here
|
2019-12-27 01:28:36 +01:00
|
|
|
PaymentNotification::dispatch($payment, $payment->company);
|
2019-05-09 07:29:31 +02:00
|
|
|
}
|
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
if (isset($data['download_invoice']) && (bool)$this->data['download_invoice']) {
|
2019-05-09 07:29:31 +02:00
|
|
|
//fire invoice download and return PDF response from here
|
|
|
|
}
|
|
|
|
|
2019-05-16 00:26:21 +02:00
|
|
|
return $this->invoice;
|
2019-05-09 07:29:31 +02:00
|
|
|
}
|
|
|
|
}
|