2018-11-02 11:54:46 +01: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
|
|
|
*
|
2021-06-16 08:58:16 +02:00
|
|
|
* @license https://www.elastic.co/licensing/elastic-license
|
2019-05-11 05:32:07 +02:00
|
|
|
*/
|
2018-11-02 11:54:46 +01:00
|
|
|
|
|
|
|
namespace App\Models;
|
|
|
|
|
2019-01-19 11:35:21 +01:00
|
|
|
use Illuminate\Database\Eloquent\Relations\Pivot;
|
2020-03-01 11:18:13 +01:00
|
|
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
2019-01-19 11:35:21 +01:00
|
|
|
|
|
|
|
class CompanyUser extends Pivot
|
2018-11-02 11:54:46 +01:00
|
|
|
{
|
2020-03-01 11:18:13 +01:00
|
|
|
use SoftDeletes;
|
2020-02-26 04:26:07 +01:00
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
// protected $guarded = ['id'];
|
2019-11-22 22:10:53 +01:00
|
|
|
|
|
|
|
protected $dateFormat = 'Y-m-d H:i:s.u';
|
|
|
|
|
2019-01-15 23:00:25 +01:00
|
|
|
/**
|
|
|
|
* The attributes that should be cast to native types.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $casts = [
|
2021-01-29 13:29:42 +01:00
|
|
|
'permissions_updated_at' => 'timestamp',
|
2019-09-26 00:27:26 +02:00
|
|
|
'updated_at' => 'timestamp',
|
|
|
|
'created_at' => 'timestamp',
|
|
|
|
'deleted_at' => 'timestamp',
|
2019-11-29 12:15:50 +01:00
|
|
|
'settings' => 'object',
|
2020-03-08 10:06:21 +01:00
|
|
|
'notifications' => 'object',
|
2020-03-10 13:54:20 +01:00
|
|
|
'permissions' => 'string',
|
2019-01-15 23:00:25 +01:00
|
|
|
];
|
|
|
|
|
2019-11-21 09:38:57 +01:00
|
|
|
protected $fillable = [
|
|
|
|
'account_id',
|
|
|
|
'permissions',
|
2020-03-01 11:18:13 +01:00
|
|
|
'notifications',
|
2019-11-21 09:38:57 +01:00
|
|
|
'settings',
|
|
|
|
'is_admin',
|
|
|
|
'is_owner',
|
2020-03-09 10:38:15 +01:00
|
|
|
'is_locked',
|
|
|
|
'slack_webhook_url',
|
2020-09-06 11:38:10 +02:00
|
|
|
'shop_restricted',
|
2019-11-21 09:38:57 +01:00
|
|
|
];
|
|
|
|
|
2021-03-03 08:22:14 +01:00
|
|
|
protected $touches = ['user'];
|
2020-07-16 13:01:39 +02:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-11-26 22:05:30 +01:00
|
|
|
public function tax_rates()
|
|
|
|
{
|
2020-11-27 12:08:42 +01:00
|
|
|
return $this->hasMany(TaxRate::class, 'company_id', 'company_id');
|
2020-11-26 22:05:30 +01:00
|
|
|
}
|
|
|
|
|
2018-11-02 11:54:46 +01:00
|
|
|
public function account()
|
|
|
|
{
|
2019-09-11 02:37:53 +02:00
|
|
|
return $this->belongsTo(Account::class);
|
2018-11-02 11:54:46 +01:00
|
|
|
}
|
|
|
|
|
2019-06-25 05:55:02 +02:00
|
|
|
public function user_pivot()
|
2018-11-02 11:54:46 +01:00
|
|
|
{
|
2020-04-04 12:32:42 +02:00
|
|
|
return $this->hasOne(User::class)->withPivot('permissions', 'settings', 'is_admin', 'is_owner', 'is_locked', 'slack_webhook_url', 'migrating');
|
2019-01-07 12:30:28 +01:00
|
|
|
}
|
|
|
|
|
2019-06-25 05:55:02 +02:00
|
|
|
public function company_pivot()
|
2019-01-07 12:30:28 +01:00
|
|
|
{
|
2020-04-04 12:32:42 +02:00
|
|
|
return $this->hasOne(Company::class)->withPivot('permissions', 'settings', 'is_admin', 'is_owner', 'is_locked', 'slack_webhook_url', 'migrating');
|
2018-11-02 11:54:46 +01:00
|
|
|
}
|
2019-06-25 05:55:02 +02:00
|
|
|
|
|
|
|
public function user()
|
|
|
|
{
|
2019-11-24 09:19:53 +01:00
|
|
|
return $this->belongsTo(User::class);
|
2019-06-25 05:55:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function company()
|
|
|
|
{
|
2019-11-24 09:19:53 +01:00
|
|
|
return $this->belongsTo(Company::class);
|
2019-06-25 05:55:02 +02:00
|
|
|
}
|
|
|
|
|
2019-10-30 08:48:44 +01:00
|
|
|
public function users()
|
|
|
|
{
|
|
|
|
return $this->hasMany(User::class);
|
|
|
|
}
|
|
|
|
|
2019-10-09 07:47:12 +02:00
|
|
|
/*todo monitor this function - may fail under certain conditions*/
|
2019-06-25 05:55:02 +02:00
|
|
|
public function token()
|
|
|
|
{
|
2020-02-26 04:26:07 +01:00
|
|
|
return $this->hasMany(CompanyToken::class, 'user_id', 'user_id');
|
|
|
|
|
|
|
|
//return $this->hasMany(CompanyToken::class);
|
|
|
|
//return $this->hasOne(CompanyToken::class, 'user_id', 'user_id','company_id', 'company_id');
|
|
|
|
|
|
|
|
|
|
|
|
//return $this->belongsTo(CompanyToken::class, 'user_id', 'user_id');
|
|
|
|
|
|
|
|
// return $this->hasOneThrough(
|
|
|
|
// CompanyToken::class,
|
|
|
|
// CompanyUser::class,
|
|
|
|
// 'user_id', // Foreign key on CompanyUser table...
|
|
|
|
// 'company_id', // Foreign key on CompanyToken table...
|
|
|
|
// 'user_id', // Local key on CompanyToken table...
|
|
|
|
// 'company_id' // Local key on CompanyUser table...
|
|
|
|
// );
|
2019-06-25 05:55:02 +02:00
|
|
|
}
|
2020-02-26 04:26:07 +01:00
|
|
|
|
|
|
|
public function tokens()
|
|
|
|
{
|
|
|
|
return $this->hasMany(CompanyToken::class, 'user_id', 'user_id');
|
|
|
|
}
|
|
|
|
|
2020-03-11 12:05:05 +01:00
|
|
|
public function scopeAuthCompany($query)
|
|
|
|
{
|
|
|
|
$query->where('company_id', auth()->user()->companyId());
|
2020-09-06 11:38:10 +02:00
|
|
|
|
2020-03-11 12:05:05 +01:00
|
|
|
return $query;
|
|
|
|
}
|
2018-11-02 11:54:46 +01:00
|
|
|
}
|