Fix invoice link if user has multiple companies #578

This commit is contained in:
Hillel Coren 2017-01-29 22:07:44 +02:00
parent d62a672999
commit 602c8e81e5
9 changed files with 55 additions and 35 deletions

View File

@ -1354,10 +1354,11 @@ class AccountController extends BaseController
});
$this->accountRepo->unlinkAccount($account);
if ($account->company->accounts->count() == 1) {
$account->company->forceDelete();
} else {
if ($account->hasMultipleAccounts()) {
$account->forceDelete();
} else {
$account->company->forceDelete();
}
Auth::logout();

View File

@ -172,7 +172,8 @@ class AuthController extends Controller
if (Auth::check() && !Auth::user()->registered) {
$account = Auth::user()->account;
$this->accountRepo->unlinkAccount($account);
if ($account->company->accounts->count() == 1) {
if ( ! $account->hasMultipleAccounts()) {
$account->company->forceDelete();
}
$account->forceDelete();

View File

@ -270,31 +270,6 @@ class UserController extends BaseController
}
}
/**
* Log the user out of the application.
*
*/
/*
public function logout()
{
if (Auth::check()) {
if (!Auth::user()->registered) {
$account = Auth::user()->account;
$this->accountRepo->unlinkAccount($account);
if ($account->company->accounts->count() == 1) {
$account->company->forceDelete();
}
$account->forceDelete();
}
}
Auth::logout();
Session::flush();
return Redirect::to('/')->with('clearGuestKey', true);
}
*/
public function changePassword()
{
// check the current password is correct
@ -347,6 +322,22 @@ class UserController extends BaseController
}
}
public function viewAccountByKey($accountKey)
{
$user = $this->accountRepo->findUser(Auth::user(), $accountKey);
if ( ! $user) {
return redirect()->to('/');
}
Auth::loginUsingId($user->id);
Auth::user()->account->loadLocalizationSettings();
$redirectTo = request()->redirect_to ?: '/';
return redirect()->to($redirectTo);
}
public function unlinkAccount($userAccountId, $userId)
{
$this->accountRepo->unlinkUser($userAccountId, $userId);

View File

@ -232,6 +232,7 @@ Route::group([
Route::post('users/bulk', 'UserController@bulk');
Route::get('send_confirmation/{user_id}', 'UserController@sendConfirmation');
Route::get('/switch_account/{user_id}', 'UserController@switchAccount');
Route::get('/account/{account_key}', 'UserController@viewAccountByKey');
Route::get('/unlink_account/{user_account_id}/{user_id}', 'UserController@unlinkAccount');
Route::get('/manage_companies', 'UserController@manageCompanies');

View File

@ -1732,6 +1732,11 @@ class Account extends Eloquent
return Carbon::now($this->getTimezone())->addDays($numDays)->format('Y-m-d');
}
public function hasMultipleAccounts()
{
return $this->company->accounts->count() > 1;
}
}
Account::updated(function ($account)

View File

@ -180,12 +180,10 @@ class InvoiceDatatable extends EntityDatatable
if ($this->entityType == ENTITY_INVOICE || $this->entityType == ENTITY_QUOTE) {
$actions[] = \DropdownButton::DIVIDER;
/*
$actions[] = [
'label' => mtrans($this->entityType, 'email_' . $this->entityType),
'url' => 'javascript:submitForm_'.$this->entityType.'("emailInvoice")',
];
*/
$actions[] = [
'label' => mtrans($this->entityType, 'mark_sent'),
'url' => 'javascript:submitForm_'.$this->entityType.'("markSent")',

View File

@ -59,6 +59,12 @@ class UserMailer extends Mailer
$account = $user->account;
$client = $invoice->client;
if ($account->hasMultipleAccounts()) {
$link = url(sprintf('/account/%s?redirect_to=%s', $account->account_key, $invoice->present()->path));
} else {
$link = $invoice->present()->url;
}
$data = [
'entityType' => $entityType,
'clientName' => $client->getDisplayName(),
@ -66,7 +72,7 @@ class UserMailer extends Mailer
'userName' => $user->getDisplayName(),
'invoiceAmount' => $account->formatMoney($invoice->getRequestedAmount(), $client),
'invoiceNumber' => $invoice->invoice_number,
'invoiceLink' => SITE_URL."/{$entityType}s/{$invoice->public_id}",
'invoiceLink' => $link,
'account' => $account,
];

View File

@ -10,12 +10,16 @@ class EntityPresenter extends Presenter
* @return string
*/
public function url()
{
return url($this->path());
}
public function path()
{
$type = Utils::pluralizeEntityType($this->entity->getEntityType());
$id = $this->entity->public_id;
$link = sprintf('/%s/%s', $type, $id);
return URL::to($link);
return sprintf('/%s/%s', $type, $id);
}
public function editUrl()

View File

@ -505,6 +505,19 @@ class AccountRepository
}
}
public function findUser($user, $accountKey)
{
$users = $this->findUsers($user, 'account');
foreach ($users as $user) {
if ($accountKey && hash_equals($user->account->account_key, $accountKey)) {
return $user;
}
}
return false;
}
public function findUserAccounts($userId1, $userId2 = false)
{
if (!Schema::hasTable('user_accounts')) {
@ -674,7 +687,7 @@ class AccountRepository
$user = User::whereId($userId)->first();
if (!$user->public_id && $user->account->company->accounts->count() > 1) {
if (!$user->public_id && $user->account->hasMultipleAccounts()) {
$company = Company::create();
$company->save();
$user->account->company_id = $company->id;