- Creation of controller : CreateProject, for creation of the company works. - Controller : ProjectDatacontroller to manage data of the equipments and installations in general.
324 lines
13 KiB
PHP
324 lines
13 KiB
PHP
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\DB;
|
|
use PhpOffice\PhpSpreadsheet\IOFactory;
|
|
|
|
|
|
use App\Models\User;
|
|
use App\Models\CompanyProject;
|
|
use App\Models\Unit;
|
|
use App\Models\EquipmentType;
|
|
use App\Models\Equipment;
|
|
use App\Models\Plant;
|
|
use App\Models\SpecificAttributesEquipmentType;
|
|
use App\Models\GeneralAttributesEquipment;
|
|
use App\Models\PendingEquipment;
|
|
|
|
|
|
class CreateProjectController extends Controller
|
|
{
|
|
|
|
// Nao vinculado ao project ainda
|
|
|
|
public function index()
|
|
{
|
|
// $results = DB::table('equipaments')
|
|
// ->join('specific_attributes_equipament_types', 'equipaments.equipment_ID', '=', 'specific_attributes_equipament_types.tb_equipament_id')
|
|
// ->join('general_attributes_equipaments', 'specific_attributes_equipament_types.specific_Attributes_Equipment_Type_ID', '=', 'general_attributes_equipaments.general_Attributes_Equipment_ID')
|
|
// ->select('equipaments.tag', 'general_attributes_equipaments.description', 'specific_attributes_equipament_types.value')
|
|
// ->get();
|
|
|
|
$results = DB::table('equipments')
|
|
->join('specific_attributes_equipament_types', 'equipments.equipment_id', '=', 'specific_attributes_equipament_types.equipment_id')
|
|
->join('general_attributes_equipaments', 'specific_attributes_equipament_types.specific_attributes_equipment_type_id', '=', 'general_attributes_equipaments.general_attributes_equipment_id')
|
|
->select('equipments.equipment_tag', 'general_attributes_equipaments.general_attributes_equipment_description', 'specific_attributes_equipament_types.specific_attributes_value')
|
|
->get();
|
|
|
|
|
|
dd($results);
|
|
|
|
$groupedEquipments = [];
|
|
|
|
foreach ($results as $result) {
|
|
if (!isset($groupedEquipments[$result->tag])) {
|
|
$groupedEquipments[$result->tag] = [];
|
|
}
|
|
|
|
$groupedEquipments[$result->tag][] = [
|
|
'description' => $result->description,
|
|
'value' => $result->value
|
|
];
|
|
}
|
|
|
|
$equipments = DB::table('equipments')->get();
|
|
|
|
foreach ($equipments as $equipment) {
|
|
if (isset($groupedEquipments[$equipment->tag])) {
|
|
$equipment->specific_attributes = $groupedEquipments[$equipment->tag];
|
|
}
|
|
}
|
|
$allPossibleAttributes = GeneralAttributesEquipment::all()->pluck('description')->toArray();
|
|
|
|
return view('test2', ['equipments' => $equipments, 'allAttributes' => $allPossibleAttributes]);
|
|
// Retorne a view com os dados
|
|
// return view('test', ['equipments' => $equipments]);
|
|
}
|
|
|
|
|
|
public function listCompanies()
|
|
{
|
|
$companies = User::where('type_users', 3)->get();
|
|
return view('projectsClients/createProject', ['companies' => $companies]);
|
|
// return view('projectsClients/createProject', ['companies' => $companies]);
|
|
}
|
|
|
|
|
|
public function createProject(Request $request)
|
|
{
|
|
|
|
// Validação...
|
|
$installationId = $request->input('installation_id');
|
|
|
|
if ($installationId == 'new_install') {
|
|
// Criar uma nova instalação...
|
|
$newInstallation = new Unit;
|
|
$newInstallation->installation_name = $request->input('new_company_name');
|
|
$newInstallation->address = $request->input('new_company_address');
|
|
$newInstallation->user_id = $request->input('user_id');
|
|
|
|
$newInstallation->save();
|
|
|
|
// Use o id da nova instalação.
|
|
$installationId = $newInstallation->id;
|
|
// dd($installationId);
|
|
}
|
|
|
|
$project = new CompanyProject;
|
|
|
|
$project->description_project = $request->input('description_project');
|
|
$project->n_project_ispt = $request->input('n_project_ispt');
|
|
$project->responsible_project_ispt = $request->input('responsible_project_ispt');
|
|
$project->responsible_project_company = $request->input('responsible_project_company');
|
|
$project->date_started = $request->input('date_started');
|
|
|
|
$project->installation_id = $installationId;
|
|
|
|
$project->save();
|
|
|
|
return redirect()->route('createProject')->with('success', 'Dados guardados com sucesso');
|
|
}
|
|
|
|
public function storeProject(Request $request)
|
|
{
|
|
if ($request->input('company_id') == 'new') {
|
|
$company = new CompanyProject; // Substitua "Company" pelo nome do seu modelo de empresas
|
|
$company->name = $request->input('new_company_name');
|
|
$company->save();
|
|
|
|
$company_id = $company->id;
|
|
} else {
|
|
$company_id = $request->input('company_id');
|
|
}
|
|
|
|
// Agora, você pode usar $company_id ao criar o projeto
|
|
}
|
|
|
|
public function getByUserNif(Request $request)
|
|
{
|
|
|
|
// dd(Installation::where('user_id', $request->input('user_id'))->get());
|
|
|
|
$user_id = $request->input('user_id'); //Check
|
|
$installations = Unit::where('user_id', $user_id)->get();
|
|
|
|
return response()->json($installations);
|
|
}
|
|
|
|
// public function createEquipamentProject(Request $request)
|
|
// {
|
|
// $file = $request->file('documento');
|
|
|
|
// // Certifique-se de que um arquivo foi enviado
|
|
// if ($file) {
|
|
// // Carregue o arquivo Excel
|
|
// $spreadsheet = IOFactory::load($file->path());
|
|
|
|
// // Obtenha a primeira planilha
|
|
// $worksheet = $spreadsheet->getSheet(0);
|
|
|
|
// // Transforme os dados da planilha em um array
|
|
// $data = $worksheet->toArray();
|
|
|
|
// $nomesColunas = $data[0];
|
|
|
|
// $dadosLinha6 = $data[6];
|
|
|
|
// $juntarArrays = array_combine($nomesColunas, $dadosLinha6);
|
|
|
|
// $datas = array_filter($juntarArrays, function ($chave) {
|
|
// return !empty($chave);
|
|
// }, ARRAY_FILTER_USE_KEY);
|
|
|
|
// $equipamentType = equipament_type::where('equipment_type_name', $datas['tipo_equipamento'])->first();
|
|
// $checkFactory = factorie::where('factories_name', $datas['fabrica'])->first();
|
|
|
|
|
|
// $newEquipament = new equipament;
|
|
// //Primeiro tem de derificar se a fabrica existe, senao cria uma.
|
|
// $newEquipament->factory_id = $checkFactory->factories_id;
|
|
// $newEquipament->equipament_type_id = $equipamentType->equipament_type_id;
|
|
// $newEquipament->equipment_Description = $datas['equipment_Description'];
|
|
// $newEquipament->tag = $datas['tag'];
|
|
// $newEquipament->serial_number = $datas['n_serie'];
|
|
// $newEquipament->model = $datas['modelo'];
|
|
|
|
// $newEquipament->save();
|
|
|
|
|
|
// $receveEquipment_ID = $newEquipament->id;
|
|
// $receveEquipament_type_ID = $newEquipament->equipament_type_id;
|
|
|
|
|
|
// // Atributos que você quer buscar e inserir
|
|
// $attributes = ["dimension", "dn_ent", "p&id", "n_sap", "isolation", "scaffolding", "grua", "interlocks"];
|
|
|
|
// // $attributes = array_slice($data[0], 7);
|
|
|
|
// foreach ($attributes as $attribute) {
|
|
|
|
// // Buscar o atributo na tabela general_attributes_equipament
|
|
// $generalAttribute = general_attributes_equipament::where('description', $attribute)->first();
|
|
|
|
|
|
// // Se o atributo foi encontrado
|
|
// if (!is_null($generalAttribute)) {
|
|
// // Criar um novo registro em specific_attributes_equipament_Types
|
|
// $specificAttribute = new specific_attributes_equipament_type;
|
|
|
|
// $specificAttribute->tb_equipament_id = $receveEquipment_ID;
|
|
// $specificAttribute->equipament_Type_id = $receveEquipament_type_ID;
|
|
// $specificAttribute->specific_attributes_equipment_type_id = $generalAttribute->general_attributes_equipment_id;
|
|
// $specificAttribute->value = $datas[$attribute];
|
|
|
|
// $specificAttribute->save();
|
|
// }
|
|
// }
|
|
// $listValves = equipament::all();
|
|
|
|
// return redirect()->route('testExcel')->with('success', 'Dados guardados com sucesso')->with('listValves', $listValves);
|
|
// }
|
|
// }
|
|
|
|
public function createEquipamentProject(Request $request)
|
|
{
|
|
$file = $request->file('documento');
|
|
|
|
// Certifique-se de que um arquivo foi enviado
|
|
if ($file) {
|
|
// Carregue o arquivo Excel
|
|
$spreadsheet = IOFactory::load($file->path());
|
|
|
|
// Obtenha a primeira planilha
|
|
$worksheet = $spreadsheet->getSheet(0);
|
|
|
|
// Transforme os dados da planilha em um array
|
|
$data = $worksheet->toArray();
|
|
|
|
$nomesColunas = $data[0];
|
|
|
|
// Atributos que você quer buscar e inserir
|
|
$attributes = ["dimension", "dn_ent", "p&id", "n_sap", "isolation", "scaffolding", "grua", "interlocks"];
|
|
|
|
// Comece a partir da sexta linha
|
|
for ($i = 6; $i < count($data); $i++) {
|
|
$dadosLinha = $data[$i];
|
|
|
|
//Se nao preencher o campo $dadosLinha[0], nao cria um novo equipamento
|
|
//Trocar pelos 5 primeiros
|
|
if (empty($dadosLinha[0])) {
|
|
continue;
|
|
}
|
|
|
|
$juntarArrays = array_combine($nomesColunas, $dadosLinha);
|
|
|
|
$datas = array_filter($juntarArrays, function ($chave) {
|
|
return !empty($chave);
|
|
}, ARRAY_FILTER_USE_KEY);
|
|
|
|
$equipmentType = EquipmentType::where('equipment_type_name', $datas['tipo_equipamento'])->first();
|
|
|
|
$checkFactory = Unit::where('unit_name', $datas['fabrica'])->first();
|
|
|
|
|
|
// Antes de criar o novo equipamento, verifique se já existe um equipamento
|
|
// com o mesmo factory_id e tag.
|
|
$existingEquipment = Equipment::where('unit_id', $checkFactory->unit_id)
|
|
->where('equipment_tag', $datas['tag'])
|
|
->first();
|
|
|
|
if ($existingEquipment) {
|
|
// Se o equipamento existir, crie o novo equipamento na tabela pending_equipaments.
|
|
$pendingEquipament = new PendingEquipment;
|
|
// Defina os atributos do pendingEquipament conforme necessário.
|
|
$pendingEquipament->pending_equipment_unit_id = $checkFactory->unit_id;
|
|
$pendingEquipament->pending_equipment_type_id = $equipmentType->equipment_type_id;
|
|
$pendingEquipament->pending_equipment_tag = $datas['tag'];
|
|
$pendingEquipament->pending_equipment_description = $datas['equipment_Description'];
|
|
$pendingEquipament->pending_equipment_serial_number = $datas['n_serie'];
|
|
$pendingEquipament->pending_equipment_brand = $datas['modelo'];
|
|
|
|
$pendingEquipament->save();
|
|
|
|
// Continue com o próximo loop.
|
|
continue;
|
|
}
|
|
|
|
$newEquipament = new Equipment;
|
|
|
|
$newEquipament->unit_id = $checkFactory->unit_id;
|
|
$newEquipament->equipment_type_id = $equipmentType->equipment_type_id;
|
|
$newEquipament->equipment_Description = $datas['equipment_Description'];
|
|
$newEquipament->equipment_tag = $datas['tag'];
|
|
$newEquipament->equipment_serial_number = $datas['n_serie'];
|
|
$newEquipament->equipment_model = $datas['modelo'];
|
|
|
|
$newEquipament->save();
|
|
|
|
$receveEquipment_ID = $newEquipament->equipment_id;
|
|
|
|
$receveEquipament_type_ID = $newEquipament->equipment_type_id;
|
|
|
|
|
|
foreach ($attributes as $attribute) {
|
|
|
|
$generalAttribute = GeneralAttributesEquipment::where('general_attributes_equipment_description', $attribute)->first();
|
|
|
|
|
|
|
|
if (!is_null($generalAttribute)) {
|
|
|
|
$specificAttribute = new SpecificAttributesEquipmentType;
|
|
|
|
$specificAttribute->equipment_id = $receveEquipment_ID;
|
|
$specificAttribute->equipment_type_id = $receveEquipament_type_ID;
|
|
$specificAttribute->specific_attributes_equipment_type_id = $generalAttribute->general_attributes_equipment_id;
|
|
$specificAttribute->specific_attributes_value = $datas[$attribute];
|
|
|
|
$specificAttribute->save();
|
|
|
|
}
|
|
}
|
|
}
|
|
|
|
$listValves = Equipment::all();
|
|
return redirect()->route('createProject')->with('success', 'Dados guardados com sucesso')->with('listValves', $listValves);
|
|
}
|
|
|
|
}
|
|
}
|