77 lines
2.4 KiB
PHP
77 lines
2.4 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' => $this->customerFriendlyPrice($basePrice),
|
|
'note' => 'Без промокода',
|
|
]];
|
|
|
|
foreach ($this->repository()->priceTiers() as $promocode) {
|
|
$discount = (float) $promocode['discount_value'];
|
|
$minOrderAmount = (float) $promocode['min_order_amount'];
|
|
$tiers[] = [
|
|
'label' => $this->priceTierLabel($minOrderAmount, (string) ($promocode['price_tier_label'] ?: $promocode['title'])),
|
|
'code' => $promocode['code'],
|
|
'min_order_amount' => $minOrderAmount,
|
|
'discount_percent' => $discount,
|
|
'price' => $this->customerFriendlyPrice($basePrice * (100 - $discount) / 100),
|
|
'note' => $promocode['payment_note'],
|
|
];
|
|
}
|
|
|
|
$tiers[] = [
|
|
'label' => 'Оплата по расчетному счету',
|
|
'code' => null,
|
|
'min_order_amount' => 0.0,
|
|
'discount_percent' => -8.0,
|
|
'price' => $this->customerFriendlyPrice($basePrice * 1.08),
|
|
'note' => '+8% к базовой цене при безналичном расчете',
|
|
];
|
|
|
|
return $tiers;
|
|
}
|
|
|
|
private function customerFriendlyPrice(float $price): int
|
|
{
|
|
return (int) floor($price);
|
|
}
|
|
|
|
private function priceTierLabel(float $minOrderAmount, string $fallback): string
|
|
{
|
|
if ((int) $minOrderAmount === 20000) {
|
|
return 'Цена при сумме заказа от 20 000р. наличными';
|
|
}
|
|
|
|
if ((int) $minOrderAmount === 50000) {
|
|
return 'Цена при сумме заказа от 50 000р. наличными';
|
|
}
|
|
|
|
return $fallback;
|
|
}
|
|
|
|
private function repository(): PromocodeRepository
|
|
{
|
|
return $this->promocodes ?? new PromocodeRepository();
|
|
}
|
|
}
|