- 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
63 lines
1.3 KiB
PHP
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);
|
|
}
|
|
}
|