lk.zachem.info/app/Modules/Main/Helpers/helper.php

104 lines
3.4 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;
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('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();
}
}