2020-07-06 01:34:25 +02:00
|
|
|
<?php
|
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
2020-07-06 01:34:25 +02: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-07-06 01:34:25 +02:00
|
|
|
*
|
|
|
|
* @license https://opensource.org/licenses/AAL
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace App\Services\Invoice;
|
|
|
|
|
2020-07-06 05:12:08 +02:00
|
|
|
use App\Events\Invoice\InvoiceWasEmailed;
|
2020-10-28 07:58:15 +01:00
|
|
|
use App\Jobs\Invoice\EmailEntity;
|
2020-07-06 01:34:25 +02:00
|
|
|
use App\Models\Invoice;
|
|
|
|
use App\Services\AbstractService;
|
2020-07-08 14:02:16 +02:00
|
|
|
use App\Utils\Ninja;
|
2020-07-06 01:34:25 +02:00
|
|
|
use App\Utils\Traits\GeneratesCounter;
|
|
|
|
use Illuminate\Http\Request;
|
|
|
|
|
|
|
|
class TriggeredActions extends AbstractService
|
|
|
|
{
|
|
|
|
use GeneratesCounter;
|
|
|
|
|
|
|
|
private $request;
|
|
|
|
|
|
|
|
private $invoice;
|
|
|
|
|
|
|
|
public function __construct(Invoice $invoice, Request $request)
|
|
|
|
{
|
|
|
|
$this->request = $request;
|
2020-09-06 11:38:10 +02:00
|
|
|
|
2020-07-06 01:34:25 +02:00
|
|
|
$this->invoice = $invoice;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function run()
|
|
|
|
{
|
2020-09-06 11:38:10 +02:00
|
|
|
if ($this->request->has('auto_bill') && $this->request->input('auto_bill') == 'true') {
|
2020-07-15 08:08:57 +02:00
|
|
|
$this->invoice = $this->invoice->service()->autoBill()->save();
|
2020-07-06 05:12:08 +02:00
|
|
|
}
|
2020-09-06 11:38:10 +02:00
|
|
|
|
|
|
|
if ($this->request->has('paid') && $this->request->input('paid') == 'true') {
|
2020-07-15 08:08:57 +02:00
|
|
|
$this->invoice = $this->invoice->service()->markPaid()->save();
|
2020-07-07 14:33:11 +02:00
|
|
|
}
|
2020-07-06 05:12:08 +02:00
|
|
|
|
2020-09-06 11:38:10 +02:00
|
|
|
if ($this->request->has('send_email') && $this->request->input('send_email') == 'true') {
|
2020-07-07 14:33:11 +02:00
|
|
|
$this->sendEmail();
|
2020-07-06 05:12:08 +02:00
|
|
|
}
|
2020-07-06 06:12:04 +02:00
|
|
|
|
2020-09-06 11:38:10 +02:00
|
|
|
if ($this->request->has('mark_sent') && $this->request->input('mark_sent') == 'true') {
|
2020-08-30 12:47:32 +02:00
|
|
|
$this->invoice = $this->invoice->service()->markSent()->save();
|
|
|
|
}
|
|
|
|
|
2020-07-06 06:12:04 +02:00
|
|
|
return $this->invoice;
|
2020-07-06 05:12:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private function sendEmail()
|
|
|
|
{
|
|
|
|
|
2020-10-27 12:57:12 +01:00
|
|
|
//$reminder_template = $this->invoice->calculateTemplate('invoice');
|
2020-07-15 09:03:35 +02:00
|
|
|
$reminder_template = 'payment';
|
2020-07-06 05:12:08 +02:00
|
|
|
|
2020-09-06 11:38:10 +02:00
|
|
|
$this->invoice->invitations->load('contact.client.country', 'invoice.client.country', 'invoice.company')->each(function ($invitation) use ($reminder_template) {
|
2020-11-05 11:14:30 +01:00
|
|
|
EmailEntity::dispatch($invitation, $this->invoice->company, $reminder_template);
|
2020-07-06 05:12:08 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
if ($this->invoice->invitations->count() > 0) {
|
2020-07-08 14:02:16 +02:00
|
|
|
event(new InvoiceWasEmailed($this->invoice->invitations->first(), $this->invoice->company, Ninja::eventVars()));
|
2020-07-06 05:12:08 +02:00
|
|
|
}
|
2020-07-06 01:34:25 +02:00
|
|
|
}
|
|
|
|
}
|