SubcontractController.php 66 KB

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