Add endpoints and routes for economic statistics management

Implemented economic-related classes, endpoints, and routes to support fetching invoice totals, sent invoices, and department data. These changes enhance functionality by exposing APIs for economic statistics and integrating them into the statistics module.
This commit is contained in:
Jepp9350
2025-02-04 08:36:05 +01:00
parent 0960f35007
commit 6ebea5504e
10 changed files with 285 additions and 0 deletions
+16
View File
@@ -2,7 +2,11 @@
namespace classes;
require_once WD . '/modules/economic/endpoints/economic_orders_endpoint.php';
require_once WD . '/modules/economic/endpoints/economic_invoices_endpoint.php';
require_once WD . '/modules/economic/endpoints/economic_departments_endpoint.php';
use endpoints\economic_departments_endpoint;
use endpoints\economic_invoices_endpoint;
use endpoints\economic_orders_endpoint;
use interfaces\economic_i;
@@ -13,9 +17,21 @@ class economic implements economic_i
* @var economic_orders_endpoint
*/
public economic_orders_endpoint $orders;
/**
* Any endpoints reached by the /invoices endpoint
* @var economic_invoices_endpoint
*/
public economic_invoices_endpoint $invoices;
/**
* Any endpoints reached by the /departments endpoint
* @var economic_departments_endpoint
*/
public economic_departments_endpoint $departments;
public function __construct()
{
$this->orders = new economic_orders_endpoint();
$this->invoices = new economic_invoices_endpoint();
$this->departments = new economic_departments_endpoint();
}
}
@@ -4,6 +4,7 @@ namespace classes;
use interfaces\statistics_i;
use statistics\bookings_s;
use statistics\economic_s;
use statistics\orders_s;
class statistics implements statistics_i
@@ -24,4 +25,12 @@ class statistics implements statistics_i
{
return new orders_s();
}
/**
* @inheritDoc
*/
public function economic(): economic_s
{
return new economic_s();
}
}
@@ -3,6 +3,7 @@
namespace interfaces;
use statistics\bookings_s;
use statistics\economic_s;
use statistics\orders_s;
interface statistics_i
@@ -18,4 +19,10 @@ interface statistics_i
* @return orders_s
*/
public function orders(): orders_s;
/**
* Get the statistics for economic
* @return economic_s
*/
public function economic(): economic_s;
}
@@ -0,0 +1,22 @@
<?php
namespace endpoints\departments;
use traits\economic_endpoint_t;
class economic_departments_endpoint
{
use economic_endpoint_t;
/**
* List all departments
* @return object {collection: [department], pagination: {maxPageSize: number, skipPages: number, results: number}}
*/
public function get(): object
{
$response = $this->send_request('/departments', 'GET');
// Return the response as an object
return json_decode($response);
}
}
@@ -0,0 +1,22 @@
<?php
namespace endpoints;
require_once WD . '/modules/economic/endpoints/departments/economic_departments_endpoint.php';
use traits\economic_endpoint_t;
class economic_departments_endpoint
{
use economic_endpoint_t;
/**
* Any endpoints reached by the /invoices/totals endpoint
* @var departments\economic_departments_endpoint
*/
public departments\economic_departments_endpoint $departments;
public function __construct()
{
$this->departments = new departments\economic_departments_endpoint();
}
}
@@ -0,0 +1,31 @@
<?php
namespace endpoints;
require_once WD . '/modules/economic/endpoints/invoices/economic_invoices_totals_endpoint.php';
require_once WD . '/modules/economic/endpoints/invoices/economic_invoices_sent_endpoint.php';
use endpoints\orders\economic_invoices_sent_endpoint;
use endpoints\orders\economic_invoices_totals_endpoint;
use traits\economic_endpoint_t;
class economic_invoices_endpoint
{
use economic_endpoint_t;
/**
* Any endpoints reached by the /invoices/totals endpoint
* @var economic_invoices_totals_endpoint
*/
public economic_invoices_totals_endpoint $totals;
/**
* Any endpoints reached by the /invoices/sent endpoint
* @var economic_invoices_sent_endpoint
*/
public economic_invoices_sent_endpoint $sent;
public function __construct()
{
$this->totals = new economic_invoices_totals_endpoint();
$this->sent = new economic_invoices_sent_endpoint();
}
}
@@ -0,0 +1,22 @@
<?php
namespace endpoints\orders;
use traits\economic_endpoint_t;
class economic_invoices_sent_endpoint
{
use economic_endpoint_t;
/**
* List sent invoices
* @return object {collection: [order], pagination: {maxPageSize: number, skipPages: number, results: number}}
*/
public function get(): object
{
$response = $this->send_request('/invoices/sent', 'GET');
// Return the response as an object
return json_decode($response);
}
}
@@ -0,0 +1,29 @@
<?php
namespace endpoints\orders;
use traits\economic_endpoint_t;
class economic_invoices_totals_endpoint
{
use economic_endpoint_t;
/**
* List all invoices, unless a query is provided
* @return object {collection: [order], pagination: {maxPageSize: number, skipPages: number, results: number}}
*/
public function get(): object
{
$response = $this->send_request('/invoices/totals', 'GET');
// Return the response as an object
return json_decode($response);
}
public function get_department_sent_invoice_totals()
{
$department_id = 20;
$response = $this->send_request('/invoices/sent?departmentId=' . $department_id, 'GET');
return json_decode($response);
}
}
@@ -74,5 +74,65 @@ class statisticsRoute
}
});
/** Statistics >> Economic >> Total income */
$this->get('/statistics/economic/totals', function () {
// Require the user to be logged in
global
/** @var response $response */
$EMAIL_WASH_CERTIFICATE_TOKEN,
$response;
$this->requirePermission('statistics_economic_income');
// Get the user object
$user = (new authentication())->get_user();
// Check if the request was successful
if ($user) {
// Log the incident
(new logs_o())->add('statistics', 'global', 1, $user->id, 'TOTAL_INCOME', 'User requested total income');
$statics = new statistics();
// Return the total income
$response->success(
$statics
->economic()
->get_totals()
->get_response()
);
} else {
// Log the incident
(new logs_o())->add('statistics', 'global', 0, 0, 'TOTAL_INCOME', 'User requested total income');
// Return an error
$response->error('Invalid session', 400);
}
});
/** Statistics >> Economic >> Department sent invoice totals */
$this->get('/statistics/economic/totals/department_sent_invoice_totals', function () {
// Require the user to be logged in
global
/** @var response $response */
$EMAIL_WASH_CERTIFICATE_TOKEN,
$response;
$this->requirePermission('statistics_economic_department_sent_invoice_totals');
// Get the user object
$user = (new authentication())->get_user();
// Check if the request was successful
if ($user) {
// Log the incident
(new logs_o())->add('statistics', 'global', 1, $user->id, 'DEPARTMENT_SENT_INVOICE_TOTALS', 'User requested department sent invoice totals');
$statics = new statistics();
// Return the department sent invoice totals
$response->success(
$statics
->economic()
->get_department_sent_invoice_totals()
->get_response()
);
} else {
// Log the incident
(new logs_o())->add('statistics', 'global', 0, 0, 'DEPARTMENT_SENT_INVOICE_TOTALS', 'User requested department sent invoice totals');
// Return an error
$response->error('Invalid session', 400);
}
});
}
}
@@ -0,0 +1,67 @@
<?php
namespace statistics;
use classes\economic;
use traits\statistic_t;
class economic_s
{
use statistic_t;
/**
* Get the totals for the invoices
* WARNING: This is function does NOT support time filtering
* @return $this
*/
public function get_totals(): economic_s
{
// Get the economic class
$economic = new economic();
// Get the totals
$data = $economic->invoices->totals->get();
// Add the data
$this->add_data_set('Drafts', [
'netAmountInBaseCurrency' => $data->drafts->netAmountInBaseCurrency,
'invoiceCount' => $data->drafts->invoiceCount,
]);
$this->add_data_set('Booked', [
'netAmountInBaseCurrency' => $data->booked->netAmountInBaseCurrency,
'invoiceCount' => $data->booked->invoiceCount,
]);
$this->add_data_set('Unpaid', [
'netAmountInBaseCurrency' => $data->booked->unpaid->netAmountInBaseCurrency,
'invoiceCount' => $data->booked->unpaid->invoiceCount,
]);
$this->add_data_set('Not Overdue', [
'netAmountInBaseCurrency' => $data->booked->unpaid->notOverdue->netAmountInBaseCurrency,
'invoiceCount' => $data->booked->unpaid->notOverdue->invoiceCount,
]);
$this->add_data_set('Overdue', [
'netAmountInBaseCurrency' => $data->booked->unpaid->overdue->netAmountInBaseCurrency,
'invoiceCount' => $data->booked->unpaid->overdue->invoiceCount,
]);
$this->add_data_set('Paid', [
'netAmountInBaseCurrency' => $data->booked->paid->netAmountInBaseCurrency,
'invoiceCount' => $data->booked->paid->invoiceCount,
]);
return $this;
}
/**
* Get department sent invoice total amount and count
* @return $this
*/
public function get_department_sent_invoice_totals(): economic_s
{
//TODO: Implement this
// Get the economic class
$economic = new economic();
// Get the totals
$data = $economic->invoices->sent->get();
//print_r($data);
return $this;
}
}