Files
wticreatorstudio/Modules/JobPortal/Traits/JobPortalUserTrait.php
T
2024-02-12 22:54:20 -05:00

138 lines
3.8 KiB
PHP

<?php
namespace Modules\JobPortal\Traits;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Support\Facades\DB;
use Modules\JobPortal\Entities\Company;
use Modules\JobPortal\Entities\Candidate;
use Modules\JobPortal\Entities\Gig;
use Modules\JobPortal\Entities\SEO;
use Modules\JobPortal\Entities\GigOrder;
use Modules\JobPortal\Entities\VendorPayout;
use Modules\JobPortal\Entities\VendorPayoutAccount;
trait JobPortalUserTrait
{
public function hasPermission($permission = ''){
$roles = app('permission_list');
$role = $roles->where('id',$this->role_id)->first();
return $role->permissions->contains('route',$permission);
}
public function company()
{
return $this->hasOne(Company::class, 'owner_id', 'id');
}
public function candidate()
{
return $this->hasOne(Candidate::class, 'id', 'id');
}
public function gigs()
{
return $this->hasMany(Gig::class, 'author_id', 'id');
}
public function gigsPublish()
{
return $this->hasMany(Gig::class, 'author_id', 'id')->where('status','publish');
}
public function checkCompanyInfo() {
if(empty($this->company)) return false;
if(empty($this->company->name)) return false;
if(empty($this->company->phone)) return false;
if(empty($this->company->email)) return false;
return true;
}
public function getAvatarUrl()
{
if (!empty($this->avatar_id)) {
return get_file_url($this->avatar_id, 'thumb');
}
if (!empty($this->avatar)) {
return showImage($row->user->avatar);
}
return showImage('frontend/default/img/avatar.jpg');
}
public function getDisplayName($email = false)
{
$name = $this->name ?? "";
if (!empty($this->first_name) or !empty($this->last_name)) {
$name = implode(' ', [$this->first_name, $this->last_name]);
}
if (!empty($this->business_name)) {
$name = $this->business_name;
}
if (!trim($name) and $this->email) $name = $this->email;
if (empty(trim($name))) {
$name = '';
}
return $name;
}
public function getDisplayNameAttribute()
{
$name = $this->name;
if (!empty($this->first_name) or !empty($this->last_name)) {
$name = implode(' ', [$this->first_name, $this->last_name]);
}
if (!empty($this->business_name)) {
$name = $this->business_name;
}
return $name;
}
public function getSeoMeta($locale = false)
{
if(!$this->seo_type) return;
$seo_key = $this->seo_type;
if(!empty($locale)) $seo_key = $seo_key."_".$locale;
$meta = SEO::where('object_id', $this->id ? $this->id : $this->origin_id )->where('object_model', $seo_key)->first();
if(!empty($meta)){
$meta = $meta->toArray();
}
$meta['slug'] = $this->slug;
$meta['full_url'] = $this->getDetailUrl();
$meta['service_title'] = $this->title ?? $this->name;
$meta['service_desc'] = $this->short_desc;
$meta['service_image'] = $this->image_id;
return $meta;
}
public function payouts(){
return $this->hasMany(VendorPayout::class,'vendor_id');
}
public function current_payout(){
return $this->hasOne(VendorPayout::class,'vendor_id')->orderByDesc('id');
}
public function payout_account(){
return $this->hasOne(VendorPayoutAccount::class,'vendor_id');
}
public function getAvailablePayoutAmountAttribute(){
$query = GigOrder::query();
$total = $query
->whereIn('status',['completed'])
->where('author_id',$this->id)
->whereNull('payout_id')
->sum('price');
return max(0,$total);
}
}