Bestellliste im ERP-Home ergänzen
This commit is contained in:
@@ -175,3 +175,148 @@ function insert_sales_order_line(PDO $pdo, array $fields): int
|
||||
|
||||
return (int) $id;
|
||||
}
|
||||
|
||||
function normalize_sales_order_sort_column(string $sortColumn): string
|
||||
{
|
||||
$allowed = [
|
||||
'order_date',
|
||||
'external_ref',
|
||||
'last_name',
|
||||
'first_name',
|
||||
'total_amount',
|
||||
];
|
||||
|
||||
return in_array($sortColumn, $allowed, true) ? $sortColumn : 'order_date';
|
||||
}
|
||||
|
||||
function normalize_sales_order_sort_direction(string $direction, string $sortColumn): string
|
||||
{
|
||||
$direction = strtoupper(trim($direction));
|
||||
if ($direction !== 'ASC' && $direction !== 'DESC') {
|
||||
return $sortColumn === 'order_date' ? 'DESC' : 'ASC';
|
||||
}
|
||||
|
||||
return $direction;
|
||||
}
|
||||
|
||||
function escape_sales_order_search_term(string $searchTerm): string
|
||||
{
|
||||
return strtr($searchTerm, [
|
||||
'\\' => '\\\\',
|
||||
'%' => '\\%',
|
||||
'_' => '\\_',
|
||||
]);
|
||||
}
|
||||
|
||||
function get_sales_order_overview(PDO $pdo, array $filters = []): array
|
||||
{
|
||||
$searchTerm = trim((string) ($filters['search'] ?? ''));
|
||||
$sortColumn = normalize_sales_order_sort_column((string) ($filters['sort_column'] ?? 'order_date'));
|
||||
$sortDirection = normalize_sales_order_sort_direction((string) ($filters['sort_direction'] ?? ''), $sortColumn);
|
||||
$limit = max(1, (int) ($filters['limit'] ?? 20));
|
||||
$pageSize = max(1, (int) ($filters['page_size'] ?? 20));
|
||||
|
||||
$filterSql = '';
|
||||
$params = [];
|
||||
if ($searchTerm !== '') {
|
||||
$filterSql = <<<'SQL'
|
||||
WHERE (
|
||||
so.external_ref ILIKE :search_term ESCAPE '\'
|
||||
OR COALESCE(ad.last_name, '') ILIKE :search_term ESCAPE '\'
|
||||
OR COALESCE(ad.first_name, '') ILIKE :search_term ESCAPE '\'
|
||||
)
|
||||
SQL;
|
||||
$params[':search_term'] = '%' . escape_sales_order_search_term($searchTerm) . '%';
|
||||
}
|
||||
|
||||
$sortExpressions = [
|
||||
'order_date' => 'so.order_date',
|
||||
'external_ref' => 'so.external_ref',
|
||||
'last_name' => 'COALESCE(ad.last_name, \'\')',
|
||||
'first_name' => 'COALESCE(ad.first_name, \'\')',
|
||||
'total_amount' => 'COALESCE(so.total_amount, 0)',
|
||||
];
|
||||
$sortExpression = $sortExpressions[$sortColumn] ?? 'so.order_date';
|
||||
|
||||
$baseFromSql = <<<'SQL'
|
||||
FROM sales_order so
|
||||
LEFT JOIN LATERAL (
|
||||
SELECT
|
||||
a.first_name,
|
||||
a.last_name,
|
||||
a.street,
|
||||
a.house_number,
|
||||
a.zip,
|
||||
a.city,
|
||||
a.country_name
|
||||
FROM address a
|
||||
WHERE a.party_id = so.party_id
|
||||
AND a.type = 'shipping'
|
||||
ORDER BY a.id DESC
|
||||
LIMIT 1
|
||||
) ad ON TRUE
|
||||
SQL;
|
||||
|
||||
$countStmt = $pdo->prepare(
|
||||
'SELECT COUNT(*) ' . $baseFromSql . "\n" . $filterSql
|
||||
);
|
||||
foreach ($params as $key => $value) {
|
||||
$countStmt->bindValue($key, $value, PDO::PARAM_STR);
|
||||
}
|
||||
$countStmt->execute();
|
||||
$totalCount = (int) $countStmt->fetchColumn();
|
||||
|
||||
$listSql = <<<'SQL'
|
||||
SELECT
|
||||
so.id,
|
||||
so.external_ref,
|
||||
so.order_date,
|
||||
so.total_amount,
|
||||
COALESCE(ad.first_name, '') AS first_name,
|
||||
COALESCE(ad.last_name, '') AS last_name,
|
||||
COALESCE(ad.street, '') AS street,
|
||||
COALESCE(ad.house_number, '') AS house_number,
|
||||
COALESCE(ad.zip, '') AS zip,
|
||||
COALESCE(ad.city, '') AS city,
|
||||
COALESCE(ad.country_name, '') AS country_name
|
||||
SQL;
|
||||
$listSql .= "\n" . $baseFromSql . "\n" . $filterSql;
|
||||
$listSql .= "\nORDER BY {$sortExpression} {$sortDirection}, so.id {$sortDirection}";
|
||||
$listSql .= "\nLIMIT :limit";
|
||||
|
||||
$listStmt = $pdo->prepare($listSql);
|
||||
foreach ($params as $key => $value) {
|
||||
$listStmt->bindValue($key, $value, PDO::PARAM_STR);
|
||||
}
|
||||
$listStmt->bindValue(':limit', $limit, PDO::PARAM_INT);
|
||||
$listStmt->execute();
|
||||
|
||||
$rows = [];
|
||||
foreach ($listStmt->fetchAll() as $row) {
|
||||
$rows[] = [
|
||||
'id' => (int) $row['id'],
|
||||
'external_ref' => (string) $row['external_ref'],
|
||||
'order_date' => (string) $row['order_date'],
|
||||
'total_amount' => $row['total_amount'] !== null ? (float) $row['total_amount'] : null,
|
||||
'first_name' => (string) $row['first_name'],
|
||||
'last_name' => (string) $row['last_name'],
|
||||
'street' => (string) $row['street'],
|
||||
'house_number' => (string) $row['house_number'],
|
||||
'zip' => (string) $row['zip'],
|
||||
'city' => (string) $row['city'],
|
||||
'country_name' => (string) $row['country_name'],
|
||||
];
|
||||
}
|
||||
|
||||
return [
|
||||
'rows' => $rows,
|
||||
'search' => $searchTerm,
|
||||
'sort_column' => $sortColumn,
|
||||
'sort_direction' => $sortDirection,
|
||||
'limit' => $limit,
|
||||
'page_size' => $pageSize,
|
||||
'total_count' => $totalCount,
|
||||
'has_more' => $totalCount > $limit,
|
||||
'next_limit' => min($totalCount, $limit + $pageSize),
|
||||
];
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user