| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283 |
- <?php
- namespace App\Models;
- use Illuminate\Database\Eloquent\Factories\HasFactory;
- use Illuminate\Database\Eloquent\Model;
- use Illuminate\Database\Eloquent\SoftDeletes;
- /**
- * @property int $id
- * @property int|null $unit_id Unidade que criou o card (nulo para Matriz)
- * @property string $title
- * @property string|null $description
- * @property string $phase a_fazer|em_progresso|em_revisao|concluido|demandas_especiais
- * @property string $priority alta|normal|baixa
- * @property string $origin matriz|unit
- * @property string $scope internal|all|specific
- * @property string|null $sector
- * @property string|null $due_date
- * @property int|null $responsible_user_id
- * @property int|null $created_by_user_id
- * @property int|null $target_unit_id
- * @property \Illuminate\Support\Carbon|null $created_at
- * @property \Illuminate\Support\Carbon|null $updated_at
- * @property string|null $deleted_at
- * @mixin \Eloquent
- */
- class Kanban extends Model
- {
- use HasFactory, SoftDeletes;
- protected $table = 'kanbans';
- protected $guarded = ['id'];
- protected $casts = [
- 'created_at' => 'datetime',
- 'updated_at' => 'datetime',
- 'due_date' => 'date',
- ];
- // Relationships
- public function applicantUnit()
- {
- return $this->belongsTo(Unit::class, 'unit_id');
- }
- public function targetUnit()
- {
- return $this->belongsTo(Unit::class, 'target_unit_id');
- }
- public function responsibleUser()
- {
- return $this->belongsTo(User::class, 'responsible_user_id');
- }
- public function createdByUser()
- {
- return $this->belongsTo(User::class, 'created_by_user_id');
- }
- public function replies()
- {
- return $this->hasMany(KanbanReply::class, 'kanban_id');
- }
- // Query Scopes
- public function scopeVisibleToUnit($query, int $unitId)
- {
- return $query->where(function ($q) use ($unitId) {
- // Cards created by this unit (origin = unit)
- $q->where('unit_id', $unitId)
- // Cards explicitly targeted at this unit (specific or broadcast-per-unit)
- ->orWhere('target_unit_id', $unitId);
- // Note: scope='all' is NOT used here — when broadcasting the controller
- // already creates one card per unit each with its own target_unit_id,
- // so filtering by target_unit_id is sufficient and correct.
- });
- }
- }
|