ClassPackageUnitVariationResource.php 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. <?php
  2. namespace App\Http\Resources;
  3. use App\Models\ClassPackageUnitVariation;
  4. use Carbon\Carbon;
  5. use Illuminate\Http\Request;
  6. use Illuminate\Http\Resources\Json\AnonymousResourceCollection;
  7. use Illuminate\Http\Resources\Json\JsonResource;
  8. class ClassPackageUnitVariationResource extends JsonResource
  9. {
  10. /**
  11. * Transform the resource into an array.
  12. *
  13. * @return array<string, mixed>
  14. */
  15. public function toArray(Request $request): array
  16. {
  17. return [
  18. 'id' => $this->id,
  19. 'class_package_unit_id' => $this->class_package_unit_id,
  20. 'type' => $this->type,
  21. 'register_value' => (float) $this->register_value,
  22. 'register_installments_min' => $this->register_installments_min,
  23. 'register_installments_max' => $this->register_installments_max,
  24. 'package_value' => (float) $this->package_value,
  25. 'package_installments_min' => $this->package_installments_min,
  26. 'package_installments_max' => $this->package_installments_max,
  27. 'discount_value' => $this->discount_value !== null ? (float) $this->discount_value : null,
  28. 'material_value' => (float) $this->material_value,
  29. 'material_installments_min' => $this->material_installments_min,
  30. 'material_installments_max' => $this->material_installments_max,
  31. 'total_value' => $this->totalValue(),
  32. 'created_at' => Carbon::parse($this->created_at)->format('Y-m-d H:i:s'),
  33. 'updated_at' => Carbon::parse($this->updated_at)->format('Y-m-d H:i:s'),
  34. // Relationships
  35. 'materials' => $this->whenLoaded('products', fn () =>
  36. $this->products->map(fn ($material) => [
  37. 'product_id' => $material->product_id,
  38. 'name' => $material->product?->name,
  39. 'quantity' => $material->quantity,
  40. 'price' => (float) $material->price,
  41. ])->values()
  42. ),
  43. ];
  44. }
  45. /**
  46. * @param \Illuminate\Database\Eloquent\Collection<ClassPackageUnitVariation> $resource
  47. * @return \Illuminate\Http\Resources\Json\AnonymousResourceCollection<ClassPackageUnitVariationResource>
  48. */
  49. public static function collection($resource): AnonymousResourceCollection
  50. {
  51. return parent::collection($resource);
  52. }
  53. }