CountersActivatorsController.php 78 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846
  1. <?php
  2. namespace App\Http\Controllers;
  3. use Illuminate\Support\Facades\DB;
  4. use Illuminate\Http\Request;
  5. use Illuminate\Support\Facades\Validator;
  6. use Illuminate\Support\Carbon;
  7. class CountersActivatorsController extends Controller{
  8. private $responseController;
  9. private $encryptionController;
  10. private $functionsController;
  11. public function __construct(){
  12. $this->responseController = new ResponseController();
  13. $this->encryptionController = new EncryptionController();
  14. $this->functionsController = new FunctionsController();
  15. }
  16. public function getActivators($idUser, $line) {
  17. DB::enableQueryLog();
  18. $idUser = $this->encryptionController->decrypt($idUser);
  19. if(!$idUser){
  20. return $this->responseController->makeResponse(true, 'El ID del usuario que realizó la petición no está encriptado correctamente.', [], 400);
  21. }
  22. $usr = DB::table('S002V01TUSUA')->where([
  23. ['USUA_NULI', '=', $line],
  24. ['USUA_IDUS', '=', $idUser],
  25. ])->first();
  26. if(is_null($usr)){
  27. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado.', [], 404);
  28. }
  29. $activators = DB::table('S002V01TACTI')->select([
  30. 'ACTI_IDAC AS ID_ACTIVADOR',
  31. 'ACTI_PRIO AS PRIORIDAD',
  32. 'ACTI_TIAC AS TIPO_ACTIVADOR',
  33. 'ACTI_CORE AS ID_CONTADOR',
  34. 'ACTI_ESTA AS ESTADO'
  35. ])->get()->all();
  36. foreach($activators as $key=>$activator){
  37. $activator->ID_ACTIVADOR = $this->encryptionController->encrypt($activator->ID_ACTIVADOR);
  38. $activator->ID_CONTADOR = $this->encryptionController->encrypt($activator->ID_CONTADOR);
  39. $activators[$key] = $activator;
  40. }
  41. $now = $this->functionsController->now();
  42. $nowStr = $now->toDateTimeString();
  43. $actions = DB::getQueryLog();
  44. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  45. $idac = $this->functionsController->registerActivity(
  46. $line,
  47. 'S002V01M06COAC',
  48. 'S002V01F01GEAC',
  49. 'S002V01P01COAC',
  50. 'Consulta',
  51. "El usuario $name (" . $usr->USUA_IDUS . ") consultó los activadores registrados.",
  52. $idUser,
  53. $nowStr,
  54. 'S002V01S02ACTI'
  55. );
  56. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $line);
  57. return $this->responseController->makeresponse(false, "EXITO", $activators);
  58. }
  59. public function getActivatorsByType($type, $idUser, $line){
  60. DB::enableQueryLog();
  61. $idUser = $this->encryptionController->decrypt($idUser);
  62. if(!$idUser){
  63. return $this->responseController->makeResponse(true, 'El ID del usuario que realizó la petición no está encriptado correctamente.', [], 400);
  64. }
  65. $usr = DB::table('S002V01TUSUA')->where([
  66. ['USUA_NULI', '=', $line],
  67. ['USUA_IDUS', '=', $idUser],
  68. ])->first();
  69. if(is_null($usr)){
  70. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado.', [], 404);
  71. }
  72. $activators = DB::table('S002V01TACTI')->select([
  73. 'ACTI_IDAC AS IDACTIVADOR',
  74. 'ACTI_PRIO AS PRIORIDAD',
  75. 'ACTI_TIAC AS TIPOACTIVACION',
  76. 'ACTI_COAC AS CONDICIONES',
  77. 'CONA_COEQ AS CODIGO_EQUIPAMIENTO',
  78. ])->where([
  79. ['ACTI_NULI', '=', $line],
  80. ['ACTI_TIAC', '=', $type]
  81. ])->join('S002V01TCONA', 'CONA_IDCO', '=', 'ACTI_CORE')->get()->all();
  82. foreach($activators as $key=>$activator){
  83. $activator->IDACTIVADOR = $this->encryptionController->encrypt($activator->IDACTIVADOR);
  84. $activator->PRIORIDAD = $this->encryptionController->encrypt($activator->PRIORIDAD);
  85. $activator->CODIGO_EQUIPAMIENTO = $this->encryptionController->encrypt($activator->CODIGO_EQUIPAMIENTO);
  86. if($activator->TIPOACTIVACION == 'Medida' || $activator->TIPOACTIVACION == 'Valor'){
  87. $activationConfigArr = json_decode($activator->CONDICIONES, true);
  88. $magnitude = DB::table('S002V01TMAGN')->where([
  89. ['MAGN_NULI', '=', $line],
  90. ['MAGN_IDMA', '=', $activationConfigArr['magnitude']]
  91. ])->first();
  92. $unit = DB::table('S002V01TLIME')->where([
  93. ['LIME_NULI', '=', $line],
  94. ['LIME_IDME', '=', $activationConfigArr['unit']],
  95. ['LIME_MAGN', '=', $activationConfigArr['magnitude']],
  96. ])->first();
  97. $activationConfigArr['magnitude'] = $magnitude->MAGN_MAGN . " (" . $activationConfigArr['magnitude'] . ")";
  98. $activationConfigArr['unit'] = $unit->LIME_ACME . " - " . $unit->LIME_NOME . " (" . $activationConfigArr['unit'] . ")";
  99. $activator->CONDICIONES = json_encode($activationConfigArr);
  100. }
  101. $activators[$key] = $activator;
  102. }
  103. $now = $this->functionsController->now();
  104. $nowStr = $now->toDateTimeString();
  105. $actions = DB::getQueryLog();
  106. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  107. $idac = $this->functionsController->registerActivity(
  108. $line,
  109. 'S002V01M06COAC',
  110. 'S002V01F01GEAC',
  111. 'S002V01P01COAC',
  112. 'Consulta',
  113. "El usuario $name (" . $usr->USUA_IDUS . ") consultó los contadores de tipo $type.",
  114. $idUser,
  115. $nowStr,
  116. 'S002V01S02ACTI'
  117. );
  118. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $line);
  119. return $this->responseController->makeresponse(false, "EXITO", $activators);
  120. }
  121. public function registerActivator(Request $request){
  122. DB::enableQueryLog();
  123. $validator = Validator::make($request->all(), [
  124. 'id_user' => 'required|string',
  125. 'linea' => 'required|integer',
  126. 'priority' => 'required|string|in:1,2,3,4',
  127. 'type' => 'required|string|in:CA,SI,ME,VA',
  128. 'condition' => 'required|json',
  129. 'counter' => 'required|string'
  130. ]);
  131. if($validator->fails()){
  132. return $this->responseController->makeResponse(
  133. true,
  134. "Se encontraron uno o más errores.",
  135. $this->responseController->makeErrors(
  136. $validator->errors()->messages()
  137. ),
  138. 401
  139. );
  140. }
  141. //$priorities = ['MA' => '1', 'AL' => '2', 'ME' => '3', 'BA' => '4'];
  142. $types = ['CA' => 'Calendario', 'SI' => 'Sintoma', 'ME' => 'Medida', 'VA' => 'Valor'];
  143. $form = $request->all();
  144. $idUser = $this->encryptionController->decrypt($form['id_user']);
  145. if(!$idUser){
  146. return $this->responseController->makeResponse(true, 'El ID del usuario que realizó la petición no está encriptado correctamente.', [], 400);
  147. }
  148. $usr = DB::table('S002V01TUSUA')->where([
  149. ['USUA_NULI', '=', $form['linea']],
  150. ['USUA_IDUS', '=', $idUser],
  151. ])->first();
  152. if(is_null($usr)){
  153. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado.', [], 404);
  154. }
  155. $idCounter = $this->encryptionController->decrypt($form['counter']);
  156. if(!$idCounter){
  157. return $this->responseController->makeResponse(true, 'El ID del contador relacionado no está encriptado correctamente.', [], 400);
  158. }
  159. $counter = DB::table('S002V01TCONA')->where([
  160. ['CONA_NULI', '=', $form['linea']],
  161. ['CONA_IDCO', '=', $idCounter]
  162. ])->first();
  163. if(is_null($counter)){
  164. return $this->responseController->makeResponse(true, 'El contador relacionado no está registrado.', [], 404);
  165. }
  166. $priority = $form['priority'];
  167. $type = $types[$form['type']];
  168. $conditionArr = json_decode($form['condition'], true);
  169. if(count($conditionArr) == 0){
  170. return $this->responseController->makeResponse(true, 'El arreglo de la condición de activación está vació.', [], 400);
  171. }else{
  172. switch($form['type']){
  173. case 'CA':
  174. if(count($conditionArr) != 5){
  175. return $this->responseController->makeResponse(true, 'El arreglo de la condición tiene un formato inválido.', [], 400);
  176. }
  177. $keysCA = ['startDate', 'startHour', 'repeat', 'customRepeat', 'color'];
  178. foreach($keysCA as $key){
  179. if(!array_key_exists($key, $conditionArr)){
  180. return $this->responseController->makeResponse(true, "El arreglo de la condición no contiene el elemento '$key'.", [], 400);
  181. }
  182. }
  183. break;
  184. case 'SI':
  185. if(count($conditionArr) != 6){
  186. return $this->responseController->makeResponse(true, 'El arreglo de la condición tiene un formato inválido.', [], 400);
  187. }
  188. $keysSI = ['sign', 'startDate', 'startHour', 'repeat', 'customRepeat', 'color'];
  189. foreach($keysSI as $key){
  190. if(!array_key_exists($key, $conditionArr)){
  191. return $this->responseController->makeResponse(true, "El arreglo de la condición no contiene el elemento '$key'.", [], 400);
  192. }
  193. }
  194. $idSymptom = $this->encryptionController->decrypt($conditionArr['sign']);
  195. if(!$idSymptom){
  196. return $this->responseController->makeResponse(true, 'El ID del síntoma relacionado no está encriptado correctamente.', [], 400);
  197. }
  198. $symptom = DB::table('S002V01TLISI')->where([
  199. ['LISI_IDSI', '=', $idSymptom],
  200. ['LISI_NULI', '=', $form['linea']]
  201. ])->first();
  202. if(is_null($symptom)){
  203. return $this->responseController->makeResponse(true, 'El síntoma relacionado no está registrado.', [], 404);
  204. }else if($symptom->LISI_ESTA == 'Eliminado'){
  205. return $this->responseController->makeResponse(true, 'El síntoma relacionado está eliminado.', [], 404);
  206. }
  207. $conditionArr['sign'] = $idSymptom;
  208. break;
  209. case 'ME':
  210. if(count($conditionArr) != 7){
  211. return $this->responseController->makeResponse(true, 'El arreglo de la condición tiene un formato inválido.', [], 400);
  212. }
  213. $keysSI = ['minValue', 'maxValue', 'magnitude', 'customMagnitude', 'unit', 'customUnit', 'color'];
  214. foreach($keysSI as $key){
  215. if(!array_key_exists($key, $conditionArr)){
  216. return $this->responseController->makeResponse(true, "El arreglo de la condición no contiene el elemento '$key'.", [], 400);
  217. }
  218. }
  219. $idMagnitude = $this->encryptionController->decrypt($conditionArr['magnitude']);
  220. if(!$idMagnitude){
  221. return $this->responseController->makeResponse(true, 'El ID de la magnitud relacionada no está encriptado correctamente.', [], 400);
  222. }
  223. $magnitude = DB::table('S002V01TMAGN')->where([
  224. ['MAGN_NULI', '=', $form['linea']],
  225. ['MAGN_IDMA', '=', $idMagnitude]
  226. ])->first();
  227. if(is_null($magnitude)){
  228. return $this->responseController->makeResponse(true, 'La magnitud relacionada no está registrado.', [], 404);
  229. }else if($magnitude->MAGN_ESTA == 'Eliminado'){
  230. return $this->responseController->makeResponse(true, 'La magnitud relacionada está eliminada.', [], 404);
  231. }
  232. $idUnit = $this->encryptionController->decrypt($conditionArr['unit']);
  233. if(!$idUnit){
  234. return $this->responseController->makeResponse(true, 'El ID de la unidad relacionada no está encriptado correctamente.', [], 400);
  235. }
  236. $unit = DB::table('S002V01TLIME')->where([
  237. ['LIME_IDME', '=', $idUnit],
  238. ['LIME_NULI', '=', $form['linea']],
  239. ['LIME_MAGN', '=', $idMagnitude],
  240. ])->first();
  241. if(is_null($unit)){
  242. return $this->responseController->makeResponse(true, 'La unidad relacionada no está registrado.', [], 404);
  243. }else if($unit->LIME_ESTA == 'Eliminado'){
  244. return $this->responseController->makeResponse(true, 'La unidad relacionada está eliminada.', [], 404);
  245. }
  246. $conditionArr['magnitude'] = $idMagnitude ;
  247. $conditionArr['unit'] = $idUnit;
  248. break;
  249. case 'VA':
  250. if(count($conditionArr) != 7){
  251. return $this->responseController->makeResponse(true, 'El arreglo de la condición tiene un formato inválido.', [], 400);
  252. }
  253. $keysVA = ['comparison', 'limitValue', 'magnitude', 'customMagnitude', 'unit', 'customUnit', 'color'];
  254. foreach($keysVA as $key){
  255. if(!array_key_exists($key, $conditionArr)){
  256. return $this->responseController->makeResponse(true, "El arreglo de la condición no contiene el elemento '$key'.", [], 400);
  257. }
  258. }
  259. $idMagnitude = $this->encryptionController->decrypt($conditionArr['magnitude']);
  260. if(!$idMagnitude){
  261. return $this->responseController->makeResponse(true, 'El ID de la magnitud relacionada no está encriptado correctamente.', [], 400);
  262. }
  263. $magnitude = DB::table('S002V01TMAGN')->where([
  264. ['MAGN_NULI', '=', $form['linea']],
  265. ['MAGN_IDMA', '=', $idMagnitude]
  266. ])->first();
  267. if(is_null($magnitude)){
  268. return $this->responseController->makeResponse(true, 'La magnitud relacionada no está registrado.', [], 404);
  269. }else if($magnitude->MAGN_ESTA == 'Eliminado'){
  270. return $this->responseController->makeResponse(true, 'La magnitud relacionada está eliminada.', [], 404);
  271. }
  272. $idUnit = $this->encryptionController->decrypt($conditionArr['unit']);
  273. if(!$idUnit){
  274. return $this->responseController->makeResponse(true, 'El ID de la unidad relacionada no está encriptado correctamente.', [], 400);
  275. }
  276. $unit = DB::table('S002V01TLIME')->where([
  277. ['LIME_IDME', '=', $idUnit],
  278. ['LIME_NULI', '=', $form['linea']],
  279. ['LIME_MAGN', '=', $idMagnitude],
  280. ])->first();
  281. if(is_null($unit)){
  282. return $this->responseController->makeResponse(true, 'La unidad relacionada no está registrado.', [], 404);
  283. }else if($unit->LIME_ESTA == 'Eliminado'){
  284. return $this->responseController->makeResponse(true, 'La unidad relacionada está eliminada.', [], 404);
  285. }
  286. $conditionArr['magnitude'] = $idMagnitude ;
  287. $conditionArr['unit'] = $idUnit;
  288. break;
  289. default:
  290. return $this->responseController->makeResponse(true, 'El tipo de contador es inválido.', [], 400);
  291. break;
  292. }
  293. }
  294. $conditionStr = json_encode($conditionArr);
  295. $now = $this->functionsController->now();
  296. $nowStr = $now->toDateTimeString();
  297. $idCont = DB::table('S002V01TACTI')->insertGetId([
  298. 'ACTI_NULI' => $form['linea'],
  299. 'ACTI_PRIO' => $priority,
  300. 'ACTI_TIAC' => $type,
  301. 'ACTI_COAC' => $conditionStr,
  302. 'ACTI_CORE' => $idCounter,
  303. 'ACTI_USRE' => $idUser,
  304. 'ACTI_FERE' => $nowStr
  305. ]);
  306. $actions = DB::getQueryLog();
  307. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  308. $idac = $this->functionsController->registerActivity(
  309. $form['linea'],
  310. 'S002V01M06COAC',
  311. 'S002V01F01GEAC',
  312. 'S002V01P02REAC',
  313. 'Registro',
  314. "El usuario $name (" . $usr->USUA_IDUS . ") registró el contador #$idCont.",
  315. $idUser,
  316. $nowStr,
  317. 'S002V01S02ACTI'
  318. );
  319. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $form['linea']);
  320. return $this->responseController->makeResponse(false, 'EXITO.', ['IDCONT' => $idCont]);
  321. }
  322. public function getActivator($idActivator, $idUser, $line){
  323. DB::enableQueryLog();
  324. $idUser = $this->encryptionController->decrypt($idUser);
  325. if(!$idUser){
  326. return $this->responseController->makeResponse(true, 'El ID del usuario que realizó la petición no está encriptado correctamente.', [], 400);
  327. }
  328. $usr = DB::table('S002V01TUSUA')->where([
  329. ['USUA_NULI', '=', $line],
  330. ['USUA_IDUS', '=', $idUser],
  331. ])->first();
  332. if(is_null($usr)){
  333. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado.', [], 404);
  334. }
  335. $idActivator = $this->encryptionController->decrypt($idActivator);
  336. if(!$idActivator){
  337. return $this->responseController->makeResponse(true, 'El ID del activador solicitado no está encriptado correctamente.', [], 400);
  338. }
  339. $activator = DB::table('S002V01TACTI')->select([
  340. 'ACTI_IDAC AS IDACTIVADOR',
  341. 'ACTI_PRIO AS PRIORIDAD',
  342. 'ACTI_TIAC AS TIPOACTIVADOR',
  343. 'ACTI_COAC AS CONDICIONES',
  344. 'ACTI_CORE AS CONTADOR',
  345. 'ACTI_ESTA AS ESTADO',
  346. 'ACTI_USRE AS USRREG',
  347. 'ACTI_FERE AS FECREG',
  348. 'ACTI_USMO AS USRMOD',
  349. 'ACTI_FEMO AS FECMOD',
  350. ])->where([
  351. ['ACTI_IDAC', '=', $idActivator],
  352. ['ACTI_NULI', '=', $line],
  353. ])->first();
  354. if(is_null($activator)){
  355. return $this->responseController->makeResponse(true, 'El activador solicitado no está registrado.', [], 404);
  356. }
  357. $activator->IDACTIVADOR = $this->encryptionController->encrypt($activator->IDACTIVADOR);
  358. $activator->CONTADOR = $this->encryptionController->encrypt($activator->CONTADOR);
  359. $conditionsArr = json_decode($activator->CONDICIONES, true);
  360. foreach($conditionsArr as $key=>$val){
  361. $encryptedParams = ['sign', 'magnitude', 'unit'];
  362. if(in_array($key, $encryptedParams)){
  363. switch($key){
  364. case 'sign':
  365. $symptom = DB::table('S002V01TLISI')->where([
  366. ['LISI_NULI', '=', $line],
  367. ['LISI_IDSI', '=', $val]
  368. ])->first();
  369. $conditionsArr[$key] = $symptom->LISI_NOSI . " (" . $val . ")";
  370. break;
  371. case 'magnitude':
  372. $magnitude = DB::table('S002V01TMAGN')->where([
  373. ['MAGN_NULI', '=', $line],
  374. ['MAGN_IDMA', '=', $val]
  375. ])->first();
  376. $conditionsArr[$key] = $magnitude->MAGN_MAGN . " (" . $val . ")";
  377. break;
  378. case 'unit':
  379. $unit = DB::table('S002V01TLIME')->where([
  380. ['LIME_NULI', '=', $line],
  381. ['LIME_IDME', '=', $val]
  382. ])->first();
  383. $conditionsArr[$key] = $unit->LIME_NOME . " (" . $val . ") - " . $unit->LIME_ACME;
  384. break;
  385. }
  386. }
  387. }
  388. $activator->CONDICIONES = json_encode($conditionsArr);
  389. $usrReg = DB::table('S002V01TUSUA')->where([
  390. ['USUA_NULI', '=', $line],
  391. ['USUA_IDUS', '=', $activator->USRREG]
  392. ])->first();
  393. $nameReg = $this->functionsController->joinName($usrReg->USUA_NOMB, $usrReg->USUA_APPA, $usrReg->USUA_APMA);
  394. $activator->USRREG = $nameReg . " (" . $activator->USRREG . ")";
  395. if(!is_null($activator->USRMOD)){
  396. $usrMod = DB::table('S002V01TUSUA')->where([
  397. ['USUA_NULI', '=', $line],
  398. ['USUA_IDUS', '=', $activator->USRMOD]
  399. ])->first();
  400. $nameMod = $this->functionsController->joinName($usrMod->USUA_NOMB, $usrMod->USUA_APPA, $usrMod->USUA_APMA);
  401. $activator->USRMOD = $nameMod . " (" . $activator->USRMOD . ")";
  402. }
  403. $now = $this->functionsController->now();
  404. $nowStr = $now->toDateTimeString();
  405. $actions = DB::getQueryLog();
  406. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  407. $idac = $this->functionsController->registerActivity(
  408. $line,
  409. 'S002V01M06COAC',
  410. 'S002V01F01GEAC',
  411. 'S002V01P01COAC',
  412. 'Consulta',
  413. "El usuario $name (" . $usr->USUA_IDUS . ") consultó la información del activador #$idActivator",
  414. $idUser,
  415. $nowStr,
  416. 'S002V01S02ACTI'
  417. );
  418. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $line);
  419. return $this->responseController->makeresponse(false, "EXITO", $activator);
  420. }
  421. public function registerCounter(Request $request) {
  422. DB::enableQueryLog();
  423. $validator = Validator::make($request->all(), [
  424. 'id_user' => 'required|string',
  425. 'linea' => 'required|integer',
  426. 'scada' => 'required|string',
  427. 'equipment' => 'required|string',
  428. 'time_interval' => 'required|numeric',
  429. 'time_interval_unit' => 'required|string|in:seg,min,hor,dia',
  430. 'time_interval_tolerance' => 'required|numeric',
  431. 'time_interval_tolerance_unit' => 'required|string|in:seg,min,hor,dia'
  432. ]);
  433. if($validator->fails()){
  434. return $this->responseController->makeResponse(
  435. true,
  436. "Se encontraron uno o más errores.",
  437. $this->responseController->makeErrors(
  438. $validator->errors()->messages()
  439. ),
  440. 401
  441. );
  442. }
  443. $form = $request->all();
  444. $idUser = $this->encryptionController->decrypt($form['id_user']);
  445. if(!$idUser){
  446. return $this->responseController->makeResponse(true, 'El ID del usuario que realizó la petición no está encriptado correctamente.', [], 400);
  447. }
  448. $usr = DB::table('S002V01TUSUA')->where([
  449. ['USUA_NULI', '=', $form['linea']],
  450. ['USUA_IDUS', '=', $idUser],
  451. ])->first();
  452. if(is_null($usr)){
  453. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado.', [], 404);
  454. }
  455. $idSCADA = $this->encryptionController->decrypt($form['scada']);
  456. if(!$idSCADA){
  457. return $this->responseController->makeResponse(true, 'El ID del SCADA relacionado no está encriptado correctamente.', [], 400);
  458. }
  459. $scada = DB::table('S002V01TLISC')->where([
  460. ['LISC_IDSC', '=', $idSCADA],
  461. ['LISC_NULI', '=', $form['linea']],
  462. ])->first();
  463. if(is_null($scada)){
  464. return $this->responseController->makeResponse(true, 'El SCADA relacionado no está registrado.', [], 404);
  465. }
  466. $equipmentCode = $this->encryptionController->decrypt($form['equipment']);
  467. if(!$equipmentCode){
  468. return $this->responseController->makeResponse(true, 'El código del equipamiento relacionado no está encriptado correctamente.', [], 400);
  469. }
  470. $equipment = DB::table('S002V01TEQUI')->where([
  471. ['EQUI_NULI', '=', $form['linea']],
  472. ['EQUI_COEQ', '=', $equipmentCode],
  473. ])->first();
  474. if(is_null($equipment)){
  475. return $this->responseController->makeResponse(true, 'El equipamiento relacionado no está registrado.', [], 404);
  476. }
  477. $now = $this->functionsController->now();
  478. $nowStr = $now->toDateTimeString();
  479. $idCont = DB::table('S002V01TCONA')->insertGetId([
  480. 'CONA_NULI' => $form['linea'],
  481. 'CONA_INLE' => $form['time_interval'],
  482. 'CONA_UTIL' => $form['time_interval_unit'],
  483. 'CONA_TOIN' => $form['time_interval_tolerance'],
  484. 'CONA_UTTI' => $form['time_interval_tolerance_unit'],
  485. 'CONA_IDSC' => $idSCADA,
  486. 'CONA_COEQ' => $equipmentCode,
  487. 'CONA_COVI' => '[]',
  488. 'CONA_USRE' => $idUser,
  489. 'CONA_FERE' => $nowStr,
  490. ]);
  491. $actions = DB::getQueryLog();
  492. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  493. $idac = $this->functionsController->registerActivity(
  494. $form['linea'],
  495. 'S002V01M06COAC',
  496. 'S002V01F01GECO',
  497. 'S002V01P02RECO',
  498. 'Registro',
  499. "El usuario $name (" . $usr->USUA_IDUS . ") registró el contador #$idCont.",
  500. $idUser,
  501. $nowStr,
  502. 'S002V01S01CONT'
  503. );
  504. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $form['linea']);
  505. return $this->responseController->makeResponse(false, 'EXITO.');
  506. }
  507. public function getCounters($idUser, $line) {
  508. DB::enableQueryLog();
  509. $idUser = $this->encryptionController->decrypt($idUser);
  510. if(!$idUser){
  511. return $this->responseController->makeResponse(true, 'El ID del usuario que realizó la petición no está encriptado correctamente.', [], 400);
  512. }
  513. $usr = DB::table('S002V01TUSUA')->where([
  514. ['USUA_NULI', '=', $line],
  515. ['USUA_IDUS', '=', $idUser],
  516. ])->first();
  517. if(is_null($usr)){
  518. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado.', [], 404);
  519. }
  520. $counters = DB::table('S002V01TCONA')->select([
  521. 'CONA_IDCO AS ID_CONTADOR',
  522. 'CONA_IDSC AS ID_SCADA',
  523. 'LISC_NOSC AS SCADA',
  524. 'CONA_COEQ AS CODIGO_EQUIPAMIENTO',
  525. 'EQUI_TIPO AS TIPO_EQUIPAMIENTO',
  526. 'EQUI_MODE AS MODELO_EQUIPAMIENTO',
  527. 'EQUI_IDEQ AS ID_EQUIPAMIENTO',
  528. 'CONA_ESTA AS ESTADO',
  529. ])->join('S002V01TLISC', 'LISC_IDSC', '=', 'CONA_IDSC')
  530. ->join('S002V01TEQUI', 'EQUI_COEQ', '=', 'CONA_COEQ')
  531. ->get()->all();
  532. foreach($counters as $key=>$counter){
  533. $activators = DB::table('S002V01TACTI')->where([
  534. ['ACTI_NULI', '=', $line],
  535. ['ACTI_CORE', '=', $counter->ID_CONTADOR]
  536. ])->get()->all();
  537. $counter->ID_CONTADOR = $this->encryptionController->encrypt($counter->ID_CONTADOR);
  538. $counter->ID_SCADA = $this->encryptionController->encrypt($counter->ID_SCADA);
  539. $counter->CODIGO_EQUIPAMIENTO = $this->encryptionController->encrypt($counter->CODIGO_EQUIPAMIENTO);
  540. $counter->ID_EQUIPAMIENTO = $this->encryptionController->encrypt($counter->ID_EQUIPAMIENTO);
  541. $counter->TIENE_ACTIVADOR = count($activators) > 0;
  542. $counter->ACTIVADOR = count($activators) > 0 ? $this->encryptionController->encrypt($activators[0]->ACTI_IDAC) : null;
  543. $counters[$key] = $counter;
  544. }
  545. $now = $this->functionsController->now();
  546. $nowStr = $now->toDateTimeString();
  547. $actions = DB::getQueryLog();
  548. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  549. $idac = $this->functionsController->registerActivity(
  550. $line,
  551. 'S002V01M06COAC',
  552. 'S002V01F01GECO',
  553. 'S002V01P01COCO',
  554. 'Consulta',
  555. "El usuario $name (" . $usr->USUA_IDUS . ") consultó los contadores registrados.",
  556. $idUser,
  557. $nowStr,
  558. 'S002V01S01CONT'
  559. );
  560. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $line);
  561. return $this->responseController->makeresponse(false, "EXITO", $counters);
  562. }
  563. public function getCounter($idCounter, $idUser, $line) {
  564. DB::enableQueryLog();
  565. $idUser = $this->encryptionController->decrypt($idUser);
  566. if(!$idUser){
  567. return $this->responseController->makeResponse(true, 'El ID del usuario que realizó la petición no está encriptado correctamente.', [], 400);
  568. }
  569. $usr = DB::table('S002V01TUSUA')->where([
  570. ['USUA_NULI', '=', $line],
  571. ['USUA_IDUS', '=', $idUser],
  572. ])->first();
  573. if(is_null($usr)){
  574. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado.', [], 404);
  575. }
  576. $idCounter = $this->encryptionController->decrypt($idCounter);
  577. if(!$idCounter){
  578. return $this->responseController->makeResponse(true, 'El ID del contador no está encriptado correctamente.', [], 400);
  579. }
  580. $counter = DB::table('S002V01TCONA')->select([
  581. 'CONA_IDCO AS ID_CONTADOR',
  582. 'CONA_IDSC AS ID_SCADA',
  583. 'LISC_NOSC AS SCADA',
  584. 'CONA_COEQ AS CODIGO_EQUIPAMIENTO',
  585. 'EQUI_TIPO AS TIPO_EQUIPAMIENTO',
  586. 'EQUI_MODE AS MODELO_EQUIPAMIENTO',
  587. 'EQUI_IDEQ AS ID_EQUIPAMIENTO',
  588. 'CONA_INLE AS INTERVALO',
  589. 'CONA_UTIL AS UNIDAD_INTERVALO',
  590. 'CONA_TOIN AS TOLERANCIA_INTERVALO',
  591. 'CONA_UTTI AS UNIDAD_TOLERANCIA_INTERVALO',
  592. 'CONA_COVI AS CONTADORES_VINCULADOS',
  593. 'CONA_ESTA AS ESTADO',
  594. 'CONA_USRE AS USRREG',
  595. 'CONA_FERE AS FECREG',
  596. 'CONA_USMO AS USRMOD',
  597. 'CONA_FEMO AS FECMOD',
  598. ])->join('S002V01TLISC', 'LISC_IDSC', '=', 'CONA_IDSC')
  599. ->join('S002V01TEQUI', 'EQUI_COEQ', '=', 'CONA_COEQ')
  600. ->where([
  601. ['CONA_IDCO', '=', $idCounter],
  602. ['CONA_NULI', '=', $line]
  603. ])->first();
  604. if(is_null($counter)){
  605. return $this->responseController->makeResponse(true, 'El contador solicitado no está registrado.', [], 404);
  606. }
  607. $counter->ID_CONTADOR = $this->encryptionController->encrypt($counter->ID_CONTADOR);
  608. $counter->ID_SCADA = $this->encryptionController->encrypt($counter->ID_SCADA);
  609. $counter->CODIGO_EQUIPAMIENTO = $this->encryptionController->encrypt($counter->CODIGO_EQUIPAMIENTO);
  610. $counter->ID_EQUIPAMIENTO = $this->encryptionController->encrypt($counter->ID_EQUIPAMIENTO);
  611. $relatedCountersArr = json_decode($counter->CONTADORES_VINCULADOS, true);
  612. foreach($relatedCountersArr as $key=>$counterStr){
  613. $counterStr = $this->encryptionController->encrypt($counterStr);
  614. $relatedCountersArr[$key] = $counterStr;
  615. }
  616. $relatedCountersStr = json_encode($relatedCountersArr);
  617. $counter->CONTADORES_VINCULADOS = $relatedCountersStr;
  618. $usrReg = DB::table('S002V01TUSUA')->where([
  619. ['USUA_IDUS', '=', $counter->USRREG],
  620. ['USUA_NULI', '=', $line]
  621. ])->first();
  622. $nameReg = $this->functionsController->joinName($usrReg->USUA_NOMB, $usrReg->USUA_APPA, $usrReg->USUA_APMA);
  623. $counter->USRREG = $nameReg . " (" . $counter->USRREG . ")";
  624. if(!is_null($counter->USRMOD)){
  625. $usrMod = DB::table('S002V01TUSUA')->where([
  626. ['USUA_IDUS', '=', $counter->USRMOD],
  627. ['USUA_NULI', '=', $line]
  628. ])->first();
  629. $nameMod = $this->functionsController->joinName($usrMod->USUA_NOMB, $usrMod->USUA_APPA, $usrMod->USUA_APMA);
  630. $counter->USRMOD = $nameMod . " (" . $counter->USRMOD . ")";
  631. }
  632. $now = $this->functionsController->now();
  633. $nowStr = $now->toDateTimeString();
  634. $actions = DB::getQueryLog();
  635. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  636. $idac = $this->functionsController->registerActivity(
  637. $line,
  638. 'S002V01M06COAC',
  639. 'S002V01F01GECO',
  640. 'S002V01P01COCO',
  641. 'Consulta',
  642. "El usuario $name (" . $usr->USUA_IDUS . ") consultó el contador #$idCounter.",
  643. $idUser,
  644. $nowStr,
  645. 'S002V01S01CONT'
  646. );
  647. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $line);
  648. return $this->responseController->makeresponse(false, "EXITO", $counter);
  649. }
  650. public function updateCounter(Request $request) {
  651. DB::enableQueryLog();
  652. $validator = Validator::make($request->all(), [
  653. 'id_user' => 'required|string',
  654. 'linea' => 'required|integer',
  655. 'scada' => 'required|string',
  656. 'equipment' => 'required|string',
  657. 'id_counter' => 'required|string',
  658. 'time_interval' => 'required|numeric',
  659. 'time_interval_unit' => 'required|string|in:seg,min,hor,dia',
  660. 'time_interval_tolerance' => 'required|numeric',
  661. 'time_interval_tolerance_unit' => 'required|string|in:seg,min,hor,dia'
  662. ]);
  663. if($validator->fails()){
  664. return $this->responseController->makeResponse(
  665. true,
  666. "Se encontraron uno o más errores.",
  667. $this->responseController->makeErrors(
  668. $validator->errors()->messages()
  669. ),
  670. 401
  671. );
  672. }
  673. $form = $request->all();
  674. $idUser = $this->encryptionController->decrypt($form['id_user']);
  675. if(!$idUser){
  676. return $this->responseController->makeResponse(true, 'El ID del usuario que realizó la petición no está encriptado correctamente.', [], 400);
  677. }
  678. $usr = DB::table('S002V01TUSUA')->where([
  679. ['USUA_NULI', '=', $form['linea']],
  680. ['USUA_IDUS', '=', $idUser],
  681. ])->first();
  682. if(is_null($usr)){
  683. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado.', [], 404);
  684. }
  685. $idSCADA = $this->encryptionController->decrypt($form['scada']);
  686. if(!$idSCADA){
  687. return $this->responseController->makeResponse(true, 'El ID del SCADA relacionado no está encriptado correctamente.', [], 400);
  688. }
  689. $scada = DB::table('S002V01TLISC')->where([
  690. ['LISC_IDSC', '=', $idSCADA],
  691. ['LISC_NULI', '=', $form['linea']],
  692. ])->first();
  693. if(is_null($scada)){
  694. return $this->responseController->makeResponse(true, 'El SCADA relacionado no está registrado.', [], 404);
  695. }
  696. $equipmentCode = $this->encryptionController->decrypt($form['equipment']);
  697. if(!$equipmentCode){
  698. return $this->responseController->makeResponse(true, 'El código del equipamiento relacionado no está encriptado correctamente.', [], 400);
  699. }
  700. $equipment = DB::table('S002V01TEQUI')->where([
  701. ['EQUI_NULI', '=', $form['linea']],
  702. ['EQUI_COEQ', '=', $equipmentCode],
  703. ])->first();
  704. if(is_null($equipment)){
  705. return $this->responseController->makeResponse(true, 'El equipamiento relacionado no está registrado.', [], 404);
  706. }
  707. $idCounter = $this->encryptionController->decrypt($form['id_counter']);
  708. if(!$idCounter){
  709. return $this->responseController->makeResponse(true, 'El ID del contador seleccionado no está encriptado correctamente.', [], 400);
  710. }
  711. $counter = DB::table('S002V01TCONA')->where([
  712. ['CONA_NULI', '=', $form['linea']],
  713. ['CONA_IDCO', '=', $idCounter]
  714. ])->first();
  715. if(is_null($counter)){
  716. return $this->responseController->makeResponse(true, 'El contador seleccionado no está registrado.', [], 404);
  717. }
  718. $now = $this->functionsController->now();
  719. $nowStr = $now->toDateTimeString();
  720. DB::table('S002V01TCONA')->where([
  721. ['CONA_IDCO', '=', $idCounter],
  722. ['CONA_NULI', '=', $form['linea']]
  723. ])->update([
  724. 'CONA_INLE' => $form['time_interval'],
  725. 'CONA_UTIL' => $form['time_interval_unit'],
  726. 'CONA_TOIN' => $form['time_interval_tolerance'],
  727. 'CONA_UTTI' => $form['time_interval_tolerance_unit'],
  728. 'CONA_IDSC' => $idSCADA,
  729. 'CONA_USMO' => $idUser,
  730. 'CONA_FEMO' => $nowStr
  731. ]);
  732. $actions = DB::getQueryLog();
  733. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  734. $idac = $this->functionsController->registerActivity(
  735. $form['linea'],
  736. 'S002V01M06COAC',
  737. 'S002V01F01GECO',
  738. 'S002V01P02RECO',
  739. 'Actualización',
  740. "El usuario $name (" . $usr->USUA_IDUS . ") actualizó el contador #$idCounter.",
  741. $idUser,
  742. $nowStr,
  743. 'S002V01S01CONT'
  744. );
  745. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $form['linea']);
  746. return $this->responseController->makeResponse(false, 'EXITO.');
  747. }
  748. public function deleteCounter(Request $request) {
  749. DB::enableQueryLog();
  750. $validator = Validator::make($request->all(), [
  751. 'id_user' => 'required|string',
  752. 'linea' => 'required|integer',
  753. 'id_counter' => 'required|string'
  754. ]);
  755. if($validator->fails()){
  756. return $this->responseController->makeResponse(
  757. true,
  758. "Se encontraron uno o más errores.",
  759. $this->responseController->makeErrors(
  760. $validator->errors()->messages()
  761. ),
  762. 401
  763. );
  764. }
  765. $form = $request->all();
  766. $idUser = $this->encryptionController->decrypt($form['id_user']);
  767. if(!$idUser){
  768. return $this->responseController->makeResponse(true, 'El ID del usuario que realizó la petición no está encriptado correctamente.', [], 400);
  769. }
  770. $usr = DB::table('S002V01TUSUA')->where([
  771. ['USUA_NULI', '=', $form['linea']],
  772. ['USUA_IDUS', '=', $idUser],
  773. ])->first();
  774. if(is_null($usr)){
  775. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado.', [], 404);
  776. }
  777. $idCounter = $this->encryptionController->decrypt($form['id_counter']);
  778. if(!$idCounter){
  779. return $this->responseController->makeResponse(true, 'El ID del contador seleccionado no está encriptado correctamente.', [], 400);
  780. }
  781. $counter = DB::table('S002V01TCONA')->where([
  782. ['CONA_NULI', '=', $form['linea']],
  783. ['CONA_IDCO', '=', $idCounter]
  784. ])->first();
  785. if(is_null($counter)){
  786. return $this->responseController->makeResponse(true, 'El contador seleccionado no está registrado.', [], 404);
  787. }
  788. $now = $this->functionsController->now();
  789. $nowStr = $now->toDateTimeString();
  790. DB::table('S002V01TCONA')->where([
  791. ['CONA_IDCO', '=', $idCounter],
  792. ['CONA_NULI', '=', $form['linea']]
  793. ])->update([
  794. 'CONA_ESTA' => 'Eliminado',
  795. 'CONA_USMO' => $idUser,
  796. 'CONA_FEMO' => $nowStr
  797. ]);
  798. $actions = DB::getQueryLog();
  799. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  800. $idac = $this->functionsController->registerActivity(
  801. $form['linea'],
  802. 'S002V01M06COAC',
  803. 'S002V01F02ELCO',
  804. '-',
  805. 'Eliminación',
  806. "El usuario $name (" . $usr->USUA_IDUS . ") eliminó el contador #$idCounter.",
  807. $idUser,
  808. $nowStr,
  809. 'S002V01S01CONT'
  810. );
  811. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $form['linea']);
  812. return $this->responseController->makeResponse(false, 'EXITO.');
  813. }
  814. public function updateActivator(Request $request) {
  815. DB::enableQueryLog();
  816. $validator = Validator::make($request->all(), [
  817. 'id_user' => 'required|string',
  818. 'linea' => 'required|integer',
  819. 'priority' => 'required|string|in:1,2,3,4',
  820. 'type' => 'required|string|in:CA,SI,ME,VA',
  821. 'condition' => 'required|json',
  822. 'counter' => 'required|string',
  823. 'id_activator' => 'required|string'
  824. ]);
  825. if($validator->fails()){
  826. return $this->responseController->makeResponse(
  827. true,
  828. "Se encontraron uno o más errores.",
  829. $this->responseController->makeErrors(
  830. $validator->errors()->messages()
  831. ),
  832. 401
  833. );
  834. }
  835. //$priorities = ['MA' => '1', 'AL' => '2', 'ME' => '3', 'BA' => '4'];
  836. $types = ['CA' => 'Calendario', 'SI' => 'Sintoma', 'ME' => 'Medida', 'VA' => 'Valor'];
  837. $form = $request->all();
  838. $idUser = $this->encryptionController->decrypt($form['id_user']);
  839. if(!$idUser){
  840. return $this->responseController->makeResponse(true, 'El ID del usuario que realizó la petición no está encriptado correctamente.', [], 400);
  841. }
  842. $usr = DB::table('S002V01TUSUA')->where([
  843. ['USUA_NULI', '=', $form['linea']],
  844. ['USUA_IDUS', '=', $idUser],
  845. ])->first();
  846. if(is_null($usr)){
  847. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado.', [], 404);
  848. }
  849. $idCounter = $this->encryptionController->decrypt($form['counter']);
  850. if(!$idCounter){
  851. return $this->responseController->makeResponse(true, 'El ID del contador relacionado no está encriptado correctamente.', [], 400);
  852. }
  853. $counter = DB::table('S002V01TCONA')->where([
  854. ['CONA_NULI', '=', $form['linea']],
  855. ['CONA_IDCO', '=', $idCounter]
  856. ])->first();
  857. if(is_null($counter)){
  858. return $this->responseController->makeResponse(true, 'El contador relacionado no está registrado.', [], 404);
  859. }
  860. $idActivator = $this->encryptionController->decrypt($form['id_activator']);
  861. if(!$idActivator){
  862. return $this->responseController->makeResponse(true, 'El ID del activador seleccionado no está encriptado correctamente.', [], 400);
  863. }
  864. $activator = DB::table('S002V01TACTI')->where([
  865. ['ACTI_IDAC', '=', $idActivator],
  866. ['ACTI_NULI', '=', $form['linea']]
  867. ])->first();
  868. if(is_null($activator)){
  869. return $this->responseController->makeResponse(true, 'El activador seleccionado no está registrado.', [], 404);
  870. }
  871. $priority = $form['priority'];
  872. $type = $types[$form['type']];
  873. $conditionArr = json_decode($form['condition'], true);
  874. if(count($conditionArr) == 0){
  875. return $this->responseController->makeResponse(true, 'El arreglo de la condición de activación está vació.', [], 400);
  876. }else{
  877. switch($form['type']){
  878. case 'CA':
  879. if(count($conditionArr) != 5){
  880. return $this->responseController->makeResponse(true, 'El arreglo de la condición tiene un formato inválido.', [], 400);
  881. }
  882. $keysCA = ['startDate', 'startHour', 'repeat', 'customRepeat', 'color'];
  883. foreach($keysCA as $key){
  884. if(!array_key_exists($key, $conditionArr)){
  885. return $this->responseController->makeResponse(true, "El arreglo de la condición no contiene el elemento '$key'.", [], 400);
  886. }
  887. }
  888. break;
  889. case 'SI':
  890. if(count($conditionArr) != 6){
  891. return $this->responseController->makeResponse(true, 'El arreglo de la condición tiene un formato inválido.', [], 400);
  892. }
  893. $keysSI = ['sign', 'startDate', 'startHour', 'repeat', 'customRepeat', 'color'];
  894. foreach($keysSI as $key){
  895. if(!array_key_exists($key, $conditionArr)){
  896. return $this->responseController->makeResponse(true, "El arreglo de la condición no contiene el elemento '$key'.", [], 400);
  897. }
  898. }
  899. $idSymptom = $this->encryptionController->decrypt($conditionArr['sign']);
  900. if(!$idSymptom){
  901. return $this->responseController->makeResponse(true, 'El ID del síntoma relacionado no está encriptado correctamente.', [], 400);
  902. }
  903. $symptom = DB::table('S002V01TLISI')->where([
  904. ['LISI_IDSI', '=', $idSymptom],
  905. ['LISI_NULI', '=', $form['linea']]
  906. ])->first();
  907. if(is_null($symptom)){
  908. return $this->responseController->makeResponse(true, 'El síntoma relacionado no está registrado.', [], 404);
  909. }else if($symptom->LISI_ESTA == 'Eliminado'){
  910. return $this->responseController->makeResponse(true, 'El síntoma relacionado está eliminado.', [], 404);
  911. }
  912. $conditionArr['sign'] = $idSymptom;
  913. break;
  914. case 'ME':
  915. if(count($conditionArr) != 7){
  916. return $this->responseController->makeResponse(true, 'El arreglo de la condición tiene un formato inválido.', [], 400);
  917. }
  918. $keysSI = ['minValue', 'maxValue', 'magnitude', 'customMagnitude', 'unit', 'customUnit', 'color'];
  919. foreach($keysSI as $key){
  920. if(!array_key_exists($key, $conditionArr)){
  921. return $this->responseController->makeResponse(true, "El arreglo de la condición no contiene el elemento '$key'.", [], 400);
  922. }
  923. }
  924. $idMagnitude = $this->encryptionController->decrypt($conditionArr['magnitude']);
  925. if(!$idMagnitude){
  926. return $this->responseController->makeResponse(true, 'El ID de la magnitud relacionada no está encriptado correctamente.', [], 400);
  927. }
  928. $magnitude = DB::table('S002V01TMAGN')->where([
  929. ['MAGN_NULI', '=', $form['linea']],
  930. ['MAGN_IDMA', '=', $idMagnitude]
  931. ])->first();
  932. if(is_null($magnitude)){
  933. return $this->responseController->makeResponse(true, 'La magnitud relacionada no está registrado.', [], 404);
  934. }else if($magnitude->MAGN_ESTA == 'Eliminado'){
  935. return $this->responseController->makeResponse(true, 'La magnitud relacionada está eliminada.', [], 404);
  936. }
  937. $idUnit = $this->encryptionController->decrypt($conditionArr['unit']);
  938. if(!$idUnit){
  939. return $this->responseController->makeResponse(true, 'El ID de la unidad relacionada no está encriptado correctamente.', [], 400);
  940. }
  941. $unit = DB::table('S002V01TLIME')->where([
  942. ['LIME_IDME', '=', $idUnit],
  943. ['LIME_NULI', '=', $form['linea']],
  944. ['LIME_MAGN', '=', $idMagnitude],
  945. ])->first();
  946. if(is_null($unit)){
  947. return $this->responseController->makeResponse(true, 'La unidad relacionada no está registrado.', [], 404);
  948. }else if($unit->LIME_ESTA == 'Eliminado'){
  949. return $this->responseController->makeResponse(true, 'La unidad relacionada está eliminada.', [], 404);
  950. }
  951. $conditionArr['magnitude'] = $idMagnitude ;
  952. $conditionArr['unit'] = $idUnit;
  953. break;
  954. case 'VA':
  955. if(count($conditionArr) != 7){
  956. return $this->responseController->makeResponse(true, 'El arreglo de la condición tiene un formato inválido.', [], 400);
  957. }
  958. $keysVA = ['comparison', 'limitValue', 'magnitude', 'customMagnitude', 'unit', 'customUnit', 'color'];
  959. foreach($keysVA as $key){
  960. if(!array_key_exists($key, $conditionArr)){
  961. return $this->responseController->makeResponse(true, "El arreglo de la condición no contiene el elemento '$key'.", [], 400);
  962. }
  963. }
  964. $idMagnitude = $this->encryptionController->decrypt($conditionArr['magnitude']);
  965. if(!$idMagnitude){
  966. return $this->responseController->makeResponse(true, 'El ID de la magnitud relacionada no está encriptado correctamente.', [], 400);
  967. }
  968. $magnitude = DB::table('S002V01TMAGN')->where([
  969. ['MAGN_NULI', '=', $form['linea']],
  970. ['MAGN_IDMA', '=', $idMagnitude]
  971. ])->first();
  972. if(is_null($magnitude)){
  973. return $this->responseController->makeResponse(true, 'La magnitud relacionada no está registrado.', [], 404);
  974. }else if($magnitude->MAGN_ESTA == 'Eliminado'){
  975. return $this->responseController->makeResponse(true, 'La magnitud relacionada está eliminada.', [], 404);
  976. }
  977. $idUnit = $this->encryptionController->decrypt($conditionArr['unit']);
  978. if(!$idUnit){
  979. return $this->responseController->makeResponse(true, 'El ID de la unidad relacionada no está encriptado correctamente.', [], 400);
  980. }
  981. $unit = DB::table('S002V01TLIME')->where([
  982. ['LIME_IDME', '=', $idUnit],
  983. ['LIME_NULI', '=', $form['linea']],
  984. ['LIME_MAGN', '=', $idMagnitude],
  985. ])->first();
  986. if(is_null($unit)){
  987. return $this->responseController->makeResponse(true, 'La unidad relacionada no está registrado.', [], 404);
  988. }else if($unit->LIME_ESTA == 'Eliminado'){
  989. return $this->responseController->makeResponse(true, 'La unidad relacionada está eliminada.', [], 404);
  990. }
  991. $conditionArr['magnitude'] = $idMagnitude ;
  992. $conditionArr['unit'] = $idUnit;
  993. break;
  994. default:
  995. return $this->responseController->makeResponse(true, 'El tipo de contador es inválido.', [], 400);
  996. break;
  997. }
  998. }
  999. $activationConfig = $form['condition'];
  1000. $activationConfigArr = json_decode($activationConfig, true);
  1001. $startDateObj = new Carbon($activationConfigArr['startDate']);
  1002. $startDateStr = $startDateObj->toDateString();
  1003. $startDateTimeObj = new Carbon("$startDateStr $activationConfigArr[startHour]");
  1004. $startDateTimeStr = $startDateTimeObj->toDateTimeString();
  1005. $now = $this->functionsController->now();
  1006. $nowStr = $now->toDateTimeString();
  1007. $preventiveOrdersRelated = DB::table('S002V01TOTPR')->where([
  1008. ['OTPR_NULI', '=', $form['linea']],
  1009. ['OTPR_ACAS', '=', $idActivator]
  1010. ])->get()->all();
  1011. foreach($preventiveOrdersRelated as $preventiveOrder){
  1012. DB::table('S002V01TOTPR')->where([
  1013. ['OTPR_NULI', '=', $form['linea']],
  1014. ['OTPR_IDOT', '=', $preventiveOrder->OTPR_IDOT],
  1015. ])->update([
  1016. "OTPR_FIAP" => $startDateTimeStr,
  1017. "OTPR_USMO" => $idUser,
  1018. "OTPR_FEMO" => $nowStr
  1019. ]);
  1020. }
  1021. $conditionStr = json_encode($conditionArr);
  1022. DB::table('S002V01TACTI')->where([
  1023. ['ACTI_IDAC', '=', $idActivator],
  1024. ['ACTI_NULI', '=', $form['linea']]
  1025. ])->update([
  1026. 'ACTI_PRIO' => $priority,
  1027. 'ACTI_TIAC' => $type,
  1028. 'ACTI_COAC' => $conditionStr,
  1029. 'ACTI_CORE' => $idCounter,
  1030. 'ACTI_USMO' => $idUser,
  1031. 'ACTI_FEMO' => $nowStr
  1032. ]);
  1033. $actions = DB::getQueryLog();
  1034. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  1035. $idac = $this->functionsController->registerActivity(
  1036. $form['linea'],
  1037. 'S002V01M06COAC',
  1038. 'S002V01F01GEAC',
  1039. 'S002V01P02REAC',
  1040. 'Actualización',
  1041. "El usuario $name (" . $usr->USUA_IDUS . ") actualizó el activador #$idActivator.",
  1042. $idUser,
  1043. $nowStr,
  1044. 'S002V01S02ACTI'
  1045. );
  1046. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $form['linea']);
  1047. return $this->responseController->makeResponse(false, 'EXITO.');
  1048. }
  1049. public function deleteActivator(Request $request) {
  1050. DB::enableQueryLog();
  1051. $validator = Validator::make($request->all(), [
  1052. 'id_user' => 'required|string',
  1053. 'linea' => 'required|integer',
  1054. 'id_activator' => 'required|string'
  1055. ]);
  1056. if($validator->fails()){
  1057. return $this->responseController->makeResponse(
  1058. true,
  1059. "Se encontraron uno o más errores.",
  1060. $this->responseController->makeErrors(
  1061. $validator->errors()->messages()
  1062. ),
  1063. 401
  1064. );
  1065. }
  1066. $form = $request->all();
  1067. $idUser = $this->encryptionController->decrypt($form['id_user']);
  1068. if(!$idUser){
  1069. return $this->responseController->makeResponse(true, 'El ID del usuario que realizó la petición no está encriptado correctamente.', [], 400);
  1070. }
  1071. $usr = DB::table('S002V01TUSUA')->where([
  1072. ['USUA_NULI', '=', $form['linea']],
  1073. ['USUA_IDUS', '=', $idUser],
  1074. ])->first();
  1075. if(is_null($usr)){
  1076. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado.', [], 404);
  1077. }
  1078. $idActivator = $this->encryptionController->decrypt($form['id_activator']);
  1079. if(!$idActivator){
  1080. return $this->responseController->makeResponse(true, 'El ID del activador seleccionado no está encriptado correctamente.', [], 400);
  1081. }
  1082. $activator = DB::table('S002V01TACTI')->where([
  1083. ['ACTI_IDAC', '=', $idActivator],
  1084. ['ACTI_NULI', '=', $form['linea']]
  1085. ])->first();
  1086. if(is_null($activator)){
  1087. return $this->responseController->makeResponse(true, 'El activador seleccionado no está registrado.', [], 404);
  1088. }
  1089. $now = $this->functionsController->now();
  1090. $nowStr = $now->toDateTimeString();
  1091. DB::table('S002V01TACTI')->where([
  1092. ['ACTI_IDAC', '=', $idActivator],
  1093. ['ACTI_NULI', '=', $form['linea']]
  1094. ])->update([
  1095. 'ACTI_ESTA' => 'Eliminado',
  1096. 'ACTI_USMO' => $idUser,
  1097. 'ACTI_FEMO' => $nowStr
  1098. ]);
  1099. $actions = DB::getQueryLog();
  1100. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  1101. $idac = $this->functionsController->registerActivity(
  1102. $form['linea'],
  1103. 'S002V01M06COAC',
  1104. 'S002V01F01GEAC',
  1105. 'S002V01P02REAC',
  1106. 'Eliminación',
  1107. "El usuario $name (" . $usr->USUA_IDUS . ") eliminó el activador #$idActivator.",
  1108. $idUser,
  1109. $nowStr,
  1110. 'S002V01S02ACTI'
  1111. );
  1112. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $form['linea']);
  1113. return $this->responseController->makeResponse(false, 'EXITO.');
  1114. }
  1115. public function getCountersByEquipment($equipmentCode, $idUser, $line) {
  1116. DB::enableQueryLog();
  1117. $idUser = $this->encryptionController->decrypt($idUser);
  1118. if(!$idUser){
  1119. return $this->responseController->makeResponse(true, 'El ID del usuario que realizó la petición no está encriptado correctamente.', [], 400);
  1120. }
  1121. $usr = DB::table('S002V01TUSUA')->where([
  1122. ['USUA_NULI', '=', $line],
  1123. ['USUA_IDUS', '=', $idUser],
  1124. ])->first();
  1125. if(is_null($usr)){
  1126. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado.', [], 404);
  1127. }
  1128. $equipmentCode = $this->encryptionController->decrypt($equipmentCode);
  1129. if(!$equipmentCode){
  1130. return $this->responseController->makeResponse(true, 'El código del equipamiento relacionado no está encriptado correctamente.', [], 400);
  1131. }
  1132. $equipment = DB::table('S002V01TEQUI')->where([
  1133. ['EQUI_COEQ', '=', $equipmentCode],
  1134. ['EQUI_NULI', '=', $line]
  1135. ])->first();
  1136. if(is_null($equipment)){
  1137. return $this->responseController->makeResponse(true, 'El equipamiento relacionado no está registrado.', [], 404);
  1138. }
  1139. $counters = DB::table('S002V01TCONA')->select([
  1140. 'CONA_IDCO AS ID_CONTADOR',
  1141. 'ACTI_IDAC AS ID_ACTIVADOR',
  1142. 'ACTI_PRIO AS PRIORIDAD',
  1143. 'CONA_IDSC AS ID_SCADA',
  1144. 'LISC_NOSC AS NOMBRE_SCADA',
  1145. 'CONA_ESTA AS ESTADO',
  1146. 'CONA_FERE AS FECREG',
  1147. 'CONA_USRE AS USRREG',
  1148. 'CONA_FEMO AS FECMOD',
  1149. 'CONA_USMO AS USRMOD'
  1150. ])->join('S002V01TACTI', 'ACTI_CORE', '=', 'CONA_IDCO')->where([
  1151. ['CONA_NULI', '=', $line],
  1152. ['CONA_COEQ', '=', $equipmentCode]
  1153. ])->join('S002V01TLISC', 'LISC_IDSC', '=', 'CONA_IDSC')->orderBy('CONA_IDCO', 'asc')->get()->all();
  1154. foreach($counters as $key=>$counter){
  1155. $counter->ID_CONTADOR = $this->encryptionController->encrypt($counter->ID_CONTADOR);
  1156. if(!is_null($counter->ID_ACTIVADOR)){
  1157. $counter->ID_ACTIVADOR = $this->encryptionController->encrypt($counter->ID_ACTIVADOR);
  1158. }
  1159. $counter->ID_SCADA = $this->encryptionController->encrypt($counter->ID_SCADA);
  1160. $usrReg = DB::table('S002V01TUSUA')->where([
  1161. ['USUA_NULI', '=', $line],
  1162. ['USUA_IDUS', '=', $counter->USRREG]
  1163. ])->first();
  1164. $nameReg = $this->functionsController->joinName($usrReg->USUA_NOMB, $usrReg->USUA_APPA, $usrReg->USUA_APMA);
  1165. $counter->USRREG = $nameReg . " (" . $counter->USRREG . ")";
  1166. if(!is_null($counter->USRMOD)){
  1167. $usrMod = DB::table('S002V01TUSUA')->where([
  1168. ['USUA_NULI', '=', $line],
  1169. ['USUA_IDUS', '=', $counter->USRMOD]
  1170. ])->first();
  1171. $nameMod = $this->functionsController->joinName($usrMod->USUA_NOMB, $usrMod->USUA_APPA, $usrMod->USUA_APMA);
  1172. $counter->USRMOD = $nameMod . " (" . $counter->USRMOD . ")";
  1173. }
  1174. $counters[$key] = $counter;
  1175. }
  1176. $now = $this->functionsController->now();
  1177. $nowStr = $now->toDateTimeString();
  1178. $actions = DB::getQueryLog();
  1179. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  1180. $idac = $this->functionsController->registerActivity(
  1181. $line,
  1182. 'S002V01M06COAC',
  1183. 'S002V01F01GECO',
  1184. 'S002V01P01COCO',
  1185. 'Consulta',
  1186. "El usuario $name (" . $usr->USUA_IDUS . ") consultó los contadores relacionados al equipamiento $equipmentCode.",
  1187. $idUser,
  1188. $nowStr,
  1189. 'S002V01S01CONT'
  1190. );
  1191. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $line);
  1192. return $this->responseController->makeresponse(false, "EXITO", $counters);
  1193. }
  1194. public function getCounterLastMeasure($idCounter, $idUser, $line) {
  1195. DB::enableQueryLog();
  1196. $idUser = $this->encryptionController->decrypt($idUser);
  1197. if(!$idUser){
  1198. return $this->responseController->makeResponse(true, 'El ID del usuario que realizó la petición no está encriptado correctamente.', [], 400);
  1199. }
  1200. $usr = DB::table('S002V01TUSUA')->where([
  1201. ['USUA_NULI', '=', $line],
  1202. ['USUA_IDUS', '=', $idUser],
  1203. ])->first();
  1204. if(is_null($usr)){
  1205. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado.', [], 404);
  1206. }
  1207. $idCounter = $this->encryptionController->decrypt($idCounter);
  1208. if(!$idCounter){
  1209. return $this->responseController->makeResponse(true, 'El ID del contador no está encriptado correctamente.', [], 400);
  1210. }
  1211. $counter = DB::table('S002V01TCONA')->where([
  1212. ['CONA_IDCO', '=', $idCounter],
  1213. ['CONA_NULI', '=', $line]
  1214. ])->first();
  1215. if(is_null($counter)){
  1216. return $this->responseController->makeResponse(true, 'El contador solicitado no está registrado.', [], 404);
  1217. }
  1218. $measure = DB::table('S002V01TMEDI')->select([
  1219. 'MEDI_IDME AS ID_MEDIDA',
  1220. 'MEDI_CORE AS CONTADOR',
  1221. 'MEDI_VALO AS VALOR',
  1222. 'MEDI_WSRE AS ID_SERVICIO_WEB',
  1223. 'LSWE_URLX AS URL_SERVICIO_WEB',
  1224. 'MEDI_HORE AS HORA_REGISTRO',
  1225. ])->join('S002V01TLSWE', 'LSWE_IDSW', '=', 'MEDI_WSRE')->where([
  1226. ['MEDI_NULI', '=', $line],
  1227. ['MEDI_CORE', '=', $idCounter]
  1228. ])->orderBy('MEDI_HORE', 'desc')->first();
  1229. if(!is_null($measure)){
  1230. $measure->ID_MEDIDA = $this->encryptionController->encrypt($measure->ID_MEDIDA);
  1231. $measure->CONTADOR = $this->encryptionController->encrypt($measure->CONTADOR);
  1232. $measure->ID_SERVICIO_WEB = $this->encryptionController->encrypt($measure->ID_SERVICIO_WEB);
  1233. }
  1234. $now = $this->functionsController->now();
  1235. $nowStr = $now->toDateTimeString();
  1236. $actions = DB::getQueryLog();
  1237. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  1238. $idac = $this->functionsController->registerActivity(
  1239. $line,
  1240. 'S002V01M06COAC',
  1241. 'S002V01F01GECO',
  1242. 'S002V01P01COCO',
  1243. 'Consulta',
  1244. "El usuario $name (" . $usr->USUA_IDUS . ") consultó las medidas relacionadas al contador #$idCounter.",
  1245. $idUser,
  1246. $nowStr,
  1247. 'S002V01S01CONT'
  1248. );
  1249. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $line);
  1250. return $this->responseController->makeresponse(false, "EXITO", is_null($measure) ? [] : [$measure]);
  1251. }
  1252. public function getCounterMeasures($idCounter, $order, $direction, $limit, $offset, $idUser, $line) {
  1253. DB::enableQueryLog();
  1254. $idUser = $this->encryptionController->decrypt($idUser);
  1255. if(!$idUser){
  1256. return $this->responseController->makeResponse(true, 'El ID del usuario que realizó la petición no está encriptado correctamente.', [], 400);
  1257. }
  1258. $usr = DB::table('S002V01TUSUA')->where([
  1259. ['USUA_NULI', '=', $line],
  1260. ['USUA_IDUS', '=', $idUser],
  1261. ])->first();
  1262. if(is_null($usr)){
  1263. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado.', [], 404);
  1264. }
  1265. $idCounter = $this->encryptionController->decrypt($idCounter);
  1266. if(!$idCounter){
  1267. return $this->responseController->makeResponse(true, 'El ID del contador no está encriptado correctamente.', [], 400);
  1268. }
  1269. $counter = DB::table('S002V01TCONA')->where([
  1270. ['CONA_IDCO', '=', $idCounter],
  1271. ['CONA_NULI', '=', $line]
  1272. ])->first();
  1273. if(is_null($counter)){
  1274. return $this->responseController->makeResponse(true, 'El contador solicitado no está registrado.', [], 404);
  1275. }
  1276. $enabledOrders = [
  1277. 'ID_MEDIDA' => 'MEDI_IDME',
  1278. 'VALOR' => 'MEDI_VALO',
  1279. 'FECREG' => 'MEDI_HORE'
  1280. ];
  1281. if(!isset($enabledOrders[$order])){
  1282. return $this->responseController->makeResponse(true, 'El orden solicitado para los registros es inválido.', [], 404);
  1283. }
  1284. $orderDB = $enabledOrders[$order];
  1285. $enbaledDirections = [
  1286. 'D' => 'desc',
  1287. 'A' => 'asc'
  1288. ];
  1289. if(!isset($enbaledDirections[$direction])){
  1290. return $this->responseController->makeResponse(true, 'La dirección del orden solicitado para los registros es inválida.', [], 404);
  1291. }
  1292. $directionDB = $enbaledDirections[$direction];
  1293. $limit = intval($limit);
  1294. $offset = intval($offset);
  1295. $measures = DB::table('S002V01TMEDI')->select([
  1296. 'MEDI_IDME AS ID_MEDIDA',
  1297. 'MEDI_CORE AS CONTADOR',
  1298. 'MEDI_VALO AS VALOR',
  1299. 'MEDI_WSRE AS ID_SERVICIO_WEB',
  1300. 'LSWE_URLX AS URL_SERVICIO_WEB',
  1301. 'MEDI_HORE AS HORA_REGISTRO',
  1302. ])->join('S002V01TLSWE', 'LSWE_IDSW', '=', 'MEDI_WSRE')->where([
  1303. ['MEDI_NULI', '=', $line],
  1304. ['MEDI_CORE', '=', $idCounter]
  1305. ])->orderBy($orderDB, $directionDB)
  1306. ->offset($offset)->limit($limit)->get()->all();
  1307. foreach($measures as $key=>$measure){
  1308. $measure->ID_MEDIDA = $this->encryptionController->encrypt($measure->ID_MEDIDA);
  1309. $measure->CONTADOR = $this->encryptionController->encrypt($measure->CONTADOR);
  1310. $measure->ID_SERVICIO_WEB = $this->encryptionController->encrypt($measure->ID_SERVICIO_WEB);
  1311. $measures[$key];
  1312. }
  1313. $now = $this->functionsController->now();
  1314. $nowStr = $now->toDateTimeString();
  1315. $actions = DB::getQueryLog();
  1316. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  1317. $idac = $this->functionsController->registerActivity(
  1318. $line,
  1319. 'S002V01M06COAC',
  1320. 'S002V01F01GECO',
  1321. 'S002V01P01COCO',
  1322. 'Consulta',
  1323. "El usuario $name (" . $usr->USUA_IDUS . ") consultó las medidas relacionadas al contador #$idCounter.",
  1324. $idUser,
  1325. $nowStr,
  1326. 'S002V01S01CONT'
  1327. );
  1328. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $line);
  1329. return $this->responseController->makeresponse(false, "EXITO", $measures);
  1330. }
  1331. public function getCounterMeasuresLength($idCounter, $idUser, $line) {
  1332. DB::enableQueryLog();
  1333. $idUser = $this->encryptionController->decrypt($idUser);
  1334. if(!$idUser){
  1335. return $this->responseController->makeResponse(true, 'El ID del usuario que realizó la petición no está encriptado correctamente.', [], 400);
  1336. }
  1337. $usr = DB::table('S002V01TUSUA')->where([
  1338. ['USUA_NULI', '=', $line],
  1339. ['USUA_IDUS', '=', $idUser],
  1340. ])->first();
  1341. if(is_null($usr)){
  1342. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado.', [], 404);
  1343. }
  1344. $idCounter = $this->encryptionController->decrypt($idCounter);
  1345. if(!$idCounter){
  1346. return $this->responseController->makeResponse(true, 'El ID del contador no está encriptado correctamente.', [], 400);
  1347. }
  1348. $counter = DB::table('S002V01TCONA')->where([
  1349. ['CONA_IDCO', '=', $idCounter],
  1350. ['CONA_NULI', '=', $line]
  1351. ])->first();
  1352. if(is_null($counter)){
  1353. return $this->responseController->makeResponse(true, 'El contador solicitado no está registrado.', [], 404);
  1354. }
  1355. $measures = DB::table('S002V01TMEDI')->select([
  1356. 'MEDI_IDME AS ID_MEDIDA',
  1357. 'MEDI_CORE AS CONTADOR',
  1358. 'MEDI_VALO AS VALOR',
  1359. 'MEDI_WSRE AS ID_SERVICIO_WEB',
  1360. 'LSWE_URLX AS URL_SERVICIO_WEB',
  1361. 'MEDI_HORE AS HORA_REGISTRO',
  1362. ])->join('S002V01TLSWE', 'LSWE_IDSW', '=', 'MEDI_WSRE')->where([
  1363. ['MEDI_NULI', '=', $line],
  1364. ['MEDI_CORE', '=', $idCounter]
  1365. ])->get()->all();
  1366. $now = $this->functionsController->now();
  1367. $nowStr = $now->toDateTimeString();
  1368. $actions = DB::getQueryLog();
  1369. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  1370. $idac = $this->functionsController->registerActivity(
  1371. $line,
  1372. 'S002V01M06COAC',
  1373. 'S002V01F01GECO',
  1374. 'S002V01P01COCO',
  1375. 'Consulta',
  1376. "El usuario $name (" . $usr->USUA_IDUS . ") consultó las medidas relacionadas al contador #$idCounter.",
  1377. $idUser,
  1378. $nowStr,
  1379. 'S002V01S01CONT'
  1380. );
  1381. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $line);
  1382. return $this->responseController->makeresponse(false, "EXITO", ['MEASURES_LENGTH' => count($measures)]);
  1383. }
  1384. public function getRelatedCounters($idCounter, $idUser, $line) {
  1385. DB::enableQueryLog();
  1386. $idUser = $this->encryptionController->decrypt($idUser);
  1387. if(!$idUser){
  1388. return $this->responseController->makeResponse(true, 'El ID del usuario que realizó la petición no está encriptado correctamente.', [], 400);
  1389. }
  1390. $usr = DB::table('S002V01TUSUA')->where([
  1391. ['USUA_NULI', '=', $line],
  1392. ['USUA_IDUS', '=', $idUser],
  1393. ])->first();
  1394. if(is_null($usr)){
  1395. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado.', [], 404);
  1396. }
  1397. $idCounter = $this->encryptionController->decrypt($idCounter);
  1398. if(!$idCounter){
  1399. return $this->responseController->makeResponse(true, 'El ID del contador no está encriptado correctamente.', [], 400);
  1400. }
  1401. $counter = DB::table('S002V01TCONA')->where([
  1402. ['CONA_IDCO', '=', $idCounter],
  1403. ['CONA_NULI', '=', $line]
  1404. ])->first();
  1405. if(is_null($counter)){
  1406. return $this->responseController->makeResponse(true, 'El contador solicitado no está registrado.', [], 404);
  1407. }
  1408. var_dump($idCounter);
  1409. }
  1410. public function relateCounters(Request $request) {
  1411. DB::enableQueryLog();
  1412. $validator = Validator::make($request->all(), [
  1413. 'id_user' => 'required|string',
  1414. 'linea' => 'required|integer',
  1415. 'id_counter' => 'required|string',
  1416. 'related_counters' => 'required|json'
  1417. ]);
  1418. if($validator->fails()){
  1419. return $this->responseController->makeResponse(
  1420. true,
  1421. "Se encontraron uno o más errores.",
  1422. $this->responseController->makeErrors(
  1423. $validator->errors()->messages()
  1424. ),
  1425. 401
  1426. );
  1427. }
  1428. $form = $request->all();
  1429. $idUser = $this->encryptionController->decrypt($form['id_user']);
  1430. if(!$idUser){
  1431. return $this->responseController->makeResponse(true, 'El ID del usuario que realizó la petición no está encriptado correctamente.', [], 400);
  1432. }
  1433. $usr = DB::table('S002V01TUSUA')->where([
  1434. ['USUA_NULI', '=', $form['linea']],
  1435. ['USUA_IDUS', '=', $idUser],
  1436. ])->first();
  1437. if(is_null($usr)){
  1438. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado.', [], 404);
  1439. }
  1440. $idCounter = $this->encryptionController->decrypt($form['id_counter']);
  1441. if(!$idCounter){
  1442. return $this->responseController->makeResponse(true, 'El ID del contador seleccionado no está encriptado correctamente.', [], 400);
  1443. }
  1444. $counter = DB::table('S002V01TCONA')->where([
  1445. ['CONA_NULI', '=', $form['linea']],
  1446. ['CONA_IDCO', '=', $idCounter]
  1447. ])->first();
  1448. if(is_null($counter)){
  1449. return $this->responseController->makeResponse(true, 'El contador seleccionado no está registrado.', [], 404);
  1450. }
  1451. $relatedCountersRegisteredArr = json_decode($counter->CONA_COVI, true);
  1452. $relatedCountersArr = json_decode($form['related_counters'], true);
  1453. if(!is_array($relatedCountersArr)){
  1454. return $this->responseController->makeResponse(true, 'El campo de contadores vinculados no es un arreglo.', [], 400);
  1455. }
  1456. $now = $this->functionsController->now();
  1457. $nowStr = $now->toDateTimeString();
  1458. $relatedCountersArrDec = [];
  1459. foreach($relatedCountersArr as $relatedCounterEnc){
  1460. $relatedCounterDec = $this->encryptionController->decrypt($relatedCounterEnc);
  1461. $relatedCounter = DB::table('S002V01TCONA')->where([
  1462. ['CONA_IDCO', '=', $relatedCounterDec],
  1463. ['CONA_NULI', '=', $form['linea']]
  1464. ])->first();
  1465. if(is_null($relatedCounter)){
  1466. return $this->responseController->makeResponse(true, "El contador #$relatedCounterDec no está registrado.", [], 404);
  1467. }
  1468. if(!in_array($relatedCounter, $relatedCountersRegisteredArr)){
  1469. $relatedCountersRegisteredArr[] = $relatedCounterDec;
  1470. }
  1471. $relatedCounterRelatedCountersArr = json_decode($relatedCounter->CONA_COVI);
  1472. if(!in_array($idCounter, $relatedCounterRelatedCountersArr)){
  1473. $relatedCounterRelatedCountersArr[] = $idCounter;
  1474. }
  1475. $relatedCounterCOVI = json_encode($relatedCounterRelatedCountersArr);
  1476. DB::table('S002V01TCONA')->where([
  1477. ['CONA_IDCO', '=', $relatedCounterDec],
  1478. ['CONA_NULI', '=', $form['linea']]
  1479. ])->update([
  1480. 'CONA_COVI' => $relatedCounterCOVI,
  1481. 'CONA_USMO' => $idUser,
  1482. 'CONA_FEMO' => $nowStr
  1483. ]);
  1484. $relatedCountersArrDec[] = $relatedCounterDec;
  1485. }
  1486. $countersWithRelation = DB::table('S002V01TCONA')->where('CONA_NULI', '=', $form['linea'])
  1487. ->whereJsonContains('CONA_COVI', $idCounter)->get()->all();
  1488. foreach($countersWithRelation as $counterWithRelation){
  1489. if(!in_array($counterWithRelation->CONA_IDCO, $relatedCountersArrDec)){
  1490. $counterWithRelationRelatedCounters = json_decode($counterWithRelation->CONA_COVI, true);
  1491. $counterWithRelationRelatedCountersFn = [];
  1492. foreach($counterWithRelationRelatedCounters as $counterWithRelationRelatedCounter){
  1493. if($idCounter != $counterWithRelationRelatedCounter){
  1494. $counterWithRelationRelatedCountersFn[] = $counterWithRelationRelatedCounter;
  1495. }
  1496. }
  1497. $counterWithRelationCOVI = json_encode($counterWithRelationRelatedCountersFn);
  1498. DB::table('S002V01TCONA')->where([
  1499. ['CONA_IDCO', '=', $counterWithRelation->CONA_IDCO],
  1500. ['CONA_NULI', '=', $form['linea']]
  1501. ])->update([
  1502. 'CONA_COVI' => $counterWithRelationCOVI,
  1503. 'CONA_USMO' => $idUser,
  1504. 'CONA_FEMO' => $nowStr
  1505. ]);
  1506. }
  1507. }
  1508. $relatedCountersFn = [];
  1509. foreach($relatedCountersRegisteredArr as $relatedCounterRegistered){
  1510. if(in_array($relatedCounterRegistered, $relatedCountersArrDec) && !in_array($relatedCounterRegistered, $relatedCountersFn)){
  1511. $relatedCountersFn[] = $relatedCounterRegistered;
  1512. }
  1513. }
  1514. $covi = json_encode($relatedCountersFn);
  1515. DB::table('S002V01TCONA')->where([
  1516. ['CONA_IDCO', '=', $idCounter],
  1517. ['CONA_NULI', '=', $form['linea']]
  1518. ])->update([
  1519. 'CONA_COVI' => $covi,
  1520. 'CONA_USMO' => $idUser,
  1521. 'CONA_FEMO' => $nowStr
  1522. ]);
  1523. $actions = DB::getQueryLog();
  1524. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  1525. $idac = $this->functionsController->registerActivity(
  1526. $form['linea'],
  1527. 'S002V01M06COAC',
  1528. 'S002V01F01GECO',
  1529. 'S002V01P02RECO',
  1530. 'Actualización',
  1531. "El usuario $name (" . $usr->USUA_IDUS . ") actualizó los contadores vinculados al contador #$idCounter.",
  1532. $idUser,
  1533. $nowStr,
  1534. 'S002V01S01CONT'
  1535. );
  1536. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $form['linea']);
  1537. return $this->responseController->makeResponse(false, 'EXITO.');
  1538. }
  1539. }