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

290 lines
13 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
*
2023-01-28 23:21:40 +01:00
* @copyright Copyright (c) 2023. Invoice Ninja LLC (https://invoiceninja.com)
2021-09-01 06:02:57 +02:00
*
* @license https://www.elastic.co/licensing/elastic-license
*/
namespace App\Http\Controllers\ClientPortal;
use App\DataMapper\Analytics\TrialStarted;
2022-02-23 07:39:36 +01:00
use App\Factory\RecurringInvoiceFactory;
2021-09-01 06:02:57 +02:00
use App\Http\Controllers\Controller;
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;
2022-02-23 03:07:48 +01:00
use App\Models\CompanyGateway;
2022-02-23 07:39:36 +01:00
use App\Models\GatewayType;
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;
2022-06-23 02:52:41 +02:00
use App\Notifications\Ninja\NewAccountNotification;
2023-01-11 02:47:03 +01:00
use App\Repositories\RecurringInvoiceRepository;
2022-02-23 07:39:36 +01:00
use App\Repositories\SubscriptionRepository;
2021-11-23 22:57:24 +01:00
use App\Utils\Traits\MakesHash;
2021-09-01 06:02:57 +02:00
use Illuminate\Http\Request;
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;
use Turbo124\Beacon\Facades\LightLogs;
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);
2023-08-06 09:03:12 +02:00
$company = Company::query()->where('company_key', $account_or_company_key)->first();
2021-10-03 04:36:43 +02:00
if (! $company) {
2021-10-03 04:36:43 +02:00
MultiDB::findAndSetDbByAccountKey($account_or_company_key);
2023-08-04 08:40:44 +02:00
/** @var \App\Models\Account $account **/
2023-08-06 09:03:12 +02:00
$account = Account::query()->where('key', $account_or_company_key)->first();
} else {
2021-10-03 04:36:43 +02:00
$account = $company->account;
}
2021-10-03 04:36:43 +02:00
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')->loginUsingId($client_contact->id, true);
2021-09-01 06:02:57 +02:00
2021-11-28 00:58:54 +01:00
return $this->plan();
2021-09-01 06:02:57 +02:00
}
return redirect()->route('client.catchall');
}
2021-11-23 11:39:43 +01:00
2022-02-23 03:07:48 +01:00
public function trial()
{
2022-02-27 09:02:00 +01:00
$gateway = CompanyGateway::on('db-ninja-01')->find(config('ninja.ninja_default_company_gateway_id'));
2022-02-23 03:07:48 +01:00
$data['gateway'] = $gateway;
2022-02-23 07:39:36 +01:00
$gateway_driver = $gateway->driver(auth()->guard('contact')->user()->client)->init();
$customer = $gateway_driver->findOrCreateCustomer();
2022-02-23 03:07:48 +01:00
$setupIntent = \Stripe\SetupIntent::create([
'payment_method_types' => ['card'],
'usage' => 'off_session',
'customer' => $customer->id,
2022-02-23 03:07:48 +01:00
]);
$data['intent'] = $setupIntent;
$data['client'] = Auth::guard('contact')->user()->client;
2022-02-23 03:07:48 +01:00
return $this->render('plan.trial', $data);
}
2022-02-23 07:39:36 +01:00
public function trial_confirmation(Request $request)
{
$client = auth()->guard('contact')->user()->client;
$client->fill($request->all());
$client->save();
//store payment method
2022-02-27 09:02:00 +01:00
$gateway = CompanyGateway::on('db-ninja-01')->find(config('ninja.ninja_default_company_gateway_id'));
2022-02-23 07:39:36 +01:00
$gateway_driver = $gateway->driver(auth()->guard('contact')->user()->client)->init();
$stripe_response = json_decode($request->input('gateway_response'));
$customer = $gateway_driver->findOrCreateCustomer();
//27-08-2022 Ensure customer is updated appropriately
$update_client_object['name'] = $client->present()->name();
$update_client_object['phone'] = substr($client->present()->phone(), 0, 20);
$update_client_object['address']['line1'] = $client->address1 ?: '';
$update_client_object['address']['line2'] = $client->address2 ?: '';
$update_client_object['address']['city'] = $client->city ?: '';
$update_client_object['address']['postal_code'] = $client->postal_code ?: '';
$update_client_object['address']['state'] = $client->state ?: '';
$update_client_object['address']['country'] = $client->country ? $client->country->iso_3166_2 : '';
$update_client_object['shipping']['name'] = $client->present()->name();
$update_client_object['shipping']['address']['line1'] = $client->shipping_address1 ?: '';
$update_client_object['shipping']['address']['line2'] = $client->shipping_address2 ?: '';
$update_client_object['shipping']['address']['city'] = $client->shipping_city ?: '';
$update_client_object['shipping']['address']['postal_code'] = $client->shipping_postal_code ?: '';
$update_client_object['shipping']['address']['state'] = $client->shipping_state ?: '';
$update_client_object['shipping']['address']['country'] = $client->shipping_country ? $client->shipping_country->iso_3166_2 : '';
\Stripe\Customer::update($customer->id, $update_client_object, $gateway_driver->stripe_connect_auth);
2022-02-23 07:39:36 +01:00
$gateway_driver->attach($stripe_response->payment_method, $customer);
$method = $gateway_driver->getStripePaymentMethod($stripe_response->payment_method);
$payment_meta = new \stdClass;
$payment_meta->exp_month = (string) $method->card->exp_month;
$payment_meta->exp_year = (string) $method->card->exp_year;
$payment_meta->brand = (string) $method->card->brand;
$payment_meta->last4 = (string) $method->card->last4;
$payment_meta->type = GatewayType::CREDIT_CARD;
$data = [
'payment_meta' => $payment_meta,
'token' => $method->id,
'payment_method_id' => GatewayType::CREDIT_CARD,
];
$gateway_driver->storeGatewayToken($data, ['gateway_customer_reference' => $customer->id]);
//set free trial
if (auth()->guard('contact')->user()->client->custom_value2) {
2022-02-27 09:02:00 +01:00
MultiDB::findAndSetDbByAccountKey(auth()->guard('contact')->user()->client->custom_value2);
2023-08-04 09:12:21 +02:00
/** @var \App\Models\Account $account **/
2022-03-07 10:06:20 +01:00
$account = Account::where('key', auth()->guard('contact')->user()->client->custom_value2)->first();
2022-05-29 10:17:03 +02:00
// $account->trial_started = now();
// $account->trial_plan = 'pro';
2022-05-03 01:00:07 +02:00
$account->plan = 'pro';
2022-05-28 02:20:32 +02:00
$account->plan_term = 'month';
$account->plan_started = now();
2022-05-03 01:00:07 +02:00
$account->plan_expires = now()->addDays(14);
2023-01-15 09:05:52 +01:00
$account->is_trial=true;
2023-01-24 22:45:05 +01:00
$account->hosted_company_count = 10;
2022-02-27 09:02:00 +01:00
$account->save();
}
MultiDB::setDB('db-ninja-01');
2022-02-23 07:39:36 +01:00
//create recurring invoice
$subscription_repo = new SubscriptionRepository();
2023-08-07 07:07:52 +02:00
/** @var \App\Models\Subscription $subscription **/
2022-02-23 07:39:36 +01:00
$subscription = Subscription::find(6);
2022-02-23 07:48:22 +01:00
2022-02-23 07:39:36 +01:00
$recurring_invoice = RecurringInvoiceFactory::create($subscription->company_id, $subscription->user_id);
$recurring_invoice->client_id = $client->id;
$recurring_invoice->line_items = $subscription_repo->generateLineItems($subscription, true, false);
$recurring_invoice->subscription_id = $subscription->id;
$recurring_invoice->frequency_id = $subscription->frequency_id ?: RecurringInvoice::FREQUENCY_MONTHLY;
$recurring_invoice->date = now()->addDays(14);
$recurring_invoice->remaining_cycles = -1;
$recurring_invoice->auto_bill = $client->getSetting('auto_bill');
$recurring_invoice->auto_bill_enabled = $this->setAutoBillFlag($recurring_invoice->auto_bill);
2022-02-23 07:39:36 +01:00
$recurring_invoice->due_date_days = 'terms';
$recurring_invoice->next_send_date = now()->addDays(14)->format('Y-m-d');
2022-06-16 00:04:20 +02:00
$recurring_invoice->next_send_date_client = now()->addDays(14)->format('Y-m-d');
2022-02-23 07:39:36 +01:00
$recurring_invoice->save();
$r = $recurring_invoice->calc()->getRecurringInvoice();
2022-02-28 04:38:13 +01:00
$recurring_invoice->service()->applyNumber()->start()->save();
2022-02-23 07:39:36 +01:00
LightLogs::create(new TrialStarted())
->increment()
->queue();
2022-03-11 02:56:45 +01:00
2023-08-06 09:03:12 +02:00
$old_recurring = RecurringInvoice::query()->where('company_id', config('ninja.ninja_default_company_id'))
2023-05-04 03:18:13 +02:00
->where('client_id', $client->id)
->where('id', '!=', $recurring_invoice->id)
->first();
2023-01-11 02:47:03 +01:00
2023-02-16 02:36:09 +01:00
if ($old_recurring) {
2023-01-11 02:47:03 +01:00
$old_recurring->service()->stop()->save();
$old_recurring_repo = new RecurringInvoiceRepository();
$old_recurring_repo->archive($old_recurring);
}
2022-06-23 02:52:41 +02:00
$ninja_company = Company::on('db-ninja-01')->find(config('ninja.ninja_default_company_id'));
2022-09-17 05:23:06 +02:00
$ninja_company->notification(new NewAccountNotification($subscription->company->account, $client))->ninja();
2022-06-23 02:52:41 +02:00
return $this->render('plan.trial_confirmed', $data);
2022-02-23 07:39:36 +01:00
}
private function setAutoBillFlag($auto_bill)
{
if ($auto_bill == 'always' || $auto_bill == 'optout') {
return true;
}
return false;
}
2021-11-23 11:39:43 +01:00
public function plan()
{
2022-09-17 05:23:06 +02:00
// return $this->trial();
2021-11-23 11:39:43 +01:00
//harvest the current plan
$data = [];
2021-11-28 11:19:34 +01:00
$data['late_invoice'] = false;
if (MultiDB::findAndSetDbByAccountKey(Auth::guard('contact')->user()->client->custom_value2)) {
2023-08-07 07:07:52 +02:00
$account = Account::query()->where('key', Auth::guard('contact')->user()->client->custom_value2)->first();
2021-11-23 11:39:43 +01:00
if ($account) {
2022-02-23 07:48:22 +01:00
//offer the option to have a free trial
2023-01-27 23:47:57 +01:00
if (!$account->plan && !$account->is_trial) {
2022-02-23 07:48:22 +01:00
return $this->trial();
}
2021-11-23 11:39:43 +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.
2022-01-14 09:11:45 +01:00
// $data['late_invoice'] = $late_invoice;
//14-01-2022 remove late invoices from blocking upgrades
$data['late_invoice'] = false;
2021-11-23 11:39:43 +01:00
}
$recurring_invoice = RecurringInvoice::on('db-ninja-01')
->where('client_id', auth()->guard('contact')->user()->client->id)
2021-11-24 04:31:05 +01:00
->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-24 04:31:05 +01:00
}
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;
2021-11-24 04:31:05 +01:00
$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 11:39:43 +01:00
}
} else {
2021-11-28 00:58:54 +01:00
return redirect('/client/dashboard');
}
2021-11-23 11:39:43 +01:00
}
2021-09-01 06:02:57 +02:00
}