Add support for handling booked invoices and department stats

Introduced functionality for fetching and processing booked invoices with department-specific statistics. Added pagination and filtering capabilities for economic endpoints, including department data handling. Updated nginx configuration to support additional HTTP methods (PUT, DELETE).
This commit is contained in:
Jepp9350
2025-02-04 11:46:35 +01:00
parent 6ebea5504e
commit c82c201ff1
7 changed files with 184 additions and 7 deletions
@@ -10,11 +10,15 @@ class economic_departments_endpoint
/**
* List all departments
* @param array $filters Example: ['filter' => 'value']
* @param array $pagination Example: ['maxPageSize' => 100, 'skipPages' => 0]
* @return object {collection: [department], pagination: {maxPageSize: number, skipPages: number, results: number}}
*/
public function get(): object
public function get(array $filters = [], array $pagination = []): object
{
$response = $this->send_request('/departments', 'GET');
$response = $this->send_request(
'/departments/?filter=' . self::filters($filters) . self::pagination($pagination),
'GET');
// Return the response as an object
return json_decode($response);
}
@@ -3,7 +3,9 @@
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';
require_once WD . '/modules/economic/endpoints/invoices/economic_invoices_booked_endpoint.php';
use endpoints\orders\economic_invoices_booked_endpoint;
use endpoints\orders\economic_invoices_sent_endpoint;
use endpoints\orders\economic_invoices_totals_endpoint;
use traits\economic_endpoint_t;
@@ -22,10 +24,16 @@ class economic_invoices_endpoint
* @var economic_invoices_sent_endpoint
*/
public economic_invoices_sent_endpoint $sent;
/**
* Any endpoints reached by the /invoices/booked endpoint
* @var economic_invoices_booked_endpoint
*/
public economic_invoices_booked_endpoint $booked;
public function __construct()
{
$this->totals = new economic_invoices_totals_endpoint();
$this->sent = new economic_invoices_sent_endpoint();
$this->booked = new economic_invoices_booked_endpoint();
}
}
@@ -0,0 +1,45 @@
<?php
namespace endpoints\orders;
use traits\economic_endpoint_t;
class economic_invoices_booked_endpoint
{
use economic_endpoint_t;
/**
* List sent invoices
* @param string $filter Set the filter using the self::filter method
* @return object {collection: [order], pagination: {maxPageSize: number, skipPages: number, results: number}}
*/
public function get(array $filters = []): object
{
$response = $this->send_request(
'/invoices/booked/?filter=' . self::filters($filters),
'GET'
);
// Return the response as an object
return json_decode($response);
}
/**
* Get invoice lines
* @param array $invoice_ids List of invoice ids to get lines for (e.g. [1, 2, 3])
* @param array $filters Set the filter using the self::filter method
* @return array {invoice_id: {collection: [invoice_line], pagination: {maxPageSize: number, skipPages: number, results: number}}}
*/
public function get_invoice_lines(array $invoice_ids, array $filters = []): array
{
$tmp_invoices = [];
foreach ( $invoice_ids as $invoice ) {
$response = $this->send_request(
'/invoices/booked/' . $invoice . '/?filter=' . self::filters($filters),
'GET'
);
$tmp_invoices[$invoice] = json_decode($response)->lines;
}
return $tmp_invoices;
}
}
+84 -3
View File
@@ -54,12 +54,93 @@ class economic_s
*/
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);
$invoices = $economic->invoices->booked->get([
'(date$gte:' . $this->start . '$and:date$lte:' . $this->end . ')' => '', // To allow for time filtering, we need to add the time frame to the query
])->collection;
// Get the departments
$departments = $economic->departments->departments->get(
[],
[
'skipPages' => 0,
'pageSize' => 1000, // Since the limit is 1000, we need to set the page size to 1000.
]
)->collection;
/**
* To get the department name from the department number, we need to create an array with the department number as the key and the department name as the value.
* This results in an array like this:
* departmentNumber => name
* [
* 10 => ['name' => 'Department 1', 'departmentNumber' => 10, 'products' => ['product 1' => 10, 'product 2' => 20], 'totalNetAmount' => 0],
* 20 => ['name' => 'Department 2', 'departmentNumber' => 20, 'products' => ['product 1' => 10, 'product 2' => 20], 'totalNetAmount' => 0],
* ]
*/
$departments_temp = array_column($departments, 'name', 'departmentNumber');
// Add an unknown (0) department, in case the departmentDistributionNumber is not set
$departments_temp[0] = 'Unknown';
$departments = [];
foreach ( $departments_temp as $departmentNumber => $name ) {
$departments[$departmentNumber] = [
'name' => $name,
'departmentNumber' => $departmentNumber,
'products' => [],
'totalNetAmount' => 0,
'invoiceCount' => 0,
];
}
// Get the bookedInvoiceNumbers
$booked_invoice_numbers = [];
foreach ( $invoices as $invoice ) {
$booked_invoice_numbers[] = $invoice->bookedInvoiceNumber;
}
// Get the invoice lines
$invoice_lines = $economic->invoices->booked->get_invoice_lines($booked_invoice_numbers);
// Add the net amount, invoice count and products array to the departments
foreach ( $invoice_lines as $invoice_id => $lines ) {
foreach ( $lines as $line ) {
// Check if the departmentDistributionNumber is set and the quantity is greater than 0
$tmp_department = 0;
if (isset($line->departmentalDistribution->departmentalDistributionNumber)) {
// Set the department to unknown
$tmp_department = $line->departmentalDistribution->departmentalDistributionNumber;
}
if ($line->quantity == 0) {
// Skip the line if the quantity is 0
continue;
}
// Add the net amount to the department
$departments[$tmp_department]['totalNetAmount'] += $line->totalNetAmount;
// Add the product to the department
if (!isset($departments[$tmp_department]['products'][$line->product->productNumber])) {
$departments[$tmp_department]['products'][$line->product->productNumber] = 0;
}
$departments[$tmp_department]['products'][$line->product->productNumber] += $line->quantity;
// Add the invoice count to the department
$departments[$tmp_department]['invoiceCount']++;
}
}
// Clear the invoice lines that has quantity 0, since they don't have any value to the statistics
foreach ( $invoice_lines as $invoice_id => $lines ) {
foreach ( $lines as $key => $line ) {
if ($line->quantity == 0) {
unset($invoice_lines[$invoice_id][$key]);
}
}
}
// Add the data sets
foreach ( $departments as $department ) {
$this->add_data_set($department['name'], [
'Faktureret (DKK)' => $department['totalNetAmount'],
]);
}
return $this;
}
@@ -18,6 +18,44 @@ trait economic_endpoint_t
}
/**
* Filters for e-conomic requests
* @param array $filters
* @return string The filters for the request (e.g. 'customerNumber$eq:12345678')
* @example ['customer.customerNumber' => '12345678']
*/
public static function filters(array $filters): string
{
$filter_string = '';
foreach ( $filters as $key => $value ) {
// If the filter string is not empty, and the key has a value, add an $and
if (!empty($filter_string) && !empty($value)) {
$filter_string .= '$and:';
}
// Check if there's a "$" in the key, and if not, add it as $eq (equals) by default
if (!str_contains($value, '$') && !empty($value)) {
$value = '$eq:' . $value;
}
$filter_string .= $key . $value;
}
return $filter_string;
}
/**
* Pagination for e-conomic requests
* @param array $pagination
* @return string The pagination for the request (e.g. '&maxPageSize=100&skipPages=0')
* @example ['maxPageSize' => 100, 'skipPages' => 0]
*/
public static function pagination(array $pagination): string
{
$pagination_string = '';
foreach ( $pagination as $key => $value ) {
$pagination_string .= '&' . $key . '=' . $value;
}
return $pagination_string;
}
/**
* Send a request to the Economic API
* @param string $url
@@ -105,6 +105,7 @@ trait statistic_t
return date('Y-m-d');
}
/**
* Add a data set to the statistics
* @param string $label The label for the data set (e.g. 'January')
+2 -2
View File
@@ -58,13 +58,13 @@ http {
# Location block for PHP files
location ^~ / {
add_header Access-Control-Allow-Methods "GET, POST, OPTIONS";
add_header Access-Control-Allow-Methods "GET, POST, OPTIONS, PUT, DELETE";
add_header Access-Control-Allow-Headers "Authorization, Content-Type, X-Requested-With";
add_header Access-Control-Allow-Credentials true;
if ($request_method = 'OPTIONS') {
add_header Access-Control-Allow-Origin *;
add_header Access-Control-Allow-Methods "GET, POST, OPTIONS";
add_header Access-Control-Allow-Methods "GET, POST, OPTIONS, PUT, DELETE";
add_header Access-Control-Allow-Headers "Authorization, Content-Type, X-Requested-With";
return 204;
}