203 lines
4.3 KiB
PHP
203 lines
4.3 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Models\Concerns\HasEcommerceChannels;
|
|
use App\Models\Concerns\HasSchemalessAttributes;
|
|
use App\Models\Concerns\InteractsWithNova;
|
|
use App\Models\Concerns\InteractsWithRoles;
|
|
use App\Models\Ecommerce\Product\Cart\CartItem;
|
|
use App\Models\Ecommerce\Product\Favorite\Favorite;
|
|
use App\Models\Ecommerce\Product\Order\Order;
|
|
use App\Models\Ecommerce\Product\Review\Review;
|
|
use App\Models\Post\User\UserDoc;
|
|
use App\Models\System\Settings\Location\UserAddress;
|
|
use App\Repositories\System\Cache\CacheRepository;
|
|
use Illuminate\Database\Eloquent\Casts\Attribute;
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
|
use Illuminate\Database\Eloquent\Relations\HasOne;
|
|
use Illuminate\Notifications\Notifiable;
|
|
use Laravel\Sanctum\HasApiTokens;
|
|
use Spatie\DeletedModels\Models\Concerns\KeepsDeletedModels;
|
|
use Spatie\Permission\Traits\HasRoles;
|
|
|
|
class User
|
|
{
|
|
use HasApiTokens;
|
|
use HasEcommerceChannels;
|
|
use HasFactory;
|
|
use HasRoles;
|
|
use HasSchemalessAttributes;
|
|
use InteractsWithNova;
|
|
use InteractsWithRoles;
|
|
use KeepsDeletedModels;
|
|
use Notifiable;
|
|
|
|
/**
|
|
* The attributes that are mass assignable.
|
|
*
|
|
* @var array<string>
|
|
*/
|
|
protected $fillable = [
|
|
'first_name',
|
|
'last_name',
|
|
'email',
|
|
'phone_number',
|
|
'verified',
|
|
'password',
|
|
'options',
|
|
];
|
|
|
|
/**
|
|
* The attributes that should be hidden for serialization.
|
|
*
|
|
* @var array<string>
|
|
*/
|
|
protected $hidden = [
|
|
'password',
|
|
'remember_token',
|
|
];
|
|
|
|
/**
|
|
* The attributes that should be cast.
|
|
*
|
|
* @var array<string, string>
|
|
*/
|
|
protected $casts = [
|
|
'email_verified_at' => 'datetime',
|
|
'password' => 'hashed',
|
|
];
|
|
|
|
/**
|
|
* Get fullname of user
|
|
*
|
|
* @return string
|
|
*/
|
|
public function fullname(): Attribute
|
|
{
|
|
return Attribute::make(
|
|
get: fn () => sprintf('%s %s', $this->first_name, $this->last_name)
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Get users locale
|
|
*/
|
|
public function locale(): Attribute
|
|
{
|
|
return Attribute::make(
|
|
get: fn () => $this->options->get('locale'),
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Get all role names
|
|
*
|
|
* @return string
|
|
*/
|
|
public function roleNames(): Attribute
|
|
{
|
|
return Attribute::make(
|
|
get: fn () => $this->getRoleNames()->implode(', ')
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Addresses
|
|
*/
|
|
public function addresses(): HasMany
|
|
{
|
|
return $this->hasMany(UserAddress::class);
|
|
}
|
|
|
|
/**
|
|
* User's carts
|
|
*/
|
|
public function carts(): HasMany
|
|
{
|
|
return $this->hasMany(CartItem::class);
|
|
}
|
|
|
|
/**
|
|
* User's favorite products
|
|
*/
|
|
public function favorites(): HasMany
|
|
{
|
|
return $this->hasMany(Favorite::class);
|
|
}
|
|
|
|
/**
|
|
* User's favorite products
|
|
*/
|
|
public function reviews(): HasMany
|
|
{
|
|
return $this->hasMany(Review::class);
|
|
}
|
|
|
|
/**
|
|
* Orders
|
|
*/
|
|
public function orders(): HasMany
|
|
{
|
|
return $this->hasMany(Order::class);
|
|
}
|
|
|
|
/**
|
|
* Documents
|
|
*/
|
|
public function documents(): HasOne
|
|
{
|
|
return $this->hasOne(UserDoc::class);
|
|
}
|
|
|
|
/**
|
|
* Get Company name
|
|
*/
|
|
public function companyName(): ?string
|
|
{
|
|
$this->loadMissing('documents');
|
|
|
|
return $this->documents?->formattedCorparationName;
|
|
}
|
|
|
|
/**
|
|
* User owns product
|
|
*/
|
|
public function ownsProduct($product): bool
|
|
{
|
|
return true;
|
|
// return CacheRepository::make();
|
|
}
|
|
|
|
/**
|
|
* Owns inventory
|
|
*/
|
|
public function ownsInventory($inventory): bool
|
|
{
|
|
return $this->channel()->id === $inventory->channel_id;
|
|
}
|
|
|
|
/**
|
|
* Check if user does not own any inventories
|
|
*/
|
|
public function doesntOwnInventory(): bool
|
|
{
|
|
return $this->inventoryCount() === 0;
|
|
}
|
|
|
|
/**
|
|
* Inventory count
|
|
*/
|
|
public function inventoryCount(): int
|
|
{
|
|
$inventoryCount = cache()->store('array')->get('user_inventory_count');
|
|
|
|
if (! $inventoryCount) {
|
|
cache()->store('array')->put('user_inventory_count', $this->channel()->inventories()->count());
|
|
}
|
|
|
|
return cache()->store('array')->get('user_inventory_count');
|
|
}
|
|
}
|