first(); if(!$company){ MultiDB::findAndSetDbByAccountKey($account_or_company_key); $account = Account::where('key', $account_or_company_key)->first(); } else $account = $company->account; if (MultiDB::findAndSetDbByContactKey($contact_key) && $client_contact = ClientContact::where('contact_key', $contact_key)->first()) { nlog("Ninja Plan Controller - Found and set Client Contact"); Auth::guard('contact')->login($client_contact,true); /* Current paid users get pushed straight to subscription overview page*/ if($account->isPaidHostedClient()) return redirect('/client/dashboard'); /* Users that are not paid get pushed to a custom purchase page */ return $this->render('subscriptions.ninja_plan', ['settings' => $client_contact->company->settings]); } return redirect()->route('client.catchall'); } public function plan() { //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(); if($account && $account->isPaidHostedClient()) { $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) ->whereNotNull('subscription_id') ->orderBy('id', 'DESC') ->first(); if($late_invoice) $data['late_invoice'] = $late_invoice; } //build list of upgrades. $monthly_plans = Subscription::on('db-ninja-01') ->where('company_id', Auth::guard('contact')->user()->company->id) ->where('group_id', 6) ->get(); $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); } }