public void PrepareForSave(LoanApplication loanApplication, DateTime today)
        {
            if (this.IsNew)
            {
                ProductFeatureApplicability featureApp;
                if (this.IsFromPickList == false)
                {
                    ProductFeature feature = ProductFeature.GetByName(this.Name);
                    if (feature == null)
                    {
                        feature = new ProductFeature();
                        feature.Name = this.Name;
                        feature.ProductFeatureCategory = ProductFeatureCategory.FeeType;
                        Context.ProductFeatures.AddObject(feature);
                    }

                    FinancialProduct financialProduct = FinancialProduct.GetById(this.FinancialProductId);
                    featureApp = ProductFeatureApplicability.GetActive(feature, financialProduct);
                    if (featureApp == null)
                        featureApp = ProductFeatureApplicability.Create(feature, financialProduct, today);

                }
                else
                {
                    featureApp = ProductFeatureApplicability.GetById(this.ProductFeatureApplicabilityId);
                }

                ApplicationItem item = new ApplicationItem();
                item.Application = loanApplication.Application;
                item.ProductFeatureApplicability = featureApp;
                item.FeeComputedValue = this.TotalChargePerFee;
                item.EffectiveDate = today;

                LoanApplicationFee fee = new LoanApplicationFee();
                fee.LoanApplication = loanApplication;
                fee.Particular = this.Name;
                fee.Amount = this.Amount;
                //fee.ApplicationItem = item;

                Context.LoanApplicationFees.AddObject(fee);
                Context.ApplicationItems.AddObject(item);
            }
            else if (this.ToBeDeleted)
            {
                LoanApplicationFee applicationFee = Context.LoanApplicationFees.SingleOrDefault(entity => entity.Id == this.LoanApplicationFeeId);
                if (applicationFee != null)
                {
                    var application = applicationFee.LoanApplication.Application;
                    var productFeature = ProductFeature.GetByName(applicationFee.Particular);
                    var applicationItem = ApplicationItem.Get(application, productFeature);
                    applicationItem.EndDate = today;
                    Context.LoanApplicationFees.DeleteObject(applicationFee);
                }
            }
            else if (this.IsNew == false && this.ToBeDeleted == false)
            {
                ProductFeatureApplicability featureApp;
                if (this.IsFromPickList == false)
                {
                    ProductFeature feature = ProductFeature.GetByName(this.Name);
                    if (feature == null)
                    {
                        feature = new ProductFeature();
                        feature.Name = this.Name;
                        feature.ProductFeatureCategory = ProductFeatureCategory.FeeType;
                        Context.ProductFeatures.AddObject(feature);
                    }

                    FinancialProduct financialProduct = FinancialProduct.GetById(this.FinancialProductId);
                    featureApp = ProductFeatureApplicability.GetActive(feature, financialProduct);
                    if (featureApp == null)
                        featureApp = ProductFeatureApplicability.Create(feature, financialProduct, today);

                }
                else
                {
                    featureApp = ProductFeatureApplicability.GetById(this.ProductFeatureApplicabilityId);
                }

                ApplicationItem item = new ApplicationItem();
                item.Application = loanApplication.Application;
                item.ProductFeatureApplicability = featureApp;
                item.FeeComputedValue = this.TotalChargePerFee;
                item.EffectiveDate = today;

                LoanApplicationFee fee = new LoanApplicationFee();
                fee.LoanApplication = loanApplication;
                fee.Particular = this.Name;
                fee.Amount = this.Amount;
                //fee.ApplicationItem = item;

                Context.LoanApplicationFees.AddObject(fee);
                Context.ApplicationItems.AddObject(item);
            }
        }
        public LoanFeeModel(LoanApplicationFee applicationFee)
            : this()
        {
            this.IsNew = false;
            var application = applicationFee.LoanApplication.Application;
            var productFeature = ProductFeature.GetByName(applicationFee.Particular);
            this.LoanApplicationFeeId = applicationFee.Id;
            var applicationItem = ApplicationItem.Get(application, productFeature);

            this.Name = applicationFee.Particular;
            this.TotalChargePerFee = applicationItem.FeeComputedValue ?? 0;
            this.Amount = applicationFee.Amount;

            this.ProductFeatureApplicabilityId = applicationItem.ProdFeatApplicabilityId;
            var feature = applicationItem.ProductFeatureApplicability;
            if (feature != null && feature.Fee != null)
            {
                Fee fee = feature.Fee;
                this.ChargeAmount = fee.ChargeAmount ?? 0;
                this.BaseAmount = fee.BaseAmount ?? 0;
                this.Rate = fee.PercentageRate ?? 0;
            }
        }