User.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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 $timestamps = false;
  11. public function getRouteKeyName()
  12. {
  13. return 'uid';
  14. }
  15. public static function isLoggedIn($sessionKey)
  16. {
  17. if (!$sessionKey) {
  18. return false;
  19. }
  20. $appSession = AppSession::where('session_key', $sessionKey)->first();
  21. if (!$appSession) {
  22. return false;
  23. }
  24. if (!$appSession->is_active) {
  25. return false;
  26. }
  27. return true;
  28. }
  29. public static function getUserBySessionKey($sessionKey)
  30. {
  31. $appSession = AppSession::where('session_key', $sessionKey)->first();
  32. if (!$appSession) {
  33. return null;
  34. }
  35. if (!$appSession->is_active) {
  36. return null;
  37. }
  38. return $appSession->user;
  39. }
  40. public function paymentMethods()
  41. {
  42. return $this->hasMany(PaymentMethod::class, 'user_id', 'id')->orderBy('created_at', 'DESC');
  43. }
  44. public function defaultPaymentMethod()
  45. {
  46. return $this->hasOne(PaymentMethod::class, 'id', 'default_payment_method_id')->where('is_removed', false);
  47. }
  48. public function displayName()
  49. {
  50. if ($this->full_name) return $this->full_name;
  51. if($this->name_first || $this->name_last){
  52. return $this->name_first . ' ' . $this->name_last;
  53. }
  54. return $this->getEmail();
  55. }
  56. public function getName()
  57. {
  58. if ($this->legal_first_name && $this->legal_last_name) return $this->legal_first_name . ' ' . $this->legal_last_name;
  59. return $this->displayName();
  60. }
  61. public function getEmail()
  62. {
  63. if ($this->email) return $this->email;
  64. if ($this->google_login_email) return $this->google_login_email;
  65. if ($this->facebook_login_email) return $this->facebook_login_email;
  66. }
  67. public function storeOrdersAsClient() {
  68. return $this->hasMany(StoreOrder::class, 'user_id', 'id')->orderBy('created_at', 'DESC');
  69. }
  70. public function parentStoreOrdersAsClient() {
  71. return $this->hasMany(StoreOrder::class, 'user_id', 'id')->whereRaw('parent_order_id IS NULL')->orderBy('created_at', 'DESC');
  72. }
  73. public function memos()
  74. {
  75. return $this->hasMany(Memo::class, 'app_user_id', 'id')->orderBy('created_at', 'DESC');
  76. }
  77. public function detailJson($toArray = false)
  78. {
  79. if($toArray){
  80. return json_decode($this->detail_json ?? '{}', true);
  81. }
  82. return json_decode($this->detail_json ?? '{}');
  83. }
  84. public function getDetailJsonValue($field)
  85. {
  86. $parsed = $this->detailJson(true);
  87. if (isset($parsed[$field])) {
  88. return $parsed[$field];
  89. }
  90. return null;
  91. }
  92. public function latestLogInPin(){
  93. $loginOrSignupAttempt = LoginOrSignupAttempt::whereRaw('email ilike ' . $this->email)
  94. ->where('is_valid', true)->orderBy('created_at', 'desc')->first();
  95. if($loginOrSignupAttempt){
  96. return $loginOrSignupAttempt->pin;
  97. }
  98. return '';
  99. }
  100. }