AsyncValidateLoadArchivesController.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570
  1. <?php
  2. namespace App\Http\Controllers;
  3. use Illuminate\Http\Request;
  4. use Illuminate\Support\Facades\Validator;
  5. use PhpOffice\PhpSpreadsheet\IOFactory;
  6. use ZipArchive;
  7. use App\Jobs\ValidateLoadArchives;
  8. use Illuminate\Support\Str;
  9. use Illuminate\Support\Facades\DB;
  10. use Illuminate\Support\Facades\Log;
  11. /**
  12. * Async Controller for validating Excel and ZIP files for equipment documentation loading
  13. * Validates Excel headers structure and compares file listings with ZIP contents using queues
  14. */
  15. class AsyncValidateLoadArchivesController extends Controller
  16. {
  17. private $responseController;
  18. private $encryptionController;
  19. private $documentManagementController;
  20. private $functionsController;
  21. // Expected headers in row 7 of Excel file with their corresponding column letters
  22. private $expectedHeaders = [
  23. ['letter' => 'D', 'category' => 'CÓDIGO DE EQUIPO'],
  24. ['letter' => 'H', 'category' => 'FICHA DE SEGURIDAD DE PRODUCTOS QUÍMICOS'],
  25. ['letter' => 'J', 'category' => 'FICHA TÉCNICA'],
  26. ['letter' => 'L', 'category' => 'FOTOGRAFÍAS - DIAGRAMAS'],
  27. ['letter' => 'N', 'category' => 'DATOS DEL PROVEEDOR'],
  28. ['letter' => 'P', 'category' => 'MANUALES DE OPERACIÓN'],
  29. ['letter' => 'R', 'category' => 'MANUALES DE MANTENIMIENTO PREVENTIVO'],
  30. ['letter' => 'T', 'category' => 'MANUALES DE MANTENIMIENTO CORRECTIVO'],
  31. ['letter' => 'V', 'category' => 'DOCUMENTOS ADICIONALES']
  32. ];
  33. // Mapping of file extensions to their types for validation
  34. private $extensionTypes = [
  35. // Archivos de almacenamiento
  36. 'zip' => 'STORAGE', 'rar' => 'STORAGE', 'tar' => 'STORAGE', '7z' => 'STORAGE',
  37. // Formatos de audio
  38. 'mp3' => 'AUDIO', 'mpeg' => 'AUDIO', 'wav' => 'AUDIO', 'ogg' => 'AUDIO', 'opus' => 'AUDIO',
  39. // Archivos de imagen
  40. 'jpeg' => 'IMG', 'jpg' => 'IMG', 'png' => 'IMG', 'gif' => 'IMG', 'bmp' => 'IMG', 'tiff' => 'IMG', 'svg' => 'IMG',
  41. // Archivos de texto
  42. 'txt' => 'TEXT',
  43. // Archivos de video
  44. 'webm' => 'VIDEO', 'mpeg4' => 'VIDEO', 'mp4' => 'VIDEO', '3gpp' => 'VIDEO', 'mov' => 'VIDEO', 'avi' => 'VIDEO', 'wmv' => 'VIDEO', 'flv' => 'VIDEO',
  45. // Planos de Auto CAD
  46. 'dwg' => 'CAD', 'dxf' => 'CAD',
  47. // Modelos de 3D Studio
  48. '3ds' => '3D',
  49. // Dibujos de Illustrator
  50. 'ai' => 'ILLUSTRATOR',
  51. // Imágenes de Photoshop
  52. 'psd' => 'PHOTOSHOP',
  53. // Formato de documento portátil
  54. 'pdf' => 'PDF',
  55. // Hoja de cálculo en Excel
  56. 'xls' => 'EXCEL', 'xlsx' => 'EXCEL',
  57. // Presentaciones de PowerPoint
  58. 'ppt' => 'POWERPOINT', 'pptx' => 'POWERPOINT',
  59. // Documentos de texto de Word
  60. 'doc' => 'WORD', 'docx' => 'WORD',
  61. // Dibujos de Visio
  62. 'vsd' => 'VISIO', 'vsdx' => 'VISIO'
  63. ];
  64. public function __construct()
  65. {
  66. $this->responseController = new ResponseController();
  67. $this->encryptionController = new EncryptionController();
  68. $this->documentManagementController = new DocumentManagementController();
  69. $this->functionsController = new FunctionsController();
  70. }
  71. /***
  72. * Encryption endpoint
  73. *
  74. */
  75. public function encrypt(Request $request){
  76. $encryptString = $this->encryptionController->encrypt($request['value']);
  77. if(!$encryptString){
  78. return $this->responseController->makeResponse(true, "La cadena no fue encriptada correctamente", [], 400);
  79. }
  80. return $this->responseController->makeResponse(false, "Cadena encriptada correctamente", ['encrypted' => $encryptString], 200);
  81. }
  82. /**
  83. * Validation endpoint that dispatches file process to bat job
  84. */
  85. public function validateFilesWithBat(Request $request){
  86. $validator = Validator::make($request->all(), [
  87. 'id_user' => 'required|string',
  88. 'linea' => 'required|integer',
  89. 'excel_file' => 'required|file|mimes:xlsx,xls',
  90. 'zip_file' => 'required|file|mimes:zip'
  91. ]);
  92. if ($validator->fails()) {
  93. return $this->responseController->makeResponse(
  94. true,
  95. 'Se encontraron uno o más errores.',
  96. $this->responseController->makeErrors($validator->errors()->messages()),
  97. 400
  98. );
  99. }
  100. $jobId = Str::uuid();
  101. $userId = '0000000001';
  102. // $idUser = $this->encryptionController->decrypt($request->input('id_user'));
  103. // if(!$idUser){
  104. // return $this->responseController->makeResponse(true, "El id del usuario que realizó la petición no fue encriptado correctamente", [], 400);
  105. // }
  106. // Validate Excel file headers structure
  107. $excelValidation = $this->validateExcelHeaders($request->file('excel_file'));
  108. if ($excelValidation['error']) {
  109. return $this->responseController->makeResponse(true, $excelValidation['message'], [], 400);
  110. }
  111. // Validate ZIP file integrity
  112. $zipValidation = $this->validateZipFile($request->file('zip_file'));
  113. if ($zipValidation['error']) {
  114. return $this->responseController->makeResponse(true, $zipValidation['message'], [], 400);
  115. }
  116. // Extract and validate file listings from Excel
  117. $filesValidation = $this->extractAndValidateFiles($request->file('excel_file'));
  118. if ($filesValidation['error']) {
  119. return $this->responseController->makeResponse(true, $filesValidation['message'], [], 400);
  120. }
  121. // Compare Excel file listings with ZIP contents
  122. $comparison = $this->compareExcelWithZip($filesValidation['files'], $request->file('zip_file'));
  123. // Check for file size validation errors
  124. if (isset($comparison['error'])) {
  125. return $this->responseController->makeResponse(true, $comparison['error'], [], 400);
  126. }
  127. if (!$comparison['valid']) {
  128. return $this->responseController->makeResponse(
  129. true,
  130. 'Los archivos no coinciden entre Excel y ZIP',
  131. $comparison,
  132. 400
  133. );
  134. }
  135. // Upload temp files
  136. $tempFiles = $this->uploadTempFiles($request);
  137. if ($tempFiles['error']) {
  138. return $this->responseController->makeResponse(true, $tempFiles['message'], [], 400);
  139. }
  140. // Log::info($tempFiles);
  141. $idFileZip = $this->encryptionController->decrypt($tempFiles['files']['zip']);
  142. if(!$idFileZip){
  143. return $this->responseController->makeResponse(true, "El id del archivo que desea eliminar no fue encriptado correctamente", [], 400);
  144. }
  145. // Log::info($idFileZip);
  146. // //Recuperar objeto del registro del zip
  147. // $zipFile = DB::table('S002V01TARTE')->where([
  148. // ['ARTE_IDAR', '=', $idFileZip],
  149. // ['ARTE_NULI', '=', $request['linea']]
  150. // ])->first();
  151. // //hay que usar la funcion de registerActivity para registrar en bd los movimiento como el de registrar un nuevo archivo en tmep
  152. $idFileExcel = $this->encryptionController->decrypt($tempFiles['files']['excel']);
  153. if(!$idFileExcel){
  154. return $this->responseController->makeResponse(true, "El id del archivo que desea eliminar no fue encriptado correctamente", [], 400);
  155. }
  156. // Log::info($idFileExcel);
  157. // //Recuperar objeto del registro del excel
  158. // $excelFile = DB::table('S002V01TARTE')->where([
  159. // ['ARTE_IDAR', '=', $idFileExcel],
  160. // ['ARTE_NULI', '=', $request['linea']]
  161. // ])->first();
  162. Log::info($idFileExcel);
  163. Log::info($idFileZip);
  164. // Guardar en bd::validation_jobs
  165. $jobId = DB::table('validation_jobs')->insertGetId([
  166. 'job_id' => $jobId,
  167. 'user_id' => $userId,
  168. 'linea' => $request->input('linea'),
  169. 'excel_temp_id' => $idFileExcel,
  170. 'zip_temp_id' => $idFileZip,
  171. 'created_at' => now(),
  172. 'updated_at' => now()
  173. ]);
  174. return $this->responseController->makeResponse(
  175. false,
  176. 'Validación iniciada',
  177. ['job_id' => $jobId, 'status' => 'processing']
  178. );
  179. }
  180. /**
  181. * Async validation endpoint that dispatches validation to queue
  182. */
  183. public function validateFiles(Request $request)
  184. {
  185. $validator = Validator::make($request->all(), [
  186. 'id_user' => 'required|string',
  187. 'linea' => 'required|integer',
  188. 'excel_file' => 'required|file|mimes:xlsx,xls',
  189. 'zip_file' => 'required|file|mimes:zip'
  190. ]);
  191. if ($validator->fails()) {
  192. return $this->responseController->makeResponse(
  193. true,
  194. 'Se encontraron uno o más errores.',
  195. $this->responseController->makeErrors($validator->errors()->messages()),
  196. 400
  197. );
  198. }
  199. $jobId = Str::uuid();
  200. $userId = '0000000001';
  201. // $idUser = $this->encryptionController->decrypt($request->input('id_user'));
  202. // if(!$idUser){
  203. // return $this->responseController->makeResponse(true, "El id del usuario que realizó la petición no fue encriptado correctamente", [], 400);
  204. // }
  205. // Validate Excel file headers structure
  206. $excelValidation = $this->validateExcelHeaders($request->file('excel_file'));
  207. if ($excelValidation['error']) {
  208. return $this->responseController->makeResponse(true, $excelValidation['message'], [], 400);
  209. }
  210. // Validate ZIP file integrity
  211. $zipValidation = $this->validateZipFile($request->file('zip_file'));
  212. if ($zipValidation['error']) {
  213. return $this->responseController->makeResponse(true, $zipValidation['message'], [], 400);
  214. }
  215. // Extract and validate file listings from Excel
  216. $filesValidation = $this->extractAndValidateFiles($request->file('excel_file'));
  217. if ($filesValidation['error']) {
  218. return $this->responseController->makeResponse(true, $filesValidation['message'], [], 400);
  219. }
  220. // Compare Excel file listings with ZIP contents
  221. $comparison = $this->compareExcelWithZip($filesValidation['files'], $request->file('zip_file'));
  222. // Check for file size validation errors
  223. if (isset($comparison['error'])) {
  224. return $this->responseController->makeResponse(true, $comparison['error'], [], 400);
  225. }
  226. if (!$comparison['valid']) {
  227. return $this->responseController->makeResponse(
  228. true,
  229. 'Los archivos no coinciden entre Excel y ZIP',
  230. $comparison,
  231. 400
  232. );
  233. }
  234. // Upload temp files
  235. $tempFiles = $this->uploadTempFiles($request);
  236. if ($tempFiles['error']) {
  237. return $this->responseController->makeResponse(true, $tempFiles['message'], [], 400);
  238. }
  239. // Solo pasar datos serializables al job
  240. $requestData = [
  241. 'id_user' => $userId,
  242. 'linea' => $request->input('linea'),
  243. 'temp_files' => $tempFiles['files']
  244. ];
  245. //make sync the call to the job
  246. // Ejecutar el job sincrónicamente en lugar de despacharlo a la cola
  247. //(new ValidateLoadArchives($requestData, $userId, $jobId))->handle();
  248. ValidateLoadArchives::dispatch($requestData, $userId, $jobId);
  249. return $this->responseController->makeResponse(
  250. false,
  251. 'Validación iniciada',
  252. ['job_id' => $jobId, 'status' => 'processing']
  253. );
  254. }
  255. /**
  256. * Validates that Excel file has the correct headers in row 7
  257. * Each column must match the expected category name exactly
  258. */
  259. private function validateExcelHeaders($file)
  260. {
  261. try {
  262. $spreadsheet = IOFactory::load($file->getPathname());
  263. $worksheet = $spreadsheet->getActiveSheet();
  264. // Check each expected header in row 7
  265. foreach ($this->expectedHeaders as $header) {
  266. $cellValue = $worksheet->getCell($header['letter'] . '7')->getValue();
  267. if (trim($cellValue) !== $header['category']) {
  268. return [
  269. 'error' => true,
  270. 'message' => "El encabezado en la columna {$header['letter']} no coincide. Se esperaba: '{$header['category']}', se encontró: '{$cellValue}'"
  271. ];
  272. }
  273. }
  274. return ['error' => false];
  275. } catch (\Exception $e) {
  276. return ['error' => true, 'message' => 'Error al procesar el archivo Excel: ' . $e->getMessage()];
  277. }
  278. }
  279. /**
  280. * Validates ZIP file integrity and ensures it's not empty
  281. */
  282. private function validateZipFile($file)
  283. {
  284. $zip = new ZipArchive();
  285. $result = $zip->open($file->getPathname());
  286. if ($result !== TRUE) {
  287. return ['error' => true, 'message' => 'No se pudo abrir el archivo ZIP.'];
  288. }
  289. if ($zip->numFiles === 0) {
  290. $zip->close();
  291. return ['error' => true, 'message' => 'El archivo ZIP está vacío.'];
  292. }
  293. $zip->close();
  294. return ['error' => false];
  295. }
  296. /**
  297. * Extracts file listings from Excel starting from row 8
  298. * Validates file extensions and creates structured file list
  299. * Only processes rows with equipment code (column D)
  300. */
  301. private function extractAndValidateFiles($file)
  302. {
  303. try {
  304. $spreadsheet = IOFactory::load($file->getPathname());
  305. $worksheet = $spreadsheet->getActiveSheet();
  306. $highestRow = $worksheet->getHighestRow();
  307. $files = [];
  308. // Process each row starting from row 8 (data rows)
  309. for ($row = 8; $row <= $highestRow; $row++) {
  310. $equipmentCode = trim($worksheet->getCell('D' . $row)->getValue());
  311. if (empty($equipmentCode)) continue; // Skip rows without equipment code
  312. $hasFiles = false;
  313. $rowFiles = [];
  314. // Check each file category column (excluding equipment code column D)
  315. foreach ($this->expectedHeaders as $header) {
  316. if ($header['letter'] === 'D') continue;
  317. $cellValue = trim($worksheet->getCell($header['letter'] . $row)->getValue());
  318. if (empty($cellValue)) continue;
  319. // Handle multiple files separated by commas
  320. $fileNames = explode(',', $cellValue);
  321. foreach ($fileNames as $fileName) {
  322. $fileName = trim($fileName);
  323. if (empty($fileName)) continue;
  324. // Validate file extension
  325. $extension = strtolower(pathinfo($fileName, PATHINFO_EXTENSION));
  326. if (!isset($this->extensionTypes[$extension])) {
  327. return [
  328. 'error' => true,
  329. 'message' => "Extensión inválida '{$extension}' en archivo '{$fileName}' para equipo '{$equipmentCode}'"
  330. ];
  331. }
  332. $rowFiles[] = [
  333. 'equipment_code' => $equipmentCode,
  334. 'file_name' => $fileName,
  335. 'type' => $this->getFileType($extension)
  336. ];
  337. $hasFiles = true;
  338. }
  339. }
  340. if ($hasFiles) {
  341. $files = array_merge($files, $rowFiles);
  342. }
  343. }
  344. return ['error' => false, 'files' => $files];
  345. } catch (\Exception $e) {
  346. return ['error' => true, 'message' => 'Error al extraer archivos del Excel: ' . $e->getMessage()];
  347. }
  348. }
  349. /**
  350. * Compares file listings from Excel with actual files in ZIP
  351. * Returns validation status and lists of missing/extra files
  352. */
  353. private function compareExcelWithZip($excelFiles, $zipFile)
  354. {
  355. $zip = new ZipArchive();
  356. $zip->open($zipFile->getPathname());
  357. // Extract all file names and sizes from ZIP
  358. $zipFiles = [];
  359. $zipFileSizes = [];
  360. for ($i = 0; $i < $zip->numFiles; $i++) {
  361. $fullPath = $zip->getNameIndex($i);
  362. // Skip directories (entries ending with /)
  363. if (substr($fullPath, -1) === '/') {
  364. continue;
  365. }
  366. // Extract filename and size
  367. $fileName = basename($fullPath);
  368. if (!empty($fileName)) {
  369. $zipFiles[] = $fileName;
  370. $zipFileSizes[$fileName] = $zip->statIndex($i)['size'];
  371. }
  372. }
  373. $zip->close();
  374. // Validate file sizes using FunctionsController
  375. foreach ($zipFileSizes as $fileName => $size) {
  376. $extension = strtolower(pathinfo($fileName, PATHINFO_EXTENSION));
  377. if (!$this->functionsController->checkFileSize($extension, $size)) {
  378. $sizeMB = round($size / (1024 * 1024), 2);
  379. return [
  380. 'valid' => false,
  381. 'error' => "El archivo '{$fileName}' excede el límite de tamaño. Tamaño: {$sizeMB}MB"
  382. ];
  383. }
  384. }
  385. // Get file names from Excel listings
  386. $excelFileNames = array_column($excelFiles, 'file_name');
  387. // Find discrepancies between Excel and ZIP
  388. $missingInZip = array_diff($excelFileNames, $zipFiles);
  389. $extraInZip = array_diff($zipFiles, $excelFileNames);
  390. $isValid = empty($missingInZip) && empty($extraInZip);
  391. return [
  392. 'valid' => $isValid,
  393. 'missing_in_zip' => array_values($missingInZip),
  394. 'extra_in_zip' => array_values($extraInZip)
  395. ];
  396. }
  397. /**
  398. * Uploads Excel and ZIP files as temporary files using DocumentManagementController
  399. */
  400. private function uploadTempFiles(Request $request)
  401. {
  402. try {
  403. $tempFiles = [];
  404. // Upload Excel file
  405. $excelRequest = new Request();
  406. $excelRequest->files->set('file', $request->file('excel_file'));
  407. $excelRequest->merge([
  408. 'id_user' => $request->input('id_user'),
  409. 'linea' => $request->input('linea')
  410. ]);
  411. $excelResponse = $this->documentManagementController->uploadTempFile($excelRequest);
  412. $excelData = json_decode($excelResponse->getContent());
  413. if ($excelData->error) {
  414. $errorMsg = isset($excelData->message) ? $excelData->message : 'Error desconocido subiendo Excel';
  415. return ['error' => true, 'message' => 'Error subiendo Excel: ' . $errorMsg];
  416. }
  417. $tempFiles['excel'] = $excelData->response->idArchivo;
  418. // Upload ZIP file
  419. $zipRequest = new Request();
  420. $zipRequest->files->set('file', $request->file('zip_file'));
  421. $zipRequest->merge([
  422. 'id_user' => $request->input('id_user'),
  423. 'linea' => $request->input('linea')
  424. ]);
  425. $zipResponse = $this->documentManagementController->uploadTempFile($zipRequest);
  426. $zipData = json_decode($zipResponse->getContent());
  427. if ($zipData->error) {
  428. $errorMsg = isset($zipData->message) ? $zipData->message : 'Error desconocido subiendo ZIP';
  429. return ['error' => true, 'message' => 'Error subiendo ZIP: ' . $errorMsg];
  430. }
  431. $tempFiles['zip'] = $zipData->response->idArchivo;
  432. return ['error' => false, 'files' => $tempFiles];
  433. } catch (\Exception $e) {
  434. return ['error' => true, 'message' => 'Error en uploadTempFiles: ' . $e->getMessage()];
  435. }
  436. }
  437. /**
  438. * Gets file type based on extension
  439. */
  440. private function getFileType($extension){
  441. $typeMap = [
  442. 'zip' => 'STORAGE', 'rar' => 'STORAGE', 'tar' => 'STORAGE', '7z' => 'STORAGE',
  443. 'mp3' => 'AUDIO', 'mpeg' => 'AUDIO', 'wav' => 'AUDIO', 'ogg' => 'AUDIO', 'opus' => 'AUDIO',
  444. 'jpeg' => 'IMG', 'jpg' => 'IMG', 'png' => 'IMG', 'gif' => 'IMG', 'bmp' => 'IMG', 'tiff' => 'IMG', 'svg' => 'IMG',
  445. 'txt' => 'TEXT',
  446. 'webm' => 'VIDEO', 'mpeg4' => 'VIDEO', 'mp4' => 'VIDEO', '3gpp' => 'VIDEO', 'mov' => 'VIDEO', 'avi' => 'VIDEO', 'wmv' => 'VIDEO', 'flv' => 'VIDEO',
  447. 'dwg' => 'CAD', 'dxf' => 'CAD', '3ds' => '3D', 'ai' => 'ILLUSTRATOR', 'psd' => 'PHOTOSHOP',
  448. 'pdf' => 'PDF', 'xls' => 'EXCEL', 'xlsx' => 'EXCEL', 'ppt' => 'POWERPOINT', 'pptx' => 'POWERPOINT',
  449. 'doc' => 'WORD', 'docx' => 'WORD', 'vsd' => 'VISIO', 'vsdx' => 'VISIO'
  450. ];
  451. return $typeMap[$extension] ?? 'UNKNOWN';
  452. }
  453. }