From 030d4ed4da5f31e7e8d4a71fb4493f0ee237fc3b Mon Sep 17 00:00:00 2001 From: Joshua Dwire Date: Sat, 16 Apr 2016 22:09:01 -0400 Subject: [PATCH] Fix renewals; fix plan edge cases --- app/Console/Commands/SendRenewalInvoices.php | 36 ++++++++++++-------- app/Http/Controllers/AccountController.php | 14 ++++++-- app/Models/Account.php | 4 +-- app/Services/PaymentService.php | 29 +++++++++++++--- 4 files changed, 61 insertions(+), 22 deletions(-) diff --git a/app/Console/Commands/SendRenewalInvoices.php b/app/Console/Commands/SendRenewalInvoices.php index 0faf1304a964..64db2fad4fb1 100644 --- a/app/Console/Commands/SendRenewalInvoices.php +++ b/app/Console/Commands/SendRenewalInvoices.php @@ -5,7 +5,7 @@ use DateTime; use Illuminate\Console\Command; use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Input\InputArgument; -use App\Models\Account; +use App\Models\Company; use App\Ninja\Mailers\ContactMailer as Mailer; use App\Ninja\Repositories\AccountRepository; @@ -30,24 +30,32 @@ class SendRenewalInvoices extends Command $today = new DateTime(); $sentTo = []; - // get all accounts with pro plans expiring in 10 days - $accounts = Account::whereRaw('datediff(curdate(), pro_plan_paid) = 355') + // get all accounts with plans expiring in 10 days + $companies = Company::whereRaw('datediff(plan_expires, curdate()) = 10') ->orderBy('id') ->get(); - $this->info(count($accounts).' accounts found'); + $this->info(count($companies).' companies found'); - foreach ($accounts as $account) { - // don't send multiple invoices to multi-company users - if ($userAccountId = $this->accountRepo->getUserAccountId($account)) { - if (isset($sentTo[$userAccountId])) { - continue; - } else { - $sentTo[$userAccountId] = true; - } + foreach ($companies as $company) { + if (!count($company->accounts)) { + continue; } - + + $account = $company->accounts->sortBy('id')->first(); + $plan = $company->plan; + $term = $company->plan_term; + + if ($company->pending_plan) { + $plan = $company->pending_plan; + $term = $company->pending_term; + } + + if ($plan == PLAN_FREE || !$plan || !$term ){ + continue; + } + $client = $this->accountRepo->getNinjaClient($account); - $invitation = $this->accountRepo->createNinjaInvoice($client, $account); + $invitation = $this->accountRepo->createNinjaInvoice($client, $account, $plan, $term); // set the due date to 10 days from now $invoice = $invitation->invoice; diff --git a/app/Http/Controllers/AccountController.php b/app/Http/Controllers/AccountController.php index 0845d9a572ae..c14193308a2c 100644 --- a/app/Http/Controllers/AccountController.php +++ b/app/Http/Controllers/AccountController.php @@ -170,7 +170,7 @@ class AccountController extends BaseController $refund_deadline = clone $planDetails['started']; $refund_deadline->modify('+30 days'); - if ($plan == PLAN_FREE && $refund_deadline > date_create()) { + if ($plan == PLAN_FREE && $refund_deadline >= date_create()) { // Refund $account->company->plan = null; $account->company->plan_term = null; @@ -207,7 +207,17 @@ class AccountController extends BaseController } if (!empty($new_plan)) { - $percent_used = $planDetails['paid']->diff(date_create())->days / $planDetails['paid']->diff($planDetails['expires'])->days; + $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; $old_plan_price = Account::$plan_prices[$planDetails['plan']][$planDetails['term']]; $credit = $old_plan_price * (1 - $percent_used); } diff --git a/app/Models/Account.php b/app/Models/Account.php index 5bd40b5637fe..852da13efec3 100644 --- a/app/Models/Account.php +++ b/app/Models/Account.php @@ -843,7 +843,7 @@ class Account extends Eloquent $trial_expires = clone $trial_started; $trial_expires->modify('+2 weeks'); - if ($trial_expires > date_create()) { + if ($trial_expires >= date_create()) { $trial_active = true; } } @@ -855,7 +855,7 @@ class Account extends Eloquent $plan_expires = false; } else { $plan_expires = DateTime::createFromFormat('Y-m-d', $this->company->plan_expires); - if ($plan_expires > date_create()) { + if ($plan_expires >= date_create()) { $plan_active = true; } } diff --git a/app/Services/PaymentService.php b/app/Services/PaymentService.php index 76e09256d2b4..2f4b57f11ff0 100644 --- a/app/Services/PaymentService.php +++ b/app/Services/PaymentService.php @@ -244,14 +244,35 @@ class PaymentService extends BaseService } } - if (!empty($plan)) { + if (!empty($plan)) { $account = Account::with('users')->find($invoice->client->public_id); + + if( + $account->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'); + } + + if ( + $account->company->plan == $plan + && $account->company->plan_term == $term + && DateTime::createFromFormat('Y-m-d', $account->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; + } else { + $account->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_paid = $account->company->plan_started = date_create()->format('Y-m-d'); - $account->company->plan_expires = date_create($term == PLAN_TERM_MONTHLY ? '+1 month' : '+1 year')->format('Y-m-d'); - + $account->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;