2018-10-15 07:00:48 +02:00
|
|
|
<?php
|
2019-05-11 05:32:07 +02:00
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
2019-05-11 05:32:07 +02:00
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
2021-01-03 22:54:54 +01:00
|
|
|
* @copyright Copyright (c) 2021. Invoice Ninja LLC (https://invoiceninja.com)
|
2019-05-11 05:32:07 +02:00
|
|
|
*
|
|
|
|
* @license https://opensource.org/licenses/AAL
|
|
|
|
*/
|
2018-10-15 07:00:48 +02:00
|
|
|
|
|
|
|
namespace App\Models;
|
|
|
|
|
2021-02-14 23:54:27 +01:00
|
|
|
use App\Jobs\Mail\NinjaMailer;
|
|
|
|
use App\Jobs\Mail\NinjaMailerJob;
|
|
|
|
use App\Jobs\Mail\NinjaMailerObject;
|
|
|
|
use App\Mail\ClientContact\ClientContactResetPasswordObject;
|
2020-10-28 11:10:49 +01:00
|
|
|
use App\Models\Presenters\ClientContactPresenter;
|
2020-10-01 12:49:47 +02:00
|
|
|
use App\Notifications\ClientContactResetPassword;
|
2018-11-20 05:36:56 +01:00
|
|
|
use App\Utils\Traits\MakesHash;
|
2019-11-04 01:22:59 +01:00
|
|
|
use Illuminate\Contracts\Translation\HasLocalePreference;
|
2020-10-01 12:49:47 +02:00
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
2020-10-28 11:10:49 +01:00
|
|
|
use Illuminate\Database\Eloquent\Model;
|
2018-11-27 07:59:16 +01:00
|
|
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
2018-10-15 07:00:48 +02:00
|
|
|
use Illuminate\Foundation\Auth\User as Authenticatable;
|
2018-11-20 05:36:56 +01:00
|
|
|
use Illuminate\Notifications\Notifiable;
|
2019-11-06 23:57:09 +01:00
|
|
|
use Illuminate\Support\Facades\Cache;
|
2019-11-04 01:22:59 +01:00
|
|
|
use Laracasts\Presenter\PresentableTrait;
|
2018-10-15 07:00:48 +02:00
|
|
|
|
2021-02-13 01:20:15 +01:00
|
|
|
/**
|
|
|
|
* Class ClientContact
|
|
|
|
*
|
|
|
|
* @method scope() static
|
|
|
|
*
|
|
|
|
* @package App\Models
|
|
|
|
*/
|
2019-11-04 01:22:59 +01:00
|
|
|
class ClientContact extends Authenticatable implements HasLocalePreference
|
2018-10-15 07:00:48 +02:00
|
|
|
{
|
|
|
|
use Notifiable;
|
2018-11-20 05:36:56 +01:00
|
|
|
use MakesHash;
|
2018-11-21 09:28:07 +01:00
|
|
|
use PresentableTrait;
|
2018-11-27 07:59:16 +01:00
|
|
|
use SoftDeletes;
|
2020-10-01 12:49:47 +02:00
|
|
|
use HasFactory;
|
2020-10-28 11:10:49 +01:00
|
|
|
|
2019-07-09 12:32:26 +02:00
|
|
|
/* Used to authenticate a contact */
|
2018-10-15 07:00:48 +02:00
|
|
|
protected $guard = 'contact';
|
|
|
|
|
2020-07-16 13:01:39 +02:00
|
|
|
protected $touches = ['client'];
|
|
|
|
|
2019-07-09 12:32:26 +02:00
|
|
|
/* Allow microtime timestamps */
|
2019-07-09 02:01:29 +02:00
|
|
|
protected $dateFormat = 'Y-m-d H:i:s.u';
|
|
|
|
|
2020-10-28 11:10:49 +01:00
|
|
|
protected $presenter = ClientContactPresenter::class;
|
2019-08-14 02:15:21 +02:00
|
|
|
|
|
|
|
protected $dates = [
|
2020-09-06 11:38:10 +02:00
|
|
|
'deleted_at',
|
2019-08-14 02:15:21 +02:00
|
|
|
];
|
2020-03-23 18:10:42 +01:00
|
|
|
|
2019-08-02 02:31:48 +02:00
|
|
|
protected $appends = [
|
2020-09-06 11:38:10 +02:00
|
|
|
'hashed_id',
|
2019-08-02 02:31:48 +02:00
|
|
|
];
|
|
|
|
|
2021-01-21 00:01:13 +01:00
|
|
|
protected $with = [];
|
2019-08-14 02:15:21 +02:00
|
|
|
|
2019-10-02 11:04:27 +02:00
|
|
|
protected $casts = [
|
|
|
|
'updated_at' => 'timestamp',
|
|
|
|
'created_at' => 'timestamp',
|
|
|
|
'deleted_at' => 'timestamp',
|
2020-09-16 12:08:51 +02:00
|
|
|
'last_login' => 'timestamp',
|
2019-10-02 11:04:27 +02:00
|
|
|
];
|
|
|
|
|
2019-07-09 12:32:26 +02:00
|
|
|
protected $hidden = [
|
2019-12-30 22:59:12 +01:00
|
|
|
'password',
|
2019-07-09 12:32:26 +02:00
|
|
|
'remember_token',
|
2019-08-08 13:07:26 +02:00
|
|
|
'user_id',
|
|
|
|
'company_id',
|
|
|
|
'client_id',
|
|
|
|
'google_2fa_secret',
|
|
|
|
'id',
|
|
|
|
'oauth_provider_id',
|
|
|
|
'oauth_user_id',
|
|
|
|
'token',
|
2019-07-09 12:32:26 +02:00
|
|
|
];
|
|
|
|
|
2019-06-26 06:04:10 +02:00
|
|
|
protected $fillable = [
|
|
|
|
'first_name',
|
|
|
|
'last_name',
|
|
|
|
'phone',
|
|
|
|
'custom_value1',
|
|
|
|
'custom_value2',
|
|
|
|
'custom_value3',
|
|
|
|
'custom_value4',
|
|
|
|
'email',
|
2019-12-08 08:33:44 +01:00
|
|
|
'is_primary',
|
2020-06-18 17:09:28 +02:00
|
|
|
'client_id',
|
2018-10-15 07:00:48 +02:00
|
|
|
];
|
2020-03-23 18:10:42 +01:00
|
|
|
|
2021-04-05 10:43:25 +02:00
|
|
|
/**
|
|
|
|
* Whitelisted fields for using from query parameters on subscriptions request.
|
|
|
|
*
|
|
|
|
* @var string[]
|
|
|
|
*/
|
|
|
|
public static $subscription_fillable = [
|
|
|
|
'first_name',
|
|
|
|
'last_name',
|
|
|
|
'phone',
|
|
|
|
'custom_value1',
|
|
|
|
'custom_value2',
|
|
|
|
'custom_value3',
|
|
|
|
'custom_value4',
|
|
|
|
'email',
|
|
|
|
];
|
2021-02-13 01:20:15 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
V2 type of scope
|
|
|
|
*/
|
|
|
|
public function scopeCompany($query)
|
|
|
|
{
|
|
|
|
$query->where('company_id', auth()->user()->companyId());
|
|
|
|
|
|
|
|
return $query;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
V1 type of scope
|
|
|
|
*/
|
|
|
|
public function scopeScope($query)
|
|
|
|
{
|
|
|
|
$query->where($this->getTable().'.company_id', '=', auth()->user()->company()->id);
|
|
|
|
|
|
|
|
return $query;
|
|
|
|
}
|
|
|
|
|
2020-05-06 13:49:42 +02:00
|
|
|
public function getEntityType()
|
|
|
|
{
|
2020-09-06 11:38:10 +02:00
|
|
|
return self::class;
|
2020-05-06 13:49:42 +02:00
|
|
|
}
|
|
|
|
|
2019-08-14 02:15:21 +02:00
|
|
|
public function getHashedIdAttribute()
|
|
|
|
{
|
|
|
|
return $this->encodePrimaryKey($this->id);
|
|
|
|
}
|
2020-03-23 18:10:42 +01:00
|
|
|
|
2019-07-09 12:32:26 +02:00
|
|
|
/**/
|
2018-11-20 05:36:56 +01:00
|
|
|
public function getRouteKeyName()
|
|
|
|
{
|
|
|
|
return 'contact_id';
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getContactIdAttribute()
|
|
|
|
{
|
|
|
|
return $this->encodePrimaryKey($this->id);
|
|
|
|
}
|
2018-10-29 04:16:17 +01:00
|
|
|
|
2019-08-08 10:13:32 +02:00
|
|
|
public function setAvatarAttribute($value)
|
2019-08-08 02:22:54 +02:00
|
|
|
{
|
2020-09-06 11:38:10 +02:00
|
|
|
if (! filter_var($value, FILTER_VALIDATE_URL) && $value) {
|
|
|
|
$this->attributes['avatar'] = url('/').$value;
|
2019-12-30 22:59:12 +01:00
|
|
|
} else {
|
2019-08-08 10:13:32 +02:00
|
|
|
$this->attributes['avatar'] = $value;
|
2019-12-30 22:59:12 +01:00
|
|
|
}
|
2019-08-08 02:22:54 +02:00
|
|
|
}
|
|
|
|
|
2018-10-29 04:16:17 +01:00
|
|
|
public function client()
|
|
|
|
{
|
2019-11-25 10:38:55 +01:00
|
|
|
return $this->belongsTo(Client::class)->withTrashed();
|
2018-10-29 04:16:17 +01:00
|
|
|
}
|
|
|
|
|
2018-11-02 11:54:46 +01:00
|
|
|
public function primary_contact()
|
|
|
|
{
|
2019-07-10 03:42:34 +02:00
|
|
|
return $this->where('is_primary', true);
|
2018-11-02 11:54:46 +01:00
|
|
|
}
|
|
|
|
|
2019-07-08 02:08:57 +02:00
|
|
|
public function company()
|
|
|
|
{
|
|
|
|
return $this->belongsTo(Company::class);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function user()
|
|
|
|
{
|
2019-11-25 10:38:55 +01:00
|
|
|
return $this->belongsTo(User::class)->withTrashed();
|
2019-07-08 02:08:57 +02:00
|
|
|
}
|
|
|
|
|
2020-08-07 00:13:15 +02:00
|
|
|
public function invoice_invitations()
|
|
|
|
{
|
|
|
|
return $this->hasMany(InvoiceInvitation::class);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function quote_invitations()
|
|
|
|
{
|
|
|
|
return $this->hasMany(QuoteInvitation::class);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function credit_invitations()
|
|
|
|
{
|
|
|
|
return $this->hasMany(CreditInvitation::class);
|
|
|
|
}
|
|
|
|
|
2019-07-18 01:45:18 +02:00
|
|
|
public function sendPasswordResetNotification($token)
|
|
|
|
{
|
2021-02-14 23:54:27 +01:00
|
|
|
$nmo = new NinjaMailerObject;
|
|
|
|
$nmo->mailable = new NinjaMailer((new ClientContactResetPasswordObject($token, $this))->build());
|
|
|
|
$nmo->to_user = $this;
|
|
|
|
$nmo->company = $this->company;
|
|
|
|
$nmo->settings = $this->company->settings;
|
|
|
|
|
|
|
|
NinjaMailerJob::dispatch($nmo);
|
|
|
|
|
|
|
|
//$this->notify(new ClientContactResetPassword($token));
|
2019-07-18 01:45:18 +02:00
|
|
|
}
|
2019-11-04 01:22:59 +01:00
|
|
|
|
|
|
|
public function preferredLocale()
|
|
|
|
{
|
2019-11-05 23:52:57 +01:00
|
|
|
$languages = Cache::get('languages');
|
2020-03-23 18:10:42 +01:00
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
return $languages->filter(function ($item) {
|
2019-11-05 23:52:57 +01:00
|
|
|
return $item->id == $this->client->getSetting('language_id');
|
|
|
|
})->first()->locale;
|
|
|
|
}
|
|
|
|
|
2020-03-29 14:22:14 +02:00
|
|
|
public function routeNotificationForMail($notification)
|
|
|
|
{
|
|
|
|
return $this->email;
|
|
|
|
}
|
2019-11-05 23:52:57 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieve the model for a bound value.
|
|
|
|
*
|
2020-10-28 11:10:49 +01:00
|
|
|
* @param mixed $value
|
|
|
|
* @param null $field
|
|
|
|
* @return Model|null
|
2019-11-05 23:52:57 +01:00
|
|
|
*/
|
2020-11-25 15:19:52 +01:00
|
|
|
public function resolveRouteBinding($value, $field = null)
|
2019-11-05 23:52:57 +01:00
|
|
|
{
|
|
|
|
return $this
|
|
|
|
->withTrashed()
|
|
|
|
->where('id', $this->decodePrimaryKey($value))->firstOrFail();
|
2019-11-04 01:22:59 +01:00
|
|
|
}
|
2020-03-23 18:10:42 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return mixed|string
|
|
|
|
*/
|
|
|
|
public function avatar()
|
|
|
|
{
|
2020-03-26 04:23:57 +01:00
|
|
|
if ($this->avatar) {
|
2020-03-23 18:10:42 +01:00
|
|
|
return $this->avatar;
|
2020-03-26 04:23:57 +01:00
|
|
|
}
|
2020-03-23 18:10:42 +01:00
|
|
|
|
|
|
|
return asset('images/svg/user.svg');
|
|
|
|
}
|
2020-09-30 00:42:55 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Provides a convenience login click for contacts to bypass the
|
|
|
|
* contact authentication layer
|
2020-10-28 11:10:49 +01:00
|
|
|
*
|
2020-09-30 00:42:55 +02:00
|
|
|
* @return string URL
|
|
|
|
*/
|
|
|
|
public function getLoginLink()
|
|
|
|
{
|
|
|
|
$domain = isset($this->company->portal_domain) ?: $this->company->domain();
|
|
|
|
|
2020-12-08 10:18:17 +01:00
|
|
|
return $domain . '/client/key_login/' . $this->contact_key;
|
2020-09-30 00:42:55 +02:00
|
|
|
}
|
2018-10-15 07:00:48 +02:00
|
|
|
}
|