Skip to content

Commit

Permalink
improved csv error handling and messages, introduces usage of signs f…
Browse files Browse the repository at this point in the history
…rom the csv-data ;)
  • Loading branch information
lukas-staab committed Jun 6, 2024
1 parent 85195fc commit a937429
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 5 deletions.
5 changes: 3 additions & 2 deletions app/Livewire/TransactionImportWire.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,11 +133,12 @@ public function parseCSV() : void
// normalize data
foreach ($lineArray as $key => $cell){
// tests
$moneyTest = Regex::match('/([0-9]+),([0-9]{1,2})/', $cell);
$moneyTest = Regex::match('/(\-?)([0-9]+),([0-9]{1,2})/', $cell);

// conversions
if($moneyTest->hasMatch()){
$lineArray[$key] = $moneyTest->group(1) . '.' . $moneyTest->group(2);
// group 1: sign, group 2: money before delimiter, group 3: cents after delimiter
$lineArray[$key] = $moneyTest->group(1) . $moneyTest->group(2) . '.' . $moneyTest->group(3);
}
}
return $lineArray;
Expand Down
8 changes: 6 additions & 2 deletions app/Rules/CsvTransactionImport/BalanceRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,17 @@ public function __construct(Collection $differences,Collection $balances,?string
public function validate(string $attribute, mixed $value, Closure $fail): void
{
try {
// if there is no inital balance (no prior transaction) then make sure4 the first csv entry as correct
// if there is no initial balance (no prior transaction) then make sure the first csv entry as correct
$currentBalance = $this->initalBalance ?? bcsub($this->balances[0], $this->differences[0],2);
foreach ($this->differences as $id => $currentValue){
$currentBalance = bcadd($currentBalance, $currentValue, 2);
$csvBalance = $this->balances->get($id);
if($currentBalance !== $csvBalance){
$fail(__('konto.csv-verify-balance-error'));
$fail(__('konto.csv-verify-balance-error', [
'error-in-row' => $id + 1,
'calc-saldo' => $currentBalance,
'csv-saldo' => $csvBalance,
]));
}
}
}catch (\ValueError $error){
Expand Down
2 changes: 1 addition & 1 deletion lang/de/konto.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,6 @@
'csv-verify-iban-error' => 'Validierungs-Fehler: Enthält ungültige IBANs',
'csv-verify-money-error' => 'Validierungs-Fehler: Enthält nicht-numerische Daten',
'csv-verify-balance-error-wrong-datatype' => 'Validierungs-Fehler: Falscher Datentyp',
'csv-verify-balance-error' => 'Validierungsfehler: Es besteht evtl. keine lückenlose Transaktionshistorie.'
'csv-verify-balance-error' => 'Validierungsfehler: Es besteht keine lückenlose Transaktionshistorie in Zeile :error-in-row der CSV. Berechnetes Saldo :calc-saldo ≠ CSV-Saldo :csv-saldo'

];

0 comments on commit a937429

Please sign in to comment.