public ActionResult Loan_transfer_entry(int id)
        {
            try
            {
                var loan = dbcontext.LoanRequest.FirstOrDefault(m => m.ID == id);
                ViewBag.emp       = dbcontext.Employee_Profile.Where(m => m.Active == true).ToList().Select(m => new { Code = m.Code + "--[" + m.Name + ']', ID = m.ID });
                ViewBag.loan_type = dbcontext.LoanInAdvanceSetup.ToList().Select(m => new { Code = m.LoanTypeCode + "--[" + m.LoanTypeDesc + ']', ID = m.ID });
                //////////////////////////////////
                var new_trans    = new LoanTransfer();
                var stru         = dbcontext.StructureModels.FirstOrDefault(m => m.All_Models == ChModels.Payroll).Structure_Code;
                var All_requests = dbcontext.LoanRequest.ToList();
                if (All_requests.Count() == 0)
                {
                    new_trans.NewLoanRequestNumber = stru + "1";
                }
                else
                {
                    new_trans.NewLoanRequestNumber = stru + (All_requests.LastOrDefault().ID + 1).ToString();
                }
                new_trans.LoanRequestNumber            = loan.LoanRequestNumber;
                new_trans.LoanTypeCode                 = loan.LoanTypeCode;
                new_trans.StartDate                    = loan.StartDate;
                new_trans.EndDate                      = loan.EndDate;
                new_trans.NumberOfDeductedInstallments = loan.NumberOfDeductedInstallments;
                new_trans.TransferTransactionNumber    = loan.TransferTransactionNumber;
                new_trans.NumberOfInstallment          = loan.NumberOfInstallment;
                new_trans.LoanAmount                   = loan.LoanAmount;
                var All_trans = dbcontext.LoanTransfer.ToList();
                if (All_trans.Count() == 0)
                {
                    new_trans.TransferTransactionNumber = stru + "1";
                }
                else
                {
                    new_trans.TransferTransactionNumber = stru + (All_trans.LastOrDefault().ID + 1).ToString();
                }
                //////////////////////////////////

                var transfer = new transferVM
                {
                    LoanRequest  = loan,
                    LoanTransfer = new_trans
                };
                return(View(transfer));
            }
            catch (Exception)
            {
                return(RedirectToAction("index"));
            }
        }
        public ActionResult Loan_transfer_entry(transferVM model)
        {
            try
            {
                var loan = dbcontext.LoanRequest.FirstOrDefault(m => m.ID == model.LoanRequest.ID);
                var sta  = dbcontext.status.FirstOrDefault(m => m.ID == loan.statusID);
                if (sta.statu == check_status.Approved || sta.statu == check_status.Rejected || sta.statu == check_status.Closed || sta.statu == check_status.Canceled)
                {
                    TempData["message"] = HR.Resource.training.status_message;
                    return(RedirectToAction("index"));
                }
                ViewBag.emp       = dbcontext.Employee_Profile.Where(m => m.Active == true).ToList().Select(m => new { Code = m.Code + "--[" + m.Name + ']', ID = m.ID });
                ViewBag.loan_type = dbcontext.LoanInAdvanceSetup.ToList().Select(m => new { Code = m.LoanTypeCode + "--[" + m.LoanTypeDesc + ']', ID = m.ID });
                var emp_id  = int.Parse(model.LoanTransfer.ToEmplpyee_Code);
                var new_emp = dbcontext.Employee_Profile.FirstOrDefault(m => m.ID == emp_id);
                var old_emp = loan.EmployeeID + " -- " + loan.emp_name;
                loan.EmployeeID = model.LoanTransfer.ToEmplpyee_Code;
                loan.emp_name   = new_emp.Full_Name;
                dbcontext.SaveChanges();

                //====
                var Payment_Type_Source_Document_ = Payment_Type_Source_Document.Loan.GetHashCode();

                var transaction = dbcontext.Employee_Payroll_Transactions.Where(m => m.SourceDocumentRefrence == loan.LoanRequestNumber && m.SourceDocumentType == Payment_Type_Source_Document_).ToList();
                foreach (var item in transaction)
                {
                    item.Employee_Code = new_emp.Code;
                    dbcontext.SaveChanges();
                }
                dbcontext.SaveChanges();
                //====
                //////////////
                model.LoanTransfer.CreatedBy   = User.Identity.Name;
                model.LoanTransfer.CreatedDate = DateTime.Now.Date;
                model.LoanTransfer.to_emp      = loan.EmployeeID + " -- " + loan.emp_name;
                model.LoanTransfer.from_emp    = old_emp;
                dbcontext.LoanTransfer.Add(model.LoanTransfer);
                dbcontext.SaveChanges();
                //=================================check for alert==================================
                var get_result_check = HR.Controllers.check.check_alert("loan transfer transaction", HR.Models.user.Action.Create, HR.Models.user.type_field.form);
                if (get_result_check != null)
                {
                    var inbox = new Models.user.Alert_inbox {
                        send_from_user_id = User.Identity.Name, send_to_user_id = get_result_check.send_to_ID_user, title = get_result_check.Subject, Subject = get_result_check.Message
                    };
                    if (get_result_check.until != null)
                    {
                        if (get_result_check.until.Value.Year != 0001)
                        {
                            inbox.until = get_result_check.until;
                        }
                    }
                    ApplicationDbContext dbcontext = new ApplicationDbContext();
                    dbcontext.Alert_inbox.Add(inbox);
                    dbcontext.SaveChanges();
                }
                //===================================================================================
                /////////////
                return(RedirectToAction("index"));
            }
            catch (Exception)
            {
                return(View(model));
            }
        }