added option to import payments from csv file

This commit is contained in:
Simon Pocrnjič
2025-10-02 22:09:05 +02:00
parent 971a9e89d1
commit 12de0186cf
21 changed files with 2828 additions and 824 deletions
@@ -77,7 +77,13 @@ public function suggest(Request $request)
if (! is_array($cols)) {
$cols = [];
}
$entities = ImportEntity::all();
// Optional filter: only suggest for specific entity keys (e.g., template meta.entities)
$only = $request->input('only_entities');
$query = ImportEntity::query()->orderByRaw("(ui->>'order')::int nulls last");
if (is_array($only) && ! empty($only)) {
$query->whereIn('key', array_values(array_filter($only, fn ($v) => is_string($v) && $v !== '')));
}
$entities = $query->get();
$suggestions = [];
foreach ($cols as $col) {
$s = $this->suggestFor($col, $entities);
@@ -92,6 +98,8 @@ public function suggest(Request $request)
private function suggestFor(string $source, $entities): ?array
{
$s = trim(mb_strtolower($source));
$best = null;
$bestRank = PHP_INT_MAX;
foreach ($entities as $ent) {
$rules = (array) ($ent->rules ?? []);
foreach ($rules as $rule) {
@@ -101,15 +109,27 @@ private function suggestFor(string $source, $entities): ?array
continue;
}
if (@preg_match($pattern, $s)) {
return [
'entity' => $ent->key, // UI key (plural except person)
'field' => $field,
'canonical_root' => $ent->canonical_root,
];
// Rank preferences: payments over address for amount/date-like terms
$rank = (int) ($ent->ui['order'] ?? 999);
$isPayment = ($ent->canonical_root ?? null) === 'payment';
$isAddress = ($ent->canonical_root ?? null) === 'address';
if ($isPayment) {
$rank -= 10; // boost payments
} elseif ($isAddress && in_array($field, ['amount', 'payment_date', 'payment_nu', 'reference'])) {
$rank += 10; // demote addresses for these
}
if ($rank < $bestRank) {
$bestRank = $rank;
$best = [
'entity' => $ent->key,
'field' => $field,
'canonical_root' => $ent->canonical_root,
];
}
}
}
}
return null;
return $best;
}
}