CountersActivatorsController.php 75 KB

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