diff --git a/Core/Core/BusinessLogic/ImportCSVLogic/DocumentLayout.cs b/Core/Core/BusinessLogic/ImportCSVLogic/DocumentLayout.cs index b4a31e9e177a27262e2e488395ab119328f65029..721bf12bed7b55caa0a8a6434e2c0275377f1dda 100644 --- a/Core/Core/BusinessLogic/ImportCSVLogic/DocumentLayout.cs +++ b/Core/Core/BusinessLogic/ImportCSVLogic/DocumentLayout.cs @@ -19,11 +19,11 @@ namespace Core.BusinessLogic.ImportCSVLogic public string Currency { get; set; } [Name("zĹŻstatek")] public string SenderAccountBallance { get; set; } - [Name("ÄŤĂslo účtu protiúčtu")] + [Name(new string[] { "ÄŤĂslo účtu protiúčtu", "protiúčet" })] public string RecipientAccount { get; set; } - [Name("kĂłd banky protiúčtu")] + [Name(new string[] { "kĂłd banky protiúčtu","bankovnĂ kĂłd protiúčtu" })] public string RecipientBankCode { get; set; } - [Name("název účtu protiúčtu")] + [Name(new string[]{"název účtu protiúčtu", "název protiúčtu"})] public string RecipientAccountName { get; set; } [Name("konstantnĂ symbol")] public string ConstantSymbol { get; set; } @@ -31,9 +31,9 @@ namespace Core.BusinessLogic.ImportCSVLogic public string VariableSymbol { get; set; } [Name("specifickĂ˝ symbol")] public string SpecificSymbol { get; set; } - [Name("oznaÄŤenĂ operace")] + [Name(new string[] { "oznaÄŤenĂ operace","typ transakce" })] public string TransactionType { get; set; } - [Name("ID transakce")] + [Name("id transakce")] public string ID { get; set; } [Name("poznámka")] public string Note { get; set; } diff --git a/Core/Core/BusinessLogic/ImportCSVLogic/ModelImporter.cs b/Core/Core/BusinessLogic/ImportCSVLogic/ModelImporter.cs index eb7312528c3ae44d4758b3e2f2c74176235efc2f..d2fd6311bb12babd87af5e37be2308c2f7331e2e 100644 --- a/Core/Core/BusinessLogic/ImportCSVLogic/ModelImporter.cs +++ b/Core/Core/BusinessLogic/ImportCSVLogic/ModelImporter.cs @@ -27,6 +27,7 @@ namespace Core.BusinessLogic.ImportCSVLogic csv.Configuration.BadDataFound = null; csv.Configuration.HeaderValidated = null; csv.Configuration.Delimiter = ","; + csv.Configuration.PrepareHeaderForMatch = (string header, int index) => header.ToLower(); csv.Configuration.MissingFieldFound = (headerNames, index, context) => { Console.WriteLine($"Field with names ['{string.Join("', '", headerNames)}'] at index '{index}' was not found."); @@ -49,35 +50,48 @@ namespace Core.BusinessLogic.ImportCSVLogic if (user.Files.Any(f => f.Name == fileName)) throw new ArgumentException($"Filename {fileName} already exists"); + if (!records.Any()) + throw new ArgumentException("file not supported"); + Models.File file = new Models.File() { Name = fileName, Transactions = new List<Transaction>() }; + string DefaultUserAccount = userID.ToString() + "/857368"; //857368 = UID in ASCII var transactionTypesList = model.TransactionTypes.ToList(); //DB performance var constantSymbolsList = model.ConstantSymbols.ToList(); var accountList = model.Accounts.ToList(); - foreach (var record in records) + try { - Transaction transaction = new Transaction(record); - - if (!string.IsNullOrEmpty(record.TransactionType)) - transaction.TransactionType = transactionTypesList.FirstOrDefault(t => record.TransactionType.StartsWith(t.Description)); - - transaction.ConstantSymbol = ParseConstantSymbol(record.ConstantSymbol, constantSymbolsList); - - transaction.SenderAccount = ParseAccount(record.Account, accountList); - if (transaction.SenderAccount == null) + foreach (var record in records) { - Console.WriteLine($"Error parsing mandatory attribut SenderAccount: {record.Account}"); - continue; - } + try + { + Transaction transaction = new Transaction(record); - transaction.RecipientAccount = ParseAccount(record.RecipientAccount, accountList); + if (!string.IsNullOrEmpty(record.TransactionType)) + transaction.TransactionType = transactionTypesList.FirstOrDefault(t => record.TransactionType.StartsWith(t.Description)); + transaction.ConstantSymbol = ParseConstantSymbol(record.ConstantSymbol, constantSymbolsList); - file.Transactions.Add(transaction); + transaction.SenderAccount = ParseAccount(string.IsNullOrEmpty(record.Account) ? DefaultUserAccount : record.Account, accountList); + transaction.RecipientAccount = ParseAccount(record.RecipientAccount, accountList); + + file.Transactions.Add(transaction); + } + catch (Exception e) + { + Console.WriteLine($"Error parsing import data: " + e.ToString()); + continue; + } + } + user.Files.Add(file); + model.SaveChanges(); + } + catch (Exception e) + { + Console.WriteLine("Import exception:" + e.ToString()); + throw; } - user.Files.Add(file); - model.SaveChanges(); } private Account ParseAccount(string accountToParse, List<Account> accountList) diff --git a/Core/Core/Models/Account.cs b/Core/Core/Models/Account.cs index 04b04059a8679755a21d68226419aa868676e81f..14010c3a3a9ae8571acb5b42c6bcf0cb20443ed1 100644 --- a/Core/Core/Models/Account.cs +++ b/Core/Core/Models/Account.cs @@ -29,6 +29,9 @@ namespace Core.Models public static Account CreateAccount(string accountNumber) { + if (string.IsNullOrWhiteSpace(accountNumber)) + return null; + string[] splitedString = accountNumber.Split(new char[] { '-', '/' }); if (!int.TryParse(splitedString.Last(), out int parsedBankCode)) diff --git a/Core/Core/Models/Transaction.cs b/Core/Core/Models/Transaction.cs index d109d6cc6c6cebfe051eb1ed34b53070310fbc8c..f48fa139b2a0b32ea68e6ccd384d66ce30da975d 100644 --- a/Core/Core/Models/Transaction.cs +++ b/Core/Core/Models/Transaction.cs @@ -65,7 +65,8 @@ namespace Core.Models { this.ProvidedID = document.ID; this.Date = DateTime.Parse(document.Date); - this.Amount = decimal.Parse(document.Amount, NumberStyles.AllowLeadingSign | NumberStyles.AllowDecimalPoint, new CultureInfo("en-US")); + //this.Amount = decimal.Parse(document.Amount, NumberStyles.AllowLeadingSign | NumberStyles.AllowDecimalPoint, new CultureInfo("en-US")); + this.Amount = decimal.Parse(document.Amount, NumberStyles.AllowLeadingSign | NumberStyles.AllowDecimalPoint | NumberStyles.AllowThousands, new CultureInfo("cs-CZ")); this.Note = document.Note; if (long.TryParse(document.VariableSymbol, out long parseValue))