/// <summary> /// Shows import dialogue and gets data from CSV file. /// </summary> /// <remarks>Created: Werner Scheffer 27/09/2013</remarks> private void btnImportStatement_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e) { try { BulkPaymentImportDialogue dialogue = new BulkPaymentImportDialogue(bulkPaymentEntries, paymentAccounts.Select(l => l.AccountId).ToList()); if (dialogue.ShowDialog(this) == System.Windows.Forms.DialogResult.OK) { ImportFile(dialogue); } } catch (Exception ex) { if (CDS.Shared.Exception.UserInterfaceExceptionHandler.HandleException(ref ex)) { throw ex; } } }
/// <summary> /// Import the CSV file data from the Dialogue into the grid /// </summary> /// <param name="dialogue"></param> private void ImportFile(BulkPaymentImportDialogue dialogue) { try { using (new CDS.Client.Desktop.Essential.UTL.WaitCursor()) { ddlAccount.EditValue = dialogue.AccountId; if (dialogue.SkippedAccounts.Count() != 0) { Essential.BaseAlert.ShowAlert("Invalid accounts", String.Format("The following accounts could not be found \"{0}\".", String.Join(",", dialogue.SkippedAccounts)) , Essential.BaseAlert.Buttons.Ok, Essential.BaseAlert.Icons.Information); } //Cant find any of the accounts if (dialogue.Data.Where(n => n.AccountId != null).Select(l => l.AccountId.Value).Distinct().ToList().Count() == 0) { return; } paymentItems = GetPaymentItems(dialogue.Data.Where(n => n.AccountId != null).Select(l => l.AccountId.Value).Distinct().ToList()); Parallel.ForEach(paymentItems.AsEnumerable(), row => //foreach (DataRow row in paymentItems.AsEnumerable()) { int agingId = -1; DateTime entryDate = Convert.ToDateTime(bulkPaymentEntries.Where(n => n.AccountId.Equals(row["AccountId"])).Select(n => n.Date).FirstOrDefault()); entryDate = entryDate.AddDays(-(entryDate.Day - 1)); DateTime date = Convert.ToDateTime(row["Date"]); date = date.AddDays(-(date.Day - 1)); int monthDiff = (int)entryDate.Subtract(date).Days / (365 / 12); if (monthDiff >= 5) { agingId = 5; } else { agingId = monthDiff + 1; } bulkPaymentEntries.Where(n => n.AccountId.Equals(row["AccountId"])).FirstOrDefault().BulkPaymentItems.Add( new BulkPaymentItem() { Type = Convert.ToString(row["Type"]), Title = Convert.ToString(row["Title"]), Reference = Convert.ToString(row["Reference"]), Description = Convert.ToString(row["Description"]), Date = Convert.ToDateTime(row["Date"]), Balance = Convert.ToDecimal(row["Balance"]), PeriodId = Convert.ToInt64(row["PeriodId"]), AgingId = Convert.ToByte(agingId), //Convert.ToInt16(row["AgingId"]), TrackNumber = Convert.ToString(row["TrackNumber"]), LineId = Convert.ToInt64(row["LineId"] != DBNull.Value ? row["LineId"] : -1), HeaderId = Convert.ToInt64(row["HeaderId"] != DBNull.Value ? row["HeaderId"] : -1) } ); }); foreach (var mainLine in bulkPaymentEntries) { //Skip OI Accounts if (mainLine.BulkPaymentItems.Count == 0) { continue; } if (mainLine.BulkPaymentItems.Select(n => n.Type).FirstOrDefault().Equals("OI")) { continue; } //Auto assigns the entry to the Correct Aging foreach (var subLine in mainLine.BulkPaymentItems) { if (subLine.AgingId.Equals(mainLine.AgingId)) { subLine.Checked = true; } } } grdEntries.RefreshDataSource(); grdEntries.Focus(); } } catch (Exception ex) { if (CDS.Shared.Exception.UserInterfaceExceptionHandler.HandleException(ref ex)) { throw ex; } } }