StudentContractResource.php 4.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. 'signature_date' => $this->signature_date ? Carbon::parse($this->signature_date)->format('d/m/Y') : null,
  24. 'end_date' => $this->end_date ? Carbon::parse($this->end_date)->format('d/m/Y') : null,
  25. 'class_quantity' => $this->class_quantity,
  26. 'weekday' => $this->weekday,
  27. 'start_time' => $this->start_time,
  28. 'end_time' => $this->end_time,
  29. 'second_weekday' => $this->second_weekday,
  30. 'second_start_time' => $this->second_start_time,
  31. 'second_end_time' => $this->second_end_time,
  32. 'due_day' => $this->recurring_day,
  33. 'tax_register' => $this->tax_register,
  34. 'installments' => $this->installments,
  35. 'enrollment_due_date' => $this->enrollment_due_date
  36. ? Carbon::parse($this->enrollment_due_date)->format('d/m/Y')
  37. : null,
  38. 'package_value' => $this->package_value,
  39. 'package_installments' => $this->package_installments,
  40. 'package_due_date' => $this->package_due_date
  41. ? Carbon::parse($this->package_due_date)->format('d/m/Y')
  42. : null,
  43. 'early_payment_discount' => $this->early_payment_discount,
  44. 'interest_rate' => $this->interest_rate,
  45. 'payment_method' => $this->payment_method,
  46. 'fine_cancelled' => $this->fine_cancelled,
  47. 'status' => $this->status,
  48. 'package_name' => $this->whenLoaded('classPackageUnit', fn () => $this->classPackageUnit?->name),
  49. 'unit_name' => $this->whenLoaded('unit', fn () => $this->unit?->fantasy_name),
  50. 'student_name' => $this->whenLoaded('student', fn () => $this->student->name),
  51. 'student_document' => $this->whenLoaded('student', fn () => $this->student->document_number),
  52. 'student_birth_date' => $this->whenLoaded('student', fn () => $this->student->birth_date?->format('d/m/Y')),
  53. 'student_phone' => $this->whenLoaded('student', fn () => $this->student->phone),
  54. 'student_city' => $this->whenLoaded('student', fn () => $this->student->city?->name),
  55. 'student_state_code' => $this->whenLoaded('student', fn () => $this->student->state?->code),
  56. 'file_url' => $this->file_url ? Storage::url($this->file_url) : null,
  57. 'file_type' => $this->file_type,
  58. 'signed_file_url' => $this->signed_file_url ? Storage::url($this->signed_file_url) : null,
  59. 'signed_file_type' => $this->signed_file_type,
  60. 'version_history' => $this->whenLoaded(
  61. 'versionHistory',
  62. fn () => self::collection($this->versionHistory),
  63. ),
  64. 'created_at' => Carbon::parse($this->created_at)->format('Y-m-d H:i:s'),
  65. 'updated_at' => Carbon::parse($this->updated_at)->format('Y-m-d H:i:s'),
  66. ];
  67. }
  68. /**
  69. * @param \Illuminate\Database\Eloquent\Collection<StudentContract> $resource
  70. * @return \Illuminate\Http\Resources\Json\AnonymousResourceCollection<StudentContractResource>
  71. */
  72. public static function collection($resource): AnonymousResourceCollection
  73. {
  74. return parent::collection($resource);
  75. }
  76. }