public Import GetPayments() { var import = new Import(); var reader = new StreamReader(stream); var account = new ImportedBankAccount { IBan = "test", AccountID=1, Bank=new Bank { BankID=1 } }; var i = 0; while (!reader.EndOfStream) { var line = reader.ReadLine(); if (i < 3) { i++; continue; } var values = line.Split(';'); try { var paymennt = new ImportedPayment { BankAccount = new BankAccount {IBan = values[8]}, ConstantSymbol = !string.IsNullOrWhiteSpace(values[19]) ? short.Parse(values[19]) : (short)0, VariableSymbol = !string.IsNullOrWhiteSpace(values[20]) ? long.Parse(values[20]) : 0, SpecificSymbol = !string.IsNullOrWhiteSpace(values[21]) ? long.Parse(values[21]): 0, TransactionAmount = new AmountInfo { Amount = Math.Abs(decimal.Parse(values[1])), Currency = values[2], Type = decimal.Parse(values[1]) > 0 ? AmountType.Credit : AmountType.Debit }, Description = $"{values[13]} {values[14]} {values[15]} {values[16]} {values[17]}", DatePosted = DateTime.Parse(values[3]), DateAvailable = DateTime.Parse(values[4]) }; account.Payments.Add(paymennt); } catch(Exception ex) { //todo: } } import.Account = account; return import; }
public ActionResult Upload() { string path = null; //HttpFileCollectionBase file; Import import = new Import(); try { //path = XMLHelpers.SaveFile(Request.Files, Server.MapPath("~/temp/")); var file = Request.Files[0]; var parser = new Parser.BaseParser.Parser(file.InputStream); using (var svc = new ParsingServiceClient()) { var data = parser.GetData(); import = svc.SaveData(data, 1); //todo: tahat aktualneho pouzivatela } } catch (Exception ex) { return View("Error", ex); } finally { if (path != null) System.IO.File.Delete(path); } Session["import"] = import; var bankImportedPayments = new BankAccountImportedPayments { Transactions = import.Account.Payments.ToPagedList(1, PAGE_SIZE), Pager = new Pager { CurrentPageIndex = 1, PageSize = PAGE_SIZE }, Account = new data.BankAccount { AccountID = import.Account.AccountID, Bank = new data.Bank { BankID = import.Account.Bank.BankID }, IBan = import.Account.IBan }, From = import.From, To = import.To }; bankImportedPayments.Pager.ItemsCount = bankImportedPayments.Transactions.Count; //bankImportedPayments.TransactionImport.Transactions = // bankImportedPayments.TransactionImport.Transactions.ToPagedList(page,PAGE_SIZE); return View("UploadDocument", bankImportedPayments); }
public static Import ObcectToModel(OFX report) { var bankTransactions = new Import() { Account = new ImportedBankAccount { AccountID = report.STMTRS.BANKACCTFROM.ACCTID, IBan = report.STMTRS.BANKACCTFROM.IBAN, Payments = new List<ImportedPayment>(), Bank = new Bank { BankID = report.STMTRS.BANKACCTFROM.BANKID } }, From = GetDate(report.STMTRS.BANKTRANLIST.DTSTART.ToString(CultureInfo.InvariantCulture)), To = GetDate(report.STMTRS.BANKTRANLIST.DTEND.ToString(CultureInfo.InvariantCulture)) }; foreach (var trasaction in report.STMTRS.BANKTRANLIST.STMTTRN) { bankTransactions.Account.Payments.Add(ObjectToModel(trasaction)); } return bankTransactions; }
public Import SaveData(Import bankPayments, int UserId) { Import ImportWithPocessedTransactions; try { using (var context = new SpendingReportEntities()) { //if (!bankPayments.Bank.BankID.HasValue) //{ // return -1; //} //entity.Bank bank = context.Banks.FirstOrDefault(t => t.BankCode == bankPayments.Bank.BankID); var currentUser = Helpers.UserHelpers.GetUserByID(UserId); entity.BankAccount SourceAccount; SourceAccount = currentUser.BankAccounts.FirstOrDefault(t => t.IBAN == bankPayments.Account.IBan); if (SourceAccount == null) { SourceAccount = new entity.BankAccount { IBAN = bankPayments.Account.IBan, AccountNumber = (long)bankPayments.Account.AccountID }; SourceAccount.Bank = context.Banks.FirstOrDefault(t => t.BankCode == bankPayments.Account.Bank.BankID) ?? new entity.Bank { BankCode = (short)bankPayments.Account.Bank.BankID, Name=String.Empty }; currentUser.BankAccounts.Add(SourceAccount); context.BankAccounts.Add(SourceAccount); } ImportWithPocessedTransactions = new Import { Account = new data.ImportedBankAccount { Bank = bankPayments.Account.Bank, AccountID = bankPayments.Account.AccountID, IBan = bankPayments.Account.IBan, Payments = new List<ImportedPayment>() },//bankPayments.Account, From = bankPayments.From, To = bankPayments.To }; //entity.Bank bank = context.Banks.FirstOrDefault(t => t.BankCode == bankPayments.Account.Bank.BankID); using (var dbTransaction = context.Database.BeginTransaction()) { foreach (Payment transaction in bankPayments.Account.Payments) { if (IsTransactionExist(context, transaction)) { //ImportedPayment tr = new ImportedPayment(false); //tr.VariableSymbol = transaction.VariableSymbol; //ImportWithPocessedTransactions.Account.Payments.Add(tr); ImportedPayment tr = transaction as ImportedPayment; tr.Imported = false; ImportWithPocessedTransactions.Account.Payments.Add(tr); continue; } entity.Entry newTransaction = new entity.Entry { ConstantSymbol = transaction.ConstantSymbol, VariableSymbol = transaction.VariableSymbol, SpecificSymbol = transaction.SpecificSymbol, Reference = transaction.Reference, DateAvailable = transaction.DateAvailable, DatePosted = transaction.DatePosted, Memo = transaction.Description, Name = transaction.TransactionName, DateAdded = DateTime.Now, AmountInfo = new entity.AmountInfo { Amount = Math.Abs(transaction.TransactionAmount.Amount), Currency = transaction.TransactionAmount.Currency }, //Bank = GetBank(context, transaction.BankName.Localise()) }; var account = context.BankAccounts.FirstOrDefault(t => t.IBAN == transaction.BankAccount.IBan); if (account != null) { newTransaction.DestinationAccountId = account.Id; } else { newTransaction.DestinationAccount = new entity.BankAccount { AccountNumber = (long?) transaction.BankAccount.AccountID, //BankCode = (short?) transaction.BankAccount.BankID, IBAN = transaction.BankAccount.IBan, Bank = (transaction?.BankAccount?.Bank?.BankID.HasValue != null && transaction.BankAccount.Bank.BankID.HasValue) ? GetBank(context, transaction.BankAccount.Bank.BankID.Value) : null, UserId = null }; } if (transaction.TransactionAmount.Type != AmountType.NotDefined) { newTransaction.AmountInfo.Type = transaction.TransactionAmount.Type == AmountType.Credit ? GetTransactionType(context, "Credit") : GetTransactionType(context, "Debit"); } else { newTransaction.AmountInfo.Type = transaction.TransactionAmount.Amount < 0 ? GetTransactionType(context, "Debit") : GetTransactionType(context, "Credit"); } SourceAccount.Entries.Add(newTransaction); ImportedPayment importedTr = transaction as ImportedPayment; //new ImportedPayment(true); importedTr.Imported = true; //importedTr.VariableSymbol = transaction.VariableSymbol; ImportWithPocessedTransactions.Account.Payments.Add(importedTr); //Payment tran = new Payment(); //tran.VariableSymbol = transaction.VariableSymbol; //ImportWithPocessedTransactions.Account.Payments.Add(tran); //break; context.SaveChanges(); } dbTransaction.Commit(); } } } catch (Exception e) { throw new Exception("Nastala chyba pri ukladani importu!", e); } return ImportWithPocessedTransactions; }