refactor(Core): optimize admin panel and refactor

This commit is contained in:
2026-05-07 00:15:32 +03:30
parent dec4e67b9e
commit 7cc14b7439
25 changed files with 1162 additions and 282 deletions

View File

@@ -11,6 +11,7 @@ class PricingService {
private $trackingService;
private $settings;
private $cache;
private $trackedApplications = [];
public function __construct(RuleRepository $ruleRepository, TrackingService $trackingService) {
$this->ruleRepository = $ruleRepository;
@@ -51,8 +52,7 @@ class PricingService {
$price = $this->applyRuleActions($rule, $price);
if ($price < $oldPrice) {
$this->trackingService->recordDiscountApplied($product, $oldPrice, $price, $rule->id);
$this->ruleRepository->incrementUsage($rule->id);
$this->trackDiscountOnce($product, $oldPrice, $price, $rule->id);
}
}
@@ -206,7 +206,7 @@ class PricingService {
return false;
}
return (bool) array_intersect($product_cats, array_map('intval', $categories));
return (bool) array_intersect($product_cats, $this->normalizeIdList($categories));
}
private function productIsInIds($product, $ids) {
@@ -214,7 +214,23 @@ class PricingService {
return false;
}
return in_array($product->get_id(), array_map('intval', $ids), true);
return in_array($product->get_id(), $this->normalizeIdList($ids), true);
}
private function normalizeIdList($value) {
$values = is_array($value) ? $value : [$value];
$ids = [];
foreach ($values as $item) {
foreach (explode(',', (string) $item) as $id) {
$id = absint(trim($id));
if ($id > 0) {
$ids[] = $id;
}
}
}
return array_values(array_unique($ids));
}
private function applyRuleActions($rule, $price) {
@@ -273,4 +289,17 @@ class PricingService {
}
return 0;
}
private function trackDiscountOnce($product, $oldPrice, $price, $ruleId) {
$productId = $product ? $product->get_id() : 0;
$key = implode(':', [$productId, (int) $ruleId, round($oldPrice, 4), round($price, 4)]);
if (isset($this->trackedApplications[$key])) {
return;
}
$this->trackedApplications[$key] = true;
$this->trackingService->recordDiscountApplied($product, $oldPrice, $price, $ruleId);
$this->ruleRepository->incrementUsage($ruleId);
}
}