Add customer rule product restrictions

This commit is contained in:
Jeppe B
2026-07-16 11:50:52 +02:00
parent 879dfcf79a
commit e1fb79d9b6
19 changed files with 1816 additions and 354 deletions
+45 -28
View File
@@ -3,6 +3,7 @@
namespace objects;
use classes\db;
use classes\customer_rule_product_restriction_schema_bootstrap;
use classes\customer_name_cache_payload_builder;
use classes\object_property;
use classes\redis;
@@ -268,15 +269,19 @@ class users_o extends db
public function addAttribute(string $attribute, ?int $user_id = null): void
{
global $db;
customer_rule_product_restriction_schema_bootstrap::ensureSchema();
if ($user_id === null) {
self::requireSelected();
$user_id = $this->id;
}
$attribute = $db->escape_string($attribute);
// To prevent two attributes with the same name for the same user, we will delete the old one if it exists
$this->deleteAttribute($attribute, $user_id);
$sql = "INSERT INTO customer_attributes (user_id, attribute) VALUES ($user_id, '$attribute')";
$db->query($sql);
$userIds = $this->attributeSiblingUserIds((int)$user_id);
foreach ($userIds as $targetUserId) {
$db->query(
"INSERT IGNORE INTO customer_attributes (user_id, attribute)
VALUES ({$targetUserId}, '{$attribute}')"
);
}
}
public function deleteAttribute(string $attribute, ?int $user_id = null): void
@@ -286,29 +291,9 @@ class users_o extends db
$user_id = $this->id;
}
$attribute = $db->escape_string($attribute);
// If the user has a customer number, we will delete the attribute from all associated customers
$customer_number = $this->customer_number->value();
if (!empty($customer_number)) {
$sql = "SELECT * FROM users WHERE customer_number = $customer_number";
$result = $db->query($sql);
$user_ids = [];
while ($row = $result->fetch_assoc()) {
$user_ids[] = (int)$row['id'];
}
// Delete the attribute from all users
foreach ($user_ids as $user_id) {
$sql = "DELETE FROM customer_attributes WHERE user_id = $user_id AND attribute = '$attribute' LIMIT 1";
$db->query($sql);
}
return;
foreach ($this->attributeSiblingUserIds((int)$user_id) as $targetUserId) {
$db->query("DELETE FROM customer_attributes WHERE user_id = {$targetUserId} AND attribute = '{$attribute}'");
}
// Make sure the attribute exists
if (!$this->doesUserHaveAttribute((string)$attribute, (int)$user_id)) {
return;
}
// If the attribute exists, delete it, if it does not exist, nothing will happen
$sql = "DELETE FROM customer_attributes WHERE user_id = $user_id AND attribute = '$attribute' LIMIT 1";
$db->query($sql);
}
public function doesUserHaveAttribute(string $attribute, ?int $user_id = null): bool
@@ -318,7 +303,8 @@ class users_o extends db
$user_id = $this->id;
}
$attribute = $db->escape_string($attribute);
$sql = "SELECT * FROM customer_attributes WHERE user_id = $user_id AND attribute = '$attribute' LIMIT 1";
$userIds = $this->attributeSiblingUserIds((int)$user_id);
$sql = "SELECT id FROM customer_attributes WHERE user_id IN (" . implode(',', $userIds) . ") AND attribute = '$attribute' LIMIT 1";
$result = $db->query($sql);
if ($result->num_rows > 0) {
return true;
@@ -736,13 +722,44 @@ class users_o extends db
if ($user_id === null) {
$user_id = $this->id;
}
$sql = "SELECT * FROM customer_attributes WHERE user_id = $user_id";
$userIds = $this->attributeSiblingUserIds((int)$user_id);
$sql = "SELECT MIN(id) AS id, MIN(user_id) AS user_id, attribute, MIN(created_at) AS created_at
FROM customer_attributes
WHERE user_id IN (" . implode(',', $userIds) . ")
GROUP BY attribute
ORDER BY attribute";
$result = $db->query($sql);
$array = $db->fetch_all($result);
$this->attributes = $array;
return $this->attributes;
}
/** @return list<int> */
private function attributeSiblingUserIds(int $userId): array
{
global $db;
if ($userId < 1) {
return [$userId];
}
$result = $db->query("SELECT customer_number FROM users WHERE id = {$userId} LIMIT 1");
if (!$result || $result->num_rows < 1) {
return [$userId];
}
$row = $result->fetch_assoc();
$customerNumber = (int)($row['customer_number'] ?? 0);
if ($customerNumber < 1) {
return [$userId];
}
$siblings = $db->query("SELECT id FROM users WHERE customer_number = {$customerNumber}");
$ids = [];
if ($siblings) {
while ($sibling = $siblings->fetch_assoc()) {
$ids[] = (int)$sibling['id'];
}
}
return $ids !== [] ? array_values(array_unique($ids)) : [$userId];
}
/**
* Get all discounts for the user (Include)
* @return void