Changes to UI and other stuff
This commit is contained in:
@@ -2,6 +2,8 @@
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Attributes\Scope;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
@@ -57,6 +59,69 @@ protected static function booted()
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Scope activities to those linked to contracts within a specific segment.
|
||||
*/
|
||||
#[Scope]
|
||||
public function scopeForSegment(Builder $query, int $segmentId, array $contractIds): Builder
|
||||
{
|
||||
return $query->where(function ($q) use ($contractIds) {
|
||||
$q->whereNull('contract_id');
|
||||
if (! empty($contractIds)) {
|
||||
$q->orWhereIn('contract_id', $contractIds);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Scope activities with decoded base64 filters.
|
||||
*/
|
||||
#[Scope]
|
||||
public function scopeWithFilters(Builder $query, ?string $encodedFilters, \App\Models\ClientCase $clientCase): Builder
|
||||
{
|
||||
if (empty($encodedFilters)) {
|
||||
return $query;
|
||||
}
|
||||
|
||||
try {
|
||||
$decompressed = base64_decode($encodedFilters);
|
||||
$filters = json_decode($decompressed, true);
|
||||
|
||||
if (! is_array($filters)) {
|
||||
return $query;
|
||||
}
|
||||
|
||||
if (! empty($filters['action_id'])) {
|
||||
$query->where('action_id', $filters['action_id']);
|
||||
}
|
||||
|
||||
if (! empty($filters['contract_uuid'])) {
|
||||
$contract = $clientCase->contracts()->where('uuid', $filters['contract_uuid'])->first(['id']);
|
||||
if ($contract) {
|
||||
$query->where('contract_id', $contract->id);
|
||||
}
|
||||
}
|
||||
|
||||
if (! empty($filters['user_id'])) {
|
||||
$query->where('user_id', $filters['user_id']);
|
||||
}
|
||||
|
||||
if (! empty($filters['date_from'])) {
|
||||
$query->whereDate('created_at', '>=', $filters['date_from']);
|
||||
}
|
||||
|
||||
if (! empty($filters['date_to'])) {
|
||||
$query->whereDate('created_at', '<=', $filters['date_to']);
|
||||
}
|
||||
} catch (\Throwable $e) {
|
||||
\Log::error('Invalid activity filter format', [
|
||||
'error' => $e->getMessage(),
|
||||
]);
|
||||
}
|
||||
|
||||
return $query;
|
||||
}
|
||||
|
||||
public function action(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(\App\Models\Action::class);
|
||||
|
||||
@@ -3,6 +3,8 @@
|
||||
namespace App\Models;
|
||||
|
||||
use App\Traits\Uuid;
|
||||
use Illuminate\Database\Eloquent\Attributes\Scope;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Database\Eloquent\Casts\Attribute;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
@@ -55,6 +57,7 @@ protected function startDate(): Attribute
|
||||
return null;
|
||||
}
|
||||
$str = is_string($value) ? $value : (string) $value;
|
||||
|
||||
return \App\Services\DateNormalizer::toDate($str);
|
||||
}
|
||||
);
|
||||
@@ -71,11 +74,26 @@ protected function endDate(): Attribute
|
||||
return null;
|
||||
}
|
||||
$str = is_string($value) ? $value : (string) $value;
|
||||
|
||||
return \App\Services\DateNormalizer::toDate($str);
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Scope contracts to those in a specific segment with active pivot.
|
||||
*/
|
||||
#[Scope]
|
||||
public function scopeForSegment(Builder $query, int $segmentId): Builder
|
||||
{
|
||||
return $query->whereExists(function ($q) use ($segmentId) {
|
||||
$q->from('contract_segment')
|
||||
->whereColumn('contract_segment.contract_id', 'contracts.id')
|
||||
->where('contract_segment.segment_id', $segmentId)
|
||||
->where('contract_segment.active', true);
|
||||
});
|
||||
}
|
||||
|
||||
public function type(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(\App\Models\ContractType::class, 'type_id');
|
||||
|
||||
Reference in New Issue
Block a user