49 lines
1.3 KiB
PHP
49 lines
1.3 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'],
|
|
];
|
|
}
|
|
|
|
return $tiers;
|
|
}
|
|
|
|
private function repository(): PromocodeRepository
|
|
{
|
|
return $this->promocodes ?? new PromocodeRepository();
|
|
}
|
|
}
|