Ninja plan

This commit is contained in:
David Bomba 2021-11-24 08:57:24 +11:00
parent 3c8651ad6f
commit 7e9afd7849
3 changed files with 163 additions and 9 deletions

View File

@ -19,8 +19,10 @@ use App\Models\Account;
use App\Models\ClientContact;
use App\Models\Company;
use App\Models\Invoice;
use App\Models\RecurringInvoice;
use App\Models\Subscription;
use App\Utils\Ninja;
use App\Utils\Traits\MakesHash;
use Illuminate\Contracts\Routing\ResponseFactory;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
@ -29,6 +31,7 @@ use Illuminate\Support\Facades\Auth;
class NinjaPlanController extends Controller
{
use MakesHash;
public function index(string $contact_key, string $account_or_company_key)
{
@ -66,6 +69,7 @@ class NinjaPlanController extends Controller
//harvest the current plan
$data = [];
if(MultiDB::findAndSetDbByAccountKey(Auth::guard('contact')->user()->client->custom_value2))
{
$account = Account::where('key', Auth::guard('contact')->user()->client->custom_value2)->first();
@ -73,14 +77,17 @@ class NinjaPlanController extends Controller
if($account && $account->isPaidHostedClient())
{
if(Carbon::parse($account->plan_expires).lt(now())){
$data['account'] = $account;
if(Carbon::parse($account->plan_expires)->lt(now())){
//expired get the most recent invoice for payment
$late_invoice = Invoice::on('db-ninja-01')
->where('company_id', Auth::guard('contact')->user()->company->id)
->where('client_id', Auth::guard('contact')->user()->client->id)
->where('status_id', Invoice::STATUS_SENT)
->orderBy('id', DESC)
->whereNotNull('subscription_id')
->orderBy('id', 'DESC')
->first();
if($late_invoice)
@ -90,17 +97,62 @@ class NinjaPlanController extends Controller
//build list of upgrades.
$data['monthly_plans'] = Subscription::on('db-ninja-01')
$monthly_plans = Subscription::on('db-ninja-01')
->where('company_id', Auth::guard('contact')->user()->company->id)
->where('group_id', 6)
->get();
$data['yearly_plans'] = Subscription::on('db-ninja-01')
$yearly_plans = Subscription::on('db-ninja-01')
->where('company_id', Auth::guard('contact')->user()->company->id)
->where('group_id', 31)
->get();
$monthly_plans->merge($yearly_plans);
}
}
$recurring_invoice = RecurringInvoice::query()
->where('client_id', auth('contact')->user()->client->id)
->where('company_id', Auth::guard('contact')->user()->company->id)
->whereNotNull('subscription_id')
->where('status_id', RecurringInvoice::STATUS_ACTIVE)
->orderBy('id', 'desc')
->first();
$data['late_invoice'] = Invoice::first();
$monthly_plans = Subscription::on('db-ninja-01')
->where('company_id', Auth::guard('contact')->user()->company->id)
// ->where('group_id', 6)
->orderBy('promo_price', 'ASC')
->get();
$yearly_plans = Subscription::on('db-ninja-01')
->where('company_id', Auth::guard('contact')->user()->company->id)
->where('group_id', 31)
->orderBy('promo_price', 'ASC')
->get();
$monthly_plans->merge($yearly_plans);
$current_subscription_id = $recurring_invoice ? $this->encodePrimaryKey($recurring_invoice->subscription_id) : false;
//remove existing subscription
if($current_subscription_id){
$monthly_plans = $monthly_plans->filter(function ($plan) use($current_subscription_id){
return (string)$plan->hashed_id != (string)$current_subscription_id;
});
}
$data['account'] = Account::first();
$data['client'] = Auth::guard('contact')->user()->client;
$data['plans'] = $monthly_plans;
$data['current_subscription_id'] = $current_subscription_id;
$data['current_recurring_id'] = $recurring_invoice ? $this->encodePrimaryKey($recurring_invoice->hashed_id) : false;
return $this->render('plan.index', $data);
}
}

View File

@ -115,17 +115,16 @@ class PortalComposer
$data[] = ['title' => ctrans('texts.documents'), 'url' => 'client.documents.index', 'icon' => 'download'];
$data[] = ['title' => ctrans('texts.subscriptions'), 'url' => 'client.subscriptions.index', 'icon' => 'calendar'];
if(Ninja::isHosted() && auth('contact')->user()->company->id == config('ninja.ninja_default_company_id'))
$data[] = ['title' => ctrans('texts.plan'), 'url' => 'client.plan', 'icon' => 'credit-card'];
if (auth('contact')->user()->client->getSetting('enable_client_portal_tasks')) {
$data[] = ['title' => ctrans('texts.tasks'), 'url' => 'client.tasks.index', 'icon' => 'clock'];
}
$data[] = ['title' => ctrans('texts.statement'), 'url' => 'client.statement', 'icon' => 'activity'];
if(Ninja::isHosted() && auth('contact')->user()->company->id == config('ninja.ninja_default_company_id'))
$data[] = ['title' => ctrans('texts.plan'), 'url' => 'client.plan', 'icon' => 'credit-card'];
return $data;
}
}

View File

@ -0,0 +1,103 @@
@extends('portal.ninja2020.layout.app')
@section('meta_title', ctrans('texts.pro_plan_call_to_action'))
@section('body')
<!-- This example requires Tailwind CSS v2.0+ -->
<div class="bg-white shadow overflow-hidden sm:rounded-lg">
<div class="px-4 py-5 sm:px-6">
<h3 class="text-lg leading-6 font-medium text-gray-900">
{{ ctrans('texts.account_management') }}
</h3>
<p class="mt-1 max-w-2xl text-sm text-gray-500">
{{ ctrans('texts.plan_status') }}
</p>
</div>
<div class="border-t border-gray-200">
<dl>
<div class="bg-white px-4 py-5 sm:grid sm:grid-cols-3 sm:gap-4 sm:px-6">
<dt class="text-sm font-medium text-gray-500">
{{ ctrans('texts.plan') }}
</dt>
<dd class="mt-1 text-sm text-gray-900 sm:mt-0 sm:col-span-2">
{{ $account->plan ? ucfirst($account->plan) : 'Free' }}
</dd>
</div>
@if($account->plan)
<div class="bg-white px-4 py-5 sm:grid sm:grid-cols-3 sm:gap-4 sm:px-6">
<dt class="text-sm font-medium text-gray-500">
{{ ctrans('texts.expires') }}
</dt>
<dd class="mt-1 text-sm text-gray-900 sm:mt-0 sm:col-span-2">
{{ $client->formatDate($account->plan_expires, $client->date_format()) }}
</dd>
</div>
@if($account->plan == 'enterprise')
<div class="bg-white px-4 py-5 sm:grid sm:grid-cols-3 sm:gap-4 sm:px-6">
<dt class="text-sm font-medium text-gray-500">
{{ ctrans('texts.users')}}
</dt>
<dd class="mt-1 text-sm text-gray-900 sm:mt-0 sm:col-span-2">
{{ $account->num_users }}
</dd>
</div>
@endif
@endif
<div class="bg-white px-4 py-5 sm:grid sm:grid-cols-3 sm:gap-4 sm:px-6">
<dt class="text-sm font-medium text-gray-500">
{{ ctrans('texts.plan_change') }}
</dt>
<dd class="mt-1 text-sm text-gray-900 sm:mt-0 sm:col-span-2">
<div>
<select id="newPlan" class="pl-3 pr-10 py-2 text-base border-gray-300 focus:outline-none focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm rounded-md">
<option value="">Select Plan</option>
@foreach($plans as $plan)
<option value="{{ $plan->hashed_id}}">{{ $plan->name }} {{ \App\Utils\Number::formatMoney($plan->promo_price, $client) }}</option>
@endforeach
</select>
@if($current_recurring_id)
<button id="handlePlanChange" class="bg-transparent hover:bg-blue-500 text-blue-700 font-semibold hover:text-white py-2 px-4 border border-blue-500 hover:border-transparent rounded">
{{ ctrans('texts.plan_change') }}
</button>
@else
<button id="handleNewPlan" class="bg-transparent hover:bg-blue-500 text-blue-700 font-semibold hover:text-white py-2 px-4 border border-blue-500 hover:border-transparent rounded">
{{ ctrans('texts.plan_upgrade') }}
</button>
@endif
</dd>
</div>
</dl>
</div>
</div>
@endsection
@push('footer')
<script type="text/javascript">
@if($current_recurring_id)
document.getElementById('handlePlanChange').addEventListener('click', function() {
location.href = 'http://ninja.test:8000/client/subscriptions/{{ $current_recurring_id }}/plan_switch/' + document.getElementById("newPlan").value + '';
});
@else
document.getElementById('handleNewPlan').addEventListener('click', function() {
location.href = 'http://ninja.test:8000/client/subscriptions/' + document.getElementById("newPlan").value + '/purchase';
});
@endif
</script>
@endpush