| Server IP : 13.235.167.51 / Your IP : 216.73.217.116 Web Server : Apache System : Linux machinox-server 6.17.0-1013-aws #13~24.04.1-Ubuntu SMP Fri Apr 24 21:36:58 UTC 2026 aarch64 User : root ( 0) PHP Version : 8.2.29 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : OFF | Sudo : ON | Pkexec : OFF Directory : /var/www/app.machinox.in/app/Models/ |
Upload File : |
<?php
namespace App\Models;
// use Illuminate\Contracts\Auth\MustVerifyEmail;
use App\Helpers\UtilityHelper;
use App\Traits\UploadTrait;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Sanctum\HasApiTokens;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasManyThrough;
use Ramsey\Uuid\Uuid;
class User extends Authenticatable
{
/** @use HasFactory<\Database\Factories\UserFactory> */
use HasApiTokens, HasFactory, Notifiable, SoftDeletes, UploadTrait;
/**
* The attributes that are mass assignable.
*
* @var list<string>
*/
protected $fillable = [
'uuid',
'name',
'email',
'mobile_number',
'dial_code',
'country_code',
'is_operator',
'is_block',
'profile_image',
'password',
'social_id',
'social_type',
'address',
'latitude',
'longitude',
'city',
'state',
'pincode',
];
/**
* The attributes that should be hidden for serialization.
*
* @var list<string>
*/
protected $hidden = [
'deleted_at',
'created_at',
'updated_at',
'password',
'remember_token',
];
/**
* Get the attributes that should be cast.
*
* @return array<string, string>
*/
protected function casts(): array
{
return [
'email_verified_at' => 'datetime',
'password' => 'hashed',
];
}
public static function boot()
{
parent::boot();
static::creating(function ($user) {
$user->uuid = Uuid::uuid4();
$user->dial_code = str_replace("+", "", $user->dial_code);
});
static::updating(function ($user) {
$user->dial_code = str_replace("+", "", $user->dial_code);
});
}
public function getProfileImageAttribute($value)
{
$folder = config("constants.PROFILE");
return $this->getURL($folder, $value);
}
public static function getInfoById($id)
{
return self::find($id);
}
public function advertisements()
{
return $this->hasMany(Advertisement::class, 'user_id', 'id');
}
public function job_requests()
{
return $this->hasMany(JobRequest::class, 'user_id', 'id');
}
public function settings()
{
return $this->hasOne(UserSetting::class, 'user_id', 'id');
}
public function otp()
{
return $this->hasMany(UserOtp::class, 'user_id', 'id');
}
public function jobs()
{
return $this->hasMany(PostJobs::class, 'user_id', 'id');
}
public function machine_enquiries()
{
return $this->hasMany(MachineEnquiry::class, 'user_id', 'id');
}
public function contact_queries()
{
return $this->hasMany(ContactQuery::class, 'user_id', 'id');
}
public function sentMessages(): HasMany
{
return $this->hasMany(Message::class, 'sender_id');
}
// Messages received by this user
public function receivedMessages(): HasMany
{
return $this->hasMany(Message::class, 'receiver_id');
}
public static function ValidateEmailAddress($email, $uuid = null)
{
return self::where("email", $email)
->when($uuid, function ($query) use ($uuid) {
$query->where('uuid', '!=', $uuid);
})
->first();
}
public static function validateMobileNumber($phoneNumber, $dialCode, $uuid = null)
{
$dialCode = str_replace("+", "", $dialCode);
return self::where('mobile_number', $phoneNumber)
->where('dial_code', $dialCode)
->when($uuid, function ($query) use ($uuid) {
$query->where('uuid', '!=', $uuid);
})
->first();
}
public function hasActiveSubscription(): bool
{
return $this->userSubscriptions()
->where('status', 'ACTIVE')
->whereDate('end_date', '>=', now())
->exists();
}
public function hasOperatorSubscription(): bool
{
return $this->userSubscriptions()
->whereHas('subscription', function ($q) {
$q->whereIn('type', ['OPERATOR', 'PRIME']);
})
->where('status', 'ACTIVE')
->whereDate('end_date', '>=', now())
->exists();
}
public function hasSellerSubscription(): bool
{
return $this->userSubscriptions()
->whereHas('subscription', function ($q) {
$q->whereIn('type', ['SELLER', 'PRIME']);
})
->where('status', 'ACTIVE')
->whereDate('end_date', '>=', now())
->exists();
}
public function hasRentalSubscription(): bool
{
return $this->userSubscriptions()
->whereHas('subscription', function ($q) {
$q->whereIn('type', ['RENTAL', 'PRIME']);
})
->where('status', 'ACTIVE')
->whereDate('end_date', '>=', now())
->exists();
}
public function hasPrimeSubscription(): bool
{
return $this->userSubscriptions()
->whereHas('subscription', function ($q) {
$q->where('type', 'PRIME');
})
->where('status', 'ACTIVE')
->whereDate('end_date', '>=', now())
->exists();
}
public function hasSeekerSubscription(): bool
{
return $this->seekerSubscription()
->where('status', 'ACTIVE')
->whereDate('end_date', '>=', now())
->exists();
}
public function userSubscriptions()
{
return $this->hasMany(UserSubscription::class);
}
public function seekerSubscription()
{
return $this->hasMany(UserSeekerSubscription::class, 'user_id', 'id');
}
public function transactions()
{
return $this->hasMany(Transaction::class, 'id', 'user_id');
}
public function operatorInfo()
{
return $this->hasOne(Operator::class);
}
public const PerPageRecord = "10";
public static function GetUsers($request)
{
$keyword = getValue($request->input('keyword'));
$offset = getValue($request->input('offset'), 0);
$sort_by = getValue($request->input('sort_by'), 'created_at');
$order_by = getValue($request->input('order_by'), 'DESC');
$query = self::query()
->with('operatorInfo', 'userSubscriptions', 'userSubscriptions.subscription')
->when($keyword, function ($query) use ($keyword) {
$query->Where('name', 'LIKE', '%' . $keyword . '%');
$query->orWhere('email', 'LIKE', '%' . $keyword . '%');
$query->orWhere('mobile_number', 'LIKE', '%' . $keyword . '%');
})
->orderBy($sort_by, $order_by);
$countQuery = (clone $query);
$total = $countQuery->count();
$perPage = self::PerPageRecord;
$currentPage = max(1, (int)$offset + 1); // since your offset is page-indexed (0-based)
$totalPages = $perPage > 0 ? (int) ceil($total / $perPage) : 0;
$query->orderBy($sort_by, $order_by)
->offset($offset * $perPage)
->limit($perPage);
$list = $query->get();
$nextOffset = ($list->count() === $perPage && $currentPage < $totalPages) ? ($offset + 1) : -1;
$data = [
'count' => $total,
'list' => $list,
'current_page' => $currentPage,
'next_offset' => $nextOffset,
'per_page' => $perPage,
'total_pages' => $totalPages,
];
return $data;
}
public static function GetNearbyOperatorsForJobs($jobId)
{
$radius = 50;
$job = PostJobs::find($jobId);
if (!$job || !$job->latitude || !$job->longitude || !$job->machine_name) {
return collect();
}
$jobMachine = strtolower($job->machine_name);
$jobLat = $job->latitude;
$jobLng = $job->longitude;
return User::where('is_operator', true)
->whereNotNull('latitude')
->whereNotNull('longitude')
->when(USER_ID(), function ($q) {
$q->where('id', '!=', USER_ID());
})
->whereHas('operatorInfo', function ($q) use ($jobMachine) {
$q->whereRaw('LOWER(machine_name) LIKE ?', ["%{$jobMachine}%"]);
})
->select('users.*')
->selectRaw("CALCULATE_DISTANCE(?, ?, users.latitude, users.longitude, 'km') as distance", [
$jobLng,
$jobLat,
])
->having('distance', '<=', $radius)
->get();
}
public static function GetNearbyOperatorsForAds($adId)
{
$radius = 50;
$ad = Advertisement::find($adId);
if (!$ad || !$ad->latitude || !$ad->longitude) {
return collect();
}
$adLat = $ad->latitude;
$adLng = $ad->longitude;
$currentUserId = USER_ID();
return User::whereNotNull('latitude')
->whereNotNull('longitude')
->where('is_operator', true)
->when($currentUserId, function ($q) use ($currentUserId) {
$q->where('id', '!=', $currentUserId);
})
->select('users.*')
->selectRaw("
CALCULATE_DISTANCE(?, ?, users.latitude, users.longitude, 'km') as distance
", [
$adLng,
$adLat,
])
->having('distance', '<=', $radius)
->orderBy('distance', 'asc')
->get();
}
}