383 lines
15 KiB
PHP
Executable File
383 lines
15 KiB
PHP
Executable File
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Models\Company;
|
|
use App\Models\EquipmentWorkHistory;
|
|
use App\Models\Plant;
|
|
use App\Models\QrcodesAssociatedEquipment;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
use Yajra\DataTables\DataTables;
|
|
|
|
|
|
use App\Models\CompanyProject;
|
|
use App\Models\Equipment;
|
|
use App\Models\EquipmentType;
|
|
use App\Models\Unit;
|
|
use App\Models\AmbitsEquipment;
|
|
use Illuminate\Support\Facades\Auth;
|
|
|
|
// use PDF;
|
|
|
|
use Mpdf\Mpdf;
|
|
|
|
use Endroid\QrCode\QrCode;
|
|
use Endroid\QrCode\Writer\PngWriter;
|
|
|
|
|
|
class PreparedProjectController extends Controller
|
|
{
|
|
// public function showAllEquipmentsInProjectForQrCode($projectId)
|
|
// {
|
|
// $equipments = Equipment::where('company_projects_id', $projectId)->get();
|
|
// $userLogoPath = Auth::user()->user_logo ? asset('user_logos/' . Auth::user()->user_logo) : asset('user_logos/logoISPT4.0.jpg');
|
|
|
|
// $equipmentData = [];
|
|
// foreach ($equipments as $equipment) {
|
|
|
|
// // Gera o QR Code para cada equipamento
|
|
// $qrCode = new QrCode($equipment->equipment_tag);
|
|
// $writer = new PngWriter();
|
|
// $qrCodeImage = 'data:image/png;base64,' . base64_encode($writer->write($qrCode)->getString());
|
|
|
|
// // Busca equipamentos associados para cada equipamento
|
|
// $associatedEquipments = QrcodesAssociatedEquipment::where('equipment_id', $equipment->equipment_id)->get();
|
|
// $associatedArray = [];
|
|
// foreach ($associatedEquipments as $associatedEquipment) {
|
|
// $associatedArray[$associatedEquipment->id] = $associatedEquipment->component_tag;
|
|
// }
|
|
|
|
// // Adiciona os dados necessários para cada equipamento em um array
|
|
// $equipmentData[] = [
|
|
// 'detailsEquipment' => $equipment,
|
|
// 'userLogoPath' => $userLogoPath,
|
|
// 'qrCodeImage' => $qrCodeImage,
|
|
// 'associatedArray' => $associatedArray
|
|
// ];
|
|
// }
|
|
|
|
// // Passa todos os dados dos equipamentos para a view
|
|
// $html = view('projectsClients.showAllEquipmentsForQrCodePdf', [
|
|
// 'equipmentData' => $equipmentData,
|
|
// 'userLogoPath' => $userLogoPath,
|
|
// ])->render();
|
|
|
|
// // Define a orientação da folha como horizontal (Landscape)
|
|
// $mpdf = new \Mpdf\Mpdf([
|
|
// 'mode' => 'utf-8',
|
|
// 'format' => 'A4-L' // Define o formato como A4 e orientação como Landscape (horizontal)
|
|
// ]);
|
|
// $mpdf->WriteHTML($html);
|
|
// $mpdf->Output('equipamentos_projeto_' . $projectId . '.pdf', 'I');
|
|
// }
|
|
|
|
|
|
// public function showAllEquipmentsInProjectForQrCode($projectId)
|
|
// {
|
|
// $equipments = Equipment::where('company_projects_id', $projectId)->get();
|
|
|
|
// $equipmentData = [];
|
|
// foreach ($equipments as $equipment) {
|
|
// $qrCode = new QrCode($equipment->equipment_tag);
|
|
// $writer = new PngWriter();
|
|
// $qrCodeImage = 'data:image/png;base64,' . base64_encode($writer->write($qrCode)->getString());
|
|
|
|
// $associatedEquipments = QrcodesAssociatedEquipment::where('equipment_id', $equipment->equipment_id)->get();
|
|
// $associatedArray = [];
|
|
// foreach ($associatedEquipments as $associatedEquipment) {
|
|
// $associatedArray[$associatedEquipment->id] = $associatedEquipment->component_tag;
|
|
// }
|
|
|
|
// $equipmentData[] = [
|
|
// 'equipment' => $equipment,
|
|
// 'qrCodeImage' => $qrCodeImage,
|
|
// 'associatedArray' => $associatedArray
|
|
// ];
|
|
// }
|
|
|
|
// $detailsCompanyProject = CompanyProject::find($projectId);
|
|
// $detailsPlant = Plant::where('plant_id', $detailsCompanyProject->plant_id)->first();
|
|
|
|
// $detailsCompany = Company::where('company_id', $detailsPlant->company_id)->first();
|
|
|
|
// $logoPath = public_path('companies_logo/' . ($detailsCompany->company_logo ?: 'logoISPT4.0.jpg'));
|
|
|
|
// if (!file_exists($logoPath)) {
|
|
// $logoPath = public_path('companies_logo/logoISPT4.0.jpg');
|
|
// }
|
|
|
|
// // dd($equipmentData);
|
|
|
|
// $html = view('projectsClients.showAllEquipmentsInProjectQrCodePdf', [
|
|
// 'equipmentData' => $equipmentData,
|
|
// 'logoPath' => $logoPath
|
|
// ])->render();
|
|
|
|
// $mpdf = new \Mpdf\Mpdf([
|
|
// 'mode' => 'utf-8',
|
|
// 'format' => 'A4'
|
|
// ]);
|
|
// $mpdf->WriteHTML($html);
|
|
// $mpdf->Output('equipamentos_projeto_' . $projectId . '.pdf', 'I');
|
|
// }
|
|
|
|
public function showAllEquipmentsInProjectForQrCode($projectId)
|
|
{
|
|
$equipments = Equipment::where('company_projects_id', $projectId)->get();
|
|
|
|
$equipmentData = [];
|
|
foreach ($equipments as $equipment) {
|
|
$associatedEquipments = QrcodesAssociatedEquipment::where('equipment_id', $equipment->equipment_id)->get();
|
|
$associatedArray = [];
|
|
|
|
foreach ($associatedEquipments as $associatedEquipment) {
|
|
$qrCode = new QrCode($associatedEquipment->component_tag);
|
|
$writer = new PngWriter();
|
|
$qrCodeImage = 'data:image/png;base64,' . base64_encode($writer->write($qrCode)->getString());
|
|
|
|
$associatedArray[] = [
|
|
'id' => $associatedEquipment->id,
|
|
'component_tag' => $associatedEquipment->component_tag,
|
|
'qrCodeImage' => $qrCodeImage
|
|
];
|
|
}
|
|
|
|
$equipmentData[] = [
|
|
'equipment' => $equipment,
|
|
'associatedArray' => $associatedArray
|
|
];
|
|
}
|
|
|
|
$detailsCompanyProject = CompanyProject::find($projectId);
|
|
$detailsPlant = Plant::where('plant_id', $detailsCompanyProject->plant_id)->first();
|
|
$detailsCompany = Company::where('company_id', $detailsPlant->company_id)->first();
|
|
$logoPath = public_path('companies_logo/' . ($detailsCompany->company_logo ?: 'logoISPT4.0.jpg'));
|
|
|
|
if (!file_exists($logoPath)) {
|
|
$logoPath = public_path('companies_logo/logoISPT4.0.jpg');
|
|
}
|
|
|
|
$html = view('projectsClients.showAllEquipmentsInProjectQrCodePdf', [
|
|
'equipmentData' => $equipmentData,
|
|
'logoPath' => $logoPath
|
|
])->render();
|
|
|
|
$mpdf = new \Mpdf\Mpdf(['mode' => 'utf-8', 'format' => 'A4']);
|
|
$mpdf->WriteHTML($html);
|
|
$mpdf->Output('equipamentos_projeto_' . $projectId . '.pdf', 'I');
|
|
}
|
|
|
|
|
|
|
|
// public function showDetailsEquipmentForQrCode($equipmentId, $projectId)
|
|
// {
|
|
// $detailsEquipment = Equipment::find($equipmentId);
|
|
|
|
// // Gera o QR Code
|
|
// $qrCode = new QrCode($detailsEquipment->equipment_tag);
|
|
// $writer = new PngWriter();
|
|
|
|
// // Converte o QR Code para base64 para incorporar na página
|
|
// $qrCodeImage = 'data:image/png;base64,' . base64_encode($writer->write($qrCode)->getString());
|
|
|
|
// $detailsCompanyProject = CompanyProject::find($projectId);
|
|
// $detailsPlant = Plant::where('plant_id', $detailsCompanyProject->plant_id)->first();
|
|
|
|
// $detailsCompany= Company::where('company_id', $detailsPlant->company_id)->first();
|
|
|
|
// $logoPath = public_path('companies_logo/' . ($detailsCompany->company_logo ?: 'logoISPT4.0.jpg'));
|
|
|
|
// if (!file_exists($logoPath)) {
|
|
// $logoPath = public_path('companies_logo/logoISPT4.0.jpg');
|
|
// }
|
|
|
|
// $associatedEquipments = QrcodesAssociatedEquipment::where('equipment_id', $detailsEquipment->equipment_id)->get();
|
|
|
|
// $associatedArray = [];
|
|
// foreach ($associatedEquipments as $associatedEquipment) {
|
|
// $associatedArray[$associatedEquipment->id] = $associatedEquipment->component_tag;
|
|
// }
|
|
|
|
// $html = view('projectsClients.showDetailsEquipmentForQrCodePdf', [
|
|
// 'detailsEquipment' => $detailsEquipment,
|
|
// 'logoPath' => $logoPath,
|
|
// 'qrCodeImage' => $qrCodeImage,
|
|
// 'associatedArray' => $associatedArray
|
|
// ])->render();
|
|
|
|
// // Define a orientação da folha como horizontal (Landscape)
|
|
// $mpdf = new \Mpdf\Mpdf([
|
|
// 'mode' => 'utf-8',
|
|
// 'format' => 'A4' // Define o formato como A4 e orientação como Landscape (horizontal)
|
|
// ]);
|
|
// $mpdf->WriteHTML($html);
|
|
// $mpdf->Output('nome_do_arquivo.pdf', 'I');
|
|
// }
|
|
|
|
public function showDetailsEquipmentForQrCode($equipmentId, $projectId)
|
|
{
|
|
$detailsEquipment = Equipment::find($equipmentId);
|
|
$detailsCompanyProject = CompanyProject::find($projectId);
|
|
$detailsPlant = Plant::where('plant_id', $detailsCompanyProject->plant_id)->first();
|
|
$detailsCompany = Company::where('company_id', $detailsPlant->company_id)->first();
|
|
$logoPath = public_path('companies_logo/' . ($detailsCompany->company_logo ?: 'logoISPT4.0.jpg'));
|
|
|
|
if (!file_exists($logoPath)) {
|
|
$logoPath = public_path('companies_logo/logoISPT4.0.jpg');
|
|
}
|
|
|
|
$associatedEquipments = QrcodesAssociatedEquipment::where('equipment_id', $detailsEquipment->equipment_id)->get();
|
|
$associatedArray = [];
|
|
$qrCodeImages = [];
|
|
|
|
foreach ($associatedEquipments as $associatedEquipment) {
|
|
$associatedArray[$associatedEquipment->id] = $associatedEquipment->component_tag;
|
|
$qrCode = new QrCode($associatedEquipment->component_tag);
|
|
$writer = new PngWriter();
|
|
$qrCodeImages[$associatedEquipment->id] = 'data:image/png;base64,' . base64_encode($writer->write($qrCode)->getString());
|
|
}
|
|
|
|
$html = view('projectsClients.showDetailsEquipmentForQrCodePdf', [
|
|
'detailsEquipment' => $detailsEquipment,
|
|
'logoPath' => $logoPath,
|
|
'qrCodeImages' => $qrCodeImages,
|
|
'associatedArray' => $associatedArray
|
|
])->render();
|
|
|
|
$mpdf = new \Mpdf\Mpdf(['mode' => 'utf-8', 'format' => 'A4']);
|
|
$mpdf->WriteHTML($html);
|
|
$mpdf->Output('nome_do_arquivo.pdf', 'I');
|
|
}
|
|
|
|
|
|
public function PreparedProject($ProjectId)
|
|
{
|
|
|
|
$numberProject = CompanyProject::find($ProjectId);
|
|
|
|
$equipmentsTypes = EquipmentType::all();
|
|
|
|
// Carrega todos os equipamentos para um dado projeto
|
|
$equipmentsProjects = Equipment::where('company_projects_id', $ProjectId)->get();
|
|
|
|
// Carrega as relações aninhadas
|
|
$equipmentsProjects->load('equipmentWorkHistory.equipmentAssociationAmbit.ambitsEquipment');
|
|
|
|
// Adiciona o 'ambits_description' a cada equipamento
|
|
foreach ($equipmentsProjects as $equipment) {
|
|
// Obtém o primeiro EquipmentWorkHistory (se existir)
|
|
$equipmentWorkHistory = $equipment->equipmentWorkHistory->first();
|
|
|
|
// Se existe um EquipmentWorkHistory, tenta obter o 'ambits_description'
|
|
if ($equipmentWorkHistory) {
|
|
// Tenta obter o EquipmentAssociationAmbit e o AmbitsEquipment relacionados
|
|
$equipmentAssociationAmbit = $equipmentWorkHistory->equipmentAssociationAmbit;
|
|
if ($equipmentAssociationAmbit) {
|
|
$ambitsEquipment = $equipmentAssociationAmbit->ambitsEquipment;
|
|
if ($ambitsEquipment) {
|
|
// Se existir um AmbitsEquipment, define o 'ambits_description'
|
|
$equipment->ambits_description = $ambitsEquipment->ambits_description;
|
|
}
|
|
}
|
|
}
|
|
|
|
// Se alguma das relações não existir, define 'ambits_description' como null
|
|
if (!isset($equipment->ambits_description)) {
|
|
$equipment->ambits_description = null;
|
|
}
|
|
}
|
|
|
|
//Retorna todas as Fabricas Unit, com base na Instalação
|
|
$checkUnits = DB::table('units')
|
|
->join('plants', 'units.plant_id', '=', 'plants.plant_id')
|
|
->join('company_projects', 'plants.plant_id', '=', 'company_projects.plant_id')
|
|
->select('units.*')
|
|
->where('company_projects.company_projects_id', '=', $numberProject->company_projects_id)
|
|
->get();
|
|
|
|
return view('projectsClients/preparedProject')
|
|
->with('equipmentsTypes', $equipmentsTypes)
|
|
->with('units', $checkUnits)
|
|
->with('numberProject', $numberProject)
|
|
->with('equipmentsProjects', $equipmentsProjects);
|
|
}
|
|
|
|
public function getAmbits($equipmentType)
|
|
{
|
|
$ambits = DB::table('ambits_equipments')
|
|
->select('ambits_equipments.*')
|
|
->where('ambits_equipments.equipment_type_id', $equipmentType)
|
|
->get();
|
|
return response()->json($ambits);
|
|
}
|
|
|
|
public function editProjectForArticulated(Request $request)
|
|
{
|
|
|
|
$numberProject = CompanyProject::find($request->ProjectId);
|
|
|
|
$numberProject->order_project = 1;
|
|
$numberProject->save();
|
|
|
|
return redirect()->route('home');
|
|
}
|
|
|
|
public function getData1()
|
|
{
|
|
|
|
$equipment_type_id = request('equipment_type_id');
|
|
$unit_id = request('unit_id');
|
|
$ambits_id = request('ambits_id');
|
|
|
|
// Equipment::all()->where('company_projects_id', $ProjectId);
|
|
|
|
// 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);
|
|
}
|
|
}
|
|
if (request()->has('inspec') && request('inspec') != '#') {
|
|
$inspectionFilter = request('inspec') === 'Sim';
|
|
$model->whereHas('orderEquipmentTasks', function ($query) use ($inspectionFilter) {
|
|
$query->where('inspection', $inspectionFilter ? 'Sim' : 'Nao');
|
|
});
|
|
}
|
|
|
|
|
|
// Aplica o filtro de 'unit_id', se aplicável
|
|
if ($unit_id != '#') {
|
|
$unit_id = intval($unit_id);
|
|
$model->where('equipments.unit_id', $unit_id);
|
|
}
|
|
|
|
// Gera a tabela de dados
|
|
return DataTables::of($model)
|
|
->addColumn('equipment_type', function ($row) {
|
|
return $row->equipmentType->equipment_type_name;
|
|
})
|
|
->addColumn('Unit', function ($row) {
|
|
return $row->unit->unit_name;
|
|
})
|
|
->addColumn('Ambits', function ($row) {
|
|
return $row->equipmentAssociationAmbit->ambitsEquipment->ambits_description;
|
|
})
|
|
->addColumn('Inspec', function ($row) {
|
|
return $row->hasInspectionYes() ? 'Sim' : 'Nao';
|
|
})
|
|
->toJson();
|
|
}
|
|
}
|