1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-09-22 09:21:34 +02:00
invoiceninja/app/Http/Controllers/ClientPortal/NinjaPlanController.php

159 lines
6.2 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
/* Current paid users get pushed straight to subscription overview page*/
2021-09-01 06:36:22 +02:00
if($account->isPaidHostedClient())
2021-10-22 01:55:58 +02:00
return redirect('/client/dashboard');
2021-09-01 06:02:57 +02: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 redirect()->route('client.catchall');
}
2021-11-23 11:39:43 +01:00
public function plan()
{
//harvest the current plan
$data = [];
2021-11-23 22:57:24 +01:00
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();
if($account && $account->isPaidHostedClient())
{
2021-11-23 22:57:24 +01:00
$data['account'] = $account;
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();
if($late_invoice)
$data['late_invoice'] = $late_invoice;
}
//build list of upgrades.
2021-11-23 22:57:24 +01:00
$monthly_plans = Subscription::on('db-ninja-01')
2021-11-23 11:39:43 +01:00
->where('company_id', Auth::guard('contact')->user()->company->id)
->where('group_id', 6)
->get();
2021-11-23 22:57:24 +01:00
$yearly_plans = Subscription::on('db-ninja-01')
2021-11-23 11:39:43 +01:00
->where('company_id', Auth::guard('contact')->user()->company->id)
->where('group_id', 31)
->get();
2021-11-23 22:57:24 +01:00
$monthly_plans->merge($yearly_plans);
2021-11-23 11:39:43 +01:00
}
}
2021-11-23 22:57:24 +01:00
$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);
2021-11-23 11:39:43 +01:00
}
2021-09-01 06:02:57 +02:00
}