Edit File: User.php
<?php namespace App\Models; use App\Traits\DeviceTrait; use App\Traits\ReportTrait; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Foundation\Auth\User as Authenticatable; use Illuminate\Notifications\Notifiable; use Illuminate\Support\Facades\Mail; use Laravel\Passport\HasApiTokens; use App\Traits\Uploadable; use Illuminate\Support\Facades\Hash; use Illuminate\Support\Facades\File; use Illuminate\Database\Eloquent\SoftDeletes; use App\Traits\SmsTrait; class User extends Authenticatable { use HasApiTokens, HasFactory, Notifiable, Uploadable, ReportTrait,SmsTrait, SoftDeletes, DeviceTrait; const ADMIN_ID = 1; const ADMIN_TYPE = 'admin'; const AVATARPATH = 'assets/uploads/users/'; const CARLICENSEPATH = 'assets/uploads/users/car-licenses/'; const CARFRONTPATH = 'assets/uploads/users/car-fronts/'; const DELEGATIONPATH = 'assets/uploads/users/delegations/'; protected $guarded = []; /** * The attributes that should be hidden for arrays. * * @var array */ protected $hidden = [ // 'password', 'remember_token', ]; protected $dates = [ 'deleted_at' ]; /** * The attributes that should be cast to native types. * * @var array */ protected $casts = [ 'email_verified_at' => 'datetime', 'phone_verified_at' => 'datetime', ]; public function getFullPhoneAttribute() { return '0'.$this->phone; } public function scopeSearch( $query, $searchArray = [] ) { $query->where( function ( $query ) use ( $searchArray ) { if ( $searchArray ) { foreach ( $searchArray as $key => $value ) { if ( str_contains( $key, '_id' ) ) { if ( null != $value ) { $query->Where( $key, $value ); } } elseif ( 'order' == $key ) { } elseif ( 'created_at_min' == $key ) { if ( null != $value ) { $query->WhereDate( 'created_at', '>=', $value ); } } elseif ( 'created_at_max' == $key ) { if ( null != $value ) { $query->WhereDate( 'created_at', '<=', $value ); } } else { if ( null != $value ) { $query->Where( $key, 'like', '%' . $value . '%' ); } } } } } ); return $query->orderBy( 'created_at', request()->searchArray && request()->searchArray[ 'order' ] ? request()->searchArray[ 'order' ] : 'DESC' ); } public function getFullChangedPhoneAttribute() { return '0'.$this->changed_phone; } public function getFileAttribute( $attribute, $path ) { if ( $this-> { $attribute} ) { return asset( $path . '/' . $this-> { $attribute} ); } return ''; } public function setFileAttribute( $attribute, $value, $path, $deleteExisting = true, $resize = 250, $otherOptions = null ) { if ( $value != 'default.png' ) { if ( $this-> { $attribute} != 'default.png' && $this-> { $attribute} && $deleteExisting ) { File::delete( public_path( $path . '/' . $this-> { $attribute} ) ); } $this->attributes[ $attribute ] = $this->uploadOne( $value, $path, $resize, $otherOptions ); } } public function getAvatarPathAttribute() { return $this->avatar?asset( 'assets/uploads/users/' . $this->avatar ):''; } public function setAvatarAttribute($value) { if($value != 'default.png' && $value != null) { $this->attributes['avatar'] = $this->uploadOne($value, 'users', 'image'); } } // public function getAvatarPathAttribute() // { // return $this->avatar?asset( 'assets/uploads/users/' . $this->avatar ):''; // } // public function setAvatarAttribute( $value ) // { // if ( $value != 'default.png' ) // { // if ( $this->avatar != 'default.png' and $this->avatar ) // { // File::delete( public_path( 'assets/uploads/users/' . $this->avatar ) ); // } // $this->attributes[ 'avatar' ] = $this->uploadOne( $value, 'users', true, 250, null ); // } // } public static function boot() { parent::boot(); /* creating, created, updating, updated, deleting, deleted, forceDeleted, restored */ self::deleted( function ( $model ) { $model->deleteFile( $model->attributes[ 'avatar' ], 'users' ); } ); } public function role() { return $this->belongsTo( Role::class ); } public function payout() { return $this->belongsTo( Payout::class ); } public function complain() { return $this->hasMany( Complain::class ); } // public function notifications() // { // return $this->hasMany( Notification::class ); // } public function devices() { return $this->hasMany( userDevices::class ); } public function reports() { return $this->hasMany( Report::class ); } public function delegateJoinRequests() { return $this->hasMany( DelegateJoinrequest::class ); } public function markAsActive() { $this->update( [ 'status' => 'active' ] ); } public function confirmChangePhone() { $this->update( [ 'phone' => $this->changed_phone,'country_key' => $this->changed_country_key, 'changed_phone'=>NULL,'changed_country_key'=>NULL ] ); } public function userOrders() { return $this->hasMany( Order::class, 'user_id' ); } public function delegateOrders() { return $this->hasMany( Order::class, 'delegate_id' ); } public function tickets() { return $this->hasMany( Ticket::class ); } // public function myReviews() { // return $this->hasMany( Review::class, 'user_id' ); // } // public function ratings() // { // return $this->morphToMany( Review::class, 'reviewable' ,); // } public function ratings() { return $this->morphMany(Review::class, 'rateable'); } public function reviews() { return $this->morphMany( Review::class, 'reviewable' ); } // public function reviewings() // { // return $this->morphMany( Review::class, 'reviewable' ); // } public function addresses() { return $this->hasMany( Address::class ); } public function deliveryOffers() { return $this->hasMany( DeliveryOffer::class ); } public function delegateCompany() { return $this->hasMany( DelegateCompany::class ); } public function city() { return $this->belongsTo( City::class, 'city_id' ); } public function rating() { $reviewsCount = $this->reviews()->count(); if ( $reviewsCount == 0 ) { return number_format( 0, 2 ); } $reviews = $this->reviews; $sum = 0; foreach ( $reviews as $review ) { $sum += $review->rate; } return number_format( round( $sum/$reviewsCount, 2 ), 2 ); } public static function cleanPhone( $phone ) { $filteredPhone = []; if ( substr( $phone, 0, 4 ) == '+966' ) { $string = str_replace( ' ', '', $phone ); $string = str_replace( '-', '', $string ); $filteredPhone = [ 'country_key' => '00966', 'phone' => substr( $string, 4 ), ]; return $filteredPhone; } elseif ( substr( $phone, 0, 5 ) == '00966' ) { $string = str_replace( ' ', '', $phone ); $string = str_replace( '-', '', $string ); $filteredPhone = [ 'country_key' => '00966', 'phone' => substr( $string, 5 ), ]; return $filteredPhone; } elseif ( substr( $phone, 0, 3 ) == '966' ) { $string = str_replace( ' ', '', $phone ); $string = str_replace( '-', '', $string ); $filteredPhone = [ 'country_key' => '00966', 'phone' => substr( $string, 3 ), ]; return $filteredPhone; // Egypt Phone } elseif ( substr( $phone, 0, 2 ) == '+2' ) { $string = str_replace( ' ', '', $phone ); $string = str_replace( '-', '', $string ); $filteredPhone = [ 'country_key' => '002', 'phone' => substr( $string, 3 ) ]; return $filteredPhone; } elseif ( substr( $phone, 0, 3 ) == '002' ) { $string = str_replace( ' ', '', $phone ); $string = str_replace( '-', '', $string ); // return substr( $string, 3 ); $filteredPhone = [ 'country_key' => '002', 'phone' => substr( $string, 4 ) ]; return $filteredPhone; } elseif ( substr( $phone, 0, 1 ) == '2' ) { $string = str_replace( ' ', '', $phone ); $string = str_replace( '-', '', $string ); $filteredPhone = [ 'country_key' => '002', 'phone' => substr( $string, 2 ), ]; return $filteredPhone; } elseif ( substr( $phone, 0, 2 ) == '01' ) { $string = str_replace( ' ', '', $phone ); $string = str_replace( '-', '', $string ); $filteredPhone = [ 'country_key' => '002', 'phone' => substr( $string, 1 ), ]; return $filteredPhone; } elseif ( substr( $phone, 0, 1 ) == '0' ) { $string = str_replace( ' ', '', $phone ); $string = str_replace( '-', '', $string ); $filteredPhone = [ 'country_key' => '002', 'phone' => substr( $string, 1 ), ]; return $filteredPhone; } else { $string = str_replace( ' ', '', $phone ); $string = str_replace( '-', '', $string ); $filteredPhone = [ 'country_key' => '00966', 'phone' => $string, ]; return $filteredPhone; } } // public function sendVerificationCode( $has_changed_phone = 'false' ) // { // // $code = mt_rand( 111111, 999999 ); // $code = '1234'; // $data[ 'code' ] = $code; // // Mail::to( $this->email )->send( new PassCode( $code ) ); // $this->update( [ 'code' => $code ] ); // } public function getFullSmsChangedPhoneAttribute() { return $this->attributes['changed_country_key'] . $this->attributes['changed_phone']; } public function sendVerificationCode( $has_changed_phone = 'false' ) { $package = SMS::where('active', '1')->first(); $code = mt_rand(111111, 999999); $code = '1234'; // $data['code'] = $code; // $msg = trans('auth.yourcode') . ' : ' . $code; // $smsPhone = $this->attributes['country_key'] . $this->attributes['phone']; // $this->sendSms($smsPhone, $msg, $package->key); $this->update(['code' => $code]); } public function resetCode() { $this->update( [ 'code' => null, // 'password' => $password ] ); } public function store() { return $this->hasOne( Store::class ); } public function user() { return $this->belongsTo( User::class ); } // public function setPasswordAttribute( $value ) // { // if ( $value != null ) { // $this->attributes[ 'password' ] = Hash::make( $value ); // } // } public function withdrawReasons() { return $this->hasMany( WithdrawReason::class ); } public function settlements() { return $this->morphMany(Settlement::class, 'transactionable'); } public function settlementOrders(){ return $this->hasMany(SettlementOrder::class); } public function waitingSettlementOrders() { return $this->morphMany(WaitingSettlementOrder::class, 'userable'); } public function FinancialInfo() { $data = []; $ids =$this->waitingSettlementOrders()->where('status', 0)->get()->pluck('order_id')->toArray(); $orders=Order::whereIn('id',$ids)->get(); $data['total_price']=0; $data['price']=0; $data['app_percentage']=0; $data['added_value']=0; foreach($orders as $order){ $data['total_price']+=$order->total_price; $data['app_percentage']+=$order->app_percentage; $data['added_value']+=$order->added_value; $data['price']+=$order->price - $order->app_percentage; } return $data; } }
Back to File Manager