StudentContractResource.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. namespace App\Http\Resources;
  3. use App\Models\StudentContract;
  4. use Carbon\Carbon;
  5. use Illuminate\Http\Request;
  6. use Illuminate\Http\Resources\Json\AnonymousResourceCollection;
  7. use Illuminate\Http\Resources\Json\JsonResource;
  8. use Illuminate\Support\Facades\Storage;
  9. class StudentContractResource extends JsonResource
  10. {
  11. public function toArray(Request $request): array
  12. {
  13. return [
  14. 'id' => $this->id,
  15. 'version' => $this->version,
  16. 'derived_from_contract_id' => $this->derived_from_contract_id,
  17. 'created_by_user_id' => $this->created_by_user_id,
  18. 'created_by_user_name' => $this->whenLoaded('createdBy', fn () => $this->createdBy?->name),
  19. 'protocol' => $this->protocol,
  20. 'student_id' => $this->student_id,
  21. 'unit_id' => $this->unit_id,
  22. 'class_package_unit_id' => $this->class_package_unit_id,
  23. 'pricing_mode' => $this->pricing_mode,
  24. 'pricing_mode_label' => match ($this->pricing_mode) {
  25. 'pavao' => 'Pavão',
  26. 'irrecusavel' => 'Irrecusável',
  27. default => null,
  28. },
  29. 'signature_date' => $this->signature_date ? Carbon::parse($this->signature_date)->format('d/m/Y') : null,
  30. 'end_date' => $this->end_date ? Carbon::parse($this->end_date)->format('d/m/Y') : null,
  31. 'class_quantity' => $this->class_quantity,
  32. 'weekday' => $this->weekday,
  33. 'start_time' => $this->start_time,
  34. 'end_time' => $this->end_time,
  35. 'second_weekday' => $this->second_weekday,
  36. 'second_start_time' => $this->second_start_time,
  37. 'second_end_time' => $this->second_end_time,
  38. 'due_day' => $this->recurring_day,
  39. 'tax_register' => $this->tax_register,
  40. 'installments' => $this->installments,
  41. 'enrollment_due_date' => $this->enrollment_due_date
  42. ? Carbon::parse($this->enrollment_due_date)->format('d/m/Y')
  43. : null,
  44. 'package_value' => $this->package_value,
  45. 'package_installments' => $this->package_installments,
  46. 'package_due_date' => $this->package_due_date
  47. ? Carbon::parse($this->package_due_date)->format('d/m/Y')
  48. : null,
  49. 'materials_value' => $this->materials_value,
  50. 'materials_installments' => $this->materials_installments,
  51. 'materials_due_date' => $this->materials_due_date
  52. ? Carbon::parse($this->materials_due_date)->format('d/m/Y')
  53. : null,
  54. 'total_value' => $this->total_value,
  55. 'total_installments' => $this->total_installments,
  56. 'total_due_date' => $this->total_due_date
  57. ? Carbon::parse($this->total_due_date)->format('d/m/Y')
  58. : null,
  59. 'early_payment_discount' => $this->early_payment_discount,
  60. 'interest_rate' => $this->interest_rate,
  61. 'payment_method' => $this->payment_method,
  62. 'fine_cancelled' => $this->fine_cancelled,
  63. 'status' => $this->status,
  64. 'package_name' => $this->whenLoaded('classPackageUnit', fn () => $this->classPackageUnit?->name),
  65. 'unit_name' => $this->whenLoaded('unit', fn () => $this->unit?->fantasy_name),
  66. 'student_name' => $this->whenLoaded('student', fn () => $this->student->name),
  67. 'student_document' => $this->whenLoaded('student', fn () => $this->student->document_number),
  68. 'student_birth_date' => $this->whenLoaded('student', fn () => $this->student->birth_date?->format('d/m/Y')),
  69. 'student_phone' => $this->whenLoaded('student', fn () => $this->student->phone),
  70. 'student_city' => $this->whenLoaded('student', fn () => $this->student->city?->name),
  71. 'student_state_code' => $this->whenLoaded('student', fn () => $this->student->state?->code),
  72. 'file_url' => $this->file_url ? Storage::url($this->file_url) : null,
  73. 'file_type' => $this->file_type,
  74. 'signed_file_url' => $this->signed_file_url ? Storage::url($this->signed_file_url) : null,
  75. 'signed_file_type' => $this->signed_file_type,
  76. 'version_history' => $this->whenLoaded(
  77. 'versionHistory',
  78. fn () => self::collection($this->versionHistory),
  79. ),
  80. 'created_at' => Carbon::parse($this->created_at)->format('Y-m-d H:i:s'),
  81. 'updated_at' => Carbon::parse($this->updated_at)->format('Y-m-d H:i:s'),
  82. ];
  83. }
  84. /**
  85. * @param \Illuminate\Database\Eloquent\Collection<StudentContract> $resource
  86. * @return \Illuminate\Http\Resources\Json\AnonymousResourceCollection<StudentContractResource>
  87. */
  88. public static function collection($resource): AnonymousResourceCollection
  89. {
  90. return parent::collection($resource);
  91. }
  92. }