87 lines
2.2 KiB
PHP
87 lines
2.2 KiB
PHP
<?php
|
|
|
|
namespace Modules\Contracts\Http\Livewire;
|
|
|
|
use Livewire\Component;
|
|
use App\Models\Company\Company;
|
|
use App\Models\Complex;
|
|
use App\Models\Agent\Agent;
|
|
use App\Models\Deal\Deal;
|
|
use App\Models\Deal\Contract;
|
|
use App\Models\Deal\ContractStatus;
|
|
use App\Models\User\UserRole;
|
|
use App\Models\User\Role;
|
|
use App\Models\Company\CompanyAdmin;
|
|
|
|
class ContractsTableLivewire extends Component
|
|
{
|
|
public $mode = false;
|
|
public $filter = [];
|
|
public function mount()
|
|
{
|
|
|
|
}
|
|
function getSelectingAgents()
|
|
{
|
|
if (
|
|
$userRole =
|
|
UserRole::where('user_id', auth()->id())
|
|
->where('role_id', Role::COMPANY_ADMIN)
|
|
->count() == 1
|
|
)
|
|
{
|
|
$companyAdmin = CompanyAdmin::where('user_id', auth()->id())->first();
|
|
return $agentsIds = Agent::where('company_id', $companyAdmin->company_id)->pluck('id');
|
|
}
|
|
else
|
|
{
|
|
$agent = Agent::where('user_id', auth()->id())->first();
|
|
return [$agent->id];
|
|
}
|
|
}
|
|
|
|
private function getContracts()
|
|
{
|
|
$contracts = Contract::whereHas('deal', function ($dealSubQuery)
|
|
{
|
|
$dealSubQuery->with('deals')
|
|
->whereIn('agent_id', $this->getSelectingAgents());
|
|
});
|
|
$this->appendMode($contracts);
|
|
$this->appendFilter($contracts);
|
|
|
|
return $contracts->get();
|
|
}
|
|
private function appendMode(&$query)
|
|
{
|
|
if ($this->mode == 'active')
|
|
{
|
|
$query->whereIn('status', [
|
|
ContractStatus::NEW ,
|
|
ContractStatus::RESERVATION,
|
|
ContractStatus::SUCCESS,
|
|
]);
|
|
}
|
|
if ($this->mode == 'finished')
|
|
{
|
|
$query->whereIn('status', [
|
|
ContractStatus::DECLINE,
|
|
ContractStatus::TREATY
|
|
]);
|
|
}
|
|
}
|
|
private function appendFilter(&$query)
|
|
{
|
|
foreach ($this->filter as $fKey => $fValue)
|
|
{
|
|
$query->where($fKey, $fValue);
|
|
}
|
|
}
|
|
public function render()
|
|
{
|
|
return view('contracts::livewire.table.index', [
|
|
'contracts' => $this->getContracts(),
|
|
'statuses' => ContractStatus::class
|
|
]);
|
|
}
|
|
} |