AsyncValidateLoadArchivesController.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562
  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. if (!$comparison['valid']) {
  127. return $this->responseController->makeResponse(
  128. true,
  129. 'Los archivos no coinciden entre Excel y ZIP',
  130. $comparison,
  131. 400
  132. );
  133. }
  134. // Upload temp files
  135. $tempFiles = $this->uploadTempFiles($request);
  136. if ($tempFiles['error']) {
  137. return $this->responseController->makeResponse(true, $tempFiles['message'], [], 400);
  138. }
  139. // Log::info($tempFiles);
  140. $idFileZip = $this->encryptionController->decrypt($tempFiles['files']['zip']);
  141. if(!$idFileZip){
  142. return $this->responseController->makeResponse(true, "El id del archivo que desea eliminar no fue encriptado correctamente", [], 400);
  143. }
  144. // Log::info($idFileZip);
  145. // //Recuperar objeto del registro del zip
  146. // $zipFile = DB::table('S002V01TARTE')->where([
  147. // ['ARTE_IDAR', '=', $idFileZip],
  148. // ['ARTE_NULI', '=', $request['linea']]
  149. // ])->first();
  150. // //hay que usar la funcion de registerActivity para registrar en bd los movimiento como el de registrar un nuevo archivo en tmep
  151. $idFileExcel = $this->encryptionController->decrypt($tempFiles['files']['excel']);
  152. if(!$idFileExcel){
  153. return $this->responseController->makeResponse(true, "El id del archivo que desea eliminar no fue encriptado correctamente", [], 400);
  154. }
  155. // Log::info($idFileExcel);
  156. // //Recuperar objeto del registro del excel
  157. // $excelFile = DB::table('S002V01TARTE')->where([
  158. // ['ARTE_IDAR', '=', $idFileExcel],
  159. // ['ARTE_NULI', '=', $request['linea']]
  160. // ])->first();
  161. Log::info($idFileExcel);
  162. Log::info($idFileZip);
  163. // Guardar en bd::validation_jobs
  164. $jobId = DB::table('validation_jobs')->insertGetId([
  165. 'job_id' => $jobId,
  166. 'user_id' => $userId,
  167. 'linea' => $request->input('linea'),
  168. 'excel_temp_id' => $idFileExcel,
  169. 'zip_temp_id' => $idFileZip,
  170. 'created_at' => now(),
  171. 'updated_at' => now()
  172. ]);
  173. return $this->responseController->makeResponse(
  174. false,
  175. 'Validación iniciada',
  176. ['job_id' => $jobId, 'status' => 'processing']
  177. );
  178. }
  179. /**
  180. * Process endpoint that moves temp files to final storage
  181. */
  182. public function processLoadArchives(Request $request)
  183. {
  184. $validator = Validator::make($request->all(), [
  185. 'id_user' => 'required|string',
  186. 'linea' => 'required|integer',
  187. 'temp_files' => 'required|array',
  188. 'temp_files.excel' => 'required|string',
  189. 'temp_files.zip' => 'required|string',
  190. 'individual_temp_files' => 'required|array'
  191. ]);
  192. if ($validator->fails()) {
  193. return $this->responseController->makeResponse(
  194. true,
  195. 'Se encontraron uno o más errores.',
  196. $this->responseController->makeErrors($validator->errors()->messages()),
  197. 400
  198. );
  199. }
  200. $form = $request->all();
  201. $idUser = '0000000001';
  202. // $idUser = $this->encryptionController->decrypt($form['id_user']);
  203. // if (!$idUser) {
  204. // return $this->responseController->makeResponse(true, 'ID de usuario inválido.', [], 400);
  205. // }
  206. $finalFiles = [];
  207. try {
  208. // Move Excel to final
  209. $excelFinal = $this->moveToFinal($form['temp_files']['excel'], $form['linea'], $idUser);
  210. if (!$excelFinal) {
  211. return $this->responseController->makeResponse(true, 'Error procesando Excel', [], 400);
  212. }
  213. $finalFiles['excel'] = $excelFinal;
  214. // Move ZIP to final
  215. $zipFinal = $this->moveToFinal($form['temp_files']['zip'], $form['linea'], $idUser);
  216. if (!$zipFinal) {
  217. return $this->responseController->makeResponse(true, 'Error procesando ZIP', [], 400);
  218. }
  219. $finalFiles['zip'] = $zipFinal;
  220. // Move individual temp files to final storage
  221. $individualFiles = [];
  222. foreach ($form['individual_temp_files'] as $tempFile) {
  223. $finalFileId = $this->moveToFinal($tempFile['temp_id'], $form['linea'], $idUser);
  224. if ($finalFileId) {
  225. $individualFiles[] = [
  226. 'original_name' => $tempFile['original_name'],
  227. 'final_id' => $finalFileId
  228. ];
  229. }
  230. }
  231. $finalFiles['individual_files'] = $individualFiles;
  232. return $this->responseController->makeResponse(false, 'Archivos procesados exitosamente.', $finalFiles);
  233. } catch (\Exception $e) {
  234. return $this->responseController->makeResponse(true, 'Error interno: ' . $e->getMessage(), [], 500);
  235. }
  236. }
  237. /**
  238. * Moves a temporary file to final storage using DocumentManagementController
  239. */
  240. private function moveToFinal($tempId, $line, $idUser)
  241. {
  242. try {
  243. $tempIdDec = $this->encryptionController->decrypt($tempId);
  244. if (!$tempIdDec) {
  245. return false;
  246. }
  247. $tempFile = DB::table('S002V01TARTE')->where('ARTE_IDAR', $tempIdDec)->first();
  248. if (!$tempFile) {
  249. return false;
  250. }
  251. $result = $this->documentManagementController->moveFinalFile($line, 'ADSI', 'LA', $tempFile, $idUser);
  252. return $result[0] ? $result[1] : false;
  253. } catch (\Exception $e) {
  254. return false;
  255. }
  256. }
  257. /**
  258. * Validates that Excel file has the correct headers in row 7
  259. * Each column must match the expected category name exactly
  260. */
  261. private function validateExcelHeaders($file)
  262. {
  263. try {
  264. $spreadsheet = IOFactory::load($file->getPathname());
  265. $worksheet = $spreadsheet->getActiveSheet();
  266. // Check each expected header in row 7
  267. foreach ($this->expectedHeaders as $header) {
  268. $cellValue = $worksheet->getCell($header['letter'] . '7')->getValue();
  269. if (trim($cellValue) !== $header['category']) {
  270. return [
  271. 'error' => true,
  272. 'message' => "El encabezado en la columna {$header['letter']} no coincide. Se esperaba: '{$header['category']}', se encontró: '{$cellValue}'"
  273. ];
  274. }
  275. }
  276. return ['error' => false];
  277. } catch (\Exception $e) {
  278. return ['error' => true, 'message' => 'Error al procesar el archivo Excel: ' . $e->getMessage()];
  279. }
  280. }
  281. /**
  282. * Validates ZIP file integrity and ensures it's not empty
  283. */
  284. private function validateZipFile($file)
  285. {
  286. $zip = new ZipArchive();
  287. $result = $zip->open($file->getPathname());
  288. if ($result !== TRUE) {
  289. return ['error' => true, 'message' => 'No se pudo abrir el archivo ZIP.'];
  290. }
  291. if ($zip->numFiles === 0) {
  292. $zip->close();
  293. return ['error' => true, 'message' => 'El archivo ZIP está vacío.'];
  294. }
  295. $zip->close();
  296. return ['error' => false];
  297. }
  298. /**
  299. * Extracts file listings from Excel starting from row 8
  300. * Validates file extensions and creates structured file list
  301. * Only processes rows with equipment code (column D)
  302. */
  303. private function extractAndValidateFiles($file)
  304. {
  305. try {
  306. $spreadsheet = IOFactory::load($file->getPathname());
  307. $worksheet = $spreadsheet->getActiveSheet();
  308. $highestRow = $worksheet->getHighestRow();
  309. $files = [];
  310. // Process each row starting from row 8 (data rows)
  311. for ($row = 8; $row <= $highestRow; $row++) {
  312. $equipmentCode = trim($worksheet->getCell('D' . $row)->getValue());
  313. if (empty($equipmentCode)) continue; // Skip rows without equipment code
  314. $hasFiles = false;
  315. $rowFiles = [];
  316. // Check each file category column (excluding equipment code column D)
  317. foreach ($this->expectedHeaders as $header) {
  318. if ($header['letter'] === 'D') continue;
  319. $cellValue = trim($worksheet->getCell($header['letter'] . $row)->getValue());
  320. if (empty($cellValue)) continue;
  321. // Handle multiple files separated by commas
  322. $fileNames = explode(',', $cellValue);
  323. foreach ($fileNames as $fileName) {
  324. $fileName = trim($fileName);
  325. if (empty($fileName)) continue;
  326. // Validate file extension
  327. $extension = strtolower(pathinfo($fileName, PATHINFO_EXTENSION));
  328. if (!isset($this->extensionTypes[$extension])) {
  329. return [
  330. 'error' => true,
  331. 'message' => "Extensión inválida '{$extension}' en archivo '{$fileName}' para equipo '{$equipmentCode}'"
  332. ];
  333. }
  334. $rowFiles[] = [
  335. 'equipment_code' => $equipmentCode,
  336. 'file_name' => $fileName,
  337. 'type' => $this->getFileType($extension)
  338. ];
  339. $hasFiles = true;
  340. }
  341. }
  342. if ($hasFiles) {
  343. $files = array_merge($files, $rowFiles);
  344. }
  345. }
  346. return ['error' => false, 'files' => $files];
  347. } catch (\Exception $e) {
  348. return ['error' => true, 'message' => 'Error al extraer archivos del Excel: ' . $e->getMessage()];
  349. }
  350. }
  351. /**
  352. * Compares file listings from Excel with actual files in ZIP
  353. * Returns validation status and lists of missing/extra files
  354. */
  355. private function compareExcelWithZip($excelFiles, $zipFile)
  356. {
  357. $zip = new ZipArchive();
  358. $zip->open($zipFile->getPathname());
  359. // Extract all file names and sizes from ZIP
  360. $zipFiles = [];
  361. $zipFileSizes = [];
  362. for ($i = 0; $i < $zip->numFiles; $i++) {
  363. $fullPath = $zip->getNameIndex($i);
  364. // Skip directories (entries ending with /)
  365. if (substr($fullPath, -1) === '/') {
  366. continue;
  367. }
  368. // Extract filename and size
  369. $fileName = basename($fullPath);
  370. if (!empty($fileName)) {
  371. $zipFiles[] = $fileName;
  372. $zipFileSizes[$fileName] = $zip->statIndex($i)['size'];
  373. }
  374. }
  375. $zip->close();
  376. // Validate file sizes using FunctionsController
  377. foreach ($zipFileSizes as $fileName => $size) {
  378. $extension = strtolower(pathinfo($fileName, PATHINFO_EXTENSION));
  379. if (!$this->functionsController->checkFileSize($extension, $size)) {
  380. $sizeMB = round($size / (1024 * 1024), 2);
  381. return [
  382. 'valid' => false,
  383. 'error' => "El archivo '{$fileName}' excede el límite de tamaño. Tamaño: {$sizeMB}MB"
  384. ];
  385. }
  386. }
  387. // Get file names from Excel listings
  388. $excelFileNames = array_column($excelFiles, 'file_name');
  389. // Find discrepancies between Excel and ZIP
  390. $missingInZip = array_diff($excelFileNames, $zipFiles);
  391. $extraInZip = array_diff($zipFiles, $excelFileNames);
  392. $isValid = empty($missingInZip) && empty($extraInZip);
  393. return [
  394. 'valid' => $isValid,
  395. 'missing_in_zip' => array_values($missingInZip),
  396. 'extra_in_zip' => array_values($extraInZip)
  397. ];
  398. }
  399. /**
  400. * Uploads Excel and ZIP files as temporary files using DocumentManagementController
  401. */
  402. private function uploadTempFiles(Request $request)
  403. {
  404. try {
  405. $tempFiles = [];
  406. // Upload Excel file
  407. $excelRequest = new Request();
  408. $excelRequest->files->set('file', $request->file('excel_file'));
  409. $excelRequest->merge([
  410. 'id_user' => $request->input('id_user'),
  411. 'linea' => $request->input('linea')
  412. ]);
  413. $excelResponse = $this->documentManagementController->uploadTempFile($excelRequest);
  414. $excelData = json_decode($excelResponse->getContent());
  415. if ($excelData->error) {
  416. $errorMsg = isset($excelData->message) ? $excelData->message : 'Error desconocido subiendo Excel';
  417. return ['error' => true, 'message' => 'Error subiendo Excel: ' . $errorMsg];
  418. }
  419. $tempFiles['excel'] = $excelData->response->idArchivo;
  420. // Upload ZIP file
  421. $zipRequest = new Request();
  422. $zipRequest->files->set('file', $request->file('zip_file'));
  423. $zipRequest->merge([
  424. 'id_user' => $request->input('id_user'),
  425. 'linea' => $request->input('linea')
  426. ]);
  427. $zipResponse = $this->documentManagementController->uploadTempFile($zipRequest);
  428. $zipData = json_decode($zipResponse->getContent());
  429. if ($zipData->error) {
  430. $errorMsg = isset($zipData->message) ? $zipData->message : 'Error desconocido subiendo ZIP';
  431. return ['error' => true, 'message' => 'Error subiendo ZIP: ' . $errorMsg];
  432. }
  433. $tempFiles['zip'] = $zipData->response->idArchivo;
  434. return ['error' => false, 'files' => $tempFiles];
  435. } catch (\Exception $e) {
  436. return ['error' => true, 'message' => 'Error en uploadTempFiles: ' . $e->getMessage()];
  437. }
  438. }
  439. /**
  440. * Gets file type based on extension
  441. */
  442. private function getFileType($extension){
  443. $typeMap = [
  444. 'zip' => 'STORAGE', 'rar' => 'STORAGE', 'tar' => 'STORAGE', '7z' => 'STORAGE',
  445. 'mp3' => 'AUDIO', 'mpeg' => 'AUDIO', 'wav' => 'AUDIO', 'ogg' => 'AUDIO', 'opus' => 'AUDIO',
  446. 'jpeg' => 'IMG', 'jpg' => 'IMG', 'png' => 'IMG', 'gif' => 'IMG', 'bmp' => 'IMG', 'tiff' => 'IMG', 'svg' => 'IMG',
  447. 'txt' => 'TEXT',
  448. 'webm' => 'VIDEO', 'mpeg4' => 'VIDEO', 'mp4' => 'VIDEO', '3gpp' => 'VIDEO', 'mov' => 'VIDEO', 'avi' => 'VIDEO', 'wmv' => 'VIDEO', 'flv' => 'VIDEO',
  449. 'dwg' => 'CAD', 'dxf' => 'CAD', '3ds' => '3D', 'ai' => 'ILLUSTRATOR', 'psd' => 'PHOTOSHOP',
  450. 'pdf' => 'PDF', 'xls' => 'EXCEL', 'xlsx' => 'EXCEL', 'ppt' => 'POWERPOINT', 'pptx' => 'POWERPOINT',
  451. 'doc' => 'WORD', 'docx' => 'WORD', 'vsd' => 'VISIO', 'vsdx' => 'VISIO'
  452. ];
  453. return $typeMap[$extension] ?? 'UNKNOWN';
  454. }
  455. }