99 lines
2.5 KiB
PHP
99 lines
2.5 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Tenancy\HasTenant;
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Str;
|
|
use MatanYadaev\EloquentSpatial\Objects\Point;
|
|
use MatanYadaev\EloquentSpatial\Objects\Polygon;
|
|
use MatanYadaev\EloquentSpatial\Traits\HasSpatial;
|
|
|
|
class Dumpsite extends Model
|
|
{
|
|
use HasFactory, HasSpatial, HasTenant, SoftDeletes;
|
|
|
|
public const STATUS_ACTIVE = 'active';
|
|
|
|
public const STATUS_MAINTENANCE = 'maintenance';
|
|
|
|
public const STATUS_CLOSED = 'closed';
|
|
|
|
protected $fillable = [
|
|
'uuid',
|
|
'tenant_id',
|
|
'name',
|
|
'code',
|
|
'address_line',
|
|
'city_municipality_id',
|
|
'coordinates',
|
|
'boundary_polygon',
|
|
'accepted_waste_types',
|
|
'operating_hours',
|
|
'capacity_tons',
|
|
'contact_person',
|
|
'contact_phone',
|
|
'permit_number',
|
|
'status',
|
|
];
|
|
|
|
protected function casts(): array
|
|
{
|
|
return [
|
|
'coordinates' => Point::class,
|
|
'boundary_polygon' => Polygon::class,
|
|
'accepted_waste_types' => 'array',
|
|
'operating_hours' => 'array',
|
|
'capacity_tons' => 'integer',
|
|
];
|
|
}
|
|
|
|
public function getRouteKeyName(): string
|
|
{
|
|
return 'uuid';
|
|
}
|
|
|
|
protected static function booted(): void
|
|
{
|
|
static::creating(function (self $d): void {
|
|
if (empty($d->uuid)) {
|
|
$d->uuid = (string) Str::uuid();
|
|
}
|
|
});
|
|
}
|
|
|
|
public function cityMunicipality(): BelongsTo
|
|
{
|
|
return $this->belongsTo(CityMunicipality::class);
|
|
}
|
|
|
|
public function releases(): HasMany
|
|
{
|
|
return $this->hasMany(DumpsiteRelease::class);
|
|
}
|
|
|
|
/**
|
|
* Geofence check: is the given (lat, lng) inside this dumpsite's
|
|
* boundary polygon? Returns false if no boundary is configured.
|
|
*/
|
|
public function containsPoint(float $latitude, float $longitude): bool
|
|
{
|
|
if (! $this->boundary_polygon) {
|
|
return false;
|
|
}
|
|
|
|
$row = DB::selectOne(
|
|
'SELECT ST_Contains(boundary_polygon, ST_SRID(POINT(?, ?), 4326)) AS contained
|
|
FROM dumpsites WHERE id = ? LIMIT 1',
|
|
[$longitude, $latitude, $this->id],
|
|
);
|
|
|
|
return (bool) ($row->contained ?? false);
|
|
}
|
|
}
|