-
-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ability to tie locations to companies
Locations are the last big part of the application that can't be tied to companies. This can be a problem with FullMultipleCompanySupport, because you can't restrict the visibility of locations to the company of the users. In order to change this, add a company_id to the locations table and wire everything up in the views and controllers. Aditionally add a new formatter to filter the locations to a specific company, like it is done for assets. Locations are properly scoped to the users company if FullMultipleCompanySupport is enabled. If a parent location of a location has a different company than the user, the location does not show up.
- Loading branch information
Showing
12 changed files
with
166 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,11 +33,13 @@ class Location extends SnipeModel | |
'zip' => 'min:3|max:10|nullable', | ||
'manager_id' => 'exists:users,id|nullable', | ||
'parent_id' => 'non_circular:locations,id', | ||
'company_id' => 'integer|nullable' | ||
]; | ||
|
||
protected $casts = [ | ||
'parent_id' => 'integer', | ||
'manager_id' => 'integer', | ||
'company_id' => 'integer', | ||
]; | ||
|
||
/** | ||
|
@@ -69,6 +71,7 @@ class Location extends SnipeModel | |
'currency', | ||
'manager_id', | ||
'image', | ||
'company_id', | ||
]; | ||
protected $hidden = ['user_id']; | ||
|
||
|
@@ -87,7 +90,8 @@ class Location extends SnipeModel | |
* @var array | ||
*/ | ||
protected $searchableRelations = [ | ||
'parent' => ['name'], | ||
'parent' => ['name'], | ||
'company' => ['name'] | ||
]; | ||
|
||
|
||
|
@@ -205,6 +209,17 @@ public function parent() | |
->with('parent'); | ||
} | ||
|
||
/** | ||
* Establishes the locations -> company relationship | ||
* | ||
* @author [T. Regnery] [<[email protected]>] | ||
* @since [v6.0] | ||
* @return \Illuminate\Database\Eloquent\Relations\Relation | ||
*/ | ||
public function company() | ||
{ | ||
return $this->belongsTo('\App\Models\Company', 'company_id'); | ||
} | ||
|
||
/** | ||
* Find the manager of a location | ||
|
@@ -304,4 +319,17 @@ public function scopeOrderManager($query, $order) | |
{ | ||
return $query->leftJoin('users as location_user', 'locations.manager_id', '=', 'location_user.id')->orderBy('location_user.first_name', $order)->orderBy('location_user.last_name', $order); | ||
} | ||
|
||
/** | ||
* Query builder scope to order on company | ||
* | ||
* @param \Illuminate\Database\Query\Builder $query Query builder instance | ||
* @param text $order Order | ||
* | ||
* @return \Illuminate\Database\Query\Builder Modified query builder | ||
*/ | ||
public function scopeOrderCompany($query, $order) | ||
{ | ||
return $query->leftJoin('companies as company_sort', 'locations.company_id', '=', 'company_sort.id')->orderBy('company_sort.name', $order); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
database/migrations/2022_02_10_110210_add_company_id_to_locations.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?php | ||
|
||
use Illuminate\Database\Migrations\Migration; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Support\Facades\Schema; | ||
|
||
class AddCompanyIdToLocations extends Migration | ||
{ | ||
/** | ||
* Run the migrations. | ||
* | ||
* @return void | ||
*/ | ||
public function up() | ||
{ | ||
Schema::table('locations', function (Blueprint $table) { | ||
$table->integer('company_id')->unsigned()->nullable(); | ||
$table->index(['company_id']); | ||
}); | ||
} | ||
|
||
/** | ||
* Reverse the migrations. | ||
* | ||
* @return void | ||
*/ | ||
public function down() | ||
{ | ||
Schema::table('locations', function (Blueprint $table) { | ||
$table->dropIndex(['company_id']); | ||
$table->dropColumn('company_id'); | ||
}); | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.