feat(tax): implement tax rate history tracking and API endpoints

This commit is contained in:
hamed
2026-06-24 13:17:08 +03:30
parent 148d033114
commit 89e4a424f8
7 changed files with 186 additions and 82 deletions
+28 -3
View File
@@ -16,8 +16,6 @@ use OpenApi\Attributes as OA;
class SiteConfigController extends BaseController
{
private const ALLOWED_KEYS = [
'commission_enabled',
'commission_percent',
// financial engine
'appointment_commission_enabled',
'upgrade_commission_enabled',
@@ -47,6 +45,7 @@ class SiteConfigController extends BaseController
public function __construct(
private readonly SiteConfigRepository $configRepo,
private readonly \App\Config\Repository\TaxRateHistoryRepository $taxHistoryRepo,
private readonly EntityManagerInterface $em,
) {}
@@ -57,10 +56,14 @@ class SiteConfigController extends BaseController
}
#[Route('/api/v1/admin/settings', methods: ['PATCH'])]
public function patch(Request $request): JsonResponse
public function patch(Request $request, #[\Symfony\Component\Security\Http\Attribute\CurrentUser] \App\Auth\Entity\User $admin): JsonResponse
{
$data = json_decode($request->getContent(), true) ?? [];
// مقادیر فعلی مالیات برای تشخیص تغییر و ثبت تاریخچه.
$prevTaxPercent = $this->configRepo->get('tax_percent');
$prevTaxEnabled = $this->configRepo->get('tax_enabled');
foreach ($data as $key => $value) {
if (!in_array($key, self::ALLOWED_KEYS, true)) {
continue;
@@ -70,6 +73,28 @@ class SiteConfigController extends BaseController
$this->em->flush();
$newTaxPercent = $this->configRepo->get('tax_percent');
$newTaxEnabled = $this->configRepo->get('tax_enabled');
if ($newTaxPercent !== $prevTaxPercent || $newTaxEnabled !== $prevTaxEnabled) {
$this->taxHistoryRepo->save(new \App\Config\Entity\TaxRateHistory(
(string) $newTaxPercent,
$newTaxEnabled === '1',
$admin,
));
}
return $this->success($this->configRepo->getAll());
}
#[Route('/api/v1/admin/settings/tax-history', methods: ['GET'])]
public function taxHistory(): JsonResponse
{
$rows = $this->taxHistoryRepo->findBy([], ['changedAt' => 'DESC'], 50);
return $this->success(array_map(
fn(\App\Config\Entity\TaxRateHistory $h) => $h->toArray(),
$rows,
));
}
}