126 lines
3.7 KiB
PHP
126 lines
3.7 KiB
PHP
<?php
|
|
namespace Sodino\Models;
|
|
|
|
/**
|
|
* Rule Model
|
|
*/
|
|
class Rule {
|
|
public $id;
|
|
public $name;
|
|
public $conditions;
|
|
public $actions;
|
|
public $priority;
|
|
public $usage_limit;
|
|
public $usage_count;
|
|
public $user_roles;
|
|
public $start_date;
|
|
public $end_date;
|
|
public $enabled;
|
|
public $created_at;
|
|
public $updated_at;
|
|
public $condition_type;
|
|
public $condition_value;
|
|
public $action_type;
|
|
public $action_value;
|
|
|
|
/**
|
|
* Constructor
|
|
*/
|
|
public function __construct($data = []) {
|
|
$this->id = $data['id'] ?? null;
|
|
$this->name = $data['name'] ?? '';
|
|
$this->conditions = $this->parseJsonField($data['conditions'] ?? '[]');
|
|
$this->actions = $this->parseJsonField($data['actions'] ?? '[]');
|
|
$this->priority = isset($data['priority']) ? (int) $data['priority'] : 10;
|
|
$this->usage_limit = isset($data['usage_limit']) ? (int) $data['usage_limit'] : 0;
|
|
$this->usage_count = isset($data['usage_count']) ? (int) $data['usage_count'] : 0;
|
|
$this->user_roles = $this->parseRolesField($data['user_roles'] ?? '');
|
|
$this->start_date = $data['start_date'] ?? null;
|
|
$this->end_date = $data['end_date'] ?? null;
|
|
$this->enabled = isset($data['enabled']) ? (int) $data['enabled'] : 1;
|
|
$this->created_at = $data['created_at'] ?? null;
|
|
$this->updated_at = $data['updated_at'] ?? null;
|
|
$this->syncLegacyFields();
|
|
}
|
|
|
|
private function parseJsonField($value) {
|
|
if (is_array($value)) {
|
|
return $value;
|
|
}
|
|
|
|
$decoded = json_decode($value, true);
|
|
return is_array($decoded) ? $decoded : [];
|
|
}
|
|
|
|
private function parseRolesField($value) {
|
|
if (is_array($value)) {
|
|
return array_filter(array_map('trim', $value));
|
|
}
|
|
|
|
if (!is_string($value)) {
|
|
return [];
|
|
}
|
|
|
|
return array_filter(array_map('trim', explode(',', $value)));
|
|
}
|
|
|
|
public function syncLegacyFields() {
|
|
$condition = $this->conditions[0] ?? [];
|
|
$action = $this->actions[0] ?? [];
|
|
|
|
$this->condition_type = $condition['type'] ?? 'user_type';
|
|
$this->condition_value = $condition['value'] ?? 'new';
|
|
$this->action_type = $action['type'] ?? 'discount_percent';
|
|
$this->action_value = $action['value'] ?? 0;
|
|
}
|
|
|
|
/**
|
|
* Convert to array
|
|
*/
|
|
public function toArray() {
|
|
return [
|
|
'id' => $this->id,
|
|
'name' => $this->name,
|
|
'conditions' => wp_json_encode($this->conditions),
|
|
'actions' => wp_json_encode($this->actions),
|
|
'priority' => $this->priority,
|
|
'usage_limit' => $this->usage_limit,
|
|
'usage_count' => $this->usage_count,
|
|
'user_roles' => is_array($this->user_roles) ? implode(',', $this->user_roles) : $this->user_roles,
|
|
'start_date' => $this->start_date,
|
|
'end_date' => $this->end_date,
|
|
'enabled' => $this->enabled,
|
|
'created_at' => $this->created_at,
|
|
'updated_at' => $this->updated_at,
|
|
];
|
|
}
|
|
|
|
/**
|
|
* Check if rule is active
|
|
*/
|
|
public function isActive() {
|
|
if (!$this->enabled) {
|
|
return false;
|
|
}
|
|
|
|
$now = current_time('mysql');
|
|
|
|
if (!empty($this->start_date) && $now < $this->start_date) {
|
|
return false;
|
|
}
|
|
|
|
if (!empty($this->end_date) && $now > $this->end_date) {
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* Check if usage limit reached
|
|
*/
|
|
public function hasReachedLimit() {
|
|
return $this->usage_limit > 0 && $this->usage_count >= $this->usage_limit;
|
|
}
|
|
}
|