Kanban.php 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <?php
  2. namespace App\Models;
  3. use Illuminate\Database\Eloquent\Factories\HasFactory;
  4. use Illuminate\Database\Eloquent\Model;
  5. use Illuminate\Database\Eloquent\SoftDeletes;
  6. /**
  7. * @property int $id
  8. * @property int|null $unit_id Unidade que criou o card (nulo para Matriz)
  9. * @property string $title
  10. * @property string|null $description
  11. * @property string $phase a_fazer|em_progresso|em_revisao|concluido|demandas_especiais
  12. * @property string $priority alta|normal|baixa
  13. * @property string $origin matriz|unit
  14. * @property string $scope internal|all|specific
  15. * @property string|null $sector
  16. * @property string|null $due_date
  17. * @property int|null $responsible_user_id
  18. * @property int|null $created_by_user_id
  19. * @property int|null $target_unit_id
  20. * @property \Illuminate\Support\Carbon|null $created_at
  21. * @property \Illuminate\Support\Carbon|null $updated_at
  22. * @property string|null $deleted_at
  23. * @mixin \Eloquent
  24. */
  25. class Kanban extends Model
  26. {
  27. use HasFactory, SoftDeletes;
  28. protected $table = 'kanbans';
  29. protected $guarded = ['id'];
  30. protected $casts = [
  31. 'created_at' => 'datetime',
  32. 'updated_at' => 'datetime',
  33. 'due_date' => 'date',
  34. ];
  35. // Relationships
  36. public function applicantUnit()
  37. {
  38. return $this->belongsTo(Unit::class, 'unit_id');
  39. }
  40. public function targetUnit()
  41. {
  42. return $this->belongsTo(Unit::class, 'target_unit_id');
  43. }
  44. public function responsibleUser()
  45. {
  46. return $this->belongsTo(User::class, 'responsible_user_id');
  47. }
  48. public function createdByUser()
  49. {
  50. return $this->belongsTo(User::class, 'created_by_user_id');
  51. }
  52. public function replies()
  53. {
  54. return $this->hasMany(KanbanReply::class, 'kanban_id');
  55. }
  56. // Query Scopes
  57. public function scopeVisibleToUnit($query, int $unitId)
  58. {
  59. return $query->where(function ($q) use ($unitId) {
  60. // Cards created by this unit (origin = unit)
  61. $q->where('unit_id', $unitId)
  62. // Cards explicitly targeted at this unit (specific or broadcast-per-unit)
  63. ->orWhere('target_unit_id', $unitId);
  64. // Note: scope='all' is NOT used here — when broadcasting the controller
  65. // already creates one card per unit each with its own target_unit_id,
  66. // so filtering by target_unit_id is sufficient and correct.
  67. });
  68. }
  69. }