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');
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;
// create a credit
$credit = $credit - $newPlan['price'];
if ($credit > 0) {
$client = $this->accountRepo->getNinjaClient($account, $credit);
$this->accountRepo->createNinjaCredit();
}
if ($plan != PLAN_FREE) {
$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');
$company->plan_expires = date_create()->modify($term == PLAN_TERM_MONTHLY ? '+1 month' : '+1 year')->format('Y-m-d');
}
// TODO change plan
var_dump($newPlan);
var_dump($credit);
dd('0');
$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\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'];