diff --git a/services/nginx/app/index.php b/services/nginx/app/index.php index 0cc03299..fe759faf 100644 --- a/services/nginx/app/index.php +++ b/services/nginx/app/index.php @@ -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; diff --git a/services/nginx/app/modules/goals/classes/goals.php b/services/nginx/app/modules/goals/classes/goals.php index 0d6c9bbd..c787c22d 100644 --- a/services/nginx/app/modules/goals/classes/goals.php +++ b/services/nginx/app/modules/goals/classes/goals.php @@ -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); + } } diff --git a/services/nginx/app/modules/goals/classes/goals_criteria.php b/services/nginx/app/modules/goals/classes/goals_criteria.php index 8918af60..13fba4c2 100644 --- a/services/nginx/app/modules/goals/classes/goals_criteria.php +++ b/services/nginx/app/modules/goals/classes/goals_criteria.php @@ -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; + } } \ No newline at end of file diff --git a/services/nginx/app/routes/exampleRoute.php b/services/nginx/app/routes/exampleRoute.php index e09d68cf..63294668 100644 --- a/services/nginx/app/routes/exampleRoute.php +++ b/services/nginx/app/routes/exampleRoute.php @@ -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!']); }); }