CountersActivatorsController.php 76 KB

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