ControlPanelController.php 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398
  1. <?php
  2. namespace App\Http\Controllers;
  3. use Illuminate\Http\Request;
  4. use Illuminate\Support\Facades\DB;
  5. use Illuminate\Support\Facades\Validator;
  6. use Illuminate\Support\Facades\Storage;
  7. use Illuminate\Http\File;
  8. use PhpOffice\PhpSpreadsheet\Spreadsheet;
  9. use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
  10. use PhpOffice\PhpSpreadsheet\Style\Fill;
  11. use PhpOffice\PhpSpreadsheet\Style\Alignment;
  12. use PhpOffice\PhpSpreadsheet\IOFactory;
  13. class ControlPanelController extends Controller{
  14. private $responseController;
  15. private $encryptionController;
  16. private $functionsController;
  17. public function __construct(){
  18. $this->responseController = new ResponseController();
  19. $this->encryptionController = new EncryptionController();
  20. $this->functionsController = new FunctionsController();
  21. }
  22. public function getPanels($idUser, $line) {
  23. DB::enableQueryLog();
  24. $idUser = $this->encryptionController->decrypt($idUser);
  25. if(!$idUser){
  26. return $this->responseController->makeResponse(true, 'El ID del usuario que realizó la solicitud no está encriptado correctamente', [], 400);
  27. }
  28. $usr = DB::table('S002V01TUSUA')->where([
  29. ['USUA_NULI', '=', $line],
  30. ['USUA_IDUS', '=', $idUser],
  31. ])->first();
  32. if(is_null($usr)){
  33. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado', [], 404);
  34. }
  35. $panels = DB::table('S002V01TPACO')->select([
  36. 'PACO_IDPC AS ID_PANEL',
  37. 'PACO_NPCO AS NOMBRE_PANEL',
  38. 'PACO_ORPA AS ORIENTACION',
  39. 'PACO_ESTA AS ESTADO',
  40. 'PACO_USRE AS USRREG',
  41. 'PACO_FERE AS FECREG',
  42. 'PACO_USMO AS USRMOD',
  43. 'PACO_FEMO AS FECMOD'
  44. ])->where('PACO_NULI', '=', $line)->get()->all();
  45. foreach($panels as $key=>$panel){
  46. $relatedUsers = DB::table('S002V01TUSUA')->where([
  47. ['USUA_NULI', '=', $line],
  48. ['USUA_PCRE', '=', $panel->ID_PANEL]
  49. ])->get()->all();
  50. $panel->USUARIOS_RELACIONADOS = count($relatedUsers);
  51. $panel->ID_PANEL = $this->encryptionController->encrypt($panel->ID_PANEL);
  52. $usrReg = DB::table('S002V01TUSUA')->where([
  53. ['USUA_NULI', '=', $line],
  54. ['USUA_IDUS', '=', $panel->USRREG]
  55. ])->first();
  56. $usrRegName = $this->functionsController->joinName($usrReg->USUA_NOMB, $usrReg->USUA_APPA, $usrReg->USUA_APMA);
  57. $panel->USRREG = $usrRegName . " (" . $panel->USRREG . ")";
  58. if(!is_null($panel->USRMOD)){
  59. $usrMod = DB::table('S002V01TUSUA')->where([
  60. ['USUA_NULI', '=', $line],
  61. ['USUA_IDUS', '=', $panel->USRMOD]
  62. ])->first();
  63. $usrModName = $this->functionsController->joinName($usrMod->USUA_NOMB, $usrMod->USUA_APPA, $usrMod->USUA_APMA);
  64. $panel->USRMOD = $usrModName . " (" . $panel->USRMOD . ")";
  65. }
  66. $panels[$key] = $panel;
  67. }
  68. $now = $this->functionsController->now();
  69. $nowStr = $now->toDateTimeString();
  70. $actions = DB::getQueryLog();
  71. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  72. $idac = $this->functionsController->registerActivity(
  73. $line,
  74. 'S002V01M14PCSA',
  75. 'S002V01F04MIIG',
  76. 'S002V01P01PCIN',
  77. 'Consulta',
  78. "El usuario $name (" . $usr->USUA_IDUS . ") consultó los paneles de control registrados.",
  79. $idUser,
  80. $nowStr,
  81. );
  82. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $line);
  83. return $this->responseController->makeResponse(false, 'EXITO.', $panels);
  84. }
  85. public function registerPanel(Request $request) {
  86. DB::enableQueryLog();
  87. $validator = Validator::make($request->all(), [
  88. 'id_user' => 'required|string',
  89. 'linea' => 'required|integer',
  90. 'panel_name' => 'required|string|max:100',
  91. 'orientation' => 'required|string|in:H,V',
  92. ]);
  93. if($validator->fails()){
  94. return $this->responseController->makeResponse(
  95. true,
  96. "Se encontraron uno o más errores.",
  97. $this->responseController->makeErrors(
  98. $validator->errors()->messages()
  99. ),
  100. 401
  101. );
  102. }
  103. $form = $request->all();
  104. $idUser = $this->encryptionController->decrypt($form['id_user']);
  105. if(!$idUser){
  106. return $this->responseController->makeResponse(true, 'El ID de usuario no fue encriptado correctamente.', [], 400);
  107. }
  108. $usr = DB::table('S002V01TUSUA')->where([
  109. ['USUA_NULI', '=', $form['linea']],
  110. ['USUA_IDUS', '=', $idUser]
  111. ])->first();
  112. if(is_null($usr)){
  113. return $this->responseController->makeResponse(true, 'El usuario que realizó la petición no existe.', [], 404);
  114. }
  115. $orientations = ['V' => 'Vertical', 'H' => 'Horizontal'];
  116. $orientation = $orientations[$form['orientation']];
  117. $now = $this->functionsController->now();
  118. $nowStr = $now->toDateTimeString();
  119. $panelID = DB::table('S002V01TPACO')->insertGetId([
  120. 'PACO_NULI' => $form['linea'],
  121. 'PACO_NPCO' => $form['panel_name'],
  122. 'PACO_INRE' => '[]',
  123. 'PACO_ORPA' => $orientation,
  124. 'PACO_USRE' => $idUser,
  125. 'PACO_FERE' => $nowStr,
  126. ]);
  127. $actions = DB::getQueryLog();
  128. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  129. $idac = $this->functionsController->registerActivity(
  130. $form['linea'],
  131. 'S002V01M14PCSA',
  132. 'S002V01F04MIIG',
  133. 'S002V01P01PCIN',
  134. 'Registro',
  135. "El usuario $name (" . $usr->USUA_IDUS . ") registró el panel de control $form[panel_name] ($panelID).",
  136. $idUser,
  137. $nowStr,
  138. );
  139. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $form['linea']);
  140. return $this->responseController->makeResponse(false, 'EXITO.');
  141. }
  142. public function updatePanel(Request $request) {
  143. DB::enableQueryLog();
  144. $validator = Validator::make($request->all(), [
  145. 'id_user' => 'required|string',
  146. 'linea' => 'required|integer',
  147. 'id_panel' => 'required|string',
  148. 'panel_name' => 'required|string|max:100',
  149. 'orientation' => 'required|string|in:H,V',
  150. ]);
  151. if($validator->fails()){
  152. return $this->responseController->makeResponse(
  153. true,
  154. "Se encontraron uno o más errores.",
  155. $this->responseController->makeErrors(
  156. $validator->errors()->messages()
  157. ),
  158. 401
  159. );
  160. }
  161. $form = $request->all();
  162. $idUser = $this->encryptionController->decrypt($form['id_user']);
  163. if(!$idUser){
  164. return $this->responseController->makeResponse(true, 'El ID de usuario no fue encriptado correctamente.', [], 400);
  165. }
  166. $usr = DB::table('S002V01TUSUA')->where([
  167. ['USUA_NULI', '=', $form['linea']],
  168. ['USUA_IDUS', '=', $idUser]
  169. ])->first();
  170. if(is_null($usr)){
  171. return $this->responseController->makeResponse(true, 'El usuario que realizó la petición no existe.', [], 404);
  172. }
  173. $idPanel = $this->encryptionController->decrypt($form['id_panel']);
  174. if(!$idPanel){
  175. return $this->responseController->makeResponse(true, 'El ID del panel que desea acualizar no fue encriptado correctamente.', [], 400);
  176. }
  177. $panel = DB::table('S002V01TPACO')->where([
  178. ['PACO_NULI', '=', $form['linea']],
  179. ['PACO_IDPC', '=', $idPanel]
  180. ])->first();
  181. if(is_null($panel)){
  182. return $this->responseController->makeResponse(true, 'El panel que desea actualizar no existe.', [], 404);
  183. }
  184. $orientations = ['V' => 'Vertical', 'H' => 'Horizontal'];
  185. $orientation = $orientations[$form['orientation']];
  186. $now = $this->functionsController->now();
  187. $nowStr = $now->toDateTimeString();
  188. DB::table('S002V01TPACO')->where([
  189. ['PACO_NULI', '=', $form['linea']],
  190. ['PACO_IDPC', '=', $idPanel]
  191. ])->update([
  192. 'PACO_NPCO' => $form['panel_name'],
  193. 'PACO_ORPA' => $orientation,
  194. 'PACO_USMO' => $idUser,
  195. 'PACO_FEMO' => $nowStr,
  196. ]);
  197. $actions = DB::getQueryLog();
  198. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  199. $idac = $this->functionsController->registerActivity(
  200. $form['linea'],
  201. 'S002V01M14PCSA',
  202. 'S002V01F04MIIG',
  203. 'S002V01P01PCIN',
  204. 'Actualización',
  205. "El usuario $name (" . $usr->USUA_IDUS . ") actualizó el panel de control #$idPanel.",
  206. $idUser,
  207. $nowStr,
  208. );
  209. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $form['linea']);
  210. return $this->responseController->makeResponse(false, 'EXITO.');
  211. }
  212. public function getCountersList($idUser, $line) {
  213. DB::enableQueryLog();
  214. $idUser = $this->encryptionController->decrypt($idUser);
  215. if(!$idUser){
  216. return $this->responseController->makeResponse(true, 'El ID del usuario que realizó la solicitud no está encriptado correctamente', [], 400);
  217. }
  218. $usr = DB::table('S002V01TUSUA')->where([
  219. ['USUA_NULI', '=', $line],
  220. ['USUA_IDUS', '=', $idUser],
  221. ])->first();
  222. if(is_null($usr)){
  223. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado', [], 404);
  224. }
  225. $counters = DB::table('S002V01TCONA')->select([
  226. 'CONA_IDCO AS ID_CONTADOR',
  227. 'CONA_COEQ AS CODIGO_EQUIPAMIENTO',
  228. 'EQUI_IDEQ AS ID_EQUIPAMIENTO',
  229. 'EQUI_TIPO AS TIPO_EQUIPAMIENTO',
  230. 'EQUI_MODE AS MODELO_EQUIPAMIENTO'
  231. ])->join('S002V01TEQUI', 'EQUI_COEQ', '=', 'CONA_COEQ')->where([
  232. ['CONA_NULI', '=', $line],
  233. ])->get()->all();
  234. foreach($counters as $key=>$counter){
  235. $counterLastMeasures = DB::table('S002V01TMEDI')->select([
  236. 'MEDI_IDME AS ID_MEDIDA',
  237. 'MEDI_CORE AS CONTADOR',
  238. 'MEDI_VALO AS VALOR',
  239. 'MEDI_WSRE AS ID_SERVICIO_WEB',
  240. 'LSWE_URLX AS URL_SERVICIO_WEB',
  241. 'MEDI_HORE AS HORA_REGISTRO',
  242. ])->join('S002V01TLSWE', 'LSWE_IDSW', '=', 'MEDI_WSRE')->where([
  243. ['MEDI_NULI', '=', $line],
  244. ['MEDI_CORE', '=', $counter->ID_CONTADOR]
  245. ])->orderBy('MEDI_IDME', 'desc')
  246. ->offset(0)->limit(10)->get()->all();
  247. foreach($counterLastMeasures as $key0=>$measure){
  248. $measure->ID_MEDIDA = $this->encryptionController->encrypt($measure->ID_MEDIDA);
  249. $measure->CONTADOR = $this->encryptionController->encrypt($measure->CONTADOR);
  250. $measure->ID_SERVICIO_WEB = $this->encryptionController->encrypt($measure->ID_SERVICIO_WEB);
  251. $counterLastMeasures[$key0] = $measure;
  252. }
  253. $counter->LAST_MEASURES = $counterLastMeasures;
  254. $counter->ID_CONTADOR = $this->encryptionController->encrypt($counter->ID_CONTADOR);
  255. $counter->CODIGO_EQUIPAMIENTO = $this->encryptionController->encrypt($counter->CODIGO_EQUIPAMIENTO);
  256. $counter->ID_EQUIPAMIENTO = $this->encryptionController->encrypt($counter->ID_EQUIPAMIENTO);
  257. $counters[$key] = $counter;
  258. }
  259. $now = $this->functionsController->now();
  260. $nowStr = $now->toDateTimeString();
  261. $actions = DB::getQueryLog();
  262. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  263. $idac = $this->functionsController->registerActivity(
  264. $line,
  265. 'S002V01M14PCSA',
  266. 'S002V01F04MIIG',
  267. 'S002V01P01PCIN',
  268. 'Consulta',
  269. "El usuario $name (" . $usr->USUA_IDUS . ") consultó los indicadores disponibles.",
  270. $idUser,
  271. $nowStr,
  272. );
  273. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $line);
  274. return $this->responseController->makeResponse(false, 'EXITO.', $counters);
  275. }
  276. public function getPanel($idPanel, $idUser, $line) {
  277. DB::enableQueryLog();
  278. $idUser = $this->encryptionController->decrypt($idUser);
  279. if(!$idUser){
  280. return $this->responseController->makeResponse(true, 'El ID del usuario que realizó la solicitud no está encriptado correctamente', [], 400);
  281. }
  282. $usr = DB::table('S002V01TUSUA')->where([
  283. ['USUA_NULI', '=', $line],
  284. ['USUA_IDUS', '=', $idUser],
  285. ])->first();
  286. if(is_null($usr)){
  287. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado', [], 404);
  288. }
  289. $idPanel = $this->encryptionController->decrypt($idPanel);
  290. if(!$idPanel){
  291. return $this->responseController->makeResponse(true, 'El ID del panel solicitado no está encriptado correctamente', [], 400);
  292. }
  293. $panel = DB::table('S002V01TPACO')->select([
  294. 'PACO_IDPC AS ID_PANEL',
  295. 'PACO_NPCO AS NOMBRE_PANEL',
  296. 'PACO_INRE AS INDICADORES_RELACIONADOS',
  297. 'PACO_ORPA AS ORIENTACION',
  298. 'PACO_ESTA AS ESTADO',
  299. 'PACO_USRE AS USRREG',
  300. 'PACO_FERE AS FECREG',
  301. 'PACO_USMO AS USRMOD',
  302. 'PACO_FEMO AS FECMOD'
  303. ])->where([
  304. ['PACO_NULI', '=', $line],
  305. ['PACO_IDPC', '=', $idPanel]
  306. ])->first();
  307. $panel->ID_PANEL = $this->encryptionController->encrypt($panel->ID_PANEL);
  308. $usrReg = DB::table('S002V01TUSUA')->where([
  309. ['USUA_NULI', '=', $line],
  310. ['USUA_IDUS', '=', $panel->USRREG]
  311. ])->first();
  312. $usrRegName = $this->functionsController->joinName($usrReg->USUA_NOMB, $usrReg->USUA_APPA, $usrReg->USUA_APMA);
  313. $panel->USRREG = $usrRegName . " (" . $panel->USRREG . ")";
  314. if(!is_null($panel->USRMOD)){
  315. $usrMod = DB::table('S002V01TUSUA')->where([
  316. ['USUA_NULI', '=', $line],
  317. ['USUA_IDUS', '=', $panel->USRMOD]
  318. ])->first();
  319. $usrModName = $this->functionsController->joinName($usrMod->USUA_NOMB, $usrMod->USUA_APPA, $usrMod->USUA_APMA);
  320. $panel->USRMOD = $usrModName . " (" . $panel->USRMOD . ")";
  321. }
  322. $panelElementsArr = json_decode($panel->INDICADORES_RELACIONADOS, true);
  323. foreach($panelElementsArr as $key=>$element){
  324. $element['module'] = $this->encryptionController->encrypt($element['module']);
  325. $element['idElement'] = $this->encryptionController->encrypt($element['idElement']);
  326. $panelElementsArr[$key] = $element;
  327. }
  328. $panel->INDICADORES_RELACIONADOS = json_encode($panelElementsArr);
  329. $now = $this->functionsController->now();
  330. $nowStr = $now->toDateTimeString();
  331. $actions = DB::getQueryLog();
  332. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  333. $idac = $this->functionsController->registerActivity(
  334. $line,
  335. 'S002V01M14PCSA',
  336. 'S002V01F04MIIG',
  337. 'S002V01P01PCIN',
  338. 'Consulta',
  339. "El usuario $name (" . $usr->USUA_IDUS . ") consultó el panel de control #$idPanel.",
  340. $idUser,
  341. $nowStr,
  342. );
  343. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $line);
  344. return $this->responseController->makeResponse(false, 'EXITO.', $panel);
  345. }
  346. public function updatePanelElements(Request $request) {
  347. DB::enableQueryLog();
  348. $validator = Validator::make($request->all(), [
  349. 'id_user' => 'required|string',
  350. 'linea' => 'required|integer',
  351. 'id_panel' => 'required|string',
  352. 'elements' => 'required|json',
  353. ]);
  354. if($validator->fails()){
  355. return $this->responseController->makeResponse(
  356. true,
  357. "Se encontraron uno o más errores.",
  358. $this->responseController->makeErrors(
  359. $validator->errors()->messages()
  360. ),
  361. 401
  362. );
  363. }
  364. $form = $request->all();
  365. $idUser = $this->encryptionController->decrypt($form['id_user']);
  366. if(!$idUser){
  367. return $this->responseController->makeResponse(true, 'El ID de usuario no fue encriptado correctamente.', [], 400);
  368. }
  369. $usr = DB::table('S002V01TUSUA')->where([
  370. ['USUA_NULI', '=', $form['linea']],
  371. ['USUA_IDUS', '=', $idUser]
  372. ])->first();
  373. if(is_null($usr)){
  374. return $this->responseController->makeResponse(true, 'El usuario que realizó la petición no existe.', [], 404);
  375. }
  376. $idPanel = $this->encryptionController->decrypt($form['id_panel']);
  377. if(!$idPanel){
  378. return $this->responseController->makeResponse(true, 'El ID del panel que desea acualizar no fue encriptado correctamente.', [], 400);
  379. }
  380. $panel = DB::table('S002V01TPACO')->where([
  381. ['PACO_NULI', '=', $form['linea']],
  382. ['PACO_IDPC', '=', $idPanel]
  383. ])->first();
  384. if(is_null($panel)){
  385. return $this->responseController->makeResponse(true, 'El panel que desea actualizar no existe.', [], 404);
  386. }
  387. $elementsArr = json_decode($form['elements'], true);
  388. foreach($elementsArr as $key=>$element){
  389. if(!isset($element['module']) || !isset($element['idElement'])){
  390. return $this->responseController->makeResponse(true, "El elemento en la posición $key tiene un formato inválido.", [], 400);
  391. }
  392. $element['module'] = $this->encryptionController->decrypt($element['module']);
  393. if(!$element['module']){
  394. return $this->responseController->makeResponse(true, "El módulo del elemento en la posición $key no fue encriptado correctamente.", [], 400);
  395. }
  396. $element['idElement'] = $this->encryptionController->decrypt($element['idElement']);
  397. if(!$element['idElement']){
  398. return $this->responseController->makeResponse(true, "El ID del elemento en la posición $key no fue encriptado correctamente.", [], 400);
  399. }
  400. $elementsArr[$key] = $element;
  401. }
  402. $elementsStr = json_encode($elementsArr);
  403. $now = $this->functionsController->now();
  404. $nowStr = $now->toDateTimeString();
  405. DB::table('S002V01TPACO')->where([
  406. ['PACO_NULI', '=', $form['linea']],
  407. ['PACO_IDPC', '=', $idPanel]
  408. ])->update([
  409. 'PACO_INRE' => $elementsStr,
  410. 'PACO_USMO' => $idUser,
  411. 'PACO_FEMO' => $nowStr,
  412. ]);
  413. $actions = DB::getQueryLog();
  414. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  415. $idac = $this->functionsController->registerActivity(
  416. $form['linea'],
  417. 'S002V01M14PCSA',
  418. 'S002V01F04MIIG',
  419. 'S002V01P01PCIN',
  420. 'Actualización',
  421. "El usuario $name (" . $usr->USUA_IDUS . ") actualizó los elementos del panel de control #$idPanel.",
  422. $idUser,
  423. $nowStr,
  424. );
  425. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $form['linea']);
  426. return $this->responseController->makeResponse(false, 'EXITO.');
  427. }
  428. public function updatePanelAssociations(Request $request) {
  429. DB::enableQueryLog();
  430. $validator = Validator::make($request->all(), [
  431. 'id_user' => 'required|string',
  432. 'linea' => 'required|integer',
  433. 'id_panel' => 'required|string',
  434. 'associated_users' => 'required|json',
  435. ]);
  436. if($validator->fails()){
  437. return $this->responseController->makeResponse(
  438. true,
  439. "Se encontraron uno o más errores.",
  440. $this->responseController->makeErrors(
  441. $validator->errors()->messages()
  442. ),
  443. 401
  444. );
  445. }
  446. $form = $request->all();
  447. $idUser = $this->encryptionController->decrypt($form['id_user']);
  448. if(!$idUser){
  449. return $this->responseController->makeResponse(true, 'El ID de usuario no fue encriptado correctamente.', [], 400);
  450. }
  451. $usr = DB::table('S002V01TUSUA')->where([
  452. ['USUA_NULI', '=', $form['linea']],
  453. ['USUA_IDUS', '=', $idUser]
  454. ])->first();
  455. if(is_null($usr)){
  456. return $this->responseController->makeResponse(true, 'El usuario que realizó la petición no existe.', [], 404);
  457. }
  458. $idPanel = $this->encryptionController->decrypt($form['id_panel']);
  459. if(!$idPanel){
  460. return $this->responseController->makeResponse(true, 'El ID del panel que desea acualizar no fue encriptado correctamente.', [], 400);
  461. }
  462. $panel = DB::table('S002V01TPACO')->where([
  463. ['PACO_NULI', '=', $form['linea']],
  464. ['PACO_IDPC', '=', $idPanel]
  465. ])->first();
  466. if(is_null($panel)){
  467. return $this->responseController->makeResponse(true, 'El panel que desea actualizar no existe.', [], 404);
  468. }
  469. $now = $this->functionsController->now();
  470. $nowStr = $now->toDateTimeString();
  471. $associatedUsersArr = json_decode($form['associated_users'], true);
  472. $associatedUsersArrDec = [];
  473. //Actualizar las asociaciones hechas en el front
  474. foreach($associatedUsersArr as $key=>$idAssociatedUser){
  475. $idAssociatedUserDec = $this->encryptionController->decrypt($idAssociatedUser);
  476. if(!$idAssociatedUserDec){
  477. return $this->responseController->makeResponse(true, "El ID del usuario en la posición $key del arreglo de usuarios asociados no fue encriptado correctamente.", [], 400);
  478. }
  479. $associatedUsersArrDec[] = $idAssociatedUserDec;
  480. $associatedUser = DB::table('S002V01TUSUA')->where([
  481. ['USUA_NULI', '=', $form['linea']],
  482. ['USUA_IDUS', '=', $idAssociatedUserDec]
  483. ])->first();
  484. if(is_null($associatedUser)){
  485. return $this->responseController->makeResponse(true, "El ID del usuario en la posición $key del arreglo de usuarios asociados no existe.", [], 404);
  486. }
  487. DB::table('S002V01TUSUA')->where([
  488. ['USUA_NULI', '=', $form['linea']],
  489. ['USUA_IDUS', '=', $idAssociatedUserDec]
  490. ])->update([
  491. 'USUA_PCRE' => $idPanel,
  492. 'USUA_USMO' => $idUser,
  493. 'USUA_FEMO' => $nowStr,
  494. ]);
  495. }
  496. //Actualizar las asociaciones de los usuarios en la tabla
  497. $users = DB::table('S002V01TUSUA')->where('USUA_NULI', '=', $form['linea'])->get()->all();
  498. foreach($users as $user){
  499. if(!in_array($user->USUA_IDUS, $associatedUsersArrDec)){
  500. if($user->USUA_PCRE == intval($idPanel)){
  501. DB::table('S002V01TUSUA')->where([
  502. ['USUA_NULI', '=', $form['linea']],
  503. ['USUA_IDUS', '=', $user->USUA_IDUS]
  504. ])->update([
  505. 'USUA_PCRE' => 0,
  506. 'USUA_USMO' => $idUser,
  507. 'USUA_FEMO' => $nowStr,
  508. ]);
  509. }
  510. }
  511. }
  512. $actions = DB::getQueryLog();
  513. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  514. $idac = $this->functionsController->registerActivity(
  515. $form['linea'],
  516. 'S002V01M14PCSA',
  517. 'S002V01F04MIIG',
  518. 'S002V01P01PCIN',
  519. 'Actualización',
  520. "El usuario $name (" . $usr->USUA_IDUS . ") actualizó los usuarios asociados al panel #$idPanel.",
  521. $idUser,
  522. $nowStr,
  523. );
  524. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $form['linea']);
  525. return $this->responseController->makeResponse(false, 'EXITO.');
  526. }
  527. public function deletePanel(Request $request) {
  528. DB::enableQueryLog();
  529. $validator = Validator::make($request->all(), [
  530. 'id_user' => 'required|string',
  531. 'linea' => 'required|integer',
  532. 'id_panel' => 'required|string',
  533. ]);
  534. if($validator->fails()){
  535. return $this->responseController->makeResponse(
  536. true,
  537. "Se encontraron uno o más errores.",
  538. $this->responseController->makeErrors(
  539. $validator->errors()->messages()
  540. ),
  541. 401
  542. );
  543. }
  544. $form = $request->all();
  545. $idUser = $this->encryptionController->decrypt($form['id_user']);
  546. if(!$idUser){
  547. return $this->responseController->makeResponse(true, 'El ID de usuario no fue encriptado correctamente.', [], 400);
  548. }
  549. $usr = DB::table('S002V01TUSUA')->where([
  550. ['USUA_NULI', '=', $form['linea']],
  551. ['USUA_IDUS', '=', $idUser]
  552. ])->first();
  553. if(is_null($usr)){
  554. return $this->responseController->makeResponse(true, 'El usuario que realizó la petición no existe.', [], 404);
  555. }
  556. $idPanel = $this->encryptionController->decrypt($form['id_panel']);
  557. if(!$idPanel){
  558. return $this->responseController->makeResponse(true, 'El ID del panel que desea acualizar no fue encriptado correctamente.', [], 400);
  559. }
  560. $panel = DB::table('S002V01TPACO')->where([
  561. ['PACO_NULI', '=', $form['linea']],
  562. ['PACO_IDPC', '=', $idPanel]
  563. ])->first();
  564. if(is_null($panel)){
  565. return $this->responseController->makeResponse(true, 'El panel que desea actualizar no existe.', [], 404);
  566. }
  567. $relatedUsers = DB::table('S002V01TUSUA')->where([
  568. ['USUA_NULI', '=', $form['linea']],
  569. ['USUA_PCRE', '=', $idPanel]
  570. ])->get()->all();
  571. if(count($relatedUsers) > 0){
  572. return $this->responseController->makeResponse(true, 'El panel de control no se peuede eliminar porque tiene usuarios relacionados.', [], 401);
  573. }
  574. $now = $this->functionsController->now();
  575. $nowStr = $now->toDateTimeString();
  576. DB::table('S002V01TPACO')->where([
  577. ['PACO_NULI', '=', $form['linea']],
  578. ['PACO_IDPC', '=', $idPanel]
  579. ])->update([
  580. 'PACO_ESTA' => 'Eliminado',
  581. 'PACO_USMO' => $idUser,
  582. 'PACO_FEMO' => $nowStr
  583. ]);
  584. $actions = DB::getQueryLog();
  585. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  586. $idac = $this->functionsController->registerActivity(
  587. $form['linea'],
  588. 'S002V01M14PCSA',
  589. 'S002V01F04MIIG',
  590. 'S002V01P01PCIN',
  591. 'Actualización',
  592. "El usuario $name (" . $usr->USUA_IDUS . ") actualizó el panel de control #$idPanel.",
  593. $idUser,
  594. $nowStr,
  595. );
  596. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $form['linea']);
  597. return $this->responseController->makeResponse(false, 'EXITO.');
  598. }
  599. public function getInfoToExcel($indicator, $idModule, $idUser, $line) {
  600. DB::enableQueryLog();
  601. $idUser = $this->encryptionController->decrypt($idUser);
  602. if(!$idUser){
  603. return $this->responseController->makeResponse(true, 'El ID del usuario que realizó la solicitud no está encriptado correctamente', [], 400);
  604. }
  605. $usr = DB::table('S002V01TUSUA')->where([
  606. ['USUA_NULI', '=', $line],
  607. ['USUA_IDUS', '=', $idUser],
  608. ])->first();
  609. if(is_null($usr)){
  610. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado', [], 404);
  611. }
  612. $idModule = $this->encryptionController->decrypt($idModule);
  613. if(!$idModule){
  614. return $this->responseController->makeResponse(true, 'El ID del módulo seleccionado no está encriptado correctamente', [], 400);
  615. }
  616. $module = DB::table('S002V01TMODU')->where([
  617. ['MODU_NULI', '=', $line],
  618. ['MODU_IDMO', '=', $idModule]
  619. ])->first();
  620. if(is_null($module)){
  621. return $this->responseController->makeResponse(true, 'El módulo relacionado no está registrado', [], 404);
  622. }
  623. $indicator = $this->encryptionController->decrypt($indicator);
  624. if(!$indicator){
  625. return $this->responseController->makeResponse(true, 'El ID del indicador seleccionado no está encriptado correctamente', [], 400);
  626. }
  627. $spreadsheet = null;
  628. switch($idModule){
  629. case "S002V01M06COAC":
  630. $activator = DB::table('S002V01TACTI')->where([
  631. ['ACTI_NULI', '=', $line],
  632. ['ACTI_CORE', '=', $indicator]
  633. ])->first();
  634. $unitStr = "";
  635. if($activator->ACTI_TIAC == 'Medida' || $activator == 'Valor'){
  636. $configArr = json_decode($activator->ACTI_COAC, true);
  637. $unit = DB::table('S002V01TLIME')->where([
  638. ['LIME_NULI', '=', $line],
  639. ['LIME_IDME', '=', $configArr['unit']],
  640. ['LIME_MAGN', '=', $configArr['magnitude']],
  641. ])->first();
  642. $unitStr = $unit->LIME_ACME;
  643. }else if($activator->ACTI_TIAC == 'Sintoma'){
  644. $configArr = json_decode($activator->ACTI_COAC, true);
  645. $sign = DB::table('S002V01TLISI')->where([
  646. ['LISI_NULI', '=', $line],
  647. ['LISI_IDSI', '=', $configArr['sign']]
  648. ])->first();
  649. $unit = DB::table('S002V01TLIME')->where([
  650. ['LIME_NULI', '=', $line],
  651. ['LIME_IDME', '=', $sign->LISI_IDME],
  652. ])->first();
  653. $unitStr = $unit->LIME_ACME;
  654. }
  655. $measures = DB::table('S002V01TMEDI')->select([
  656. 'MEDI_IDME AS ID_MEDIDA',
  657. 'MEDI_CORE AS CONTADOR',
  658. 'MEDI_VALO AS VALOR',
  659. DB::raw("CONCAT(LSWE_URLX, ' (', MEDI_WSRE, ')') AS SERVICIO_WEB"),
  660. 'MEDI_HORE AS HORA_REGISTRO',
  661. ])->join('S002V01TLSWE', 'LSWE_IDSW', '=', 'MEDI_WSRE')->where([
  662. ['MEDI_NULI', '=', $line],
  663. ['MEDI_CORE', '=', $indicator]
  664. ])->orderBy('MEDI_IDME', 'desc')->get()->all();
  665. foreach($measures as $key=>$measure){
  666. $measure->VALOR = trim($measure->VALOR . " $unitStr");
  667. $measure->HORA_REGISTRO = $this->functionsController->buildProjectDate($measure->HORA_REGISTRO);
  668. $measures[$key] = $measure;
  669. }
  670. $columns = ['ID', 'CONTADOR', 'VALOR', 'WEB SERVICE', 'REGISTRO'];
  671. $spreadsheet = $this->exportExcel($measures, $columns);
  672. break;
  673. }
  674. if(is_null($spreadsheet)){
  675. return $this->responseController->makeResponse(true, 'La hoja de cálculo no pudo ser generada', [], 400);
  676. }
  677. $now = $this->functionsController->now();
  678. $nowStr = $now->toDateTimeString();
  679. $dateTimeArr = explode(" ", $nowStr);
  680. $dateArr = explode("-", $dateTimeArr[0]);
  681. $year = substr($dateArr[0], 2);
  682. $como = 'PCSA';
  683. $cldo = 'IN';
  684. $fecr = $year . $dateArr[1] . $dateArr[2];
  685. $sec = DB::table('S002V01TAFAL')->where([
  686. ['AFAL_NULI', '=', $line],
  687. ['AFAL_COMO', '=', $como],
  688. ['AFAL_CLDO', '=', $cldo],
  689. ])->orderBy('AFAL_NUSE', 'desc')->first();
  690. $nuse = "";
  691. if(is_null($sec)){
  692. $nuse = '000001';
  693. }else{
  694. $secu = "" . intval($sec->AFAL_NUSE) + 1 . "";
  695. $nuse = "";
  696. for($i = strlen($secu); $i < 6; $i++){
  697. $nuse .= "0";
  698. }
  699. $nuse = $nuse . $secu;
  700. }
  701. $timestamp = $now->timestamp;
  702. $noar = "informe_panel_control_$timestamp";
  703. $exte = "xlsx";
  704. $ver = DB::table('S002V01TAFAL')->where([
  705. ['AFAL_NULI', '=', $line],
  706. ['AFAL_COMO', '=', $como],
  707. ['AFAL_CLDO', '=', $cldo],
  708. ['AFAL_NOAR', '=', $noar],
  709. ['AFAL_EXTE', '=', $exte],
  710. ])->orderBy('AFAL_NUVE', 'desc')->first();
  711. $nuve = "";
  712. if(is_null($ver)){
  713. $nuve = "01";
  714. }else{
  715. $vers = intval($ver->AFAL_NUVE) + 1;
  716. $nuve = $vers < 10 ? "0$vers" : "$vers";
  717. }
  718. $line = $line < 10 ? "0$line" : "$line";
  719. $filePath = 'C:\inetpub\wwwroot\sam\public_files\\';
  720. $fileName = "$line-$como-$cldo-$fecr-$nuse=$nuve=$noar.$exte";
  721. $tempFile = $filePath . $fileName;
  722. if(file_exists($tempFile)){
  723. unlink($tempFile);
  724. }
  725. $writer = IOFactory::createWriter($spreadsheet, 'Xlsx');
  726. $writer->save($tempFile);
  727. $ubic = Storage::putFile('files', new File($tempFile));
  728. $ubic = str_replace("/", "\\", $ubic);
  729. $ubic = "C:\inetpub\wwwroot\sam\storage\app\\" . $ubic;
  730. $tama = filesize($ubic);
  731. $usac = json_encode([$idUser]);
  732. unlink($tempFile);
  733. DB::table('S002V01TAFAL')->insert([
  734. 'AFAL_NULI' => $line,
  735. 'AFAL_COMO' => $como,
  736. 'AFAL_CLDO' => $cldo,
  737. 'AFAL_FECR' => $fecr,
  738. 'AFAL_NUSE' => $nuse,
  739. 'AFAL_NUVE' => $nuve,
  740. 'AFAL_NOAR' => $noar,
  741. 'AFAL_EXTE' => $exte,
  742. 'AFAL_TAMA' => $tama,
  743. 'AFAL_UBIC' => $ubic,
  744. 'AFAL_USAC' => $usac,
  745. 'AFAL_USRE' => $idUser,
  746. 'AFAL_FERE' => $nowStr,
  747. ]);
  748. $actions = DB::getQueryLog();
  749. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  750. $idac = $this->functionsController->registerActivity(
  751. $line,
  752. 'S002V01M14PCSA',
  753. 'S002V01F03EDJC',
  754. '-',
  755. 'Registro',
  756. "El usuario $name (" . $usr->USUA_IDUS . ") registró el archivo #$fileName.",
  757. $idUser,
  758. $nowStr,
  759. );
  760. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $line);
  761. return $this->responseController->makeResponse(false, 'EXITO.', ['report' => $this->encryptionController->encrypt($fileName)]);
  762. }
  763. private function exportExcel($data, $columns) : Spreadsheet {
  764. $spreadsheet = new Spreadsheet;
  765. $spreadsheet->getProperties()->setCreator('STC');
  766. $worksheet = $spreadsheet->getActiveSheet();
  767. $startRow = 2;
  768. $startColumn = 2;
  769. $endRow = count($data) + $startRow;
  770. $endColumn = count($columns) + $startColumn;
  771. for($row = $startRow; $row <= $endRow; $row++){
  772. $rowIndex = $row - 2;
  773. $rowInfo = $rowIndex == 0 ? $columns : (array) $data[$rowIndex - 1];
  774. $rowHeaders = array_keys($rowInfo);
  775. for($col = $startColumn; $col < $endColumn; $col++){
  776. $colIndex = $col - 2;
  777. $colName = $rowHeaders[$colIndex];
  778. $colValue = $rowInfo[$colName];
  779. $colStr = Coordinate::stringFromColumnIndex($col);
  780. if($rowIndex == 0){
  781. $worksheet->setCellValue($colStr . $row, $colValue)->getStyle($colStr . $row)->getFill()
  782. ->setFillType(Fill::FILL_SOLID)
  783. ->getStartColor()->setRGB('B7BCC4');
  784. $worksheet->getStyle($colStr . $row)->getFont()->setBold(true);
  785. $worksheet->getStyle($colStr . $row)->getAlignment()->setHorizontal(Alignment::HORIZONTAL_CENTER);
  786. }else{
  787. $worksheet->setCellValue($colStr . $row, $colValue);
  788. }
  789. $worksheet->getColumnDimension($colStr)->setAutoSize(true);
  790. }
  791. }
  792. return $spreadsheet;
  793. }
  794. public function registerBroadcastList(Request $request) {
  795. DB::enableQueryLog();
  796. $validator = Validator::make($request->all(), [
  797. 'id_user' => 'required|string',
  798. 'linea' => 'required|integer',
  799. 'list_name' => 'required|string|max:75',
  800. 'related_users' => 'required|json'
  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. $form = $request->all();
  813. $idUser = $this->encryptionController->decrypt($form['id_user']);
  814. if(!$idUser){
  815. return $this->responseController->makeResponse(true, 'El ID de usuario no fue encriptado correctamente.', [], 400);
  816. }
  817. $usr = DB::table('S002V01TUSUA')->where([
  818. ['USUA_NULI', '=', $form['linea']],
  819. ['USUA_IDUS', '=', $idUser]
  820. ])->first();
  821. if(is_null($usr)){
  822. return $this->responseController->makeResponse(true, 'El usuario que realizó la petición no existe.', [], 404);
  823. }
  824. $relatedUsers = json_decode($form['related_users'], true);
  825. if(count($relatedUsers) < 2){
  826. return $this->responseController->makeResponse(true, 'La lista de difusión debe tener al menos 2 usuarios relacionados.', [], 400);
  827. }
  828. foreach($relatedUsers as $key=>$user){
  829. $user = $this->encryptionController->decrypt($user);
  830. if(!$user){
  831. return $this->responseController->makeResponse(true, "El ID del usuario en la posición $key del arreglo de integrantes no fue encriptado correctamente.", [], 400);
  832. }
  833. $userInfo = DB::table('S002V01TUSUA')->where([
  834. ['USUA_NULI', '=', $form['linea']],
  835. ['USUA_IDUS', '=', $user]
  836. ])->first();
  837. if(is_null($userInfo)){
  838. return $this->responseController->makeResponse(true, "El usuario en la posición $key no existe.", [], 404);
  839. }else if($userInfo->USUA_ESTA == 'Eliminado'){
  840. return $this->responseController->makeResponse(true, "El usuario en la posición $key está eliminado.", [], 404);
  841. }
  842. $relatedUsers[$key] = $user;
  843. }
  844. $relatedUsersStr = json_encode($relatedUsers);
  845. $now = $this->functionsController->now();
  846. $nowStr = $now->toDateTimeString();
  847. $idList = DB::table('S002V01TLIDI')->insertGetId([
  848. 'LIDI_NULI' => $form['linea'],
  849. 'LIDI_NLDI' => $form['list_name'],
  850. 'LIDI_INLI' => $relatedUsersStr,
  851. 'LIDI_USRE' => $idUser,
  852. 'LIDI_FERE' => $nowStr
  853. ]);
  854. $actions = DB::getQueryLog();
  855. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  856. $idac = $this->functionsController->registerActivity(
  857. $form['linea'],
  858. 'S002V01M14PCSA',
  859. 'S002V01F01CLDI',
  860. 'S002V01P01ASPE',
  861. 'Registro',
  862. "El usuario $name (" . $usr->USUA_IDUS . ") registró la lista de difusión #$idList.",
  863. $idUser,
  864. $nowStr,
  865. );
  866. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $form['linea']);
  867. return $this->responseController->makeResponse(false, 'EXITO.');
  868. }
  869. public function getBroadcastLists($idUser, $line) {
  870. DB::enableQueryLog();
  871. $idUser = $this->encryptionController->decrypt($idUser);
  872. if(!$idUser){
  873. return $this->responseController->makeResponse(true, 'El ID del usuario que realizó la solicitud no está encriptado correctamente', [], 400);
  874. }
  875. $usr = DB::table('S002V01TUSUA')->where([
  876. ['USUA_NULI', '=', $line],
  877. ['USUA_IDUS', '=', $idUser],
  878. ])->first();
  879. if(is_null($usr)){
  880. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado', [], 404);
  881. }
  882. $lists = DB::table('S002V01TLIDI')->select([
  883. 'LIDI_IDLD AS ID_LISTA',
  884. 'LIDI_NLDI AS NOMBRE_LISTA',
  885. 'LIDI_ESTA AS ESTADO',
  886. 'LIDI_USRE AS USRREG',
  887. 'LIDI_FERE AS FECREG',
  888. 'LIDI_USMO AS USRMOD',
  889. 'LIDI_FEMO AS FECMOD'
  890. ])->where('LIDI_NULI', '=', $line)->get()->all();
  891. foreach($lists as $key=>$list){
  892. $list->ID_LISTA = $this->encryptionController->encrypt($list->ID_LISTA);
  893. $usrReg = DB::table('S002V01TUSUA')->where([
  894. ['USUA_NULI', '=', $line],
  895. ['USUA_IDUS', '=', $list->USRREG]
  896. ])->first();
  897. $usrRegName = $this->functionsController->joinName($usrReg->USUA_NOMB, $usrReg->USUA_APPA, $usrReg->USUA_APMA);
  898. $list->USRREG = $usrRegName . ' (' . $list->USRREG . ')';
  899. if(!is_null($list->USRMOD)){
  900. $usrMod = DB::table('S002V01TUSUA')->where([
  901. ['USUA_NULI', '=', $line],
  902. ['USUA_IDUS', '=', $list->USRMOD]
  903. ])->first();
  904. $usrModName = $this->functionsController->joinName($usrMod->USUA_NOMB, $usrMod->USUA_APPA, $usrMod->USUA_APMA);
  905. $list->USRMOD = $usrModName . ' (' . $list->USRMOD . ')';
  906. }
  907. $lists[$key] = $list;
  908. }
  909. $now = $this->functionsController->now();
  910. $nowStr = $now->toDateTimeString();
  911. $actions = DB::getQueryLog();
  912. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  913. $idac = $this->functionsController->registerActivity(
  914. $line,
  915. 'S002V01M14PCSA',
  916. 'S002V01F01CLDI',
  917. 'S002V01P01ASPE',
  918. 'Consulta',
  919. "El usuario $name (" . $usr->USUA_IDUS . ") consultó las listas de difusión registradas.",
  920. $idUser,
  921. $nowStr,
  922. );
  923. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $line);
  924. return $this->responseController->makeResponse(false, 'EXITO.', $lists);
  925. }
  926. public function getBroadcastList($idList, $idUser, $line) {
  927. DB::enableQueryLog();
  928. $idUser = $this->encryptionController->decrypt($idUser);
  929. if(!$idUser){
  930. return $this->responseController->makeResponse(true, 'El ID del usuario que realizó la solicitud no está encriptado correctamente', [], 400);
  931. }
  932. $usr = DB::table('S002V01TUSUA')->where([
  933. ['USUA_NULI', '=', $line],
  934. ['USUA_IDUS', '=', $idUser],
  935. ])->first();
  936. if(is_null($usr)){
  937. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado', [], 404);
  938. }
  939. $idList = $this->encryptionController->decrypt($idList);
  940. if(!$idList){
  941. return $this->responseController->makeResponse(true, 'El ID de la lista solicitada no está encriptado correctamente', [], 400);
  942. }
  943. $broadcastList = DB::table('S002V01TLIDI')->select([
  944. 'LIDI_IDLD AS ID_LISTA',
  945. 'LIDI_NLDI AS NOMBRE_LISTA',
  946. 'LIDI_INLI AS INTEGRANTES',
  947. 'LIDI_ESTA AS ESTADO',
  948. 'LIDI_USRE AS USRREG',
  949. 'LIDI_FERE AS FECREG',
  950. 'LIDI_USMO AS USRMOD',
  951. 'LIDI_FEMO AS FECMOD'
  952. ])->where([
  953. ['LIDI_NULI', '=', $line],
  954. ['LIDI_IDLD', '=', $idList]
  955. ])->first();
  956. if(is_null($broadcastList)){
  957. return $this->responseController->makeResponse(true, 'La lista solicitada no existe.', [], 404);
  958. }else if($broadcastList->ESTADO == 'Eliminado'){
  959. return $this->responseController->makeResponse(true, 'La lista solicitada está eliminada.', [], 404);
  960. }
  961. $broadcastList->ID_LISTA = $this->encryptionController->encrypt($broadcastList->ID_LISTA);
  962. $usersList = json_decode($broadcastList->INTEGRANTES, true);
  963. $usersListFn = [];
  964. foreach($usersList as $key=>$userID){
  965. $user = DB::table('S002V01TUSUA')
  966. ->join('S002V01TPERF', 'PERF_IDPE', '=', 'USUA_PERF')->where([
  967. ['USUA_NULI', '=', $line],
  968. ['USUA_IDUS', '=', $userID]
  969. ])->first();
  970. $usersListFn[] = [
  971. 'ID_USUARIO' => $this->encryptionController->encrypt($user->USUA_IDUS),
  972. 'NOMBRE_USUARIO' => $this->functionsController->joinName($user->USUA_NOMB, $user->USUA_APPA, $user->USUA_APMA),
  973. 'ID_PERFIL' => $this->encryptionController->encrypt($user->PERF_IDPE),
  974. 'NOMBRE_PERFIL' => $user->PERF_NOPE
  975. ];
  976. }
  977. $broadcastList->INTEGRANTES = json_encode($usersListFn);
  978. $usrReg = DB::table('S002V01TUSUA')->where([
  979. ['USUA_NULI', '=', $line],
  980. ['USUA_IDUS', '=', $broadcastList->USRREG]
  981. ])->first();
  982. $usrRegName = $this->functionsController->joinName($usrReg->USUA_NOMB, $usrReg->USUA_APPA, $usrReg->USUA_APMA);
  983. $broadcastList->USRREG = $usrRegName . ' (' . $broadcastList->USRREG . ')';
  984. if(!is_null($broadcastList->USRMOD)){
  985. $usrMod = DB::table('S002V01TUSUA')->where([
  986. ['USUA_NULI', '=', $line],
  987. ['USUA_IDUS', '=', $broadcastList->USRMOD]
  988. ])->first();
  989. $usrModName = $this->functionsController->joinName($usrMod->USUA_NOMB, $usrMod->USUA_APPA, $usrMod->USUA_APMA);
  990. $broadcastList->USRMOD = $usrModName . ' (' . $broadcastList->USRMOD . ')';
  991. }
  992. $now = $this->functionsController->now();
  993. $nowStr = $now->toDateTimeString();
  994. $actions = DB::getQueryLog();
  995. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  996. $idac = $this->functionsController->registerActivity(
  997. $line,
  998. 'S002V01M14PCSA',
  999. 'S002V01F01CLDI',
  1000. 'S002V01P01ASPE',
  1001. 'Consulta',
  1002. "El usuario $name (" . $usr->USUA_IDUS . ") consultó la lista de difusión #$idList.",
  1003. $idUser,
  1004. $nowStr,
  1005. );
  1006. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $line);
  1007. return $this->responseController->makeResponse(false, 'EXITO.', $broadcastList);
  1008. }
  1009. public function updateBroadcastList(Request $request) {
  1010. DB::enableQueryLog();
  1011. $validator = Validator::make($request->all(), [
  1012. 'id_user' => 'required|string',
  1013. 'linea' => 'required|integer',
  1014. 'list_name' => 'required|string|max:75',
  1015. 'related_users' => 'required|json',
  1016. 'id_list' => 'required|string',
  1017. ]);
  1018. if($validator->fails()){
  1019. return $this->responseController->makeResponse(
  1020. true,
  1021. "Se encontraron uno o más errores.",
  1022. $this->responseController->makeErrors(
  1023. $validator->errors()->messages()
  1024. ),
  1025. 401
  1026. );
  1027. }
  1028. $form = $request->all();
  1029. $idUser = $this->encryptionController->decrypt($form['id_user']);
  1030. if(!$idUser){
  1031. return $this->responseController->makeResponse(true, 'El ID de usuario no fue encriptado correctamente.', [], 400);
  1032. }
  1033. $usr = DB::table('S002V01TUSUA')->where([
  1034. ['USUA_NULI', '=', $form['linea']],
  1035. ['USUA_IDUS', '=', $idUser]
  1036. ])->first();
  1037. if(is_null($usr)){
  1038. return $this->responseController->makeResponse(true, 'El usuario que realizó la petición no existe.', [], 404);
  1039. }
  1040. $idList = $this->encryptionController->decrypt($form['id_list']);
  1041. if(!$idList){
  1042. return $this->responseController->makeResponse(true, 'El ID de la lista solicitada no está encriptado correctamente', [], 400);
  1043. }
  1044. $list = DB::table('S002V01TLIDI')->where([
  1045. ['LIDI_NULI', '=', $form['linea']],
  1046. ['LIDI_IDLD', '=', $idList]
  1047. ])->first();
  1048. if(is_null($list)){
  1049. return $this->responseController->makeResponse(true, 'La lista de difusión solicitada no existe.', [], 404);
  1050. }
  1051. $relatedUsers = json_decode($form['related_users'], true);
  1052. if(count($relatedUsers) < 2){
  1053. return $this->responseController->makeResponse(true, 'La lista de difusión debe tener al menos 2 usuarios relacionados.', [], 400);
  1054. }
  1055. foreach($relatedUsers as $key=>$user){
  1056. $user = $this->encryptionController->decrypt($user);
  1057. if(!$user){
  1058. return $this->responseController->makeResponse(true, "El ID del usuario en la posición $key del arreglo de integrantes no fue encriptado correctamente.", [], 400);
  1059. }
  1060. $userInfo = DB::table('S002V01TUSUA')->where([
  1061. ['USUA_NULI', '=', $form['linea']],
  1062. ['USUA_IDUS', '=', $user]
  1063. ])->first();
  1064. if(is_null($userInfo)){
  1065. return $this->responseController->makeResponse(true, "El usuario en la posición $key no existe.", [], 404);
  1066. }else if($userInfo->USUA_ESTA == 'Eliminado'){
  1067. return $this->responseController->makeResponse(true, "El usuario en la posición $key está eliminado.", [], 404);
  1068. }
  1069. $relatedUsers[$key] = $user;
  1070. }
  1071. $relatedUsersStr = json_encode($relatedUsers);
  1072. $now = $this->functionsController->now();
  1073. $nowStr = $now->toDateTimeString();
  1074. DB::table('S002V01TLIDI')->where([
  1075. ['LIDI_NULI', '=', $form['linea']],
  1076. ['LIDI_IDLD', '=', $idList]
  1077. ])->update([
  1078. 'LIDI_NLDI' => $form['list_name'],
  1079. 'LIDI_INLI' => $relatedUsersStr,
  1080. 'LIDI_USMO' => $idUser,
  1081. 'LIDI_FEMO' => $nowStr
  1082. ]);
  1083. $actions = DB::getQueryLog();
  1084. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  1085. $idac = $this->functionsController->registerActivity(
  1086. $form['linea'],
  1087. 'S002V01M14PCSA',
  1088. 'S002V01F01CLDI',
  1089. 'S002V01P01ASPE',
  1090. 'Actualización',
  1091. "El usuario $name (" . $usr->USUA_IDUS . ") actualizó la lista de difusión #$idList.",
  1092. $idUser,
  1093. $nowStr,
  1094. );
  1095. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $form['linea']);
  1096. return $this->responseController->makeResponse(false, 'EXITO.');
  1097. }
  1098. public function deleteBroadcastList(Request $request) {
  1099. DB::enableQueryLog();
  1100. $validator = Validator::make($request->all(), [
  1101. 'id_user' => 'required|string',
  1102. 'linea' => 'required|integer',
  1103. 'id_list' => 'required|string',
  1104. ]);
  1105. if($validator->fails()){
  1106. return $this->responseController->makeResponse(
  1107. true,
  1108. "Se encontraron uno o más errores.",
  1109. $this->responseController->makeErrors(
  1110. $validator->errors()->messages()
  1111. ),
  1112. 401
  1113. );
  1114. }
  1115. $form = $request->all();
  1116. $idUser = $this->encryptionController->decrypt($form['id_user']);
  1117. if(!$idUser){
  1118. return $this->responseController->makeResponse(true, 'El ID de usuario no fue encriptado correctamente.', [], 400);
  1119. }
  1120. $usr = DB::table('S002V01TUSUA')->where([
  1121. ['USUA_NULI', '=', $form['linea']],
  1122. ['USUA_IDUS', '=', $idUser]
  1123. ])->first();
  1124. if(is_null($usr)){
  1125. return $this->responseController->makeResponse(true, 'El usuario que realizó la petición no existe.', [], 404);
  1126. }
  1127. $idList = $this->encryptionController->decrypt($form['id_list']);
  1128. if(!$idList){
  1129. return $this->responseController->makeResponse(true, 'El ID de la lista solicitada no está encriptado correctamente', [], 400);
  1130. }
  1131. $list = DB::table('S002V01TLIDI')->where([
  1132. ['LIDI_NULI', '=', $form['linea']],
  1133. ['LIDI_IDLD', '=', $idList]
  1134. ])->first();
  1135. if(is_null($list)){
  1136. return $this->responseController->makeResponse(true, 'La lista de difusión solicitada no existe.', [], 404);
  1137. }
  1138. $now = $this->functionsController->now();
  1139. $nowStr = $now->toDateTimeString();
  1140. DB::table('S002V01TLIDI')->where([
  1141. ['LIDI_NULI', '=', $form['linea']],
  1142. ['LIDI_IDLD', '=', $idList]
  1143. ])->update([
  1144. 'LIDI_ESTA' => 'Eliminado',
  1145. 'LIDI_USMO' => $idUser,
  1146. 'LIDI_FEMO' => $nowStr
  1147. ]);
  1148. $actions = DB::getQueryLog();
  1149. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  1150. $idac = $this->functionsController->registerActivity(
  1151. $form['linea'],
  1152. 'S002V01M14PCSA',
  1153. 'S002V01F01CLDI',
  1154. 'S002V01P01ASPE',
  1155. 'Eliminación',
  1156. "El usuario $name (" . $usr->USUA_IDUS . ") eliminó la lista de difusión #$idList.",
  1157. $idUser,
  1158. $nowStr,
  1159. );
  1160. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $form['linea']);
  1161. return $this->responseController->makeResponse(false, 'EXITO.');
  1162. }
  1163. }