From 431f596d1d03384ec37c15aa9c10cf387961b352 Mon Sep 17 00:00:00 2001 From: Hillel Coren Date: Thu, 14 Jul 2016 22:58:48 +0300 Subject: [PATCH] Working on new pricing --- app/Http/Controllers/AccountController.php | 35 ++++++++++---------- app/Ninja/Repositories/AccountRepository.php | 19 +++++++++++ 2 files changed, 36 insertions(+), 18 deletions(-) diff --git a/app/Http/Controllers/AccountController.php b/app/Http/Controllers/AccountController.php index 844f904005ca..6a0308b81dc9 100644 --- a/app/Http/Controllers/AccountController.php +++ b/app/Http/Controllers/AccountController.php @@ -183,13 +183,6 @@ class AccountController extends BaseController $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(); @@ -199,8 +192,6 @@ class AccountController extends BaseController } else { Session::flash('message', trans('texts.updated_plan')); } - - $account->company->save(); } } @@ -223,16 +214,24 @@ class AccountController extends BaseController 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'); + // create a credit + $credit = $credit - $newPlan['price']; + if ($credit > 0) { + $client = $this->accountRepo->getNinjaClient($account, $credit); + $this->accountRepo->createNinjaCredit(); + } - // TODO change plan - var_dump($newPlan); - var_dump($credit); - dd('0'); + if ($plan != PLAN_FREE) { + $company->plan_term = $term; + $company->plan_price = $newPlan['price']; + $company->num_users = $numUsers; + $company->plan_expires = date_create()->modify($term == PLAN_TERM_MONTHLY ? '+1 month' : '+1 year')->format('Y-m-d'); + } + + $company->plan = $plan; + $company->save(); + + return Redirect::to('settings/account_management'); } } diff --git a/app/Ninja/Repositories/AccountRepository.php b/app/Ninja/Repositories/AccountRepository.php index 8de9ee19a337..41a6af5384de 100644 --- a/app/Ninja/Repositories/AccountRepository.php +++ b/app/Ninja/Repositories/AccountRepository.php @@ -13,6 +13,7 @@ use App\Models\Invitation; use App\Models\Invoice; use App\Models\InvoiceItem; use App\Models\Client; +use App\Models\Credit; use App\Models\Language; use App\Models\Contact; use App\Models\Account; @@ -238,6 +239,24 @@ class AccountRepository return $invitation; } + public function createNinjaCredit($client, $amount) + { + $account = $this->getNinjaAccount(); + + $lastCredit = Credit::withTrashed()->whereAccountId($account->id)->orderBy('public_id', 'DESC')->first(); + $publicId = $lastCredit ? ($lastCredit->public_id + 1) : 1; + + $credit = new Credit(); + $credit->public_id = $publicId; + $credit->account_id = $account->id; + $credit->user_id = $account->users()->first()->id; + $credit->client_id = $client->id; + $credit->amount = $amount; + $credit->save(); + + return $credit; + } + public function createNinjaInvoice($client, $clientAccount, $plan, $credit = 0) { $term = $plan['term'];