SubcontractController.php 66 KB

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