User.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. namespace App\Models;
  3. use Illuminate\Database\Eloquent\Factories\HasFactory;
  4. use Illuminate\Database\Eloquent\Model;
  5. use App\Models\BaseModel;
  6. class User extends BaseModel
  7. {
  8. use HasFactory;
  9. protected $table = 'app_user';
  10. public function getRouteKeyName()
  11. {
  12. return 'uid';
  13. }
  14. public static function isLoggedIn($sessionKey)
  15. {
  16. if (!$sessionKey) {
  17. return false;
  18. }
  19. $appSession = AppSession::where('session_key', $sessionKey)->first();
  20. if (!$appSession) {
  21. return false;
  22. }
  23. if (!$appSession->is_active) {
  24. return false;
  25. }
  26. return true;
  27. }
  28. public static function getUserBySessionKey($sessionKey)
  29. {
  30. $appSession = AppSession::where('session_key', $sessionKey)->first();
  31. if (!$appSession) {
  32. return null;
  33. }
  34. if (!$appSession->is_active) {
  35. return null;
  36. }
  37. return $appSession->user;
  38. }
  39. public function paymentMethods()
  40. {
  41. return $this->hasMany(PaymentMethod::class, 'user_id', 'id')->orderBy('created_at', 'ASC');
  42. }
  43. public function defaultPaymentMethod()
  44. {
  45. return $this->hasOne(PaymentMethod::class, 'id', 'default_payment_method_id')->where('is_removed', false);
  46. }
  47. public function displayName()
  48. {
  49. if ($this->full_name) return $this->full_name;
  50. return $this->name_first . ' ' . $this->name_last;
  51. }
  52. public function getName()
  53. {
  54. if ($this->legal_first_name && $this->legal_last_name) return $this->legal_first_name . ' ' . $this->legal_last_name;
  55. return $this->displayName();
  56. }
  57. public function getEmail()
  58. {
  59. if ($this->email) return $this->email;
  60. if ($this->google_login_email) return $this->google_login_email;
  61. if ($this->facebook_login_email) return $this->facebook_login_email;
  62. }
  63. public function storeOrdersAsClient() {
  64. return $this->hasMany(StoreOrder::class, 'user_id', 'id')->orderBy('created_at', 'ASC');
  65. }
  66. public function memos()
  67. {
  68. return $this->hasMany(Memo::class, 'app_user_id', 'id')->orderBy('created_at', 'ASC');
  69. }
  70. public function detailJson($toArray = false)
  71. {
  72. if($toArray){
  73. return json_decode($this->detail_json ?? '{}', true);
  74. }
  75. return json_decode($this->detail_json ?? '{}');
  76. }
  77. public function getDetailJsonValue($field)
  78. {
  79. $parsed = $this->detailJson(true);
  80. if (isset($parsed[$field])) {
  81. return $parsed[$field];
  82. }
  83. return null;
  84. }
  85. }