Add JSON import/export functionality for goals module
- Implement `exportToJson` and `importFromJson` methods in `goals` for serialization and deserialization. - Add `fromJson` method in `goals_criteria` to parse criteria from JSON input. - Extend `exampleRoute` for debugging with sample JSON-based goal creation. - Update `index.php` to autoload new `goals` module classes and dependencies.
This commit is contained in:
@@ -89,6 +89,20 @@ require_once 'modules/economic/customers/economic_customer_mo.php';
|
||||
require_once 'modules/economic/invoices/draft/economicInvoicesDrafts.php';
|
||||
require_once 'modules/economic/invoices/draft/economic_invoice_draft_mo.php';
|
||||
|
||||
// Goals module
|
||||
foreach (glob(WD . '/modules/goals/interfaces/*.php') as $interface) {
|
||||
require_once $interface;
|
||||
}
|
||||
foreach (glob(WD . '/modules/goals/traits/*.php') as $trait) {
|
||||
require_once $trait;
|
||||
}
|
||||
require_once 'modules/goals/helpers/goals_criteria_type.php';
|
||||
require_once 'modules/goals/classes/goals_criteria_users.php';
|
||||
require_once 'modules/goals/classes/goals_criteria_departments.php';
|
||||
require_once 'modules/goals/classes/goals_criteria_products.php';
|
||||
require_once 'modules/goals/classes/goals_criteria.php';
|
||||
require_once 'modules/goals/classes/goals.php';
|
||||
|
||||
use classes\db;
|
||||
use classes\redis;
|
||||
use classes\request;
|
||||
|
||||
@@ -59,4 +59,25 @@ class goals
|
||||
$remaining = $this->criteria->target - $this->getResult();
|
||||
return max(0, $remaining);
|
||||
}
|
||||
|
||||
/**
|
||||
* Export to JSON (Used to be able to import/export goals)
|
||||
* @return string
|
||||
*/
|
||||
public function exportToJson(): string
|
||||
{
|
||||
return json_encode($this->criteria);
|
||||
}
|
||||
|
||||
/**
|
||||
* Import from JSON (Used to be able to import/export goals)
|
||||
* @param string $json
|
||||
* @return goals
|
||||
* @throws \Exception If the JSON is invalid
|
||||
*/
|
||||
public static function importFromJson(string $json): goals
|
||||
{
|
||||
$criteria = goals_criteria::fromJson($json);
|
||||
return new goals($criteria);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -43,6 +43,31 @@ class goals_criteria implements goals_criteria_i
|
||||
$this->products = new goals_criteria_products();
|
||||
$this->start = new \DateTime();
|
||||
$this->end = new \DateTime();
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws \Exception If the JSON is invalid
|
||||
*/
|
||||
public static function fromJson(string $json): goals_criteria
|
||||
{
|
||||
$data = json_decode($json, true);
|
||||
$criteria = new goals_criteria();
|
||||
|
||||
if (isset($data['type'])) {
|
||||
$criteria->type = goals_criteria_type::tryFrom($data['type']);
|
||||
}
|
||||
if (isset($data['target'])) {
|
||||
$criteria->target = $data['target'];
|
||||
}
|
||||
if (isset($data['start'])) {
|
||||
$criteria->start = new \DateTime($data['start']);
|
||||
}
|
||||
if (isset($data['end'])) {
|
||||
$criteria->end = new \DateTime($data['end']);
|
||||
}
|
||||
// Additional parsing for users, departments, products can be added here
|
||||
|
||||
return $criteria;
|
||||
}
|
||||
}
|
||||
@@ -7,6 +7,8 @@ use classes\redis;
|
||||
use goals\classes\goals;
|
||||
use goals\classes\goals_criteria;
|
||||
use objects\departments_o;
|
||||
use objects\products_o;
|
||||
use objects\users_o;
|
||||
use traits\route_t;
|
||||
|
||||
class exampleRoute
|
||||
@@ -51,7 +53,21 @@ class exampleRoute
|
||||
|
||||
$this->get('/debug', function () {
|
||||
global $response;
|
||||
$goal = (new goals());
|
||||
$criteria = (new goals_criteria());
|
||||
$criteria->type = \goals\helpers\goals_criteria_type::PRODUCT;
|
||||
$criteria->target = 100;
|
||||
$criteria->products->add((new products_o())->select(1));
|
||||
$criteria->users->add((new users_o())->getUserByCustomerNumber(12345679));
|
||||
$criteria->departments->add([
|
||||
(new departments_o())->select(1),
|
||||
(new departments_o())->select(2),
|
||||
(new departments_o())->select(3),
|
||||
]);
|
||||
$criteria->setTimeframe(new \DateTime('2026-01-01'), new \DateTime('2026-01-31'));
|
||||
$goal = new goals($criteria);
|
||||
echo 'Result: ' . $goal->getResult() . PHP_EOL;
|
||||
echo 'Percentage: ' . $goal->getPercentage() . '%' . PHP_EOL;
|
||||
echo 'Remaining: ' . $goal->getRemaining() . PHP_EOL;
|
||||
$response->success(['message' => 'Debugging route!']);
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user