Working on new pricing

This commit is contained in:
Hillel Coren 2016-07-14 12:46:00 +03:00
parent c10fd83c74
commit 8590f9ad17
6 changed files with 107 additions and 79 deletions

View File

@ -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)) {

View File

@ -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

View File

@ -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;
}

View File

@ -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();
}
}

View File

@ -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;

View File

@ -38,11 +38,7 @@
@if ($planDetails && $planDetails['active'])
<div class="form-group">
<label class="col-sm-4 control-label">
@if((!$account->company->pending_plan || $account->company->pending_plan == $planDetails['plan']) && $planDetails['expires'] && !$planDetails['trial'])
{{ trans('texts.renews') }}
@else
{{ trans('texts.expires') }}
@endif
{{ trans('texts.renews') }}
</label>
<div class="col-sm-8">
<p class="form-control-static">
@ -54,28 +50,6 @@
</p>
</div>
</div>
@if ($account->company->pending_plan)
<div class="form-group">
<label class="col-sm-4 control-label">{{ trans('texts.pending_change_to') }}</label>
<div class="col-sm-8">
<p class="form-control-static">
@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<br>
<a href="#" onclick="cancelPendingChange()">{{ trans('texts.cancel_plan_change') }}</a>
</p>
</div>
</div>
@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();