Files
api/services/nginx/app/modules/goals/classes/goals.php
T
Jeppe Bundgaard 51b8198a67 Introduce goals module with methods for result, percentage, and remaining calculations
- Add `goals` class for managing goal metrics and calculations.
- Extend `goals_criteria` with timeframe initialization and validation.
- Implement `getListByCriteria` in `order_items_o.php` for product-based goal evaluation.
- Add unit tests for `goals` to validate
2026-01-26 16:24:23 +01:00

63 lines
1.3 KiB
PHP

<?php
namespace goals\classes;
use goals\helpers\goals_criteria_type;
/**
* Class goals
* Main class for the goals module.
*/
class goals
{
/**
* The goals criteria
* @var goals_criteria $criteria
*/
public goals_criteria $criteria;
/**
* Constructor
* @param goals_criteria|null $criteria
*/
public function __construct(?goals_criteria $criteria = null)
{
$this->criteria = $criteria ?? new goals_criteria();
}
/**
* Get the current result for the goal based on the criteria.
* @return int|float
*/
public function getResult(): int|float
{
return match ($this->criteria->type) {
goals_criteria_type::PRODUCT => $this->criteria->products->count($this->criteria),
default => 0,
};
}
/**
* Get the percentage completion of the goal.
* @return float
*/
public function getPercentage(): float
{
if ($this->criteria->target <= 0) {
return 0.0;
}
return ($this->getResult() / $this->criteria->target) * 100;
}
/**
* Get the remaining value to reach the goal target.
* @return int|float
*/
public function getRemaining(): int|float
{
$remaining = $this->criteria->target - $this->getResult();
return max(0, $remaining);
}
}