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

117 lines
3.0 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()
{
$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()->toArray();
}
}
if (!function_exists('GetAvailableComplexes'))
{
function GetAvailableComplexes()
{
$complexes = false;
if ($adminCompany = AdminCompanyOfUser())
{
$complexes = Complex::where('city_id', $adminCompany->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()->toArray();
}
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()->toArray();
}
}