Working on new pricing

This commit is contained in:
Hillel Coren 2016-07-14 22:58:48 +03:00
parent d2d70eb12a
commit 431f596d1d
2 changed files with 36 additions and 18 deletions

View File

@ -183,13 +183,6 @@ class AccountController extends BaseController
$refund_deadline->modify('+30 days'); $refund_deadline->modify('+30 days');
if ($plan == PLAN_FREE && $refund_deadline >= date_create()) { 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) { if ($payment = $account->company->payment) {
$ninjaAccount = $this->accountRepo->getNinjaAccount(); $ninjaAccount = $this->accountRepo->getNinjaAccount();
$paymentDriver = $ninjaAccount->paymentDriver(); $paymentDriver = $ninjaAccount->paymentDriver();
@ -199,8 +192,6 @@ class AccountController extends BaseController
} else { } else {
Session::flash('message', trans('texts.updated_plan')); 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); return Redirect::to('view/' . $invitation->invitation_key);
} else { } else {
$company->plan = $plan; // create a credit
$company->plan_term = $term; $credit = $credit - $newPlan['price'];
$company->plan_price = $newPlan['price']; if ($credit > 0) {
$company->num_users = $numUsers; $client = $this->accountRepo->getNinjaClient($account, $credit);
$company->plan_expires = DateTime::createFromFormat('Y-m-d', $company->plan_paid)->modify($term == PLAN_TERM_MONTHLY ? '+1 month' : '+1 year')->format('Y-m-d'); $this->accountRepo->createNinjaCredit();
}
// TODO change plan if ($plan != PLAN_FREE) {
var_dump($newPlan); $company->plan_term = $term;
var_dump($credit); $company->plan_price = $newPlan['price'];
dd('0'); $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');
} }
} }

View File

@ -13,6 +13,7 @@ use App\Models\Invitation;
use App\Models\Invoice; use App\Models\Invoice;
use App\Models\InvoiceItem; use App\Models\InvoiceItem;
use App\Models\Client; use App\Models\Client;
use App\Models\Credit;
use App\Models\Language; use App\Models\Language;
use App\Models\Contact; use App\Models\Contact;
use App\Models\Account; use App\Models\Account;
@ -238,6 +239,24 @@ class AccountRepository
return $invitation; 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) public function createNinjaInvoice($client, $clientAccount, $plan, $credit = 0)
{ {
$term = $plan['term']; $term = $plan['term'];