EmployeeController.php 87 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961
  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 ElephantIO\Client;
  12. use stdClass;
  13. use Throwable;
  14. class EmployeeController extends Controller{
  15. private $responseController;
  16. private $encryptionController;
  17. private $documentManagementController;
  18. private $functionsController;
  19. public function __construct(){
  20. $this->responseController = new ResponseController();
  21. $this->encryptionController = new EncryptionController();
  22. $this->documentManagementController = new DocumentManagementController();
  23. $this->functionsController = new FunctionsController();
  24. }
  25. public function getConsultOfEmployees($idUser, $line) {
  26. DB::enableQueryLog();
  27. $idUser = $this->encryptionController->decrypt($idUser);
  28. if(!$idUser){
  29. return $this->responseController->makeResponse(true, 'El ID del usuario que realizó la solicitud no está encriptado correctamente', [], 400);
  30. }
  31. $usr = DB::table('S002V01TUSUA')->where([
  32. ['USUA_NULI', '=', $line],
  33. ['USUA_IDUS', '=', $idUser],
  34. ])->first();
  35. if(is_null($usr)){
  36. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado', [], 404);
  37. }
  38. $employees = DB::table('S002V01TPERS')->select([
  39. 'PERS_IDPE AS ID_EMPLOYEE',
  40. DB::raw('TRIM(CONCAT(USUA_NOMB, " " , USUA_APPA, " ", COALESCE(USUA_APMA,""))) as NAME'),
  41. 'PERS_TICO AS CONTRACT_TYPE',
  42. 'PERS_ESPE AS SPECIALITY',
  43. 'PERS_EQTR AS TEAM_ID',
  44. 'EQMA_NOMB AS TEAM_NAME',
  45. 'PERS_FERE AS REGISTER_DATE',
  46. 'PERS_USRE AS REGISTERED_BY_USER',
  47. 'PERS_FEMO AS UPDATE_DATE',
  48. 'PERS_USMO AS UPDATED_BY_USER',
  49. 'PERS_ESTA AS STATUS'
  50. ])->join('S002V01TUSUA', 'PERS_IDUS', '=', 'USUA_IDUS')
  51. ->leftJoin('S002V01TEQMA', 'PERS_EQTR', '=', 'EQMA_IDEQ')
  52. ->where('USUA_NULI', '=', $line)->get()->all();
  53. $now = $this->functionsController->now();
  54. $nowStr = $now->toDateTimeString();
  55. $actions = DB::getQueryLog();
  56. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  57. $idac = $this->functionsController->registerActivity(
  58. $line,
  59. 'S002V01M11GPRS',
  60. 'S002V01F01GEPE',
  61. 'S002V01P01COEM',
  62. 'Consulta',
  63. "El usuario $name (" . $usr->USUA_IDUS . ") consultó los empleados registrados.",
  64. $idUser,
  65. $nowStr,
  66. 'S002V01S02GEPE'
  67. );
  68. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $line);
  69. return $this->responseController->makeResponse(false, 'EXITO', $employees);
  70. }
  71. public function getConsultOfEmployeesForLeader($idEmployee, $idUser, $line) {
  72. DB::enableQueryLog();
  73. $idUser = $this->encryptionController->decrypt($idUser);
  74. if(!$idUser){
  75. return $this->responseController->makeResponse(true, 'El ID del usuario que realizó la solicitud no está encriptado correctamente', [], 400);
  76. }
  77. try {
  78. $idEmployee = $this->encryptionController->decrypt($idEmployee);
  79. } catch (Throwable $th) {
  80. return $this->responseController->makeResponse(true, 'El ID del empleado no se encriptó correctamente', [], 400);
  81. }
  82. $usr = DB::table('S002V01TUSUA')->where([
  83. ['USUA_NULI', '=', $line],
  84. ['USUA_IDUS', '=', $idUser],
  85. ])->first();
  86. if(is_null($usr)){
  87. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado', [], 404);
  88. }
  89. if ( is_null($idEmployee) || $idEmployee === '') {
  90. $employees = DB::table('S002V01TPERS')
  91. ->select([
  92. 'PERS_IDPE AS ID_EMPLOYEE',
  93. DB::raw('TRIM(CONCAT(USUA_NOMB, " " , USUA_APPA, " ", COALESCE(USUA_APMA,""))) as NAME'),
  94. 'PERS_TICO AS CONTRACT_TYPE',
  95. 'PERS_ESPE AS SPECIALITY',
  96. 'PERS_EQTR AS TEAM_ID',
  97. 'EQMA_NOMB AS TEAM_NAME',
  98. 'PERS_FERE AS REGISTER_DATE',
  99. 'PERS_USRE AS REGISTERED_BY_USER',
  100. 'PERS_FEMO AS UPDATE_DATE',
  101. 'PERS_USMO AS UPDATED_BY_USER',
  102. ])
  103. ->join('S002V01TUSUA', 'PERS_IDUS', '=', 'USUA_IDUS')
  104. ->leftJoin('S002V01TEQMA', 'PERS_EQTR', '=', 'EQMA_IDEQ')
  105. ->where('PERS_ESTA', '=', 'Activo')
  106. ->where('USUA_NULI', '=', $line)
  107. ->get()
  108. ->all();
  109. } else {
  110. $employees = DB::table('S002V01TPERS')
  111. ->select([
  112. 'PERS_IDPE AS ID_EMPLOYEE',
  113. DB::raw('TRIM(CONCAT(USUA_NOMB, " " , USUA_APPA, " ", COALESCE(USUA_APMA,""))) as NAME'),
  114. 'PERS_TICO AS CONTRACT_TYPE',
  115. 'PERS_ESPE AS SPECIALITY',
  116. 'PERS_EQTR AS TEAM_ID',
  117. 'EQMA_NOMB AS TEAM_NAME',
  118. 'PERS_FERE AS REGISTER_DATE',
  119. 'PERS_USRE AS REGISTERED_BY_USER',
  120. 'PERS_FEMO AS UPDATE_DATE',
  121. 'PERS_USMO AS UPDATED_BY_USER',
  122. ])
  123. ->join('S002V01TUSUA', 'PERS_IDUS', '=', 'USUA_IDUS')
  124. ->leftJoin('S002V01TEQMA', 'PERS_EQTR', '=', 'EQMA_IDEQ')
  125. ->where('PERS_ESTA', '=', 'Activo')
  126. ->where('USUA_NULI', '=', $line)
  127. ->where('PERS_IDPE', '!=', $idEmployee)
  128. ->get()
  129. ->all();
  130. }
  131. $now = $this->functionsController->now();
  132. $nowStr = $now->toDateTimeString();
  133. $actions = DB::getQueryLog();
  134. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  135. $idac = $this->functionsController->registerActivity(
  136. $line,
  137. 'S002V01M11GPRS',
  138. 'S002V01F01GEPE',
  139. 'S002V01P01COEM',
  140. 'Consulta',
  141. "El usuario $name (" . $usr->USUA_IDUS . ") consultó los empleados registrados.",
  142. $idUser,
  143. $nowStr,
  144. 'S002V01S02GEPE'
  145. );
  146. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $line);
  147. return $this->responseController->makeResponse(false, 'EXITO', $employees);
  148. }
  149. public function storeEmployee(Request $request) {
  150. DB::enableQueryLog();
  151. $request['RFC'] = $this->encryptionController->decrypt($request->RFC) ? $this->encryptionController->decrypt($request->RFC) : 'ENC_ERR';
  152. $request['TAX'] = $this->encryptionController->decrypt($request->TAX) ? $this->encryptionController->decrypt($request->TAX) : 'ENC_ERR';
  153. $request['CONTACT_NAME'] = $this->encryptionController->decrypt($request->CONTACT_NAME) ? $this->encryptionController->decrypt($request->CONTACT_NAME) : 'ENC_ERR';
  154. $request['CONTACT_TELEPHONE'] = $this->encryptionController->decrypt($request->CONTACT_TELEPHONE) ? $this->encryptionController->decrypt($request->CONTACT_TELEPHONE) : 'ENC_ERR';
  155. $request['CONTACT_LADA'] = $this->encryptionController->decrypt($request->CONTACT_LADA) ? $this->encryptionController->decrypt($request->CONTACT_LADA) : 'ENC_ERR';
  156. $request['CONTACT_ADDRESS'] = $this->encryptionController->decrypt($request->CONTACT_ADDRESS) ? $this->encryptionController->decrypt($request->CONTACT_ADDRESS) : 'ENC_ERR';
  157. $validator = Validator::make($request->all(), [
  158. 'id_user' => 'required|string',
  159. 'linea' => 'required|integer',
  160. 'USER_ID' => 'required|string',
  161. 'WORKTEAM_ID' => 'required|string',
  162. 'CONTRACT_TYPE' => 'required|string|in:Subcontratista,Interno',
  163. 'SUBCONTRATIST_ID' => 'required_if:CONTRACT_TYPE,=,Subcontratista|string',
  164. 'SPECIALITY' => 'required|string|max:75',
  165. 'FOREIGNER' => 'required|string|in:Si,No',
  166. 'RFC' => 'required_if:FOREIGNER,=,No|string|max:13',
  167. 'TAX' => 'required_if:FOREIGNER,=,Si|string|max:13',
  168. 'COUNTRY' => 'required|string|max:75',
  169. 'FEDERAL_ENTITY' => 'required|string|max:75',
  170. 'CITY' => 'string|max:75',
  171. 'TOWN' => 'string|max:75',
  172. 'SUBURB' => 'required|string|max:75',
  173. 'POSTAL_CODE' => 'required|string|max:5',
  174. 'STREET' => 'required|string|max:150',
  175. 'EXTERIOR_NUMBER' => 'required|integer',
  176. 'INTERIOR_NUMBER' => 'integer',
  177. 'CONTACT_NAME' => 'required|string|max:150',
  178. 'CONTACT_LADA' => 'required|string|max:10',
  179. 'CONTACT_TELEPHONE' => 'required|string|max:11',
  180. 'CONTACT_ADDRESS' => 'required|string|max:250',
  181. 'ATTACHED' => 'required|json',
  182. ]);
  183. if($validator->fails()){
  184. return $this->responseController->makeResponse(
  185. TRUE,
  186. 'ERR_PERSONAL_REG001: Uno o más errores encontrados',
  187. $this->responseController->makeErrors($validator->errors()->messages()),
  188. 400
  189. );
  190. }
  191. $form = $request->all();
  192. foreach($form as $k=>$v){
  193. if($k == 'INTERIOR_NUMBER' && $v == '0'){
  194. unset($form[$k]);
  195. }else if($v == '-'){
  196. unset($form[$k]);
  197. }
  198. }
  199. $idUser = $this->encryptionController->decrypt($form['id_user']);
  200. if(!$idUser){
  201. return $this->responseController->makeResponse(true, 'El ID de usuario no fue encriptado correctamente.', [], 400);
  202. }
  203. $usr = DB::table('S002V01TUSUA')->where([
  204. ['USUA_NULI', '=', $form['linea']],
  205. ['USUA_IDUS', '=', $idUser]
  206. ])->first();
  207. if(is_null($usr)){
  208. return $this->responseController->makeResponse(true, 'El usuario que realizó la petición no existe.', [], 404);
  209. }
  210. $idUserReg = $this->encryptionController->decrypt($form['USER_ID']);
  211. if(!$idUserReg){
  212. return $this->responseController->makeResponse(true, 'El ID del usuario seleccionado no fue encriptado correctamente.', [], 400);
  213. }else if($idUser == $idUserReg){
  214. return $this->responseController->makeResponse(true, 'El usuario no puede registrarse a sí mismo.', [], 400);
  215. }
  216. $usrReg = DB::table('S002V01TUSUA')->where([
  217. ['USUA_NULI', '=', $form['linea']],
  218. ['USUA_IDUS', '=', $idUserReg]
  219. ])->first();
  220. if(is_null($usrReg)){
  221. return $this->responseController->makeResponse(true, 'El usuario seleccionado no existe.', [], 404);
  222. }
  223. $idTeam = null;
  224. if(isset($form['WORKTEAM_ID'])){
  225. $idTeam = $this->encryptionController->decrypt($form['WORKTEAM_ID']);
  226. if(!$idTeam){
  227. return $this->responseController->makeResponse(true, 'El ID del equipo seleccionado encriptado correctamente.', [], 400);
  228. }
  229. $team = DB::table('S002V01TEQMA')->where([
  230. ['EQMA_IDEQ', '=', $idTeam],
  231. ['EQMA_NULI', '=', $form['linea']],
  232. ])->first();
  233. if(is_null($team)){
  234. return $this->responseController->makeResponse(true, 'El equipo seleccionado no existe.', [], 404);
  235. }else if($team->EQMA_ESTA == 'Eliminado'){
  236. return $this->responseController->makeResponse(true, 'El equipo seleccionado está eliminado.', [], 404);
  237. }
  238. }
  239. $idSubcontratist = null;
  240. if(isset($form['SUBCONTRATIST_ID'])){
  241. $idSubcontratist = $this->encryptionController->decrypt($form['SUBCONTRATIST_ID']);
  242. if(!$idSubcontratist){
  243. return $this->responseController->makeResponse(true, 'El ID del subcontratista seleccionado no fue encriptado correctamente.', [], 400);
  244. }
  245. $subcontratist = DB::table('S002V01TPESU')->where([
  246. ['PESU_IDPS', '=', $idSubcontratist],
  247. ['PESU_NULI', '=', $form['linea']],
  248. ])->first();
  249. if(is_null($subcontratist)){
  250. return $this->responseController->makeResponse(true, 'El subcontratista seleccionado no existe.', [], 404);
  251. }
  252. }
  253. $rfcx = null;
  254. if(isset($form['RFC']) && $form['FOREIGNER'] == 'No' && $form['RFC'] == 'ENC_ERR'){
  255. return $this->responseController->makeResponse(true, 'El RFC no fue encriptado correctamente.', [], 400);
  256. }else if(isset($form['RFC']) && $form['RFC'] != 'ENC_ERR'){
  257. $rfcx = $form['RFC'];
  258. }
  259. $taid = null;
  260. if(isset($form['TAX']) && $form['FOREIGNER'] == 'Si' && $form['TAX'] == 'ENC_ERR'){
  261. return $this->responseController->makeResponse(true, 'El TAX ID no fue encriptado correctamente.', [], 400);
  262. }else if(isset($form['TAX']) && $form['TAX'] != 'ENC_ERR'){
  263. $taid = $form['TAX'];
  264. }
  265. $country = DB::table('S002V01TPAIS')->where([
  266. ['PAIS_NULI', '=', $form['linea']],
  267. ['PAIS_IDPA', '=', $form['COUNTRY']],
  268. ])->first();
  269. if(is_null($country)){
  270. return $this->responseController->makeResponse(true, 'El país seleccionado no existe.', [], 404);
  271. }
  272. $colo = null;
  273. $ciud = null;
  274. $loca = null;
  275. if($form['COUNTRY'] == 'MEX' || $form['COUNTRY'] == 'USA' || $form['COUNTRY'] == 'CAN'){
  276. $state = DB::table('S002V01TESTA')->where([
  277. ['ESTA_NULI', '=', $form['linea']],
  278. ['ESTA_COES', '=', $form['FEDERAL_ENTITY']],
  279. ['ESTA_COPA', '=', $form['COUNTRY']],
  280. ])->first();
  281. if(is_null($state)){
  282. return $this->responseController->makeResponse(true, 'El estado seleccionado no existe.', [], 404);
  283. }
  284. if($form['COUNTRY'] == 'MEX'){
  285. if(isset($form['CITY'])){
  286. $city = DB::table('S002V01TMUNI')->where([
  287. ['MUNI_NULI', '=', $form['linea']],
  288. ['MUNI_COMU', '=', $form['CITY']],
  289. ['MUNI_COES', '=', $form['FEDERAL_ENTITY']],
  290. ])->first();
  291. if(is_null($city)){
  292. return $this->responseController->makeResponse(true, 'El municipio seleccionado no existe.', [], 404);
  293. }else{
  294. $ciud = $form['CITY'];
  295. }
  296. }
  297. if(isset($form['TOWN'])){
  298. $town = DB::table('S002V01TLOCA')->where([
  299. ['LOCA_NULI', '=', $form['linea']],
  300. ['LOCA_COLO', '=', $form['TOWN']],
  301. ['LOCA_COES', '=', $form['FEDERAL_ENTITY']],
  302. ])->first();
  303. if(is_null($town)){
  304. return $this->responseController->makeResponse(true, 'La localidad seleccionada no existe.', [], 404);
  305. }else{
  306. $loca = $form['TOWN'];
  307. }
  308. }
  309. $setting = DB::table('S002V01TCOLO')->where([
  310. ['COLO_NULI', '=', $form['linea']],
  311. ['COLO_COCO', '=', $form['SUBURB']],
  312. ['COLO_COPO', '=', $form['POSTAL_CODE']],
  313. ])->first();
  314. if(is_null($setting)){
  315. return $this->responseController->makeResponse(true, 'La colonia seleccionada no existe.', [], 404);
  316. }else{
  317. $colo = $form['SUBURB'];
  318. }
  319. $zipCode = DB::table('S002V01TCOPO')->where([
  320. ['COPO_NULI', '=', $form['linea']],
  321. ['COPO_COPO', '=', $form['POSTAL_CODE']],
  322. ['COPO_COES', '=', $form['FEDERAL_ENTITY']],
  323. ])->first();
  324. if(is_null($zipCode)){
  325. return $this->responseController->makeResponse(true, 'El código postal seleccionado no existe.', [], 404);
  326. }
  327. }else{
  328. $colo = $form['SUBURB'];
  329. $ciud = $form['CITY'];
  330. $loca = isset($form['TOWN']) ? $form['TOWN'] : null;
  331. }
  332. }else{
  333. $colo = $form['SUBURB'];
  334. $ciud = $form['CITY'];
  335. $loca = isset($form['TOWN']) ? $form['TOWN'] : null;
  336. }
  337. $subArr = DB::table('S002V01TPERS')->where('PERS_NULI', '=', $form['linea'])->where(function(Builder $query) use ($form, $idUserReg) {
  338. $query->where('PERS_IDUS', '=', $idUserReg)
  339. ->orWhere('PERS_XRFC', '=', $form['RFC']);
  340. })->where('PERS_ESTA', '=', 'Activo')->get()->all();
  341. if(count($subArr) > 0){
  342. return $this->responseController->makeResponse(true, 'La razón social, el RFC o el correo electrónico ya fueron registrados.', [], 401);
  343. }
  344. $ladasValues = [];
  345. $ladas = DB::table('S002V01TPAIS')->select([
  346. 'PAIS_LADA AS LADA'
  347. ])->where('PAIS_NULI', '=', $form['linea'])->get()->all();
  348. foreach($ladas as $lada){
  349. if($lada->LADA != '' && $lada->LADA != '0'){
  350. $ladasValues[] = $lada->LADA;
  351. }
  352. }
  353. if($form['CONTACT_LADA'] == 'ENC_ERR'){
  354. return $this->responseController->makeResponse(true, "La lada del contacto de emergencia no fue encriptada correctamente.", [], 400);
  355. }else if(!in_array($form['CONTACT_LADA'], $ladasValues)){
  356. return $this->responseController->makeResponse(true, "La lada $form[CONTACT_LADA] no está relacionada a ningún país.", [], 400);
  357. }else if($form['CONTACT_TELEPHONE'] == 'ENC_ERR'){
  358. return $this->responseController->makeResponse(true, 'El número telefónico del contacto de emergencia no fue encriptado correctamente.', [], 400);
  359. }
  360. if($form['CONTACT_ADDRESS'] == 'ENC_ERR'){
  361. return $this->responseController->makeResponse(true, "La dirección del contacto de emergencia no fue encriptada correctamente.", [], 400);
  362. }
  363. if($form['CONTACT_NAME'] == 'ENC_ERR'){
  364. return $this->responseController->makeResponse(true, "El nombre del contacto de emergencia no fue encriptada correctamente.", [], 400);
  365. }
  366. $docsArr = json_decode($form['ATTACHED'], true);
  367. if(count($docsArr) == 0){
  368. return $this->responseController->makeResponse(true, "El arreglo de archivos adjuntos está vacío.", [], 400);
  369. }
  370. $authCont = 0;
  371. foreach($docsArr as $doc){
  372. if($doc['type'] == 'AU') $authCont++;
  373. }
  374. if($authCont < 1){
  375. return $this->responseController->makeResponse(true, "El arreglo de archivos adjuntos debe contener al menos un documento de autorización.", [], 400);
  376. }
  377. $finalDocsArr = [];
  378. foreach($docsArr as $doc){
  379. $idFileDec = $this->encryptionController->decrypt($doc['id']);
  380. $tempFile = DB::table('S002V01TARTE')->where([
  381. ['ARTE_NULI', '=', $form['linea']],
  382. ['ARTE_IDAR', '=', $idFileDec],
  383. ])->first();
  384. if(is_null($tempFile)){
  385. return $this->responseController->makeResponse(true, "El archivo $doc[name] no está registrado.", [], 404);
  386. }
  387. $classifications = ["AU" => "IN", "OF" => "CO", "CE" => "CE"];
  388. $cldo = $classifications[$doc['type']];
  389. $finalFile = $this->documentManagementController->moveFinalFile($form['linea'], "GPRS", $cldo, $tempFile, $idUser);
  390. if(!$finalFile[0]){
  391. return $this->responseController->makeResponse(true, $finalFile[1], [], 400);
  392. }else{
  393. $finalDocsArr[] = [
  394. 'CODE' => $finalFile[1],
  395. 'TYPE' => $doc['type']
  396. ];
  397. }
  398. }
  399. $now = $this->functionsController->now();
  400. $nowStr = $now->toDateTimeString();
  401. $doas = json_encode($finalDocsArr);
  402. $nuin = isset($form['INTERIOR_NUMBER']) ? $form['INTERIOR_NUMBER'] : null;
  403. $employeeID = DB::table('S002V01TPERS')->insertGetId([
  404. 'PERS_NULI' => $form['linea'],
  405. 'PERS_IDUS' => $idUserReg,
  406. 'PERS_EQTR' => $idTeam,
  407. 'PERS_TICO' => $form['CONTRACT_TYPE'],
  408. 'PERS_IDPS' => $idSubcontratist,
  409. 'PERS_ESPE' => $form['SPECIALITY'],
  410. 'PERS_EXTR' => $form['FOREIGNER'],
  411. 'PERS_XRFC' => $rfcx,
  412. 'PERS_TAID' => $taid,
  413. 'PERS_IDPA' => $form['COUNTRY'],
  414. 'PERS_ENFE' => $form['FEDERAL_ENTITY'],
  415. 'PERS_CIUD' => $ciud,
  416. 'PERS_LOCA' => $loca,
  417. 'PERS_COLO' => $colo,
  418. 'PERS_COPO' => $form['POSTAL_CODE'],
  419. 'PERS_CALL' => $form['STREET'],
  420. 'PERS_NUEX' => $form['EXTERIOR_NUMBER'],
  421. 'PERS_NUIN' => $nuin,
  422. 'PERS_NOCE' => $form['CONTACT_NAME'],
  423. 'PERS_LCEM' => $form['CONTACT_LADA'],
  424. 'PERS_NUTC' => $form['CONTACT_TELEPHONE'],
  425. 'PERS_DCEM' => $form['CONTACT_ADDRESS'],
  426. 'PERS_DOAS' => $doas,
  427. 'PERS_USRE' => $idUser,
  428. 'PERS_FERE' => $nowStr,
  429. ]);
  430. if ( !is_null($form['EMPLOYEE_BOSS']) && $form['EMPLOYEE_BOSS'] !== '') {
  431. try {
  432. $validateRegister = DB::table('S002V01TCREM')->insert([
  433. 'CREM_IDES' => $employeeID,
  434. 'CREM_IDEJ' => $form['EMPLOYEE_BOSS'],
  435. 'CREM_NULI' => $form['linea'],
  436. 'CREM_USRE' => $idUser,
  437. 'CREM_FERE' => $nowStr,
  438. 'CREM_FEAR' => DB::raw('CURRENT_TIMESTAMP'),
  439. ]);
  440. } catch (Throwable $th) {
  441. return $this->responseController->makeResponse(true, "Ocurrió un error al registrar el cronograma.", [], 400);
  442. }
  443. if (!$validateRegister) {
  444. return $this->responseController->makeResponse(true, "No se pudo registrar los empleados en el cronograma.", [], 400);
  445. }
  446. }
  447. $actions = DB::getQueryLog();
  448. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  449. $nameEmp = $this->functionsController->joinName($usrReg->USUA_NOMB, $usrReg->USUA_APPA, $usrReg->USUA_APMA);
  450. $idac = $this->functionsController->registerActivity(
  451. $form['linea'],
  452. 'S002V01M11GPRS',
  453. 'S002V01F01GEPE',
  454. 'S002V01P02REEM',
  455. 'Registro',
  456. "El usuario $name (" . $usr->USUA_IDUS . ") registró al empleado " . $nameEmp . "($employeeID).",
  457. $idUser,
  458. $nowStr,
  459. 'S002V01S02GEPE'
  460. );
  461. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $form['linea']);
  462. return $this->responseController->makeResponse(false, 'EXITO');
  463. }
  464. public function getEmployeeById($idEmployee, $idUser, $line) {
  465. DB::enableQueryLog();
  466. $idUser = $this->encryptionController->decrypt($idUser);
  467. if(!$idUser){
  468. return $this->responseController->makeResponse(true, 'El ID del usuario que realizó la solicitud no está encriptado correctamente', [], 400);
  469. }
  470. $usr = DB::table('S002V01TUSUA')->where([
  471. ['USUA_NULI', '=', $line],
  472. ['USUA_IDUS', '=', $idUser],
  473. ])->first();
  474. if(is_null($usr)){
  475. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado', [], 404);
  476. }
  477. $idEmployee = $this->encryptionController->decrypt($idEmployee);
  478. if(!$idEmployee){
  479. return $this->responseController->makeResponse(true, 'El ID del empleado solicitado no está encriptado correctamente', [], 400);
  480. }
  481. $employee = DB::table('S002V01TPERS')->select([
  482. 'PERS_IDPE AS ID_EMPLOYEE',
  483. DB::raw('TRIM(CONCAT(USUA_NOMB, " " , USUA_APPA, " ", COALESCE(USUA_APMA,""))) as EMPLOYEE_NAME'),
  484. 'PERS_IDUS AS USER_ID',
  485. 'EQMA_IDEQ AS WORKTEAM_ID',
  486. 'EQMA_NOMB AS WORKTEAM_NAME',
  487. 'PERS_TICO AS CONTRACT_TYPE',
  488. 'PERS_IDPS AS SUBCONTRATIST_ID',
  489. 'PESU_RASO AS SUBCONTRATIST_NAME',
  490. 'PESU_REFI AS TAX_REGIME',
  491. 'REFI_DRFI AS TAX_REGIME_DESCRIPTION',
  492. 'PERS_ESPE AS SPECIALITY',
  493. 'PERS_EXTR AS FOREIGNER',
  494. 'PERS_XRFC AS RFC',
  495. 'PERS_TAID AS TAX_ID',
  496. 'PERS_IDPA AS COUNTRY',
  497. 'PERS_ENFE AS STATE',
  498. 'PERS_CIUD AS CITY',
  499. 'PERS_LOCA AS TOWN',
  500. 'PERS_COLO AS SETTING',
  501. 'PERS_COPO AS ZIP_CODE',
  502. 'PERS_CALL AS STREET',
  503. 'PERS_NUEX AS EXTERIOR_NUMBER',
  504. 'PERS_NUIN AS INTERIOR_NUMBER',
  505. 'PERS_NOCE AS CONTACT_NAME',
  506. 'PERS_LCEM AS CONTACT_LADA',
  507. 'PERS_NUTC AS CONTACT_TELEPHONE',
  508. 'PERS_DCEM AS CONTACT_ADDRESS',
  509. 'PERS_DOAS AS DOCUMENTS',
  510. 'PERS_ESTA AS STATUS',
  511. 'PERS_USRE AS USRREG',
  512. 'PERS_USMO AS USRMOD',
  513. 'PERS_FERE AS FECREG',
  514. 'PERS_FEMO AS FECMOD',
  515. ])->leftJoin('S002V01TEQMA', 'EQMA_IDEQ', '=', 'PERS_EQTR')
  516. ->leftJoin('S002V01TPESU', 'PESU_IDPS', '=', 'PERS_IDPS')
  517. ->leftJoin('S002V01TREFI', 'REFI_CRFI', '=', 'PESU_REFI')
  518. ->join('S002V01TUSUA', 'PERS_IDUS', '=', 'USUA_IDUS')->where([
  519. ['PERS_NULI', '=', $line],
  520. ['PERS_IDPE', '=', $idEmployee],
  521. ])->first();
  522. $attached = [];
  523. $docsArr = json_decode($employee->DOCUMENTS, true);
  524. foreach($docsArr as $doc){
  525. $docArr = explode('=', $doc['CODE']);
  526. $codeArr = explode('-', $docArr[0]);
  527. $document = DB::table('S002V01TAFAL')->select([
  528. 'AFAL_NOAR AS FILE_NAME',
  529. 'AFAL_EXTE AS FILE_EXTENSION',
  530. 'AFAL_TAMA AS FILE_SIZE',
  531. 'AFAL_ESTA AS FILE_STATUS'
  532. ])->where([
  533. ['AFAL_NULI', '=', $line],
  534. ['AFAL_COMO', '=', $codeArr[1]],
  535. ['AFAL_CLDO', '=', $codeArr[2]],
  536. ['AFAL_FECR', '=', $codeArr[3]],
  537. ['AFAL_NUSE', '=', $codeArr[4]],
  538. ['AFAL_NUVE', '=', $docArr[1]],
  539. ])->first();
  540. $document->FILE_ID = $this->encryptionController->encrypt($doc['CODE']);
  541. $document->FILE_TYPE = $doc['TYPE'];
  542. $attached[] = $document;
  543. }
  544. $employee->DOCUMENTS = json_encode($attached);
  545. $country = DB::table('S002V01TPAIS')->where([
  546. ['PAIS_NULI', '=', $line],
  547. ['PAIS_IDPA', '=', $employee->COUNTRY]
  548. ])->first();
  549. $state = DB::table('S002V01TESTA')->where([
  550. ['ESTA_NULI', '=', $line],
  551. ['ESTA_COES', '=', $employee->STATE],
  552. ['ESTA_COPA', '=', $employee->COUNTRY]
  553. ])->first();
  554. $city = DB::table('S002V01TMUNI')->where([
  555. ['MUNI_NULI', '=', $line],
  556. ['MUNI_COMU', '=', $employee->CITY],
  557. ['MUNI_COES', '=', $employee->STATE],
  558. ])->first();
  559. $town = DB::table('S002V01TLOCA')->where([
  560. ['LOCA_NULI', '=', $line],
  561. ['LOCA_COLO', '=', $employee->TOWN],
  562. ['LOCA_COES', '=', $employee->STATE],
  563. ])->first();
  564. $setting = DB::table('S002V01TCOLO')->where([
  565. ['COLO_NULI', '=', $line],
  566. ['COLO_COCO', '=', $employee->SETTING],
  567. ['COLO_COPO', '=', $employee->ZIP_CODE],
  568. ])->first();
  569. $employee->COUNTRY = $country->PAIS_NOMB . " (" . $employee->COUNTRY . ")";
  570. $employee->STATE = is_null($state) ? $employee->STATE : $state->ESTA_NOES . " (" . $employee->STATE . ")";
  571. $employee->CITY = is_null($city) ? $employee->CITY : $city->MUNI_NOMU . " (" . $employee->CITY . ")";
  572. $employee->TOWN = is_null($town) ? $employee->TOWN : $town->LOCA_NOLO . " (" . $employee->TOWN . ")";
  573. $employee->SETTING = is_null($setting) ? $employee->SETTING : $setting->COLO_NOCO . " (" . $employee->SETTING . ")";
  574. $usrReg = DB::table('S002V01TUSUA')->where([
  575. ['USUA_NULI', '=', $line],
  576. ['USUA_IDUS', '=', $employee->USRREG]
  577. ])->first();
  578. $nameReg = $this->functionsController->joinName($usrReg->USUA_NOMB, $usrReg->USUA_APPA, $usrReg->USUA_APMA);
  579. $employee->USRREG = "$nameReg (" . $employee->USRREG . ")";
  580. if(!is_null($employee->USRMOD)){
  581. $usrMod = DB::table('S002V01TUSUA')->where([
  582. ['USUA_NULI', '=', $line],
  583. ['USUA_IDUS', '=', $employee->USRMOD]
  584. ])->first();
  585. $nameMod = $this->functionsController->joinName($usrMod->USUA_NOMB, $usrMod->USUA_APPA, $usrMod->USUA_APMA);
  586. $employee->USRMOD = "$nameMod (" . $employee->USRMOD . ")";
  587. }
  588. $now = $this->functionsController->now();
  589. $nowStr = $now->toDateTimeString();
  590. $actions = DB::getQueryLog();
  591. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  592. $idac = $this->functionsController->registerActivity(
  593. $line,
  594. 'S002V01M11GPRS',
  595. 'S002V01F01GEPE',
  596. 'S002V01P03DEEM',
  597. 'Consulta',
  598. "El usuario $name (" . $usr->USUA_IDUS . ") consultó al empleado " . $employee->EMPLOYEE_NAME . " ($idEmployee).",
  599. $idUser,
  600. $nowStr,
  601. 'S002V01S02GEPE'
  602. );
  603. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $line);
  604. return $this->responseController->makeResponse(false, 'EXITO', $employee);
  605. }
  606. public function updateEmployee(Request $request) {
  607. DB::enableQueryLog();
  608. $request['RFC'] = $this->encryptionController->decrypt($request->RFC) ? $this->encryptionController->decrypt($request->RFC) : 'ENC_ERR';
  609. $request['TAX'] = $this->encryptionController->decrypt($request->TAX) ? $this->encryptionController->decrypt($request->TAX) : 'ENC_ERR';
  610. $request['CONTACT_NAME'] = $this->encryptionController->decrypt($request->CONTACT_NAME) ? $this->encryptionController->decrypt($request->CONTACT_NAME) : 'ENC_ERR';
  611. $request['CONTACT_TELEPHONE'] = $this->encryptionController->decrypt($request->CONTACT_TELEPHONE) ? $this->encryptionController->decrypt($request->CONTACT_TELEPHONE) : 'ENC_ERR';
  612. $request['CONTACT_LADA'] = $this->encryptionController->decrypt($request->CONTACT_LADA) ? $this->encryptionController->decrypt($request->CONTACT_LADA) : 'ENC_ERR';
  613. $request['CONTACT_ADDRESS'] = $this->encryptionController->decrypt($request->CONTACT_ADDRESS) ? $this->encryptionController->decrypt($request->CONTACT_ADDRESS) : 'ENC_ERR';
  614. $validator = Validator::make($request->all(), [
  615. 'id_user' => 'required|string',
  616. 'linea' => 'required|integer',
  617. 'id_employee' => 'required|string',
  618. 'USER_ID' => 'required|string',
  619. 'WORKTEAM_ID' => 'required|string',
  620. 'CONTRACT_TYPE' => 'required|string|in:Subcontratista,Interno',
  621. 'SUBCONTRATIST_ID' => 'required_if:CONTRACT_TYPE,=,Subcontratista|string',
  622. 'SPECIALITY' => 'required|string|max:75',
  623. 'FOREIGNER' => 'required|string|in:Si,No',
  624. 'RFC' => 'required_if:FOREIGNER,=,No|string|max:13',
  625. 'TAX' => 'required_if:FOREIGNER,=,Si|string|max:13',
  626. 'COUNTRY' => 'required|string|max:75',
  627. 'FEDERAL_ENTITY' => 'required|string|max:75',
  628. 'CITY' => 'string|max:75',
  629. 'TOWN' => 'string|max:75',
  630. 'SUBURB' => 'required|string|max:75',
  631. 'POSTAL_CODE' => 'required|string|max:5',
  632. 'STREET' => 'required|string|max:150',
  633. 'EXTERIOR_NUMBER' => 'required|integer',
  634. 'INTERIOR_NUMBER' => 'integer',
  635. 'CONTACT_NAME' => 'required|string|max:150',
  636. 'CONTACT_LADA' => 'required|string|max:10',
  637. 'CONTACT_TELEPHONE' => 'required|string|max:11',
  638. 'CONTACT_ADDRESS' => 'required|string|max:250',
  639. 'ATTACHED' => 'required|json',
  640. ]);
  641. if($validator->fails()){
  642. return $this->responseController->makeResponse(
  643. TRUE,
  644. 'ERR_PERSONAL_REG001: Uno o más errores encontrados',
  645. $this->responseController->makeErrors($validator->errors()->messages()),
  646. 400
  647. );
  648. }
  649. $form = $request->all();
  650. foreach($form as $k=>$v){
  651. if($k == 'INTERIOR_NUMBER' && $v == '0'){
  652. unset($form[$k]);
  653. }else if($v == '-'){
  654. unset($form[$k]);
  655. }
  656. }
  657. $idUser = $this->encryptionController->decrypt($form['id_user']);
  658. if(!$idUser){
  659. return $this->responseController->makeResponse(true, 'El ID de usuario no fue encriptado correctamente.', [], 400);
  660. }
  661. $usr = DB::table('S002V01TUSUA')->where([
  662. ['USUA_NULI', '=', $form['linea']],
  663. ['USUA_IDUS', '=', $idUser]
  664. ])->first();
  665. if(is_null($usr)){
  666. return $this->responseController->makeResponse(true, 'El usuario que realizó la petición no existe.', [], 404);
  667. }
  668. $idEmployee = $this->encryptionController->decrypt($form['id_employee']);
  669. if(!$idEmployee){
  670. return $this->responseController->makeResponse(true, 'El ID del empleado que desea actualizar no fue encriptado correctamente.', [], 400);
  671. }
  672. $employee = DB::table('S002V01TPERS')->where([
  673. ['PERS_NULI', '=', $form['linea']],
  674. ['PERS_IDPE', '=', $idEmployee]
  675. ])->first();
  676. if(is_null($employee)){
  677. return $this->responseController->makeResponse(true, 'El empleado que desea actualizar no existe.', [], 404);
  678. }
  679. $idUserReg = $this->encryptionController->decrypt($form['USER_ID']);
  680. if(!$idUserReg){
  681. return $this->responseController->makeResponse(true, 'El ID del usuario seleccionado no fue encriptado correctamente.', [], 400);
  682. }else if($idUser == $idUserReg){
  683. return $this->responseController->makeResponse(true, 'El usuario no puede registrarse a sí mismo.', [], 400);
  684. }
  685. $usrReg = DB::table('S002V01TUSUA')->where([
  686. ['USUA_NULI', '=', $form['linea']],
  687. ['USUA_IDUS', '=', $idUserReg]
  688. ])->first();
  689. if(is_null($usrReg)){
  690. return $this->responseController->makeResponse(true, 'El usuario seleccionado no existe.', [], 404);
  691. }
  692. $idTeam = null;
  693. if(isset($form['WORKTEAM_ID'])){
  694. $idTeam = $this->encryptionController->decrypt($form['WORKTEAM_ID']);
  695. if(!$idTeam){
  696. return $this->responseController->makeResponse(true, 'El ID del equipo seleccionado encriptado correctamente.', [], 400);
  697. }
  698. $team = DB::table('S002V01TEQMA')->where([
  699. ['EQMA_IDEQ', '=', $idTeam],
  700. ['EQMA_NULI', '=', $form['linea']],
  701. ])->first();
  702. if(is_null($team)){
  703. return $this->responseController->makeResponse(true, 'El equipo seleccionado no existe.', [], 404);
  704. }else if($team->EQMA_ESTA == 'Eliminado'){
  705. return $this->responseController->makeResponse(true, 'El equipo seleccionado está eliminado.', [], 404);
  706. }
  707. }
  708. $idSubcontratist = null;
  709. if(isset($form['SUBCONTRATIST_ID'])){
  710. $idSubcontratist = $this->encryptionController->decrypt($form['SUBCONTRATIST_ID']);
  711. if(!$idSubcontratist){
  712. return $this->responseController->makeResponse(true, 'El ID del subcontratista seleccionado no fue encriptado correctamente.', [], 400);
  713. }
  714. $subcontratist = DB::table('S002V01TPESU')->where([
  715. ['PESU_IDPS', '=', $idSubcontratist],
  716. ['PESU_NULI', '=', $form['linea']],
  717. ])->first();
  718. if(is_null($subcontratist)){
  719. return $this->responseController->makeResponse(true, 'El subcontratista seleccionado no existe.', [], 404);
  720. }
  721. }
  722. $rfcx = null;
  723. if(isset($form['RFC']) && $form['FOREIGNER'] == 'No' && $form['RFC'] == 'ENC_ERR'){
  724. return $this->responseController->makeResponse(true, 'El RFC no fue encriptado correctamente.', [], 400);
  725. }else if(isset($form['RFC']) && $form['RFC'] != 'ENC_ERR'){
  726. $rfcx = $form['RFC'];
  727. }
  728. $taid = null;
  729. if(isset($form['TAX']) && $form['FOREIGNER'] == 'Si' && $form['TAX'] == 'ENC_ERR'){
  730. return $this->responseController->makeResponse(true, 'El TAX ID no fue encriptado correctamente.', [], 400);
  731. }else if(isset($form['TAX']) && $form['TAX'] != 'ENC_ERR'){
  732. $taid = $form['TAX'];
  733. }
  734. $country = DB::table('S002V01TPAIS')->where([
  735. ['PAIS_NULI', '=', $form['linea']],
  736. ['PAIS_IDPA', '=', $form['COUNTRY']],
  737. ])->first();
  738. if(is_null($country)){
  739. return $this->responseController->makeResponse(true, 'El país seleccionado no existe.', [], 404);
  740. }
  741. $colo = null;
  742. $ciud = null;
  743. $loca = null;
  744. if($form['COUNTRY'] == 'MEX' || $form['COUNTRY'] == 'USA' || $form['COUNTRY'] == 'CAN'){
  745. $state = DB::table('S002V01TESTA')->where([
  746. ['ESTA_NULI', '=', $form['linea']],
  747. ['ESTA_COES', '=', $form['FEDERAL_ENTITY']],
  748. ['ESTA_COPA', '=', $form['COUNTRY']],
  749. ])->first();
  750. if(is_null($state)){
  751. return $this->responseController->makeResponse(true, 'El estado seleccionado no existe.', [], 404);
  752. }
  753. if($form['COUNTRY'] == 'MEX'){
  754. if(isset($form['CITY'])){
  755. $city = DB::table('S002V01TMUNI')->where([
  756. ['MUNI_NULI', '=', $form['linea']],
  757. ['MUNI_COMU', '=', $form['CITY']],
  758. ['MUNI_COES', '=', $form['FEDERAL_ENTITY']],
  759. ])->first();
  760. if(is_null($city)){
  761. return $this->responseController->makeResponse(true, 'El municipio seleccionado no existe.', [], 404);
  762. }else{
  763. $ciud = $form['CITY'];
  764. }
  765. }
  766. if(isset($form['TOWN'])){
  767. $town = DB::table('S002V01TLOCA')->where([
  768. ['LOCA_NULI', '=', $form['linea']],
  769. ['LOCA_COLO', '=', $form['TOWN']],
  770. ['LOCA_COES', '=', $form['FEDERAL_ENTITY']],
  771. ])->first();
  772. if(is_null($town)){
  773. return $this->responseController->makeResponse(true, 'La localidad seleccionada no existe.', [], 404);
  774. }else{
  775. $loca = $form['TOWN'];
  776. }
  777. }
  778. $setting = DB::table('S002V01TCOLO')->where([
  779. ['COLO_NULI', '=', $form['linea']],
  780. ['COLO_COCO', '=', $form['SUBURB']],
  781. ['COLO_COPO', '=', $form['POSTAL_CODE']],
  782. ])->first();
  783. if(is_null($setting)){
  784. return $this->responseController->makeResponse(true, 'La colonia seleccionada no existe.', [], 404);
  785. }else{
  786. $colo = $form['SUBURB'];
  787. }
  788. $zipCode = DB::table('S002V01TCOPO')->where([
  789. ['COPO_NULI', '=', $form['linea']],
  790. ['COPO_COPO', '=', $form['POSTAL_CODE']],
  791. ['COPO_COES', '=', $form['FEDERAL_ENTITY']],
  792. ])->first();
  793. if(is_null($zipCode)){
  794. return $this->responseController->makeResponse(true, 'El código postal seleccionado no existe.', [], 404);
  795. }
  796. }else{
  797. $colo = $form['SUBURB'];
  798. $ciud = $form['CITY'];
  799. $loca = isset($form['TOWN']) ? $form['TOWN'] : null;
  800. }
  801. }else{
  802. $colo = $form['SUBURB'];
  803. $ciud = $form['CITY'];
  804. $loca = isset($form['TOWN']) ? $form['TOWN'] : null;
  805. }
  806. $subArr = DB::table('S002V01TPERS')->where('PERS_NULI', '=', $form['linea'])->where(function(Builder $query) use ($form, $idUserReg) {
  807. $query->where('PERS_IDUS', '=', $idUserReg)
  808. ->orWhere('PERS_XRFC', '=', $form['RFC']);
  809. })->where([
  810. ['PERS_ESTA', '=', 'Activo'],
  811. ['PERS_IDPE', '!=', $idEmployee],
  812. ])->get()->all();
  813. if(count($subArr) > 0){
  814. return $this->responseController->makeResponse(true, 'La razón social, el RFC o el correo electrónico ya fueron registrados.', [], 401);
  815. }
  816. $ladasValues = [];
  817. $ladas = DB::table('S002V01TPAIS')->select([
  818. 'PAIS_LADA AS LADA'
  819. ])->where('PAIS_NULI', '=', $form['linea'])->get()->all();
  820. foreach($ladas as $lada){
  821. if($lada->LADA != '' && $lada->LADA != '0'){
  822. $ladasValues[] = $lada->LADA;
  823. }
  824. }
  825. if($form['CONTACT_LADA'] == 'ENC_ERR'){
  826. return $this->responseController->makeResponse(true, "La lada del contacto de emergencia no fue encriptada correctamente.", [], 400);
  827. }else if(!in_array($form['CONTACT_LADA'], $ladasValues)){
  828. return $this->responseController->makeResponse(true, "La lada $form[CONTACT_LADA] no está relacionada a ningún país.", [], 400);
  829. }else if($form['CONTACT_TELEPHONE'] == 'ENC_ERR'){
  830. return $this->responseController->makeResponse(true, 'El número telefónico del contacto de emergencia no fue encriptado correctamente.', [], 400);
  831. }
  832. if($form['CONTACT_ADDRESS'] == 'ENC_ERR'){
  833. return $this->responseController->makeResponse(true, "La dirección del contacto de emergencia no fue encriptada correctamente.", [], 400);
  834. }
  835. if($form['CONTACT_NAME'] == 'ENC_ERR'){
  836. return $this->responseController->makeResponse(true, "El nombre del contacto de emergencia no fue encriptada correctamente.", [], 400);
  837. }
  838. $docsArr = json_decode($form['ATTACHED'], true);
  839. if(count($docsArr) == 0){
  840. return $this->responseController->makeResponse(true, "El arreglo de archivos adjuntos está vacío.", [], 400);
  841. }
  842. $authCont = 0;
  843. foreach($docsArr as $doc){
  844. if($doc['type'] == 'AU') $authCont++;
  845. }
  846. if($authCont < 1){
  847. return $this->responseController->makeResponse(true, "El arreglo de archivos adjuntos debe contener al menos un documento de autorización.", [], 400);
  848. }
  849. $finalDocsArr = [];
  850. foreach($docsArr as $doc){
  851. if(!array_key_exists('isFinalFile', $doc)){
  852. return $this->responseController->makeResponse(true, "La información del archivo $doc[name] no tiene un formato correcto.", [], 400);
  853. }
  854. if($doc['isFinalFile']){
  855. $finalDocsArr[] = [
  856. 'CODE' => $doc['name'],
  857. 'TYPE' => $doc['type']
  858. ];
  859. }else{
  860. $idFileDec = $this->encryptionController->decrypt($doc['id']);
  861. $tempFile = DB::table('S002V01TARTE')->where([
  862. ['ARTE_NULI', '=', $form['linea']],
  863. ['ARTE_IDAR', '=', $idFileDec],
  864. ])->first();
  865. if(is_null($tempFile)){
  866. return $this->responseController->makeResponse(true, "El archivo $doc[name] no está registrado.", [], 404);
  867. }
  868. $classifications = ["AU" => "IN", "OF" => "CO", "CE" => "CE"];
  869. $cldo = $classifications[$doc['type']];
  870. $finalFile = $this->documentManagementController->moveFinalFile($form['linea'], "GPRS", $cldo, $tempFile, $idUser);
  871. if(!$finalFile[0]){
  872. return $this->responseController->makeResponse(true, $finalFile[1], [], 400);
  873. }else{
  874. $finalDocsArr[] = [
  875. 'CODE' => $finalFile[1],
  876. 'TYPE' => $doc['type']
  877. ];
  878. }
  879. }
  880. }
  881. $doas = json_encode($finalDocsArr);
  882. $nuin = isset($form['INTERIOR_NUMBER']) ? $form['INTERIOR_NUMBER'] : null;
  883. $now = $this->functionsController->now();
  884. $nowStr = $now->toDateTimeString();
  885. DB::table('S002V01TPERS')->where([
  886. ['PERS_NULI', '=', $form['linea']],
  887. ['PERS_IDPE', '=', $idEmployee]
  888. ])->update([
  889. 'PERS_EQTR' => $idTeam,
  890. 'PERS_TICO' => $form['CONTRACT_TYPE'],
  891. 'PERS_IDPS' => $idSubcontratist,
  892. 'PERS_ESPE' => $form['SPECIALITY'],
  893. 'PERS_EXTR' => $form['FOREIGNER'],
  894. 'PERS_XRFC' => $rfcx,
  895. 'PERS_TAID' => $taid,
  896. 'PERS_IDPA' => $form['COUNTRY'],
  897. 'PERS_ENFE' => $form['FEDERAL_ENTITY'],
  898. 'PERS_CIUD' => $ciud,
  899. 'PERS_LOCA' => $loca,
  900. 'PERS_COLO' => $colo,
  901. 'PERS_COPO' => $form['POSTAL_CODE'],
  902. 'PERS_CALL' => $form['STREET'],
  903. 'PERS_NUEX' => $form['EXTERIOR_NUMBER'],
  904. 'PERS_NUIN' => $nuin,
  905. 'PERS_NOCE' => $form['CONTACT_NAME'],
  906. 'PERS_LCEM' => $form['CONTACT_LADA'],
  907. 'PERS_NUTC' => $form['CONTACT_TELEPHONE'],
  908. 'PERS_DCEM' => $form['CONTACT_ADDRESS'],
  909. 'PERS_DOAS' => $doas,
  910. 'PERS_USMO' => $idUser,
  911. 'PERS_FEMO' => $nowStr,
  912. ]);
  913. if ( !is_null($form['EMPLOYEE_BOSS']) && $form['EMPLOYEE_BOSS'] !== '') {
  914. try {
  915. $arrCronograma = (array) DB::table('S002V01TCREM')
  916. ->where('CREM_IDES', '=', $idEmployee)
  917. ->where('CREM_IDEJ', '=', $form['EMPLOYEE_BOSS'])
  918. ->where('CREM_NULI', '=', $form['linea'])
  919. ->first([
  920. 'CREM_IDES AS ID_SUBORDINADO',
  921. 'CREM_IDEJ AS IS_JEFE',
  922. 'CREM_ESTA AS ESTADO',
  923. ]);
  924. } catch (Throwable $th) {
  925. return $this->responseController->makeResponse(true, "Ocurrió un error al obtener el cronograma.", $th->getMessage(), 400);
  926. }
  927. if ( count($arrCronograma) === 0 ) {
  928. try {
  929. $validateRegister = DB::table('S002V01TCREM')->insert([
  930. 'CREM_IDES' => $idEmployee,
  931. 'CREM_IDEJ' => $form['EMPLOYEE_BOSS'],
  932. 'CREM_NULI' => $form['linea'],
  933. 'CREM_USRE' => $idUser,
  934. 'CREM_FERE' => $nowStr,
  935. 'CREM_FEAR' => DB::raw('CURRENT_TIMESTAMP'),
  936. ]);
  937. } catch (Throwable $th) {
  938. return $this->responseController->makeResponse(true, "Ocurrió un error al registrar el cronograma.", $th->getMessage(), 400);
  939. }
  940. if (!$validateRegister) {
  941. return $this->responseController->makeResponse(true, "No se pudo registrar los empleados en el cronograma.", [], 400);
  942. }
  943. } else {
  944. try {
  945. $validateUpdate = DB::table('S002V01TCREM')
  946. ->where('CREM_IDES', '=', $idEmployee)
  947. ->where('CREM_IDEJ', '=', $form['EMPLOYEE_BOSS'])
  948. ->where('CREM_NULI', '=', $form['linea'])
  949. ->update([
  950. 'CREM_ESTA' => 'Activo',
  951. 'CREM_USMO' => $idUser,
  952. 'CREM_FEMO' => $nowStr,
  953. 'CREM_FEAR' => DB::raw('CURRENT_TIMESTAMP'),
  954. ]);
  955. } catch (Throwable $th) {
  956. return $this->responseController->makeResponse(true, "Ocurrió un error al modificar el cronograma.", $th->getMessage(), 400);
  957. }
  958. if (!$validateUpdate) {
  959. return $this->responseController->makeResponse(true, "No se pudo modificar los empleados en el cronograma.", [], 400);
  960. }
  961. }
  962. }
  963. $actions = DB::getQueryLog();
  964. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  965. $nameEmp = $this->functionsController->joinName($usrReg->USUA_NOMB, $usrReg->USUA_APPA, $usrReg->USUA_APMA);
  966. $idac = $this->functionsController->registerActivity(
  967. $form['linea'],
  968. 'S002V01M11GPRS',
  969. 'S002V01F01GEPE',
  970. 'S002V01P02REEM',
  971. 'Actualización',
  972. "El usuario $name (" . $usr->USUA_IDUS . ") actualizó al empleado " . $nameEmp . "($idEmployee).",
  973. $idUser,
  974. $nowStr,
  975. 'S002V01S02GEPE'
  976. );
  977. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $form['linea']);
  978. return $this->responseController->makeResponse(false, 'EXITO');
  979. }
  980. public function updateToInactiveStatus(Request $request) {
  981. DB::enableQueryLog();
  982. $validator = Validator::make($request->all(), [
  983. 'id_user' => 'required|string',
  984. 'linea' => 'required|integer',
  985. 'id_employee' => 'required|string',
  986. ]);
  987. if($validator->fails()){
  988. return $this->responseController->makeResponse(
  989. true,
  990. "Se encontraron uno o más errores.",
  991. $this->responseController->makeErrors(
  992. $validator->errors()->messages()
  993. ),
  994. 401
  995. );
  996. }
  997. $form = $request->all();
  998. $idUser = $this->encryptionController->decrypt($form['id_user']);
  999. if(!$idUser){
  1000. return $this->responseController->makeResponse(true, 'El ID de usuario no fue encriptado correctamente.', [], 400);
  1001. }
  1002. $usr = DB::table('S002V01TUSUA')->where([
  1003. ['USUA_NULI', '=', $form['linea']],
  1004. ['USUA_IDUS', '=', $idUser]
  1005. ])->first();
  1006. if(is_null($usr)){
  1007. return $this->responseController->makeResponse(true, 'El usuario que realizó la petición no existe.', [], 404);
  1008. }
  1009. $idEmployee = $this->encryptionController->decrypt($form['id_employee']);
  1010. if(!$idEmployee){
  1011. return $this->responseController->makeResponse(true, 'El ID del empleado que desea actualizar no fue encriptado correctamente.', [], 400);
  1012. }
  1013. $employee = DB::table('S002V01TPERS')->where([
  1014. ['PERS_NULI', '=', $form['linea']],
  1015. ['PERS_IDPE', '=', $idEmployee]
  1016. ])->first();
  1017. if(is_null($employee)){
  1018. return $this->responseController->makeResponse(true, 'El empleado que desea actualizar no existe.', [], 404);
  1019. }
  1020. if($idUser == $employee->PERS_IDUS){
  1021. return $this->responseController->makeResponse(true, 'El usuario no puede eliminarse a sí mismo.', [], 400);
  1022. }
  1023. $now = $this->functionsController->now();
  1024. $nowStr = $now->toDateTimeString();
  1025. DB::table('S002V01TPERS')->where([
  1026. ['PERS_NULI', '=', $form['linea']],
  1027. ['PERS_IDPE', '=', $idEmployee]
  1028. ])->update([
  1029. 'PERS_ESTA' => 'Eliminado',
  1030. 'PERS_USMO' => $idUser,
  1031. 'PERS_FEMO' => $nowStr,
  1032. ]);
  1033. $actions = DB::getQueryLog();
  1034. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  1035. $idac = $this->functionsController->registerActivity(
  1036. $form['linea'],
  1037. 'S002V01M11GPRS',
  1038. 'S002V01F01GEPE',
  1039. 'S002V01P02REEM',
  1040. 'Eliminación',
  1041. "El usuario $name (" . $usr->USUA_IDUS . ") eliminó al empleado $idEmployee.",
  1042. $idUser,
  1043. $nowStr,
  1044. 'S002V01S02GEPE'
  1045. );
  1046. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $form['linea']);
  1047. return $this->responseController->makeResponse(false, 'EXITO');
  1048. }
  1049. public function getContractsOfEveryEmployee($idUser, $line) {
  1050. DB::enableQueryLog();
  1051. $idUser = $this->encryptionController->decrypt($idUser);
  1052. if(!$idUser){
  1053. return $this->responseController->makeResponse(true, 'El ID del usuario que realizó la solicitud no está encriptado correctamente', [], 400);
  1054. }
  1055. $usr = DB::table('S002V01TUSUA')->where([
  1056. ['USUA_NULI', '=', $line],
  1057. ['USUA_IDUS', '=', $idUser],
  1058. ])->first();
  1059. if(is_null($usr)){
  1060. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado', [], 404);
  1061. }
  1062. $contractsByEmployees = DB::table('S002V01TPERS')->select([
  1063. DB::raw("
  1064. TRIM(CONCAT(
  1065. USUA_NOMB,
  1066. ' ',
  1067. USUA_APPA,
  1068. IF(ISNULL(USUA_APMA), '', CONCAT(' ', USUA_APMA)),
  1069. ' <(',
  1070. PERS_IDPE,
  1071. ')> (',
  1072. PERS_IDUS,
  1073. ')'
  1074. )) AS EMPLOYEE
  1075. "),
  1076. DB::raw("COUNT(CONT_IDCO) AS CONTRACTS")
  1077. ])->leftJoin('S002V01TCONT', 'CONT_IDEM', '=', 'PERS_IDPE')
  1078. ->join('S002V01TUSUA', 'USUA_IDUS', '=', 'PERS_IDUS')
  1079. ->groupBy('EMPLOYEE')->where([
  1080. ['PERS_NULI', '=', $line],
  1081. ['PERS_TICO', '=', 'Interno'],
  1082. ])->get()->all();
  1083. foreach($contractsByEmployees as $key=>$val){
  1084. $employeeArr = explode('<', $val->EMPLOYEE);
  1085. $employeeName = $employeeArr[0];
  1086. $employeeArr = array_reverse($employeeArr);
  1087. $employeeArr = explode('>', $employeeArr[0]);
  1088. $employeeID = str_replace('(', '', $employeeArr[0]);
  1089. $employeeID = str_replace(')', '', $employeeID);
  1090. $val->ID_EMPLOYEE = $this->encryptionController->encrypt($employeeID);
  1091. $val->EMPLOYEE = $employeeName . join('', $employeeArr);
  1092. $contractsByEmployees[$key] = $val;
  1093. }
  1094. $now = $this->functionsController->now();
  1095. $nowStr = $now->toDateTimeString();
  1096. $actions = DB::getQueryLog();
  1097. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  1098. $idac = $this->functionsController->registerActivity(
  1099. $line,
  1100. 'S002V01M11GPRS',
  1101. 'S002V01F02ACEM',
  1102. 'S002V01P01HCEM',
  1103. 'Consulta',
  1104. "El usuario $name (" . $usr->USUA_IDUS . ") consultó los contratos por empleados.",
  1105. $idUser,
  1106. $nowStr,
  1107. 'S002V01S02GEPE'
  1108. );
  1109. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $line);
  1110. return $this->responseController->makeResponse(false, 'EXITO', $contractsByEmployees);
  1111. }
  1112. public function getDetailsOfContractsByEmployee($idEmployee, $idUser, $line) {
  1113. DB::enableQueryLog();
  1114. $idUser = $this->encryptionController->decrypt($idUser);
  1115. if(!$idUser){
  1116. return $this->responseController->makeResponse(true, 'El ID de usuario no fue encriptado correctamente.', [], 400);
  1117. }
  1118. $usr = DB::table('S002V01TUSUA')->where([
  1119. ['USUA_NULI', '=', $line],
  1120. ['USUA_IDUS', '=', $idUser]
  1121. ])->first();
  1122. if(is_null($usr)){
  1123. return $this->responseController->makeResponse(true, 'El usuario que realizó la petición no existe.', [], 404);
  1124. }
  1125. $idEmployee = $this->encryptionController->decrypt($idEmployee);
  1126. if(!$idEmployee){
  1127. return $this->responseController->makeResponse(true, 'El ID del empleado no fue encriptado correctamente.', [], 400);
  1128. }
  1129. $sub = DB::table('S002V01TPERS')->where([
  1130. ['PERS_NULI', '=', $line],
  1131. ['PERS_IDPE', '=', $idEmployee],
  1132. ['PERS_TICO', '=', 'Interno'],
  1133. ])->first();
  1134. if(is_null($sub)){
  1135. return $this->responseController->makeResponse(true, 'El empleado solicitado no existe.', [], 404);
  1136. }
  1137. $contracts = DB::table('S002V01TCONT')->select([
  1138. 'CONT_NOCO AS CONTRACT_NAME',
  1139. 'CONT_FEIN AS START_DATE',
  1140. 'CONT_FEFI AS END_DATE',
  1141. 'CONT_COST AS COST',
  1142. ])->where([
  1143. ['CONT_NULI', '=', $line],
  1144. ['CONT_IDEM', '=', $idEmployee],
  1145. ['CONT_TIPO', '=', 'Interno'],
  1146. ])->get()->all();
  1147. $now = $this->functionsController->now();
  1148. $nowStr = $now->toDateTimeString();
  1149. $actions = DB::getQueryLog();
  1150. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  1151. $idac = $this->functionsController->registerActivity(
  1152. $line,
  1153. 'S002V01M11GPRS',
  1154. 'S002V01F02ACEM',
  1155. 'S002V01P01HCEM',
  1156. 'Consulta',
  1157. "El usuario $name (" . $usr->USUA_IDUS . ") consultó los contratos del empleado #$idEmployee.",
  1158. $idUser,
  1159. $nowStr,
  1160. 'S002V01S02GEPE'
  1161. );
  1162. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $line);
  1163. return $this->responseController->makeResponse(false, 'EXITO', $contracts);
  1164. }
  1165. public function getInterventionsByEmployee($idEmployee, $idUser, $line) {
  1166. DB::enableQueryLog();
  1167. $idUser = $this->encryptionController->decrypt($idUser);
  1168. if(!$idUser){
  1169. return $this->responseController->makeResponse(true, 'El ID de usuario no fue encriptado correctamente.', [], 400);
  1170. }
  1171. $usr = DB::table('S002V01TUSUA')->where([
  1172. ['USUA_NULI', '=', $line],
  1173. ['USUA_IDUS', '=', $idUser]
  1174. ])->first();
  1175. if(is_null($usr)){
  1176. return $this->responseController->makeResponse(true, 'El usuario que realizó la petición no existe.', [], 404);
  1177. }
  1178. $idEmployee = $this->encryptionController->decrypt($idEmployee);
  1179. if(!$idEmployee){
  1180. return $this->responseController->makeResponse(true, 'El ID del empleado no fue encriptado correctamente.', [], 400);
  1181. }
  1182. $employee = DB::table('S002V01TPERS')->where([
  1183. ['PERS_NULI', '=', $line],
  1184. ['PERS_IDPE', '=', $idEmployee],
  1185. ])->first();
  1186. if(is_null($employee)){
  1187. return $this->responseController->makeResponse(true, 'El empleado solicitado no existe.', [], 404);
  1188. }
  1189. $preventiveOrders = DB::select("
  1190. SELECT
  1191. DISTINCT(S002V01TOTPR.OTPR_IDOT) AS IDORDEN,
  1192. S002V01TOTPR.OTPR_DEIN AS DESCRIPCION,
  1193. 'Preventivo' AS TIPOORDEN
  1194. FROM
  1195. S002V01TOTPR,
  1196. JSON_TABLE(OTPR_OPPR, '$[*]' COLUMNS(
  1197. OTPR_OPPR_ID INT PATH '$.ID',
  1198. OTPR_OPPR_TYPE VARCHAR(100) PATH '$.TYPE'
  1199. )) OPPR
  1200. WHERE
  1201. (OTPR_OPPR_ID = :emp_id AND OTPR_OPPR_TYPE = 'EM')
  1202. OR (OTPR_OPPR_ID = :sub_id AND OTPR_OPPR_TYPE = 'SU')
  1203. OR (OTPR_OPPR_ID = :team_id AND OTPR_OPPR_TYPE = 'EQ')
  1204. ", [
  1205. 'emp_id' => $employee->PERS_IDPE,
  1206. 'sub_id' => $employee->PERS_IDPS,
  1207. 'team_id' => $employee->PERS_EQTR
  1208. ]);
  1209. //PENDIENTE MANTENIMIENTO CORRECTIVO
  1210. $ordersFN = [];
  1211. $relatedOrders = [];
  1212. foreach($preventiveOrders as $order){
  1213. if(!in_array($order->IDORDEN, $relatedOrders)){
  1214. $relatedOrders[] = $order->IDORDEN;
  1215. $order->IDORDEN = $this->encryptionController->encrypt($order->IDORDEN);
  1216. $ordersFN[] = $order;
  1217. }
  1218. }
  1219. $now = $this->functionsController->now();
  1220. $nowStr = $now->toDateTimeString();
  1221. $actions = DB::getQueryLog();
  1222. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  1223. $idac = $this->functionsController->registerActivity(
  1224. $line,
  1225. 'S002V01M11GPRS',
  1226. 'S002V01F01GEPE',
  1227. 'S002V01P03DEEM',
  1228. 'Consulta',
  1229. "El usuario $name (" . $usr->USUA_IDUS . ") consultó las órdenes de trabajo relacionadas al empleado #$idEmployee.",
  1230. $idUser,
  1231. $nowStr,
  1232. 'S002V01S02GEPE'
  1233. );
  1234. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $line);
  1235. return $this->responseController->makeResponse(false, 'EXITO', $ordersFN);
  1236. }
  1237. public function getHoulyRateByEmployee($idEmployee, $idUser, $line) {
  1238. try {
  1239. $idEmployee = $this->encryptionController->decrypt($idEmployee);
  1240. } catch (\Throwable $th) {
  1241. return $this->responseController->makeResponse(true, "ERR_EMPLOYEE_GETHOULYRATE001: Ocurrió un error al obtener el ID del empleado.", $th->getMessage(), 500);
  1242. }
  1243. try {
  1244. $idUser = $this->encryptionController->decrypt($idUser);
  1245. } catch (\Throwable $th) {
  1246. return $this->responseController->makeResponse(true, "ERR_EMPLOYEE_GETHOULYRATE002: Ocurrió un error al obtener el número de usuario.", $th->getMessage(), 500);
  1247. }
  1248. try {
  1249. $validateUser = DB::table('S002V01TUSUA')->where('USUA_NULI', '=', $line)->where('USUA_IDUS', '=', $idUser)->exists();
  1250. } catch (\Throwable $th) {
  1251. return $this->responseController->makeResponse(true, "ERR_EMPLOYEE_GETHOULYRATE003: Ocurrió un error al verificar el usuario.", $th->getMessage(), 500);
  1252. }
  1253. if( !$validateUser ){
  1254. return $this->responseController->makeResponse(true, 'ERR_EMPLOYEE_GETHOULYRATE004: El usuario que realizó la petición no existe.', [], 404);
  1255. }
  1256. try {
  1257. $employee = (array) DB::table('S002V01TPERS')->where('PERS_NULI', '=', $line)->where('PERS_IDPE', '=', $idEmployee)->first();
  1258. } catch (\Throwable $th) {
  1259. return $this->responseController->makeResponse(true, "ERR_EMPLOYEE_GETHOULYRATE005: Ocurrió un error al obtener la información del empleado.", $th->getMessage(), 500);
  1260. }
  1261. if(empty($employee)){
  1262. return $this->responseController->makeResponse(true, 'ERR_EMPLOYEE_GETHOULYRATE006: El empleado solicitado no existe.', [], 404);
  1263. }
  1264. try {
  1265. $preventiveOrders = DB::select("
  1266. SELECT
  1267. DISTINCT(S002V01TOTPR.OTPR_IDOT) AS IDORDEN,
  1268. S002V01TOTPR.OTPR_DEIN AS DESCRIPCION,
  1269. 'Preventivo' AS TIPOORDEN,
  1270. FROM
  1271. S002V01TOTPR,
  1272. JSON_TABLE(OTPR_OPPR, '$[*]' COLUMNS(
  1273. OTPR_OPPR_ID INT PATH '$.ID',
  1274. OTPR_OPPR_TYPE VARCHAR(100) PATH '$.TYPE'
  1275. )) OPPR
  1276. WHERE
  1277. (OTPR_OPPR_ID = :emp_id AND OTPR_OPPR_TYPE = 'EM')
  1278. OR (OTPR_OPPR_ID = :sub_id AND OTPR_OPPR_TYPE = 'SU')
  1279. OR (OTPR_OPPR_ID = :team_id AND OTPR_OPPR_TYPE = 'EQ')
  1280. ", [
  1281. 'emp_id' => $employee['PERS_IDPE'],
  1282. 'sub_id' => $employee['PERS_IDPS'],
  1283. 'team_id' => $employee['PERS_EQTR']
  1284. ]);
  1285. } catch (\Throwable $th) {
  1286. return $this->responseController->makeResponse(true, "ERR_EMPLOYEE_GETHOULYRATE007: Ocurrió un error al obtener los trabajos de mantenimiento preventivo", $th->getMessage(), 500);
  1287. }
  1288. if (is_null($preventiveOrders) || $preventiveOrders == false) {
  1289. return $this->responseController->makeResponse(true, "ERR_EMPLOYEE_GETHOULYRATE008: Ocurrió un error al obtener los trabajos de mantenimiento preventivo", [], 500);
  1290. }
  1291. $preventiveOrders = json_decode( json_encode($preventiveOrders), true );
  1292. }
  1293. public function getScheduleActivitiesByEmployee($idEmployee, $user, $line) {
  1294. try {
  1295. $idEmployee = $this->encryptionController->decrypt($idEmployee);
  1296. } catch (\Throwable $th) {
  1297. return $this->responseController->makeResponse(true, "ERR_EMPLOYEE_GETACTIVITY000: Ocurrió un error al ibtener los datos.", $th->getMessage(), 404);
  1298. }
  1299. try {
  1300. $getSheduleActivities = DB::table('S002V01TCOAC')
  1301. ->where('COAC_NULI','=',$line)
  1302. ->where('COAC_ESTA','=','Activo')
  1303. ->where('COAC_IDPE','=',$idEmployee)
  1304. ->get([
  1305. 'COAC_IDAC AS ID_ACTIVIDAD',
  1306. 'COAC_IDPE AS ID_EMPLEADO',
  1307. 'COAC_ACTI AS ACTIVIDAD',
  1308. 'COAC_DESC AS DESCRIPCION',
  1309. 'COAC_TIPO AS TIPO_ACTIVIDAD',
  1310. 'COAC_OTPR AS ID_ORDEN_PREVENTIVO',
  1311. 'COAC_OTCO AS ID_ORDEN_CORRECTIVO',
  1312. 'COAC_FEIN AS FECHA_INICIO',
  1313. 'COAC_HOIN AS HORA_INICIO',
  1314. 'COAC_FEFE AS FECHA_FINAL',
  1315. 'COAC_HOFE AS HORA_FINAL',
  1316. 'COAC_FEFR AS FECHA_FINAL_REAL',
  1317. 'COAC_HOFR AS HORA_FINAL_REAL',
  1318. 'COAC_PERI AS PERIODICIDAD',
  1319. 'COAC_PEPE AS PERIODICIDAD_PERSONALIZADA',
  1320. 'COAC_CLAS AS CLASIFICACION',
  1321. 'COAC_COLO AS COLOR',
  1322. 'COAC_ESTA AS ESTADO',
  1323. 'COAC_FERE AS FECHA_REGISTRA',
  1324. 'COAC_USRE AS USUARIO_REGISTRA',
  1325. 'COAC_FEMO AS FECHA_MODIFICA',
  1326. 'COAC_USMO AS USUARIO_MODIFICA',
  1327. ]);
  1328. } catch (\Throwable $th) {
  1329. DB::rollBack();
  1330. return $this->responseController->makeResponse(true, "ERR_EMPLOYEE_GETACTIVITY001: Ocurrió un error al ibtener los datos.", $th->getMessage(), 404);
  1331. }
  1332. $arrSheduleActivities = json_decode(json_encode($getSheduleActivities), true);
  1333. return $this->responseController->makeResponse(false, "ÉXITO: Consulta Exitosa", $arrSheduleActivities);
  1334. }
  1335. public function registerActivity(Request $request) {
  1336. $valitador = Validator::make($request->all(), [
  1337. 'ID_EMPLEADO' => 'required|string',
  1338. 'ACTIVIDAD' => 'required|string',
  1339. 'CLASIFICACION' => 'required|string',
  1340. 'DESCRIPCION' => 'required|string',
  1341. 'TIPO_ACTIVIDAD' => 'required|string',
  1342. 'ID_ORDEN' => 'nullable|string',
  1343. 'PERIODICIDAD' => 'nullable|string',
  1344. 'FECHA_INICIO' => 'required|string',
  1345. 'HORA_INICIO' => 'required|string',
  1346. 'FECHA_FINAL' => 'nullable|string',
  1347. 'HORA_FINAL' => 'nullable|string',
  1348. 'PERIODICIDAD_PERSONALIZADA' => 'nullable|string',
  1349. 'COLOR' => 'required|string',
  1350. 'USUARIO' => 'required|string',
  1351. 'NUMERO_LINEA' => 'required|string',
  1352. ]);
  1353. if ($valitador->fails()) {
  1354. return $this->responseController->makeResponse(
  1355. true,
  1356. "ERR_EMPLOYEE_REGACTIVITY000: Se encontraron uno o más errores.",
  1357. $this->responseController->makeErrors($valitador->errors()->messages()),
  1358. 401
  1359. );
  1360. }
  1361. DB::beginTransaction();
  1362. $requestData = $request->all();
  1363. try {
  1364. $user = $this->encryptionController->decrypt($requestData['USUARIO']);
  1365. } catch (\Throwable $th) {
  1366. DB::rollBack();
  1367. return $this->responseController->makeResponse(true, "ERR_EMPLOYEE_REGACTIVITY001: Ocurrió un error al desencriptar los datos.", $th->getMessage(), 401);
  1368. }
  1369. try {
  1370. $validateUser = DB::table('S002V01TUSUA')
  1371. ->where('USUA_NULI', '=', $requestData['NUMERO_LINEA'])
  1372. ->where('USUA_IDUS', '=', $user)
  1373. ->exists();
  1374. } catch(\Throwable $th) {
  1375. DB::rollBack();
  1376. return $this->responseController->makeResponse(true, "ERR_EMPLOYEE_REGACTIVITY002: Ocurrió al verificar la existencia del usuario.", $th->getMessage(), 404);
  1377. }
  1378. if (!$validateUser) {
  1379. DB::rollBack();
  1380. return $this->responseController->makeResponse(true, "ERR_EMPLOYEE_REGACTIVITY003: El usuario no existe.", [], 404);
  1381. }
  1382. $idPreventiveMaintenance = null;
  1383. $idCorrectiveMaintenance = null;
  1384. if ($requestData['TIPO_ACTIVIDAD'] === 'MP') {
  1385. if (is_null($requestData['ID_ORDEN'])) {
  1386. DB::rollBack();
  1387. return $this->responseController->makeResponse(true, "ERR_EMPLOYEE_REGACTIVITY004: El ID de la orden de mantenimiento no viene incluida en los datos del formulario.", [], 404);
  1388. }
  1389. try {
  1390. $validateMaintenance = DB::table('S002V01TOTPR')
  1391. ->where('OTPR_IDOT', '=', $requestData['ID_ORDEN'])
  1392. ->where('OTPR_NULI', '=', $requestData['NUMERO_LINEA'])
  1393. ->exists();
  1394. } catch (\Throwable $th) {
  1395. DB::rollBack();
  1396. return $this->responseController->makeResponse(true, "ERR_EMPLOYEE_REGACTIVITY005: Ocurrió un error al validar la orden de mantenimiento.", $th->getMessage(), 404);
  1397. }
  1398. if (!$validateMaintenance) {
  1399. DB::rollBack();
  1400. return $this->responseController->makeResponse(true, "ERR_EMPLOYEE_REGACTIVITY006: La orden de trabajo no existe en la lista de mantenimientos preventivos.", [], 404);
  1401. }
  1402. $idPreventiveMaintenance = $requestData['ID_ORDEN'];
  1403. } else if ($requestData['TIPO_ACTIVIDAD'] === 'MC') {
  1404. if (is_null($requestData['ID_ORDEN'])) {
  1405. DB::rollBack();
  1406. return $this->responseController->makeResponse(true, "ERR_EMPLOYEE_REGACTIVITY007: El ID de la orden de mantenimiento no viene incluida en los datos del formulario.", [], 404);
  1407. }
  1408. try {
  1409. $validateMaintenance = DB::table('S002V01TOTCO')
  1410. ->where('OTCO_IDOT ', '=', $requestData['ID_ORDEN'])
  1411. ->where('OTCO_NULI ', '=', $requestData['NUMERO_LINEA'])
  1412. ->exists();
  1413. } catch (\Throwable $th) {
  1414. DB::rollBack();
  1415. return $this->responseController->makeResponse(true, "ERR_EMPLOYEE_REGACTIVITY008: Ocurrió un error al validar la orden de mantenimiento.", $th->getMessage(), 404);
  1416. }
  1417. if (!$validateMaintenance) {
  1418. DB::rollBack();
  1419. return $this->responseController->makeResponse(true, "ERR_EMPLOYEE_REGACTIVITY009: La orden de trabajo no existe en la lista de mantenimientos correctivo.", [], 404);
  1420. }
  1421. $idCorrectiveMaintenance = $requestData['ID_ORDEN'];
  1422. } else if ($requestData['TIPO_ACTIVIDAD'] === 'NA' && is_null($requestData['ID_ORDEN']) && !is_null($requestData['PERIODICIDAD']) ) {
  1423. if ($requestData['PERIODICIDAD'] === 'PE' && is_null($requestData['PERIODICIDAD_PERSONALIZADA'])) {
  1424. DB::rollBack();
  1425. return $this->responseController->makeResponse(true, "ERR_EMPLOYEE_REGACTIVITY010: El tipo de periodicidad personalizada debe estar incluida si el tipo de periodicidad está selecciona como Personalizada.", [], 404);
  1426. }
  1427. if ($requestData['PERIODICIDAD'] === 'NR' && (is_null($requestData['FECHA_FINAL']) | is_null($requestData['HORA_FINAL'])) ) {
  1428. DB::rollBack();
  1429. return $this->responseController->makeResponse(true, "ERR_EMPLOYEE_REGACTIVITY011: La fecha y la hora final esperada debe estar incluida en el formulario si la periodicidad está seleccionada como No se repite.", [], 404);
  1430. }
  1431. } else {
  1432. DB::rollBack();
  1433. return $this->responseController->makeResponse(true, "ERR_EMPLOYEE_REGACTIVITY012: El tipo de actividad no es correcto.", [], 404);
  1434. }
  1435. $now = $this->functionsController->now();
  1436. $currentDate = $now->toDateTimeString();
  1437. try {
  1438. $validateInsert = DB::table('S002V01TCOAC')->insert([
  1439. 'COAC_NULI' => $requestData['NUMERO_LINEA'],
  1440. 'COAC_IDPE' => $requestData['ID_EMPLEADO'],
  1441. 'COAC_ACTI' => $requestData['ACTIVIDAD'],
  1442. 'COAC_DESC' => $requestData['DESCRIPCION'],
  1443. 'COAC_TIPO' => $requestData['TIPO_ACTIVIDAD'],
  1444. 'COAC_OTPR' => $idPreventiveMaintenance,
  1445. 'COAC_OTCO' => $idCorrectiveMaintenance,
  1446. 'COAC_FEIN' => $requestData['FECHA_INICIO'],
  1447. 'COAC_HOIN' => $requestData['HORA_INICIO'],
  1448. 'COAC_FEFE' => $requestData['FECHA_FINAL'],
  1449. 'COAC_HOFE' => $requestData['HORA_FINAL'],
  1450. // 'COAC_FEFR' => $requestData[''],
  1451. // 'COAC_HOFR' => $requestData[''],
  1452. 'COAC_PERI' => $requestData['PERIODICIDAD'],
  1453. 'COAC_PEPE' => $requestData['PERIODICIDAD_PERSONALIZADA'],
  1454. 'COAC_CLAS' => $requestData['CLASIFICACION'],
  1455. 'COAC_COLO' => $requestData['COLOR'],
  1456. 'COAC_FERE' => $currentDate,
  1457. 'COAC_USRE' => $user,
  1458. 'COAC_FEAR' => DB::raw('CURRENT_TIMESTAMP'),
  1459. ]);
  1460. } catch (\Throwable $th) {
  1461. DB::rollBack();
  1462. return $this->responseController->makeResponse(true, "ERR_EMPLOYEE_REGACTIVITY013: Ocurrió un error al insertar los datos.", $th->getMessage(), 404);
  1463. }
  1464. if (!$validateInsert) {
  1465. DB::rollBack();
  1466. return $this->responseController->makeResponse(true, "ERR_EMPLOYEE_REGACTIVITY014: No se pudo insertar los datos en la base de datos.", [], 404);
  1467. }
  1468. DB::commit();
  1469. return $this->responseController->makeResponse(false, "ÉXITO: Registro Exitoso");
  1470. }
  1471. public function updateActivity(Request $request, $idActivity) {
  1472. $valitador = Validator::make($request->all(), [
  1473. // 'ID_ACTIVIDAD' => 'required|integer',
  1474. 'ID_EMPLEADO' => 'required|string',
  1475. 'ACTIVIDAD' => 'required|string',
  1476. 'CLASIFICACION' => 'required|string',
  1477. 'DESCRIPCION' => 'required|string',
  1478. 'TIPO_ACTIVIDAD' => 'required|string',
  1479. 'ID_ORDEN' => 'nullable|string',
  1480. 'PERIODICIDAD' => 'nullable|string',
  1481. 'FECHA_INICIO' => 'required|string',
  1482. 'HORA_INICIO' => 'required|string',
  1483. 'FECHA_FINAL' => 'nullable|string',
  1484. 'HORA_FINAL' => 'nullable|string',
  1485. 'PERIODICIDAD_PERSONALIZADA' => 'nullable|string',
  1486. 'COLOR' => 'required|string',
  1487. 'USUARIO' => 'required|string',
  1488. 'NUMERO_LINEA' => 'required|string',
  1489. ]);
  1490. if ($valitador->fails()) {
  1491. return $this->responseController->makeResponse(
  1492. true,
  1493. "ERR_EMPLOYEE_UPDACTIVITY000: Se encontraron uno o más errores.",
  1494. $this->responseController->makeErrors($valitador->errors()->messages()),
  1495. 401
  1496. );
  1497. }
  1498. DB::beginTransaction();
  1499. $requestData = $request->all();
  1500. try {
  1501. $idActivity = $this->encryptionController->decrypt($idActivity);
  1502. } catch (\Throwable $th) {
  1503. DB::rollBack();
  1504. return $this->responseController->makeResponse(true, "ERR_EMPLOYEE_UPDACTIVITY001: Ocurrió un error al desencriptar los datos de la actividad.", $th->getMessage(), 401);
  1505. }
  1506. try {
  1507. $user = $this->encryptionController->decrypt($requestData['USUARIO']);
  1508. } catch (\Throwable $th) {
  1509. DB::rollBack();
  1510. return $this->responseController->makeResponse(true, "ERR_EMPLOYEE_UPDACTIVITY002: Ocurrió un error al desencriptar los datos.", $th->getMessage(), 401);
  1511. }
  1512. try {
  1513. $validateUser = DB::table('S002V01TUSUA')
  1514. ->where('USUA_NULI', '=', $requestData['NUMERO_LINEA'])
  1515. ->where('USUA_IDUS', '=', $user)
  1516. ->exists();
  1517. } catch(\Throwable $th) {
  1518. DB::rollBack();
  1519. return $this->responseController->makeResponse(true, "ERR_EMPLOYEE_UPDACTIVITY003: Ocurrió al verificar la existencia del usuario.", $th->getMessage(), 404);
  1520. }
  1521. if (!$validateUser) {
  1522. DB::rollBack();
  1523. return $this->responseController->makeResponse(true, "ERR_EMPLOYEE_UPDACTIVITY004: El usuario no existe.", [], 404);
  1524. }
  1525. $idPreventiveMaintenance = null;
  1526. $idCorrectiveMaintenance = null;
  1527. if ($requestData['TIPO_ACTIVIDAD'] === 'MP') {
  1528. if (is_null($requestData['ID_ORDEN'])) {
  1529. DB::rollBack();
  1530. return $this->responseController->makeResponse(true, "ERR_EMPLOYEE_UPDACTIVITY005: El ID de la orden de mantenimiento no viene incluida en los datos del formulario.", [], 404);
  1531. }
  1532. try {
  1533. $validateMaintenance = DB::table('S002V01TOTPR')
  1534. ->where('OTPR_IDOT', '=', $requestData['ID_ORDEN'])
  1535. ->where('OTPR_NULI', '=', $requestData['NUMERO_LINEA'])
  1536. ->exists();
  1537. } catch (\Throwable $th) {
  1538. DB::rollBack();
  1539. return $this->responseController->makeResponse(true, "ERR_EMPLOYEE_UPDACTIVITY006: Ocurrió un error al validar la orden de mantenimiento.", $th->getMessage(), 404);
  1540. }
  1541. if (!$validateMaintenance) {
  1542. DB::rollBack();
  1543. return $this->responseController->makeResponse(true, "ERR_EMPLOYEE_UPDACTIVITY007: La orden de trabajo no existe en la lista de mantenimientos preventivos.", [], 404);
  1544. }
  1545. $idPreventiveMaintenance = $requestData['ID_ORDEN'];
  1546. } else if ($requestData['TIPO_ACTIVIDAD'] === 'MC') {
  1547. if (is_null($requestData['ID_ORDEN'])) {
  1548. DB::rollBack();
  1549. return $this->responseController->makeResponse(true, "ERR_EMPLOYEE_UPDACTIVITY008: El ID de la orden de mantenimiento no viene incluida en los datos del formulario.", [], 404);
  1550. }
  1551. try {
  1552. $validateMaintenance = DB::table('S002V01TOTCO')
  1553. ->where('OTCO_IDOT ', '=', $requestData['ID_ORDEN'])
  1554. ->where('OTCO_NULI ', '=', $requestData['NUMERO_LINEA'])
  1555. ->exists();
  1556. } catch (\Throwable $th) {
  1557. DB::rollBack();
  1558. return $this->responseController->makeResponse(true, "ERR_EMPLOYEE_UPDACTIVITY009: Ocurrió un error al validar la orden de mantenimiento.", $th->getMessage(), 404);
  1559. }
  1560. if (!$validateMaintenance) {
  1561. DB::rollBack();
  1562. return $this->responseController->makeResponse(true, "ERR_EMPLOYEE_UPDACTIVITY010: La orden de trabajo no existe en la lista de mantenimientos correctivo.", [], 404);
  1563. }
  1564. $idCorrectiveMaintenance = $requestData['ID_ORDEN'];
  1565. } else if ($requestData['TIPO_ACTIVIDAD'] === 'NA' && is_null($requestData['ID_ORDEN']) && !is_null($requestData['PERIODICIDAD']) ) {
  1566. if ($requestData['PERIODICIDAD'] === 'PE' && is_null($requestData['PERIODICIDAD_PERSONALIZADA'])) {
  1567. DB::rollBack();
  1568. return $this->responseController->makeResponse(true, "ERR_EMPLOYEE_UPDACTIVITY011: El tipo de periodicidad personalizada debe estar incluida si el tipo de periodicidad está selecciona como Personalizada.", [], 404);
  1569. }
  1570. if ($requestData['PERIODICIDAD'] === 'NR' && (is_null($requestData['FECHA_FINAL']) | is_null($requestData['HORA_FINAL'])) ) {
  1571. DB::rollBack();
  1572. return $this->responseController->makeResponse(true, "ERR_EMPLOYEE_UPDACTIVITY012: La fecha y la hora final esperada debe estar incluida en el formulario si la periodicidad está seleccionada como No se repite.", [], 404);
  1573. }
  1574. } else {
  1575. DB::rollBack();
  1576. return $this->responseController->makeResponse(true, "ERR_EMPLOYEE_UPDACTIVITY013: El tipo de actividad no es correcto.", [], 404);
  1577. }
  1578. $now = $this->functionsController->now();
  1579. $currentDate = $now->toDateTimeString();
  1580. try {
  1581. $validateInsert = DB::table('S002V01TCOAC')
  1582. ->where('COAC_IDAC','=',$idActivity)
  1583. ->where('COAC_NULI','=',$requestData['NUMERO_LINEA'])
  1584. ->where('COAC_IDPE','=',$requestData['ID_EMPLEADO'])
  1585. ->update([
  1586. 'COAC_ACTI' => $requestData['ACTIVIDAD'],
  1587. 'COAC_DESC' => $requestData['DESCRIPCION'],
  1588. 'COAC_TIPO' => $requestData['TIPO_ACTIVIDAD'],
  1589. 'COAC_OTPR' => $idPreventiveMaintenance,
  1590. 'COAC_OTCO' => $idCorrectiveMaintenance,
  1591. 'COAC_FEIN' => $requestData['FECHA_INICIO'],
  1592. 'COAC_HOIN' => $requestData['HORA_INICIO'],
  1593. 'COAC_FEFE' => $requestData['FECHA_FINAL'],
  1594. 'COAC_HOFE' => $requestData['HORA_FINAL'],
  1595. // 'COAC_FEFR' => $requestData[''],
  1596. // 'COAC_HOFR' => $requestData[''],
  1597. 'COAC_PERI' => $requestData['PERIODICIDAD'],
  1598. 'COAC_PEPE' => $requestData['PERIODICIDAD_PERSONALIZADA'],
  1599. 'COAC_CLAS' => $requestData['CLASIFICACION'],
  1600. 'COAC_COLO' => $requestData['COLOR'],
  1601. 'COAC_FEMO' => $currentDate,
  1602. 'COAC_USMO' => $user,
  1603. 'COAC_FEAR' => DB::raw('CURRENT_TIMESTAMP'),
  1604. ]);
  1605. } catch (\Throwable $th) {
  1606. DB::rollBack();
  1607. return $this->responseController->makeResponse(true, "ERR_EMPLOYEE_UPDACTIVITY014: Ocurrió un error al insertar los datos.", $th->getMessage(), 404);
  1608. }
  1609. if (!$validateInsert) {
  1610. DB::rollBack();
  1611. return $this->responseController->makeResponse(true, "ERR_EMPLOYEE_UPDACTIVITY015: No se pudo insertar los datos en la base de datos.", [], 404);
  1612. }
  1613. DB::commit();
  1614. return $this->responseController->makeResponse(false, "ÉXITO: Modificación Exitosa");
  1615. }
  1616. public function deleteActivity(Request $request, $idActivity) {
  1617. $valitador = Validator::make($request->all(), [
  1618. 'USUARIO' => 'required|string',
  1619. 'NUMERO_LINEA' => 'required|string',
  1620. ]);
  1621. if ($valitador->fails()) {
  1622. return $this->responseController->makeResponse(
  1623. true,
  1624. "ERR_EMPLOYEE_DELACTIVITY000: Se encontraron uno o más errores.",
  1625. $this->responseController->makeErrors($valitador->errors()->messages()),
  1626. 401
  1627. );
  1628. }
  1629. DB::beginTransaction();
  1630. $requestData = $request->all();
  1631. try {
  1632. $idActivity = $this->encryptionController->decrypt($idActivity);
  1633. } catch (\Throwable $th) {
  1634. DB::rollBack();
  1635. return $this->responseController->makeResponse(true, "ERR_EMPLOYEE_DELACTIVITY001: Ocurrió un error al desencriptar los datos.", $th->getMessage(), 401);
  1636. }
  1637. try {
  1638. $user = $this->encryptionController->decrypt($requestData['USUARIO']);
  1639. } catch (\Throwable $th) {
  1640. DB::rollBack();
  1641. return $this->responseController->makeResponse(true, "ERR_EMPLOYEE_DELACTIVITY002: Ocurrió un error al desencriptar los datos.", $th->getMessage(), 401);
  1642. }
  1643. $now = $this->functionsController->now();
  1644. $currentDate = $now->toDateTimeString();
  1645. try {
  1646. $validateDelete = DB::table('S002V01TCOAC')
  1647. ->where('COAC_NULI', '=', $requestData['NUMERO_LINEA'])
  1648. ->where('COAC_IDAC', '=', $idActivity)
  1649. ->update([
  1650. 'COAC_ESTA' => 'Eliminado',
  1651. 'COAC_FEMO' => $currentDate,
  1652. 'COAC_USMO' => $user,
  1653. 'COAC_FEAR' => DB::raw('CURRENT_TIMESTAMP'),
  1654. ]);
  1655. } catch (\Throwable $th) {
  1656. DB::rollBack();
  1657. return $this->responseController->makeResponse(true, "ERR_EMPLOYEE_DELACTIVITY003: Ocurrió un error al eliminar la actividad.", $th->getMessage(), 401);
  1658. }
  1659. if (!$validateDelete) {
  1660. DB::rollBack();
  1661. return $this->responseController->makeResponse(true, "ERR_EMPLOYEE_DELACTIVITY004: No se pudo eliminar la actividad.", [], 401);
  1662. }
  1663. DB::commit();
  1664. return $this->responseController->makeResponse(false, "ÉXITO: Modificación Exitosa");
  1665. }
  1666. public function getScheduleActivitiesByActivity($idActivity, $user, $line) {
  1667. try {
  1668. $idActivity = $this->encryptionController->decrypt($idActivity);
  1669. } catch (\Throwable $th) {
  1670. return $this->responseController->makeResponse(true, "ERR_EMPLOYEE_GETACTIVITY000: Ocurrió un error al ibtener los datos.", $th->getMessage(), 404);
  1671. }
  1672. try {
  1673. $arrSheduleActivities = (array) DB::table('S002V01TCOAC')
  1674. ->where('COAC_NULI','=',$line)
  1675. ->where('COAC_ESTA','=','Activo')
  1676. ->where('COAC_IDAC','=',$idActivity)
  1677. ->first([
  1678. 'COAC_IDPE AS ID_EMPLEADO',
  1679. 'COAC_ACTI AS ACTIVIDAD',
  1680. 'COAC_DESC AS DESCRIPCION',
  1681. 'COAC_TIPO AS TIPO_ACTIVIDAD',
  1682. 'COAC_OTPR AS ID_ORDEN_PREVENTIVO',
  1683. 'COAC_OTCO AS ID_ORDEN_CORRECTIVO',
  1684. 'COAC_FEIN AS FECHA_INICIO',
  1685. 'COAC_HOIN AS HORA_INICIO',
  1686. 'COAC_FEFE AS FECHA_FINAL',
  1687. 'COAC_HOFE AS HORA_FINAL',
  1688. 'COAC_FEFR AS FECHA_FINAL_REAL',
  1689. 'COAC_HOFR AS HORA_FINAL_REAL',
  1690. 'COAC_PERI AS PERIODICIDAD',
  1691. 'COAC_PEPE AS PERIODICIDAD_PERSONALIZADA',
  1692. 'COAC_CLAS AS CLASIFICACION',
  1693. 'COAC_COLO AS COLOR',
  1694. 'COAC_ESTA AS ESTADO',
  1695. 'COAC_FERE AS FECHA_REGISTRA',
  1696. 'COAC_USRE AS USUARIO_REGISTRA',
  1697. 'COAC_FEMO AS FECHA_MODIFICA',
  1698. 'COAC_USMO AS USUARIO_MODIFICA',
  1699. ]);
  1700. } catch (\Throwable $th) {
  1701. DB::rollBack();
  1702. return $this->responseController->makeResponse(true, "ERR_EMPLOYEE_GETACTIVITY001: Ocurrió un error al ibtener los datos.", $th->getMessage(), 404);
  1703. }
  1704. return $this->responseController->makeResponse(false, "ÉXITO: Consulta Exitosa", $arrSheduleActivities);
  1705. }
  1706. }