Refactor InvoicingPeriodRoute: add meta parameter for transactions, enhance fixed pricing retrieval, and implement utility method for array-based object matching.

This commit is contained in:
Jepp9350
2025-07-03 09:59:12 +02:00
parent bf6c2d711d
commit 3d8700bad6
@@ -215,7 +215,8 @@ class InvoicingPeriodRoute
string $customer_name,
array $transactions = [],
bool $requires_action = false,
?int $user_id = null
?int $user_id = null,
?array $meta = null
): array
{
return [
@@ -226,6 +227,7 @@ class InvoicingPeriodRoute
return self::constructTransactionObject($transaction);
}, $transactions),
'requires_action' => self::checkRequiresAction($parsed_transactions, $requires_action),
'meta' => $meta ?? [],
];
}
@@ -311,9 +313,18 @@ class InvoicingPeriodRoute
if ($customersWithTransactions === null) {
$customersWithTransactions = self::getCustomersWithTransactions($dateFrom, $dateTo);
}
// Filter out customers that do not have any transactions in the specified date range
// Get all customers with fixed pricing
$customer_numbers = (new \objects\users_o())->getCustomersWithFixedPricing();
//self::filterCustomersWithTransactions($customer_numbers, $customersWithTransactions);
// Get the customers fixed pricing
$tmp_fixed_pricing = array_map(function ($arr) {
// Return the customer number and price
return [
'customer_number' => (int)$arr['customer_number'],
'price' => (float)$arr['price'],
'description' => (string)($arr['description'] ?? ''),
];
}, (new \objects\customer_fixed_pricing_o())->getFieldsWhere(['customer_number' => $customer_numbers], ['customer_number', 'price', 'description']));
// Get all customers with fixed pricing
$fixed_pricing = [];
@@ -327,15 +338,42 @@ class InvoicingPeriodRoute
if (end($fixed_pricing) === null) {
$fixed_pricing[count($fixed_pricing) - 1] = self::constructCustomerObject(
(int)$customer_number,
(new \objects\users_o())->getCustomerName((int)$customer_number) ?? 'Unknown Customer',
(new \objects\users_o())->getCustomerName((int)$customer_number) ?? (new \objects\users_o())->select((int)$customer_number)->display_name->value(),
[],
true
true,
);
}
// Add the fixed pricing to the customer object
$fixed_pricing[count($fixed_pricing) - 1]['meta']['fixed_pricing'] = self::getObjectFromArray(
$tmp_fixed_pricing,
function ($item) use ($customer_number) {
return $item['customer_number'] === $customer_number;
}
);
}
return $fixed_pricing;
}
/**
* Get an object from an array based on a callback function.
*
* @param array $array The array to search in.
* @param callable $callback The callback function to use for searching.
* @return mixed|null The found object or null if not found.
*/
private static function getObjectFromArray(array $array, callable $callback): mixed
{
// Iterate through the array and apply the callback function to each element
foreach ( $array as $item ) {
// If the callback returns true, return the item
if ($callback($item)) {
return $item;
}
}
// If no item matches the callback, return null
return null;
}
/**
* @throws Exception
*/