/// <summary> /// Loops over all the files we downloaded from the SFTP. Goes through the data line by line. /// Looks for a line beginning with "8" as that signifies a valid file structure /// </summary> private static void processFiles() { appendToBody(String.Format("Wells Fargo files to be processed: {0}", sftpFilesToProcess.Count())); //int count = 0; //Loop over each file. We need to determine if it is a morning file and in the correct format foreach (var file in sftpFilesToProcess) { //create a new payment record list for each file ... batches = new List<batch>(); bool validFile = false; bool multiCheck = false; try { using (StreamReader sr = new StreamReader(file)) { //appendToBody(String.Format("Parsing file {0}", file.Name)); string line = ""; string recordType = ""; currentPaymentRecord = null; currentBatch = new batch(); currentInvoice = null; invoiceValue = 0; while ((line = sr.ReadLine()) != null && line.Length > 15) { line = line.Replace(" ", ""); recordType = line.Substring(0, 2); Console.WriteLine(line); switch (recordType) { //File contains 1 FH case "FH": //This is a header line. We can pull the 10 digits and the date from this line... parseHeader(line); break; //File can contain multiple BH case "BH": //create a new Batch currentBatch = new batch(); break; //Each BH can contain multiple PR case "PR": //In order for the file to be valid we have to have at least 1 payment record... validFile = true; currentBatch.numChecks++; //If there is no open paymentRecord. Create a new one if (!multiCheck){ currentPaymentRecord = new paymentRecord(); currentBatch.paymentRecords.Add(currentPaymentRecord); } //Every PR wil create a new "check". currentCheck = new check(); currentCheck.checkAmount = parsePR(line); currentBatch.checkTotal += currentCheck.checkAmount; currentPaymentRecord.checks.Add(currentCheck); currentPaymentRecord.total += currentCheck.checkAmount; break; //Each PR will contain a SP case "SP": //Need to parse the SP and see if the flag is set for additional checks multiCheck = parseSP(line); break; //Each PR can contain multiple IV case "IV": currentBatch.numInvoices++; currentInvoice = new invoice(); invoiceValue = parseIV(line); currentPaymentRecord.invoices.Add(currentInvoice); break; case "SI": //Supplemental Invoice //Parse the line and determine what type of data it is... if (currentInvoice != null) { currentInvoice.invoiceDate = parseSI(line); } else { Console.WriteLine("Current invoice is null while reading in an SI...Should Never happen!!!! problem!"); Console.ReadKey(); } //push into the invoice break; case "BT": //Trailer Record batches.Add(currentBatch); break; default: //Do nothing //Console.WriteLine("unrecognized line header: {0}", line); break; } } //close the file sr.Close(); } if (validFile) { string fileName = createOutputFile(file); attachments.Add(new FileInfo(fileName)); appendToBody(""); //upload file ... //ftpUploadFile(fileName, fileName); } } catch (Exception e) { Console.WriteLine("Exception processing file...{0}", e); appendToBody(String.Format("Error while processing {0}", file)); } } //appendToBody(String.Format("Processed {0} valid files", count)); }
/// <summary> /// This is a debug version of Process files that allows me to pass in the name of an individual file to be processed /// </summary> /// <param name="file">The name of the file to be processed. Starts looking in CWD. If you want a different path, send in the entire location</param> private static void processFiles(string file) { //create a new payment record list for each file ... paymentRecords = new List<paymentRecord>(); //appendToBody(String.Format("Downloaded {0} files from Wells Fargo", sftpFiles.Count())); int count = 0; try { using (StreamReader sr = new StreamReader(file)) { //appendToBody(String.Format("Parsing file {0}", file)); string line = ""; string recordType = ""; currentPaymentRecord = new paymentRecord(); currentInvoice = null; previousInvoice = null; currentSI = null; invoiceValue = 0; while ((line = sr.ReadLine()) != null && line.Length > 15) { line = line.Replace(" ", ""); recordType = line.Substring(0, 2); Console.WriteLine(line); switch (recordType) { case "FH": //This is a header line. We can pull the 10 digits and the date from this line... parseHeader(line); break; case "PR": //We need to make sure we catch the final payment in the PR before we start another if (currentInvoice != null) { //If we have a previous invoice if (previousInvoice != null) { //compare the currentInvocie Id vs previousInvoiceID. If they don't match. Push the previousInvoice if (currentInvoice.SupplimentalInvoices.transactionID != previousInvoice.SupplimentalInvoices.transactionID) { if (previousInvoice.SupplimentalInvoices.transactionID != null) { currentPaymentRecord.invoices.Add(previousInvoice); } previousInvoice = null; } } if (currentInvoice.SupplimentalInvoices.transactionID != null) { currentPaymentRecord.invoices.Add(currentInvoice); } currentInvoice = null; } //This is a payment record... //create a new payment record currentPaymentRecord = new paymentRecord(); currentPaymentRecord.total = parsePR(line); paymentRecords.Add(currentPaymentRecord); break; case "IV": //if there is a currentInvoice if (currentInvoice != null) { //If we have a previous invoice if (previousInvoice != null) { //compare the currentInvocie Id vs previousInvoiceID. If they don't match. Push the previousInvoice //If transaction Ids are null. that means there were no SI fields below it ... if (previousInvoice.SupplimentalInvoices.transactionID != null && (currentInvoice.SupplimentalInvoices.transactionID != previousInvoice.SupplimentalInvoices.transactionID)) { currentPaymentRecord.invoices.Add(previousInvoice); } } //regardless, currentInvoice becomes previous invoice previousInvoice = currentInvoice; } invoiceValue = parseIV(line); currentInvoice = new invoice(); //create a new SI currentSI = new supplimentalInvoice(); currentInvoice.SupplimentalInvoices = currentSI; break; case "SI": //Supplemental Invoice //Parse the line and determine what type of data it is... parseSI(line); //push into the invoice break; case "BT": //Trailer Record //If we have payment records. We need to do a final check to make sure we don't miss the last 2 records we read... //If the currentPaymentRecord does not have at least 2 records in it... //Case 1. //Case 2 //Case 3 //Case 1. There are 0 records in the paymentRecord. Need to add all valid if (currentPaymentRecord.invoices.Count == 0) { if (previousInvoice != null && previousInvoice.SupplimentalInvoices.transactionID != null) { currentPaymentRecord.invoices.Add(previousInvoice); } if (currentInvoice != null && currentInvoice.SupplimentalInvoices.transactionID != null) { currentPaymentRecord.invoices.Add(currentInvoice); } } //Case 2. There is 1 record in the paymentRecord...Need to add all valid else if (currentPaymentRecord.invoices.Count == 1) { if (previousInvoice != null && previousInvoice.SupplimentalInvoices.transactionID != null && previousInvoice.SupplimentalInvoices.transactionID != currentPaymentRecord.invoices[0].SupplimentalInvoices.transactionID) { currentPaymentRecord.invoices.Add(previousInvoice); } if (currentInvoice != null && currentInvoice.SupplimentalInvoices.transactionID != null && currentInvoice.SupplimentalInvoices.transactionID != currentPaymentRecord.invoices[0].SupplimentalInvoices.transactionID) { currentPaymentRecord.invoices.Add(currentInvoice); } } //Case 3. There are x records in the paymentRecord ... need to add all valid else { string prevID = currentPaymentRecord.invoices[currentPaymentRecord.invoices.Count - 2].SupplimentalInvoices.transactionID; string curID = currentPaymentRecord.invoices[currentPaymentRecord.invoices.Count - 1].SupplimentalInvoices.transactionID; if (previousInvoice.SupplimentalInvoices.transactionID != prevID && previousInvoice.SupplimentalInvoices.transactionID != curID) { currentPaymentRecord.invoices.Add(previousInvoice); } if (currentInvoice.SupplimentalInvoices.transactionID != prevID && currentInvoice.SupplimentalInvoices.transactionID != curID) { currentPaymentRecord.invoices.Add(currentInvoice); } } break; default: //Do nothing //Console.WriteLine("unrecognized line header: {0}", line); break; } } //close the file sr.Close(); } //add stuff to the body of the email //we can validate all checks add up here... //upload the file to the lockbox createOutputFile(file); appendToBody(""); } catch (Exception e) { Console.WriteLine("Exception processing file...{0}", e); appendToBody(String.Format("Error while processing {0}", file)); } //appendToBody(String.Format("Processed {0} valid files", count)); }
private static void EmailPaymentData(paymentRecord curPayment) { appendToBody(String.Format("There was {0} check(s) in this payment", curPayment.checks.Count)); for (int i = 0; i < curPayment.checks.Count; i++) { appendToBody(String.Format("Check# {0}: ${1}",curPayment.checks[i].checkID, curPayment.checks[i].checkAmount.ToString("0.00"))); } appendToBody(""); appendToBody(String.Format("There was {0} coupons in this payment", curPayment.invoices.Count)); for (int i = 0; i < curPayment.invoices.Count; i++){ bool appendSpecialStyle = false; totalCouponValueForAllFiles += curPayment.invoices[i].couponAmount; //appendToBody(String.Format("coupon {0}: ",i+1)); if (int.Parse(curPayment.invoices[i].customerNumber) == -1) { appendSpecialStyle = true; miscPayments.Add(curPayment.invoices[i]); } if (appendSpecialStyle) { appendToBody("<div style=' background-color: aqua;'>"); } appendToBody("<li>"); //print customer number appendToBody("Customer Number: " + curPayment.invoices[i].customerNumber); //print coupon amount appendToBody(String.Format("Coupon Amount: ${0}", curPayment.invoices[i].couponAmount.ToString("0.00"))); //print coupon date appendToBody("Coupon Date: " + curPayment.invoices[i].invoiceDate); appendToBody(""); appendToBody("</li>"); if (appendSpecialStyle){ appendToBody("</div>"); } } //print check total appendToBody(string.Format("Total amount of checks: ${0}", curPayment.total.ToString("0.00"))); //print coupon total appendToBody(string.Format("Total amount of coupons: ${0}", curPayment.couponTotal.ToString("0.00"))); if (curPayment.total.ToString("0.00") == curPayment.couponTotal.ToString("0.00")) { appendToBody("<p style='color: green;'>BALANCE</p>"); } else { appendToBody("<p style='color: red;'>NOT BALANCE</p>"); } appendToBody(""); }