2024-08-29 06:55:18 -07:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Rules;
|
|
|
|
use App\Models\Setting;
|
|
|
|
use App\Models\User;
|
|
|
|
use Closure;
|
|
|
|
use Illuminate\Contracts\Validation\ValidationRule;
|
|
|
|
|
|
|
|
class UserCannotSwitchCompaniesIfItemsAssigned implements ValidationRule
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Run the validation rule.
|
|
|
|
*
|
|
|
|
* @param \Closure(string): \Illuminate\Translation\PotentiallyTranslatedString $fail
|
|
|
|
*/
|
|
|
|
public function validate(string $attribute, mixed $value, Closure $fail): void
|
|
|
|
{
|
|
|
|
$user = User::find(request()->route('user')->id);
|
2024-09-17 07:46:45 -07:00
|
|
|
|
|
|
|
if (($value) && ($user->allAssignedCount() > 0) && (Setting::getSettings()->full_multiple_companies_support=='1')) {
|
|
|
|
|
|
|
|
// Check for assets with a different company_id than the selected company_id
|
|
|
|
$user_assets = $user->assets()->where('assets.company_id', '!=', $value)->count();
|
|
|
|
if ($user_assets > 0) {
|
|
|
|
$fail(trans('admin/users/message.error.multi_company_items_assigned'));
|
|
|
|
}
|
2024-08-29 06:55:18 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|