Simplified reseller setup

This commit is contained in:
Hillel Coren 2016-05-13 12:23:13 +03:00
parent fba37171ae
commit f873998e22

View File

@ -28,7 +28,7 @@ class AccountRepository
{ {
$company = new Company(); $company = new Company();
$company->save(); $company->save();
$account = new Account(); $account = new Account();
$account->ip = Request::getClientIp(); $account->ip = Request::getClientIp();
$account->account_key = str_random(RANDOM_KEY_LENGTH); $account->account_key = str_random(RANDOM_KEY_LENGTH);
@ -87,7 +87,7 @@ class AccountRepository
private function getAccountSearchData($user) private function getAccountSearchData($user)
{ {
$account = $user->account; $account = $user->account;
$data = [ $data = [
'clients' => [], 'clients' => [],
'contacts' => [], 'contacts' => [],
@ -102,7 +102,7 @@ class AccountRepository
if ($account->custom_client_label2) { if ($account->custom_client_label2) {
$data[$account->custom_client_label2] = []; $data[$account->custom_client_label2] = [];
} }
if ($user->hasPermission('view_all')) { if ($user->hasPermission('view_all')) {
$clients = Client::scope() $clients = Client::scope()
->with('contacts', 'invoices') ->with('contacts', 'invoices')
@ -114,7 +114,7 @@ class AccountRepository
$query->where('user_id', '=', $user->id); $query->where('user_id', '=', $user->id);
}])->get(); }])->get();
} }
foreach ($clients as $client) { foreach ($clients as $client) {
if ($client->name) { if ($client->name) {
$data['clients'][] = [ $data['clients'][] = [
@ -122,20 +122,20 @@ class AccountRepository
'tokens' => $client->name, 'tokens' => $client->name,
'url' => $client->present()->url, 'url' => $client->present()->url,
]; ];
} }
if ($client->custom_value1) { if ($client->custom_value1) {
$data[$account->custom_client_label1][] = [ $data[$account->custom_client_label1][] = [
'value' => "{$client->custom_value1}: " . $client->getDisplayName(), 'value' => "{$client->custom_value1}: " . $client->getDisplayName(),
'tokens' => $client->custom_value1, 'tokens' => $client->custom_value1,
'url' => $client->present()->url, 'url' => $client->present()->url,
]; ];
} }
if ($client->custom_value2) { if ($client->custom_value2) {
$data[$account->custom_client_label2][] = [ $data[$account->custom_client_label2][] = [
'value' => "{$client->custom_value2}: " . $client->getDisplayName(), 'value' => "{$client->custom_value2}: " . $client->getDisplayName(),
'tokens' => $client->custom_value2, 'tokens' => $client->custom_value2,
'url' => $client->present()->url, 'url' => $client->present()->url,
]; ];
} }
@ -242,9 +242,9 @@ class AccountRepository
if ($credit < 0) { if ($credit < 0) {
$credit = 0; $credit = 0;
} }
$plan_cost = Account::$plan_prices[$plan][$term]; $plan_cost = Account::$plan_prices[$plan][$term];
$account = $this->getNinjaAccount(); $account = $this->getNinjaAccount();
$lastInvoice = Invoice::withTrashed()->whereAccountId($account->id)->orderBy('public_id', 'DESC')->first(); $lastInvoice = Invoice::withTrashed()->whereAccountId($account->id)->orderBy('public_id', 'DESC')->first();
$publicId = $lastInvoice ? ($lastInvoice->public_id + 1) : 1; $publicId = $lastInvoice ? ($lastInvoice->public_id + 1) : 1;
@ -266,28 +266,28 @@ class AccountRepository
$credit_item->product_key = trans('texts.plan_credit_product'); $credit_item->product_key = trans('texts.plan_credit_product');
$invoice->invoice_items()->save($credit_item); $invoice->invoice_items()->save($credit_item);
} }
$item = InvoiceItem::createNew($invoice); $item = InvoiceItem::createNew($invoice);
$item->qty = 1; $item->qty = 1;
$item->cost = $plan_cost; $item->cost = $plan_cost;
$item->notes = trans("texts.{$plan}_plan_{$term}_description"); $item->notes = trans("texts.{$plan}_plan_{$term}_description");
// Don't change this without updating the regex in PaymentService->createPayment() // Don't change this without updating the regex in PaymentService->createPayment()
$item->product_key = 'Plan - '.ucfirst($plan).' ('.ucfirst($term).')'; $item->product_key = 'Plan - '.ucfirst($plan).' ('.ucfirst($term).')';
$invoice->invoice_items()->save($item); $invoice->invoice_items()->save($item);
if ($pending_monthly) { if ($pending_monthly) {
$term_end = $term == PLAN_MONTHLY ? date_create('+1 month') : date_create('+1 year'); $term_end = $term == PLAN_MONTHLY ? date_create('+1 month') : date_create('+1 year');
$pending_monthly_item = InvoiceItem::createNew($invoice); $pending_monthly_item = InvoiceItem::createNew($invoice);
$item->qty = 1; $item->qty = 1;
$pending_monthly_item->cost = 0; $pending_monthly_item->cost = 0;
$pending_monthly_item->notes = trans("texts.plan_pending_monthly", array('date', Utils::dateToString($term_end))); $pending_monthly_item->notes = trans("texts.plan_pending_monthly", array('date', Utils::dateToString($term_end)));
// Don't change this without updating the text in PaymentService->createPayment() // Don't change this without updating the text in PaymentService->createPayment()
$pending_monthly_item->product_key = 'Pending Monthly'; $pending_monthly_item->product_key = 'Pending Monthly';
$invoice->invoice_items()->save($pending_monthly_item); $invoice->invoice_items()->save($pending_monthly_item);
} }
$invitation = new Invitation(); $invitation = new Invitation();
$invitation->account_id = $account->id; $invitation->account_id = $account->id;
@ -328,12 +328,14 @@ class AccountRepository
$user->notify_paid = true; $user->notify_paid = true;
$account->users()->save($user); $account->users()->save($user);
$accountGateway = new AccountGateway(); if ($config = env(NINJA_GATEWAY_CONFIG)) {
$accountGateway->user_id = $user->id; $accountGateway = new AccountGateway();
$accountGateway->gateway_id = NINJA_GATEWAY_ID; $accountGateway->user_id = $user->id;
$accountGateway->public_id = 1; $accountGateway->gateway_id = NINJA_GATEWAY_ID;
$accountGateway->setConfig(json_decode(env(NINJA_GATEWAY_CONFIG))); $accountGateway->public_id = 1;
$account->account_gateways()->save($accountGateway); $accountGateway->setConfig(json_decode($config));
$account->account_gateways()->save($accountGateway);
}
} }
return $account; return $account;
@ -356,11 +358,11 @@ class AccountRepository
$client->user_id = $ninjaUser->id; $client->user_id = $ninjaUser->id;
$client->currency_id = 1; $client->currency_id = 1;
} }
foreach (['name', 'address1', 'address2', 'city', 'state', 'postal_code', 'country_id', 'work_phone', 'language_id', 'vat_number'] as $field) { foreach (['name', 'address1', 'address2', 'city', 'state', 'postal_code', 'country_id', 'work_phone', 'language_id', 'vat_number'] as $field) {
$client->$field = $account->$field; $client->$field = $account->$field;
} }
$client->save(); $client->save();
if ($clientExists) { if ($clientExists) {
@ -372,7 +374,7 @@ class AccountRepository
$contact->public_id = $account->id; $contact->public_id = $account->id;
$contact->is_primary = true; $contact->is_primary = true;
} }
$user = $account->getPrimaryUser(); $user = $account->getPrimaryUser();
foreach (['first_name', 'last_name', 'email', 'phone'] as $field) { foreach (['first_name', 'last_name', 'email', 'phone'] as $field) {
$contact->$field = $user->$field; $contact->$field = $user->$field;
@ -513,7 +515,7 @@ class AccountRepository
if ($with) { if ($with) {
$users->with($with); $users->with($with);
} }
return $users->get(); return $users->get();
} }
@ -565,7 +567,7 @@ class AccountRepository
$record->save(); $record->save();
$users = $this->getUserAccounts($record); $users = $this->getUserAccounts($record);
// Pick the primary user // Pick the primary user
foreach ($users as $user) { foreach ($users as $user) {
if (!$user->public_id) { if (!$user->public_id) {
@ -573,16 +575,16 @@ class AccountRepository
if(empty($primaryUser)) { if(empty($primaryUser)) {
$useAsPrimary = true; $useAsPrimary = true;
} }
$planDetails = $user->account->getPlanDetails(false, false); $planDetails = $user->account->getPlanDetails(false, false);
$planLevel = 0; $planLevel = 0;
if ($planDetails) { if ($planDetails) {
$planLevel = 1; $planLevel = 1;
if ($planDetails['plan'] == PLAN_ENTERPRISE) { if ($planDetails['plan'] == PLAN_ENTERPRISE) {
$planLevel = 2; $planLevel = 2;
} }
if (!$useAsPrimary && ( if (!$useAsPrimary && (
$planLevel > $primaryUserPlanLevel $planLevel > $primaryUserPlanLevel
|| ($planLevel == $primaryUserPlanLevel && $planDetails['expires'] > $primaryUserPlanExpires) || ($planLevel == $primaryUserPlanLevel && $planDetails['expires'] > $primaryUserPlanExpires)
@ -590,7 +592,7 @@ class AccountRepository
$useAsPrimary = true; $useAsPrimary = true;
} }
} }
if ($useAsPrimary) { if ($useAsPrimary) {
$primaryUser = $user; $primaryUser = $user;
$primaryUserPlanLevel = $planLevel; $primaryUserPlanLevel = $planLevel;
@ -600,14 +602,14 @@ class AccountRepository
} }
} }
} }
// Merge other companies into the primary user's company // Merge other companies into the primary user's company
if (!empty($primaryUser)) { if (!empty($primaryUser)) {
foreach ($users as $user) { foreach ($users as $user) {
if ($user == $primaryUser || $user->public_id) { if ($user == $primaryUser || $user->public_id) {
continue; continue;
} }
if ($user->account->company_id != $primaryUser->account->company_id) { if ($user->account->company_id != $primaryUser->account->company_id) {
foreach ($user->account->company->accounts as $account) { foreach ($user->account->company->accounts as $account) {
$account->company_id = $primaryUser->account->company_id; $account->company_id = $primaryUser->account->company_id;
@ -636,9 +638,9 @@ class AccountRepository
$userAccount->removeUserId($userId); $userAccount->removeUserId($userId);
$userAccount->save(); $userAccount->save();
} }
$user = User::whereId($userId)->first(); $user = User::whereId($userId)->first();
if (!$user->public_id && $user->account->company->accounts->count() > 1) { if (!$user->public_id && $user->account->company->accounts->count() > 1) {
$company = Company::create(); $company = Company::create();
$company->save(); $company->save();
@ -660,7 +662,7 @@ class AccountRepository
->withTrashed() ->withTrashed()
->first(); ->first();
} while ($match); } while ($match);
return $code; return $code;
} }
@ -668,7 +670,7 @@ class AccountRepository
{ {
$name = trim($name) ?: 'TOKEN'; $name = trim($name) ?: 'TOKEN';
$users = $this->findUsers($user); $users = $this->findUsers($user);
foreach ($users as $user) { foreach ($users as $user) {
if ($token = AccountToken::whereUserId($user->id)->whereName($name)->first()) { if ($token = AccountToken::whereUserId($user->id)->whereName($name)->first()) {
continue; continue;