Пример #1
0
        /// <summary>
        /// Method that will be called on an entity immediately before the item is saved by context
        /// </summary>
        /// <param name="dbContext"></param>
        /// <param name="entry"></param>
        public override void PreSaveChanges(Rock.Data.DbContext dbContext, DbEntityEntry entry)
        {
            var rockContext = ( RockContext )dbContext;

            HistoryChangeList = new History.HistoryChangeList();

            var scheduledTransaction = this.ScheduledTransaction ?? new FinancialScheduledTransactionService(dbContext as RockContext).Get(this.ScheduledTransactionId);

            switch (entry.State)
            {
            case EntityState.Added:
            {
                string acct = History.GetValue <FinancialAccount>(this.Account, this.AccountId, rockContext);
                HistoryChangeList.AddChange(History.HistoryVerb.Add, History.HistoryChangeType.Record, acct).SetNewValue(Amount.FormatAsCurrency(scheduledTransaction?.ForeignCurrencyCodeValueId));
                break;
            }

            case EntityState.Modified:
            {
                string acct = History.GetValue <FinancialAccount>(this.Account, this.AccountId, rockContext);

                int?accountId     = this.Account != null ? this.Account.Id : this.AccountId;
                int?origAccountId = entry.OriginalValues["AccountId"].ToStringSafe().AsIntegerOrNull();
                if (!accountId.Equals(origAccountId))
                {
                    History.EvaluateChange(HistoryChangeList, "Account", History.GetValue <FinancialAccount>(null, origAccountId, rockContext), acct);
                }

                var originalCurrencyCodeValueId = scheduledTransaction?.ForeignCurrencyCodeValueId;
                if (scheduledTransaction != null)
                {
                    var originalScheduledTransactionEntry = rockContext.ChangeTracker
                                                            .Entries <FinancialScheduledTransaction>()
                                                            .Where(s => ( int )s.OriginalValues["Id"] == scheduledTransaction.Id)
                                                            .FirstOrDefault();

                    if (originalScheduledTransactionEntry != null)
                    {
                        originalCurrencyCodeValueId = ( int? )originalScheduledTransactionEntry.OriginalValues["ForeignCurrencyCodeValueId"];
                    }
                }

                History.EvaluateChange(HistoryChangeList, acct + " Amount", entry.OriginalValues["Amount"].ToStringSafe().AsDecimal().FormatAsCurrency(originalCurrencyCodeValueId), Amount.FormatAsCurrency(scheduledTransaction?.ForeignCurrencyCodeValueId));
                History.EvaluateChange(HistoryChangeList, acct + " Fee Coverage Amount", (entry.OriginalValues["FeeCoverageAmount"] as int?).FormatAsCurrency(), FeeCoverageAmount.FormatAsCurrency());

                break;
            }

            case EntityState.Deleted:
            {
                string acct = History.GetValue <FinancialAccount>(this.Account, this.AccountId, rockContext);
                HistoryChangeList.AddChange(History.HistoryVerb.Delete, History.HistoryChangeType.Record, acct).SetOldValue(Amount.FormatAsCurrency(scheduledTransaction?.ForeignCurrencyCodeValueId));
                break;
            }
            }

            base.PreSaveChanges(dbContext, entry);
        }
Пример #2
0
        /// <summary>
        /// Method that will be called on an entity immediately before the item is saved by context
        /// </summary>
        /// <param name="dbContext"></param>
        /// <param name="entry"></param>
        public override void PreSaveChanges(Rock.Data.DbContext dbContext, DbEntityEntry entry)
        {
            var rockContext = ( RockContext )dbContext;

            HistoryChangeList = new History.HistoryChangeList();

            switch (entry.State)
            {
            case EntityState.Added:
            {
                string acct = History.GetValue <FinancialAccount>(this.Account, this.AccountId, rockContext);
                HistoryChangeList.AddChange(History.HistoryVerb.Add, History.HistoryChangeType.Record, acct).SetNewValue(Amount.FormatAsCurrency());
                break;
            }

            case EntityState.Modified:
            {
                string acct = History.GetValue <FinancialAccount>(this.Account, this.AccountId, rockContext);

                int?accountId     = this.Account != null ? this.Account.Id : this.AccountId;
                int?origAccountId = entry.OriginalValues["AccountId"].ToStringSafe().AsIntegerOrNull();
                if (!accountId.Equals(origAccountId))
                {
                    History.EvaluateChange(HistoryChangeList, "Account", History.GetValue <FinancialAccount>(null, origAccountId, rockContext), acct);
                }

                History.EvaluateChange(HistoryChangeList, acct, entry.OriginalValues["Amount"].ToStringSafe().AsDecimal().FormatAsCurrency(), Amount.FormatAsCurrency());
                History.EvaluateChange(HistoryChangeList, acct, entry.OriginalValues["FeeAmount"].ToStringSafe().AsDecimal().FormatAsCurrency(), FeeAmount.FormatAsCurrency());
                History.EvaluateChange(HistoryChangeList, acct, entry.OriginalValues["FeeCoverageAmount"].ToStringSafe().AsDecimal().FormatAsCurrency(), FeeCoverageAmount.FormatAsCurrency());

                break;
            }

            case EntityState.Deleted:
            {
                string acct = History.GetValue <FinancialAccount>(this.Account, this.AccountId, rockContext);
                HistoryChangeList.AddChange(History.HistoryVerb.Delete, History.HistoryChangeType.Record, acct).SetOldValue(Amount.FormatAsCurrency());
                break;
            }
            }

            base.PreSaveChanges(dbContext, entry);
        }