public ActionResult create()
        {
            try
            {
                var new_record = new Loan_VM();
                new_record.LoanInAdvanceSetup = new LoanInAdvanceSetup();
                new_record.LoanInAdvanceSetup.MinimumAmount     = 0; new_record.LoanInAdvanceSetup.MaximumAmount = 0;
                new_record.LoanInAdvanceSetup.Percentage        = 0; new_record.LoanInAdvanceSetup.PeriodLength = 0;
                new_record.LoanInAdvanceSetup.InstallmentAmount = 0; new_record.LoanInAdvanceSetup.NumberOfDeductedInstallments = 0;
                new_record.LoanInAdvanceSetup.IntervalPeriod    = 0; new_record.interval = 0;
                new_record.installment_period_type = (installment_period_type)installment_period_type.fixed_period;
                new_record.interval_type           = new interval_type();
                new_record.loan_amount_type        = (loan_amount_type)loan_amount_type.Fixed_amount;
                new_record.loan_eligibility_type   = new loan_eligibility_type();

                var stru   = dbcontext.StructureModels.FirstOrDefault(m => m.All_Models == ChModels.Payroll).Structure_Code;
                var model_ = dbcontext.LoanInAdvanceSetup.ToList();
                if (model_.Count() == 0)
                {
                    new_record.LoanInAdvanceSetup.LoanTypeCode = stru + "1";
                }
                else
                {
                    new_record.LoanInAdvanceSetup.LoanTypeCode = stru + (model_.LastOrDefault().ID + 1).ToString();
                }
                ////ViewBag.eligibility_matrix_code
                ViewBag.manual_payment     = dbcontext.ManualPaymentTypes_Header.Where(m => m.PaymentTypeSourceDocument == 2).Select(m => new { Code = m.PaymentTypeCode + "-[" + m.PaymentTypeDesc + "-]", ID = m.ID }).ToList();                 ///loan only
                ViewBag.salary_code        = dbcontext.salary_code.Where(m => m.CodeGroupType == 1 && m.CodeValueType == 7 && m.SourceEntry == 1).Select(m => new { Code = m.SalaryCodeID + "-[" + m.SalaryCodeDesc + "-]", ID = m.ID }).ToList(); //earning and money and contract
                ViewBag.monthly_salarycode = dbcontext.salary_code.Select(m => new { Code = m.SalaryCodeID + "-[" + m.SalaryCodeDesc + "-]", ID = m.ID }).ToList();



                return(View(new_record));
            }
            catch (Exception e)
            {
                return(RedirectToAction("index"));
            }
        }
        public ActionResult edit(int id)
        {
            try
            {
                ////ViewBag.eligibility_matrix_code
                ViewBag.manual_payment     = dbcontext.ManualPaymentTypes_Header.Where(m => m.PaymentTypeSourceDocument == 2).Select(m => new { Code = m.PaymentTypeCode + "-[" + m.PaymentTypeDesc + "-]", ID = m.ID }).ToList();                 ///loan only
                ViewBag.salary_code        = dbcontext.salary_code.Where(m => m.CodeGroupType == 1 && m.CodeValueType == 7 && m.SourceEntry == 1).Select(m => new { Code = m.SalaryCodeID + "-[" + m.SalaryCodeDesc + "-]", ID = m.ID }).ToList(); //earning and money and contract
                ViewBag.monthly_salarycode = dbcontext.salary_code.Select(m => new { Code = m.SalaryCodeID + "-[" + m.SalaryCodeDesc + "-]", ID = m.ID }).ToList();

                var model      = dbcontext.LoanInAdvanceSetup.FirstOrDefault(m => m.ID == id);
                var edit_model = new Loan_VM();
                edit_model.LoanInAdvanceSetup      = model;
                edit_model.installment_period_type = (installment_period_type)model.InstallmentPeriodType;
                edit_model.interval              = int.Parse(model.Interval);
                edit_model.interval_type         = (interval_type)model.IntervalType;
                edit_model.loan_amount_type      = (loan_amount_type)model.LoanAmountType;
                edit_model.loan_eligibility_type = (loan_eligibility_type)model.LoanEligibilityType;
                return(View(edit_model));
            }
            catch (Exception e)
            {
                return(RedirectToAction("index"));
            }
        }
        public ActionResult create(Loan_VM model, FormCollection form, string Command)
        {
            try
            {
                ////ViewBag.eligibility_matrix_code
                ViewBag.manual_payment     = dbcontext.ManualPaymentTypes_Header.Where(m => m.PaymentTypeSourceDocument == 2).Select(m => new { Code = m.PaymentTypeCode + "-[" + m.PaymentTypeDesc + "-]", ID = m.ID }).ToList();                 ///loan only
                ViewBag.salary_code        = dbcontext.salary_code.Where(m => m.CodeGroupType == 1 && m.CodeValueType == 7 && m.SourceEntry == 1).Select(m => new { Code = m.SalaryCodeID + "-[" + m.SalaryCodeDesc + "-]", ID = m.ID }).ToList(); //earning and money and contract
                ViewBag.monthly_salarycode = dbcontext.salary_code.Select(m => new { Code = m.SalaryCodeID + "-[" + m.SalaryCodeDesc + "-]", ID = m.ID }).ToList();

                var add_record = new LoanInAdvanceSetup();
                add_record = model.LoanInAdvanceSetup;
                add_record.LoanEligibilityType   = model.loan_eligibility_type.GetHashCode();
                add_record.LoanAmountType        = model.loan_amount_type.GetHashCode();
                add_record.InstallmentAmount     = model.installment_period_type.GetHashCode();
                add_record.IntervalType          = model.interval_type.GetHashCode();
                add_record.InstallmentPeriodType = model.installment_period_type.GetHashCode();
                /////
                var a1 = form["A1"].Split(',');
                var a2 = form["A2"].Split(',');
                var a3 = form["A3"].Split(',');
                var a4 = form["A4"].Split(',');
                var a5 = form["A5"].Split(',');
                ////
                if (a1.Length == 1)
                {
                    add_record.EnableToGenerateManualPayment = false;
                    model.LoanInAdvanceSetup.EnableToGenerateManualPayment = false;
                }
                else
                {
                    add_record.EnableToGenerateManualPayment = true;
                    model.LoanInAdvanceSetup.EnableToGenerateManualPayment = true;
                }
                if (a2.Length == 1)
                {
                    add_record.EnableLoanAmountRestriction = false;
                    model.LoanInAdvanceSetup.EnableLoanAmountRestriction = false;
                }
                else
                {
                    add_record.EnableLoanAmountRestriction = true;
                    model.LoanInAdvanceSetup.EnableLoanAmountRestriction = true;
                }
                if (a3.Length == 1)
                {
                    add_record.EnableFreezing = false;
                    model.LoanInAdvanceSetup.EnableFreezing = false;
                }
                else
                {
                    add_record.EnableFreezing = true;
                    model.LoanInAdvanceSetup.EnableFreezing = true;
                }
                if (a4.Length == 1)
                {
                    add_record.EnableAutomaticPayrollDeduction = false;
                    model.LoanInAdvanceSetup.EnableAutomaticPayrollDeduction = false;
                }
                else
                {
                    add_record.EnableAutomaticPayrollDeduction = true;
                    model.LoanInAdvanceSetup.EnableAutomaticPayrollDeduction = true;
                }
                if (a5.Length == 1)
                {
                    add_record.EnableToRecuiningLoanRequestAutomaticAfterCloseTheRequest = false;
                    model.LoanInAdvanceSetup.EnableToRecuiningLoanRequestAutomaticAfterCloseTheRequest = false;
                }
                else
                {
                    add_record.EnableToRecuiningLoanRequestAutomaticAfterCloseTheRequest = true;
                    model.LoanInAdvanceSetup.EnableToRecuiningLoanRequestAutomaticAfterCloseTheRequest = true;
                }

                if (model.loan_eligibility_type == (loan_eligibility_type)loan_eligibility_type.Specific_employees)
                {
                    if (model.LoanInAdvanceSetup.EligibilityMatrixCode == null)
                    {
                        return(View(model));
                    }
                }
                if (a1.Length == 2)
                {
                    if (model.LoanInAdvanceSetup.ManualPaymentCode == null)
                    {
                        return(View(model));
                    }
                }
                if (model.loan_amount_type == (loan_amount_type)loan_amount_type.percentage_amount_from_salary_code_amount)
                {
                    if (model.LoanInAdvanceSetup.SalaryCodeAmount == null)
                    {
                        return(View(model));
                    }
                }
                if (a4.Length == 2)
                {
                    if (model.LoanInAdvanceSetup.SalaryCodeID == null)
                    {
                        return(View(model));
                    }
                }
                add_record.Interval     = model.interval.ToString();
                add_record.Created_By   = User.Identity.Name;
                add_record.Created_Date = DateTime.Now.Date;
                var loan = dbcontext.LoanInAdvanceSetup.Add(add_record);
                dbcontext.SaveChanges();
                //=================================check for alert==================================
                var get_result_check = HR.Controllers.check.check_alert("loan InAdvance card", 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();
                }
                //===================================================================================
                if (Command == "link")
                {
                    return(RedirectToAction("link", new { loan_ID = loan.ID, type = 0 }));
                }
                return(RedirectToAction("index"));
            }
            catch (Exception e)
            {
                return(View(model));
            }
        }