From 08b317f7b898550e69642f054a36cc886cdfa689 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benjamin=20Beganovi=C4=87?= Date: Thu, 1 Apr 2021 16:38:46 +0200 Subject: [PATCH] allow cancellation under specific conditions --- .../RecurringInvoiceController.php | 33 +++++++-------- app/Models/RecurringInvoice.php | 15 ++++--- .../recurring_invoices/show.blade.php | 40 ++++++++++--------- 3 files changed, 49 insertions(+), 39 deletions(-) diff --git a/app/Http/Controllers/ClientPortal/RecurringInvoiceController.php b/app/Http/Controllers/ClientPortal/RecurringInvoiceController.php index e484dedeba..5d8043c767 100644 --- a/app/Http/Controllers/ClientPortal/RecurringInvoiceController.php +++ b/app/Http/Controllers/ClientPortal/RecurringInvoiceController.php @@ -61,27 +61,28 @@ class RecurringInvoiceController extends Controller public function requestCancellation(Request $request, RecurringInvoice $recurring_invoice) { - //todo double check the user is able to request a cancellation - //can add locale specific by chaining ->locale(); - - $nmo = new NinjaMailerObject; - $nmo->mailable = (new NinjaMailer((new ClientContactRequestCancellationObject($recurring_invoice, auth()->user()))->build())); - $nmo->company = $recurring_invoice->company; - $nmo->settings = $recurring_invoice->company->settings; + if (is_null($recurring_invoice->subscription_id) || optional($recurring_invoice->subscription)->allow_cancellation) { + $nmo = new NinjaMailerObject; + $nmo->mailable = (new NinjaMailer((new ClientContactRequestCancellationObject($recurring_invoice, auth()->user()))->build())); + $nmo->company = $recurring_invoice->company; + $nmo->settings = $recurring_invoice->company->settings; - $notifiable_users = $this->filterUsersByPermissions($recurring_invoice->company->company_users, $recurring_invoice, ['recurring_cancellation']); + $notifiable_users = $this->filterUsersByPermissions($recurring_invoice->company->company_users, $recurring_invoice, ['recurring_cancellation']); - $notifiable_users->each(function ($company_user) use($nmo){ + $notifiable_users->each(function ($company_user) use($nmo){ - $nmo->to_user = $company_user->user; - NinjaMailerJob::dispatch($nmo); + $nmo->to_user = $company_user->user; + NinjaMailerJob::dispatch($nmo); - }); + }); - //$recurring_invoice->user->notify(new ClientContactRequestCancellation($recurring_invoice, auth()->user())); + //$recurring_invoice->user->notify(new ClientContactRequestCancellation($recurring_invoice, auth()->user())); - return $this->render('recurring_invoices.cancellation.index', [ - 'invoice' => $recurring_invoice, - ]); + return $this->render('recurring_invoices.cancellation.index', [ + 'invoice' => $recurring_invoice, + ]); + } + + return back(); } } diff --git a/app/Models/RecurringInvoice.php b/app/Models/RecurringInvoice.php index 6bba8fc624..6727e0285c 100644 --- a/app/Models/RecurringInvoice.php +++ b/app/Models/RecurringInvoice.php @@ -196,7 +196,7 @@ class RecurringInvoice extends BaseModel { return $this->morphMany(Document::class, 'documentable'); } - + public function getStatusAttribute() { if ($this->status_id == self::STATUS_ACTIVE && Carbon::parse($this->next_send_date)->isFuture()) { @@ -394,9 +394,9 @@ class RecurringInvoice extends BaseModel if ($this->remaining_cycles == -1) { $iterations = 10; } - + $data = []; - + if (!Carbon::parse($this->next_send_date)) { return $data; } @@ -407,7 +407,7 @@ class RecurringInvoice extends BaseModel // we don't add the days... we calc the day of the month!! $next_due_date = $this->calculateDueDate($next_send_date->copy()->format('Y-m-d')); $next_due_date_string = $next_due_date ? $next_due_date->format('Y-m-d') : ''; - + $next_send_date = Carbon::parse($next_send_date); $data[] = [ @@ -420,7 +420,7 @@ class RecurringInvoice extends BaseModel /*If no due date is set - unset the due_date value */ // if(!$this->due_date_days || $this->due_date_days == 0){ - + // foreach($data as $key => $value) // $data[$key]['due_date'] = ''; @@ -468,4 +468,9 @@ class RecurringInvoice extends BaseModel { return new RecurringService($this); } + + public function subscription(): \Illuminate\Database\Eloquent\Relations\BelongsTo + { + return $this->belongsTo(Subscription::class); + } } diff --git a/resources/views/portal/ninja2020/recurring_invoices/show.blade.php b/resources/views/portal/ninja2020/recurring_invoices/show.blade.php index 3509eed150..fb25f6beae 100644 --- a/resources/views/portal/ninja2020/recurring_invoices/show.blade.php +++ b/resources/views/portal/ninja2020/recurring_invoices/show.blade.php @@ -55,31 +55,35 @@ {{ \App\Utils\Number::formatMoney($invoice->amount, $invoice->client) }} + -
-
-
-
-

- {{ ctrans('texts.cancellation') }} -

-
-

- {{ ctrans('texts.about_cancellation') }} -

+ @if(is_null($invoice->subscription_id) || optional($invoice->subscription)->allow_cancellation) +
+
+
+
+

+ {{ ctrans('texts.cancellation') }} +

+
+

+ {{ ctrans('texts.about_cancellation') }} +

+
-
-
-
- - @include('portal.ninja2020.recurring_invoices.includes.modals.cancellation') +
+
+ + @include('portal.ninja2020.recurring_invoices.includes.modals.cancellation') +
-
+ @endif +
@endsection