Esempio n. 1
0
        protected void lbSaveAccounts_Click(object sender, EventArgs e)
        {
            using (var rockContext = new RockContext())
            {
                var txn = GetTransaction(rockContext);
                {
                    decimal totalAmount = TransactionDetailsState.Select(d => d.Amount).ToList().Sum();
                    if (txn.TotalAmount != totalAmount)
                    {
                        nbError.Title   = "Incorrect Amount";
                        nbError.Text    = string.Format("<p>When updating account allocations, the total amount needs to remain the same as the original amount ({0}).</p>", txn.TotalAmount.FormatAsCurrency());
                        nbError.Visible = true;
                        return;
                    }

                    var txnDetailService = new FinancialScheduledTransactionDetailService(rockContext);
                    var accountService   = new FinancialAccountService(rockContext);

                    // Delete any transaction details that were removed
                    var txnDetailsInDB = txnDetailService.Queryable().Where(a => a.ScheduledTransactionId.Equals(txn.Id)).ToList();
                    var deletedDetails = from txnDetail in txnDetailsInDB
                                         where !TransactionDetailsState.Select(d => d.Guid).Contains(txnDetail.Guid)
                                         select txnDetail;

                    bool accountChanges = deletedDetails.Any();

                    deletedDetails.ToList().ForEach(txnDetail =>
                    {
                        txnDetailService.Delete(txnDetail);
                    });

                    var changeSummary = new StringBuilder();

                    // Save Transaction Details
                    foreach (var editorTxnDetail in TransactionDetailsState)
                    {
                        editorTxnDetail.Account = accountService.Get(editorTxnDetail.AccountId);

                        // Add or Update the activity type
                        var txnDetail = txn.ScheduledTransactionDetails.FirstOrDefault(d => d.Guid.Equals(editorTxnDetail.Guid));
                        if (txnDetail == null)
                        {
                            accountChanges = true;
                            txnDetail      = new FinancialScheduledTransactionDetail();
                            txnDetail.Guid = editorTxnDetail.Guid;
                            txn.ScheduledTransactionDetails.Add(txnDetail);
                        }
                        else
                        {
                            if (txnDetail.AccountId != editorTxnDetail.AccountId ||
                                txnDetail.Amount != editorTxnDetail.Amount ||
                                txnDetail.Summary != editorTxnDetail.Summary)
                            {
                                accountChanges = true;
                            }
                        }

                        changeSummary.AppendFormat("{0}: {1}", editorTxnDetail.Account != null ? editorTxnDetail.Account.Name : "?", editorTxnDetail.Amount.FormatAsCurrency());
                        changeSummary.AppendLine();

                        txnDetail.AccountId = editorTxnDetail.AccountId;
                        txnDetail.Amount    = editorTxnDetail.Amount;
                        txnDetail.Summary   = editorTxnDetail.Summary;
                    }

                    if (accountChanges)
                    {
                        // save changes
                        rockContext.SaveChanges();

                        // Add a note about the change
                        var noteType = NoteTypeCache.Read(Rock.SystemGuid.NoteType.SCHEDULED_TRANSACTION_NOTE.AsGuid());
                        if (noteType != null)
                        {
                            var noteService = new NoteService(rockContext);
                            var note        = new Note();
                            note.NoteTypeId = noteType.Id;
                            note.EntityId   = txn.Id;
                            note.Caption    = "Updated Transaction";
                            note.Text       = changeSummary.ToString();
                            noteService.Add(note);
                        }
                        rockContext.SaveChanges();
                    }

                    ShowView(txn);
                }
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Handles the Click event of the btnSaveAccounts control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
        protected void btnSaveAccounts_Click(object sender, EventArgs e)
        {
            using (var rockContext = new RockContext())
            {
                var financialScheduledTransaction = GetTransaction(rockContext);

                decimal totalAmount = TransactionDetailsState.Select(d => d.Amount).ToList().Sum();
                if (financialScheduledTransaction.TotalAmount != totalAmount)
                {
                    nbError.Title   = "Incorrect Amount";
                    nbError.Text    = string.Format("<p>When updating account allocations, the total amount needs to remain the same as the original amount ({0}).</p>", financialScheduledTransaction.TotalAmount.FormatAsCurrency());
                    nbError.Visible = true;
                    return;
                }

                var txnDetailService = new FinancialScheduledTransactionDetailService(rockContext);
                var accountService   = new FinancialAccountService(rockContext);

                // Delete any transaction details that were removed
                var txnDetailsInDB = txnDetailService.Queryable().Where(a => a.ScheduledTransactionId.Equals(financialScheduledTransaction.Id)).ToList();
                var deletedDetails = from txnDetail in txnDetailsInDB
                                     where !TransactionDetailsState.Select(d => d.Guid).Contains(txnDetail.Guid)
                                     select txnDetail;

                bool accountChanges = deletedDetails.Any();

                deletedDetails.ToList().ForEach(txnDetail =>
                {
                    txnDetailService.Delete(txnDetail);
                });

                // Save Transaction Details
                foreach (var editorTxnDetail in TransactionDetailsState)
                {
                    editorTxnDetail.Account = accountService.Get(editorTxnDetail.AccountId);

                    // Add or Update the activity type
                    var financialTransactionDetail = financialScheduledTransaction.ScheduledTransactionDetails.FirstOrDefault(d => d.Guid.Equals(editorTxnDetail.Guid));
                    if (financialTransactionDetail == null)
                    {
                        accountChanges                  = true;
                        financialTransactionDetail      = new FinancialScheduledTransactionDetail();
                        financialTransactionDetail.Guid = editorTxnDetail.Guid;
                        financialScheduledTransaction.ScheduledTransactionDetails.Add(financialTransactionDetail);
                    }
                    else
                    {
                        if (financialTransactionDetail.AccountId != editorTxnDetail.AccountId ||
                            financialTransactionDetail.Amount != editorTxnDetail.Amount ||
                            financialTransactionDetail.Summary != editorTxnDetail.Summary)
                        {
                            accountChanges = true;
                        }
                    }

                    financialTransactionDetail.AccountId = editorTxnDetail.AccountId;
                    financialTransactionDetail.Amount    = editorTxnDetail.Amount;
                    financialTransactionDetail.Summary   = editorTxnDetail.Summary;
                }

                if (accountChanges)
                {
                    // save changes
                    rockContext.SaveChanges();
                }

                ShowView(financialScheduledTransaction);
            }
        }