1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-10 13:12:50 +01:00
invoiceninja/app/Models/ClientContact.php

361 lines
10 KiB
PHP
Raw Normal View History

<?php
2019-05-11 05:32:07 +02:00
/**
* Invoice Ninja (https://invoiceninja.com).
2019-05-11 05:32:07 +02:00
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
2024-04-12 06:15:41 +02:00
* @copyright Copyright (c) 2024. Invoice Ninja LLC (https://invoiceninja.com)
2019-05-11 05:32:07 +02:00
*
2021-06-16 08:58:16 +02:00
* @license https://www.elastic.co/licensing/elastic-license
2019-05-11 05:32:07 +02:00
*/
namespace App\Models;
use App\Utils\Ninja;
2024-01-22 00:52:30 +01:00
use Illuminate\Support\Str;
use App\Jobs\Mail\NinjaMailer;
2023-02-16 21:18:59 +01:00
use App\Utils\Traits\AppSetup;
use App\Utils\Traits\MakesHash;
2024-01-22 00:52:30 +01:00
use App\Jobs\Mail\NinjaMailerJob;
use App\Jobs\Mail\NinjaMailerObject;
use Illuminate\Support\Facades\Cache;
2020-10-28 11:10:49 +01:00
use Illuminate\Database\Eloquent\Model;
use Illuminate\Notifications\Notifiable;
use Laracasts\Presenter\PresentableTrait;
2024-01-22 00:52:30 +01:00
use Illuminate\Database\Eloquent\SoftDeletes;
use App\Models\Presenters\ClientContactPresenter;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Contracts\Translation\HasLocalePreference;
use App\Mail\ClientContact\ClientContactResetPasswordObject;
2021-02-13 01:20:15 +01:00
/**
* Class ClientContact
*
* @method scope() static
2023-03-08 08:33:42 +01:00
* @property int $id
* @property int $company_id
* @property int $client_id
* @property int $user_id
* @property string|null $first_name
* @property string|null $last_name
* @property string|null $phone
* @property string|null $custom_value1
* @property string|null $custom_value2
* @property string|null $custom_value3
* @property string|null $custom_value4
* @property string|null $email
* @property string|null $email_verified_at
* @property string|null $confirmation_code
2023-06-17 22:02:52 +02:00
* @property bool $is_primary
* @property bool $confirmed
2023-03-08 08:33:42 +01:00
* @property int|null $last_login
* @property int|null $failed_logins
* @property string|null $oauth_user_id
* @property int|null $oauth_provider_id
* @property string|null $google_2fa_secret
* @property string|null $accepted_terms_version
* @property string|null $avatar
* @property string|null $avatar_type
* @property string|null $avatar_size
* @property string $password
* @property string|null $token
2023-06-17 22:02:52 +02:00
* @property bool $is_locked
* @property bool $send_email
2023-03-08 08:33:42 +01:00
* @property string|null $contact_key
* @property string|null $remember_token
* @property int|null $created_at
* @property int|null $updated_at
* @property int|null $deleted_at
* @property-read \App\Models\Client $client
* @property-read \App\Models\Company $company
* @property-read int|null $credit_invitations_count
* @property-read mixed $contact_id
* @property-read mixed $hashed_id
* @property-read int|null $invoice_invitations_count
* @property-read int|null $notifications_count
* @property-read int|null $quote_invitations_count
* @property-read int|null $recurring_invoice_invitations_count
* @property-read \App\Models\User $user
* @method static \Database\Factories\ClientContactFactory factory($count = null, $state = [])
* @method static \Illuminate\Database\Eloquent\Builder|ClientContact newModelQuery()
* @method static \Illuminate\Database\Eloquent\Builder|ClientContact newQuery()
* @method static \Illuminate\Database\Eloquent\Builder|ClientContact onlyTrashed()
* @method static \Illuminate\Database\Eloquent\Builder|ClientContact query()
* @method static \Illuminate\Database\Eloquent\Builder|ClientContact withTrashed()
* @method static \Illuminate\Database\Eloquent\Builder|ClientContact withoutTrashed()
2023-03-29 05:23:06 +02:00
* @property-read \Illuminate\Notifications\DatabaseNotificationCollection<int, \Illuminate\Notifications\DatabaseNotification> $notifications
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\QuoteInvitation> $quote_invitations
2023-04-13 07:39:12 +02:00
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\RecurringInvoiceInvitation> $recurring_invoice_invitations
2023-04-26 08:55:49 +02:00
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\CreditInvitation> $credit_invitations
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\InvoiceInvitation> $invoice_invitations
2023-03-08 08:33:42 +01:00
* @mixin \Eloquent
2021-02-13 01:20:15 +01:00
*/
class ClientContact extends Authenticatable implements HasLocalePreference
{
use Notifiable;
use MakesHash;
use PresentableTrait;
use SoftDeletes;
2020-10-01 12:49:47 +02:00
use HasFactory;
2023-02-16 21:18:59 +01:00
use AppSetup;
2024-01-14 05:05:00 +01:00
/* Used to authenticate a contact */
protected $guard = 'contact';
protected $touches = ['client'];
/* Allow microtime timestamps */
protected $dateFormat = 'Y-m-d H:i:s.u';
2020-10-28 11:10:49 +01:00
protected $presenter = ClientContactPresenter::class;
2022-06-21 12:03:51 +02:00
2019-08-02 02:31:48 +02:00
protected $appends = [
'hashed_id',
2019-08-02 02:31:48 +02: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
];
protected $hidden = [
'password',
'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',
];
protected $fillable = [
'first_name',
'last_name',
'phone',
'custom_value1',
'custom_value2',
'custom_value3',
'custom_value4',
'email',
'is_primary',
'send_email',
];
2020-03-23 18:10:42 +01: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());
2021-02-13 01:20:15 +01:00
return $query;
}
2021-02-13 01:20:15 +01:00
/*
V1 type of scope
*/
public function scopeScope($query)
{
$query->where($this->getTable().'.company_id', '=', auth()->user()->company()->id);
2021-02-13 01:20:15 +01:00
return $query;
}
2021-02-13 01:20:15 +01:00
public function getEntityType()
{
return self::class;
}
2019-08-14 02:15:21 +02:00
public function getHashedIdAttribute()
{
return $this->encodePrimaryKey($this->id);
}
2020-03-23 18:10:42 +01:00
/**/
public function getRouteKeyName()
{
return 'contact_id';
}
public function getContactIdAttribute()
{
return $this->encodePrimaryKey($this->id);
}
2019-08-08 10:13:32 +02:00
public function setAvatarAttribute($value)
2019-08-08 02:22:54 +02:00
{
if (! filter_var($value, FILTER_VALIDATE_URL) && $value) {
$this->attributes['avatar'] = url('/').$value;
} else {
2019-08-08 10:13:32 +02:00
$this->attributes['avatar'] = $value;
}
2019-08-08 02:22:54 +02:00
}
2023-08-03 06:30:14 +02:00
public function client(): \Illuminate\Database\Eloquent\Relations\BelongsTo
{
return $this->belongsTo(Client::class)->withTrashed();
}
public function primary_contact()
{
2019-07-10 03:42:34 +02:00
return $this->where('is_primary', true);
}
2023-08-03 06:30:14 +02:00
public function company(): \Illuminate\Database\Eloquent\Relations\BelongsTo
2019-07-08 02:08:57 +02:00
{
return $this->belongsTo(Company::class);
}
2023-08-03 06:30:14 +02:00
public function user(): \Illuminate\Database\Eloquent\Relations\BelongsTo
2019-07-08 02:08:57 +02:00
{
return $this->belongsTo(User::class)->withTrashed();
2019-07-08 02:08:57 +02:00
}
2023-08-03 06:30:14 +02:00
public function invoice_invitations(): \Illuminate\Database\Eloquent\Relations\HasMany
{
return $this->hasMany(InvoiceInvitation::class);
}
2023-08-03 06:30:14 +02:00
public function recurring_invoice_invitations(): \Illuminate\Database\Eloquent\Relations\HasMany
{
return $this->hasMany(RecurringInvoiceInvitation::class);
}
2023-08-03 06:30:14 +02:00
public function quote_invitations(): \Illuminate\Database\Eloquent\Relations\HasMany
{
return $this->hasMany(QuoteInvitation::class);
}
2023-08-03 06:30:14 +02:00
public function credit_invitations(): \Illuminate\Database\Eloquent\Relations\HasMany
{
return $this->hasMany(CreditInvitation::class);
}
2024-02-13 05:25:18 +01:00
public function sendPasswordResetNotification($token)
{
2021-07-29 04:19:56 +02:00
$this->token = $token;
$this->save();
2024-01-14 05:05:00 +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);
}
public function preferredLocale()
{
/** @var \Illuminate\Support\Collection<\App\Models\Language> */
$languages = app('languages');
2020-03-23 18:10:42 +01:00
return $languages->first(function ($item) {
return $item->id == $this->client->getSetting('language_id');
})->locale;
}
public function routeNotificationForMail($notification)
{
return $this->email;
}
/**
* Retrieve the model for a bound value.
*
2020-10-28 11:10:49 +01:00
* @param mixed $value
* @param null $field
* @return Model|null
*/
2020-11-25 15:19:52 +01:00
public function resolveRouteBinding($value, $field = null)
{
return $this
->withTrashed()
->where('id', $this->decodePrimaryKey($value))->firstOrFail();
}
2020-03-23 18:10:42 +01:00
/**
* @return mixed|string
*/
public function avatar()
{
if ($this->avatar) {
2020-03-23 18:10:42 +01:00
return $this->avatar;
}
2020-03-23 18:10:42 +01:00
return asset('images/svg/user.svg');
}
/**
* Provides a convenience login click for contacts to bypass the
* contact authentication layer
2020-10-28 11:10:49 +01:00
*
* @return string URL
*/
public function getLoginLink()
{
if (Ninja::isHosted()) {
$domain = $this->company->domain();
} else {
$domain = config('ninja.app_url');
}
switch ($this->company->portal_mode) {
case 'subdomain':
return $domain.'/client/key_login/'.$this->contact_key;
case 'iframe':
return $domain.'/client/key_login/'.$this->contact_key;
case 'domain':
return $domain.'/client/key_login/'.$this->contact_key;
default:
return '';
}
}
2024-01-22 00:52:30 +01:00
public function getAdminLink($use_react_link = false): string
{
return $use_react_link ? $this->getReactLink() : config('ninja.app_url');
}
private function getReactLink(): string
{
return config('ninja.react_url')."/#/clients/{$this->client->hashed_id}";
}
public function showRff(): bool
{
2024-07-27 07:18:21 +02:00
// if (\strlen($this->first_name ?? '') === 0 || \strlen($this->last_name ?? '') === 0 || \strlen($this->email ?? '') === 0) {
// return true;
// }
2024-01-22 00:52:30 +01:00
return false;
}
}