private void butImport_Click(object sender, EventArgs e) { Cursor = Cursors.WaitCursor; OpenFileDialog Dlg = new OpenFileDialog(); if (Directory.Exists(@"C:\X-Charge\")) { Dlg.InitialDirectory = @"C:\X-Charge\"; } else if (Directory.Exists(@"C:\")) { Dlg.InitialDirectory = @"C:\"; } if (Dlg.ShowDialog() != DialogResult.OK) { Cursor = Cursors.Default; return; } if (!File.Exists(Dlg.FileName)) { Cursor = Cursors.Default; MsgBox.Show(this, "File not found"); return; } XChargeTransaction trans = new XChargeTransaction(); string[] fields; XChargeTransaction transCheck; using (StreamReader sr = new StreamReader(Dlg.FileName)) { Cursor = Cursors.WaitCursor; string line = sr.ReadLine(); while (line != null) { fields = line.Split(new string[1] { "," }, StringSplitOptions.None); if (fields.Length < 16) { continue; } trans.TransType = fields[0]; fields[1] = fields[1].Replace("$", ""); if (fields[1].Contains("(")) { fields[1] = fields[1].TrimStart('('); fields[1] = fields[1].TrimEnd(')'); fields[1] = fields[1].Insert(0, "-"); } trans.Amount = PIn.Double(fields[1]); trans.CCEntry = fields[2]; trans.PatNum = PIn.Long(fields[3].Substring(3)); //remove "PAT" from the beginning of the string trans.Result = fields[4]; trans.ClerkID = fields[5]; trans.ResultCode = fields[7]; trans.Expiration = fields[8]; trans.CCType = fields[9]; trans.CreditCardNum = fields[10]; trans.BatchNum = fields[11]; trans.ItemNum = fields[12]; trans.ApprCode = fields[13]; trans.TransactionDateTime = PIn.Date(fields[14]).AddHours(PIn.Double(fields[15].Substring(0, 2))).AddMinutes(PIn.Double(fields[15].Substring(3, 2))); if (trans.BatchNum == "" || trans.ItemNum == "") { line = sr.ReadLine(); continue; } transCheck = XChargeTransactions.CheckByBatchItem(trans.BatchNum, trans.ItemNum); if (transCheck == trans) { XChargeTransactions.Delete(transCheck.XChargeTransactionNum); XChargeTransactions.Insert(trans); } else { XChargeTransactions.Insert(trans); } line = sr.ReadLine(); } } Cursor = Cursors.Default; MsgBox.Show(this, "Done."); }
private void butImport_Click(object sender, EventArgs e) { Cursor = Cursors.WaitCursor; OpenFileDialog Dlg = new OpenFileDialog(); if (Directory.Exists(@"C:\X-Charge\")) { Dlg.InitialDirectory = @"C:\X-Charge\"; } else if (Directory.Exists(@"C:\")) { Dlg.InitialDirectory = @"C:\"; } if (Dlg.ShowDialog() != DialogResult.OK) { Cursor = Cursors.Default; return; } if (!File.Exists(Dlg.FileName)) { Cursor = Cursors.Default; MsgBox.Show(this, "File not found"); return; } XChargeTransaction trans = new XChargeTransaction(); string[] fields; XChargeTransaction transCheck; using (StreamReader sr = new StreamReader(Dlg.FileName)) { Cursor = Cursors.WaitCursor; string line = sr.ReadLine(); while (line != null) { fields = line.Split(new string[1] { "," }, StringSplitOptions.None); if (fields.Length < 16 || fields.All(x => x == "")) { //This occurs when the XCharge transaction is blank. For example, ,,,,,,,,,,,,,,,, which is what is outputted by XCharge //when no transactions occurred for a day. line = sr.ReadLine(); continue; } trans.TransType = fields[0]; fields[1] = fields[1].Replace("$", ""); if (fields[1].Contains("(")) { fields[1] = fields[1].TrimStart('('); fields[1] = fields[1].TrimEnd(')'); fields[1] = fields[1].Insert(0, "-"); } trans.Amount = PIn.Double(fields[1]); trans.CCEntry = fields[2]; trans.PatNum = 0; if (!string.IsNullOrWhiteSpace(fields[3])) { trans.PatNum = PIn.Long(fields[3].Substring(3)); //remove "PAT" from the beginning of the string } trans.Result = fields[4]; trans.ClerkID = fields[5]; trans.ResultCode = fields[7]; trans.Expiration = fields[8]; trans.CCType = fields[9]; trans.CreditCardNum = fields[10]; trans.BatchNum = fields[11]; trans.ItemNum = fields[12]; trans.ApprCode = fields[13]; trans.TransactionDateTime = PIn.Date(fields[14]) .AddHours(PIn.Double(fields[15].Substring(0, 2))) .AddMinutes(PIn.Double(fields[15].Substring(3, 2))) .AddSeconds(PIn.Double(fields[15].Substring(6, 2))); transCheck = XChargeTransactions.GetOneMatch(trans.BatchNum, trans.ItemNum, trans.PatNum, trans.TransactionDateTime, trans.TransType); if (transCheck != null && trans.Result != "AP DUPE" && transCheck.Amount.IsEqual(trans.Amount) //Before 17.1.19, we did not store the seconds value of the TransactionDateTime, so we consider it a match if Second==0. && (transCheck.TransactionDateTime == trans.TransactionDateTime || transCheck.TransactionDateTime.Second == 0)) { XChargeTransactions.Delete(transCheck.XChargeTransactionNum); XChargeTransactions.Insert(trans); } else { XChargeTransactions.Insert(trans); } line = sr.ReadLine(); } } Cursor = Cursors.Default; MsgBox.Show(this, "Done."); }