50 lines
2 KiB
PHP
50 lines
2 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Http\Resources\Fo\Ajax;
|
|
|
|
use App\Dto\BasketProductDto;
|
|
use App\Lib\Helpers\MoneyHelper;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Http\Resources\Json\JsonResource;
|
|
use Illuminate\Support\Facades\Gate;
|
|
|
|
class BasketProductResource extends JsonResource
|
|
{
|
|
public function __construct(private readonly BasketProductDto $item)
|
|
{
|
|
}
|
|
|
|
/** @return array<string,mixed> */
|
|
#[\Override]
|
|
public function toArray(Request $request): array
|
|
{
|
|
$product = $this->item->product;
|
|
$canViewPrices = Gate::allows('viewPrices', $this->item);
|
|
|
|
return [
|
|
'id' => $product->id->value,
|
|
'name' => $product->name,
|
|
'qty' => $product->qty,
|
|
|
|
'unitPriceHt' => $canViewPrices ? MoneyHelper::serialized($product->unitPriceHt()) : null,
|
|
'discountTotal' => $canViewPrices ? MoneyHelper::serialized($product->discountTotal()) : null,
|
|
'taxTotal' => $canViewPrices ? MoneyHelper::serialized($product->taxTotal()) : null,
|
|
'subtotalHt' => $canViewPrices ? MoneyHelper::serialized($product->subtotalHt()) : null,
|
|
'totalTtc' => $canViewPrices ? MoneyHelper::serialized($product->totalTtc()) : null,
|
|
|
|
'discountedPriceTtc' => $canViewPrices ? MoneyHelper::serialized($product->unitTtc()) : null,
|
|
'priceKgTtc' => $canViewPrices ? MoneyHelper::serialized($product->priceKgTtc) : null,
|
|
|
|
'slug' => $product->slug,
|
|
'picture' => $product->picture ? publicS3Asset($product->picture) : null,
|
|
'description' => $product->description,
|
|
'weight_variable' => $product->weightVariable,
|
|
'discountRate' => $product->discountRate->ratio(),
|
|
|
|
'taxRate' => $product->taxRate->percent(),
|
|
'weightGrams' => $product->weightGrams,
|
|
];
|
|
}
|
|
}
|