47 lines
1.4 KiB
PHP
47 lines
1.4 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Resources\Product;
|
|
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Http\Resources\Json\ResourceCollection;
|
|
|
|
class ProductCollection extends ResourceCollection
|
|
{
|
|
/**
|
|
* Transform the resource collection into an array.
|
|
*
|
|
* @return array<string, mixed>
|
|
*/
|
|
public function toArray(Request $request): array
|
|
{
|
|
return [
|
|
'data' => $this->collection,
|
|
'pagination' => [
|
|
'current_page' => $this->currentPage(),
|
|
'from' => $this->firstItem(),
|
|
'last_page' => $this->lastPage(),
|
|
'per_page' => $this->perPage(),
|
|
'to' => $this->lastItem(),
|
|
'total' => $this->total(),
|
|
],
|
|
'summary' => [
|
|
'total_products' => $this->collection->count(),
|
|
'low_stock_products' => $this->collection->filter(function ($product) {
|
|
return $product->stock_actuel <= $product->stock_minimum;
|
|
})->count(),
|
|
'total_value' => $this->collection->sum(function ($product) {
|
|
return $product->stock_actuel * $product->prix_unitaire;
|
|
}),
|
|
],
|
|
];
|
|
}
|
|
|
|
public function with(Request $request): array
|
|
{
|
|
return [
|
|
'status' => 'success',
|
|
'message' => 'Produits récupérés avec succès',
|
|
];
|
|
}
|
|
}
|