public Response ProcessMoveLoanToEmployee(List <SelectedLoans> loans, int oldEmployeeId, int newEmployeeId)
        {
            Db       db       = new Db();
            Response response = new Cf.ViewModels.Response();

            try
            {
                if (!(db.Connection.State == ConnectionState.Open))
                {
                    db.Connection.Open();
                }
                db.Transaction = db.Connection.BeginTransaction();

                // 1- Loan Decision
                LoanDecision loanDecision = new LoanDecision()
                {
                    Date = System.DateTime.Now,
                    Year = (short)System.DateTime.Now.Year,
                    DeductionStartDate = System.DateTime.Now,
                    LoanDecisionType   = (int)LoanDecisionTypeEnum.ChangeSubscriber
                };
                loanDecision = LoanDecisionServices.Insert(loanDecision);

                // 2- move loans
                foreach (SelectedLoans loan in loans)
                {
                    if (loan.IsSelected)
                    {
                        List <EmployeeProductCalculatorResult> result = db.EmployeeProductCalculator(newEmployeeId, (short)loan.ProductTypeId, loan.RemainingAmount, (short)loan.RemainingPeriod);
                        if (result.Count > 0)
                        {
                            db.LoanMoveFromEmployeeToEmployee(loan.LoanId, newEmployeeId, loanDecision.Id);
                        }
                    }
                }
                db.Transaction.Commit();
                db.Connection.Close();
                response.IsSuccess = true;
            }
            catch (CfException exc)
            {
                db.Transaction.Rollback();
                db.Connection.Close();
                response.IsSuccess = false;
                response.Messages.Add(exc.ErrorDefinition.LocalizedMessage);
            }
            catch (Exception exc)
            {
                db.Transaction.Rollback();
                db.Connection.Close();
                response.IsSuccess = false;
                response.Messages.Add(exc.Message);
            }
            return(response);
        }
        public Response ProcessMoveLoanToGuarantors(List <SelectedGuarantor> guarantors, int LoanId)
        {
            Db       db       = new Db();
            Response response = new Cf.ViewModels.Response();

            try
            {
                int numberOfGuarantors = guarantors.Where(c => c.IsSelected).ToList().Count;
                if (numberOfGuarantors == 0)
                {
                    db.Transaction.Rollback();
                    db.Connection.Close();
                    response.IsSuccess = false;
                    string message = "No Guarantors was selected";
                    response.Messages.Add(message);
                    TempData["Failure"] = message;
                    return(response);
                }

                if (!(db.Connection.State == ConnectionState.Open))
                {
                    db.Connection.Open();
                }
                db.Transaction = db.Connection.BeginTransaction();

                // 1- Loan Decision
                LoanDecision loanDecision = new LoanDecision()
                {
                    Date = System.DateTime.Now,
                    Year = (short)System.DateTime.Now.Year,
                    DeductionStartDate = System.DateTime.Now,
                    LoanDecisionType   = (int)LoanDecisionTypeEnum.ChangeToGuarantor
                };
                LoanDecision oldDecision = LoanDecisionServices.GetByNumber_YearFirstOrNull(loanDecision.Number, loanDecision.Year);
                if (oldDecision != null)
                {
                    loanDecision.Number = (short)(loanDecision.Number + 1);
                }
                loanDecision = LoanDecisionServices.Insert(loanDecision);

                // 2- move loan to selected guarantors
                foreach (SelectedGuarantor guarantor in guarantors)
                {
                    if (guarantor.IsSelected)
                    {
                        db.LoanMoveFromEmployeeToGuarantor(LoanId, guarantor.GuarantorId, loanDecision.Id, numberOfGuarantors);
                    }
                }
                db.Transaction.Commit();
                db.Connection.Close();
                response.IsSuccess = true;
            }
            catch (CfException exc)
            {
                db.Transaction.Rollback();
                db.Connection.Close();
                response.IsSuccess = false;
                response.Messages.Add(exc.ErrorDefinition.LocalizedMessage);
            }
            catch (Exception exc)
            {
                db.Transaction.Rollback();
                db.Connection.Close();
                response.IsSuccess = false;
                response.Messages.Add(exc.Message);
            }
            return(response);
        }