1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-11 13:42:49 +01:00
invoiceninja/app/Http/Controllers/ClientPortal/NinjaPlanController.php

148 lines
5.8 KiB
PHP
Raw Normal View History

2021-09-01 06:02:57 +02:00
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2021. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://www.elastic.co/licensing/elastic-license
*/
namespace App\Http\Controllers\ClientPortal;
use App\Http\Controllers\Controller;
use App\Http\Requests\ClientPortal\Uploads\StoreUploadRequest;
use App\Libraries\MultiDB;
2021-10-03 04:36:43 +02:00
use App\Models\Account;
2021-09-01 06:02:57 +02:00
use App\Models\ClientContact;
2021-09-01 07:03:01 +02:00
use App\Models\Company;
2021-11-23 11:39:43 +01:00
use App\Models\Invoice;
2021-11-23 22:57:24 +01:00
use App\Models\RecurringInvoice;
2021-11-23 11:39:43 +01:00
use App\Models\Subscription;
2021-09-01 06:02:57 +02:00
use App\Utils\Ninja;
2021-11-23 22:57:24 +01:00
use App\Utils\Traits\MakesHash;
2021-09-01 06:02:57 +02:00
use Illuminate\Contracts\Routing\ResponseFactory;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
2021-11-23 11:39:43 +01:00
use Illuminate\Support\Carbon;
2021-09-09 08:59:23 +02:00
use Illuminate\Support\Facades\Auth;
2021-09-01 06:02:57 +02:00
class NinjaPlanController extends Controller
{
2021-11-23 22:57:24 +01:00
use MakesHash;
2021-09-01 06:02:57 +02:00
2021-10-03 04:36:43 +02:00
public function index(string $contact_key, string $account_or_company_key)
2021-09-01 06:02:57 +02:00
{
2021-10-03 04:36:43 +02:00
MultiDB::findAndSetDbByCompanyKey($account_or_company_key);
$company = Company::where('company_key', $account_or_company_key)->first();
if(!$company){
MultiDB::findAndSetDbByAccountKey($account_or_company_key);
$account = Account::where('key', $account_or_company_key)->first();
}
else
$account = $company->account;
2021-09-11 08:26:00 +02:00
if (MultiDB::findAndSetDbByContactKey($contact_key) && $client_contact = ClientContact::where('contact_key', $contact_key)->first())
2021-09-01 07:03:01 +02:00
{
2021-09-03 15:24:18 +02:00
nlog("Ninja Plan Controller - Found and set Client Contact");
2021-09-01 07:03:01 +02:00
Auth::guard('contact')->login($client_contact,true);
2021-09-01 06:02:57 +02:00
2021-11-28 00:58:54 +01:00
// /* Current paid users get pushed straight to subscription overview page*/
// if($account->isPaidHostedClient())
// return redirect('/client/dashboard');
2021-09-01 06:02:57 +02:00
2021-11-28 00:58:54 +01:00
// /* Users that are not paid get pushed to a custom purchase page */
// return $this->render('subscriptions.ninja_plan', ['settings' => $client_contact->company->settings]);
return $this->plan();
2021-09-01 06:02:57 +02:00
}
return redirect()->route('client.catchall');
}
2021-11-23 11:39:43 +01:00
public function plan()
{
//harvest the current plan
$data = [];
2021-11-28 11:19:34 +01:00
$data['late_invoice'] = false;
2021-11-23 11:39:43 +01:00
if(MultiDB::findAndSetDbByAccountKey(Auth::guard('contact')->user()->client->custom_value2))
{
$account = Account::where('key', Auth::guard('contact')->user()->client->custom_value2)->first();
2021-11-24 04:31:05 +01:00
if($account)
2021-11-23 11:39:43 +01:00
{
2021-11-23 22:57:24 +01:00
if(Carbon::parse($account->plan_expires)->lt(now())){
2021-11-23 11:39:43 +01:00
//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)
2021-11-23 22:57:24 +01:00
->whereNotNull('subscription_id')
->orderBy('id', 'DESC')
2021-11-23 11:39:43 +01:00
->first();
2021-11-24 04:31:05 +01:00
//account status means user cannot perform upgrades until they pay their account.
2021-11-23 11:39:43 +01:00
$data['late_invoice'] = $late_invoice;
}
2021-11-24 04:31:05 +01:00
$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')
->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 = $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;
});
}
2021-11-23 11:39:43 +01:00
2021-11-24 04:31:05 +01:00
$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;
2021-11-23 11:39:43 +01:00
2021-11-24 04:31:05 +01:00
return $this->render('plan.index', $data);
2021-11-23 22:57:24 +01:00
2021-11-23 11:39:43 +01:00
}
2021-11-23 22:57:24 +01:00
}
2021-11-24 04:31:05 +01:00
else
2021-11-28 00:58:54 +01:00
return redirect('/client/dashboard');
2021-11-24 04:31:05 +01:00
2021-11-23 11:39:43 +01:00
}
2021-09-01 06:02:57 +02:00
}