//Queball Nightly Utility using System; using System.Collections.Generic; using System.Linq; using System.Text; using EntitySpaces.Interfaces; using Queball3.BusinessObjects; using System.Net.Mail; namespace NightlyUtility { class Program { static void Main(string[] args) { try { //Init Entity Spaces esConnectionElement conn = new esConnectionElement(); // Add another connection conn = new esConnectionElement(); conn.Name = "Queball3"; conn.ConnectionString = "AppSettings:Queball3"; conn.Provider = "EntitySpaces.OracleClientProvider"; conn.ProviderClass = "DataProvider"; conn.SqlAccessType = esSqlAccessType.DynamicSQL; conn.ProviderMetadataKey = "esDefault"; esConfigSettings.ConnectionInfo.Connections.Add(conn); // Assign the default connection esConfigSettings.ConnectionInfo.Default = "Queball3"; esProviderFactory.Factory = new EntitySpaces.LoaderMT.esDataProviderFactory(); ProviderCollection.TurnOffDupes(); CampaignCollection campaigns = CampaignCollection.GetActiveCampaigns(); using (ProviderCollection providers = ProviderCollection.GetUnassignedProviders()) { campaigns.AssignProviders(providers); } Money.ClearSurplus(); campaigns.InActivateProviders(); campaigns.SendFax(); campaigns.SendEmail(); campaigns.CreateMailing(); } catch(Exception ex){ MailMessage msg = new MailMessage("QueBallError@primehealthservices.com", "support@prime-health.net", "Error in Queball", ex.Message + "\n\n" + ex.StackTrace); SmtpClient client = new SmtpClient("mail.prime-health.net"); client.Send(msg); } } } } //Queball USPS Mail Presorter using System; using System.Collections.Generic; using System.Data; using System.Data.OracleClient; using System.IO; using System.Text; using System.Text.RegularExpressions; using System.Net; using Queball3.BusinessObjects; namespace Presorter { public enum PieceType{ Letter, Flat } public static class Sorter { //Constants const int MINIMUM_PIECES = 150; const int PIECES_PER_TRAY = 500; //Internal Classes class PostageRates { public decimal FiveDigit; public decimal ThreeDigit; public decimal AADC; public decimal Mixed; public decimal PerLbWeight = 0; public decimal PerLbRate = 0; public PostageRates(PieceType pieceType) { } } public static Tray[] SortProviders(List providers,PieceType pieceType) { //Load Postage Rate variables PostageRates r = new PostageRates(pieceType); List trays = new List(); providers.Sort( delegate(Provider p1, Provider p2) { return p1.Zip.CompareTo(p2.Zip); } ); #region Load AADC data from usps website string orig = String.Empty; Dictionary aadcMap = null; try { Console.WriteLine("Presort:Checking USPS website..."); HttpWebRequest Request = (HttpWebRequest)HttpWebRequest.Create("http://pe.usps.gov/text/dmm300/L801.htm"); // Pretend we're Firefox Request.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.8.1.3) Gecko/20070309 Firefox/2.0.0.3"; // Get the response from the server try { using (HttpWebResponse Response = (HttpWebResponse)Request.GetResponse()) { StreamReader sr = new StreamReader(Response.GetResponseStream()); orig = sr.ReadToEnd(); } aadcMap = ParseUspsHtml(orig); Console.WriteLine("Presort:USPS Website Check Successfull."); } catch (Exception ex) { Console.WriteLine("Presort:USPS Website Check Failed on Request with message:\n\t" + ex.Message); } } catch (Exception ex) { Console.WriteLine("Presort:USPS Website Check Failed on setup with message:\n\t" + ex.Message); } if (aadcMap == null || aadcMap.Count == 0) { //Rerun on cache Console.WriteLine("Presort:Error on USPS website, reverting to cache version..."); if (!Directory.Exists(@"C:\QueBall_Logs\")) Directory.CreateDirectory(@"C:\QueBall_Logs\"); aadcMap = ParseUspsHtml(File.ReadAllText(@"C:\QueBall_Logs\UspsCache.html")); Console.WriteLine("Presort:Cache version read in completed."); } else { File.WriteAllText(@"C:\QueBall_Logs\UspsCache.html", orig); //Write cache file } #endregion //Mixed AADC foreach (Provider p in providers) { p.Endorsement = "MIXED AADC " + aadcMap[p.Zip.Substring(0, 3)]; } providers.Sort( delegate(Provider p1, Provider p2) { int c=p1.Endorsement.CompareTo(p2.Endorsement); if (c == 0 && p1.FacilityName!=null && p2.FacilityName!=null) c = p1.FacilityName.CompareTo(p2.FacilityName); return c; } ); trays.Add(new Tray("MIXED AADC", providers)); //Break up trays that are overfilled for (int i = 0; i < trays.Count; i++) { if (trays[i].Providers.Count > PIECES_PER_TRAY) { Provider[] overflow = new Provider[trays[i].Providers.Count - PIECES_PER_TRAY]; trays[i].Providers.CopyTo(PIECES_PER_TRAY, overflow, 0, overflow.Length); trays[i].Providers.RemoveRange(PIECES_PER_TRAY, overflow.Length); trays.Insert(i + 1, new Tray(trays[i].Id, new List(overflow))); } } return trays.ToArray(); } private static Dictionary ParseUspsHtml(string stuff) { Regex body = new Regex(@".*", RegexOptions.IgnorePatternWhitespace | RegexOptions.Singleline); string s = body.Match(stuff).Groups[0].ToString().Replace(" ", "0").Replace(" ", "0"); Regex html = new Regex("<.*>", RegexOptions.Compiled); Regex rows = new Regex(@" ( \s* (<(p|div).*?>\s*)* ()? (?.*?) (\s*)*? \s* )+ \s* ", RegexOptions.IgnorePatternWhitespace | RegexOptions.Singleline | RegexOptions.Multiline | RegexOptions.ExplicitCapture); Regex aadcNum = new Regex(@"\d+$"); MatchCollection mc = rows.Matches(s); Dictionary aadcMap = new Dictionary(); foreach (Match m in mc) { string geoZips = html.Replace(m.Groups["guts"].Captures[0].Value.Trim(), ""); string stringValue = aadcNum.Match(m.Groups["guts"].Captures[1].Value.Trim()).Value; if (m.Groups["guts"].Captures[2].Value.Trim() == "1") continue; string[] toks = geoZips.Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries); foreach (string tok in toks) { int start = 0; int end = 0; if (tok.Contains("-")) { string[] range = tok.Split('-'); start = Convert.ToInt32(range[0]); end = Convert.ToInt32(range[1]); } else { start = end = Convert.ToInt32(tok.Trim()); } for (int i = start; i <= end; i++) { aadcMap[i.ToString().PadLeft(3,'0')] = stringValue; } } //Console.WriteLine("{0}\t{1}\t{2}", m.Groups["guts"].Captures[0].Value.Trim(), m.Groups["guts"].Captures[1].Value.Trim(), m.Groups["guts"].Captures[2].Value.Trim()); } return aadcMap; } } }