137 lines
4.7 KiB
PHP
137 lines
4.7 KiB
PHP
<?php
|
|
use Modules\Contracts\Models\ContractStatus;
|
|
|
|
use Modules\Main\Models\Company\Company;
|
|
use Modules\Main\Models\Company\CompanyAdmin;
|
|
use Modules\CityManager\Models\CityManager;
|
|
use Modules\Main\Models\Agent\Agent;
|
|
use Modules\Main\Models\Complex;
|
|
use Modules\User\Models\Role;
|
|
use Modules\User\Models\UserRole;
|
|
|
|
if (!function_exists('AdminCompanyOfUser')) {
|
|
function AdminCompanyOfUser()
|
|
{
|
|
if ($adminAccount = CompanyAdmin::where('user_id', auth()->user()->id)->first()) {
|
|
return $adminAccount->company;
|
|
} else {
|
|
return false;
|
|
}
|
|
}
|
|
}
|
|
|
|
if (!function_exists(function: 'AgentCompanyOfUser')) {
|
|
function AgentCompanyOfUser()
|
|
{
|
|
$city = false;
|
|
if ($adminAccount = Agent::where('user_id', auth()->user()->id)->first()) {
|
|
return $adminAccount->company;
|
|
} else {
|
|
return false;
|
|
}
|
|
}
|
|
}
|
|
|
|
if (!function_exists('GetAvailableAgents')) {
|
|
function GetAvailableAgents($resultType = 'Collection')
|
|
{
|
|
$agents = false;
|
|
if ($adminCompany = AdminCompanyOfUser()) {
|
|
$agents = Agent::where('company_id', $adminCompany->id);
|
|
} else {
|
|
$cityManager = CityManager::where('user_id', auth()->user()->id);
|
|
if ($cityManager->count()) {
|
|
$companies = Company::whereIn('city_id', $cityManager->pluck('city_id'));
|
|
if ($companies->count()) {
|
|
$agents = Agent::whereIn('company_id', $companies->get()->pluck('id'));
|
|
}
|
|
} else {
|
|
$agents = Agent::where('user_id', auth()->user()->id);
|
|
}
|
|
}
|
|
if ($agents === false) {
|
|
$agents = Agent::where('id', 0);
|
|
}
|
|
$agents->with('company:id,name');
|
|
$agents->with('user:id,name');
|
|
if ($resultType == 'Collection') {
|
|
return $agents->get();
|
|
} else {
|
|
return $agents;
|
|
};
|
|
}
|
|
}
|
|
|
|
if (!function_exists('GetAvailableCompanies')) {
|
|
function GetAvailableCompanies($resultType = 'Collection')
|
|
{
|
|
$companiesIds = [];
|
|
if (UserRole::where('user_id', auth()->user()->id)->where('role_id', Role::SUPER_ADMIN)->count() == 1) {
|
|
$companies = Company::all()->pluck('id');
|
|
$companiesIds = array_merge($companiesIds, $companies->all());
|
|
} else {
|
|
if ($adminCompany = AdminCompanyOfUser()) {
|
|
$companiesIds[] = $adminCompany->id;
|
|
}
|
|
if ($cityManager = CityManager::where('user_id', auth()->user()->id)) {
|
|
if ($cityManager->count()) {
|
|
$companies = Company::whereIn('city_id', $cityManager->pluck('city_id'));
|
|
if ($companies->count()) {
|
|
$companies = $companies->get()->pluck('id');
|
|
$companiesIds = array_merge($companiesIds, $companies->all());
|
|
}
|
|
}
|
|
}
|
|
}
|
|
$companiesIds = array_unique($companiesIds);
|
|
$companies = Company::whereIn('id', $companiesIds);
|
|
if ($resultType == 'Collection') {
|
|
return $companies->get();
|
|
} else {
|
|
return $companies;
|
|
};
|
|
}
|
|
}
|
|
|
|
if (!function_exists('GetAvailableComplexes')) {
|
|
function GetAvailableComplexes()
|
|
{
|
|
$complexes = false;
|
|
if ($adminCompany = AdminCompanyOfUser()) {
|
|
$complexes = Complex::where('city_id', $adminCompany->city_id);
|
|
} else {
|
|
$cityManager = CityManager::where('user_id', auth()->user()->id);
|
|
if ($cityManager->count()) {
|
|
$complexes = Complex::where('city_id', $cityManager->first()->city_id);
|
|
} else {
|
|
if ($agent = Agent::where('user_id', auth()->user()->id)->first()) {
|
|
$complexes = Complex::where('city_id', $agent->company->city_id);
|
|
}
|
|
}
|
|
}
|
|
if ($complexes) {
|
|
return $complexes->get();
|
|
}
|
|
return [];
|
|
}
|
|
}
|
|
|
|
if (!function_exists('GetAvailableCities')) {
|
|
function GetAvailableCities()
|
|
{
|
|
$agents = [];
|
|
if ($adminCompany = AdminCompanyOfUser()) {
|
|
$agents = Agent::where('company_id', $adminCompany->id);
|
|
} else {
|
|
$cityManager = CityManager::where('user_id', auth()->user()->id);
|
|
if ($cityManager->count()) {
|
|
$agents = Agent::whereIn('company_id', Company::where('city_id', $cityManager->city_id)->get()->pluck('company_id'));
|
|
} else {
|
|
$agents = Agent::where('user_id', auth()->user()->id);
|
|
}
|
|
}
|
|
$agents->with('company:id,name');
|
|
$agents->with('user:id,name');
|
|
return $agents->get();
|
|
}
|
|
} |