CommonMethods.php_1 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. namespace App\Helpers;
  3. use App\Companies;
  4. use Illuminate\Support\Facades\Crypt;
  5. use Str;
  6. use App\ApiTokens;
  7. Class CommonMethods {
  8. public static function formatDate($date=""){
  9. if(empty($date))
  10. $date = now();
  11. return date('d, M Y',strtotime($date));
  12. }
  13. public static function formatDateTime($date=""){
  14. if(empty($date))
  15. $date = now();
  16. return date('Y-m-d h:i a',strtotime($date));
  17. }
  18. public static function input_string_sanitize($string){
  19. return htmlentities($string, ENT_QUOTES, 'UTF-8', false);
  20. }
  21. public static function format_report_number($id){
  22. return str_pad($id,6,0, STR_PAD_LEFT);
  23. }
  24. public static function generateRandomString($length = 8) {
  25. $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!%$#*&^(){}[]';
  26. $charactersLength = strlen($characters);
  27. $randomString = '';
  28. for ($i = 0; $i < $length; $i++) {
  29. $randomString .= $characters[rand(0, $charactersLength - 1)];
  30. }
  31. return $randomString;
  32. }
  33. public static function generate_user_name($restaurants){
  34. return Str::slug(strtolower(($restaurants).'-'.rand(1000,9999).'-'.substr(str_shuffle($restaurants),0,3)));
  35. }
  36. public static function encrypt($id){
  37. return Crypt::encryptString($id);
  38. }
  39. public static function decrypt($string){
  40. return Crypt::decryptString($string);
  41. }
  42. public static function fileSize($bytes){
  43. $kb = 1024;
  44. $mb = $kb * 1024;
  45. $gb = $mb * 1024;
  46. $tb = $gb * 1024;
  47. if (($bytes >= 0) && ($bytes < $kb)) {
  48. return $bytes . ' B';
  49. } elseif (($bytes >= $kb) && ($bytes < $mb)) {
  50. return ceil($bytes / $kb) . ' KB';
  51. } elseif (($bytes >= $mb) && ($bytes < $gb)) {
  52. return ceil($bytes / $mb) . ' MB';
  53. } elseif (($bytes >= $gb) && ($bytes < $tb)) {
  54. return ceil($bytes / $gb) . ' GB';
  55. } elseif ($bytes >= $tb) {
  56. return ceil($bytes / $tb) . ' TB';
  57. } else {
  58. return $bytes . ' B';
  59. }
  60. }
  61. public static function generateRequestID($user_id){
  62. return date('Ymdhis').'-'.$user_id;
  63. }
  64. public static function authenticate_token($token){
  65. $user = ApiTokens::where('api_token',$token)->first();
  66. if($user->users->is_active=="0" || $user->users->is_active==0){
  67. //dd($user->users->is_active);
  68. $response = [
  69. 'type' => 'error',
  70. 'message' => 'Account is inative, contact to administrator!',
  71. 'code' =>'X003'
  72. ];
  73. echo json_encode($response);
  74. exit;
  75. }
  76. return $user;
  77. }
  78. public static function sendResponse($result, $message)
  79. {
  80. $response = [
  81. 'type' => "success",
  82. 'data' => $result,
  83. 'message' => $message,
  84. ];
  85. return response()->json($response, 200,[],JSON_UNESCAPED_UNICODE);
  86. }
  87. public static function sendError($error, $errorMessages = [], $code = 404)
  88. {
  89. $response = [
  90. 'type' => 'error',
  91. 'message' => $error,
  92. 'code' =>$code
  93. ];
  94. if(!empty($errorMessages)){
  95. $response['data'] = $errorMessages;
  96. }
  97. return response()->json($response, 400);
  98. }
  99. public static function idFormat($restoId,$id){
  100. return str_pad($restoId,4,0, STR_PAD_LEFT).'-'.str_pad($id,6,0, STR_PAD_LEFT);
  101. }
  102. public static function idFormatItem($restoId,$order_id,$id){
  103. return str_pad($restoId,4,0, STR_PAD_LEFT).'-'.str_pad($order_id,6,0, STR_PAD_LEFT).'-'.str_pad($id,6,0, STR_PAD_LEFT);
  104. }
  105. }