From 8590f9ad17efadbe4e8df8327c4bef7c3253650e Mon Sep 17 00:00:00 2001 From: Hillel Coren Date: Thu, 14 Jul 2016 12:46:00 +0300 Subject: [PATCH] Working on new pricing --- app/Console/Commands/SendRenewalInvoices.php | 2 +- app/Http/Controllers/AccountController.php | 81 +++++++++++++++++++ app/Models/User.php | 7 +- .../PaymentDrivers/BasePaymentDriver.php | 40 ++++----- app/Ninja/Repositories/AccountRepository.php | 19 +---- resources/views/accounts/management.blade.php | 37 +-------- 6 files changed, 107 insertions(+), 79 deletions(-) diff --git a/app/Console/Commands/SendRenewalInvoices.php b/app/Console/Commands/SendRenewalInvoices.php index 1c46f50680c5..6ed81a1183de 100644 --- a/app/Console/Commands/SendRenewalInvoices.php +++ b/app/Console/Commands/SendRenewalInvoices.php @@ -53,7 +53,7 @@ class SendRenewalInvoices extends Command $companies = Company::whereRaw('datediff(plan_expires, curdate()) = 10') ->orderBy('id') ->get(); - $this->info(count($companies).' companies found'); + $this->info(count($companies).' companies found renewing in 10 days'); foreach ($companies as $company) { if (!count($company->accounts)) { diff --git a/app/Http/Controllers/AccountController.php b/app/Http/Controllers/AccountController.php index 17fac3e3d8ed..844f904005ca 100644 --- a/app/Http/Controllers/AccountController.php +++ b/app/Http/Controllers/AccountController.php @@ -158,6 +158,85 @@ class AccountController extends BaseController /** * @return \Illuminate\Http\RedirectResponse */ + public function changePlan() { + $user = Auth::user(); + $account = $user->account; + $company = $account->company; + + $plan = Input::get('plan'); + $term = Input::get('plan_term'); + $numUsers = Input::get('num_users'); + + $planDetails = $account->getPlanDetails(false, false); + + $newPlan = [ + 'plan' => $plan, + 'term' => $term, + 'num_users' => $numUsers, + ]; + $newPlan['price'] = Utils::getPlanPrice($newPlan); + $credit = 0; + + if (!empty($planDetails['started']) && $plan == PLAN_FREE) { + // Downgrade + $refund_deadline = clone $planDetails['started']; + $refund_deadline->modify('+30 days'); + + if ($plan == PLAN_FREE && $refund_deadline >= date_create()) { + // Refund + $company->plan = null; + $company->plan_term = null; + $company->plan_started = null; + $company->plan_expires = null; + $company->plan_paid = null; + + if ($payment = $account->company->payment) { + $ninjaAccount = $this->accountRepo->getNinjaAccount(); + $paymentDriver = $ninjaAccount->paymentDriver(); + $paymentDriver->refundPayment($payment); + Session::flash('message', trans('texts.plan_refunded')); + \Log::info("Refunded Plan Payment: {$account->name} - {$user->email}"); + } else { + Session::flash('message', trans('texts.updated_plan')); + } + + $account->company->save(); + } + } + + if (!empty($planDetails['paid']) && $plan != PLAN_FREE) { + $time_used = $planDetails['paid']->diff(date_create()); + $days_used = $time_used->days; + + if ($time_used->invert) { + // They paid in advance + $days_used *= -1; + } + + $days_total = $planDetails['paid']->diff($planDetails['expires'])->days; + $percent_used = $days_used / $days_total; + $credit = $planDetails['plan_price'] * (1 - $percent_used); + } + + if ($newPlan['price'] > $credit) { + $invitation = $this->accountRepo->enablePlan($newPlan, $credit); + return Redirect::to('view/' . $invitation->invitation_key); + } else { + + $company->plan = $plan; + $company->plan_term = $term; + $company->plan_price = $newPlan['price']; + $company->num_users = $numUsers; + $company->plan_expires = DateTime::createFromFormat('Y-m-d', $company->plan_paid)->modify($term == PLAN_TERM_MONTHLY ? '+1 month' : '+1 year')->format('Y-m-d'); + + // TODO change plan + var_dump($newPlan); + var_dump($credit); + dd('0'); + } + } + + /* public function changePlan() { $user = Auth::user(); $account = $user->account; @@ -280,6 +359,8 @@ class AccountController extends BaseController return Redirect::to('/settings/'.ACCOUNT_MANAGEMENT, 301); } + */ + /** * @param $entityType diff --git a/app/Models/User.php b/app/Models/User.php index e95459f384b6..933e7f888d82 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -432,8 +432,11 @@ class User extends Model implements AuthenticatableContract, AuthorizableContrac } - public function caddAddUsers() { - if ( ! $this->hasFeature(FEATURE_USERS)) { + public function caddAddUsers() + { + if ( ! Utils::isNinja()) { + return true; + } elseif ( ! $this->hasFeature(FEATURE_USERS)) { return false; } diff --git a/app/Ninja/PaymentDrivers/BasePaymentDriver.php b/app/Ninja/PaymentDrivers/BasePaymentDriver.php index 33e9cd9b259a..baef52a7a34e 100644 --- a/app/Ninja/PaymentDrivers/BasePaymentDriver.php +++ b/app/Ninja/PaymentDrivers/BasePaymentDriver.php @@ -591,57 +591,49 @@ class BasePaymentDriver if (1 == preg_match('/^Plan - (.+) \((.+)\)$/', $invoice_item->product_key, $matches)) { $plan = strtolower($matches[1]); $term = strtolower($matches[2]); + $price = $invoice_item->cost; if ($plan == PLAN_ENTERPRISE) { preg_match('/###[\d] [\w]* (\d*)/', $invoice_item->notes, $matches); $numUsers = $matches[1]; } else { $numUsers = 1; } - } elseif ($invoice_item->product_key == 'Pending Monthly') { - $pending_monthly = true; } } if (!empty($plan)) { $account = Account::with('users')->find($invoice->client->public_id); + $company = $account->company; if( - $account->company->plan != $plan + $company->plan != $plan || DateTime::createFromFormat('Y-m-d', $account->company->plan_expires) >= date_create('-7 days') ) { // Either this is a different plan, or the subscription expired more than a week ago // Reset any grandfathering - $account->company->plan_started = date_create()->format('Y-m-d'); + $company->plan_started = date_create()->format('Y-m-d'); } if ( - $account->company->plan == $plan - && $account->company->plan_term == $term - && DateTime::createFromFormat('Y-m-d', $account->company->plan_expires) >= date_create() + $company->plan == $plan + && $company->plan_term == $term + && DateTime::createFromFormat('Y-m-d', $company->plan_expires) >= date_create() ) { // This is a renewal; mark it paid as of when this term expires - $account->company->plan_paid = $account->company->plan_expires; + $company->plan_paid = $company->plan_expires; } else { - $account->company->plan_paid = date_create()->format('Y-m-d'); + $company->plan_paid = date_create()->format('Y-m-d'); } - $account->company->payment_id = $payment->id; - $account->company->plan = $plan; - $account->company->plan_term = $term; - $account->company->plan_price = $payment->amount; - $account->company->num_users = $numUsers; - $account->company->plan_expires = DateTime::createFromFormat('Y-m-d', $account->company->plan_paid) + $company->payment_id = $payment->id; + $company->plan = $plan; + $company->plan_term = $term; + $company->plan_price = $price; + $company->num_users = $numUsers; + $company->plan_expires = DateTime::createFromFormat('Y-m-d', $account->company->plan_paid) ->modify($term == PLAN_TERM_MONTHLY ? '+1 month' : '+1 year')->format('Y-m-d'); - if (!empty($pending_monthly)) { - $account->company->pending_plan = $plan; - $account->company->pending_term = PLAN_TERM_MONTHLY; - } else { - $account->company->pending_plan = null; - $account->company->pending_term = null; - } - - $account->company->save(); + $company->save(); } } diff --git a/app/Ninja/Repositories/AccountRepository.php b/app/Ninja/Repositories/AccountRepository.php index 0d461d08b4b1..8de9ee19a337 100644 --- a/app/Ninja/Repositories/AccountRepository.php +++ b/app/Ninja/Repositories/AccountRepository.php @@ -229,16 +229,16 @@ class AccountRepository return $data; } - public function enablePlan($plan, $credit = 0, $pending_monthly = false) + public function enablePlan($plan, $credit = 0) { $account = Auth::user()->account; $client = $this->getNinjaClient($account); - $invitation = $this->createNinjaInvoice($client, $account, $plan, $credit, $pending_monthly); + $invitation = $this->createNinjaInvoice($client, $account, $plan, $credit); return $invitation; } - public function createNinjaInvoice($client, $clientAccount, $plan, $credit = 0, $pending_monthly = false) + public function createNinjaInvoice($client, $clientAccount, $plan, $credit = 0) { $term = $plan['term']; $plan_cost = $plan['price']; @@ -285,19 +285,6 @@ class AccountRepository $item->product_key = 'Plan - '.ucfirst($plan).' ('.ucfirst($term).')'; $invoice->invoice_items()->save($item); - if ($pending_monthly) { - $term_end = $term == PLAN_MONTHLY ? date_create('+1 month') : date_create('+1 year'); - $pending_monthly_item = InvoiceItem::createNew($invoice); - $item->qty = 1; - $pending_monthly_item->cost = 0; - $pending_monthly_item->notes = trans('texts.plan_pending_monthly', ['date', Utils::dateToString($term_end)]); - - // Don't change this without updating the text in PaymentService->createPayment() - $pending_monthly_item->product_key = 'Pending Monthly'; - $invoice->invoice_items()->save($pending_monthly_item); - } - - $invitation = new Invitation(); $invitation->account_id = $account->id; $invitation->user_id = $account->users()->first()->id; diff --git a/resources/views/accounts/management.blade.php b/resources/views/accounts/management.blade.php index 1e9a25b5fe01..1f6669d15c61 100644 --- a/resources/views/accounts/management.blade.php +++ b/resources/views/accounts/management.blade.php @@ -38,11 +38,7 @@ @if ($planDetails && $planDetails['active'])

@@ -54,28 +50,6 @@

- @if ($account->company->pending_plan) -
- -
-

- @if ($account->company->pending_plan == PLAN_FREE) - {{ trans('texts.plan_changes_to', [ - 'plan'=>trans('texts.plan_free'), - 'date'=>Utils::dateToString($planDetails['expires']) - ]) }} - @else - {{ trans('texts.plan_term_changes_to', [ - 'plan'=>trans('texts.plan_'.$account->company->pending_plan), - 'term'=>trans('texts.plan_term_'.$account->company->pending_term.'ly'), - 'date'=>Utils::dateToString($planDetails['expires']) - ]) }} - @endif
- {{ trans('texts.cancel_plan_change') }} -

-
-
- @endif @if (Utils::isNinjaProd()) {!! Former::actions( Button::info(trans('texts.plan_change'))->large()->withAttributes(['onclick' => 'showChangePlan()'])->appendIcon(Icon::create('edit'))) !!} @endif @@ -221,15 +195,6 @@ } } - @if ($account->company->pending_plan) - function cancelPendingChange(){ - $('#plan').val('{{ $planDetails['plan'] }}') - $('#plan_term').val('{{ $planDetails['term'] }}') - confirmChangePlan(); - return false; - } - @endif - jQuery(document).ready(function($){ function updatePlanModal() { var plan = $('#plan').val();