Ninja plan

This commit is contained in:
David Bomba 2021-11-24 14:31:05 +11:00
parent 7e9afd7849
commit 9edd8c6de5
6 changed files with 85 additions and 74 deletions

View File

@ -69,16 +69,13 @@ class NinjaPlanController extends Controller
//harvest the current plan //harvest the current plan
$data = []; $data = [];
if(MultiDB::findAndSetDbByAccountKey(Auth::guard('contact')->user()->client->custom_value2)) if(MultiDB::findAndSetDbByAccountKey(Auth::guard('contact')->user()->client->custom_value2))
{ {
$account = Account::where('key', Auth::guard('contact')->user()->client->custom_value2)->first(); $account = Account::where('key', Auth::guard('contact')->user()->client->custom_value2)->first();
if($account && $account->isPaidHostedClient()) if($account)
{ {
$data['account'] = $account;
if(Carbon::parse($account->plan_expires)->lt(now())){ if(Carbon::parse($account->plan_expires)->lt(now())){
//expired get the most recent invoice for payment //expired get the most recent invoice for payment
@ -90,69 +87,58 @@ class NinjaPlanController extends Controller
->orderBy('id', 'DESC') ->orderBy('id', 'DESC')
->first(); ->first();
if($late_invoice) //account status means user cannot perform upgrades until they pay their account.
$data['late_invoice'] = $late_invoice; $data['late_invoice'] = $late_invoice;
} }
//build list of upgrades. $recurring_invoice = RecurringInvoice::on('db-ninja-01')
->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();
$monthly_plans = Subscription::on('db-ninja-01') $monthly_plans = Subscription::on('db-ninja-01')
->where('company_id', Auth::guard('contact')->user()->company->id) ->where('company_id', Auth::guard('contact')->user()->company->id)
->where('group_id', 6) ->where('group_id', 6)
->get(); ->orderBy('promo_price', 'ASC')
->get();
$yearly_plans = Subscription::on('db-ninja-01') $yearly_plans = Subscription::on('db-ninja-01')
->where('company_id', Auth::guard('contact')->user()->company->id) ->where('company_id', Auth::guard('contact')->user()->company->id)
->where('group_id', 31) ->where('group_id', 31)
->get(); ->orderBy('promo_price', 'ASC')
->get();
$monthly_plans = $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;
$data['client'] = Auth::guard('contact')->user()->client;
$data['plans'] = $monthly_plans;
$data['current_subscription_id'] = $current_subscription_id;
$data['current_recurring_id'] = $recurring_invoice ? $recurring_invoice->hashed_id : false;
return $this->render('plan.index', $data);
$monthly_plans->merge($yearly_plans);
} }
} }
else
return redirect()->route('client.catchall');
$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

@ -60,8 +60,8 @@ class RequiredClientInfo extends Component
'contact_first_name' => 'first_name', 'contact_first_name' => 'first_name',
'contact_last_name' => 'last_name', 'contact_last_name' => 'last_name',
'contact_email' => 'email', // 'contact_email' => 'email',
'contact_phone' => 'phone', // 'contact_phone' => 'phone',
]; ];
public $show_form = false; public $show_form = false;
@ -141,7 +141,7 @@ class RequiredClientInfo extends Component
$_field = $this->mappings[$field['name']]; $_field = $this->mappings[$field['name']];
if (Str::startsWith($field['name'], 'client_')) { if (Str::startsWith($field['name'], 'client_')) {
if (empty($this->contact->client->{$_field}) || is_null($this->contact->client->{$_field}) || $this->contact->client->{$_field} = 840) { if (empty($this->contact->client->{$_field}) || is_null($this->contact->client->{$_field}) || $this->contact->client->{$_field} == 840) {
$this->show_form = true; $this->show_form = true;
} else { } else {
$this->fields[$index]['filled'] = true; $this->fields[$index]['filled'] = true;
@ -149,7 +149,7 @@ class RequiredClientInfo extends Component
} }
if (Str::startsWith($field['name'], 'contact_')) { if (Str::startsWith($field['name'], 'contact_')) {
if ((empty($this->contact->{$_field}) || is_null($this->contact->{$_field})) || $this->contact->client->{$_field} = 840) { if ((empty($this->contact->{$_field}) || is_null($this->contact->{$_field})) || $this->contact->client->{$_field} == 840) {
$this->show_form = true; $this->show_form = true;
} else { } else {
$this->fields[$index]['filled'] = true; $this->fields[$index]['filled'] = true;

View File

@ -38,6 +38,7 @@ class SubscriptionRecurringInvoicesTable extends Component
->where('client_id', auth('contact')->user()->client->id) ->where('client_id', auth('contact')->user()->client->id)
->where('company_id', $this->company->id) ->where('company_id', $this->company->id)
->whereNotNull('subscription_id') ->whereNotNull('subscription_id')
->where('is_deleted', false)
->where('status_id', RecurringInvoice::STATUS_ACTIVE) ->where('status_id', RecurringInvoice::STATUS_ACTIVE)
->orderBy($this->sort_field, $this->sort_asc ? 'asc' : 'desc') ->orderBy($this->sort_field, $this->sort_asc ? 'asc' : 'desc')
->withTrashed() ->withTrashed()

View File

@ -136,7 +136,7 @@ class CreditCard
return $this->processSuccessfulPayment($result); return $this->processSuccessfulPayment($result);
} }
$error = $result ?: 'Undefined gateway error'; $error = 'Undefined gateway error';
return $this->processUnsuccessfulPayment($error); return $this->processUnsuccessfulPayment($error);

View File

@ -18,6 +18,7 @@ use App\Factory\InvoiceToRecurringInvoiceFactory;
use App\Factory\RecurringInvoiceFactory; use App\Factory\RecurringInvoiceFactory;
use App\Jobs\Util\SubscriptionWebhookHandler; use App\Jobs\Util\SubscriptionWebhookHandler;
use App\Jobs\Util\SystemLogger; use App\Jobs\Util\SystemLogger;
use App\Libraries\MultiDB;
use App\Models\Client; use App\Models\Client;
use App\Models\ClientContact; use App\Models\ClientContact;
use App\Models\Credit; use App\Models\Credit;
@ -240,10 +241,10 @@ class SubscriptionService
elseif ($outstanding->count() > 1) { elseif ($outstanding->count() > 1) {
//user is changing plan mid frequency cycle //user is changing plan mid frequency cycle
//we cannot handle this if there are more than one invoice outstanding. //we cannot handle this if there are more than one invoice outstanding.
return null; return $target->price;
} }
return null; return $target->price;
} }
@ -439,7 +440,7 @@ class SubscriptionService
$credit = false; $credit = false;
/* Only generate a credit if the previous invoice was paid in full. */ /* Only generate a credit if the previous invoice was paid in full. */
if($last_invoice->balance == 0) if($last_invoice && $last_invoice->balance == 0)
$credit = $this->createCredit($last_invoice, $target_subscription, $is_credit); $credit = $this->createCredit($last_invoice, $target_subscription, $is_credit);
$new_recurring_invoice = $this->createNewRecurringInvoice($recurring_invoice); $new_recurring_invoice = $this->createNewRecurringInvoice($recurring_invoice);

View File

@ -1,5 +1,5 @@
@extends('portal.ninja2020.layout.app') @extends('portal.ninja2020.layout.app')
@section('meta_title', ctrans('texts.pro_plan_call_to_action')) @section('meta_title', ctrans('texts.account_management'))
@section('body') @section('body')
@ -7,11 +7,8 @@
<div class="bg-white shadow overflow-hidden sm:rounded-lg"> <div class="bg-white shadow overflow-hidden sm:rounded-lg">
<div class="px-4 py-5 sm:px-6"> <div class="px-4 py-5 sm:px-6">
<h3 class="text-lg leading-6 font-medium text-gray-900"> <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') }} {{ ctrans('texts.plan_status') }}
</p> </h3>
</div> </div>
<div class="border-t border-gray-200"> <div class="border-t border-gray-200">
<dl> <dl>
@ -49,6 +46,29 @@
@endif @endif
@if($late_invoice)
<div class="px-4 py-5 sm:px-6">
<h3 class="text-lg leading-6 font-medium text-gray-900">
{{ ctrans('texts.invoice_status_id') }}
</h3>
<p class="mt-1 max-w-2xl text-sm text-gray-500">
{{ ctrans('texts.past_due') }}
</p>
</div>
<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.invoice') }}
</dt>
<dd class="mt-1 text-sm text-gray-900 sm:mt-0 sm:col-span-2">
{{ $late_invoice->number }} - {{ \App\Utils\Number::formatMoney($late_invoice->balance, $client) }} <a class="button-link text-primary" href="/client/invoices/{{$late_invoice->hashed_id}}">{{ ctrans('texts.pay_now')}}</a>
</dd>
</div>
</dl>
@else
<div class="bg-white px-4 py-5 sm:grid sm:grid-cols-3 sm:gap-4 sm:px-6"> <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"> <dt class="text-sm font-medium text-gray-500">
{{ ctrans('texts.plan_change') }} {{ ctrans('texts.plan_change') }}
@ -71,10 +91,11 @@
{{ ctrans('texts.plan_upgrade') }} {{ ctrans('texts.plan_upgrade') }}
</button> </button>
@endif @endif
</dd> </dd>
</div> </div>
@endif
</dl> </dl>
</div> </div>
</div> </div>
@ -88,13 +109,15 @@
@if($current_recurring_id) @if($current_recurring_id)
document.getElementById('handlePlanChange').addEventListener('click', function() { document.getElementById('handlePlanChange').addEventListener('click', function() {
location.href = 'http://ninja.test:8000/client/subscriptions/{{ $current_recurring_id }}/plan_switch/' + document.getElementById("newPlan").value + ''; if(document.getElementById("newPlan").value.length > 1)
location.href = 'https://invoiceninja.invoicing.co/client/subscriptions/{{ $current_recurring_id }}/plan_switch/' + document.getElementById("newPlan").value + '';
}); });
@else @else
document.getElementById('handleNewPlan').addEventListener('click', function() { document.getElementById('handleNewPlan').addEventListener('click', function() {
location.href = 'http://ninja.test:8000/client/subscriptions/' + document.getElementById("newPlan").value + '/purchase'; if(document.getElementById("newPlan").value.length > 1)
location.href = 'https://invoiceninja.invoicing.co/client/subscriptions/' + document.getElementById("newPlan").value + '/purchase';
}); });
@endif @endif