Skip to content

Commit

Permalink
Merge pull request #137 from enrique-lozano/hot-fixes
Browse files Browse the repository at this point in the history
Errors during the CSV import
  • Loading branch information
enrique-lozano authored Mar 18, 2024
2 parents b0deef1 + 8b822bc commit 2e5255d
Showing 1 changed file with 40 additions and 20 deletions.
60 changes: 40 additions & 20 deletions lib/app/settings/import_csv.dart
Original file line number Diff line number Diff line change
Expand Up @@ -154,11 +154,16 @@ class _ImportCSVPageState extends State<ImportCSVPage> {
}

Future<void> addTransactions() async {
if (accountColumn == null || amountColumn == null) {
throw Exception('Account and amount columns can not be null');
final snackbarDisplayer = ScaffoldMessenger.of(context).showSnackBar;

if (amountColumn == null) {
snackbarDisplayer(
const SnackBar(content: Text('Amount column can not be null')),
);

return;
}

final snackbarDisplayer = ScaffoldMessenger.of(context).showSnackBar;
final loadingOverlay = LoadingOverlay.of(context);

onSuccess() {
Expand All @@ -178,11 +183,13 @@ class _ImportCSVPageState extends State<ImportCSVPage> {
final csvRows = csvData!.slice(1).toList();
final db = AppDB.instance;

const unknownAccountName = 'Account imported';

for (final row in csvRows) {
final account = await (db.select(db.accounts)
..where((tbl) => tbl.name
.lower()
.isValue(row[accountColumn!].toString().toLowerCase())))
..where((tbl) => tbl.name.lower().isValue(accountColumn == null
? unknownAccountName.toLowerCase()
: row[accountColumn!].toString().toLowerCase())))
.getSingleOrNull();

final accountID = account != null
Expand All @@ -194,7 +201,9 @@ class _ImportCSVPageState extends State<ImportCSVPage> {
if (account == null && defaultAccount == null) {
await AccountService.instance.insertAccount(AccountInDB(
id: accountID,
name: row[accountColumn!].toString(),
name: accountColumn == null
? unknownAccountName
: row[accountColumn!].toString(),
iniValue: 0,
displayOrder: 10,
date: DateTime.now(),
Expand All @@ -206,19 +215,29 @@ class _ImportCSVPageState extends State<ImportCSVPage> {
.code));
}

final categoryToFind =
row[categoryColumn!].toString().toLowerCase().trim();

final String categoryID = (await CategoryService.instance
.getCategories(
predicate: (catTable, pCatTable) =>
catTable.name.lower().trim().isValue(categoryToFind) |
pCatTable.name.lower().trim().isValue(categoryToFind),
)
.first)
.firstOrNull
?.id ??
defaultCategory!.id;
final categoryToFind = categoryColumn == null
? null
: row[categoryColumn!].toString().toLowerCase().trim();

final String categoryID = categoryToFind == null
? defaultCategory!.id
: (await CategoryService.instance
.getCategories(
limit: 1,
predicate: (catTable, pCatTable) =>
catTable.name
.lower()
.trim()
.isValue(categoryToFind) |
pCatTable.name
.lower()
.trim()
.isValue(categoryToFind),
)
.first)
.firstOrNull
?.id ??
defaultCategory!.id;

await TransactionService.instance.insertTransaction(TransactionInDB(
id: const Uuid().v4(),
Expand All @@ -242,6 +261,7 @@ class _ImportCSVPageState extends State<ImportCSVPage> {
loadingOverlay.hide();
onSuccess();
} catch (e) {
print(e);
loadingOverlay.hide();
snackbarDisplayer(SnackBar(content: Text(e.toString())));
}
Expand Down

0 comments on commit 2e5255d

Please sign in to comment.