EmployeeController.php 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278
  1. <?php
  2. namespace App\Http\Controllers;
  3. use Carbon\Carbon;
  4. use Illuminate\Http\Request;
  5. use Illuminate\Support\Facades\DB;
  6. use Illuminate\Support\Facades\Storage;
  7. use Illuminate\Support\Facades\Validator;
  8. use PhpOffice\PhpSpreadsheet\IOFactory;
  9. use Illuminate\Database\Query\Builder;
  10. use PhpOffice\PhpSpreadsheet\Spreadsheet;
  11. use stdClass;
  12. use Throwable;
  13. class EmployeeController extends Controller{
  14. private $responseController;
  15. private $encryptionController;
  16. private $documentManagementController;
  17. private $functionsController;
  18. public function __construct(){
  19. $this->responseController = new ResponseController();
  20. $this->encryptionController = new EncryptionController();
  21. $this->documentManagementController = new DocumentManagementController();
  22. $this->functionsController = new FunctionsController();
  23. }
  24. public function getConsultOfEmployees($idUser, $line) {
  25. DB::enableQueryLog();
  26. $idUser = $this->encryptionController->decrypt($idUser);
  27. if(!$idUser){
  28. return $this->responseController->makeResponse(true, 'El ID del usuario que realizó la solicitud no está encriptado correctamente', [], 400);
  29. }
  30. $usr = DB::table('S002V01TUSUA')->where([
  31. ['USUA_NULI', '=', $line],
  32. ['USUA_IDUS', '=', $idUser],
  33. ])->first();
  34. if(is_null($usr)){
  35. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado', [], 404);
  36. }
  37. $employees = DB::table('S002V01TPERS')->select([
  38. 'PERS_IDPE AS ID_EMPLOYEE',
  39. DB::raw('TRIM(CONCAT(USUA_NOMB, " " , USUA_APPA, " ", COALESCE(USUA_APMA,""))) as NAME'),
  40. 'PERS_TICO AS CONTRACT_TYPE',
  41. 'PERS_ESPE AS SPECIALITY',
  42. 'PERS_EQTR AS TEAM_ID',
  43. 'EQMA_NOMB AS TEAM_NAME',
  44. 'PERS_FERE AS REGISTER_DATE',
  45. 'PERS_USRE AS REGISTERED_BY_USER',
  46. 'PERS_FEMO AS UPDATE_DATE',
  47. 'PERS_USMO AS UPDATED_BY_USER',
  48. 'PERS_ESTA AS STATUS'
  49. ])->join('S002V01TUSUA', 'PERS_IDUS', '=', 'USUA_IDUS')
  50. ->leftJoin('S002V01TEQMA', 'PERS_EQTR', '=', 'EQMA_IDEQ')
  51. ->where('USUA_NULI', '=', $line)->get()->all();
  52. $now = $this->functionsController->now();
  53. $nowStr = $now->toDateTimeString();
  54. $actions = DB::getQueryLog();
  55. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  56. $idac = $this->functionsController->registerActivity(
  57. $line,
  58. 'S002V01M11GPRS',
  59. 'S002V01F01GEPE',
  60. 'S002V01P01COEM',
  61. 'Consulta',
  62. "El usuario $name (" . $usr->USUA_IDUS . ") consultó los empleados registrados.",
  63. $idUser,
  64. $nowStr,
  65. 'S002V01S02GEPE'
  66. );
  67. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $line);
  68. return $this->responseController->makeResponse(false, 'EXITO', $employees);
  69. }
  70. public function storeEmployee(Request $request) {
  71. DB::enableQueryLog();
  72. $request['RFC'] = $this->encryptionController->decrypt($request->RFC) ? $this->encryptionController->decrypt($request->RFC) : 'ENC_ERR';
  73. $request['TAX'] = $this->encryptionController->decrypt($request->TAX) ? $this->encryptionController->decrypt($request->TAX) : 'ENC_ERR';
  74. $request['CONTACT_NAME'] = $this->encryptionController->decrypt($request->CONTACT_NAME) ? $this->encryptionController->decrypt($request->CONTACT_NAME) : 'ENC_ERR';
  75. $request['CONTACT_TELEPHONE'] = $this->encryptionController->decrypt($request->CONTACT_TELEPHONE) ? $this->encryptionController->decrypt($request->CONTACT_TELEPHONE) : 'ENC_ERR';
  76. $request['CONTACT_LADA'] = $this->encryptionController->decrypt($request->CONTACT_LADA) ? $this->encryptionController->decrypt($request->CONTACT_LADA) : 'ENC_ERR';
  77. $request['CONTACT_ADDRESS'] = $this->encryptionController->decrypt($request->CONTACT_ADDRESS) ? $this->encryptionController->decrypt($request->CONTACT_ADDRESS) : 'ENC_ERR';
  78. $validator = Validator::make($request->all(), [
  79. 'id_user' => 'required|string',
  80. 'linea' => 'required|integer',
  81. 'USER_ID' => 'required|string',
  82. 'WORKTEAM_ID' => 'required|string',
  83. 'CONTRACT_TYPE' => 'required|string|in:Subcontratista,Interno',
  84. 'SUBCONTRATIST_ID' => 'required_if:CONTRACT_TYPE,=,Subcontratista|string',
  85. 'SPECIALITY' => 'required|string|max:75',
  86. 'FOREIGNER' => 'required|string|in:Si,No',
  87. 'RFC' => 'required_if:FOREIGNER,=,No|string|max:13',
  88. 'TAX' => 'required_if:FOREIGNER,=,Si|string|max:13',
  89. 'COUNTRY' => 'required|string|max:75',
  90. 'FEDERAL_ENTITY' => 'required|string|max:75',
  91. 'CITY' => 'string|max:75',
  92. 'TOWN' => 'string|max:75',
  93. 'SUBURB' => 'required|string|max:75',
  94. 'POSTAL_CODE' => 'required|string|max:5',
  95. 'STREET' => 'required|string|max:150',
  96. 'EXTERIOR_NUMBER' => 'required|integer',
  97. 'INTERIOR_NUMBER' => 'integer',
  98. 'CONTACT_NAME' => 'required|string|max:150',
  99. 'CONTACT_LADA' => 'required|string|max:10',
  100. 'CONTACT_TELEPHONE' => 'required|string|max:11',
  101. 'CONTACT_ADDRESS' => 'required|string|max:250',
  102. 'ATTACHED' => 'required|json',
  103. ]);
  104. if($validator->fails()){
  105. return $this->responseController->makeResponse(
  106. TRUE,
  107. 'ERR_PERSONAL_REG001: Uno o más errores encontrados',
  108. $this->responseController->makeErrors($validator->errors()->messages()),
  109. 400
  110. );
  111. }
  112. $form = $request->all();
  113. foreach($form as $k=>$v){
  114. if($k == 'INTERIOR_NUMBER' && $v == '0'){
  115. unset($form[$k]);
  116. }else if($v == '-'){
  117. unset($form[$k]);
  118. }
  119. }
  120. $idUser = $this->encryptionController->decrypt($form['id_user']);
  121. if(!$idUser){
  122. return $this->responseController->makeResponse(true, 'El ID de usuario no fue encriptado correctamente.', [], 400);
  123. }
  124. $usr = DB::table('S002V01TUSUA')->where([
  125. ['USUA_NULI', '=', $form['linea']],
  126. ['USUA_IDUS', '=', $idUser]
  127. ])->first();
  128. if(is_null($usr)){
  129. return $this->responseController->makeResponse(true, 'El usuario que realizó la petición no existe.', [], 404);
  130. }
  131. $idUserReg = $this->encryptionController->decrypt($form['USER_ID']);
  132. if(!$idUserReg){
  133. return $this->responseController->makeResponse(true, 'El ID del usuario seleccionado no fue encriptado correctamente.', [], 400);
  134. }else if($idUser == $idUserReg){
  135. return $this->responseController->makeResponse(true, 'El usuario no puede registrarse a sí mismo.', [], 400);
  136. }
  137. $usrReg = DB::table('S002V01TUSUA')->where([
  138. ['USUA_NULI', '=', $form['linea']],
  139. ['USUA_IDUS', '=', $idUserReg]
  140. ])->first();
  141. if(is_null($usrReg)){
  142. return $this->responseController->makeResponse(true, 'El usuario seleccionado no existe.', [], 404);
  143. }
  144. $idTeam = null;
  145. if(isset($form['WORKTEAM_ID'])){
  146. $idTeam = $this->encryptionController->decrypt($form['WORKTEAM_ID']);
  147. if(!$idTeam){
  148. return $this->responseController->makeResponse(true, 'El ID del equipo seleccionado encriptado correctamente.', [], 400);
  149. }
  150. $team = DB::table('S002V01TEQMA')->where([
  151. ['EQMA_IDEQ', '=', $idTeam],
  152. ['EQMA_NULI', '=', $form['linea']],
  153. ])->first();
  154. if(is_null($team)){
  155. return $this->responseController->makeResponse(true, 'El equipo seleccionado no existe.', [], 404);
  156. }else if($team->EQMA_ESTA == 'Eliminado'){
  157. return $this->responseController->makeResponse(true, 'El equipo seleccionado está eliminado.', [], 404);
  158. }
  159. }
  160. $idSubcontratist = null;
  161. if(isset($form['SUBCONTRATIST_ID'])){
  162. $idSubcontratist = $this->encryptionController->decrypt($form['SUBCONTRATIST_ID']);
  163. if(!$idSubcontratist){
  164. return $this->responseController->makeResponse(true, 'El ID del subcontratista seleccionado no fue encriptado correctamente.', [], 400);
  165. }
  166. $subcontratist = DB::table('S002V01TPESU')->where([
  167. ['PESU_IDPS', '=', $idSubcontratist],
  168. ['PESU_NULI', '=', $form['linea']],
  169. ])->first();
  170. if(is_null($subcontratist)){
  171. return $this->responseController->makeResponse(true, 'El subcontratista seleccionado no existe.', [], 404);
  172. }
  173. }
  174. $rfcx = null;
  175. if(isset($form['RFC']) && $form['FOREIGNER'] == 'No' && $form['RFC'] == 'ENC_ERR'){
  176. return $this->responseController->makeResponse(true, 'El RFC no fue encriptado correctamente.', [], 400);
  177. }else if(isset($form['RFC']) && $form['RFC'] != 'ENC_ERR'){
  178. $rfcx = $form['RFC'];
  179. }
  180. $taid = null;
  181. if(isset($form['TAX']) && $form['FOREIGNER'] == 'Si' && $form['TAX'] == 'ENC_ERR'){
  182. return $this->responseController->makeResponse(true, 'El TAX ID no fue encriptado correctamente.', [], 400);
  183. }else if(isset($form['TAX']) && $form['TAX'] != 'ENC_ERR'){
  184. $taid = $form['TAX'];
  185. }
  186. $country = DB::table('S002V01TPAIS')->where([
  187. ['PAIS_NULI', '=', $form['linea']],
  188. ['PAIS_IDPA', '=', $form['COUNTRY']],
  189. ])->first();
  190. if(is_null($country)){
  191. return $this->responseController->makeResponse(true, 'El país seleccionado no existe.', [], 404);
  192. }
  193. $colo = null;
  194. $ciud = null;
  195. $loca = null;
  196. if($form['COUNTRY'] == 'MEX' || $form['COUNTRY'] == 'USA' || $form['COUNTRY'] == 'CAN'){
  197. $state = DB::table('S002V01TESTA')->where([
  198. ['ESTA_NULI', '=', $form['linea']],
  199. ['ESTA_COES', '=', $form['FEDERAL_ENTITY']],
  200. ['ESTA_COPA', '=', $form['COUNTRY']],
  201. ])->first();
  202. if(is_null($state)){
  203. return $this->responseController->makeResponse(true, 'El estado seleccionado no existe.', [], 404);
  204. }
  205. if($form['COUNTRY'] == 'MEX'){
  206. if(isset($form['CITY'])){
  207. $city = DB::table('S002V01TMUNI')->where([
  208. ['MUNI_NULI', '=', $form['linea']],
  209. ['MUNI_COMU', '=', $form['CITY']],
  210. ['MUNI_COES', '=', $form['FEDERAL_ENTITY']],
  211. ])->first();
  212. if(is_null($city)){
  213. return $this->responseController->makeResponse(true, 'El municipio seleccionado no existe.', [], 404);
  214. }else{
  215. $ciud = $form['CITY'];
  216. }
  217. }
  218. if(isset($form['TOWN'])){
  219. $town = DB::table('S002V01TLOCA')->where([
  220. ['LOCA_NULI', '=', $form['linea']],
  221. ['LOCA_COLO', '=', $form['TOWN']],
  222. ['LOCA_COES', '=', $form['FEDERAL_ENTITY']],
  223. ])->first();
  224. if(is_null($town)){
  225. return $this->responseController->makeResponse(true, 'La localidad seleccionada no existe.', [], 404);
  226. }else{
  227. $loca = $form['TOWN'];
  228. }
  229. }
  230. $setting = DB::table('S002V01TCOLO')->where([
  231. ['COLO_NULI', '=', $form['linea']],
  232. ['COLO_COCO', '=', $form['SUBURB']],
  233. ['COLO_COPO', '=', $form['POSTAL_CODE']],
  234. ])->first();
  235. if(is_null($setting)){
  236. return $this->responseController->makeResponse(true, 'La colonia seleccionada no existe.', [], 404);
  237. }else{
  238. $colo = $form['SUBURB'];
  239. }
  240. $zipCode = DB::table('S002V01TCOPO')->where([
  241. ['COPO_NULI', '=', $form['linea']],
  242. ['COPO_COPO', '=', $form['POSTAL_CODE']],
  243. ['COPO_COES', '=', $form['FEDERAL_ENTITY']],
  244. ])->first();
  245. if(is_null($zipCode)){
  246. return $this->responseController->makeResponse(true, 'El código postal seleccionado no existe.', [], 404);
  247. }
  248. }else{
  249. $colo = $form['SUBURB'];
  250. $ciud = $form['CITY'];
  251. $loca = isset($form['TOWN']) ? $form['TOWN'] : null;
  252. }
  253. }else{
  254. $colo = $form['SUBURB'];
  255. $ciud = $form['CITY'];
  256. $loca = isset($form['TOWN']) ? $form['TOWN'] : null;
  257. }
  258. $subArr = DB::table('S002V01TPERS')->where('PERS_NULI', '=', $form['linea'])->where(function(Builder $query) use ($form, $idUserReg) {
  259. $query->where('PERS_IDUS', '=', $idUserReg)
  260. ->orWhere('PERS_XRFC', '=', $form['RFC']);
  261. })->where('PERS_ESTA', '=', 'Activo')->get()->all();
  262. if(count($subArr) > 0){
  263. return $this->responseController->makeResponse(true, 'La razón social, el RFC o el correo electrónico ya fueron registrados.', [], 401);
  264. }
  265. $ladasValues = [];
  266. $ladas = DB::table('S002V01TPAIS')->select([
  267. 'PAIS_LADA AS LADA'
  268. ])->where('PAIS_NULI', '=', $form['linea'])->get()->all();
  269. foreach($ladas as $lada){
  270. if($lada->LADA != '' && $lada->LADA != '0'){
  271. $ladasValues[] = $lada->LADA;
  272. }
  273. }
  274. if($form['CONTACT_LADA'] == 'ENC_ERR'){
  275. return $this->responseController->makeResponse(true, "La lada del contacto de emergencia no fue encriptada correctamente.", [], 400);
  276. }else if(!in_array($form['CONTACT_LADA'], $ladasValues)){
  277. return $this->responseController->makeResponse(true, "La lada $form[CONTACT_LADA] no está relacionada a ningún país.", [], 400);
  278. }else if($form['CONTACT_TELEPHONE'] == 'ENC_ERR'){
  279. return $this->responseController->makeResponse(true, 'El número telefónico del contacto de emergencia no fue encriptado correctamente.', [], 400);
  280. }
  281. if($form['CONTACT_ADDRESS'] == 'ENC_ERR'){
  282. return $this->responseController->makeResponse(true, "La dirección del contacto de emergencia no fue encriptada correctamente.", [], 400);
  283. }
  284. if($form['CONTACT_NAME'] == 'ENC_ERR'){
  285. return $this->responseController->makeResponse(true, "El nombre del contacto de emergencia no fue encriptada correctamente.", [], 400);
  286. }
  287. $docsArr = json_decode($form['ATTACHED'], true);
  288. if(count($docsArr) == 0){
  289. return $this->responseController->makeResponse(true, "El arreglo de archivos adjuntos está vacío.", [], 400);
  290. }
  291. $authCont = 0;
  292. foreach($docsArr as $doc){
  293. if($doc['type'] == 'AU') $authCont++;
  294. }
  295. if($authCont < 1){
  296. return $this->responseController->makeResponse(true, "El arreglo de archivos adjuntos debe contener al menos un documento de autorización.", [], 400);
  297. }
  298. $finalDocsArr = [];
  299. foreach($docsArr as $doc){
  300. $idFileDec = $this->encryptionController->decrypt($doc['id']);
  301. $tempFile = DB::table('S002V01TARTE')->where([
  302. ['ARTE_NULI', '=', $form['linea']],
  303. ['ARTE_IDAR', '=', $idFileDec],
  304. ])->first();
  305. if(is_null($tempFile)){
  306. return $this->responseController->makeResponse(true, "El archivo $doc[name] no está registrado.", [], 404);
  307. }
  308. $classifications = ["AU" => "IN", "OF" => "CO", "CE" => "CE"];
  309. $cldo = $classifications[$doc['type']];
  310. $finalFile = $this->documentManagementController->moveFinalFile($form['linea'], "GPRS", $cldo, $tempFile, $idUser);
  311. if(!$finalFile[0]){
  312. return $this->responseController->makeResponse(true, $finalFile[1], [], 400);
  313. }else{
  314. $finalDocsArr[] = [
  315. 'CODE' => $finalFile[1],
  316. 'TYPE' => $doc['type']
  317. ];
  318. }
  319. }
  320. $now = $this->functionsController->now();
  321. $nowStr = $now->toDateTimeString();
  322. $doas = json_encode($finalDocsArr);
  323. $nuin = isset($form['INTERIOR_NUMBER']) ? $form['INTERIOR_NUMBER'] : null;
  324. $employeeID = DB::table('S002V01TPERS')->insertGetId([
  325. 'PERS_NULI' => $form['linea'],
  326. 'PERS_IDUS' => $idUserReg,
  327. 'PERS_EQTR' => $idTeam,
  328. 'PERS_TICO' => $form['CONTRACT_TYPE'],
  329. 'PERS_IDPS' => $idSubcontratist,
  330. 'PERS_ESPE' => $form['SPECIALITY'],
  331. 'PERS_EXTR' => $form['FOREIGNER'],
  332. 'PERS_XRFC' => $rfcx,
  333. 'PERS_TAID' => $taid,
  334. 'PERS_IDPA' => $form['COUNTRY'],
  335. 'PERS_ENFE' => $form['FEDERAL_ENTITY'],
  336. 'PERS_CIUD' => $ciud,
  337. 'PERS_LOCA' => $loca,
  338. 'PERS_COLO' => $colo,
  339. 'PERS_COPO' => $form['POSTAL_CODE'],
  340. 'PERS_CALL' => $form['STREET'],
  341. 'PERS_NUEX' => $form['EXTERIOR_NUMBER'],
  342. 'PERS_NUIN' => $nuin,
  343. 'PERS_NOCE' => $form['CONTACT_NAME'],
  344. 'PERS_LCEM' => $form['CONTACT_LADA'],
  345. 'PERS_NUTC' => $form['CONTACT_TELEPHONE'],
  346. 'PERS_DCEM' => $form['CONTACT_ADDRESS'],
  347. 'PERS_DOAS' => $doas,
  348. 'PERS_USRE' => $idUser,
  349. 'PERS_FERE' => $nowStr,
  350. ]);
  351. $actions = DB::getQueryLog();
  352. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  353. $nameEmp = $this->functionsController->joinName($usrReg->USUA_NOMB, $usrReg->USUA_APPA, $usrReg->USUA_APMA);
  354. $idac = $this->functionsController->registerActivity(
  355. $form['linea'],
  356. 'S002V01M11GPRS',
  357. 'S002V01F01GEPE',
  358. 'S002V01P02REEM',
  359. 'Registro',
  360. "El usuario $name (" . $usr->USUA_IDUS . ") registró al empleado " . $nameEmp . "($employeeID).",
  361. $idUser,
  362. $nowStr,
  363. 'S002V01S02GEPE'
  364. );
  365. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $form['linea']);
  366. return $this->responseController->makeResponse(false, 'EXITO');
  367. }
  368. public function getEmployeeById($idEmployee, $idUser, $line) {
  369. DB::enableQueryLog();
  370. $idUser = $this->encryptionController->decrypt($idUser);
  371. if(!$idUser){
  372. return $this->responseController->makeResponse(true, 'El ID del usuario que realizó la solicitud no está encriptado correctamente', [], 400);
  373. }
  374. $usr = DB::table('S002V01TUSUA')->where([
  375. ['USUA_NULI', '=', $line],
  376. ['USUA_IDUS', '=', $idUser],
  377. ])->first();
  378. if(is_null($usr)){
  379. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado', [], 404);
  380. }
  381. $idEmployee = $this->encryptionController->decrypt($idEmployee);
  382. if(!$idEmployee){
  383. return $this->responseController->makeResponse(true, 'El ID del empleado solicitado no está encriptado correctamente', [], 400);
  384. }
  385. $employee = DB::table('S002V01TPERS')->select([
  386. 'PERS_IDPE AS ID_EMPLOYEE',
  387. DB::raw('TRIM(CONCAT(USUA_NOMB, " " , USUA_APPA, " ", COALESCE(USUA_APMA,""))) as EMPLOYEE_NAME'),
  388. 'PERS_IDUS AS USER_ID',
  389. 'EQMA_IDEQ AS WORKTEAM_ID',
  390. 'EQMA_NOMB AS WORKTEAM_NAME',
  391. 'PERS_TICO AS CONTRACT_TYPE',
  392. 'PERS_IDPS AS SUBCONTRATIST_ID',
  393. 'PESU_RASO AS SUBCONTRATIST_NAME',
  394. 'PESU_REFI AS TAX_REGIME',
  395. 'REFI_DRFI AS TAX_REGIME_DESCRIPTION',
  396. 'PERS_ESPE AS SPECIALITY',
  397. 'PERS_EXTR AS FOREIGNER',
  398. 'PERS_XRFC AS RFC',
  399. 'PERS_TAID AS TAX_ID',
  400. 'PERS_IDPA AS COUNTRY',
  401. 'PERS_ENFE AS STATE',
  402. 'PERS_CIUD AS CITY',
  403. 'PERS_LOCA AS TOWN',
  404. 'PERS_COLO AS SETTING',
  405. 'PERS_COPO AS ZIP_CODE',
  406. 'PERS_CALL AS STREET',
  407. 'PERS_NUEX AS EXTERIOR_NUMBER',
  408. 'PERS_NUIN AS INTERIOR_NUMBER',
  409. 'PERS_NOCE AS CONTACT_NAME',
  410. 'PERS_LCEM AS CONTACT_LADA',
  411. 'PERS_NUTC AS CONTACT_TELEPHONE',
  412. 'PERS_DCEM AS CONTACT_ADDRESS',
  413. 'PERS_DOAS AS DOCUMENTS',
  414. 'PERS_ESTA AS STATUS',
  415. 'PERS_USRE AS USRREG',
  416. 'PERS_USMO AS USRMOD',
  417. 'PERS_FERE AS FECREG',
  418. 'PERS_FEMO AS FECMOD',
  419. ])->leftJoin('S002V01TEQMA', 'EQMA_IDEQ', '=', 'PERS_EQTR')
  420. ->leftJoin('S002V01TPESU', 'PESU_IDPS', '=', 'PERS_IDPS')
  421. ->leftJoin('S002V01TREFI', 'REFI_CRFI', '=', 'PESU_REFI')
  422. ->join('S002V01TUSUA', 'PERS_IDUS', '=', 'USUA_IDUS')->where([
  423. ['PERS_NULI', '=', $line],
  424. ['PERS_IDPE', '=', $idEmployee],
  425. ])->first();
  426. $attached = [];
  427. $docsArr = json_decode($employee->DOCUMENTS, true);
  428. foreach($docsArr as $doc){
  429. $docArr = explode('=', $doc['CODE']);
  430. $codeArr = explode('-', $docArr[0]);
  431. $document = DB::table('S002V01TAFAL')->select([
  432. 'AFAL_NOAR AS FILE_NAME',
  433. 'AFAL_EXTE AS FILE_EXTENSION',
  434. 'AFAL_TAMA AS FILE_SIZE',
  435. 'AFAL_ESTA AS FILE_STATUS'
  436. ])->where([
  437. ['AFAL_NULI', '=', $line],
  438. ['AFAL_COMO', '=', $codeArr[1]],
  439. ['AFAL_CLDO', '=', $codeArr[2]],
  440. ['AFAL_FECR', '=', $codeArr[3]],
  441. ['AFAL_NUSE', '=', $codeArr[4]],
  442. ['AFAL_NUVE', '=', $docArr[1]],
  443. ])->first();
  444. $document->FILE_ID = $this->encryptionController->encrypt($doc['CODE']);
  445. $document->FILE_TYPE = $doc['TYPE'];
  446. $attached[] = $document;
  447. }
  448. $employee->DOCUMENTS = json_encode($attached);
  449. $country = DB::table('S002V01TPAIS')->where([
  450. ['PAIS_NULI', '=', $line],
  451. ['PAIS_IDPA', '=', $employee->COUNTRY]
  452. ])->first();
  453. $state = DB::table('S002V01TESTA')->where([
  454. ['ESTA_NULI', '=', $line],
  455. ['ESTA_COES', '=', $employee->STATE],
  456. ['ESTA_COPA', '=', $employee->COUNTRY]
  457. ])->first();
  458. $city = DB::table('S002V01TMUNI')->where([
  459. ['MUNI_NULI', '=', $line],
  460. ['MUNI_COMU', '=', $employee->CITY],
  461. ['MUNI_COES', '=', $employee->STATE],
  462. ])->first();
  463. $town = DB::table('S002V01TLOCA')->where([
  464. ['LOCA_NULI', '=', $line],
  465. ['LOCA_COLO', '=', $employee->TOWN],
  466. ['LOCA_COES', '=', $employee->STATE],
  467. ])->first();
  468. $setting = DB::table('S002V01TCOLO')->where([
  469. ['COLO_NULI', '=', $line],
  470. ['COLO_COCO', '=', $employee->SETTING],
  471. ['COLO_COPO', '=', $employee->ZIP_CODE],
  472. ])->first();
  473. $employee->COUNTRY = $country->PAIS_NOMB . " (" . $employee->COUNTRY . ")";
  474. $employee->STATE = is_null($state) ? $employee->STATE : $state->ESTA_NOES . " (" . $employee->STATE . ")";
  475. $employee->CITY = is_null($city) ? $employee->CITY : $city->MUNI_NOMU . " (" . $employee->CITY . ")";
  476. $employee->TOWN = is_null($town) ? $employee->TOWN : $town->LOCA_NOLO . " (" . $employee->TOWN . ")";
  477. $employee->SETTING = is_null($setting) ? $employee->SETTING : $setting->COLO_NOCO . " (" . $employee->SETTING . ")";
  478. $usrReg = DB::table('S002V01TUSUA')->where([
  479. ['USUA_NULI', '=', $line],
  480. ['USUA_IDUS', '=', $employee->USRREG]
  481. ])->first();
  482. $nameReg = $this->functionsController->joinName($usrReg->USUA_NOMB, $usrReg->USUA_APPA, $usrReg->USUA_APMA);
  483. $employee->USRREG = "$nameReg (" . $employee->USRREG . ")";
  484. if(!is_null($employee->USRMOD)){
  485. $usrMod = DB::table('S002V01TUSUA')->where([
  486. ['USUA_NULI', '=', $line],
  487. ['USUA_IDUS', '=', $employee->USRMOD]
  488. ])->first();
  489. $nameMod = $this->functionsController->joinName($usrMod->USUA_NOMB, $usrMod->USUA_APPA, $usrMod->USUA_APMA);
  490. $employee->USRMOD = "$nameMod (" . $employee->USRMOD . ")";
  491. }
  492. $now = $this->functionsController->now();
  493. $nowStr = $now->toDateTimeString();
  494. $actions = DB::getQueryLog();
  495. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  496. $idac = $this->functionsController->registerActivity(
  497. $line,
  498. 'S002V01M11GPRS',
  499. 'S002V01F01GEPE',
  500. 'S002V01P03DEEM',
  501. 'Consulta',
  502. "El usuario $name (" . $usr->USUA_IDUS . ") consultó al empleado " . $employee->EMPLOYEE_NAME . " ($idEmployee).",
  503. $idUser,
  504. $nowStr,
  505. 'S002V01S02GEPE'
  506. );
  507. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $line);
  508. return $this->responseController->makeResponse(false, 'EXITO', $employee);
  509. }
  510. public function updateEmployee(Request $request) {
  511. DB::enableQueryLog();
  512. $request['RFC'] = $this->encryptionController->decrypt($request->RFC) ? $this->encryptionController->decrypt($request->RFC) : 'ENC_ERR';
  513. $request['TAX'] = $this->encryptionController->decrypt($request->TAX) ? $this->encryptionController->decrypt($request->TAX) : 'ENC_ERR';
  514. $request['CONTACT_NAME'] = $this->encryptionController->decrypt($request->CONTACT_NAME) ? $this->encryptionController->decrypt($request->CONTACT_NAME) : 'ENC_ERR';
  515. $request['CONTACT_TELEPHONE'] = $this->encryptionController->decrypt($request->CONTACT_TELEPHONE) ? $this->encryptionController->decrypt($request->CONTACT_TELEPHONE) : 'ENC_ERR';
  516. $request['CONTACT_LADA'] = $this->encryptionController->decrypt($request->CONTACT_LADA) ? $this->encryptionController->decrypt($request->CONTACT_LADA) : 'ENC_ERR';
  517. $request['CONTACT_ADDRESS'] = $this->encryptionController->decrypt($request->CONTACT_ADDRESS) ? $this->encryptionController->decrypt($request->CONTACT_ADDRESS) : 'ENC_ERR';
  518. $validator = Validator::make($request->all(), [
  519. 'id_user' => 'required|string',
  520. 'linea' => 'required|integer',
  521. 'id_employee' => 'required|string',
  522. 'USER_ID' => 'required|string',
  523. 'WORKTEAM_ID' => 'required|string',
  524. 'CONTRACT_TYPE' => 'required|string|in:Subcontratista,Interno',
  525. 'SUBCONTRATIST_ID' => 'required_if:CONTRACT_TYPE,=,Subcontratista|string',
  526. 'SPECIALITY' => 'required|string|max:75',
  527. 'FOREIGNER' => 'required|string|in:Si,No',
  528. 'RFC' => 'required_if:FOREIGNER,=,No|string|max:13',
  529. 'TAX' => 'required_if:FOREIGNER,=,Si|string|max:13',
  530. 'COUNTRY' => 'required|string|max:75',
  531. 'FEDERAL_ENTITY' => 'required|string|max:75',
  532. 'CITY' => 'string|max:75',
  533. 'TOWN' => 'string|max:75',
  534. 'SUBURB' => 'required|string|max:75',
  535. 'POSTAL_CODE' => 'required|string|max:5',
  536. 'STREET' => 'required|string|max:150',
  537. 'EXTERIOR_NUMBER' => 'required|integer',
  538. 'INTERIOR_NUMBER' => 'integer',
  539. 'CONTACT_NAME' => 'required|string|max:150',
  540. 'CONTACT_LADA' => 'required|string|max:10',
  541. 'CONTACT_TELEPHONE' => 'required|string|max:11',
  542. 'CONTACT_ADDRESS' => 'required|string|max:250',
  543. 'ATTACHED' => 'required|json',
  544. ]);
  545. if($validator->fails()){
  546. return $this->responseController->makeResponse(
  547. TRUE,
  548. 'ERR_PERSONAL_REG001: Uno o más errores encontrados',
  549. $this->responseController->makeErrors($validator->errors()->messages()),
  550. 400
  551. );
  552. }
  553. $form = $request->all();
  554. foreach($form as $k=>$v){
  555. if($k == 'INTERIOR_NUMBER' && $v == '0'){
  556. unset($form[$k]);
  557. }else if($v == '-'){
  558. unset($form[$k]);
  559. }
  560. }
  561. $idUser = $this->encryptionController->decrypt($form['id_user']);
  562. if(!$idUser){
  563. return $this->responseController->makeResponse(true, 'El ID de usuario no fue encriptado correctamente.', [], 400);
  564. }
  565. $usr = DB::table('S002V01TUSUA')->where([
  566. ['USUA_NULI', '=', $form['linea']],
  567. ['USUA_IDUS', '=', $idUser]
  568. ])->first();
  569. if(is_null($usr)){
  570. return $this->responseController->makeResponse(true, 'El usuario que realizó la petición no existe.', [], 404);
  571. }
  572. $idEmployee = $this->encryptionController->decrypt($form['id_employee']);
  573. if(!$idEmployee){
  574. return $this->responseController->makeResponse(true, 'El ID del empleado que desea actualizar no fue encriptado correctamente.', [], 400);
  575. }
  576. $employee = DB::table('S002V01TPERS')->where([
  577. ['PERS_NULI', '=', $form['linea']],
  578. ['PERS_IDPE', '=', $idEmployee]
  579. ])->first();
  580. if(is_null($employee)){
  581. return $this->responseController->makeResponse(true, 'El empleado que desea actualizar no existe.', [], 404);
  582. }
  583. $idUserReg = $this->encryptionController->decrypt($form['USER_ID']);
  584. if(!$idUserReg){
  585. return $this->responseController->makeResponse(true, 'El ID del usuario seleccionado no fue encriptado correctamente.', [], 400);
  586. }else if($idUser == $idUserReg){
  587. return $this->responseController->makeResponse(true, 'El usuario no puede registrarse a sí mismo.', [], 400);
  588. }
  589. $usrReg = DB::table('S002V01TUSUA')->where([
  590. ['USUA_NULI', '=', $form['linea']],
  591. ['USUA_IDUS', '=', $idUserReg]
  592. ])->first();
  593. if(is_null($usrReg)){
  594. return $this->responseController->makeResponse(true, 'El usuario seleccionado no existe.', [], 404);
  595. }
  596. $idTeam = null;
  597. if(isset($form['WORKTEAM_ID'])){
  598. $idTeam = $this->encryptionController->decrypt($form['WORKTEAM_ID']);
  599. if(!$idTeam){
  600. return $this->responseController->makeResponse(true, 'El ID del equipo seleccionado encriptado correctamente.', [], 400);
  601. }
  602. $team = DB::table('S002V01TEQMA')->where([
  603. ['EQMA_IDEQ', '=', $idTeam],
  604. ['EQMA_NULI', '=', $form['linea']],
  605. ])->first();
  606. if(is_null($team)){
  607. return $this->responseController->makeResponse(true, 'El equipo seleccionado no existe.', [], 404);
  608. }else if($team->EQMA_ESTA == 'Eliminado'){
  609. return $this->responseController->makeResponse(true, 'El equipo seleccionado está eliminado.', [], 404);
  610. }
  611. }
  612. $idSubcontratist = null;
  613. if(isset($form['SUBCONTRATIST_ID'])){
  614. $idSubcontratist = $this->encryptionController->decrypt($form['SUBCONTRATIST_ID']);
  615. if(!$idSubcontratist){
  616. return $this->responseController->makeResponse(true, 'El ID del subcontratista seleccionado no fue encriptado correctamente.', [], 400);
  617. }
  618. $subcontratist = DB::table('S002V01TPESU')->where([
  619. ['PESU_IDPS', '=', $idSubcontratist],
  620. ['PESU_NULI', '=', $form['linea']],
  621. ])->first();
  622. if(is_null($subcontratist)){
  623. return $this->responseController->makeResponse(true, 'El subcontratista seleccionado no existe.', [], 404);
  624. }
  625. }
  626. $rfcx = null;
  627. if(isset($form['RFC']) && $form['FOREIGNER'] == 'No' && $form['RFC'] == 'ENC_ERR'){
  628. return $this->responseController->makeResponse(true, 'El RFC no fue encriptado correctamente.', [], 400);
  629. }else if(isset($form['RFC']) && $form['RFC'] != 'ENC_ERR'){
  630. $rfcx = $form['RFC'];
  631. }
  632. $taid = null;
  633. if(isset($form['TAX']) && $form['FOREIGNER'] == 'Si' && $form['TAX'] == 'ENC_ERR'){
  634. return $this->responseController->makeResponse(true, 'El TAX ID no fue encriptado correctamente.', [], 400);
  635. }else if(isset($form['TAX']) && $form['TAX'] != 'ENC_ERR'){
  636. $taid = $form['TAX'];
  637. }
  638. $country = DB::table('S002V01TPAIS')->where([
  639. ['PAIS_NULI', '=', $form['linea']],
  640. ['PAIS_IDPA', '=', $form['COUNTRY']],
  641. ])->first();
  642. if(is_null($country)){
  643. return $this->responseController->makeResponse(true, 'El país seleccionado no existe.', [], 404);
  644. }
  645. $colo = null;
  646. $ciud = null;
  647. $loca = null;
  648. if($form['COUNTRY'] == 'MEX' || $form['COUNTRY'] == 'USA' || $form['COUNTRY'] == 'CAN'){
  649. $state = DB::table('S002V01TESTA')->where([
  650. ['ESTA_NULI', '=', $form['linea']],
  651. ['ESTA_COES', '=', $form['FEDERAL_ENTITY']],
  652. ['ESTA_COPA', '=', $form['COUNTRY']],
  653. ])->first();
  654. if(is_null($state)){
  655. return $this->responseController->makeResponse(true, 'El estado seleccionado no existe.', [], 404);
  656. }
  657. if($form['COUNTRY'] == 'MEX'){
  658. if(isset($form['CITY'])){
  659. $city = DB::table('S002V01TMUNI')->where([
  660. ['MUNI_NULI', '=', $form['linea']],
  661. ['MUNI_COMU', '=', $form['CITY']],
  662. ['MUNI_COES', '=', $form['FEDERAL_ENTITY']],
  663. ])->first();
  664. if(is_null($city)){
  665. return $this->responseController->makeResponse(true, 'El municipio seleccionado no existe.', [], 404);
  666. }else{
  667. $ciud = $form['CITY'];
  668. }
  669. }
  670. if(isset($form['TOWN'])){
  671. $town = DB::table('S002V01TLOCA')->where([
  672. ['LOCA_NULI', '=', $form['linea']],
  673. ['LOCA_COLO', '=', $form['TOWN']],
  674. ['LOCA_COES', '=', $form['FEDERAL_ENTITY']],
  675. ])->first();
  676. if(is_null($town)){
  677. return $this->responseController->makeResponse(true, 'La localidad seleccionada no existe.', [], 404);
  678. }else{
  679. $loca = $form['TOWN'];
  680. }
  681. }
  682. $setting = DB::table('S002V01TCOLO')->where([
  683. ['COLO_NULI', '=', $form['linea']],
  684. ['COLO_COCO', '=', $form['SUBURB']],
  685. ['COLO_COPO', '=', $form['POSTAL_CODE']],
  686. ])->first();
  687. if(is_null($setting)){
  688. return $this->responseController->makeResponse(true, 'La colonia seleccionada no existe.', [], 404);
  689. }else{
  690. $colo = $form['SUBURB'];
  691. }
  692. $zipCode = DB::table('S002V01TCOPO')->where([
  693. ['COPO_NULI', '=', $form['linea']],
  694. ['COPO_COPO', '=', $form['POSTAL_CODE']],
  695. ['COPO_COES', '=', $form['FEDERAL_ENTITY']],
  696. ])->first();
  697. if(is_null($zipCode)){
  698. return $this->responseController->makeResponse(true, 'El código postal seleccionado no existe.', [], 404);
  699. }
  700. }else{
  701. $colo = $form['SUBURB'];
  702. $ciud = $form['CITY'];
  703. $loca = isset($form['TOWN']) ? $form['TOWN'] : null;
  704. }
  705. }else{
  706. $colo = $form['SUBURB'];
  707. $ciud = $form['CITY'];
  708. $loca = isset($form['TOWN']) ? $form['TOWN'] : null;
  709. }
  710. $subArr = DB::table('S002V01TPERS')->where('PERS_NULI', '=', $form['linea'])->where(function(Builder $query) use ($form, $idUserReg) {
  711. $query->where('PERS_IDUS', '=', $idUserReg)
  712. ->orWhere('PERS_XRFC', '=', $form['RFC']);
  713. })->where([
  714. ['PERS_ESTA', '=', 'Activo'],
  715. ['PERS_IDPE', '!=', $idEmployee],
  716. ])->get()->all();
  717. if(count($subArr) > 0){
  718. return $this->responseController->makeResponse(true, 'La razón social, el RFC o el correo electrónico ya fueron registrados.', [], 401);
  719. }
  720. $ladasValues = [];
  721. $ladas = DB::table('S002V01TPAIS')->select([
  722. 'PAIS_LADA AS LADA'
  723. ])->where('PAIS_NULI', '=', $form['linea'])->get()->all();
  724. foreach($ladas as $lada){
  725. if($lada->LADA != '' && $lada->LADA != '0'){
  726. $ladasValues[] = $lada->LADA;
  727. }
  728. }
  729. if($form['CONTACT_LADA'] == 'ENC_ERR'){
  730. return $this->responseController->makeResponse(true, "La lada del contacto de emergencia no fue encriptada correctamente.", [], 400);
  731. }else if(!in_array($form['CONTACT_LADA'], $ladasValues)){
  732. return $this->responseController->makeResponse(true, "La lada $form[CONTACT_LADA] no está relacionada a ningún país.", [], 400);
  733. }else if($form['CONTACT_TELEPHONE'] == 'ENC_ERR'){
  734. return $this->responseController->makeResponse(true, 'El número telefónico del contacto de emergencia no fue encriptado correctamente.', [], 400);
  735. }
  736. if($form['CONTACT_ADDRESS'] == 'ENC_ERR'){
  737. return $this->responseController->makeResponse(true, "La dirección del contacto de emergencia no fue encriptada correctamente.", [], 400);
  738. }
  739. if($form['CONTACT_NAME'] == 'ENC_ERR'){
  740. return $this->responseController->makeResponse(true, "El nombre del contacto de emergencia no fue encriptada correctamente.", [], 400);
  741. }
  742. $docsArr = json_decode($form['ATTACHED'], true);
  743. if(count($docsArr) == 0){
  744. return $this->responseController->makeResponse(true, "El arreglo de archivos adjuntos está vacío.", [], 400);
  745. }
  746. $authCont = 0;
  747. foreach($docsArr as $doc){
  748. if($doc['type'] == 'AU') $authCont++;
  749. }
  750. if($authCont < 1){
  751. return $this->responseController->makeResponse(true, "El arreglo de archivos adjuntos debe contener al menos un documento de autorización.", [], 400);
  752. }
  753. $finalDocsArr = [];
  754. foreach($docsArr as $doc){
  755. if(!array_key_exists('isFinalFile', $doc)){
  756. return $this->responseController->makeResponse(true, "La información del archivo $doc[name] no tiene un formato correcto.", [], 400);
  757. }
  758. if($doc['isFinalFile']){
  759. $finalDocsArr[] = [
  760. 'CODE' => $doc['name'],
  761. 'TYPE' => $doc['type']
  762. ];
  763. }else{
  764. $idFileDec = $this->encryptionController->decrypt($doc['id']);
  765. $tempFile = DB::table('S002V01TARTE')->where([
  766. ['ARTE_NULI', '=', $form['linea']],
  767. ['ARTE_IDAR', '=', $idFileDec],
  768. ])->first();
  769. if(is_null($tempFile)){
  770. return $this->responseController->makeResponse(true, "El archivo $doc[name] no está registrado.", [], 404);
  771. }
  772. $classifications = ["AU" => "IN", "OF" => "CO", "CE" => "CE"];
  773. $cldo = $classifications[$doc['type']];
  774. $finalFile = $this->documentManagementController->moveFinalFile($form['linea'], "GPRS", $cldo, $tempFile, $idUser);
  775. if(!$finalFile[0]){
  776. return $this->responseController->makeResponse(true, $finalFile[1], [], 400);
  777. }else{
  778. $finalDocsArr[] = [
  779. 'CODE' => $finalFile[1],
  780. 'TYPE' => $doc['type']
  781. ];
  782. }
  783. }
  784. }
  785. $doas = json_encode($finalDocsArr);
  786. $nuin = isset($form['INTERIOR_NUMBER']) ? $form['INTERIOR_NUMBER'] : null;
  787. $now = $this->functionsController->now();
  788. $nowStr = $now->toDateTimeString();
  789. DB::table('S002V01TPERS')->where([
  790. ['PERS_NULI', '=', $form['linea']],
  791. ['PERS_IDPE', '=', $idEmployee]
  792. ])->update([
  793. 'PERS_EQTR' => $idTeam,
  794. 'PERS_TICO' => $form['CONTRACT_TYPE'],
  795. 'PERS_IDPS' => $idSubcontratist,
  796. 'PERS_ESPE' => $form['SPECIALITY'],
  797. 'PERS_EXTR' => $form['FOREIGNER'],
  798. 'PERS_XRFC' => $rfcx,
  799. 'PERS_TAID' => $taid,
  800. 'PERS_IDPA' => $form['COUNTRY'],
  801. 'PERS_ENFE' => $form['FEDERAL_ENTITY'],
  802. 'PERS_CIUD' => $ciud,
  803. 'PERS_LOCA' => $loca,
  804. 'PERS_COLO' => $colo,
  805. 'PERS_COPO' => $form['POSTAL_CODE'],
  806. 'PERS_CALL' => $form['STREET'],
  807. 'PERS_NUEX' => $form['EXTERIOR_NUMBER'],
  808. 'PERS_NUIN' => $nuin,
  809. 'PERS_NOCE' => $form['CONTACT_NAME'],
  810. 'PERS_LCEM' => $form['CONTACT_LADA'],
  811. 'PERS_NUTC' => $form['CONTACT_TELEPHONE'],
  812. 'PERS_DCEM' => $form['CONTACT_ADDRESS'],
  813. 'PERS_DOAS' => $doas,
  814. 'PERS_USMO' => $idUser,
  815. 'PERS_FEMO' => $nowStr,
  816. ]);
  817. $actions = DB::getQueryLog();
  818. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  819. $nameEmp = $this->functionsController->joinName($usrReg->USUA_NOMB, $usrReg->USUA_APPA, $usrReg->USUA_APMA);
  820. $idac = $this->functionsController->registerActivity(
  821. $form['linea'],
  822. 'S002V01M11GPRS',
  823. 'S002V01F01GEPE',
  824. 'S002V01P02REEM',
  825. 'Actualización',
  826. "El usuario $name (" . $usr->USUA_IDUS . ") actualizó al empleado " . $nameEmp . "($idEmployee).",
  827. $idUser,
  828. $nowStr,
  829. 'S002V01S02GEPE'
  830. );
  831. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $form['linea']);
  832. return $this->responseController->makeResponse(false, 'EXITO');
  833. }
  834. public function updateToInactiveStatus(Request $request) {
  835. DB::enableQueryLog();
  836. $validator = Validator::make($request->all(), [
  837. 'id_user' => 'required|string',
  838. 'linea' => 'required|integer',
  839. 'id_employee' => 'required|string',
  840. ]);
  841. if($validator->fails()){
  842. return $this->responseController->makeResponse(
  843. true,
  844. "Se encontraron uno o más errores.",
  845. $this->responseController->makeErrors(
  846. $validator->errors()->messages()
  847. ),
  848. 401
  849. );
  850. }
  851. $form = $request->all();
  852. $idUser = $this->encryptionController->decrypt($form['id_user']);
  853. if(!$idUser){
  854. return $this->responseController->makeResponse(true, 'El ID de usuario no fue encriptado correctamente.', [], 400);
  855. }
  856. $usr = DB::table('S002V01TUSUA')->where([
  857. ['USUA_NULI', '=', $form['linea']],
  858. ['USUA_IDUS', '=', $idUser]
  859. ])->first();
  860. if(is_null($usr)){
  861. return $this->responseController->makeResponse(true, 'El usuario que realizó la petición no existe.', [], 404);
  862. }
  863. $idEmployee = $this->encryptionController->decrypt($form['id_employee']);
  864. if(!$idEmployee){
  865. return $this->responseController->makeResponse(true, 'El ID del empleado que desea actualizar no fue encriptado correctamente.', [], 400);
  866. }
  867. $employee = DB::table('S002V01TPERS')->where([
  868. ['PERS_NULI', '=', $form['linea']],
  869. ['PERS_IDPE', '=', $idEmployee]
  870. ])->first();
  871. if(is_null($employee)){
  872. return $this->responseController->makeResponse(true, 'El empleado que desea actualizar no existe.', [], 404);
  873. }
  874. if($idUser == $employee->PERS_IDUS){
  875. return $this->responseController->makeResponse(true, 'El usuario no puede eliminarse a sí mismo.', [], 400);
  876. }
  877. $now = $this->functionsController->now();
  878. $nowStr = $now->toDateTimeString();
  879. DB::table('S002V01TPERS')->where([
  880. ['PERS_NULI', '=', $form['linea']],
  881. ['PERS_IDPE', '=', $idEmployee]
  882. ])->update([
  883. 'PERS_ESTA' => 'Eliminado',
  884. 'PERS_USMO' => $idUser,
  885. 'PERS_FEMO' => $nowStr,
  886. ]);
  887. $actions = DB::getQueryLog();
  888. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  889. $idac = $this->functionsController->registerActivity(
  890. $form['linea'],
  891. 'S002V01M11GPRS',
  892. 'S002V01F01GEPE',
  893. 'S002V01P02REEM',
  894. 'Eliminación',
  895. "El usuario $name (" . $usr->USUA_IDUS . ") eliminó al empleado $idEmployee.",
  896. $idUser,
  897. $nowStr,
  898. 'S002V01S02GEPE'
  899. );
  900. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $form['linea']);
  901. return $this->responseController->makeResponse(false, 'EXITO');
  902. }
  903. public function getContractsOfEveryEmployee($idUser, $line) {
  904. DB::enableQueryLog();
  905. $idUser = $this->encryptionController->decrypt($idUser);
  906. if(!$idUser){
  907. return $this->responseController->makeResponse(true, 'El ID del usuario que realizó la solicitud no está encriptado correctamente', [], 400);
  908. }
  909. $usr = DB::table('S002V01TUSUA')->where([
  910. ['USUA_NULI', '=', $line],
  911. ['USUA_IDUS', '=', $idUser],
  912. ])->first();
  913. if(is_null($usr)){
  914. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado', [], 404);
  915. }
  916. $contractsByEmployees = DB::table('S002V01TPERS')->select([
  917. DB::raw("
  918. TRIM(CONCAT(
  919. USUA_NOMB,
  920. ' ',
  921. USUA_APPA,
  922. IF(ISNULL(USUA_APMA), '', CONCAT(' ', USUA_APMA)),
  923. ' <(',
  924. PERS_IDPE,
  925. ')> (',
  926. PERS_IDUS,
  927. ')'
  928. )) AS EMPLOYEE
  929. "),
  930. DB::raw("COUNT(CONT_IDCO) AS CONTRACTS")
  931. ])->leftJoin('S002V01TCONT', 'CONT_IDEM', '=', 'PERS_IDPE')
  932. ->join('S002V01TUSUA', 'USUA_IDUS', '=', 'PERS_IDUS')
  933. ->groupBy('EMPLOYEE')->where([
  934. ['PERS_NULI', '=', $line],
  935. ['PERS_TICO', '=', 'Interno'],
  936. ])->get()->all();
  937. foreach($contractsByEmployees as $key=>$val){
  938. $employeeArr = explode('<', $val->EMPLOYEE);
  939. $employeeName = $employeeArr[0];
  940. $employeeArr = array_reverse($employeeArr);
  941. $employeeArr = explode('>', $employeeArr[0]);
  942. $employeeID = str_replace('(', '', $employeeArr[0]);
  943. $employeeID = str_replace(')', '', $employeeID);
  944. $val->ID_EMPLOYEE = $this->encryptionController->encrypt($employeeID);
  945. $val->EMPLOYEE = $employeeName . join('', $employeeArr);
  946. $contractsByEmployees[$key] = $val;
  947. }
  948. $now = $this->functionsController->now();
  949. $nowStr = $now->toDateTimeString();
  950. $actions = DB::getQueryLog();
  951. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  952. $idac = $this->functionsController->registerActivity(
  953. $line,
  954. 'S002V01M11GPRS',
  955. 'S002V01F02ACEM',
  956. 'S002V01P01HCEM',
  957. 'Consulta',
  958. "El usuario $name (" . $usr->USUA_IDUS . ") consultó los contratos por empleados.",
  959. $idUser,
  960. $nowStr,
  961. 'S002V01S02GEPE'
  962. );
  963. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $line);
  964. return $this->responseController->makeResponse(false, 'EXITO', $contractsByEmployees);
  965. }
  966. public function getDetailsOfContractsByEmployee($idEmployee, $idUser, $line) {
  967. DB::enableQueryLog();
  968. $idUser = $this->encryptionController->decrypt($idUser);
  969. if(!$idUser){
  970. return $this->responseController->makeResponse(true, 'El ID de usuario no fue encriptado correctamente.', [], 400);
  971. }
  972. $usr = DB::table('S002V01TUSUA')->where([
  973. ['USUA_NULI', '=', $line],
  974. ['USUA_IDUS', '=', $idUser]
  975. ])->first();
  976. if(is_null($usr)){
  977. return $this->responseController->makeResponse(true, 'El usuario que realizó la petición no existe.', [], 404);
  978. }
  979. $idEmployee = $this->encryptionController->decrypt($idEmployee);
  980. if(!$idEmployee){
  981. return $this->responseController->makeResponse(true, 'El ID del empleado no fue encriptado correctamente.', [], 400);
  982. }
  983. $sub = DB::table('S002V01TPERS')->where([
  984. ['PERS_NULI', '=', $line],
  985. ['PERS_IDPE', '=', $idEmployee],
  986. ['PERS_TICO', '=', 'Interno'],
  987. ])->first();
  988. if(is_null($sub)){
  989. return $this->responseController->makeResponse(true, 'El empleado solicitado no existe.', [], 404);
  990. }
  991. $contracts = DB::table('S002V01TCONT')->select([
  992. 'CONT_NOCO AS CONTRACT_NAME',
  993. 'CONT_FEIN AS START_DATE',
  994. 'CONT_FEFI AS END_DATE',
  995. 'CONT_COST AS COST',
  996. ])->where([
  997. ['CONT_NULI', '=', $line],
  998. ['CONT_IDEM', '=', $idEmployee],
  999. ['CONT_TIPO', '=', 'Interno'],
  1000. ])->get()->all();
  1001. $now = $this->functionsController->now();
  1002. $nowStr = $now->toDateTimeString();
  1003. $actions = DB::getQueryLog();
  1004. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  1005. $idac = $this->functionsController->registerActivity(
  1006. $line,
  1007. 'S002V01M11GPRS',
  1008. 'S002V01F02ACEM',
  1009. 'S002V01P01HCEM',
  1010. 'Consulta',
  1011. "El usuario $name (" . $usr->USUA_IDUS . ") consultó los contratos del empleado #$idEmployee.",
  1012. $idUser,
  1013. $nowStr,
  1014. 'S002V01S02GEPE'
  1015. );
  1016. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $line);
  1017. return $this->responseController->makeResponse(false, 'EXITO', $contracts);
  1018. }
  1019. public function getInterventionsByEmployee($idEmployee, $idUser, $line) {
  1020. DB::enableQueryLog();
  1021. $idUser = $this->encryptionController->decrypt($idUser);
  1022. if(!$idUser){
  1023. return $this->responseController->makeResponse(true, 'El ID de usuario no fue encriptado correctamente.', [], 400);
  1024. }
  1025. $usr = DB::table('S002V01TUSUA')->where([
  1026. ['USUA_NULI', '=', $line],
  1027. ['USUA_IDUS', '=', $idUser]
  1028. ])->first();
  1029. if(is_null($usr)){
  1030. return $this->responseController->makeResponse(true, 'El usuario que realizó la petición no existe.', [], 404);
  1031. }
  1032. $idEmployee = $this->encryptionController->decrypt($idEmployee);
  1033. if(!$idEmployee){
  1034. return $this->responseController->makeResponse(true, 'El ID del empleado no fue encriptado correctamente.', [], 400);
  1035. }
  1036. $employee = DB::table('S002V01TPERS')->where([
  1037. ['PERS_NULI', '=', $line],
  1038. ['PERS_IDPE', '=', $idEmployee],
  1039. ])->first();
  1040. if(is_null($employee)){
  1041. return $this->responseController->makeResponse(true, 'El empleado solicitado no existe.', [], 404);
  1042. }
  1043. $preventiveOrders = DB::select("
  1044. SELECT
  1045. DISTINCT(S002V01TOTPR.OTPR_IDOT) AS IDORDEN,
  1046. S002V01TOTPR.OTPR_DEIN AS DESCRIPCION,
  1047. 'Preventivo' AS TIPOORDEN
  1048. FROM
  1049. S002V01TOTPR,
  1050. JSON_TABLE(OTPR_OPPR, '$[*]' COLUMNS(
  1051. OTPR_OPPR_ID INT PATH '$.ID',
  1052. OTPR_OPPR_TYPE VARCHAR(100) PATH '$.TYPE'
  1053. )) OPPR
  1054. WHERE
  1055. (OTPR_OPPR_ID = :emp_id AND OTPR_OPPR_TYPE = 'EM')
  1056. OR (OTPR_OPPR_ID = :sub_id AND OTPR_OPPR_TYPE = 'SU')
  1057. OR (OTPR_OPPR_ID = :team_id AND OTPR_OPPR_TYPE = 'EQ')
  1058. ", [
  1059. 'emp_id' => $employee->PERS_IDPE,
  1060. 'sub_id' => $employee->PERS_IDPS,
  1061. 'team_id' => $employee->PERS_EQTR
  1062. ]);
  1063. //PENDIENTE MANTENIMIENTO CORRECTIVO
  1064. $ordersFN = [];
  1065. $relatedOrders = [];
  1066. foreach($preventiveOrders as $order){
  1067. if(!in_array($order->IDORDEN, $relatedOrders)){
  1068. $relatedOrders[] = $order->IDORDEN;
  1069. $order->IDORDEN = $this->encryptionController->encrypt($order->IDORDEN);
  1070. $ordersFN[] = $order;
  1071. }
  1072. }
  1073. $now = $this->functionsController->now();
  1074. $nowStr = $now->toDateTimeString();
  1075. $actions = DB::getQueryLog();
  1076. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  1077. $idac = $this->functionsController->registerActivity(
  1078. $line,
  1079. 'S002V01M11GPRS',
  1080. 'S002V01F01GEPE',
  1081. 'S002V01P03DEEM',
  1082. 'Consulta',
  1083. "El usuario $name (" . $usr->USUA_IDUS . ") consultó las órdenes de trabajo relacionadas al empleado #$idEmployee.",
  1084. $idUser,
  1085. $nowStr,
  1086. 'S002V01S02GEPE'
  1087. );
  1088. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $line);
  1089. return $this->responseController->makeResponse(false, 'EXITO', $ordersFN);
  1090. }
  1091. }