private void btnExport_Click(object sender, EventArgs e) { try { if (this.foldBrowse.ShowDialog() == DialogResult.OK) { this.Cursor = Cursors.WaitCursor; //Grab data from grid and export. CashTransactionImportResult data = new CashTransactionImportResult(this.tbInput.Text); foreach (DataGridViewRow r in this.grdResults.Rows) { data.ValidTransactions.Add((CashTransactionDTO)r.Tag); } string outPathFile = this.ExportData(this.foldBrowse.SelectedPath.AddTrailingSlash(), data); MessageBox.Show("Data exported to: " + outPathFile, "Status", MessageBoxButtons.OK, MessageBoxIcon.Information); } } catch (Exception ex) { //This would be logged in a real scenario. MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } finally { this.Cursor = Cursors.Default; } }
private string ExportData(string path, CashTransactionImportResult data) { DataAccess.CurrencyRepository r = new DataAccess.CurrencyRepository(); DataAccess.DelimitedFileBasicValidationRepository v = new DataAccess.DelimitedFileBasicValidationRepository(); BusinessLogic.CashRegister.Worker w = new BusinessLogic.CashRegister.Worker(r, v); return(w.ExportData(path, "ProcessedData_" + DateTime.Now.ToString("yyyyMMdd_hhmmss") + "_" + data.InputFile, data)); }
private void btnProcess_Click(object sender, EventArgs e) { try { this.Cursor = Cursors.WaitCursor; this.grdResults.Rows.Clear(); this.btnExport.Enabled = false; List <string> errList = this.ValidateForm(); if (errList.Count != 0) { MessageBox.Show("Please correct the following errors:" + Environment.NewLine + Environment.NewLine + errList.ListToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } else { CashTransactionImportResult res = this.LoadTransactionData(this.tbInput.Text); //Add successful transactions to the grid. foreach (CashTransactionDTO t in res.ValidTransactions) { DataGridViewRow row = (DataGridViewRow)this.grdResults.RowTemplate.Clone(); row.CreateCells(this.grdResults, new object[] { t.Owed, t.Paid, t.Change, t.Change_Formatted_Verbose }); row.Tag = t; this.grdResults.Rows.Add(row); } if (res.ErrorMessages.Count != 0) { MessageBox.Show("Process complete with errors:" + Environment.NewLine + Environment.NewLine + res.ErrorMessages.ListToString(), "Status", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } else { MessageBox.Show("Process complete with no errors.", "Status", MessageBoxButtons.OK, MessageBoxIcon.Information); } if (res.ValidTransactions.Count != 0) { this.btnExport.Enabled = true; } } } catch (Exception ex) { //This would be logged in a real scenario. MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } finally { this.Cursor = Cursors.Default; } }
/// <summary> /// Export transaction results to flat file. /// </summary> /// <param name="path">Path to export to.</param> /// <param name="fileName">File name to create.</param> /// <param name="obj">Object containing transaction results.</param> /// <returns>Path + File created.</returns> public string ExportData(string path, string fileName, CashTransactionImportResult obj) { using (StreamWriter sw = new StreamWriter(path + fileName)) { foreach (var t in obj.ValidTransactions) { sw.WriteLine(t.Change_Formatted_Verbose); } } return(path + fileName); }
/// <summary> /// Import a transaction file. /// </summary> /// <param name="pathFile">Path and file name to import.</param> /// <param name="randomizeChangeWhenDivisibleBy">Null or a number.</param> /// <returns></returns> public CashTransactionImportResult LoadTransactionData(string pathFile, int?randomizeChangeWhenDivisibleBy) { CashTransactionImportResult res = new CashTransactionImportResult(pathFile); List <Currency> allCurrency = this._currencyRepository.LoadAll_SortedByValueDescending(); Currency dollar = allCurrency.Where(x => x.Value == 1).FirstOrDefault(); List <Currency> coins = allCurrency.Where(x => x.Value != 1).ToList(); List <DelimitedFileValidationRule> valRules = this._delimitedFileValidationRepository.LoadAll_ByInputFileType("CashTransaction"); //Process file string line = ""; int lineNum = 0; using (StreamReader sr = new StreamReader(pathFile)) { while ((line = sr.ReadLine()) != null) { lineNum++; if (!String.IsNullOrEmpty(line)) { string[] rawValues = line.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries); List <string> lineErrors = this.ValidateRawTransaction(rawValues, lineNum, valRules); if (lineErrors.Count == 0) { decimal owed = Convert.ToDecimal(rawValues[0]); decimal paid = Convert.ToDecimal(rawValues[1]); decimal changeDue = paid - owed; if (changeDue < 0) { res.ErrorMessages.Add("Line: " + lineNum.ToString() + ". The change owed is less than zero."); } else { string changeVerbose = this.ConvertChange_ToVerbose(owed, changeDue, randomizeChangeWhenDivisibleBy, allCurrency, dollar, coins); res.ValidTransactions.Add(new CashTransactionDTO(owed, paid, changeDue, changeVerbose)); } } else { res.ErrorMessages.AddRange(lineErrors); } Array.Clear(rawValues, 0, rawValues.Length); } } } return(res); }