StudentContractResource.php 5.0 KB

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