protected void SubmitPurchase_Click( object sender, EventArgs e ) { try { dataMgr.Lock(); try { // TODO: Remove obsolete DataManager code. // Make sure we have the very latest data file. ledger = dataMgr.Load( false, false ); // Build a transaction object Transaction trans = new Transaction(); trans.Id = ledger.GetNextTransactionId(); trans.Date = DateTime.Parse( txtDate.Text ); trans.Amount = Convert.ToDecimal( txtAmount.Text ); trans.Description = txtPayee.Text; if( txtMemo.Text.Length > 0 ) trans.Memo = txtMemo.Text; // Populate line items Account account = ledger.FindAccount( Convert.ToInt32( ddlAccount.SelectedValue ) ); LineItemCredit credit = new LineItemCredit( account, trans.Amount ); credit.Id = ledger.GetNextLineItemId(); credit.Transaction = trans; if( txtNumber.Text.Length > 0 ) credit.Number = txtNumber.Text; if( txtMemo.Text.Length > 0 ) credit.Memo = txtMemo.Text; trans.LineItems.Add( credit ); Account expenseAccount = null; Category expenseCategory = null; if( !ledger.ParseCategory( txtCategory.Text, out expenseAccount, out expenseCategory ) ) { SubmitPurchaseStatusLabel.Text = "The category provided could not be found."; return; } LineItemDebit debit = new LineItemDebit( expenseAccount, trans.Amount ); debit.Id = ledger.GetNextLineItemId(); debit.Transaction = trans; debit.CategoryObject = expenseCategory; trans.LineItems.Add( debit ); // Add to ledger and save ledger.AddTransaction( trans ); // HACK: Removing until we figure out how to fix this... // ledger.RefreshMissingTransactions(); ledger.SortTransactions(); dataMgr.Save( ledger ); decimal newBalance = account.Balance; SubmitPurchaseStatusLabel.Text = string.Format( "Transaction #{0} saved. {2} balance {1:N2}.", trans.Id, newBalance, account.Name ); #if DATASTORAGE try { // DataLedger is initialized in BasePage.OnInit DataLedger.Storage.BeginTransaction(); try { IAccount dbaccount = DataLedger.FindAccountByName( account.Name ); if( dbaccount == null ) throw new ApplicationException( "Account " + account.Name + " not found in data storage." ); IAccount dbexpense = DataLedger.FindAccountByName( expenseAccount.Name ); if( dbexpense == null ) throw new ApplicationException( "Expense account " + expenseAccount.Name + " not found in data storage." ); ITransaction dbtrans = DataLedger.Storage.CreateTransaction(); dbtrans.Amount = trans.Amount; dbtrans.Date = trans.Date; dbtrans.Memo = trans.Memo; dbtrans.Payee = trans.Payee; DataLedger.Storage.SaveTransaction( dbtrans ); ILineItem dbcredit = DataLedger.Storage.CreateLineItem( dbtrans ); dbcredit.AccountId = dbaccount.Id; dbcredit.Amount = credit.Amount; dbcredit.Category = null; dbcredit.Memo = credit.Memo; dbcredit.Number = credit.Number; dbcredit.Type = TransactionType.Credit; DataLedger.Storage.SaveLineItem( dbcredit ); ILineItem dbdebit = DataLedger.Storage.CreateLineItem( dbtrans ); dbdebit.AccountId = dbexpense.Id; dbdebit.Amount = debit.Amount; dbdebit.Category = debit.Category; dbdebit.Type = TransactionType.Debit; DataLedger.Storage.SaveLineItem( dbdebit ); DataLedger.Storage.CommitTransaction(); DataLedger.RefreshUnmatchedRecords(); } catch( Exception ) { DataLedger.Storage.RollbackTransaction(); throw; } } catch( Exception ex ) { SubmitPurchaseStatusLabel.Text += "<br />An error occurred while saving to data storage: <br /><pre>" + ex.ToString() + "</pre>"; } #endif // Reset text boxes txtNumber.Text = ""; txtAmount.Text = ""; txtPayee.Text = ""; txtMemo.Text = ""; txtCategory.Text = ""; } finally { dataMgr.Unlock(); } } catch( LockException ex ) { SubmitPurchaseStatusLabel.Text = "The data file is in use by '" + ex.LockOwner + "'. Please try again in a moment."; } catch( Exception ex ) { SubmitPurchaseStatusLabel.Text = "An error occurred while saving: " + ex.Message; } }
/// <summary> /// Load transactional data from the storage file if necessary. /// </summary> public Ledger Load( bool addMissingTransactions, bool sortTransactions ) { if( ledger == null || cacheTime == DateTime.MinValue || ledgerStorage.FileDateTime > cacheTime ) { stopwatch.Reset(); stopwatch.Start(); ledger = ledgerStorage.LoadLedger(); if( addMissingTransactions ) ledger.AddMissingTransactions(); if( sortTransactions ) ledger.SortTransactions(); cacheTime = ledger.LoadDate; stopwatch.Stop(); loadTimeMilliseconds = stopwatch.ElapsedMilliseconds; } // Extra insurance, but at a cost: this may be second time done (LoadLedger does too) ledgerStorage.ValidateIdentifierLinks( ledger ); return ledger; }