SubcontractController.php 58 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433
  1. <?php
  2. namespace App\Http\Controllers;
  3. use Carbon\Carbon;
  4. use Illuminate\Database\Query\Builder;
  5. use Illuminate\Http\Request;
  6. use Illuminate\Support\Facades\DB;
  7. use Illuminate\Support\Facades\Storage;
  8. use Illuminate\Support\Facades\Validator;
  9. use Illuminate\Http\File;
  10. use PhpOffice\PhpSpreadsheet\IOFactory;
  11. use PhpOffice\PhpSpreadsheet\Spreadsheet;
  12. use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
  13. use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
  14. use PhpOffice\PhpSpreadsheet\Style\Fill;
  15. use PhpOffice\PhpSpreadsheet\Style\Alignment;
  16. use PhpOffice\PhpSpreadsheet\Style\NumberFormat;
  17. use PhpOffice\PhpSpreadsheet\Style\Border;
  18. use stdClass;
  19. use Throwable;
  20. class SubcontractController extends Controller{
  21. private $responseController;
  22. private $encryptionController;
  23. private $documentManagementController;
  24. private $functionsController;
  25. public function __construct(){
  26. $this->responseController = new ResponseController();
  27. $this->encryptionController = new EncryptionController();
  28. $this->documentManagementController = new DocumentManagementController();
  29. $this->functionsController = new FunctionsController();
  30. }
  31. public function getConsultOfSubcontratists($idUser, $line) {
  32. DB::enableQueryLog();
  33. $idUser = $this->encryptionController->decrypt($idUser);
  34. if(!$idUser){
  35. return $this->responseController->makeResponse(true, 'El ID del usuario que realizó la solicitud no está encriptado correctamente', [], 400);
  36. }
  37. $usr = DB::table('S002V01TUSUA')->where([
  38. ['USUA_NULI', '=', $line],
  39. ['USUA_IDUS', '=', $idUser],
  40. ])->first();
  41. if(is_null($usr)){
  42. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado', [], 404);
  43. }
  44. $subcontratists = DB::table('S002V01TPESU')->select([
  45. 'PESU_IDPS as ID_SUBCONTRATIST',
  46. DB::raw('CONCAT(PESU_RASO, " (" , COALESCE(PESU_REFI, ""), ")") AS NAME'),
  47. 'PESU_EXTR AS TIPO',
  48. 'PESU_ESPE AS SPECIALTY',
  49. 'PESU_FERE AS REGISTER_DATE',
  50. 'PESU_FEMO AS UPDATE_DATE',
  51. 'PESU_USRE AS REGISTERED_BY_USER',
  52. 'PESU_USMO AS UPDATED_BY_USER',
  53. 'PESU_ESTA AS STATUS'
  54. ])->where('PESU_NULI', '=', $line)->get()->all();
  55. foreach($subcontratists as $subcontratist){
  56. $regUsr = DB::table('S002V01TUSUA')->where([
  57. ['USUA_NULI', '=', $line],
  58. ['USUA_IDUS', '=', $subcontratist->REGISTERED_BY_USER],
  59. ])->first();
  60. if(!is_null($regUsr)){
  61. $subcontratist->REGISTERED_BY_USER = $this->functionsController->joinName($regUsr->USUA_NOMB, $regUsr->USUA_APPA, $regUsr->USUA_APMA) . " (" . $subcontratist->REGISTERED_BY_USER . ")";
  62. }else{
  63. $subcontratist->REGISTERED_BY_USER = "DESCONOCIDO (" . $subcontratist->REGISTERED_BY_USER . ")";
  64. }
  65. if(!is_null($subcontratist->UPDATED_BY_USER)){
  66. $modUsr = DB::table('S002V01TUSUA')->where([
  67. ['USUA_NULI', '=', $line],
  68. ['USUA_IDUS', '=', $subcontratist->UPDATED_BY_USER],
  69. ])->first();
  70. if(!is_null($modUsr)){
  71. $subcontratist->UPDATED_BY_USER = $this->functionsController->joinName($modUsr->USUA_NOMB, $modUsr->USUA_APPA, $modUsr->USUA_APMA) . " (" . $subcontratist->UPDATED_BY_USER . ")";
  72. }else{
  73. $subcontratist->UPDATED_BY_USER = "DESCONOCIDO (" . $subcontratist->UPDATED_BY_USER . ")";
  74. }
  75. }
  76. if($subcontratist->TIPO == 'No'){
  77. $subcontratist->TIPO = 'Nacional';
  78. }else{
  79. $subcontratist->TIPO = 'Extranjero';
  80. }
  81. $preventiveInterventions = DB::select("
  82. SELECT
  83. DISTINCT(S002V01TOTPR.OTPR_IDOT),
  84. OPPR.*
  85. FROM
  86. S002V01TOTPR,
  87. JSON_TABLE(OTPR_OPPR, '$[*]' COLUMNS(
  88. OTPR_OPPR_ID INT PATH '$.ID',
  89. OTPR_OPPR_TYPE VARCHAR(100) PATH '$.TYPE'
  90. )) OPPR
  91. WHERE
  92. OPPR.OTPR_OPPR_TYPE = :emp_type
  93. AND OPPR.OTPR_OPPR_ID = :emp_id
  94. ", ['emp_type' => 'SU', 'emp_id' => $subcontratist->ID_SUBCONTRATIST]);
  95. //PENDIENTE IMPLEMENTAR INTERVENCIONES DE MANTENIMIENTO CORRECTIVO
  96. $subcontratist->INTERVENTIONS_COUNT = count($preventiveInterventions);
  97. }
  98. $now = $this->functionsController->now();
  99. $nowStr = $now->toDateTimeString();
  100. $actions = DB::getQueryLog();
  101. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  102. $idac = $this->functionsController->registerActivity(
  103. $line,
  104. 'S002V01M11GPRS',
  105. 'S002V01F01GESU',
  106. 'S002V01P01COSU',
  107. 'Consulta',
  108. "El usuario $name (" . $usr->USUA_IDUS . ") consultó los subcontratistas registrados.",
  109. $idUser,
  110. $nowStr,
  111. 'S002V01S01GESU'
  112. );
  113. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $line);
  114. return $this->responseController->makeResponse(false, 'EXITO', $subcontratists);
  115. }
  116. public function storeSubcontratist(Request $request) {
  117. DB::enableQueryLog();
  118. $request['TELEPHONE1'] = $this->encryptionController->decrypt($request->TELEPHONE1) ? $this->encryptionController->decrypt($request->TELEPHONE1) : 'ENC_ERR';
  119. $request['TELEPHONE2'] = $this->encryptionController->decrypt($request->TELEPHONE2) ? $this->encryptionController->decrypt($request->TELEPHONE2) : 'ENC_ERR';
  120. $request['EMAIL'] = $this->encryptionController->decrypt($request->EMAIL) ? $this->encryptionController->decrypt($request->EMAIL) : 'ENC_ERR';
  121. $request['RFC'] = $this->encryptionController->decrypt($request->RFC) ? $this->encryptionController->decrypt($request->RFC) : 'ENC_ERR';
  122. $request['TAX'] = $this->encryptionController->decrypt($request->TAX) ? $this->encryptionController->decrypt($request->TAX) : 'ENC_ERR';
  123. $validator = Validator::make($request->all(), [
  124. 'id_user' => 'required|string',
  125. 'linea' => 'required|integer',
  126. 'SOCIAL_REASON' => 'required|string|max:150',
  127. 'TAX_REFERENCE' => 'required|string|max:15',
  128. 'CONTRACT_TYPE' => 'required|string|in:Persona moral,Persona física',
  129. 'FOREIGNER' => 'required|string|in:Si,No',
  130. 'RFC' => 'required_if:FOREIGNER,=,No|string|max:13',
  131. 'TAX' => 'required_if:FOREIGNER,=,Si|string|max:13',
  132. 'EMAIL' => 'required|string|max:150',
  133. 'COUNTRY' => 'required|string|max:75',
  134. 'FEDERAL_ENTITY' => 'required|string|max:75',
  135. 'CITY' => 'string|max:75',
  136. 'TOWN' => 'string|max:75',
  137. 'SUBURB' => 'required|string|max:75',
  138. 'POSTAL_CODE' => 'required|string|max:5',
  139. 'STREET' => 'required|string|max:150',
  140. 'EXTERIOR_NUMBER' => 'required|integer',
  141. 'INTERIOR_NUMBER' => 'integer',
  142. 'LADA1' => 'required|string|max:15',
  143. 'TELEPHONE1' => 'required|string|min:7|max:11',
  144. 'LADA2' => 'string|max:15',
  145. 'TELEPHONE2' => 'string|min:7|max:11',
  146. 'SPECIALTY' => 'required|string|max:100'
  147. ]);
  148. if($validator->fails()){
  149. return $this->responseController->makeResponse(
  150. true,
  151. "Se encontraron uno o más errores.",
  152. $this->responseController->makeErrors(
  153. $validator->errors()->messages()
  154. ),
  155. 401
  156. );
  157. }
  158. $form = $request->all();
  159. foreach($form as $k=>$v){
  160. if($k == 'INTERIOR_NUMBER' && $v == '0'){
  161. unset($form[$k]);
  162. }else if($v == '-'){
  163. unset($form[$k]);
  164. }
  165. }
  166. $idUser = $this->encryptionController->decrypt($form['id_user']);
  167. if(!$idUser){
  168. return $this->responseController->makeResponse(true, 'El ID de usuario no fue encriptado correctamente.', [], 400);
  169. }
  170. $usr = DB::table('S002V01TUSUA')->where([
  171. ['USUA_NULI', '=', $form['linea']],
  172. ['USUA_IDUS', '=', $idUser]
  173. ])->first();
  174. if(is_null($usr)){
  175. return $this->responseController->makeResponse(true, 'El usuario que realizó la petición no existe.', [], 404);
  176. }
  177. $ladasValues = [];
  178. $ladas = DB::table('S002V01TPAIS')->select([
  179. 'PAIS_LADA AS LADA'
  180. ])->where('PAIS_NULI', '=', $form['linea'])->get()->all();
  181. foreach($ladas as $lada){
  182. if($lada->LADA != '' && $lada->LADA != '0'){
  183. $ladasValues[] = $lada->LADA;
  184. }
  185. }
  186. if(!in_array($form['LADA1'], $ladasValues)){
  187. return $this->responseController->makeResponse(true, "La lada $form[LADA1] no está relacionada a ningún país.", [], 400);
  188. }else if($form['TELEPHONE1'] == 'ENC_ERR'){
  189. return $this->responseController->makeResponse(true, 'El número telefónico 1 no fue encriptado correctamente.', [], 400);
  190. }
  191. $lada2 = null;
  192. $phone2 = null;
  193. if(isset($form['LADA2'])){
  194. if(!in_array($form['LADA2'], $ladasValues)){
  195. return $this->responseController->makeResponse(true, "La lada $form[LADA2] no está relacionada a ningún país.", [], 400);
  196. }else if($form['TELEPHONE2'] == 'ENC_ERR'){
  197. return $this->responseController->makeResponse(true, 'El número telefónico 2 no fue encriptado correctamente.', [], 400);
  198. }
  199. $lada2 = $form['LADA2'];
  200. $phone2 = $form['TELEPHONE2'];
  201. }
  202. if($form['EMAIL'] == 'ENC_ERR'){
  203. return $this->responseController->makeResponse(true, 'El email no fue encriptado correctamente.', [], 400);
  204. }
  205. $rfcx = null;
  206. if(isset($form['RFC']) && $form['FOREIGNER'] == 'No' && $form['RFC'] == 'ENC_ERR'){
  207. return $this->responseController->makeResponse(true, 'El RFC no fue encriptado correctamente.', [], 400);
  208. }else if(isset($form['RFC']) && $form['RFC'] != 'ENC_ERR'){
  209. $rfcx = $form['RFC'];
  210. }
  211. $taid = null;
  212. if(isset($form['TAX']) && $form['FOREIGNER'] == 'Si' && $form['TAX'] == 'ENC_ERR'){
  213. return $this->responseController->makeResponse(true, 'El TAX ID no fue encriptado correctamente.', [], 400);
  214. }else if(isset($form['TAX']) && $form['TAX'] != 'ENC_ERR'){
  215. $taid = $form['TAX'];
  216. }
  217. $country = DB::table('S002V01TPAIS')->where([
  218. ['PAIS_NULI', '=', $form['linea']],
  219. ['PAIS_IDPA', '=', $form['COUNTRY']],
  220. ])->first();
  221. if(is_null($country)){
  222. return $this->responseController->makeResponse(true, 'El país seleccionado no existe.', [], 404);
  223. }
  224. $taxRegime = DB::table('S002V01TREFI')->where([
  225. ['REFI_NULI', '=', $form['linea']],
  226. ['REFI_CRFI', '=', $form['TAX_REFERENCE']],
  227. ])->first();
  228. if(is_null($taxRegime)){
  229. return $this->responseController->makeResponse(true, 'El régimen fiscal seleccionado no existe.', [], 404);
  230. }
  231. $colo = null;
  232. $ciud = null;
  233. $loca = null;
  234. if($form['COUNTRY'] == 'MEX' || $form['COUNTRY'] == 'USA' || $form['COUNTRY'] == 'CAN'){
  235. $state = DB::table('S002V01TESTA')->where([
  236. ['ESTA_NULI', '=', $form['linea']],
  237. ['ESTA_COES', '=', $form['FEDERAL_ENTITY']],
  238. ['ESTA_COPA', '=', $form['COUNTRY']],
  239. ])->first();
  240. if(is_null($state)){
  241. return $this->responseController->makeResponse(true, 'El estado seleccionado no existe.', [], 404);
  242. }
  243. if($form['COUNTRY'] == 'MEX'){
  244. if(isset($form['CITY'])){
  245. $city = DB::table('S002V01TMUNI')->where([
  246. ['MUNI_NULI', '=', $form['linea']],
  247. ['MUNI_COMU', '=', $form['CITY']],
  248. ['MUNI_COES', '=', $form['FEDERAL_ENTITY']],
  249. ])->first();
  250. if(is_null($city)){
  251. return $this->responseController->makeResponse(true, 'El municipio seleccionado no existe.', [], 404);
  252. }else{
  253. $ciud = $form['CITY'];
  254. }
  255. }
  256. if(isset($form['TOWN'])){
  257. $town = DB::table('S002V01TLOCA')->where([
  258. ['LOCA_NULI', '=', $form['linea']],
  259. ['LOCA_COLO', '=', $form['TOWN']],
  260. ['LOCA_COES', '=', $form['FEDERAL_ENTITY']],
  261. ])->first();
  262. if(is_null($town)){
  263. return $this->responseController->makeResponse(true, 'La localidad seleccionada no existe.', [], 404);
  264. }else{
  265. $loca = $form['TOWN'];
  266. }
  267. }
  268. $setting = DB::table('S002V01TCOLO')->where([
  269. ['COLO_NULI', '=', $form['linea']],
  270. ['COLO_COCO', '=', $form['SUBURB']],
  271. ['COLO_COPO', '=', $form['POSTAL_CODE']],
  272. ])->first();
  273. if(is_null($setting)){
  274. return $this->responseController->makeResponse(true, 'La colonia seleccionada no existe.', [], 404);
  275. }else{
  276. $colo = $form['SUBURB'];
  277. }
  278. $zipCode = DB::table('S002V01TCOPO')->where([
  279. ['COPO_NULI', '=', $form['linea']],
  280. ['COPO_COPO', '=', $form['POSTAL_CODE']],
  281. ['COPO_COES', '=', $form['FEDERAL_ENTITY']],
  282. ])->first();
  283. if(is_null($zipCode)){
  284. return $this->responseController->makeResponse(true, 'El código postal seleccionado no existe.', [], 404);
  285. }
  286. }else{
  287. $colo = $form['SUBURB'];
  288. $ciud = $form['CITY'];
  289. $loca = isset($form['TOWN']) ? $form['TOWN'] : null;
  290. }
  291. }else{
  292. $colo = $form['SUBURB'];
  293. $ciud = $form['CITY'];
  294. $loca = isset($form['TOWN']) ? $form['TOWN'] : null;
  295. }
  296. $subArr = DB::table('S002V01TPESU')->where('PESU_NULI', '=', $form['linea'])->where(function(Builder $query) use ($form) {
  297. $query->where('PESU_RASO', '=', $form['SOCIAL_REASON'])
  298. ->orWhere('PESU_XRFC', '=', $form['RFC'])
  299. ->orWhere('PESU_CORR', '=', $form['EMAIL']);
  300. })->where('PESU_ESTA', '=', 'Activo')->get()->all();
  301. if(count($subArr) > 0){
  302. return $this->responseController->makeResponse(true, 'La razón social, el RFC o el correo electrónico ya fueron registrados.', [], 401);
  303. }
  304. $nuin = isset($form['INTERIOR_NUMBER']) ? $form['INTERIOR_NUMBER'] : null;
  305. $now = $this->functionsController->now();
  306. $nowStr = $now->toDateTimeString();
  307. $subcontratistID = DB::table('S002V01TPESU')->insertGetId([
  308. 'PESU_NULI' => $form['linea'],
  309. 'PESU_RASO' => $form['SOCIAL_REASON'],
  310. 'PESU_REFI' => $form['TAX_REFERENCE'],
  311. 'PESU_XRFC' => $rfcx,
  312. 'PESU_TIPO' => $form['CONTRACT_TYPE'],
  313. 'PESU_CORR' => $form['EMAIL'],
  314. 'PESU_EXTR' => $form['FOREIGNER'],
  315. 'PESU_TAID' => $taid,
  316. 'PESU_CALL' => $form['STREET'],
  317. 'PESU_NUEX' => $form['EXTERIOR_NUMBER'],
  318. 'PESU_NUIN' => $nuin,
  319. 'PESU_COLO' => $colo,
  320. 'PESU_CIUD' => $ciud,
  321. 'PESU_LOCA' => $loca,
  322. 'PESU_COPO' => $form['POSTAL_CODE'],
  323. 'PESU_ENFE' => $form['FEDERAL_ENTITY'],
  324. 'PESU_IDPA' => $form['COUNTRY'],
  325. 'PESU_TEL1' => $form['TELEPHONE1'],
  326. 'PESU_LAT1' => $form['LADA1'],
  327. 'PESU_TEL2' => $phone2,
  328. 'PESU_LAT2' => $lada2,
  329. 'PESU_ESPE' => $form['SPECIALTY'],
  330. 'PESU_USRE' => $idUser,
  331. 'PESU_FERE' => $nowStr,
  332. ]);
  333. $actions = DB::getQueryLog();
  334. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  335. $idac = $this->functionsController->registerActivity(
  336. $form['linea'],
  337. 'S002V01M11GPRS',
  338. 'S002V01F01GESU',
  339. 'S002V01P02RESU',
  340. 'Registro',
  341. "El usuario $name (" . $usr->USUA_IDUS . ") registró al subcontratista $form[SOCIAL_REASON] ($subcontratistID).",
  342. $idUser,
  343. $nowStr,
  344. 'S002V01S01GESU'
  345. );
  346. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $form['linea']);
  347. return $this->responseController->makeResponse(false, 'EXITO');
  348. }
  349. public function getSubcontratistById($idSub, $idUser, $line) {
  350. DB::enableQueryLog();
  351. $idUser = $this->encryptionController->decrypt($idUser);
  352. if(!$idUser){
  353. return $this->responseController->makeResponse(true, 'El ID del usuario que realizó la consulta no está encriptado correctamente.', [], 400);
  354. }
  355. $usr = DB::table('S002V01TUSUA')->where([
  356. ['USUA_IDUS', '=', $idUser],
  357. ['USUA_NULI', '=', $line]
  358. ])->first();
  359. if(is_null($usr)){
  360. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado', [], 404);
  361. }
  362. $idSub = $this->encryptionController->decrypt($idSub);
  363. if(!$idSub){
  364. return $this->responseController->makeResponse(true, 'El ID del subcontratista consultado no está encriptado correctamente.', [], 400);
  365. }
  366. $subcontratists_info = DB::table('S002V01TPESU')->select([
  367. 'PESU_IDPS as ID_SUBCONTRATIST',
  368. 'PESU_RASO as SOCIAL_REASON',
  369. 'PESU_REFI as TAX_REFERENCE',
  370. 'PESU_XRFC as RFC',
  371. 'PESU_TIPO as CONTRACT_TYPE',
  372. 'PESU_CORR as EMAIL',
  373. 'PESU_EXTR as FOREIGNER',
  374. 'PESU_TAID as TAX',
  375. 'PESU_CALL as STREET',
  376. 'PESU_NUEX as EXTERIOR_NUMBER',
  377. 'PESU_NUIN as INTERIOR_NUMBER',
  378. 'PESU_COLO as SUBURB',
  379. 'PESU_CIUD as CITY',
  380. 'PESU_LOCA as TOWN',
  381. 'PESU_COPO as POSTAL_CODE',
  382. 'PESU_ENFE as FEDERAL_ENTITY',
  383. 'PESU_IDPA as COUNTRY',
  384. 'PESU_TEL1 as TELEPHONE1',
  385. 'PESU_LAT1 as LADA1',
  386. 'PESU_TEL2 as TELEPHONE2',
  387. 'PESU_LAT2 as LADA2',
  388. 'PESU_ESPE as SPECIALTY',
  389. 'PESU_ESTA as STATUS'
  390. ])->where([
  391. ['PESU_NULI', '=', $line],
  392. ['PESU_IDPS', '=', $idSub],
  393. ])->first();
  394. $taxRegime = DB::table('S002V01TREFI')->where([
  395. ['REFI_NULI', '=', $line],
  396. ['REFI_CRFI', '=', $subcontratists_info->TAX_REFERENCE],
  397. ])->first();
  398. $subcontratists_info->TAX_REFERENCE = $taxRegime->REFI_DRFI . " (" . $subcontratists_info->TAX_REFERENCE . ")";
  399. $country = DB::table('S002V01TPAIS')->where([
  400. ['PAIS_NULI', '=', $line],
  401. ['PAIS_IDPA', '=', $subcontratists_info->COUNTRY],
  402. ])->first();
  403. $state = DB::table('S002V01TESTA')->where([
  404. ['ESTA_NULI', '=', $line],
  405. ['ESTA_COES', '=', $subcontratists_info->FEDERAL_ENTITY],
  406. ['ESTA_COPA', '=', $subcontratists_info->COUNTRY],
  407. ])->first();
  408. $city = DB::table('S002V01TMUNI')->where([
  409. ['MUNI_NULI', '=', $line],
  410. ['MUNI_COMU', '=', $subcontratists_info->CITY],
  411. ['MUNI_COES', '=', $subcontratists_info->FEDERAL_ENTITY],
  412. ])->first();
  413. $town = DB::table('S002V01TLOCA')->where([
  414. ['LOCA_NULI', '=', $line],
  415. ['LOCA_COLO', '=', $subcontratists_info->TOWN],
  416. ['LOCA_COES', '=', $subcontratists_info->FEDERAL_ENTITY],
  417. ])->first();
  418. $setting = DB::table('S002V01TCOLO')->where([
  419. ['COLO_NULI', '=', $line],
  420. ['COLO_COCO', '=', $subcontratists_info->SUBURB],
  421. ['COLO_COPO', '=', $subcontratists_info->POSTAL_CODE],
  422. ])->first();
  423. if(!is_null($country)){
  424. $subcontratists_info->COUNTRY = trim($country->PAIS_NOMB) . " (" . $subcontratists_info->COUNTRY . ")";
  425. }
  426. if(!is_null($state)){
  427. $subcontratists_info->FEDERAL_ENTITY = trim($state->ESTA_NOES) . " (" . $subcontratists_info->FEDERAL_ENTITY . ")";
  428. }
  429. if(!is_null($city)){
  430. $subcontratists_info->CITY = trim($city->MUNI_NOMU) . " (" . $subcontratists_info->CITY . ")";
  431. }
  432. if(!is_null($town)){
  433. $subcontratists_info->TOWN = trim($town->LOCA_NOLO) . " (" . $subcontratists_info->TOWN . ")";
  434. }
  435. if(!is_null($setting)){
  436. $subcontratists_info->SUBURB = trim($setting->COLO_NOCO) . " (" . $subcontratists_info->SUBURB . ")";
  437. }
  438. $now = $this->functionsController->now();
  439. $nowStr = $now->toDateTimeString();
  440. $actions = DB::getQueryLog();
  441. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  442. $idac = $this->functionsController->registerActivity(
  443. $line,
  444. 'S002V01M11GPRS',
  445. 'S002V01F01GESU',
  446. 'S002V01P03DESU',
  447. 'Consulta',
  448. "El usuario $name (" . $usr->USUA_IDUS . ") consultó al subcontratista " . $subcontratists_info->SOCIAL_REASON . " ($idSub).",
  449. $idUser,
  450. $nowStr,
  451. 'S002V01S01GESU'
  452. );
  453. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $line);
  454. return $this->responseController->makeResponse(false, 'EXITO', $subcontratists_info);
  455. }
  456. public function updateSubcontratist(Request $request) {
  457. DB::enableQueryLog();
  458. $request['TELEPHONE1'] = $this->encryptionController->decrypt($request->TELEPHONE1) ? $this->encryptionController->decrypt($request->TELEPHONE1) : 'ENC_ERR';
  459. $request['TELEPHONE2'] = $this->encryptionController->decrypt($request->TELEPHONE2) ? $this->encryptionController->decrypt($request->TELEPHONE2) : 'ENC_ERR';
  460. $request['EMAIL'] = $this->encryptionController->decrypt($request->EMAIL) ? $this->encryptionController->decrypt($request->EMAIL) : 'ENC_ERR';
  461. $request['RFC'] = $this->encryptionController->decrypt($request->RFC) ? $this->encryptionController->decrypt($request->RFC) : 'ENC_ERR';
  462. $request['TAX'] = $this->encryptionController->decrypt($request->TAX) ? $this->encryptionController->decrypt($request->TAX) : 'ENC_ERR';
  463. $validator = Validator::make($request->all(), [
  464. 'id_user' => 'required|string',
  465. 'linea' => 'required|integer',
  466. 'id_subcontratist' => 'required|string',
  467. 'SOCIAL_REASON' => 'required|string|max:150',
  468. 'TAX_REFERENCE' => 'required|string|max:15',
  469. 'CONTRACT_TYPE' => 'required|string|in:Persona moral,Persona física',
  470. 'FOREIGNER' => 'required|string|in:Si,No',
  471. 'RFC' => 'required_if:FOREIGNER,=,No|string|max:13',
  472. 'TAX' => 'required_if:FOREIGNER,=,Si|string|max:13',
  473. 'EMAIL' => 'required|string|max:150',
  474. 'COUNTRY' => 'required|string|max:75',
  475. 'FEDERAL_ENTITY' => 'required|string|max:75',
  476. 'CITY' => 'string|max:75',
  477. 'TOWN' => 'string|max:75',
  478. 'SUBURB' => 'required|string|max:75',
  479. 'POSTAL_CODE' => 'required|string|max:5',
  480. 'STREET' => 'required|string|max:150',
  481. 'EXTERIOR_NUMBER' => 'required|integer',
  482. 'INTERIOR_NUMBER' => 'integer',
  483. 'LADA1' => 'required|string|max:15',
  484. 'TELEPHONE1' => 'required|string|min:7|max:11',
  485. 'LADA2' => 'string|max:15',
  486. 'TELEPHONE2' => 'string|min:7|max:11',
  487. 'SPECIALTY' => 'required|string|max:100'
  488. ]);
  489. if($validator->fails()){
  490. return $this->responseController->makeResponse(
  491. true,
  492. "Se encontraron uno o más errores.",
  493. $this->responseController->makeErrors(
  494. $validator->errors()->messages()
  495. ),
  496. 401
  497. );
  498. }
  499. $form = $request->all();
  500. foreach($form as $k=>$v){
  501. if($k == 'INTERIOR_NUMBER' && $v == '0'){
  502. unset($form[$k]);
  503. }else if($v == '-'){
  504. unset($form[$k]);
  505. }
  506. }
  507. $idUser = $this->encryptionController->decrypt($form['id_user']);
  508. if(!$idUser){
  509. return $this->responseController->makeResponse(true, 'El ID de usuario no fue encriptado correctamente.', [], 400);
  510. }
  511. $usr = DB::table('S002V01TUSUA')->where([
  512. ['USUA_NULI', '=', $form['linea']],
  513. ['USUA_IDUS', '=', $idUser]
  514. ])->first();
  515. if(is_null($usr)){
  516. return $this->responseController->makeResponse(true, 'El usuario que realizó la petición no existe.', [], 404);
  517. }
  518. $idSub = $this->encryptionController->decrypt($form['id_subcontratist']);
  519. if(!$idSub){
  520. return $this->responseController->makeResponse(true, 'El ID del subcontratista no fue encriptado correctamente.', [], 400);
  521. }
  522. $sub = DB::table('S002V01TPESU')->where([
  523. ['PESU_NULI', '=', $form['linea']],
  524. ['PESU_IDPS', '=', $idSub],
  525. ])->first();
  526. if(is_null($sub)){
  527. return $this->responseController->makeResponse(true, 'El subcontratista solicitado no existe.', [], 404);
  528. }
  529. $ladasValues = [];
  530. $ladas = DB::table('S002V01TPAIS')->select([
  531. 'PAIS_LADA AS LADA'
  532. ])->where('PAIS_NULI', '=', $form['linea'])->get()->all();
  533. foreach($ladas as $lada){
  534. if($lada->LADA != '' && $lada->LADA != '0'){
  535. $ladasValues[] = $lada->LADA;
  536. }
  537. }
  538. if(!in_array($form['LADA1'], $ladasValues)){
  539. return $this->responseController->makeResponse(true, "La lada $form[LADA1] no está relacionada a ningún país.", [], 400);
  540. }else if($form['TELEPHONE1'] == 'ENC_ERR'){
  541. return $this->responseController->makeResponse(true, 'El número telefónico 1 no fue encriptado correctamente.', [], 400);
  542. }
  543. $lada2 = null;
  544. $phone2 = null;
  545. if(isset($form['LADA2'])){
  546. if(!in_array($form['LADA2'], $ladasValues)){
  547. return $this->responseController->makeResponse(true, "La lada $form[LADA2] no está relacionada a ningún país.", [], 400);
  548. }else if($form['TELEPHONE2'] == 'ENC_ERR'){
  549. return $this->responseController->makeResponse(true, 'El número telefónico 2 no fue encriptado correctamente.', [], 400);
  550. }
  551. $lada2 = $form['LADA2'];
  552. $phone2 = $form['TELEPHONE2'];
  553. }
  554. if($form['EMAIL'] == 'ENC_ERR'){
  555. return $this->responseController->makeResponse(true, 'El email no fue encriptado correctamente.', [], 400);
  556. }
  557. $rfcx = null;
  558. if(isset($form['RFC']) && $form['FOREIGNER'] == 'No' && $form['RFC'] == 'ENC_ERR'){
  559. return $this->responseController->makeResponse(true, 'El RFC no fue encriptado correctamente.', [], 400);
  560. }else if(isset($form['RFC']) && $form['RFC'] != 'ENC_ERR'){
  561. $rfcx = $form['RFC'];
  562. }
  563. $taid = null;
  564. if(isset($form['TAX']) && $form['FOREIGNER'] == 'Si' && $form['TAX'] == 'ENC_ERR'){
  565. return $this->responseController->makeResponse(true, 'El TAX ID no fue encriptado correctamente.', [], 400);
  566. }else if(isset($form['TAX']) && $form['TAX'] != 'ENC_ERR'){
  567. $taid = $form['TAX'];
  568. }
  569. $country = DB::table('S002V01TPAIS')->where([
  570. ['PAIS_NULI', '=', $form['linea']],
  571. ['PAIS_IDPA', '=', $form['COUNTRY']],
  572. ])->first();
  573. if(is_null($country)){
  574. return $this->responseController->makeResponse(true, 'El país seleccionado no existe.', [], 404);
  575. }
  576. $taxRegime = DB::table('S002V01TREFI')->where([
  577. ['REFI_NULI', '=', $form['linea']],
  578. ['REFI_CRFI', '=', $form['TAX_REFERENCE']],
  579. ])->first();
  580. if(is_null($taxRegime)){
  581. return $this->responseController->makeResponse(true, 'El régimen fiscal seleccionado no existe.', [], 404);
  582. }
  583. $colo = null;
  584. $ciud = null;
  585. $loca = null;
  586. if($form['COUNTRY'] == 'MEX' || $form['COUNTRY'] == 'USA' || $form['COUNTRY'] == 'CAN'){
  587. $state = DB::table('S002V01TESTA')->where([
  588. ['ESTA_NULI', '=', $form['linea']],
  589. ['ESTA_COES', '=', $form['FEDERAL_ENTITY']],
  590. ['ESTA_COPA', '=', $form['COUNTRY']],
  591. ])->first();
  592. if(is_null($state)){
  593. return $this->responseController->makeResponse(true, 'El estado seleccionado no existe.', [], 404);
  594. }
  595. if($form['COUNTRY'] == 'MEX'){
  596. if(isset($form['CITY'])){
  597. $city = DB::table('S002V01TMUNI')->where([
  598. ['MUNI_NULI', '=', $form['linea']],
  599. ['MUNI_COMU', '=', $form['CITY']],
  600. ['MUNI_COES', '=', $form['FEDERAL_ENTITY']],
  601. ])->first();
  602. if(is_null($city)){
  603. return $this->responseController->makeResponse(true, 'El municipio seleccionado no existe.', [], 404);
  604. }else{
  605. $ciud = $form['CITY'];
  606. }
  607. }
  608. if(isset($form['TOWN'])){
  609. $town = DB::table('S002V01TLOCA')->where([
  610. ['LOCA_NULI', '=', $form['linea']],
  611. ['LOCA_COLO', '=', $form['TOWN']],
  612. ['LOCA_COES', '=', $form['FEDERAL_ENTITY']],
  613. ])->first();
  614. if(is_null($town)){
  615. return $this->responseController->makeResponse(true, 'La localidad seleccionada no existe.', [], 404);
  616. }else{
  617. $loca = $form['TOWN'];
  618. }
  619. }
  620. $setting = DB::table('S002V01TCOLO')->where([
  621. ['COLO_NULI', '=', $form['linea']],
  622. ['COLO_COCO', '=', $form['SUBURB']],
  623. ['COLO_COPO', '=', $form['POSTAL_CODE']],
  624. ])->first();
  625. if(is_null($setting)){
  626. return $this->responseController->makeResponse(true, 'La colonia seleccionada no existe.', [], 404);
  627. }else{
  628. $colo = $form['SUBURB'];
  629. }
  630. $zipCode = DB::table('S002V01TCOPO')->where([
  631. ['COPO_NULI', '=', $form['linea']],
  632. ['COPO_COPO', '=', $form['POSTAL_CODE']],
  633. ['COPO_COES', '=', $form['FEDERAL_ENTITY']],
  634. ])->first();
  635. if(is_null($zipCode)){
  636. return $this->responseController->makeResponse(true, 'El código postal seleccionado no existe.', [], 404);
  637. }
  638. }else{
  639. $colo = $form['SUBURB'];
  640. $ciud = $form['CITY'];
  641. $loca = isset($form['TOWN']) ? $form['TOWN'] : null;
  642. }
  643. }else{
  644. $colo = $form['SUBURB'];
  645. $ciud = $form['CITY'];
  646. $loca = isset($form['TOWN']) ? $form['TOWN'] : null;
  647. }
  648. $subArr = DB::table('S002V01TPESU')->where('PESU_NULI', '=', $form['linea'])->where(function(Builder $query) use ($form) {
  649. $query->where('PESU_RASO', '=', $form['SOCIAL_REASON'])
  650. ->orWhere('PESU_XRFC', '=', $form['RFC'])
  651. ->orWhere('PESU_CORR', '=', $form['EMAIL']);
  652. })->where([
  653. ['PESU_IDPS', '!=', $idSub],
  654. ['PESU_ESTA', '=', 'Activo'],
  655. ])->get()->all();
  656. if(count($subArr) > 0){
  657. return $this->responseController->makeResponse(true, 'La razón social, el RFC o el correo electrónico ya fueron registrados.', [], 401);
  658. }
  659. $nuin = isset($form['INTERIOR_NUMBER']) ? $form['INTERIOR_NUMBER'] : null;
  660. $now = $this->functionsController->now();
  661. $nowStr = $now->toDateTimeString();
  662. DB::table('S002V01TPESU')->where([
  663. ['PESU_NULI', '=', $form['linea']],
  664. ['PESU_IDPS', '=', $idSub],
  665. ])->update([
  666. 'PESU_RASO' => $form['SOCIAL_REASON'],
  667. 'PESU_REFI' => $form['TAX_REFERENCE'],
  668. 'PESU_XRFC' => $rfcx,
  669. 'PESU_TIPO' => $form['CONTRACT_TYPE'],
  670. 'PESU_CORR' => $form['EMAIL'],
  671. 'PESU_EXTR' => $form['FOREIGNER'],
  672. 'PESU_TAID' => $taid,
  673. 'PESU_CALL' => $form['STREET'],
  674. 'PESU_NUEX' => $form['EXTERIOR_NUMBER'],
  675. 'PESU_NUIN' => $nuin,
  676. 'PESU_COLO' => $colo,
  677. 'PESU_CIUD' => $ciud,
  678. 'PESU_LOCA' => $loca,
  679. 'PESU_COPO' => $form['POSTAL_CODE'],
  680. 'PESU_ENFE' => $form['FEDERAL_ENTITY'],
  681. 'PESU_IDPA' => $form['COUNTRY'],
  682. 'PESU_TEL1' => $form['TELEPHONE1'],
  683. 'PESU_LAT1' => $form['LADA1'],
  684. 'PESU_TEL2' => $phone2,
  685. 'PESU_LAT2' => $lada2,
  686. 'PESU_ESPE' => $form['SPECIALTY'],
  687. 'PESU_USMO' => $idUser,
  688. 'PESU_FEMO' => $nowStr,
  689. ]);
  690. $actions = DB::getQueryLog();
  691. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  692. $idac = $this->functionsController->registerActivity(
  693. $form['linea'],
  694. 'S002V01M11GPRS',
  695. 'S002V01F01GESU',
  696. 'S002V01P02RESU',
  697. 'Actualización',
  698. "El usuario $name (" . $usr->USUA_IDUS . ") actualizó al subcontratista $idSub.",
  699. $idUser,
  700. $nowStr,
  701. 'S002V01S01GESU'
  702. );
  703. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $form['linea']);
  704. return $this->responseController->makeResponse(false, 'EXITO');
  705. }
  706. public function updateToInactiveStatus(Request $request) {
  707. DB::enableQueryLog();
  708. $validator = Validator::make($request->all(), [
  709. 'id_user' => 'required|string',
  710. 'linea' => 'required|integer',
  711. 'id_subcontratist' => 'required|string',
  712. ]);
  713. if($validator->fails()){
  714. return $this->responseController->makeResponse(
  715. true,
  716. "Se encontraron uno o más errores.",
  717. $this->responseController->makeErrors(
  718. $validator->errors()->messages()
  719. ),
  720. 401
  721. );
  722. }
  723. $form = $request->all();
  724. $idUser = $this->encryptionController->decrypt($form['id_user']);
  725. if(!$idUser){
  726. return $this->responseController->makeResponse(true, 'El ID de usuario no fue encriptado correctamente.', [], 400);
  727. }
  728. $usr = DB::table('S002V01TUSUA')->where([
  729. ['USUA_NULI', '=', $form['linea']],
  730. ['USUA_IDUS', '=', $idUser]
  731. ])->first();
  732. if(is_null($usr)){
  733. return $this->responseController->makeResponse(true, 'El usuario que realizó la petición no existe.', [], 404);
  734. }
  735. $idSub = $this->encryptionController->decrypt($form['id_subcontratist']);
  736. if(!$idSub){
  737. return $this->responseController->makeResponse(true, 'El ID del subcontratista no fue encriptado correctamente.', [], 400);
  738. }
  739. $sub = DB::table('S002V01TPESU')->where([
  740. ['PESU_NULI', '=', $form['linea']],
  741. ['PESU_IDPS', '=', $idSub],
  742. ])->first();
  743. if(is_null($sub)){
  744. return $this->responseController->makeResponse(true, 'El subcontratista solicitado no existe.', [], 404);
  745. }
  746. $users = DB::table('S002V01TPERS')->where([
  747. ['PERS_NULI', '=', $form['linea']],
  748. ['PERS_ESTA', '=', 'Activo'],
  749. ])->join('S002V01TPESU', 'PERS_IDPS', '=', 'PESU_IDPS')->get()->all();
  750. if(count($users) > 0){
  751. return $this->responseController->makeResponse(true, 'El subcontratista que desea eliminar tiene usuarios relacionados.', [], 401);
  752. }
  753. $now = $this->functionsController->now();
  754. $nowStr = $now->toDateTimeString();
  755. DB::table('S002V01TPESU')->where([
  756. ['PESU_NULI', '=', $form['linea']],
  757. ['PESU_IDPS', '=', $idSub],
  758. ])->update([
  759. 'PESU_ESTA' => 'Eliminado',
  760. 'PESU_USMO' => $idUser,
  761. 'PESU_FEMO' => $nowStr
  762. ]);
  763. $actions = DB::getQueryLog();
  764. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  765. $idac = $this->functionsController->registerActivity(
  766. $form['linea'],
  767. 'S002V01M11GPRS',
  768. 'S002V01F01GESU',
  769. 'S002V01P02RESU',
  770. 'Eliminación',
  771. "El usuario $name (" . $usr->USUA_IDUS . ") eliminó al subcontratista $idSub.",
  772. $idUser,
  773. $nowStr,
  774. 'S002V01S01GESU'
  775. );
  776. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $form['linea']);
  777. return $this->responseController->makeResponse(false, 'EXITO');
  778. }
  779. public function getContractsOfEverySubcontratist($idUser, $line) {
  780. DB::enableQueryLog();
  781. $idUser = $this->encryptionController->decrypt($idUser);
  782. if(!$idUser){
  783. return $this->responseController->makeResponse(true, 'El ID del usuario que realizó la solicitud no está encriptado correctamente', [], 400);
  784. }
  785. $usr = DB::table('S002V01TUSUA')->where([
  786. ['USUA_NULI', '=', $line],
  787. ['USUA_IDUS', '=', $idUser],
  788. ])->first();
  789. if(is_null($usr)){
  790. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado', [], 404);
  791. }
  792. $contractsBySubcontratists = DB::table('S002V01TPERS')->select([
  793. DB::raw("
  794. TRIM(CONCAT(
  795. USUA_NOMB,
  796. ' ',
  797. USUA_APPA,
  798. IF(ISNULL(USUA_APMA), '', CONCAT(' ', USUA_APMA)),
  799. ' <(',
  800. PERS_IDPE,
  801. ')> (',
  802. PERS_IDUS,
  803. ')'
  804. )) AS EMPLOYEE
  805. "),
  806. DB::raw("COUNT(CONT_IDCO) AS CONTRACTS")
  807. ])->leftJoin('S002V01TCONT', 'CONT_IDEM', '=', 'PERS_IDPE')
  808. ->join('S002V01TUSUA', 'USUA_IDUS', '=', 'PERS_IDUS')
  809. ->groupBy('EMPLOYEE')->where([
  810. ['PERS_NULI', '=', $line],
  811. ['PERS_TICO', '=', 'Subcontratista'],
  812. ])->get()->all();
  813. foreach($contractsBySubcontratists as $key=>$val){
  814. $employeeArr = explode('<', $val->EMPLOYEE);
  815. $employeeName = $employeeArr[0];
  816. $employeeArr = array_reverse($employeeArr);
  817. $employeeArr = explode('>', $employeeArr[0]);
  818. $employeeID = str_replace('(', '', $employeeArr[0]);
  819. $employeeID = str_replace(')', '', $employeeID);
  820. $val->ID_EMPLOYEE = $this->encryptionController->encrypt($employeeID);
  821. $val->EMPLOYEE = $employeeName . join('', $employeeArr);
  822. $contractsBySubcontratists[$key] = $val;
  823. }
  824. $now = $this->functionsController->now();
  825. $nowStr = $now->toDateTimeString();
  826. $actions = DB::getQueryLog();
  827. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  828. $idac = $this->functionsController->registerActivity(
  829. $line,
  830. 'S002V01M11GPRS',
  831. 'S002V01F02ADCO',
  832. 'S002V01P01HCSU',
  833. 'Consulta',
  834. "El usuario $name (" . $usr->USUA_IDUS . ") consultó los contratos por subcontratista.",
  835. $idUser,
  836. $nowStr,
  837. 'S002V01S01GESU'
  838. );
  839. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $line);
  840. return $this->responseController->makeResponse(false, 'EXITO', $contractsBySubcontratists);
  841. }
  842. public function getContractsBySubcontratist($idSubcontratist, $idUser, $line) {
  843. DB::enableQueryLog();
  844. $idUser = $this->encryptionController->decrypt($idUser);
  845. if(!$idUser){
  846. return $this->responseController->makeResponse(true, 'El ID de usuario no fue encriptado correctamente.', [], 400);
  847. }
  848. $usr = DB::table('S002V01TUSUA')->where([
  849. ['USUA_NULI', '=', $line],
  850. ['USUA_IDUS', '=', $idUser]
  851. ])->first();
  852. if(is_null($usr)){
  853. return $this->responseController->makeResponse(true, 'El usuario que realizó la petición no existe.', [], 404);
  854. }
  855. $idSubcontratist = $this->encryptionController->decrypt($idSubcontratist);
  856. if(!$idSubcontratist){
  857. return $this->responseController->makeResponse(true, 'El ID del subcontratista no fue encriptado correctamente.', [], 400);
  858. }
  859. $sub = DB::table('S002V01TPERS')->where([
  860. ['PERS_NULI', '=', $line],
  861. ['PERS_IDPE', '=', $idSubcontratist],
  862. ['PERS_TICO', '=', 'Subcontratista'],
  863. ])->first();
  864. if(is_null($sub)){
  865. return $this->responseController->makeResponse(true, 'El subcontratista solicitado no existe.', [], 404);
  866. }
  867. $contracts = DB::table('S002V01TCONT')->select([
  868. 'CONT_NOCO AS CONTRACT_NAME',
  869. 'CONT_FEIN AS START_DATE',
  870. 'CONT_FEFI AS END_DATE',
  871. 'CONT_COST AS COST',
  872. ])->where([
  873. ['CONT_NULI', '=', $line],
  874. ['CONT_IDEM', '=', $idSubcontratist],
  875. ['CONT_TIPO', '=', 'Subcontratista'],
  876. ])->get()->all();
  877. $now = $this->functionsController->now();
  878. $nowStr = $now->toDateTimeString();
  879. $actions = DB::getQueryLog();
  880. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  881. $idac = $this->functionsController->registerActivity(
  882. $line,
  883. 'S002V01M11GPRS',
  884. 'S002V01F02ADCO',
  885. 'S002V01P01HCSU',
  886. 'Consulta',
  887. "El usuario $name (" . $usr->USUA_IDUS . ") consultó los contratos del subcontratista #$idSubcontratist.",
  888. $idUser,
  889. $nowStr,
  890. 'S002V01S01GESU'
  891. );
  892. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $line);
  893. return $this->responseController->makeResponse(false, 'EXITO', $contracts);
  894. }
  895. public function downloadSubcontractInfoOnExcel($idSubcontratist, $idUser, $line){
  896. DB::enableQueryLog();
  897. $idUser = $this->encryptionController->decrypt($idUser);
  898. if(!$idUser){
  899. return $this->responseController->makeResponse(true, 'El ID de usuario no fue encriptado correctamente.', [], 400);
  900. }
  901. $usr = DB::table('S002V01TUSUA')->where([
  902. ['USUA_NULI', '=', $line],
  903. ['USUA_IDUS', '=', $idUser]
  904. ])->first();
  905. if(is_null($usr)){
  906. return $this->responseController->makeResponse(true, 'El usuario que realizó la petición no existe.', [], 404);
  907. }
  908. $idSubcontratist = $this->encryptionController->decrypt($idSubcontratist);
  909. if(!$idSubcontratist){
  910. return $this->responseController->makeResponse(true, 'El ID del subcontratista no fue encriptado correctamente.', [], 400);
  911. }
  912. $sub = DB::table('S002V01TPERS')->where([
  913. ['PERS_NULI', '=', $line],
  914. ['PERS_IDPE', '=', $idSubcontratist],
  915. ['PERS_TICO', '=', 'Subcontratista'],
  916. ])->first();
  917. if(is_null($sub)){
  918. return $this->responseController->makeResponse(true, 'El subcontratista solicitado no existe.', [], 404);
  919. }
  920. $contracts = DB::table('S002V01TCONT')->select([
  921. 'CONT_IDCO AS IDCONTRATO',
  922. 'CONT_NOCO AS NOMBRECONTRATO',
  923. 'CONT_IDOT AS IDORDEN',
  924. 'CONT_TOTR AS TIPOORDEN',
  925. 'PERS_ESPE AS ESPECIALIDAD',
  926. 'CONT_FEIN AS FECHAINICIO',
  927. 'CONT_FEFI AS FECHAFIN',
  928. 'CONT_COST AS COSTO',
  929. ])->join('S002V01TPERS', 'PERS_IDPE', '=', 'CONT_IDEM')->where([
  930. ['CONT_NULI', '=', $line],
  931. ['CONT_IDEM', '=', $idSubcontratist],
  932. ['CONT_TIPO', '=', 'Subcontratista'],
  933. ])->get()->all();
  934. $document = $this->generateDocument($contracts);
  935. $now = $this->functionsController->now();
  936. $nowStr = $now->toDateTimeString();
  937. $dateTimeArr = explode(" ", $nowStr);
  938. $dateArr = explode("-", $dateTimeArr[0]);
  939. $year = substr($dateArr[0], 2);
  940. $como = 'GPRS';
  941. $cldo = 'IN';
  942. $fecr = $year . $dateArr[1] . $dateArr[2];
  943. $sec = DB::table('S002V01TAFAL')->where([
  944. ['AFAL_NULI', '=', $line],
  945. ['AFAL_COMO', '=', $como],
  946. ['AFAL_CLDO', '=', $cldo],
  947. ])->orderBy('AFAL_NUSE', 'desc')->first();
  948. $nuse = "";
  949. if(is_null($sec)){
  950. $nuse = '000001';
  951. }else{
  952. $secu = "" . intval($sec->AFAL_NUSE) + 1 . "";
  953. $nuse = "";
  954. for($i = strlen($secu); $i < 6; $i++){
  955. $nuse .= "0";
  956. }
  957. $nuse = $nuse . $secu;
  958. }
  959. $noar = "ficha_de_subcontratacion_del_subcontratista_$idSubcontratist";
  960. $exte = "xlsx";
  961. $ver = DB::table('S002V01TAFAL')->where([
  962. ['AFAL_NULI', '=', $line],
  963. ['AFAL_COMO', '=', $como],
  964. ['AFAL_CLDO', '=', $cldo],
  965. ['AFAL_NOAR', '=', $noar],
  966. ['AFAL_EXTE', '=', $exte],
  967. ])->orderBy('AFAL_NUVE', 'desc')->first();
  968. $nuve = "";
  969. if(is_null($ver)){
  970. $nuve = "01";
  971. }else{
  972. $vers = intval($ver->AFAL_NUVE) + 1;
  973. $nuve = $vers < 10 ? "0$vers" : "$vers";
  974. }
  975. $line = $line < 10 ? "0$line" : "$line";
  976. $filePath = 'C:\inetpub\wwwroot\sam\public_files\\';
  977. $fileName = "$line-$como-$cldo-$fecr-$nuse=$nuve=$noar.$exte";
  978. $tempFile = $filePath . $fileName;
  979. if(file_exists($tempFile)){
  980. unlink($tempFile);
  981. }
  982. $writer = IOFactory::createWriter($document, 'Xlsx');
  983. $writer->save($tempFile);
  984. $ubic = Storage::putFile('files', new File($tempFile));
  985. $ubic = str_replace("/", "\\", $ubic);
  986. $ubic = "C:\inetpub\wwwroot\sam\storage\app\\" . $ubic;
  987. $tama = filesize($ubic);
  988. $usac = json_encode([$idUser]);
  989. unlink($tempFile);
  990. DB::table('S002V01TAFAL')->insert([
  991. 'AFAL_NULI' => $line,
  992. 'AFAL_COMO' => $como,
  993. 'AFAL_CLDO' => $cldo,
  994. 'AFAL_FECR' => $fecr,
  995. 'AFAL_NUSE' => $nuse,
  996. 'AFAL_NUVE' => $nuve,
  997. 'AFAL_NOAR' => $noar,
  998. 'AFAL_EXTE' => $exte,
  999. 'AFAL_TAMA' => $tama,
  1000. 'AFAL_UBIC' => $ubic,
  1001. 'AFAL_USAC' => $usac,
  1002. 'AFAL_USRE' => $idUser,
  1003. 'AFAL_FERE' => $nowStr,
  1004. ]);
  1005. $actions = DB::getQueryLog();
  1006. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  1007. $idac = $this->functionsController->registerActivity(
  1008. $line,
  1009. 'S002V01M11GPRS',
  1010. 'S002V01F02ADCO',
  1011. 'S002V01P02FISU',
  1012. 'Registro',
  1013. "El usuario $name (" . $usr->USUA_IDUS . ") generó la ficha de subcontratación del subcontratista #$idSubcontratist para Excel.",
  1014. $idUser,
  1015. $nowStr,
  1016. 'S002V01S01GESU'
  1017. );
  1018. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $line);
  1019. return $this->responseController->makeResponse(false, 'EXITO.', ['fileID' => $fileName]);
  1020. }
  1021. public function downloadSubcontractInfoOnPdf($idSubcontratist, $idUser, $line) {
  1022. DB::enableQueryLog();
  1023. $idUser = $this->encryptionController->decrypt($idUser);
  1024. if(!$idUser){
  1025. return $this->responseController->makeResponse(true, 'El ID de usuario no fue encriptado correctamente.', [], 400);
  1026. }
  1027. $usr = DB::table('S002V01TUSUA')->where([
  1028. ['USUA_NULI', '=', $line],
  1029. ['USUA_IDUS', '=', $idUser]
  1030. ])->first();
  1031. if(is_null($usr)){
  1032. return $this->responseController->makeResponse(true, 'El usuario que realizó la petición no existe.', [], 404);
  1033. }
  1034. $idSubcontratist = $this->encryptionController->decrypt($idSubcontratist);
  1035. if(!$idSubcontratist){
  1036. return $this->responseController->makeResponse(true, 'El ID del subcontratista no fue encriptado correctamente.', [], 400);
  1037. }
  1038. $sub = DB::table('S002V01TPERS')->where([
  1039. ['PERS_NULI', '=', $line],
  1040. ['PERS_IDPE', '=', $idSubcontratist],
  1041. ['PERS_TICO', '=', 'Subcontratista'],
  1042. ])->first();
  1043. if(is_null($sub)){
  1044. return $this->responseController->makeResponse(true, 'El subcontratista solicitado no existe.', [], 404);
  1045. }
  1046. $contracts = DB::table('S002V01TCONT')->select([
  1047. 'CONT_IDCO AS IDCONTRATO',
  1048. 'CONT_NOCO AS NOMBRECONTRATO',
  1049. 'CONT_IDOT AS IDORDEN',
  1050. 'CONT_TOTR AS TIPOORDEN',
  1051. 'PERS_ESPE AS ESPECIALIDAD',
  1052. 'CONT_FEIN AS FECHAINICIO',
  1053. 'CONT_FEFI AS FECHAFIN',
  1054. 'CONT_COST AS COSTO',
  1055. ])->join('S002V01TPERS', 'PERS_IDPE', '=', 'CONT_IDEM')->where([
  1056. ['CONT_NULI', '=', $line],
  1057. ['CONT_IDEM', '=', $idSubcontratist],
  1058. ['CONT_TIPO', '=', 'Subcontratista'],
  1059. ])->get()->all();
  1060. $document = $this->generateDocument($contracts);
  1061. $now = $this->functionsController->now();
  1062. $nowStr = $now->toDateTimeString();
  1063. $dateTimeArr = explode(" ", $nowStr);
  1064. $dateArr = explode("-", $dateTimeArr[0]);
  1065. $year = substr($dateArr[0], 2);
  1066. $como = 'GPRS';
  1067. $cldo = 'IN';
  1068. $fecr = $year . $dateArr[1] . $dateArr[2];
  1069. $sec = DB::table('S002V01TAFAL')->where([
  1070. ['AFAL_NULI', '=', $line],
  1071. ['AFAL_COMO', '=', $como],
  1072. ['AFAL_CLDO', '=', $cldo],
  1073. ])->orderBy('AFAL_NUSE', 'desc')->first();
  1074. $nuse = "";
  1075. if(is_null($sec)){
  1076. $nuse = '000001';
  1077. }else{
  1078. $secu = "" . intval($sec->AFAL_NUSE) + 1 . "";
  1079. $nuse = "";
  1080. for($i = strlen($secu); $i < 6; $i++){
  1081. $nuse .= "0";
  1082. }
  1083. $nuse = $nuse . $secu;
  1084. }
  1085. $noar = "ficha_de_subcontratacion_del_subcontratista_$idSubcontratist";
  1086. $exte = "pdf";
  1087. $ver = DB::table('S002V01TAFAL')->where([
  1088. ['AFAL_NULI', '=', $line],
  1089. ['AFAL_COMO', '=', $como],
  1090. ['AFAL_CLDO', '=', $cldo],
  1091. ['AFAL_NOAR', '=', $noar],
  1092. ['AFAL_EXTE', '=', $exte],
  1093. ])->orderBy('AFAL_NUVE', 'desc')->first();
  1094. $nuve = "";
  1095. if(is_null($ver)){
  1096. $nuve = "01";
  1097. }else{
  1098. $vers = intval($ver->AFAL_NUVE) + 1;
  1099. $nuve = $vers < 10 ? "0$vers" : "$vers";
  1100. }
  1101. $line = $line < 10 ? "0$line" : "$line";
  1102. $filePath = 'C:\inetpub\wwwroot\sam\public_files\\';
  1103. $fileName = "$line-$como-$cldo-$fecr-$nuse=$nuve=$noar.$exte";
  1104. $tempFile = $filePath . $fileName;
  1105. if(file_exists($tempFile)){
  1106. unlink($tempFile);
  1107. }
  1108. $writer = IOFactory::createWriter($document, 'Mpdf');
  1109. $writer->save($tempFile);
  1110. $ubic = Storage::putFile('files', new File($tempFile));
  1111. $ubic = str_replace("/", "\\", $ubic);
  1112. $ubic = "C:\inetpub\wwwroot\sam\storage\app\\" . $ubic;
  1113. $tama = filesize($ubic);
  1114. $usac = json_encode([$idUser]);
  1115. unlink($tempFile);
  1116. DB::table('S002V01TAFAL')->insert([
  1117. 'AFAL_NULI' => $line,
  1118. 'AFAL_COMO' => $como,
  1119. 'AFAL_CLDO' => $cldo,
  1120. 'AFAL_FECR' => $fecr,
  1121. 'AFAL_NUSE' => $nuse,
  1122. 'AFAL_NUVE' => $nuve,
  1123. 'AFAL_NOAR' => $noar,
  1124. 'AFAL_EXTE' => $exte,
  1125. 'AFAL_TAMA' => $tama,
  1126. 'AFAL_UBIC' => $ubic,
  1127. 'AFAL_USAC' => $usac,
  1128. 'AFAL_USRE' => $idUser,
  1129. 'AFAL_FERE' => $nowStr,
  1130. ]);
  1131. $actions = DB::getQueryLog();
  1132. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  1133. $idac = $this->functionsController->registerActivity(
  1134. $line,
  1135. 'S002V01M11GPRS',
  1136. 'S002V01F02ADCO',
  1137. 'S002V01P02FISU',
  1138. 'Registro',
  1139. "El usuario $name (" . $usr->USUA_IDUS . ") generó la ficha de subcontratación del subcontratista #$idSubcontratist para Excel.",
  1140. $idUser,
  1141. $nowStr,
  1142. 'S002V01S01GESU'
  1143. );
  1144. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $line);
  1145. return $this->responseController->makeResponse(false, 'EXITO.', ['fileID' => $fileName]);
  1146. }
  1147. private function generateDocument($contracts) : Spreadsheet {
  1148. $spreadsheet = new Spreadsheet;
  1149. $spreadsheet->getProperties()
  1150. ->setCreator('STC')
  1151. ->setTitle('Historial de contratos por subcontratista.')
  1152. ->setSubject('Historial documento')
  1153. ->setKeywords('Subcontratista Contratos Historial')
  1154. ->setCategory('Historial archivo');
  1155. $worksheet = $spreadsheet->getActiveSheet();
  1156. $worksheet->setTitle('HISTORIAL DE CONTRATOS');
  1157. $columns = ['# DE CONTRATO', 'NOMBRE DEL CONTRATO', 'ORDEN DE TRABAJO', 'TIPO DE ORDEN', 'ESPECIALIDAD', 'FECHA DE INICIO', 'FECHA DE TÉRMINO', 'COSTO'];
  1158. $startRow = 2;
  1159. $startCol = 2;
  1160. $maxRow = $startRow + count($contracts) + 1;
  1161. $maxCol = $startCol + 7;
  1162. for($row = $startRow; $row <= $maxRow; $row++){
  1163. $startColStr = Coordinate::stringFromColumnIndex($startCol);
  1164. $maxColStr = Coordinate::stringFromColumnIndex($maxCol);
  1165. if($row == 2){
  1166. $worksheet->mergeCells($startColStr . $row . ':' . $maxColStr . $row);
  1167. $worksheet->setCellValue($startColStr . $row, 'FICHA DE SUBCONTRATACIÓN')->getStyle($startColStr . $row)->getFill()
  1168. ->setFillType(Fill::FILL_SOLID)
  1169. ->getStartColor()->setRGB('B7BCC4');
  1170. $worksheet->getStyle($startColStr . $row)->getFont()->setBold(true);
  1171. $worksheet->getStyle($startColStr . $row)->getAlignment()->setHorizontal(Alignment::HORIZONTAL_CENTER);
  1172. }else if($row == 3){
  1173. for($col = $startCol; $col <= $maxCol; $col++){
  1174. $colStr = Coordinate::stringFromColumnIndex($col);
  1175. $colInd = $col - 2;
  1176. $column = $columns[$colInd];
  1177. $worksheet->setCellValue($colStr . $row, $column);
  1178. $worksheet->getColumnDimension($colStr)->setAutoSize(true);
  1179. $worksheet->getStyle($colStr . $row)->getFont()->setBold(true);
  1180. $worksheet->getStyle($colStr . $row)->getAlignment()->setHorizontal(Alignment::HORIZONTAL_CENTER);
  1181. }
  1182. }else if($row > 3){
  1183. $rowInd = $row - 4;
  1184. $contract = (array) $contracts[$rowInd];
  1185. $keys = array_keys($contract);
  1186. for($col = $startCol; $col <= $maxCol; $col++){
  1187. $colInd = $col - 2;
  1188. $key = $keys[$colInd];
  1189. $value = $contract[$key];
  1190. $colStr = Coordinate::stringFromColumnIndex($col);
  1191. if($key == 'IDORDEN'){
  1192. $value = "Orden #$value";
  1193. }else if($key == 'FECHAINICIO' || $key == 'FECHAFIN'){
  1194. $value = $this->functionsController->formatDateTime($value);
  1195. }else if($key == 'COSTO'){
  1196. $worksheet->getStyle($colStr . $row)->getNumberFormat()->setFormatCode(NumberFormat::FORMAT_CURRENCY_USD);
  1197. }
  1198. $worksheet->setCellValue($colStr . $row, $value);
  1199. }
  1200. }
  1201. }
  1202. if(count($contracts) == 0){
  1203. $startColStr = Coordinate::stringFromColumnIndex($startCol);
  1204. $maxColStr = Coordinate::stringFromColumnIndex($maxCol);
  1205. $maxRow++;
  1206. $worksheet->mergeCells($startColStr . $maxRow . ':' . $maxColStr . $maxRow);
  1207. $worksheet->setCellValue($startColStr . $maxRow, 'Aún no se han asociado contratos al subcontratista.');
  1208. $worksheet->getStyle($startColStr . $maxRow)->getFont()->setBold(true);
  1209. $worksheet->getStyle($startColStr . $maxRow)->getAlignment()->setHorizontal(Alignment::HORIZONTAL_CENTER);
  1210. }
  1211. $worksheet->getStyle($startColStr . $startRow . ':' . $maxColStr . $maxRow)
  1212. ->getBorders()
  1213. ->getAllBorders()
  1214. ->setBorderStyle(Border::BORDER_MEDIUM);
  1215. return $spreadsheet;
  1216. }
  1217. }