92 lines
2.3 KiB
PHP
92 lines
2.3 KiB
PHP
<?php
|
|
|
|
namespace Modules\ContractorManagement\Models;
|
|
|
|
use App\Traits\HasPublicIdentifier;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
|
use Modules\ProjectManagement\Models\Project;
|
|
|
|
class Contractor extends Model
|
|
{
|
|
use HasPublicIdentifier;
|
|
|
|
protected $fillable = [
|
|
'company_name', 'contact_person', 'email', 'phone',
|
|
'specialization', 'address', 'tax_id',
|
|
'payment_terms', 'status',
|
|
'type', 'parent_id', 'user_limit',
|
|
'shares_materials_catalog',
|
|
];
|
|
|
|
protected function casts(): array
|
|
{
|
|
return [
|
|
'user_limit' => 'integer',
|
|
'shares_materials_catalog' => 'boolean',
|
|
];
|
|
}
|
|
|
|
public function equipment(): HasMany
|
|
{
|
|
return $this->hasMany(ContractorEquipment::class);
|
|
}
|
|
|
|
public function users(): HasMany
|
|
{
|
|
return $this->hasMany(\App\Models\User::class);
|
|
}
|
|
|
|
public function parent()
|
|
{
|
|
return $this->belongsTo(self::class, 'parent_id');
|
|
}
|
|
|
|
public function children(): HasMany
|
|
{
|
|
return $this->hasMany(self::class, 'parent_id');
|
|
}
|
|
|
|
public function certifications(): HasMany
|
|
{
|
|
return $this->hasMany(ContractorCertification::class);
|
|
}
|
|
|
|
public function invoices(): HasMany
|
|
{
|
|
return $this->hasMany(ContractorInvoice::class);
|
|
}
|
|
|
|
public function projects(): BelongsToMany
|
|
{
|
|
return $this->belongsToMany(Project::class, 'project_contractor')
|
|
->withPivot('role', 'contract_amount')
|
|
->withTimestamps();
|
|
}
|
|
|
|
/**
|
|
* Projects assigned directly through projects.contractor_id.
|
|
*
|
|
* The pivot-based projects() relation is retained for older assignments.
|
|
*/
|
|
public function directProjects(): HasMany
|
|
{
|
|
return $this->hasMany(Project::class, 'contractor_id');
|
|
}
|
|
|
|
public function getActiveCertificationsCountAttribute(): int
|
|
{
|
|
return $this->certifications()->where('status', 'active')->count();
|
|
}
|
|
|
|
public function getExpiringCertificationsCountAttribute(): int
|
|
{
|
|
return $this->certifications()
|
|
->where('status', 'active')
|
|
->where('expiry_date', '<=', now()->addDays(30))
|
|
->where('expiry_date', '>', now())
|
|
->count();
|
|
}
|
|
}
|