Issues (129)

app/User.php (1 issue)

1
<?php
2
3
namespace App;
4
5
use Illuminate\Auth\Authenticatable;
6
use Illuminate\Auth\Passwords\CanResetPassword;
7
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
8
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
9
use Illuminate\Database\Eloquent\Model;
10
use Illuminate\Database\Eloquent\SoftDeletes;
11
use Spatie\Activitylog\Models\Activity;
12
use Spatie\Activitylog\Traits\LogsActivity;
13
14
//use Laravel\Cashier\Billable;
15
//use LinkThrow\Billing\CustomerBillableTrait;
16
//use App\Model\Common\Website;
17
18
class User extends Model implements AuthenticatableContract, CanResetPasswordContract
19
{
20
    use Authenticatable,
21
    CanResetPassword;
22
    use LogsActivity;
23
    use SoftDeletes;
24
25
    // use Billable;
26
    // use CustomerBillableTrait;
27
28
    /**
29
     * The database table used by the model.
30
     *
31
     * @var string
32
     */
33
    protected $table = 'users';
34
35
    /**
36
     * The attributes that are mass assignable.
37
     *
38
     * @var array
39
     */
40
    protected $fillable = ['first_name', 'last_name', 'user_name', 'company', 'zip',
41
        'state', 'town', 'mobile',
42
        'email', 'password', 'role', 'active', 'profile_pic',
43
        'address', 'country', 'currency', 'currency_symbol', 'timezone_id', 'mobile_code', 'bussiness',
44
        'company_type', 'company_size', 'ip', 'mobile_verified', 'position', 'skype', 'manager', 'currency_symbol', 'account_manager', 'referrer', 'google2fa_secret', 'is_2fa_enabled', 'google2fa_activation_date', 'backup_code', 'code_usage_count', ];
45
46
    protected static $logName = 'User';
47
    protected static $logAttributes = ['first_name', 'last_name', 'user_name', 'company', 'zip',
48
        'state', 'town', 'mobile',
49
        'email', 'password', 'role', 'active', 'profile_pic',
50
        'address', 'country', 'currency', 'timezone_id', 'mobile_code', 'bussiness',
51
        'company_type', 'company_size', 'ip', 'mobile_verified', 'position', 'skype', 'manager', 'account_manager', 'google2fa_activation_date', 'backup_code', 'code_usage_count', ];
52
53
    protected static $logOnlyDirty = true;
54
55
    public function getDescriptionForEvent(string $eventName): string
56
    {
57
        $lastActivity = Activity::all()->last(); //returns the last logged activity
58
        if ($eventName == 'updated') {
59
            $this->enableLogging();
60
61
            return 'User  <strong> '.$this->first_name.' '.$this->last_name.'</strong> was updated';
62
        }
63
64
        if ($eventName == 'deleted') {
65
            return 'User <strong> '.$this->first_name.' '.$this->last_name.' </strong> was deleted';
66
        }
67
68
        return '';
69
    }
70
71
    /**
72
     * The attributes excluded from the model's JSON form.
73
     *
74
     * @var array
75
     */
76
    protected $hidden = ['password', 'remember_token'];
77
78
    public function order()
79
    {
80
        return $this->hasMany('App\Model\Order\Order', 'client');
81
    }
82
83
    public function comments()
84
    {
85
        return $this->hasMany('App\Comment', 'updated_by_user_id');
86
    }
87
88
    public function subscription()
89
    {
90
        // Return an Eloquent relationship.
91
        return $this->hasMany('App\Model\Product\Subscription');
92
    }
93
94
    public function invoiceItem()
95
    {
96
        return $this->hasManyThrough('App\Model\Order\InvoiceItem', 'App\Model\Order\Invoice');
97
    }
98
99
    public function orderRelation()
100
    {
101
        return $this->hasManyThrough('App\Model\Order\OrderInvoiceRelation', 'App\Model\Order\Invoice');
102
    }
103
104
    public function invoice()
105
    {
106
        return $this->hasMany('App\Model\Order\Invoice');
107
    }
108
109
    public function timezone()
110
    {
111
        return $this->belongsTo('App\Model\Common\Timezone');
112
    }
113
114
    // public function getCreatedAtAttribute($value)
115
    // {
116
    //     if (\Auth::user()) {
117
    //         $tz = \Auth::user()->timezone()->first()->name;
118
    //         $date = \Carbon\Carbon::createFromFormat('Y-m-d H:i:s', $value, 'UTC');
119
120
    //         return $date->setTimezone($tz);
121
    //     }
122
123
    //     return $value;
124
    // }
125
126
    public function getProfilePicAttribute($value)
127
    {
128
        $image = \Gravatar::src($this->attributes['email']);
129
        if ($value) {
130
            $file = public_path('common/images/users/'.$value);
131
            if (is_file($file)) {
132
                $mime = \File::mimeType($file);
133
                $extension = \File::extension($file);
134
                if (mime($mime) == 'image' && mime($extension) == 'image') {
135
                    $image = asset('common/images/users/'.$value);
136
                } else {
137
                    unlink($file);
138
                }
139
            }
140
        }
141
142
        return $image;
143
    }
144
145
    public function payment()
146
    {
147
        return $this->hasMany('App\Model\Order\Payment');
148
    }
149
150
    public function setCountryAttribute($value)
151
    {
152
        $value = strtoupper($value);
153
        $this->attributes['country'] = $value;
154
    }
155
156
    public function getBussinessAttribute($value)
157
    {
158
        $short = $this->attributes['bussiness'];
159
        $name = '--';
160
        $bussiness = \App\Model\Common\Bussiness::where('short', $short)->first();
161
        if ($bussiness) {
162
            $name = $bussiness->name;
163
        }
164
165
        return $name;
166
    }
167
168
    public function getCompanyTypeAttribute()
169
    {
170
        $short = $this->attributes['company_type'];
171
        $name = '--';
172
        $company = \DB::table('company_types')->where('short', $short)->first();
173
        if ($company) {
174
            $name = $company->name;
175
        }
176
177
        return $name;
178
    }
179
180
    // public function forceDelete()
181
    // {
182
    //     $this->invoiceItem()->delete();
183
    //     $this->orderRelation()->delete();
184
    //     $this->invoice()->delete();
185
    //     $this->order()->delete();
186
    //     $this->subscription()->delete();
187
    //     $this->comments()->delete();
188
189
    //     return parent::delete();
190
    // }
191
192
    public function manager()
193
    {
194
        return $this->belongsTo('App\User', 'manager');
195
    }
196
197
    public function accountManager()
198
    {
199
        return $this->belongsTo('App\User', 'account_manager');
200
    }
201
202
    public function assignSalesManager()
203
    {
204
        $managers = $this->where('role', 'admin')->where('position', 'manager')->pluck('id', 'first_name')->toArray();
205
        if (count($managers) > 0) {
206
            $randomized[] = array_rand($managers);
0 ignored issues
show
Comprehensibility Best Practice introduced by
$randomized was never initialized. Although not strictly required by PHP, it is generally a good practice to add $randomized = array(); before regardless.
Loading history...
207
            shuffle($randomized);
208
            $manager = $managers[$randomized[0]];
209
        } else {
210
            $manager = '';
211
        }
212
213
        return $manager;
214
    }
215
216
    public function save(array $options = [])
217
    {
218
        $changed = $this->isDirty() ? $this->getDirty() : false;
219
        parent::save($options);
220
        $role = $this->role;
221
        if ($changed && checkArray('manager', $changed) && $role == 'user' && emailSendingStatus()) {
222
            $auth = new Http\Controllers\Auth\AuthController();
223
            $auth->salesManagerMail($this);
224
        }
225
226
        if ($changed && checkArray('account_manager', $changed) && $role == 'user' && emailSendingStatus()) {
227
            $auth = new Http\Controllers\Auth\AuthController();
228
            $auth->accountManagerMail($this);
229
        }
230
    }
231
}
232