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
|
|
|
|
*
|
2023-01-28 23:21:40 +01:00
|
|
|
* @copyright Copyright (c) 2023. Invoice Ninja LLC (https://invoiceninja.com)
|
2020-07-06 01:34:25 +02:00
|
|
|
*
|
2021-06-16 08:58:16 +02:00
|
|
|
* @license https://www.elastic.co/licensing/elastic-license
|
2020-07-06 01:34:25 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
namespace App\Services\Invoice;
|
|
|
|
|
2020-07-06 05:12:08 +02:00
|
|
|
use App\Events\Invoice\InvoiceWasEmailed;
|
2021-08-22 23:12:58 +02:00
|
|
|
use App\Jobs\Entity\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;
|
|
|
|
|
2022-11-13 21:53:23 +01:00
|
|
|
private bool $updated = false;
|
|
|
|
|
2020-07-06 01:34:25 +02:00
|
|
|
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') {
|
2022-11-13 21:53:23 +01:00
|
|
|
$this->invoice->service()->autoBill(); //update notification sends automatically for this.
|
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') {
|
2023-02-09 23:47:22 +01:00
|
|
|
$this->invoice = $this->invoice->service()->markPaid($this->request->input('reference'))->save(); //update notification sends automatically for this.
|
2020-07-07 14:33:11 +02:00
|
|
|
}
|
2020-07-06 05:12:08 +02:00
|
|
|
|
2022-11-13 21:53:23 +01:00
|
|
|
if ($this->request->has('mark_sent') && $this->request->input('mark_sent') == 'true' && $this->invoice->status_id == Invoice::STATUS_DRAFT) {
|
|
|
|
$this->invoice = $this->invoice->service()->markSent()->save(); //update notification NOT sent
|
2022-12-08 01:17:18 +01:00
|
|
|
$this->updated = false;
|
2022-10-02 02:00:32 +02:00
|
|
|
}
|
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
if ($this->request->has('amount_paid') && is_numeric($this->request->input('amount_paid'))) {
|
2023-02-09 23:47:22 +01:00
|
|
|
$this->invoice = $this->invoice->service()->applyPaymentAmount($this->request->input('amount_paid'), $this->request->input('reference'))->save();
|
2022-11-13 21:53:23 +01:00
|
|
|
$this->updated = false;
|
2021-08-13 03:30:48 +02:00
|
|
|
}
|
|
|
|
|
2020-09-06 11:38:10 +02:00
|
|
|
if ($this->request->has('send_email') && $this->request->input('send_email') == 'true') {
|
2022-01-07 01:54:12 +01:00
|
|
|
$this->invoice->service()->markSent()->touchPdf()->save();
|
2020-07-07 14:33:11 +02:00
|
|
|
$this->sendEmail();
|
2022-11-13 21:53:23 +01:00
|
|
|
$this->updated = false;
|
2020-07-06 05:12:08 +02:00
|
|
|
}
|
2020-07-06 06:12:04 +02:00
|
|
|
|
2022-03-30 05:12:53 +02:00
|
|
|
if ($this->request->has('cancel') && $this->request->input('cancel') == 'true') {
|
|
|
|
$this->invoice = $this->invoice->service()->handleCancellation()->save();
|
2022-11-13 21:53:23 +01:00
|
|
|
$this->updated = false;
|
2022-06-21 11:57:17 +02:00
|
|
|
}
|
2022-03-30 05:12:53 +02:00
|
|
|
|
2023-02-16 02:36:09 +01:00
|
|
|
if ($this->request->has('save_default_footer') && $this->request->input('save_default_footer') == 'true') {
|
2022-11-14 02:02:52 +01:00
|
|
|
$company = $this->invoice->company;
|
|
|
|
$settings = $company->settings;
|
|
|
|
$settings->invoice_footer = $this->invoice->footer;
|
|
|
|
$company->settings = $settings;
|
|
|
|
$company->save();
|
|
|
|
}
|
|
|
|
|
2023-02-16 02:36:09 +01:00
|
|
|
if ($this->request->has('save_default_terms') && $this->request->input('save_default_terms') == 'true') {
|
2022-11-14 02:02:52 +01:00
|
|
|
$company = $this->invoice->company;
|
|
|
|
$settings = $company->settings;
|
|
|
|
$settings->invoice_terms = $this->invoice->terms;
|
|
|
|
$company->settings = $settings;
|
|
|
|
$company->save();
|
|
|
|
}
|
|
|
|
|
2023-02-16 02:36:09 +01:00
|
|
|
if ($this->updated) {
|
2022-11-13 21:53:23 +01:00
|
|
|
event('eloquent.updated: App\Models\Invoice', $this->invoice);
|
2023-02-16 02:36:09 +01:00
|
|
|
}
|
2022-11-13 21:53:23 +01:00
|
|
|
|
|
|
|
|
2020-07-06 06:12:04 +02:00
|
|
|
return $this->invoice;
|
2020-07-06 05:12:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private function sendEmail()
|
|
|
|
{
|
2021-09-09 09:23:47 +02:00
|
|
|
$reminder_template = $this->invoice->calculateTemplate('invoice');
|
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) {
|
2021-06-13 06:19:40 +02:00
|
|
|
EmailEntity::dispatch($invitation, $this->invoice->company, $reminder_template);
|
2020-07-06 05:12:08 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
if ($this->invoice->invitations->count() > 0) {
|
2021-01-13 08:20:46 +01:00
|
|
|
event(new InvoiceWasEmailed($this->invoice->invitations->first(), $this->invoice->company, Ninja::eventVars(), 'invoice'));
|
2020-07-06 05:12:08 +02:00
|
|
|
}
|
2020-07-06 01:34:25 +02:00
|
|
|
}
|
|
|
|
}
|