| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859 |
- <?php
- namespace App\Http\Resources;
- use App\Models\ClassPackageUnitVariation;
- use Carbon\Carbon;
- use Illuminate\Http\Request;
- use Illuminate\Http\Resources\Json\AnonymousResourceCollection;
- use Illuminate\Http\Resources\Json\JsonResource;
- class ClassPackageUnitVariationResource extends JsonResource
- {
- /**
- * Transform the resource into an array.
- *
- * @return array<string, mixed>
- */
- public function toArray(Request $request): array
- {
- return [
- 'id' => $this->id,
- 'class_package_unit_id' => $this->class_package_unit_id,
- 'type' => $this->type,
- 'register_value' => (float) $this->register_value,
- 'register_installments_min' => $this->register_installments_min,
- 'register_installments_max' => $this->register_installments_max,
- 'package_value' => (float) $this->package_value,
- 'package_installments_min' => $this->package_installments_min,
- 'package_installments_max' => $this->package_installments_max,
- 'discount_value' => $this->discount_value !== null ? (float) $this->discount_value : null,
- 'material_value' => (float) $this->material_value,
- 'material_installments_min' => $this->material_installments_min,
- 'material_installments_max' => $this->material_installments_max,
- 'total_value' => $this->totalValue(),
- 'created_at' => Carbon::parse($this->created_at)->format('Y-m-d H:i:s'),
- 'updated_at' => Carbon::parse($this->updated_at)->format('Y-m-d H:i:s'),
- // Relationships
- 'materials' => $this->whenLoaded('products', fn () =>
- $this->products->map(fn ($material) => [
- 'product_id' => $material->product_id,
- 'name' => $material->product?->name,
- 'quantity' => $material->quantity,
- 'price' => (float) $material->price,
- ])->values()
- ),
- ];
- }
- /**
- * @param \Illuminate\Database\Eloquent\Collection<ClassPackageUnitVariation> $resource
- * @return \Illuminate\Http\Resources\Json\AnonymousResourceCollection<ClassPackageUnitVariationResource>
- */
- public static function collection($resource): AnonymousResourceCollection
- {
- return parent::collection($resource);
- }
- }
|