FunctionsController.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. <?php
  2. namespace App\Http\Controllers;
  3. use Illuminate\Http\Request;
  4. use Illuminate\Support\Facades\Validator;
  5. use Illuminate\Support\Facades\DB;
  6. use Illuminate\Support\Facades\Hash;
  7. use Illuminate\Support\Carbon;
  8. class FunctionsController extends Controller{
  9. public function __construct(){}
  10. public static function uuidv5($namespace, $name) {
  11. if(!self::is_valid($namespace)) return false;
  12. // Get hexadecimal components of namespace
  13. $nhex = str_replace(array('-','{','}'), '', $namespace);
  14. // Binary Value
  15. $nstr = '';
  16. // Convert Namespace UUID to bits
  17. for($i = 0; $i < strlen($nhex); $i+=2) {
  18. $nstr .= chr(hexdec($nhex[$i].$nhex[$i+1]));
  19. }
  20. // Calculate hash value
  21. $hash = sha1($nstr . $name);
  22. return sprintf('%08s-%04s-%04x-%04x-%12s',
  23. // 32 bits for "time_low"
  24. substr($hash, 0, 8),
  25. // 16 bits for "time_mid"
  26. substr($hash, 8, 4),
  27. // 16 bits for "time_hi_and_version",
  28. // four most significant bits holds version number 5
  29. (hexdec(substr($hash, 12, 4)) & 0x0fff) | 0x5000,
  30. // 16 bits, 8 bits for "clk_seq_hi_res",
  31. // 8 bits for "clk_seq_low",
  32. // two most significant bits holds zero and one for variant DCE1.1
  33. (hexdec(substr($hash, 16, 4)) & 0x3fff) | 0x8000,
  34. // 48 bits for "node"
  35. substr($hash, 20, 12)
  36. );
  37. }
  38. public static function is_valid($uuid) {
  39. return preg_match('/^\{?[0-9a-f]{8}\-?[0-9a-f]{4}\-?[0-9a-f]{4}\-?'.
  40. '[0-9a-f]{4}\-?[0-9a-f]{12}\}?$/i', $uuid) === 1;
  41. }
  42. public function generateID($seed, $timestamp){
  43. $string = str_replace(" ", "", $seed) . $timestamp;
  44. $array = [];
  45. for($i = 0; $i < strlen($string); $i++){
  46. $array[] = $string[$i];
  47. }
  48. shuffle($array);
  49. $chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
  50. $ID = '';
  51. foreach($array as $char){
  52. $ascii = ord($char);
  53. $s = $ascii / 100;
  54. if($s >= 1) continue;
  55. $s = $s * strlen($chars);
  56. $s = intval($s);
  57. $l = $chars[$s];
  58. if(strlen($ID) < 20) $ID .= $l;
  59. }
  60. if(strlen($ID) < 20){
  61. for($i = strlen($ID); $i < 20; $i++){
  62. $s = rand(0, strlen($chars) - 1);
  63. $l = $chars[$s];
  64. $ID .= $l;
  65. }
  66. }
  67. return $ID;
  68. }
  69. public function getType($type){
  70. switch($type){
  71. case "insert":
  72. return "CREATE";
  73. break;
  74. case "update":
  75. return strtoupper($type);
  76. break;
  77. case "select":
  78. return "READ";
  79. break;
  80. case "delete":
  81. return strtoupper($type);
  82. break;
  83. default:
  84. return "UNKNOWN";
  85. break;
  86. }
  87. }
  88. public function registerLog($querys, $user, $date, $idac, $line){
  89. foreach($querys as $query){
  90. $script = $query['query'];
  91. $params = json_encode($query['bindings']);
  92. DB::table('S002V01TLOMY')->insert([
  93. 'LOMY_NULI' => $line,
  94. 'LOMY_IDAC' => $idac,
  95. 'LOMY_QUER' => $script,
  96. 'LOMY_PARA' => $params,
  97. 'LOMY_FECH' => $date,
  98. 'LOMY_IDUS' => $user,
  99. ]);
  100. }
  101. }
  102. public function registerActivity($line, $module, $function, $screen, $action, $description, $user, $date, $submodule = null){
  103. $id = DB::table('S002V01TACCI')->insertGetId([
  104. 'ACCI_NULI' => $line,
  105. 'ACCI_IDMO' => $module,
  106. 'ACCI_IDSM' => $submodule,
  107. 'ACCI_IDFU' => $function,
  108. 'ACCI_IDPA' => $screen,
  109. 'ACCI_TIAC' => $action,
  110. 'ACCI_DESC' => $description,
  111. 'ACCI_IDUS' => $user,
  112. 'ACCI_FEAC' => $date
  113. ]);
  114. DB::table('S002V01TUSUA')->where('USUA_IDUS', '=', $user)->update([
  115. 'USUA_ULAC' => $id
  116. ]);
  117. return $id;
  118. }
  119. public function joinName($name, $fApe, $sApe = null){
  120. $fullName = "$name $fApe";
  121. if(!is_null($sApe)){
  122. $fullName = "$fullName $sApe";
  123. }
  124. return $fullName;
  125. }
  126. public function checkFileSize($ext, $size){
  127. $availableFiles = [
  128. 250=> ["zip", "tar", "rar", "7z", "mp3", "mpeg", "wav", "ogg", "opus", "jpg", "png", "gif", "bmp", "tiff", "svg",
  129. "txt", "webm", "mp4", "3gp", "mov", "avi", "wmv", "flv", "dwg", "dxf", "3ds", "ai", "psd", "xml"],
  130. 50 => ["doc", "docx", "vsd", "vsdx", "pdf"],
  131. 75 => ["xls", "xlsx"],
  132. 100=> ["ppt", "pptx"],
  133. ];
  134. $maxFileSizeMB = 0;
  135. foreach($availableFiles as $k=>$v){
  136. if(in_array($ext, $v)) $maxFileSizeMB = $k;
  137. }
  138. if($maxFileSizeMB == 0) return false;
  139. $maxFileSize = $maxFileSizeMB * 1048576;
  140. return $size < $maxFileSize;
  141. }
  142. public function checkInvoice($file) {
  143. try {
  144. $xml = simplexml_load_string($file, 'SimpleXMLElement', LIBXML_NOCDATA);
  145. $ns = $xml->getNamespaces(true);
  146. $xml->registerXPathNamespace('c', $ns['cfdi']);
  147. $xml->registerXPathNamespace('t', $ns['tfd']);
  148. } catch (\Throwable $th) {
  149. return $this->responseController->makeResponse(
  150. true,
  151. "ERR_INVOICE_COMPARE006: Ocurrió un error al obtener la información del documento.",
  152. $th->getMessage(),
  153. 500
  154. );
  155. }
  156. $arrInfoInvoice = ((array) $xml)['@attributes'];
  157. if (
  158. array_key_exists('Version', $arrInfoInvoice) &&
  159. array_key_exists('Serie', $arrInfoInvoice) &&
  160. array_key_exists('Folio', $arrInfoInvoice) &&
  161. array_key_exists('TipoDeComprobante', $arrInfoInvoice) &&
  162. array_key_exists('NoCertificado', $arrInfoInvoice) &&
  163. array_key_exists('Certificado', $arrInfoInvoice) &&
  164. array_key_exists('Sello', $arrInfoInvoice) &&
  165. array_key_exists('FormaPago', $arrInfoInvoice) &&
  166. array_key_exists('MetodoPago', $arrInfoInvoice) &&
  167. array_key_exists('SubTotal', $arrInfoInvoice) &&
  168. array_key_exists('Moneda', $arrInfoInvoice) &&
  169. array_key_exists('TipoCambio', $arrInfoInvoice) &&
  170. array_key_exists('Total', $arrInfoInvoice) &&
  171. array_key_exists('LugarExpedicion', $arrInfoInvoice) &&
  172. array_key_exists('CondicionesDePago', $arrInfoInvoice) &&
  173. array_key_exists('Fecha', $arrInfoInvoice) &&
  174. array_key_exists('Exportacion', $arrInfoInvoice)
  175. ) {
  176. return true;
  177. }
  178. return false;
  179. }
  180. public function now(){
  181. $completeDate = shell_exec('echo %date%');
  182. $completeDate = trim($completeDate);
  183. $serverDateTime = "";
  184. if(strlen($completeDate) == 14){
  185. $serverDateTime = shell_exec('echo %date:~10,4%-%date:~4,2%-%date:~7,2% %time:~0,2%:%time:~3,2%:%time:~6,2%');
  186. $serverDateTime = trim($serverDateTime);
  187. }else{
  188. $serverDateTime = shell_exec('echo %date:~6,4%-%date:~3,2%-%date:~0,2% %time:~0,2%:%time:~3,2%:%time:~6,2%');
  189. $serverDateTime = trim($serverDateTime);
  190. }
  191. return new Carbon($serverDateTime);
  192. }
  193. public function validDate($date){
  194. if(preg_match("/^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$/",$date)){
  195. return true;
  196. }else{
  197. return false;
  198. }
  199. }
  200. public function floatAdd(...$numbers) : float{
  201. $result = "0.0";
  202. foreach($numbers as $n){
  203. $result = bcadd("$result", "$n", 18);
  204. }
  205. return floatval($result);
  206. }
  207. public function floatSub($fn, $sn) : float{
  208. $result = bcsub("$fn", "$sn", 18);
  209. return floatval($result);
  210. }
  211. public function floatDiv($fn, $sn) : float{
  212. $result = bcdiv("$fn", "$sn", 18);
  213. return floatval($result);
  214. }
  215. public function floatMul($n1, $n2) : float {
  216. $result = bcmul("$n1", "$n2", 18);
  217. return floatval($result);
  218. }
  219. public function formatDateTime(string $dateTime) : string {
  220. if(str_contains($dateTime, '0001-01-01')){
  221. return "-";
  222. }
  223. $MONTHS = ["01" => "Enero", "02" => "Febrero", "03" => "Marzo", "04" => "Abril", "05" => "Mayo", "06" => "Junio", "07" => "Julio",
  224. "08" => "Agosto", "09" => "Septiembre", "10" => "Octubre", "11" => "Noviembre", "12" => "Diciembre"];
  225. $dateTimeArr = explode(" ", $dateTime);
  226. $dateArr = explode("-", $dateTimeArr[0]);
  227. $month = $MONTHS[$dateArr[1]];
  228. $day = intval($dateArr[2]);
  229. $dateTimeStr = $day . " de " . $month . " del " . $dateArr[0];
  230. $timeArr = explode(":", $dateTimeArr[1]);
  231. $hour = intval($timeArr[0]);
  232. $period = $hour < 12 ? 'AM' : 'PM';
  233. $hour = $hour > 12 ? $hour - 12 : $hour;
  234. $dateTimeStr .= " a las " . $hour . ":" . $timeArr[1] . " " . $period;
  235. return $dateTimeStr;
  236. }
  237. public function buildHumanCurrentDate(string $dateTime) : string {
  238. $DAYS = ["Mo" => "Lunes", "Tu" => "Martes", "We" => "Miércoles", "Th" => "Jueves",
  239. "Fr" => "Viernes", "Sa" => "Sábado", "Su" => "Domingo"];
  240. $MONTHS = ["01" => "Enero", "02" => "Febrero", "03" => "Marzo", "04" => "Abril", "05" => "Mayo", "06" => "Junio", "07" => "Julio",
  241. "08" => "Agosto", "09" => "Septiembre", "10" => "Octubre", "11" => "Noviembre", "12" => "Diciembre"];
  242. $dateTimeArr = explode(" ", $dateTime);
  243. $dateArr = explode("-", $dateTimeArr[0]);
  244. $dateObj = new Carbon($dateTime);
  245. $dayName = $dateObj->format('l');
  246. $dayInd = substr($dayName, 0, 2);
  247. return $DAYS[$dayInd] . ", " . $dateArr[2] . " de " . $MONTHS[$dateArr[1]] . " del " . $dateArr[0];
  248. }
  249. public function buildProjectDate($dateStr): string {
  250. $MONTHS = ["01" => "enero", "02" => "febrero", "03" => "marzo", "04" => "abril", "05" => "mayo", "06" => "junio", "07" => "julio",
  251. "08" => "agosto", "09" => "septiembre", "10" => "octubre", "11" => "noviembre", "12" => "diciembre"];
  252. $dateObj = new Carbon($dateStr);
  253. $dateFormatted = $dateObj->toDateTimeString();
  254. $dateTimeArr = explode(" ", $dateFormatted);
  255. $dateArr = explode('-', $dateTimeArr[0]);
  256. $monthName = $MONTHS[$dateArr[1]];
  257. $timeArr = explode(':', $dateTimeArr[1]);
  258. $hour = intval($timeArr[0]);
  259. $period = $hour < 12 ? 'a. m.' : 'p. m.';
  260. $hour = $hour > 12 ? $hour - 12 : $hour;
  261. $hourStr = $hour < 10 ? "0$hour" : "$hour";
  262. return "$dateArr[2] de $monthName $dateArr[0] $hourStr:$timeArr[1] $period";
  263. }
  264. }