515 lines
23 KiB
PHP
Executable File
515 lines
23 KiB
PHP
Executable File
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Models\EquipmentWorkHistory;
|
|
use App\Models\Plant;
|
|
use App\Models\Unit;
|
|
use Illuminate\Http\Request;
|
|
use App\Models\CompanyProject;
|
|
use App\Models\ConstructionWorkstation;
|
|
use App\Models\ControlEquipmentWorkstation;
|
|
use App\Models\Equipment;
|
|
use App\Models\EquipmentComments;
|
|
use App\Models\EquipmentType;
|
|
|
|
|
|
use App\Models\ElementalTasks;
|
|
|
|
use Yajra\DataTables\Facades\DataTables;
|
|
|
|
class ExecutionProjectController extends Controller
|
|
{
|
|
public function getDataEquipmentClient(Request $request)
|
|
{
|
|
|
|
$clientId = $request->get('clientId');
|
|
|
|
// Recebe as plants com base no USer
|
|
$allPlantsClient = Plant::where('user_id', $clientId)->get();
|
|
|
|
// Extrai os plant_id da coleção $recevePlantClient
|
|
$plantsIds = $allPlantsClient->pluck('plant_id');
|
|
|
|
// Busca todas as fabricas referente a plantas recebidas, guardamos o id de cada fabrica em $unitsIds
|
|
$allUnitsClient = Unit::whereIn('plant_id', $plantsIds)->get();
|
|
$unitsIds = $allUnitsClient->pluck('unit_id');
|
|
|
|
$receiveAllPlants = $request->get('receiveAllPlants');
|
|
|
|
$receiveAllUnits = $request->get('receiveAllUnits');
|
|
|
|
$receiveEquipmentsType = $request->get('receiveEquipmentsType');
|
|
|
|
|
|
|
|
$query = Equipment::with('equipmentType', 'unit')
|
|
->whereIn('unit_id', $unitsIds)
|
|
->select(['equipment_id', 'ispt_number', 'equipment_tag', 'unit_id', 'equipment_type_id']);
|
|
|
|
|
|
//Filtar equipamentos por uma instalacao especifica.
|
|
if ($receiveAllPlants && $receiveAllPlants !== '#') {
|
|
$query->whereHas('unit', function ($query) use ($receiveAllPlants) {
|
|
$query->where('plant_id', $receiveAllPlants);
|
|
});
|
|
}
|
|
|
|
if ($receiveAllUnits && $receiveAllUnits !== '#') {
|
|
$query->where('unit_id', $receiveAllUnits);
|
|
}
|
|
|
|
if ($receiveEquipmentsType && $receiveEquipmentsType !== '#') {
|
|
$query->where('equipment_type_id', $receiveEquipmentsType);
|
|
}
|
|
|
|
return DataTables::of($query)->make(true);
|
|
|
|
}
|
|
|
|
public function getDataEquipment(Request $request)
|
|
{
|
|
//Variaveis recebidos da Obra em Planeamento (Filtros)
|
|
$numberProject = $request->get('numberProject');
|
|
$receiveRespostUnitsSelect = $request->get('checkUnits');
|
|
$receiveRespostPlatsSelect = $request->get('tipo_valvulasList');
|
|
|
|
// Variaveis recebidas da gestao de ativos (Filtros)
|
|
$receiveAllClients = $request->get('receiveAllClients');
|
|
$receiveAllPlants = $request->get('receiveAllPlants');
|
|
$receiveAllUnits = $request->get('receiveAllUnits');
|
|
$receiveEquipmentsType = $request->get('receiveEquipmentsType');
|
|
|
|
|
|
|
|
|
|
// Se receber o numero do projecto.
|
|
if ($numberProject) {
|
|
|
|
// Query para DataTables do Planeamento
|
|
$query = Equipment::with('equipmentType', 'unit', 'equipmentWorkHistory')
|
|
->where('company_projects_id', $numberProject)
|
|
->select(['equipment_id', 'equipment_tag', 'unit_id', 'equipment_type_id', 'equipment_description']);
|
|
|
|
if ($receiveRespostUnitsSelect && $receiveRespostUnitsSelect !== '#') {
|
|
$query->where('unit_id', $receiveRespostUnitsSelect);
|
|
}
|
|
|
|
if ($receiveRespostPlatsSelect && $receiveRespostPlatsSelect !== '#') {
|
|
$query->where('equipment_type_id', $receiveRespostPlatsSelect);
|
|
}
|
|
|
|
// Executa a consulta e obtém os resultados
|
|
$equipment = $query->get();
|
|
|
|
return DataTables::of($equipment)
|
|
->addColumn('ispt_number', function ($equipment) {
|
|
// Assumindo que você quer o 'ispt_number' do primeiro 'EquipmentWorkHistory' que corresponde ao 'company_projects_id'
|
|
$equipmentWorkHistory = $equipment->equipmentWorkHistory->first();
|
|
return $equipmentWorkHistory ? $equipmentWorkHistory->ispt_number : 'N/A';
|
|
})
|
|
|
|
->addColumn('unit_name', function ($equipment) {
|
|
// Retorna 'unit_name' do relacionamento 'unit'
|
|
return $equipment->unit ? $equipment->unit->unit_name : 'N/A';
|
|
})
|
|
->addColumn('equipment_type_name', function ($equipment) {
|
|
// Retorna 'equipment_type_name' do relacionamento 'equipmentType'
|
|
return $equipment->equipmentType ? $equipment->equipmentType->equipment_type_name : 'N/A';
|
|
})
|
|
|
|
->addColumn('ambit', function ($equipment) {
|
|
$firstEquipmentWorkHistory = $equipment->equipmentWorkHistory->first();
|
|
return $firstEquipmentWorkHistory->equipmentAssociationAmbit->ambitsEquipment->ambits_description;
|
|
})
|
|
|
|
->addColumn('action', function ($equipment) use ($numberProject) {
|
|
$dropdownHtml = '<div class="d-flex justify-content-center dropdown">
|
|
<button data-toggle="dropdown" aria-expanded="false" class="actions-btn btn btn-light circle">
|
|
<i class="fa fa-ellipsis-v"></i>
|
|
</button>
|
|
<div class="dropdown-menu dropdown-menu-light">';
|
|
|
|
// Adiciona a primeira ação (Detalhes do equipamento)
|
|
$detailsUrl = !is_null($numberProject) ? route('projectDetails_11', ['projectID' => $numberProject, 'equipmentID' => $equipment->equipment_id]) : route('articulated_22', ['equipmentID' => $equipment->equipment_id]);
|
|
$dropdownHtml .= '<a href="' . $detailsUrl . '" class="dropdown-item text-primary">
|
|
<i class="fa-solid fa-eye text-primary"></i>
|
|
Detalhes
|
|
</a>';
|
|
|
|
// Adiciona a ação para abrir a modal para Trocar de Ambito
|
|
$dropdownHtml .= '<button class="dropdown-item text-primary" data-toggle="modal" data-target="#modal-changeAmbitEquipment-' . $equipment->equipment_id . '">
|
|
<i class="fa-solid fa-pen text-primary"></i>
|
|
Trocar de Ambito
|
|
</button>';
|
|
|
|
// Adiciona a ação para abrir a modal para apagar
|
|
$dropdownHtml .= '<button class="dropdown-item text-primary" data-toggle="modal" data-target="#modal-confirmDeleteEquipmentInProject-' . $equipment->equipment_id . '">
|
|
<i class="fa-solid fa-trash text-primary"></i>
|
|
Apagar
|
|
</button>';
|
|
|
|
$dropdownHtml .= '</div></div>'; // Fecha as tags do dropdown
|
|
|
|
return $dropdownHtml;
|
|
})
|
|
->make(true);
|
|
} else {
|
|
// Query padrão que todas as dataTables recebem, a partir dele fazemos os filt
|
|
$query = Equipment::with('equipmentType', 'unit')
|
|
->select(['equipment_id', 'equipment_tag', 'unit_id', 'equipment_type_id', 'equipment_description']);
|
|
|
|
//Filtar equipamentos por um cliente .
|
|
if ($receiveAllClients && $receiveAllClients !== '#') {
|
|
// Filtra os equipamentos cujas unidades estão associadas às plantas do usuário especificado
|
|
$query->whereHas('unit.plant', function ($query) use ($receiveAllClients) {
|
|
$query->where('user_id', $receiveAllClients);
|
|
});
|
|
}
|
|
//Filtar equipamentos por uma instalacao .
|
|
if ($receiveAllPlants && $receiveAllPlants !== '#') {
|
|
$query->whereHas('unit', function ($query) use ($receiveAllPlants) {
|
|
$query->where('plant_id', $receiveAllPlants);
|
|
});
|
|
}
|
|
//Filtrar por Uma fabrica
|
|
if ($receiveAllUnits && $receiveAllUnits !== '#') {
|
|
$query->where('unit_id', $receiveAllUnits);
|
|
}
|
|
//Filtrar por tipo de equipamento
|
|
if ($receiveEquipmentsType && $receiveEquipmentsType !== '#') {
|
|
$query->where('equipment_type_id', $receiveEquipmentsType);
|
|
}
|
|
|
|
|
|
// Executa a consulta e obtém os resultados
|
|
$equipment = $query->get();
|
|
|
|
return DataTables::of($equipment)
|
|
|
|
->addColumn('client_name', function ($equipment) {
|
|
return $equipment->unit->plant->user ? $equipment->unit->plant->user->user_name : 'N/A';
|
|
})
|
|
|
|
->addColumn('unit_name', function ($equipment) {
|
|
// Retorna 'unit_name' do relacionamento 'unit'
|
|
return $equipment->unit ? $equipment->unit->unit_name : 'N/A';
|
|
})
|
|
->addColumn('equipment_type_name', function ($equipment) {
|
|
// Retorna 'equipment_type_name' do relacionamento 'equipmentType'
|
|
return $equipment->equipmentType ? $equipment->equipmentType->equipment_type_name : 'N/A';
|
|
})
|
|
|
|
->addColumn('action', function ($equipment) use ($numberProject) {
|
|
$dropdownHtml = '<div class="d-flex justify-content-center dropdown">
|
|
<button data-toggle="dropdown" aria-expanded="false" class="actions-btn btn btn-light circle">
|
|
<i class="fa fa-ellipsis-v"></i>
|
|
</button>
|
|
<div class="dropdown-menu dropdown-menu-light">';
|
|
|
|
// Adiciona a primeira ação (Detalhes do equipamento)
|
|
$detailsUrl = route('articulated_22', ['equipmentID' => $equipment->equipment_id]);
|
|
$dropdownHtml .= '<a href="' . $detailsUrl . '" class="dropdown-item text-primary">
|
|
<i class="fa-solid fa-eye text-primary"></i>
|
|
Detalhes
|
|
</a>';
|
|
// Adiciona a ação para abrir a modal para apagar
|
|
$dropdownHtml .= '<button class="dropdown-item text-primary" data-toggle="modal" data-target="#modal-confirmDeleteEquipmentInProject-' . $equipment->equipment_id . '">
|
|
<i class="fa-solid fa-trash text-primary"></i>
|
|
Apagar
|
|
</button>';
|
|
|
|
|
|
// Adicionar outras ações aqui, como editar e deletar, conforme necessário
|
|
//Adicionar o "Apagar" e "Trocar de Ambito"
|
|
|
|
$dropdownHtml .= '</div></div>'; // Fecha as tags do dropdown
|
|
|
|
return $dropdownHtml;
|
|
})
|
|
|
|
|
|
|
|
->make(true);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
public function getDataEquipment1(Request $request)
|
|
{
|
|
// pode receber um request ou nao, depende de onde for chamado. (Tabela de equipamentos associada a uma Obra)
|
|
$numberProject = $request->get('numberProject');
|
|
|
|
// pode receber um request ou nao, depende de onde for chamado. (Utilizadores 'Cliente')
|
|
$clientId = $request->get('clientId');
|
|
|
|
$checkUnits = $request->get('checkUnits');
|
|
$tipo_valvulasList = $request->get('tipo_valvulasList');
|
|
|
|
$receiveAllClients = $request->get('receiveAllClients');
|
|
|
|
$receiveAllPlants = $request->get('receiveAllPlants');
|
|
$receiveAllUnits = $request->get('receiveAllUnits');
|
|
|
|
$receiveEquipmentsType = $request->get('receiveEquipmentsType');
|
|
|
|
|
|
if ($clientId) {
|
|
// Recebe as plants com base no USer
|
|
$allPlantsClient = Plant::where('user_id', $clientId)->get();
|
|
|
|
// Extrai os plant_id da coleção $recevePlantClient
|
|
$plantsIds = $allPlantsClient->pluck('plant_id');
|
|
|
|
// Busca todas as fabricas referente a plantas recebidas, guardamos o id de cada fabrica em $unitsIds
|
|
$allUnitsClient = Unit::whereIn('plant_id', $plantsIds)->get();
|
|
$unitsIds = $allUnitsClient->pluck('unit_id');
|
|
|
|
$receiveAllPlants = $request->get('receiveAllPlants');
|
|
|
|
$receiveAllUnits = $request->get('receiveAllUnits');
|
|
|
|
$receiveEquipmentsType = $request->get('receiveEquipmentsType');
|
|
|
|
|
|
// $query = Equipment::with('equipmentType', 'unit')
|
|
// ->whereIn('unit_id', $unitsIds)
|
|
// ->select(['equipment_id', 'equipment_tag', 'unit_id', 'equipment_type_id', 'equipment_description']);
|
|
$query = Equipment::with([
|
|
'equipmentType',
|
|
'unit',
|
|
'equipmentWorkHistory' => function ($query) use ($numberProject) {
|
|
$query->where('company_projects_id', $numberProject)->orderBy('created_at', 'desc');
|
|
}
|
|
])
|
|
->whereIn('unit_id', $unitsIds)
|
|
->select(['equipment_id', 'equipment_tag', 'unit_id', 'equipment_type_id', 'equipment_description']);
|
|
|
|
} else {
|
|
// Query padrão que todas as dataTables recebem, a partir dele fazemos os filt
|
|
$query = Equipment::with('equipmentType', 'unit')
|
|
->select(['equipment_id', 'equipment_tag', 'unit_id', 'equipment_type_id', 'equipment_description']);
|
|
}
|
|
|
|
// Consultas para a Criacao da Obra, Ambas vao ser diferentes, pois na creacao, recebes os equipamentos por obra, porem no portifolio vamos buscar todos.
|
|
if ($numberProject) {
|
|
$query->where('company_projects_id', $numberProject);
|
|
|
|
if ($checkUnits && $checkUnits !== '#') {
|
|
$query->where('unit_id', $checkUnits);
|
|
}
|
|
|
|
if ($tipo_valvulasList && $tipo_valvulasList !== '#') {
|
|
$query->where('equipment_type_id', $tipo_valvulasList);
|
|
}
|
|
}
|
|
|
|
// Consultas para utilizadores do perfil Cliente
|
|
if ($clientId) {
|
|
|
|
}
|
|
|
|
//Filtar equipamentos por um cliente especifico.
|
|
if ($receiveAllClients && $receiveAllClients !== '#') {
|
|
// Filtra os equipamentos cujas unidades estão associadas às plantas do usuário especificado
|
|
$query->whereHas('unit.plant', function ($query) use ($receiveAllClients) {
|
|
$query->where('user_id', $receiveAllClients);
|
|
});
|
|
}
|
|
|
|
//Filtar equipamentos por uma instalacao especifica.
|
|
if ($receiveAllPlants && $receiveAllPlants !== '#') {
|
|
$query->whereHas('unit', function ($query) use ($receiveAllPlants) {
|
|
$query->where('plant_id', $receiveAllPlants);
|
|
});
|
|
}
|
|
|
|
if ($receiveAllUnits && $receiveAllUnits !== '#') {
|
|
$query->where('unit_id', $receiveAllUnits);
|
|
}
|
|
if ($receiveEquipmentsType && $receiveEquipmentsType !== '#') {
|
|
$query->where('equipment_type_id', $receiveEquipmentsType);
|
|
}
|
|
|
|
// Executa a consulta e obtém os resultados
|
|
$equipment = $query->get();
|
|
|
|
return DataTables::of($equipment)
|
|
|
|
->addColumn('ispt_number', function ($equipment) {
|
|
// Assumindo que você quer o 'ispt_number' do primeiro 'EquipmentWorkHistory' que corresponde ao 'company_projects_id'
|
|
$equipmentWorkHistory = $equipment->equipmentWorkHistory->first();
|
|
return $equipmentWorkHistory ? $equipmentWorkHistory->ispt_number : 'N/A';
|
|
})
|
|
|
|
->addColumn('unit_name', function ($equipment) {
|
|
// Retorna 'unit_name' do relacionamento 'unit'
|
|
return $equipment->unit ? $equipment->unit->unit_name : 'N/A';
|
|
})
|
|
->addColumn('equipment_type_name', function ($equipment) {
|
|
// Retorna 'equipment_type_name' do relacionamento 'equipmentType'
|
|
return $equipment->equipmentType ? $equipment->equipmentType->equipment_type_name : 'N/A';
|
|
})
|
|
|
|
->addColumn('ambit', function ($equipment) {
|
|
$firstEquipmentWorkHistory = $equipment->equipmentWorkHistory->first();
|
|
return $firstEquipmentWorkHistory->equipmentAssociationAmbit->ambitsEquipment->ambits_description;
|
|
})
|
|
|
|
|
|
->addColumn('action', function ($equipment) use ($numberProject) {
|
|
$dropdownHtml = '<div class="d-flex justify-content-center dropdown">
|
|
<button data-toggle="dropdown" aria-expanded="false" class="actions-btn btn btn-light circle">
|
|
<i class="fa fa-ellipsis-v"></i>
|
|
</button>
|
|
<div class="dropdown-menu dropdown-menu-light">';
|
|
|
|
// Adiciona a primeira ação (Detalhes do equipamento)
|
|
$detailsUrl = !is_null($numberProject) ? route('projectDetails_11', ['projectID' => $numberProject, 'equipmentID' => $equipment->equipment_id]) : route('articulated_22', ['equipmentID' => $equipment->equipment_id]);
|
|
$dropdownHtml .= '<a href="' . $detailsUrl . '" class="dropdown-item text-primary">
|
|
<i class="fa-solid fa-eye text-primary"></i>
|
|
Detalhes
|
|
</a>';
|
|
|
|
// Adiciona a ação para abrir a modal para Trocar de Ambito
|
|
$dropdownHtml .= '<button class="dropdown-item text-primary" data-toggle="modal" data-target="#modal-changeAmbitEquipment-' . $equipment->equipment_id . '">
|
|
<i class="fa-solid fa-pen text-primary"></i>
|
|
Trocar de Ambito
|
|
</button>';
|
|
|
|
// Adiciona a ação para abrir a modal para apagar
|
|
$dropdownHtml .= '<button class="dropdown-item text-primary" data-toggle="modal" data-target="#modal-confirmDeleteEquipmentInProject-' . $equipment->equipment_id . '">
|
|
<i class="fa-solid fa-trash text-primary"></i>
|
|
Apagar
|
|
</button>';
|
|
|
|
|
|
// Adicionar outras ações aqui, como editar e deletar, conforme necessário
|
|
//Adicionar o "Apagar" e "Trocar de Ambito"
|
|
|
|
$dropdownHtml .= '</div></div>'; // Fecha as tags do dropdown
|
|
|
|
return $dropdownHtml;
|
|
})
|
|
|
|
|
|
->editColumn('unit_id', function ($equipment) {
|
|
// Isto irá substituir 'unit_id' pelo 'unit_name' associado
|
|
return $equipment->unit->unit_name ?? 'N/A';
|
|
})
|
|
->editColumn('equipment_type_id', function ($equipment) {
|
|
// Isto irá substituir 'equipment_type_id' pelo 'equipment_type_name' associado
|
|
return $equipment->equipmentType->equipment_type_name ?? 'N/A';
|
|
})
|
|
->rawColumns(['action'])
|
|
->make(true);
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public function enterWorkstation()
|
|
{
|
|
return view('workstations/index');
|
|
}
|
|
|
|
public function receiveExecutionProject($ProjectId)
|
|
{
|
|
$DatasProject = CompanyProject::find($ProjectId);
|
|
$equipmentsTypes = EquipmentType::all();
|
|
|
|
// return view('projectsClients/executionProject')
|
|
return view('projectsClients/executionProjectNew')
|
|
->with('DatasProject', $DatasProject)
|
|
->with('equipmentsTypes', $equipmentsTypes);
|
|
}
|
|
|
|
public function receiveWorkstationExecutionProject($receiveNumberProject)
|
|
{
|
|
$model = ConstructionWorkstation::where('company_projects_id', $receiveNumberProject)->with('workstationsAssociationTasks');
|
|
|
|
return DataTables::of($model)
|
|
->addColumn('workstations_Association_Tasks', function ($row) {
|
|
return $row->workstationsAssociationTasks->map(function ($task) {
|
|
return $task->elementalTask->elemental_tasks_code;
|
|
})->implode('-');
|
|
})
|
|
|
|
->toJson();
|
|
}
|
|
|
|
public function receiveEquipmentIdForShowModal($EquipmentID)
|
|
{
|
|
// Recebe e encontra os dados do Equipamento indicada na Tabela.
|
|
$equipment = Equipment::find($EquipmentID);
|
|
|
|
// Recebe todas as tarefas e devolve em um Array
|
|
$task_codes = $equipment->orderEquipmentTasks->map(function ($task) {
|
|
return $task->elementalTask->elemental_tasks_code;
|
|
})->toArray();
|
|
|
|
$receveControlEquipment = ControlEquipmentWorkstation::where('equipment_id', $EquipmentID)->get();
|
|
|
|
$receiveCommentsEquipment = EquipmentComments::where('equipment_id', $EquipmentID)->get();
|
|
|
|
// return view('projectsClients/executionProject',['receveControlEquipment'=>$receveControlEquipment]);
|
|
return response()->json(['task_codes' => $task_codes, 'receveControlEquipment' => $receveControlEquipment, 'receiveCommentsEquipment' => $receiveCommentsEquipment]);
|
|
}
|
|
|
|
// public function receiveEquipmentsExecutionProject($receiveNumberProject)
|
|
// {
|
|
// // Recebe os dados vindos da funcao 'data' criada na view
|
|
// $equipment_type_id = request('equipment_type_id');
|
|
// $ambits_id = request('ambits_id');
|
|
|
|
// //Recebe sempre apenas os equipamentos relacionados a obra
|
|
// $model = Equipment::where('company_projects_id', $receiveNumberProject);
|
|
|
|
// // Caso 'equipment_type_id' seja '#', mostra todos os equipamentos
|
|
// if ($equipment_type_id == '#') {
|
|
// $model = Equipment::query()->with(['equipmentType', 'unit', 'equipmentAssociationAmbit.ambitsEquipment']);
|
|
// }
|
|
// // Caso 'equipment_type_id' não seja '#', filtra os equipamentos por tipo e Âmbito (caso 'ambits_id' não seja '#')
|
|
// else {
|
|
// $equipment_type_id = intval($equipment_type_id);
|
|
// $model = Equipment::where('equipments.equipment_type_id', $equipment_type_id)
|
|
// ->join('equipment_association_ambits', 'equipments.equipment_id', '=', 'equipment_association_ambits.equipment_id')
|
|
// ->with(['equipmentType', 'unit', 'equipmentAssociationAmbit.ambitsEquipment']);
|
|
|
|
// if ($ambits_id != '#') {
|
|
// $ambits_id = intval($ambits_id);
|
|
// $model->where('equipment_association_ambits.ambits_id', $ambits_id);
|
|
// }
|
|
// }
|
|
|
|
// return DataTables::of($model)
|
|
// ->addColumn('equipment_type', function ($row) {
|
|
// return $row->equipmentType->equipment_type_name;
|
|
// })
|
|
// ->addColumn('Ambits', function ($row) {
|
|
// return $row->equipmentAssociationAmbit->ambitsEquipment->ambits_description;
|
|
// })
|
|
// ->addColumn('order_tasks', function ($row) {
|
|
// return $row->orderEquipmentTasks->map(function ($task) {
|
|
// return $task->elementalTask->elemental_tasks_code;
|
|
// })->implode('-');
|
|
// })
|
|
// ->addColumn('current_task', function ($row) {
|
|
// return $row->controlEquipmentWorkstation->map(function ($task) {
|
|
// return $task->elementalTask->elemental_tasks_code;
|
|
// })->implode('-');
|
|
// })
|
|
// ->toJson();
|
|
// }
|
|
}
|