static void Main( string[] args ) { UvMoney.Data.Xml.XmlDataProvider provider = new UvMoney.Data.Xml.XmlDataProvider(); new Importer( provider ).ImportXml(); if( args.Length > 0 ) { provider.ArchiveTransactions( DateTime.Parse( args[0] ) ); Console.WriteLine( "Archived transactions prior to {0}", args[0] ); } }
private void timer_Elapsed( object sender, System.Timers.ElapsedEventArgs e ) { this.timer.Stop(); Console.WriteLine( "Timer elapsed" ); string reminderFilename = ConfigurationManager.AppSettings["ReminderDataFile"]; int reminderInterval = int.Parse( ConfigurationManager.AppSettings["ReminderInterval"] ); ReminderData data = new ReminderData(); data.Load( reminderFilename ); List<RecurringEvent> triggeredTransactions = new List<RecurringEvent>(); List<RecurringEvent> triggeredMessages = new List<RecurringEvent>(); DateTime now = DateTime.Now; bool saveDates = false; foreach( RecurringEvent revent in data.RecurringTransactions ) { /* ---------|-----x-----x-----|---------- 1 2 3 4 1. NextTransactionDate - adds transaction to ledger 2. Second reminder next day 3. Third reminder next day 4. NextEventDate - actual event time, last reminder NextTransactionDate NextReminderDate NextEventDate if( Now > NextEventDate ) // NextEventDate = ComputeNextEventDate() // NextReminderDate = NextEventDate - advanceDays // NextTransactionDate = NextReminderDate if( Now > NextReminderDate && Now <= NextEventDate ) // send reminder message // NextReminderDate += 1 day if( Now > NextTransactionDate ) // insert transaction // NextTransactionDate = DateTime.MaxValue if( revent.Transaction != null && revent.Triggers.IsTriggeredReminder && revent.LastTriggered < revent.ReminderDate ) triggeredTransactions.Add( revent ); if( !string.IsNullOrEmpty( revent.Message ) && revent.Triggers.IsTriggeredReminder && revent.Last ) triggeredTransactions.Add( revent ); */ if( revent.Transaction != null && now >= revent.NextTransactionDate ) { // A transaction event has been triggered. triggeredTransactions.Add( revent ); // So transaction doesn't trigger again: revent.NextTransactionDate = revent.NextEventDate.Value.AddDays( 1 ); saveDates = true; } if( revent.Message != null && now >= revent.NextMessageDate ) { // A message event has been triggered. triggeredMessages.Add( revent ); // Pick next time for message: while( revent.NextMessageDate < now ) revent.NextMessageDate = revent.NextMessageDate.Value.AddSeconds( reminderInterval ); saveDates = true; } if( now >= revent.NextEventDate ) { revent.AdvanceEventDate(); saveDates = true; } } if( saveDates ) data.Save( reminderFilename ); bool hadEvents = false; StringBuilder sb = new StringBuilder(); if( triggeredTransactions.Count > 0 ) { IDataProvider provider = new UvMoney.Data.Xml.XmlDataProvider(); provider.Open(); try { foreach( RecurringEvent revent in triggeredTransactions ) { // Add the associated transaction to the ledger. // The transaction is added on the first // reminder date, not necessarily the // date of the event. This is so the // transaction will appear in the ledger // for pre-planning purposes. ITransaction trans = provider.NewTransaction(); trans.Amount = revent.Transaction.Amount; trans.Memo = revent.Transaction.Memo; trans.Payee = revent.Transaction.Payee; trans.TransactionDate = revent.NextEventDate.Value; trans.IsGenerated = true; foreach( ILineItem item in revent.Transaction.LineItems ) { IAccount account = provider.GetAccount( item.AccountId ); ILineItem newitem = trans.NewLineItem(); newitem.SetAccount( account ); newitem.Amount = item.Amount; newitem.Category = item.Category; newitem.IsTaxRelated = item.IsTaxRelated; newitem.IsReconciled = item.IsReconciled; newitem.Memo = item.Memo; newitem.Number = item.Number; newitem.TransactionType = item.TransactionType; } sb.Append( "Transaction added: " ); sb.Append( trans.Payee ); if( trans.Memo != null ) sb.AppendFormat( " ({0})", trans.Memo ); sb.Append( Environment.NewLine ); hadEvents = true; } provider.CommitChanges(); } finally { provider.Close(); } } if( triggeredMessages.Count > 0 ) { foreach( RecurringEvent revent in triggeredMessages ) { // A trigger without a transaction is // simply a reminder message. Reminder // messages are sent out daily before // the event date. sb.Append( "Reminder: " ); if( revent.Message != null ) sb.Append( revent.Message ); sb.AppendLine(); hadEvents = true; } } if( hadEvents ) { Console.Write( sb.ToString() ); try { // Send email with contents of StringBuilder // SMTP settings set in config file. SmtpClient smtp = new SmtpClient(); smtp.Host = ConfigurationManager.AppSettings["MailHost"]; if( ConfigurationManager.AppSettings["MailUserName"] != null ) { smtp.Credentials = new NetworkCredential( ConfigurationManager.AppSettings["MailUserName"], ConfigurationManager.AppSettings["MailPassword"] ); } string[] addresses = ConfigurationManager.AppSettings["MailTo"].Split( ';' ); MailMessage message = new MailMessage(); message.From = new MailAddress( "*****@*****.**", "UvMoney" ); foreach( string s in addresses ) message.To.Add( s.Trim() ); message.Subject = "UvMoney Reminder"; message.Priority = MailPriority.High; message.Body = sb.ToString(); smtp.Send( message ); } catch( SmtpException ex ) { Console.WriteLine( ex.ToString() ); } } int interval = int.Parse( ConfigurationManager.AppSettings["TimerInterval"] ); this.timer.Interval = 1000 * interval; this.timer.Start(); }