Files
new.rybstock.ru/app/Services/PriceTierService.php
T
2026-06-04 19:19:35 +03:00

58 lines
1.7 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Services;
use App\Repositories\PromocodeRepository;
final class PriceTierService
{
public function __construct(
private readonly ?PromocodeRepository $promocodes = null
) {
}
/**
* @return array<int, array<string, mixed>>
*/
public function forAmount(float $basePrice): array
{
$tiers = [[
'label' => 'Базовая цена',
'min_order_amount' => 0.0,
'discount_percent' => 0.0,
'price' => round($basePrice, 2),
'note' => 'Без промокода',
]];
foreach ($this->repository()->priceTiers() as $promocode) {
$discount = (float) $promocode['discount_value'];
$tiers[] = [
'label' => $promocode['price_tier_label'] ?: $promocode['title'],
'code' => $promocode['code'],
'min_order_amount' => (float) $promocode['min_order_amount'],
'discount_percent' => $discount,
'price' => round($basePrice * (100 - $discount) / 100, 2),
'note' => $promocode['payment_note'],
];
}
$tiers[] = [
'label' => 'Безналичный расчет',
'code' => null,
'min_order_amount' => 0.0,
'discount_percent' => -8.0,
'price' => round($basePrice * 1.08, 2),
'note' => '+8% к базовой цене при безналичном расчете',
];
return $tiers;
}
private function repository(): PromocodeRepository
{
return $this->promocodes ?? new PromocodeRepository();
}
}