FailureLogController.php 56 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252
  1. <?php
  2. /*
  3. Desarrollador: Ing. Jean Jairo Benitez Meza
  4. Ultima Modificación: 21/09/2023
  5. Módulo: Analisis de Fallas
  6. */
  7. namespace App\Http\Controllers;
  8. use App\Http\Controllers\Controller;
  9. use App\Http\Controllers\ResponseController;
  10. use App\Http\Controllers\EncryptionController;
  11. use Illuminate\Http\Request;
  12. use Illuminate\Support\Carbon;
  13. use Illuminate\Support\Facades\DB;
  14. use Illuminate\Support\Facades\Validator;
  15. use App\Http\Controllers\FunctionsController;
  16. use Illuminate\Support\Facades\Storage;
  17. use Illuminate\Http\File;
  18. use PhpOffice\PhpSpreadsheet\Spreadsheet;
  19. use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
  20. use Dompdf\Dompdf;
  21. class FailureLogController extends Controller
  22. {
  23. private $responseController;
  24. private $encController;
  25. private $functionsController;
  26. private $resourcesController;
  27. public function __construct( ) {
  28. $this->responseController = new ResponseController();
  29. $this->encController = new EncryptionController();
  30. $this->functionsController = new FunctionsController();
  31. $this->resourcesController = new ResourcesController();
  32. }
  33. public function getFailureLog($user, $line) {
  34. try {
  35. $getFailureLog = DB::table('S002V01TBIFA')
  36. ->where('BIFA_NULI', '=', $line)
  37. ->where('LIFA_NULI', '=', $line)
  38. ->where('LISI_NULI', '=', $line)
  39. ->where('LIME_NULI', '=', $line)
  40. ->where('LIFA_ESTA', '=', 'Activo')
  41. ->where('LISI_ESTA', '=', 'Activo')
  42. ->where('LIME_ESTA', '=', 'Activo')
  43. ->join('S002V01TLIFA', 'LIFA_IDFA', '=', 'BIFA_IDFA')
  44. ->join('S002V01TLISI', 'LISI_IDSI', '=', 'BIFA_IDSI')
  45. ->join('S002V01TLIME', 'LIME_IDME', '=', 'BIFA_IDME')
  46. ->get([
  47. 'BIFA_NUFA AS NUMERO_FALLA',
  48. 'BIFA_COEQ AS CODIGO_EQUIPAMIENTO',
  49. 'BIFA_ESFA AS ESTADO_FALLA',
  50. 'LIFA_IDFA AS ID_FALLA',
  51. 'LIFA_NOFA AS NOMBRE_FALLA',
  52. 'LIFA_NIVE AS NIVEL_CRITICIDAD_FALLA',
  53. 'LIFA_CAUS AS CAUSA_FALLA',
  54. 'LISI_IDSI AS ID_SINTOMA',
  55. 'LISI_NOSI AS NOMBRE_SINTOMA',
  56. 'LISI_CLAS AS CLASIFICACION_SINTOMA',
  57. 'LISI_CAUS AS CAUSA_SINTOMA',
  58. 'BIFA_FEFA AS FECHA_FALLA',
  59. 'BIFA_REPA AS REPARABLE',
  60. 'BIFA_DESO AS SOLUCION',
  61. 'BIFA_COME AS COMENTARIOS',
  62. 'BIFA_VAOB AS VALOR_OBTENIDO',
  63. 'LIME_IDME AS ID_MEDIDA',
  64. 'LIME_NOME AS NOMBRE_MEDIDA',
  65. 'LIME_ACME AS ACRONIMO_MEDIDA',
  66. 'BIFA_ESTA AS ESTADO',
  67. 'BIFA_USRE AS USUARIO_REGISTRA',
  68. 'BIFA_FERE AS FECHA_REGISTRA',
  69. 'BIFA_USMO AS USUARIO_MODIFICA',
  70. 'BIFA_FEMO AS FECHA_MODIFICA',
  71. ]);
  72. } catch (\Throwable $th) {
  73. return $this->responseController->makeResponse(true, "ERR_FAILURELOG_GET000: No se pudo realizar la consulta a la base.", $th->getMessage(), 500);
  74. }
  75. $arrFailureLog = json_decode( json_encode( $getFailureLog ), true );
  76. $arrEquipmentCode = array_column($arrFailureLog, 'CODIGO_EQUIPAMIENTO');
  77. $arrEquipmentCode = array_unique($arrEquipmentCode);
  78. $count = 0;
  79. $arrEquipmentFailureLog = array();
  80. foreach ($arrEquipmentCode as $keyEquipmentCode => $equipmentCode) {
  81. $lastUpdate = '';
  82. $lastUser = '';
  83. $lastState = '';
  84. $lastClassification = '';
  85. $amountFailures = 0;
  86. foreach ($arrFailureLog as $keyFailureLog => $failureLog) {
  87. if ( $equipmentCode === $failureLog['CODIGO_EQUIPAMIENTO'] ) {
  88. $tempLastUpdate = '';
  89. $tempLastUser = '';
  90. if ( is_null($failureLog['FECHA_MODIFICA']) ) {
  91. $tempLastUpdate = $failureLog['FECHA_REGISTRA'];
  92. $tempLastUser = $failureLog['USUARIO_REGISTRA'];
  93. } else {
  94. $tempLastUpdate = $failureLog['FECHA_MODIFICA'];
  95. $tempLastUser = $failureLog['USUARIO_MODIFICA'];
  96. }
  97. if ($lastUpdate === '') {
  98. $lastUpdate = $tempLastUpdate;
  99. $lastUser = $tempLastUser;
  100. $lastState = $failureLog['ESTADO_FALLA'];
  101. $lastClassification = $failureLog['NIVEL_CRITICIDAD_FALLA'];
  102. } else {
  103. $carTempLastUpdate = Carbon::create($tempLastUpdate);
  104. $carLastState = Carbon::create($lastUpdate);
  105. if ($carTempLastUpdate->greaterThan($carLastState)) {
  106. $lastUpdate = $tempLastUpdate;
  107. $lastUser = $tempLastUser;
  108. $lastState = $failureLog['ESTADO_FALLA'];
  109. $lastClassification = $failureLog['NIVEL_CRITICIDAD_FALLA'];
  110. }
  111. }
  112. $amountFailures ++;
  113. }
  114. }
  115. $arrEquipmentFailureLog[$count]['CODIGO_EQUIPAMIENTO'] = $equipmentCode;
  116. $arrEquipmentFailureLog[$count]['ULTIMA_MODIFICACION'] = $lastUpdate;
  117. $arrEquipmentFailureLog[$count]['ULTIMO_USUARIO'] = $lastUser;
  118. $arrEquipmentFailureLog[$count]['ESTADO_ACTUAL'] = $lastState;
  119. $arrEquipmentFailureLog[$count]['CLASIFICACION_ACTUAL'] = $lastClassification;
  120. $arrEquipmentFailureLog[$count]['CANTIDAD_FALLAS'] = $amountFailures;
  121. $count++;
  122. }
  123. return $this->responseController->makeResponse(false, "ÉXITO: Consulta Exitosa", $arrEquipmentFailureLog);
  124. }
  125. public function getHistoryFailureEquipment($equipment, $user, $line) {
  126. try {
  127. $equipment = $this->encController->decrypt($equipment);
  128. } catch (\Throwable $th) {
  129. return $this->responseController->makeResponse(true, "ERR_GETHISTORY_GETEQUIPMENT000", $th->getMessage(), 500);
  130. }
  131. try {
  132. $getFailuresLog = DB::table('S002V01TBIFA')
  133. ->where('BIFA_COEQ', '=', $equipment)
  134. ->where('BIFA_NULI', '=', $line)
  135. ->where('BIFA_ESTA', '=', 'Activo')
  136. ->where('LIFA_ESTA', '=', 'Activo')
  137. ->where('LISI_ESTA', '=', 'Activo')
  138. ->where('LIME_ESTA', '=', 'Activo')
  139. ->join('S002V01TLIFA', 'LIFA_IDFA', '=', 'BIFA_IDFA')
  140. ->join('S002V01TLISI', 'LISI_IDSI', '=', 'BIFA_IDSI')
  141. ->join('S002V01TLIME', 'LIME_IDME', '=', 'BIFA_IDME')
  142. ->get([
  143. 'BIFA_NUFA AS NUMERO_FALLA',
  144. 'BIFA_COEQ AS CODIGO_EQUIPAMIENTO',
  145. 'BIFA_ESFA AS ESTADO_FALLA',
  146. 'LIFA_IDFA AS ID_FALLA',
  147. 'LIFA_NOFA AS NOMBRE_FALLA',
  148. 'LIFA_NIVE AS NIVEL_CRITICIDAD_FALLA',
  149. 'LIFA_CAUS AS CAUSA_FALLA',
  150. 'LISI_IDSI AS ID_SINTOMA',
  151. 'LISI_NOSI AS NOMBRE_SINTOMA',
  152. 'LISI_CLAS AS CLASIFICACION_SINTOMA',
  153. 'LISI_CAUS AS CAUSA_SINTOMA',
  154. 'BIFA_FEFA AS FECHA_FALLA',
  155. 'BIFA_REPA AS REPARABLE',
  156. 'BIFA_DESO AS SOLUCION',
  157. 'BIFA_COME AS COMENTARIOS',
  158. 'BIFA_VAOB AS VALOR_OBTENIDO',
  159. 'LIME_IDME AS ID_MEDIDA',
  160. 'LIME_NOME AS NOMBRE_MEDIDA',
  161. 'LIME_ACME AS ACRONIMO_MEDIDA',
  162. 'BIFA_USRE AS USUARIO_REGISTRA',
  163. 'BIFA_FERE AS FECHA_REGISTRA',
  164. 'BIFA_USMO AS USUARIO_MODIFICA',
  165. 'BIFA_FEMO AS FECHA_MODIFICA',
  166. ]);
  167. } catch (\Throwable $th) {
  168. return $this->responseController->makeResponse(true, "ERR_GETHISTORY_GETEQUIPMENT001: No se pudo realizar la consulta a la base.", $th->getMessage(), 500);
  169. }
  170. $arrFailureLogTemp = json_decode( json_encode( $getFailuresLog ), true );
  171. $arrFailureLog = array();
  172. foreach ($arrFailureLogTemp as $keyFailureLogTemp => $failureLogTemp) {
  173. $numero_falla = $failureLogTemp['NUMERO_FALLA'];
  174. $nombre_falla = $failureLogTemp['NOMBRE_FALLA'].' ('.$failureLogTemp['ID_FALLA'].')';
  175. $nivel_criticidad_falla = $failureLogTemp['NIVEL_CRITICIDAD_FALLA'];
  176. $nombre_sintoma = $failureLogTemp['NOMBRE_SINTOMA'].' ('.$failureLogTemp['ID_SINTOMA'].')';
  177. $estado_falla = $failureLogTemp['ESTADO_FALLA'];
  178. $causa = $failureLogTemp['CAUSA_FALLA'];
  179. $fecha_fala = $failureLogTemp['FECHA_FALLA'];
  180. $reparable = $failureLogTemp['REPARABLE'] === 1 ? 'Si' : 'No';
  181. $usuario_registra = is_null($failureLogTemp['USUARIO_MODIFICA']) ? $failureLogTemp['USUARIO_REGISTRA'] : $failureLogTemp['USUARIO_MODIFICA'];
  182. $fecha_registra = is_null($failureLogTemp['FECHA_MODIFICA']) ? $failureLogTemp['FECHA_REGISTRA'] : $failureLogTemp['FECHA_MODIFICA'];
  183. $arrFailureLog[] = [
  184. 'NUMERO_FALLA' => $numero_falla,
  185. 'NOMBRE_FALLA' => $nombre_falla,
  186. 'NIVEL_CRITICIDAD_FALLA' => $nivel_criticidad_falla,
  187. 'ESTADO_FALLA' => $estado_falla,
  188. 'CAUSA' => $causa,
  189. 'FECHA_FALLA' => $fecha_fala,
  190. 'REPARABLE' => $reparable,
  191. 'USUARIO_REGISTRA' => $usuario_registra,
  192. 'FECHA_REGISTRA' => $fecha_registra,
  193. ];
  194. }
  195. return $this->responseController->makeResponse(false, "ÉXITO: Consulta Exitosa", $arrFailureLog);
  196. }
  197. public function getDetailsHistoryFailureEquipment($failure, $user, $line) {
  198. try {
  199. $failure = $this->encController->decrypt($failure);
  200. } catch (\Throwable $th) {
  201. return $this->responseController->makeResponse(true, "ERR_GETHISTORY_GETEQUIPMENT000", $th->getMessage(), 500);
  202. }
  203. try {
  204. $getFailureLog = DB::table('S002V01TBIFA')
  205. ->where('BIFA_NUFA', '=', $failure)
  206. ->where('BIFA_NULI', '=', $line)
  207. ->where('LIFA_NULI', '=', $line)
  208. ->where('LISI_NULI', '=', $line)
  209. ->where('LIME_NULI', '=', $line)
  210. ->where('BIFA_ESTA', '=', 'Activo')
  211. ->where('LIFA_ESTA', '=', 'Activo')
  212. ->where('LISI_ESTA', '=', 'Activo')
  213. ->where('LIME_ESTA', '=', 'Activo')
  214. ->join('S002V01TLIFA', 'LIFA_IDFA', '=', 'BIFA_IDFA')
  215. ->join('S002V01TLISI', 'LISI_IDSI', '=', 'BIFA_IDSI')
  216. ->join('S002V01TLIME', 'LIME_IDME', '=', 'BIFA_IDME')
  217. ->first([
  218. 'BIFA_NUFA AS NUMERO_FALLA',
  219. 'BIFA_COEQ AS CODIGO_EQUIPAMIENTO',
  220. 'BIFA_ESFA AS ESTADO_FALLA',
  221. 'LIFA_IDFA AS ID_FALLA',
  222. 'LIFA_NOFA AS NOMBRE_FALLA',
  223. 'LIFA_NIVE AS NIVEL_CRITICIDAD_FALLA',
  224. 'LIFA_CAUS AS CAUSA_FALLA',
  225. 'LISI_IDSI AS ID_SINTOMA',
  226. 'LISI_NOSI AS NOMBRE_SINTOMA',
  227. 'LISI_CLAS AS CLASIFICACION_SINTOMA',
  228. 'LISI_CAUS AS CAUSA_SINTOMA',
  229. 'BIFA_FEFA AS FECHA_FALLA',
  230. 'BIFA_FERF AS FECHA_RESOLUCION',
  231. 'BIFA_REPA AS REPARABLE',
  232. 'BIFA_DESO AS SOLUCION',
  233. 'BIFA_COME AS COMENTARIOS',
  234. 'BIFA_VAOB AS VALOR_OBTENIDO',
  235. 'LIME_IDME AS ID_MEDIDA',
  236. 'LIME_NOME AS NOMBRE_MEDIDA',
  237. 'LIME_ACME AS ACRONIMO_MEDIDA',
  238. ]);
  239. } catch (\Throwable $th) {
  240. return $this->responseController->makeResponse(true, "ERR_FAILURELOG_GETACTIVE000: No se pudo realizar la consulta a la base.", $th->getMessage(), 500);
  241. }
  242. $arrFailureLog = json_decode( json_encode( $getFailureLog ), true );
  243. return $this->responseController->makeResponse(false, "ÉXITO: Consulta Exitosa", $arrFailureLog);
  244. }
  245. public function getFailureLogActives($user, $line) {
  246. try {
  247. $getFailureLog = DB::table('S002V01TBIFA')
  248. ->where('BIFA_NULI', '=', $line)
  249. ->where('LIFA_NULI', '=', $line)
  250. ->where('LISI_NULI', '=', $line)
  251. ->where('LIME_NULI', '=', $line)
  252. ->where('BIFA_ESTA', '=', 'Activo')
  253. ->where('LIFA_ESTA', '=', 'Activo')
  254. ->where('LISI_ESTA', '=', 'Activo')
  255. ->where('LIME_ESTA', '=', 'Activo')
  256. ->join('S002V01TLIFA', 'LIFA_IDFA', '=', 'BIFA_IDFA')
  257. ->join('S002V01TLISI', 'LISI_IDSI', '=', 'BIFA_IDSI')
  258. ->join('S002V01TLIME', 'LIME_IDME', '=', 'BIFA_IDME')
  259. ->get([
  260. 'BIFA_NUFA AS NUMERO_FALLA',
  261. 'BIFA_COEQ AS CODIGO_EQUIPAMIENTO',
  262. 'LIFA_IDFA AS ID_FALLA',
  263. 'LIFA_NOFA AS NOMBRE_FALLA',
  264. 'LIFA_NIVE AS NIVEL_CRITICIDAD_FALLA',
  265. 'LIFA_CAUS AS CAUSA_FALLA',
  266. 'LISI_IDSI AS ID_SINTOMA',
  267. 'LISI_NOSI AS NOMBRE_SINTOMA',
  268. 'LISI_CLAS AS CLASIFICACION_SINTOMA',
  269. 'LISI_CAUS AS CAUSA_SINTOMA',
  270. 'BIFA_FEFA AS FECHA_FALLA',
  271. 'BIFA_REPA AS REPARABLE',
  272. 'BIFA_DESO AS SOLUCION',
  273. 'BIFA_COME AS COMENTARIOS',
  274. 'BIFA_VAOB AS VALOR_OBTENIDO',
  275. 'LIME_IDME AS ID_MEDIDA',
  276. 'LIME_NOME AS NOMBRE_MEDIDA',
  277. 'LIME_ACME AS ACRONIMO_MEDIDA',
  278. 'BIFA_USRE AS USUARIO_REGISTRA',
  279. 'BIFA_FERE AS FECHA_REGISTRA',
  280. 'BIFA_USMO AS USUARIO_MODIFICA',
  281. 'BIFA_FEMO AS FECHA_MODIFICA',
  282. ]);
  283. } catch (\Throwable $th) {
  284. return $this->responseController->makeResponse(true, "ERR_FAILURELOG_GETACTIVE000: No se pudo realizar la consulta a la base.", $th->getMessage(), 500);
  285. }
  286. $arrFailureLog = json_decode( json_encode( $getFailureLog ), true );
  287. return $this->responseController->makeResponse(false, "ÉXITO: Consulta Exitosa", $arrFailureLog);
  288. }
  289. public function getEquipmentByFailure($failure, $user, $line) {
  290. try {
  291. $failure = $this->encController->decrypt($failure);
  292. } catch (\Throwable $th) {
  293. DB::rollBack();
  294. return $this->responseController->makeResponse(true, "ERR_FAILURES_EQUIPMENT000: No se pudo obtener el usuario.", $th->getMessage(), 500);
  295. }
  296. try {
  297. $getEquipmentByFailure = DB::table('S002V01TBIFA')
  298. ->where('BIFA_IDFA', '=', $failure)
  299. ->where('BIFA_NULI', '=', $line)
  300. ->where('BIFA_ESTA', '=', 'Activo')
  301. ->join('S002V01TLIME', 'LIME_IDME', '=', 'BIFA_IDME')
  302. ->get([
  303. 'BIFA_COEQ AS CODIGO_EQUIPAMIENTO',
  304. 'BIFA_ESFA AS ESTADO_FALLA',
  305. 'BIFA_FEFA AS FECHA_FALLA',
  306. 'BIFA_FERF AS FECHA_REPARACION',
  307. 'BIFA_VAOB AS VALOR_OBTENIDO',
  308. 'LIME_IDME AS ID_MEDIDA',
  309. 'LIME_NOME AS NOMBRE_MEDIDA',
  310. 'LIME_ACME AS ACRONIMO_MEDIDA',
  311. ]);
  312. } catch (\Throwable $th) {
  313. return $this->responseController->makeResponse(
  314. true,
  315. "ERR_FAILURES_EQUIPMENT001: No se pudo realizar la consulta a la base.",
  316. $th->getMessage(),
  317. 500
  318. );
  319. }
  320. $arrFaultyEquipment = json_decode(json_encode($getEquipmentByFailure), true);
  321. $arrEquipmentCode = array_column($arrFaultyEquipment, 'CODIGO_EQUIPAMIENTO');
  322. $arrEquipmentCode = array_unique($arrEquipmentCode);
  323. $count = 0;
  324. $arrEquipmentFailureLog = array();
  325. foreach ($arrEquipmentCode as $keyEquipmentCode => $equipmentCode) {
  326. foreach ($arrFaultyEquipment as $keyFaultyEquipment => $faultyEquipment) {
  327. if ( $equipmentCode === $faultyEquipment['CODIGO_EQUIPAMIENTO'] ) {
  328. $arrEquipmentFailureLog[$count]['CODIGO_EQUIPAMIENTO'] = $faultyEquipment['CODIGO_EQUIPAMIENTO'];
  329. $arrEquipmentFailureLog[$count]['DETALLES'][] = [
  330. 'ESTADO_FALLA' => $faultyEquipment['ESTADO_FALLA'],
  331. 'FECHA_FALLA' => $faultyEquipment['FECHA_FALLA'],
  332. 'FECHA_REPARACION' => $faultyEquipment['FECHA_REPARACION'],
  333. 'VALOR_OBTENIDO' => $faultyEquipment['VALOR_OBTENIDO'],
  334. 'ID_MEDIDA' => $faultyEquipment['ID_MEDIDA'],
  335. 'NOMBRE_MEDIDA' => $faultyEquipment['NOMBRE_MEDIDA'],
  336. 'ACRONIMO_MEDIDA' => $faultyEquipment['ACRONIMO_MEDIDA'],
  337. ];
  338. }
  339. }
  340. $count++;
  341. }
  342. return $this->responseController->makeResponse(false, "ÉXITO: Consulta Exitosa", $arrEquipmentFailureLog);
  343. }
  344. public function getAlarmEmissionFromFailures($user, $line) {
  345. try {
  346. $getAlarmEmission = DB::table('S002V01TBIFA')
  347. ->where('BIFA_NULI', '=', $line)
  348. ->where('BIFA_ESTA', '=', 'Activo')
  349. ->where('ACTI_NULI', '=', $line)
  350. ->join('S002V01TACTI', 'ACTI_IDAC', '=', 'BIFA_IDAC')
  351. ->get([
  352. 'BIFA_NUFA AS NUMERO_FALLA',
  353. 'BIFA_COEQ AS CODIGO_EQUIPAMIENTO',
  354. 'ACTI_IDAC AS ID_ACTIVADOR',
  355. 'BIFA_FEFA AS FECHA_FALLA',
  356. 'ACTI_PRIO AS PRIORIDAD',
  357. 'ACTI_TIAC AS TIPO_ACTIVADOR',
  358. 'ACTI_COAC AS CONDICION_ACTIVADOR',
  359. ]);
  360. } catch (\Throwable $th) {
  361. DB::rollBack();
  362. return $this->responseController->makeResponse(true, "ERR_FAILURES_ALARM000: No se pudo obtener la información de la base de datos.", $th->getMessage(), 500);
  363. }
  364. $arrAlarmEmission = json_decode(json_encode($getAlarmEmission), true);
  365. foreach ($arrAlarmEmission as $key => $alarmEmission) {
  366. switch ($alarmEmission['PRIORIDAD']) {
  367. case 1:
  368. $alarmEmission['PRIORIDAD'] = 'Muy alta';
  369. break;
  370. case 2:
  371. $alarmEmission['PRIORIDAD'] = 'Alta';
  372. break;
  373. case 3:
  374. $alarmEmission['PRIORIDAD'] = 'Media';
  375. break;
  376. case 4:
  377. $alarmEmission['PRIORIDAD'] = 'Baja';
  378. break;
  379. }
  380. $arrAlarmEmission[$key] = $alarmEmission;
  381. }
  382. return $this->responseController->makeResponse(false, "ÉXITO: Consulta Exitosa", $arrAlarmEmission);
  383. }
  384. public function generateSheetFailureXLS($failure, $user, $line) {
  385. try {
  386. $failure = $this->encController->decrypt($failure);
  387. } catch (\Throwable $th) {
  388. return $this->responseController->makeResponse(true, "ERR_FAILURELOG_GENERATEXLS000", $th->getMessage(), 500);
  389. }
  390. try {
  391. $user = $this->encController->decrypt($user);
  392. } catch (\Throwable $th) {
  393. return $this->responseController->makeResponse(true, "ERR_FAILURELOG_GENERATEXLS001", $th->getMessage(), 500);
  394. }
  395. try {
  396. $getFailureLog = DB::table('S002V01TBIFA')
  397. ->where('BIFA_NUFA', '=', $failure)
  398. ->where('BIFA_NULI', '=', $line)
  399. ->where('LIFA_NULI', '=', $line)
  400. ->where('LISI_NULI', '=', $line)
  401. ->where('LIME_NULI', '=', $line)
  402. ->where('BIFA_ESTA', '=', 'Activo')
  403. ->where('LIFA_ESTA', '=', 'Activo')
  404. ->where('LISI_ESTA', '=', 'Activo')
  405. ->where('LIME_ESTA', '=', 'Activo')
  406. ->join('S002V01TLIFA', 'LIFA_IDFA', '=', 'BIFA_IDFA')
  407. ->join('S002V01TLISI', 'LISI_IDSI', '=', 'BIFA_IDSI')
  408. ->join('S002V01TLIME', 'LIME_IDME', '=', 'BIFA_IDME')
  409. ->first([
  410. 'BIFA_NUFA AS NUMERO_FALLA',
  411. 'BIFA_COEQ AS CODIGO_EQUIPAMIENTO',
  412. 'BIFA_ESFA AS ESTADO_FALLA',
  413. 'LIFA_IDFA AS ID_FALLA',
  414. 'LIFA_NOFA AS NOMBRE_FALLA',
  415. 'LIFA_NIVE AS NIVEL_CRITICIDAD_FALLA',
  416. 'LIFA_CAUS AS CAUSA_FALLA',
  417. 'LISI_IDSI AS ID_SINTOMA',
  418. 'LISI_NOSI AS NOMBRE_SINTOMA',
  419. 'LISI_CLAS AS CLASIFICACION_SINTOMA',
  420. 'LISI_CAUS AS CAUSA_SINTOMA',
  421. 'BIFA_FEFA AS FECHA_FALLA',
  422. 'BIFA_FERF AS FECHA_RESOLUCION',
  423. 'BIFA_REPA AS REPARABLE',
  424. 'BIFA_DESO AS SOLUCION',
  425. 'BIFA_COME AS COMENTARIOS',
  426. 'BIFA_VAOB AS VALOR_OBTENIDO',
  427. 'LIME_IDME AS ID_MEDIDA',
  428. 'LIME_NOME AS NOMBRE_MEDIDA',
  429. 'LIME_ACME AS ACRONIMO_MEDIDA',
  430. ]);
  431. } catch (\Throwable $th) {
  432. return $this->responseController->makeResponse(true, "ERR_FAILURELOG_GENERATEXLS002: No se pudo realizar la consulta a la base.", $th->getMessage(), 500);
  433. }
  434. $arrFailureLog = json_decode( json_encode( $getFailureLog ), true );
  435. $spreadsheet = new Spreadsheet();
  436. $activeWorksheet = $spreadsheet->getActiveSheet()->setTitle($arrFailureLog['NUMERO_FALLA']);
  437. $activeWorksheet->setCellValue("A1", 'Número de Falla');
  438. $activeWorksheet->setCellValue("B1", $arrFailureLog['NUMERO_FALLA']);
  439. $activeWorksheet->getStyle( "A1" )->getFont()->setBold(true);
  440. $activeWorksheet->setCellValue("A2", 'Número de Equipamiento');
  441. $activeWorksheet->setCellValue("B2", $arrFailureLog['CODIGO_EQUIPAMIENTO']);
  442. $activeWorksheet->getStyle( "A2" )->getFont()->setBold(true);
  443. $activeWorksheet->setCellValue("A3", 'Estado de la Falla');
  444. $activeWorksheet->setCellValue("B3", $arrFailureLog['ESTADO_FALLA']);
  445. $activeWorksheet->getStyle( "A3" )->getFont()->setBold(true);
  446. $activeWorksheet->setCellValue("A4", 'Nombre de la Falla');
  447. $activeWorksheet->setCellValue("B4", $arrFailureLog['NOMBRE_FALLA'].' ('.$arrFailureLog['ID_FALLA'].')');
  448. $activeWorksheet->getStyle( "A4" )->getFont()->setBold(true);
  449. $activeWorksheet->setCellValue("A5", 'Nivel de Criticidad de la Falla');
  450. $activeWorksheet->setCellValue("B5", $arrFailureLog['NIVEL_CRITICIDAD_FALLA']);
  451. $activeWorksheet->getStyle( "A5" )->getFont()->setBold(true);
  452. $activeWorksheet->setCellValue("A6", 'Causa de la Falla');
  453. $activeWorksheet->setCellValue("B6", $arrFailureLog['CAUSA_FALLA']);
  454. $activeWorksheet->getStyle( "A6" )->getFont()->setBold(true);
  455. $activeWorksheet->setCellValue("A7", 'Nombre del Síntoma');
  456. $activeWorksheet->setCellValue("B7", $arrFailureLog['NOMBRE_SINTOMA'].' ('.$arrFailureLog['ID_SINTOMA'].')');
  457. $activeWorksheet->getStyle( "A7" )->getFont()->setBold(true);
  458. $activeWorksheet->setCellValue("A8", 'Clasificación del Síntoma');
  459. $activeWorksheet->setCellValue("B8", $arrFailureLog['CLASIFICACION_SINTOMA']);
  460. $activeWorksheet->getStyle( "A8" )->getFont()->setBold(true);
  461. $activeWorksheet->setCellValue("A9", 'Causa del Síntoma');
  462. $activeWorksheet->setCellValue("B9", $arrFailureLog['CAUSA_SINTOMA']);
  463. $activeWorksheet->getStyle( "A9" )->getFont()->setBold(true);
  464. $activeWorksheet->setCellValue("A10", 'Fecha de la Falla');
  465. $activeWorksheet->setCellValue("B10", $arrFailureLog['FECHA_FALLA']);
  466. $activeWorksheet->getStyle( "A10" )->getFont()->setBold(true);
  467. $activeWorksheet->setCellValue("A11", 'Fecha de la Resolución');
  468. $activeWorksheet->setCellValue("B11", $arrFailureLog['FECHA_RESOLUCION']);
  469. $activeWorksheet->getStyle( "A11" )->getFont()->setBold(true);
  470. $activeWorksheet->setCellValue("A12", 'Reparable');
  471. $activeWorksheet->setCellValue("B12", $arrFailureLog['REPARABLE'] === 1 ? 'Si' : 'No');
  472. $activeWorksheet->getStyle( "A12" )->getFont()->setBold(true);
  473. $activeWorksheet->setCellValue("A13", 'Valor Obtenido');
  474. $activeWorksheet->setCellValue("B13", $arrFailureLog['VALOR_OBTENIDO'].' '.$arrFailureLog['ACRONIMO_MEDIDA']);
  475. $activeWorksheet->getStyle( "A13" )->getFont()->setBold(true);
  476. $activeWorksheet->setCellValue("A14", 'Tipo de Medido');
  477. $activeWorksheet->setCellValue("B14", $arrFailureLog['NOMBRE_MEDIDA']);
  478. $activeWorksheet->getStyle( "A14" )->getFont()->setBold(true);
  479. $activeWorksheet->setCellValue("A15", 'Solución de la Falla');
  480. $activeWorksheet->setCellValue("B15", $arrFailureLog['SOLUCION'] === null || $arrFailureLog['SOLUCION'] === '' ? '' : $arrFailureLog['SOLUCION']);
  481. $activeWorksheet->getStyle( "A15" )->getFont()->setBold(true);
  482. $activeWorksheet->setCellValue("A16", 'Comentarios');
  483. $activeWorksheet->setCellValue("B16", $arrFailureLog['COMENTARIOS']);
  484. $activeWorksheet->getStyle( "A16" )->getFont()->setBold(true);
  485. $activeWorksheet->getColumnDimension("A")->setAutoSize(true);
  486. $activeWorksheet->getColumnDimension("B")->setAutoSize(true);
  487. $nuli = $this->resourcesController->formatSecuence($line, 2);
  488. $como = 'ANFA'; // Código del módulo
  489. $cldo = 'IN'; // Código de la clasificación
  490. $fecr = date('ymd'); // Fecha en la se carga el archivo
  491. try {
  492. $arrSecuence = (array) DB::table('S002V01TAFAL')
  493. ->where('AFAL_COMO', '=', $como)
  494. ->where('AFAL_CLDO', '=', $cldo)
  495. ->where('AFAL_NULI', '=', $line)
  496. ->orderBy('AFAL_NUSE', 'desc')
  497. ->first([ 'AFAL_NUSE' ]);
  498. } catch (\Throwable $th) {
  499. return $this->responseController->makeResponse(
  500. true,
  501. "ERR_FAILURELOG_GENERATEXLS003: Ocurrió un error al obtener la información de la secuencia.",
  502. $th->getMessage(),
  503. 500
  504. );
  505. }
  506. $nuse = 1; // Secuencia del documento
  507. if ( !empty( $arrSecuence ) && !is_null( $arrSecuence ) ) {
  508. $nuse = intval( $arrSecuence['AFAL_NUSE'] ) + 1;
  509. }
  510. $nuse = $this->resourcesController->formatSecuence($nuse, 6);
  511. $nuve = $this->resourcesController->formatSecuence('1', 2);
  512. $noar = 'ficha_de_falla_'.$arrFailureLog['NUMERO_FALLA'];
  513. $exte = 'xlsx';
  514. if ($_SERVER['SERVER_NAME'] === '192.168.100.105') {
  515. $filePath = 'C:/ITTEC/SAM/Dev/SistemaMantenimiento/sistema-mantenimiento-back/public/public_files/'; // API JEAN
  516. } else {
  517. $filePath = 'C:\inetpub\wwwroot\sam\public_files\\'; // API QA
  518. }
  519. $fileName = $nuli.'-'.$como.'-'.$cldo.'-'.$fecr.'-'.$nuse.'='.$nuve.'='.$noar.'.'.$exte;
  520. $tempFile = $filePath.$fileName;
  521. if ( file_exists( $tempFile ) ) {
  522. if ( !unlink( $tempFile ) ) {
  523. return $this->responseController->makeResponse(
  524. true,
  525. "ERR_FAILURELOG_GENERATEXLS004: Ocurrió un error al eliminar el siguiente archivo: " . $tempFile,
  526. [],
  527. 500
  528. );
  529. }
  530. }
  531. try {
  532. $writer = new Xlsx($spreadsheet);
  533. ob_start();
  534. $writer->save('php://output');
  535. $base64 = base64_encode(ob_get_clean());
  536. $validate = \File::put( $tempFile, base64_decode($base64));
  537. } catch (\Throwable $th) {
  538. return $this->responseController->makeResponse(
  539. true,
  540. "ERR_FAILURELOG_GENERATEXLS005: Ocurrió un error al guardar el documento.",
  541. $th->getMessage(),
  542. 500
  543. );
  544. }
  545. $ubic = Storage::putFile('files', new File($tempFile));
  546. $ubic = str_replace("/", "\\", $ubic);
  547. if ($_SERVER['SERVER_NAME'] === '192.168.100.105') {
  548. $ubic = "C:\ITTEC\SAM\Dev\SistemaMantenimiento\sistema-mantenimiento-back\storage\app\\" . $ubic;
  549. } else {
  550. $ubic = "C:\inetpub\wwwroot\sam\storage\app\\" . $ubic;
  551. }
  552. $tama = filesize($ubic);
  553. $usac = json_encode([$user]);
  554. $now = $this->functionsController->now();
  555. $currentDate = $now->toDateTimeString();
  556. try {
  557. $validateInsert = DB::table('S002V01TAFAL')->insert([
  558. 'AFAL_NULI' => $line,
  559. 'AFAL_COMO' => $como,
  560. 'AFAL_CLDO' => $cldo,
  561. 'AFAL_FECR' => $fecr,
  562. 'AFAL_NUSE' => $nuse,
  563. 'AFAL_NUVE' => $nuve,
  564. 'AFAL_NOAR' => $noar,
  565. 'AFAL_EXTE' => $exte,
  566. 'AFAL_TAMA' => $tama,
  567. 'AFAL_UBIC' => $ubic,
  568. 'AFAL_USAC' => $usac,
  569. 'AFAL_USRE' => $user,
  570. 'AFAL_FERE' => $currentDate,
  571. ]);
  572. } catch (\Throwable $th) {
  573. return $this->responseController->makeResponse(
  574. true,
  575. "ERR_FAILURELOG_GENERATEXLS006: Ocurrió un error guardar los datos a la tabla final de archivos.",
  576. $th->getMessage(),
  577. 500
  578. );
  579. }
  580. if ( !$validateInsert ) {
  581. return $this->responseController->makeResponse(
  582. true,
  583. "ERR_FAILURELOG_GENERATEXLS007: No se pudo guardar la ficha del proveedor en la base de datos.",
  584. [],
  585. 500
  586. );
  587. }
  588. if ($_SERVER['SERVER_NAME'] === '192.168.100.105') {
  589. $urlPublic = 'http://192.168.100.105:8000/public_files/' . $fileName;
  590. } else {
  591. $urlPublic = $this->functionsController->getApiURI().'sam/public_files/' . $fileName;
  592. }
  593. return $this->responseController->makeResponse(false, "EXITO: Modificación Exitosa", [ 'url' => $urlPublic ]);
  594. return $arrFailureLog;
  595. }
  596. public function generateSheetFailurePDF($failure, $user, $line){
  597. try {
  598. $failure = $this->encController->decrypt($failure);
  599. } catch (\Throwable $th) {
  600. return $this->responseController->makeResponse(true, "ERR_FAILURELOG_GENERATEPDF000: Ocurrió un error al obtener la falla.", $th->getMessage(), 500);
  601. }
  602. try {
  603. $user = $this->encController->decrypt($user);
  604. } catch (\Throwable $th) {
  605. return $this->responseController->makeResponse(true, "ERR_FAILURELOG_GENERATEPDF001: Ocurrió un error al obtener el usuario.", $th->getMessage(), 500);
  606. }
  607. try {
  608. $getFailureLog = DB::table('S002V01TBIFA')
  609. ->where('BIFA_NUFA', '=', $failure)
  610. ->where('BIFA_NULI', '=', $line)
  611. ->where('LIFA_NULI', '=', $line)
  612. ->where('LISI_NULI', '=', $line)
  613. ->where('LIME_NULI', '=', $line)
  614. ->where('BIFA_ESTA', '=', 'Activo')
  615. ->where('LIFA_ESTA', '=', 'Activo')
  616. ->where('LISI_ESTA', '=', 'Activo')
  617. ->where('LIME_ESTA', '=', 'Activo')
  618. ->join('S002V01TLIFA', 'LIFA_IDFA', '=', 'BIFA_IDFA')
  619. ->join('S002V01TLISI', 'LISI_IDSI', '=', 'BIFA_IDSI')
  620. ->join('S002V01TLIME', 'LIME_IDME', '=', 'BIFA_IDME')
  621. ->first([
  622. 'BIFA_NUFA AS NUMERO_FALLA',
  623. 'BIFA_COEQ AS CODIGO_EQUIPAMIENTO',
  624. 'BIFA_ESFA AS ESTADO_FALLA',
  625. 'LIFA_IDFA AS ID_FALLA',
  626. 'LIFA_NOFA AS NOMBRE_FALLA',
  627. 'LIFA_NIVE AS NIVEL_CRITICIDAD_FALLA',
  628. 'LIFA_CAUS AS CAUSA_FALLA',
  629. 'LISI_IDSI AS ID_SINTOMA',
  630. 'LISI_NOSI AS NOMBRE_SINTOMA',
  631. 'LISI_CLAS AS CLASIFICACION_SINTOMA',
  632. 'LISI_CAUS AS CAUSA_SINTOMA',
  633. 'BIFA_FEFA AS FECHA_FALLA',
  634. 'BIFA_FERF AS FECHA_RESOLUCION',
  635. 'BIFA_REPA AS REPARABLE',
  636. 'BIFA_DESO AS SOLUCION',
  637. 'BIFA_COME AS COMENTARIOS',
  638. 'BIFA_VAOB AS VALOR_OBTENIDO',
  639. 'LIME_IDME AS ID_MEDIDA',
  640. 'LIME_NOME AS NOMBRE_MEDIDA',
  641. 'LIME_ACME AS ACRONIMO_MEDIDA',
  642. ]);
  643. } catch (\Throwable $th) {
  644. return $this->responseController->makeResponse(true, "ERR_FAILURELOG_GENERATEPDF002: No se pudo realizar la consulta a la base.", $th->getMessage(), 500);
  645. }
  646. $arrFailureLog = json_decode( json_encode( $getFailureLog ), true );
  647. // return $arrFailureLog;
  648. $html = '<!DOCTYPE html>
  649. <html lang="en">
  650. <head>
  651. <meta charset="UTF-8">
  652. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  653. <title>Document</title>
  654. <style>
  655. body {
  656. font-family: Arial, Helvetica, sans-serif;
  657. font-size: 8pt;
  658. }
  659. .section-title {
  660. margin: 20px 0;
  661. font-size: 12pt;
  662. }
  663. table {
  664. width: 100%;
  665. }
  666. thead tr th {
  667. text-align: start;
  668. padding: 0 16px;
  669. height: 52px;
  670. border-bottom-width: 1px;
  671. border-bottom-style: solid;
  672. border-bottom-color: #CCC;
  673. }
  674. tbody tr td {
  675. padding: 0 16px;
  676. height: 52px;
  677. border-bottom-width: 1px;
  678. border-bottom-style: solid;
  679. border-bottom-color: #CCC;
  680. }
  681. p {
  682. text-align: justify;
  683. }
  684. </style>
  685. </head>
  686. <body>
  687. <div class="item">
  688. <b>Número de Falla: </b>
  689. <span>'.$arrFailureLog['NUMERO_FALLA'].'</span>
  690. </div>
  691. <div class="item">
  692. <b>Número de Equipamiento: </b>
  693. <span>'.$arrFailureLog['CODIGO_EQUIPAMIENTO'].'</span>
  694. </div>
  695. <div class="item">
  696. <b>Estado de la Falla: </b>
  697. <span>'.$arrFailureLog['ESTADO_FALLA'].'</span>
  698. </div>
  699. <div class="item">
  700. <b>Nombre de la Falla: </b>
  701. <span>'.$arrFailureLog['NOMBRE_FALLA'].' ('.$arrFailureLog['ID_FALLA'].')'.'</span>
  702. </div>
  703. <div class="item">
  704. <b>Nivel de Criticidad de la Falla: </b>
  705. <span>'.$arrFailureLog['NIVEL_CRITICIDAD_FALLA'].'</span>
  706. </div>
  707. <div class="item">
  708. <b>Causa de la Falla: </b>
  709. <span>'.$arrFailureLog['CAUSA_FALLA'].'</span>
  710. </div>
  711. <div class="item">
  712. <b>Nombre del Síntoma: </b>
  713. <span>'.$arrFailureLog['NOMBRE_SINTOMA'].' ('.$arrFailureLog['ID_SINTOMA'].')'.'</span>
  714. </div>
  715. <div class="item">
  716. <b>Clasificación del Síntoma: </b>
  717. <span>'.$arrFailureLog['CLASIFICACION_SINTOMA'].'</span>
  718. </div>
  719. <div class="item">
  720. <b>Causa del Síntoma: </b>
  721. <span>'.$arrFailureLog['CAUSA_SINTOMA'].'</span>
  722. </div>
  723. <div class="item">
  724. <b>Fecha de la Falla: </b>
  725. <span>'.$arrFailureLog['FECHA_FALLA'].'</span>
  726. </div>
  727. <div class="item">
  728. <b>Fecha de la Resolución: </b>
  729. <span>'.$arrFailureLog['FECHA_RESOLUCION'].'</span>
  730. </div>
  731. <div class="item">
  732. <b>Reparable: </b>
  733. <span>'.(($arrFailureLog['REPARABLE'] === 1) ? ('Si') : ('No')).'</span>
  734. </div>
  735. <div class="item">
  736. <b>Valor Obtenido: </b>
  737. <span>'.$arrFailureLog['VALOR_OBTENIDO'].' '.$arrFailureLog['ACRONIMO_MEDIDA'].'</span>
  738. </div>
  739. <div class="item">
  740. <b>Tipo de Medido: </b>
  741. <span>'.$arrFailureLog['NOMBRE_MEDIDA'].'</span>
  742. </div>
  743. <div class="item">
  744. <b>Solución de la Falla: </b>
  745. <span>'.(($arrFailureLog['SOLUCION'] === null || $arrFailureLog['SOLUCION'] === '') ? ('') : ($arrFailureLog['SOLUCION'])).'</span>
  746. </div>
  747. <div class="item">
  748. <b>Comentarios: </b>
  749. <span>'.$arrFailureLog['COMENTARIOS'].'</span>
  750. </div>';
  751. $html .= '</body></html>';
  752. $nuli = $this->resourcesController->formatSecuence($line, 2);
  753. $como = 'ANFA'; // Código del módulo
  754. $cldo = 'IN'; // Código de la clasificación
  755. $fecr = date('ymd'); // Fecha en la se carga el archivo
  756. try {
  757. $arrSecuence = (array) DB::table('S002V01TAFAL')
  758. ->where('AFAL_COMO', '=', $como)
  759. ->where('AFAL_CLDO', '=', $cldo)
  760. ->where('AFAL_NULI', '=', $line)
  761. ->orderBy('AFAL_NUSE', 'desc')
  762. ->first([ 'AFAL_NUSE' ]);
  763. } catch (\Throwable $th) {
  764. return $this->responseController->makeResponse(
  765. true,
  766. "ERR_FAILURELOG_GENERATEPDF003: Ocurrió un error al obtener la información de la secuencia.",
  767. $th->getMessage(),
  768. 500
  769. );
  770. }
  771. $nuse = 1; // Secuencia del documento
  772. if ( !empty( $arrSecuence ) && !is_null( $arrSecuence ) ) {
  773. $nuse = intval( $arrSecuence['AFAL_NUSE'] ) + 1;
  774. }
  775. $nuse = $this->resourcesController->formatSecuence($nuse, 6);
  776. $nuve = $this->resourcesController->formatSecuence('1', 2);
  777. $noar = 'ficha_de_proveedor_' . $arrFailureLog['NUMERO_FALLA'];
  778. $nuli = $this->resourcesController->formatSecuence($line, 2);
  779. $como = 'GEAD'; // Código del módulo
  780. $cldo = 'IN'; // Código de la clasificación
  781. $fecr = date('ymd'); // Fecha en la se carga el archivo
  782. try {
  783. $arrSecuence = (array) DB::table('S002V01TAFAL')
  784. ->where('AFAL_COMO', '=', $como)
  785. ->where('AFAL_CLDO', '=', $cldo)
  786. ->where('AFAL_NULI', '=', $line)
  787. ->orderBy('AFAL_NUSE', 'desc')
  788. ->first([ 'AFAL_NUSE' ]);
  789. } catch (\Throwable $th) {
  790. return $this->responseController->makeResponse(
  791. true,
  792. "ERR_FAILURELOG_GENERATEPDF004: Ocurrió un error al obtener la información de la secuencia.",
  793. $th->getMessage(),
  794. 500
  795. );
  796. }
  797. $nuse = 1; // Secuencia del documento
  798. if ( !empty( $arrSecuence ) && !is_null( $arrSecuence ) ) {
  799. $nuse = intval( $arrSecuence['AFAL_NUSE'] ) + 1;
  800. }
  801. $nuse = $this->resourcesController->formatSecuence($nuse, 6);
  802. $nuve = $this->resourcesController->formatSecuence('1', 2);
  803. $noar = 'ficha_de_falla_' . $arrFailureLog['NUMERO_FALLA'];
  804. $exte = 'pdf';
  805. if ($_SERVER['SERVER_NAME'] === '192.168.100.105') {
  806. $filePath = 'C:/ITTEC/SAM/Dev/SistemaMantenimiento/sistema-mantenimiento-back/public/public_files/'; // API JEAN
  807. } else {
  808. $filePath = 'C:\inetpub\wwwroot\sam\public_files\\'; // API QA
  809. }
  810. $fileName = $nuli.'-'.$como.'-'.$cldo.'-'.$fecr.'-'.$nuse.'='.$nuve.'='.$noar.'.'.$exte;
  811. $tempFile = $filePath . $fileName;
  812. $dompdf = new Dompdf();
  813. $dompdf ->loadHtml($html);
  814. $dompdf->setPaper('A4', 'portrait');
  815. $dompdf->render();
  816. $output = $dompdf->output();
  817. file_put_contents($tempFile, $output);
  818. $ubic = Storage::putFile('files', new File($tempFile));
  819. $ubic = str_replace("/", "\\", $ubic);
  820. if ($_SERVER['SERVER_NAME'] === '192.168.100.105') {
  821. $ubic = "C:\ITTEC\SAM\Dev\SistemaMantenimiento\sistema-mantenimiento-back\storage\app\\" . $ubic;
  822. } else {
  823. $ubic = "C:\inetpub\wwwroot\sam\storage\app\\" . $ubic;
  824. }
  825. $tama = filesize($ubic);
  826. $usac = json_encode([$user]);
  827. $now = $this->functionsController->now();
  828. $currentDate = $now->toDateTimeString();
  829. try {
  830. $validateInsert = DB::table('S002V01TAFAL')->insert([
  831. 'AFAL_NULI' => $line,
  832. 'AFAL_COMO' => $como,
  833. 'AFAL_CLDO' => $cldo,
  834. 'AFAL_FECR' => $fecr,
  835. 'AFAL_NUSE' => $nuse,
  836. 'AFAL_NUVE' => $nuve,
  837. 'AFAL_NOAR' => $noar,
  838. 'AFAL_EXTE' => $exte,
  839. 'AFAL_TAMA' => $tama,
  840. 'AFAL_UBIC' => $ubic,
  841. 'AFAL_USAC' => $usac,
  842. 'AFAL_USRE' => $user,
  843. 'AFAL_FERE' => $currentDate,
  844. ]);
  845. } catch (\Throwable $th) {
  846. return $this->responseController->makeResponse(
  847. true,
  848. "ERR_FAILURELOG_GENERATEPDF005: Ocurrió un error guardar los datos a la tabla final de archivos.",
  849. $th->getMessage(),
  850. 500
  851. );
  852. }
  853. if ( !$validateInsert ) {
  854. return $this->responseController->makeResponse(
  855. true,
  856. "ERR_FAILURELOG_GENERATEPDF006: No se pudo guardar la ficha del proveedor en la base de datos.",
  857. [],
  858. 500
  859. );
  860. }
  861. if ($_SERVER['SERVER_NAME'] === '192.168.100.105') {
  862. $urlPublic = 'http://192.168.100.105:8000/public_files/' . $fileName;
  863. } else {
  864. $urlPublic = $this->functionsController->getApiURI().'sam/public_files/' . $fileName;
  865. }
  866. return $this->responseController->makeResponse(false, "EXITO: Modificación Exitosa", [ 'url' => $urlPublic ]);
  867. }
  868. public function registerFailureLog(Request $request) {
  869. $validator = Validator::make($request->all(), [
  870. 'CODIGO_EQUIPAMIENTO' => 'required|string',
  871. 'ID_FALLA' => 'required|integer',
  872. // 'ID_SINTOMA' => 'required|integer',
  873. 'FECHA_FALLA' => 'required|string',
  874. 'REPARABLE' => 'required|string',
  875. 'SOLUCION' => 'required|string',
  876. 'COMENTARIOS' => 'required|string',
  877. 'VALOR_OBTENIDO' => 'required|string',
  878. 'ID_MEDIDA' => 'required|string',
  879. 'USUARIO' => 'required|string',
  880. 'NUMERO_LINEA' => 'required|string',
  881. ]);
  882. if ($validator->fails()) {
  883. return $this->responseController->makeResponse(
  884. true,
  885. "ERR_FAILURELOG_REG000: Se encontraron uno o más errores.",
  886. $this->responseController->makeErrors($validator->errors()->messages()),
  887. 401
  888. );
  889. }
  890. DB::beginTransaction();
  891. $requestData = $request->all();
  892. try {
  893. $user = $this->encController->decrypt($requestData['USUARIO']);
  894. } catch (\Throwable $th) {
  895. DB::rollBack();
  896. return $this->responseController->makeResponse(true, "ERR_FAILURELOG_REG001: No se pudo obtener el usuario.", $th->getMessage(), 500);
  897. }
  898. try {
  899. $validateExistsFailureList = DB::table('S002V01TLIFA')
  900. ->where('LIFA_NULI', '=', $requestData['NUMERO_LINEA'])
  901. ->where('LIFA_IDFA', '=', $requestData['ID_FALLA'])
  902. ->where('LIFA_ESTA', '=', 'Activo')
  903. ->exists();
  904. } catch (\Throwable $th) {
  905. DB::rollBack();
  906. return $this->responseController->makeResponse(true, "ERR_FAILURELOG_REG002: Ocurrió un error al consultar la lista de fallas.", $th->getMessage(), 500);
  907. }
  908. if ( !$validateExistsFailureList ) {
  909. DB::rollBack();
  910. return $this->responseController->makeResponse(true, "ERR_FAILURELOG_REG003: No existe la falla #".$requestData['ID_FALLA']." en la lista de fallas.", [], 500);
  911. }
  912. if ( !is_null($requestData['ID_SINTOMA']) && $requestData['ID_SINTOMA'] !== '' ) {
  913. try {
  914. $validateExistsSymptomList = DB::table('S002V01TLISI')
  915. ->where('LISI_NULI', '=', $requestData['NUMERO_LINEA'])
  916. ->where('LISI_IDSI', '=', $requestData['ID_SINTOMA'])
  917. ->where('LISI_ESTA', '=', 'Activo')
  918. ->exists();
  919. } catch (\Throwable $th) {
  920. DB::rollBack();
  921. return $this->responseController->makeResponse(true, "ERR_FAILURELOG_REG004: Ocurrió un error al consultar la lista de síntomas.", $th->getMessage(), 500);
  922. }
  923. if ( !$validateExistsSymptomList ) {
  924. DB::rollBack();
  925. return $this->responseController->makeResponse(true, "ERR_FAILURELOG_REG005: No existe el síntoma #".$requestData['ID_SINTOMA']." en la lista de síntomas.", [], 500);
  926. }
  927. }
  928. try {
  929. $validateExistsMeasure = DB::table('S002V01TLIME')
  930. ->where('LIME_NULI', '=', $requestData['NUMERO_LINEA'])
  931. ->where('LIME_IDME', '=', $requestData['ID_MEDIDA'])
  932. ->where('LIME_ESTA', '=', 'Activo')
  933. ->exists();
  934. } catch (\Throwable $th) {
  935. DB::rollBack();
  936. return $this->responseController->makeResponse(true, "ERR_FAILURELOG_REG006: Ocurrió un error al consultar la lista de medidas.", $th->getMessage(), 500);
  937. }
  938. if ( !$validateExistsMeasure ) {
  939. DB::rollBack();
  940. return $this->responseController->makeResponse(true, "ERR_FAILURELOG_REG007: No existe la medida #".$requestData['ID_MEDIDA']." en la lista de medidas.", [], 500);
  941. }
  942. try {
  943. $validateExistsEquipment = DB::table('S002V01TEQUI')
  944. ->where('EQUI_NULI', '=', $requestData['NUMERO_LINEA'])
  945. ->where('EQUI_COEQ', '=', $requestData['CODIGO_EQUIPAMIENTO'])
  946. ->exists();
  947. } catch (\Throwable $th) {
  948. DB::rollBack();
  949. return $this->responseController->makeResponse(true, "ERR_FAILURELOG_REG008: Ocurrió un error al consultar los equipamientos.", $th->getMessage(), 500);
  950. }
  951. if ( !$validateExistsEquipment ) {
  952. DB::rollBack();
  953. return $this->responseController->makeResponse(true, "ERR_FAILURELOG_REG009: No existe el equipamiento ".$requestData['CODIGO_EQUIPAMIENTO']." en la lista de equipamientos.", [], 500);
  954. }
  955. $now = $this->functionsController->now();
  956. $currentDate = $now->toDateTimeString();
  957. try {
  958. $validateInsert = DB::table('S002V01TBIFA')->insert([
  959. 'BIFA_NULI' => $requestData['NUMERO_LINEA'],
  960. 'BIFA_COEQ' => $requestData['CODIGO_EQUIPAMIENTO'],
  961. 'BIFA_IDFA' => $requestData['ID_FALLA'],
  962. 'BIFA_IDSI' => $requestData['ID_SINTOMA'],
  963. 'BIFA_FEFA' => $requestData['FECHA_FALLA'],
  964. 'BIFA_REPA' => $requestData['REPARABLE'],
  965. 'BIFA_DESO' => $requestData['SOLUCION'],
  966. 'BIFA_COME' => $requestData['COMENTARIOS'],
  967. 'BIFA_VAOB' => $requestData['VALOR_OBTENIDO'],
  968. 'BIFA_IDME' => $requestData['ID_MEDIDA'],
  969. 'BIFA_USRE' => $user,
  970. 'BIFA_FERE' => $currentDate,
  971. 'BIFA_FEAR' => DB::raw('CURRENT_TIMESTAMP')
  972. ]);
  973. } catch (\Throwable $th) {
  974. DB::rollBack();
  975. return $this->responseController->makeResponse(true, "ERR_FAILURELOG_REG010: Ocurrió un error al intentar registrar la falla en la base de datos.", $th->getMessage(), 500);
  976. }
  977. if ( !$validateInsert ) {
  978. DB::rollBack();
  979. return $this->responseController->makeResponse(true, "ERR_FAILURELOG_REG011: No se pudo registrar la falla.", [], 500);
  980. }
  981. DB::commit();
  982. return $this->responseController->makeResponse(false, "ÉXITO: Registro Exitoso");
  983. }
  984. public function updateFailureLog(Request $request) {
  985. $validator = Validator::make($request->all(), [
  986. 'NUMERO_FALLA' => 'required|integer',
  987. 'CODIGO_EQUIPAMIENTO' => 'required|string',
  988. 'ID_FALLA' => 'required|integer',
  989. // 'ID_SINTOMA' => 'required|integer',
  990. 'FECHA_FALLA' => 'required|string',
  991. 'REPARABLE' => 'required|string',
  992. 'SOLUCION' => 'required|string',
  993. 'COMENTARIOS' => 'required|string',
  994. 'VALOR_OBTENIDO' => 'required|string',
  995. 'ID_MEDIDA' => 'required|string',
  996. 'USUARIO' => 'required|string',
  997. 'NUMERO_LINEA' => 'required|string',
  998. ]);
  999. if ($validator->fails()) {
  1000. return $this->responseController->makeResponse(
  1001. true,
  1002. "ERR_FAILURELOG_UPD000: Se encontraron uno o más errores.",
  1003. $this->responseController->makeErrors($validator->errors()->messages()),
  1004. 401
  1005. );
  1006. }
  1007. DB::beginTransaction();
  1008. $requestData = $request->all();
  1009. try {
  1010. $user = $this->encController->decrypt($requestData['USUARIO']);
  1011. } catch (\Throwable $th) {
  1012. DB::rollBack();
  1013. return $this->responseController->makeResponse(true, "ERR_FAILURELOG_UPD001: No se pudo obtener el usuario.", $th->getMessage(), 500);
  1014. }
  1015. try {
  1016. $validateExistsFailureLog = DB::table('S002V01TBIFA')
  1017. ->where('BIFA_NULI', '=', $requestData['NUMERO_LINEA'])
  1018. ->where('BIFA_NUFA', '=', $requestData['NUMERO_FALLA'])
  1019. ->where('BIFA_ESTA', '=', 'Activo')
  1020. ->exists();
  1021. } catch (\Throwable $th) {
  1022. DB::rollBack();
  1023. return $this->responseController->makeResponse(true, "ERR_FAILURELOG_UPD002: Ocurrió un error al consultar la bitácora de fallas.", $th->getMessage(), 500);
  1024. }
  1025. if ( !$validateExistsFailureLog ) {
  1026. DB::rollBack();
  1027. return $this->responseController->makeResponse(true, "ERR_FAILURELOG_UPD003: No existe la falla #".$requestData['NUMERO_FALLA']." en la bitácora de fallas.", [], 500);
  1028. }
  1029. try {
  1030. $validateExistsFailureList = DB::table('S002V01TLIFA')
  1031. ->where('LIFA_NULI', '=', $requestData['NUMERO_LINEA'])
  1032. ->where('LIFA_IDFA', '=', $requestData['ID_FALLA'])
  1033. ->where('LIFA_ESTA', '=', 'Activo')
  1034. ->exists();
  1035. } catch (\Throwable $th) {
  1036. DB::rollBack();
  1037. return $this->responseController->makeResponse(true, "ERR_FAILURELOG_UPD004: Ocurrió un error al consultar la lista de fallas.", $th->getMessage(), 500);
  1038. }
  1039. if ( !$validateExistsFailureList ) {
  1040. DB::rollBack();
  1041. return $this->responseController->makeResponse(true, "ERR_FAILURELOG_UPD005: No existe la falla #".$requestData['ID_FALLA']." en la lista de fallas.", [], 500);
  1042. }
  1043. if ( !is_null($requestData['ID_SINTOMA']) && $requestData['ID_SINTOMA'] !== '' ) {
  1044. try {
  1045. $validateExistsSymptomList = DB::table('S002V01TLISI')
  1046. ->where('LISI_NULI', '=', $requestData['NUMERO_LINEA'])
  1047. ->where('LISI_IDSI', '=', $requestData['ID_SINTOMA'])
  1048. ->where('LISI_ESTA', '=', 'Activo')
  1049. ->exists();
  1050. } catch (\Throwable $th) {
  1051. DB::rollBack();
  1052. return $this->responseController->makeResponse(true, "ERR_FAILURELOG_UPD006: Ocurrió un error al consultar la lista de síntomas.", $th->getMessage(), 500);
  1053. }
  1054. if ( !$validateExistsSymptomList ) {
  1055. DB::rollBack();
  1056. return $this->responseController->makeResponse(true, "ERR_FAILURELOG_UPD007: No existe el síntoma #".$requestData['ID_SINTOMA']." en la lista de síntomas.", [], 500);
  1057. }
  1058. }
  1059. try {
  1060. $validateExistsMeasure = DB::table('S002V01TLIME')
  1061. ->where('LIME_NULI', '=', $requestData['NUMERO_LINEA'])
  1062. ->where('LIME_IDME', '=', $requestData['ID_MEDIDA'])
  1063. ->where('LIME_ESTA', '=', 'Activo')
  1064. ->exists();
  1065. } catch (\Throwable $th) {
  1066. DB::rollBack();
  1067. return $this->responseController->makeResponse(true, "ERR_FAILURELOG_UPD008: Ocurrió un error al consultar la lista de medidas.", $th->getMessage(), 500);
  1068. }
  1069. if ( !$validateExistsMeasure ) {
  1070. DB::rollBack();
  1071. return $this->responseController->makeResponse(true, "ERR_FAILURELOG_UPD009: No existe la medida #".$requestData['ID_MEDIDA']." en la lista de medidas.", [], 500);
  1072. }
  1073. try {
  1074. $validateExistsEquipment = DB::table('S002V01TEQUI')
  1075. ->where('EQUI_NULI', '=', $requestData['NUMERO_LINEA'])
  1076. ->where('EQUI_COEQ', '=', $requestData['CODIGO_EQUIPAMIENTO'])
  1077. ->exists();
  1078. } catch (\Throwable $th) {
  1079. DB::rollBack();
  1080. return $this->responseController->makeResponse(true, "ERR_FAILURELOG_UPD010: Ocurrió un error al consultar los equipamientos.", $th->getMessage(), 500);
  1081. }
  1082. if ( !$validateExistsEquipment ) {
  1083. DB::rollBack();
  1084. return $this->responseController->makeResponse(true, "ERR_FAILURELOG_UPD011: No existe el equipamiento ".$requestData['CODIGO_EQUIPAMIENTO']." en la lista de equipamientos.", [], 500);
  1085. }
  1086. $now = $this->functionsController->now();
  1087. $currentDate = $now->toDateTimeString();
  1088. try {
  1089. $validateUpdate = DB::table('S002V01TBIFA')
  1090. ->where('BIFA_NUFA', '=', $requestData['NUMERO_FALLA'])
  1091. ->where('BIFA_NULI', '=', $requestData['NUMERO_LINEA'])
  1092. ->update([
  1093. 'BIFA_COEQ' => $requestData['CODIGO_EQUIPAMIENTO'],
  1094. 'BIFA_IDFA' => $requestData['ID_FALLA'],
  1095. 'BIFA_IDSI' => $requestData['ID_SINTOMA'],
  1096. 'BIFA_FEFA' => $requestData['FECHA_FALLA'],
  1097. 'BIFA_REPA' => $requestData['REPARABLE'],
  1098. 'BIFA_DESO' => $requestData['SOLUCION'],
  1099. 'BIFA_COME' => $requestData['COMENTARIOS'],
  1100. 'BIFA_VAOB' => $requestData['VALOR_OBTENIDO'],
  1101. 'BIFA_IDME' => $requestData['ID_MEDIDA'],
  1102. 'BIFA_USMO' => $user,
  1103. 'BIFA_FEMO' => $currentDate,
  1104. 'BIFA_FEAR' => DB::raw('CURRENT_TIMESTAMP')
  1105. ]);
  1106. } catch (\Throwable $th) {
  1107. DB::rollBack();
  1108. return $this->responseController->makeResponse(true, "ERR_FAILURELOG_UPD012: Ocurrió un error al intentar modificar la falla en la base de datos.", $th->getMessage(), 500);
  1109. }
  1110. if ( !$validateUpdate ) {
  1111. DB::rollBack();
  1112. return $this->responseController->makeResponse(true, "ERR_FAILURELOG_UPD013: No se pudo modificar la bitácora de falla.", [], 500);
  1113. }
  1114. DB::commit();
  1115. return $this->responseController->makeResponse(false, "ÉXITO: Modificación Exitosa");
  1116. }
  1117. }