AsyncValidateLoadArchivesController.php 21 KB

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