Files
api/services/nginx/app/statistics/economic_s.php
T
Jepp9350 c82c201ff1 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).
2025-02-04 11:46:35 +01:00

148 lines
6.0 KiB
PHP

<?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
{
// Get the economic class
$economic = new economic();
// Get the totals
$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;
}
}