protected void btnSave_OnDirectEvent(object sender, DirectEventArgs e)
        {
            int loanId = int.Parse(hdnLoanId.Text);
            var LoanStatus = ObjectContext.LoanAccountStatus.SingleOrDefault(entity => entity.LoanAccount.FinancialAccountId == loanId && entity.IsActive == true);
            var loanAccountStatusTypeAssoc = ObjectContext.LoanAccountStatusTypeAssocs.Where(entity => entity.EndDate == null
                                                && entity.FromStatusTypeId == LoanStatus.LoanAccountStatusType.Id
                                                && entity.ToStatusTypeId == LoanAccountStatusType.UnderLitigationType.Id);

            if (loanAccountStatusTypeAssoc == null)
            {
                X.Msg.Alert("Error!", "Cannot change status to Under Litigation.").Show();
                return;
            }
            LoanStatus.IsActive = false;

            LoanAccountStatu newLoanAccountStatus = new LoanAccountStatu();
            newLoanAccountStatus.FinancialAccountId = loanId;
            newLoanAccountStatus.LoanAccountStatusType = LoanAccountStatusType.UnderLitigationType;
            newLoanAccountStatus.Remarks = txtComment.Text;
            newLoanAccountStatus.TransitionDateTime = DateTime.Now;
            newLoanAccountStatus.IsActive = true;
            ObjectContext.LoanAccountStatus.AddObject(newLoanAccountStatus);

            ChangeCustomerStatusToUnderLitigation(loanId);

            ObjectContext.SaveChanges();
        }
        public static void UpdateLoanStatus(DateTime today, LoanAccount loanAccount, decimal balance)
        {
            var currentstatus = loanAccount.CurrentStatus;

            if ((currentstatus.StatusTypeId == LoanAccountStatusType.CurrentType.Id
                     || currentstatus.StatusTypeId == LoanAccountStatusType.DelinquentType.Id
                     || currentstatus.StatusTypeId == LoanAccountStatusType.UnderLitigationType.Id
                     || currentstatus.StatusTypeId == LoanAccountStatusType.RestructuredType.Id)
                     && balance == 0)
            {
                var isValidIndicator = ObjectContext.LoanAccountStatusTypeAssocs.Where(entity => entity.FromStatusTypeId == currentstatus.StatusTypeId
                    && entity.ToStatusTypeId == LoanAccountStatusType.PaidOffType.Id && entity.EndDate == null).Count();

                if (isValidIndicator > 0)
                {
                    currentstatus.IsActive = false;
                    LoanAccountStatu loanAccountStatus = new LoanAccountStatu();
                    loanAccountStatus.FinancialAccountId = currentstatus.FinancialAccountId;
                    loanAccountStatus.StatusTypeId = LoanAccountStatusType.PaidOffType.Id;
                    loanAccountStatus.TransitionDateTime = today;
                    loanAccountStatus.IsActive = true;
                    ObjectContext.LoanAccountStatus.AddObject(loanAccountStatus);
                }
            }
        }
        private LoanAccountStatu CreateLoanAccountStatus(LoanAccount loanAccount, DateTime today)
        {
            LoanAccountStatu loanAccountStatus1 = new LoanAccountStatu();
            loanAccountStatus1.LoanAccount = loanAccount;
            loanAccountStatus1.LoanAccountStatusType = LoanAccountStatusType.CurrentType;
            loanAccountStatus1.TransitionDateTime = today;
            loanAccountStatus1.IsActive = true;

            return loanAccountStatus1;
        }
        public static void UpdateLoanAccountStatus(DateTime date)
        {
            var dayDiff = 0;

            var loanAccounts = from la in ObjectContext.LoanAccounts
                               join ls in ObjectContext.LoanAccountStatus on la.FinancialAccountId equals ls.FinancialAccountId
                               where ls.IsActive == true && (ls.StatusTypeId == LoanAccountStatusType.CurrentType.Id)
                               select la;

            foreach (var loanAccount in loanAccounts)
            {
                var payments = ObjectContext.FinAcctTrans.Where(entity => entity.FinancialAccountId == loanAccount.FinancialAccountId && entity.FinancialAcctTransTypeId == FinlAcctTransType.AccountPaymentType.Id).OrderByDescending(entity => entity.TransactionDate);
                //3 months after last payment date or if no payment since after release, change status to delinquent
                if (payments.Count() > 0)
                {
                    var LastPaymentDate = payments.FirstOrDefault().TransactionDate;
                    dayDiff = date.Subtract(LastPaymentDate).Days;
                }
                else dayDiff = date.Subtract(loanAccount.LoanReleaseDate.Value).Days;

                    if (dayDiff > 90)
                    {
                        var activeStatus = loanAccount.CurrentStatus;
                        var isValidIndicator = ObjectContext.LoanAccountStatusTypeAssocs.Where(entity => entity.FromStatusTypeId == activeStatus.StatusTypeId
                              && entity.ToStatusTypeId == LoanAccountStatusType.DelinquentType.Id && entity.EndDate == null).Count();

                        if (isValidIndicator > 0)
                        {
                            activeStatus.IsActive = false;
                            LoanAccountStatu loanStatus = new LoanAccountStatu();
                            loanStatus.LoanAccount = loanAccount;
                            loanStatus.LoanAccountStatusType = LoanAccountStatusType.DelinquentType;
                            loanStatus.TransitionDateTime = DateTime.Now;
                            loanStatus.IsActive = true;

                            ObjectContext.LoanAccountStatus.AddObject(loanStatus);
                        }
                    }

            }
        }