diff --git a/services/nginx/app/modules/economic/endpoints/departments/economic_departments_endpoint.php b/services/nginx/app/modules/economic/endpoints/departments/economic_departments_endpoint.php index 620eca30..617f3561 100644 --- a/services/nginx/app/modules/economic/endpoints/departments/economic_departments_endpoint.php +++ b/services/nginx/app/modules/economic/endpoints/departments/economic_departments_endpoint.php @@ -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); } diff --git a/services/nginx/app/modules/economic/endpoints/economic_invoices_endpoint.php b/services/nginx/app/modules/economic/endpoints/economic_invoices_endpoint.php index 141b8d28..397c5130 100644 --- a/services/nginx/app/modules/economic/endpoints/economic_invoices_endpoint.php +++ b/services/nginx/app/modules/economic/endpoints/economic_invoices_endpoint.php @@ -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(); } } \ No newline at end of file diff --git a/services/nginx/app/modules/economic/endpoints/invoices/economic_invoices_booked_endpoint.php b/services/nginx/app/modules/economic/endpoints/invoices/economic_invoices_booked_endpoint.php new file mode 100644 index 00000000..02419725 --- /dev/null +++ b/services/nginx/app/modules/economic/endpoints/invoices/economic_invoices_booked_endpoint.php @@ -0,0 +1,45 @@ +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; + } + +} \ No newline at end of file diff --git a/services/nginx/app/statistics/economic_s.php b/services/nginx/app/statistics/economic_s.php index 530bee9c..e54fc678 100644 --- a/services/nginx/app/statistics/economic_s.php +++ b/services/nginx/app/statistics/economic_s.php @@ -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; } diff --git a/services/nginx/app/traits/economic_endpoint_t.php b/services/nginx/app/traits/economic_endpoint_t.php index c9795405..f9b776a2 100644 --- a/services/nginx/app/traits/economic_endpoint_t.php +++ b/services/nginx/app/traits/economic_endpoint_t.php @@ -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 diff --git a/services/nginx/app/traits/statistic_t.php b/services/nginx/app/traits/statistic_t.php index 65845edd..f99c28c5 100644 --- a/services/nginx/app/traits/statistic_t.php +++ b/services/nginx/app/traits/statistic_t.php @@ -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') diff --git a/services/nginx/nginx.conf b/services/nginx/nginx.conf index 7cde20ea..eba20470 100644 --- a/services/nginx/nginx.conf +++ b/services/nginx/nginx.conf @@ -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; }