Introduce Economic customer helper class and customer route

Added a helper class `economic_customer` to standardize customer data handling within the Economic API. Introduced a new route, `/modules/economic/customer`, for retrieving customers using their customer number. Updated `economic` class to integrate the helper and support streamlined customer operations.
This commit is contained in:
Jepp9350
2025-02-19 11:58:58 +01:00
parent 156f036d4c
commit 143b024f78
5 changed files with 203 additions and 1 deletions
+19
View File
@@ -8,15 +8,18 @@ require_once WD . '/modules/economic/endpoints/economic_departments_endpoint.php
require_once WD . '/modules/economic/endpoints/economic_layouts_endpoint.php';
require_once WD . '/modules/economic/endpoints/economic_customers_endpoint.php';
require_once WD . '/modules/economic/endpoints/economic_products_endpoint.php';
require_once WD . '/modules/economic/economic_helpers.php';
use economic_c;
use economic_helpers;
use endpoints\economic_customers_endpoint;
use endpoints\economic_departments_endpoint;
use endpoints\economic_invoices_endpoint;
use endpoints\economic_layouts_endpoint;
use endpoints\economic_orders_endpoint;
use endpoints\economic_products_endpoint;
use helpers\economic_customer;
use interfaces\economic_i;
class economic implements economic_i
@@ -56,6 +59,11 @@ class economic implements economic_i
* @var economic_products_endpoint
*/
public economic_products_endpoint $products;
/**
* Helper classes
* @var economic_helpers
*/
protected economic_helpers $helpers;
public function __construct()
@@ -67,5 +75,16 @@ class economic implements economic_i
$this->layouts = new economic_layouts_endpoint();
$this->customers = new economic_customers_endpoint();
$this->products = new economic_products_endpoint();
$this->helpers = new economic_helpers();
}
/**
* Get a customer by their customer number
* @param int $customer_number The Economic customer number
* @return economic_customer
*/
public function getCustomer(int $customer_number): economic_customer
{
return new $this->helpers->economic_customer($customer_number);
}
}
@@ -0,0 +1,12 @@
<?php
use helpers\economic_customer;
class economic_helpers
{
/**
* The Economic customer helper
* @var string $economic_customer
*/
public string $economic_customer = economic_customer::class;
}
@@ -36,5 +36,4 @@ class economic_customers_endpoint
// Return true if the customer exists
return isset($tmp->customerNumber);
}
}
@@ -0,0 +1,129 @@
<?php
namespace helpers;
use classes\economic;
use Exception;
class economic_customer
{
/**
* The Economic customerNumber, which is the unique identifier for a customer
* @var int $customer_number
*/
protected int $customer_number;
/**
* The raw customer data from the Economic API
* @var object $customer_data
*/
protected object $customer_data_object;
/**
* Construct a new Economic customer object
* @throws Exception if the customer does not exist
*/
public function __construct(int $customer_number)
{
self::setCustomerNumber($customer_number);
self::requireExists();
self::fetchCustomerData();
self::requireSelected();
}
/**
* Check if a customer exists
* @param int $customer_number The Economic customer number to check for
* @throws Exception if the customer does not exist
*/
protected function requireExists(): void
{
if (!self::exists()) {
throw new Exception('The customer does not exist in the Economic system');
}
}
/**
* Check if a customer exists in the Economic system
* @return bool True if the customer exists, false if not
* @throws Exception
*/
public function exists(): bool
{
// Make sure the customer number is set
self::requireCustomerNumber();
// Check if the customer exists
$economic = new economic();
return $economic->customers->customers->exists(self::getCustomerNumber());
}
/**
* Require that the customer number is set
* @throws Exception if the customer number is not set
*/
protected function requireCustomerNumber(): void
{
if (!isset($this->customer_number)) {
throw new Exception('The customer number is not set');
}
}
/**
* Get the customer number
* @return int The Economic customer number
*/
public function getCustomerNumber(): int
{
return $this->customer_number;
}
/**
* Set the customer number
* @param int $customer_number The Economic customer number
*/
protected function setCustomerNumber(int $customer_number): void
{
$this->customer_number = $customer_number;
}
/**
* Fetch the customer data from the Economic API and store it in the object property $customer_data_object
* @throws Exception if the customer data could not be fetched
* @throws Exception if the customer data is invalid or empty
*/
protected function fetchCustomerData(): void
{
// Get the customer data from the Economic API
$economic = new economic();
$tmp = $economic->customers->customers->get($this->customer_number);
// Check if the customer data is invalid or empty
if (!isset($tmp->customerNumber)) {
throw new Exception('The customer data is invalid or empty');
}
// Store the customer data in the object property
$this->customer_data_object = $tmp;
}
/**
* Require that a customer is selected, i.e. that the customer data is valid and not empty
* @throws Exception if the customer data is invalid or empty
*/
protected function requireSelected(): void
{
if (!isset($this->customer_data_object->customerNumber)) {
throw new Exception('The customer data is invalid or empty');
}
}
/**
* Get the customer data as an object
* @return object The customer data as an object
* @throws Exception if the customer data is invalid or empty
*/
public function asObject(): object
{
self::requireSelected();
return $this->customer_data_object;
}
}
@@ -0,0 +1,43 @@
<?php
namespace routes;
use classes\authentication;
use classes\economic;
use classes\response;
use classes\router;
use objects\logs_o;
use traits\route_t;
class moduleEconomicCustomerRoute
{
use route_t;
public function run(): void
{
global /** @var response $response */
/** @var router $router */
$router, $response;
/** Modules > Economic > Customer > Get customer */
$this->get('/modules/economic/customer', function () {
global $response;
self::requirePermission('modules_economic_customer_get');
$user = (new authentication())->get_user();
if ($user) {
self::requireParameters(['customer_number']);
self::requireType('customer_number', self::type_int());
self::requireMinLength('customer_number', 1);
self::requireMaxLength('customer_number', 10);
(new logs_o())->add('modules_economic', 'global', 1, 0, 'MODULES_ECONOMIC', 'User accessed the customer');
$result = (new economic())->getCustomer((int)$_GET['customer_number']);
$response->success((object)$result);
} else {
(new logs_o())->add('modules_economic', 'global', 0, 0, 'MODULES_ECONOMIC', 'User tried to access the customer without a valid session');
$response->error('Invalid session', 400);
}
});
}
}