AsyncValidateLoadArchivesController.php 21 KB

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