public ActionResult Edit(int?id)
        {
            LoanRequestViewModel vm = new LoanRequestViewModel();

            try
            {
                Db db = new Db(DbServices.ConnectionString);

                if (id == null)
                {
                    return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
                }
                Product           product           = ProductServices.Get(id.Value, db);
                Request           request           = RequestServices.Get(id.Value, db);
                LoanRequest       loanRequest       = LoanRequestServices.Get(id.Value, db);
                RefundableProduct refundableProduct = RefundableProductServices.Get(id.Value, db);

                if (product == null || request == null || loanRequest == null || refundableProduct == null)
                {
                    return(HttpNotFound());
                }

                // For Product
                //ViewBag.EmployeeList = new SelectList(EmployeeServices.List(db), "Id", "Id_Name", product.Employee);
                ViewBag.ProductTypeList = new SelectList(ProductTypeServices.List(db), "Id", "Name", product.ProductType);

                // For Request
                //We need to customise the droplist for two options
                ViewBag.BypassStatusList = new SelectList(BypassStatusServices.List(db).Where((c => (c.Id == 0 || c.Id == 2))), "Id", "Name");


                EmployeeProductCalculatorFilter f = new EmployeeProductCalculatorFilter();
                f.EmployeeId = product.Employee; f.ProductTypeId = (short)product.ProductType;
                f.Amount     = (decimal)request.Amount; f.Period = (short)refundableProduct.PaymentPeriod;
                EmployeeProductCalculatorResult result = db.EmployeeProductCalculatorFirstOrDefault(f);
                if (result != null)
                {
                    ViewBag.Calculations = result;
                }

                vm.RequestProduct = product;
                vm.Request        = request;
                vm.LoanRequest    = loanRequest;
                vm.RequestProductProductRefundableProduct = refundableProduct;
            }
            catch (CfException cfex)
            {
                TempData["Failure"] = cfex.ErrorDefinition.LocalizedMessage;
            }
            catch (Exception ex)
            {
                TempData["Failure"] = ex.Message;
            }
            return(View(vm));
        }
        public JsonResult Calculate(int employeeId, int productTypeId, float amount, int period, float netAmount, float deductions)
        {
            try
            {
                EmployeeVw employee = EmployeeVwServices.Get(employeeId);
                if (employee == null)
                {
                    return(Json(new { status = false, message = "الرقم الذاتي غير صحيح" }, JsonRequestBehavior.AllowGet));
                }

                EmployeeProductCalculatorFilter f = new EmployeeProductCalculatorFilter()
                {
                    EmployeeId    = employeeId,
                    ProductTypeId = (short)productTypeId,
                    Amount        = (decimal)amount,
                    Period        = (short)period
                };

                EmployeeProductCalculatorResult result = db.EmployeeProductCalculatorFirstOrDefault(f);
                if (result == null)
                {
                    return(Json(new { status = false, message = "NoResult" }, JsonRequestBehavior.AllowGet));
                }
                else
                {
                    GetEmployeeSolvencyFilter filter = new GetEmployeeSolvencyFilter()
                    {
                        EmployeeId  = employeeId,
                        Amount      = (decimal)amount,
                        Date        = System.DateTime.Now,
                        Installment = result.Installment,
                        GrossSalary = (decimal)netAmount,
                        NetSalary   = (decimal)netAmount
                    };
                    GetEmployeeSolvencyResult solvencyResult = db.GetEmployeeSolvencyFirstOrDefault(filter);

                    return(Json(new { status = true, Calculator = result, Solevency = solvencyResult }, JsonRequestBehavior.AllowGet));
                }
            }
            catch (CfException cfex)
            {
                //TempData["Failure"] = cfex.ErrorDefinition.LocalizedMessage;
                return(Json(new { status = false, message = cfex.ErrorDefinition.LocalizedMessage }, JsonRequestBehavior.AllowGet));
            }
            catch (Exception ex)
            {
                //TempData["Failure"] = ex.Message;
                return(Json(new { status = false, message = ex.Message }, JsonRequestBehavior.AllowGet));
            }
        }
        public string Validate(decimal?Amount, int?RequestId)
        {
            if (RequestId == 0)
            {
                return("Error");// RedirectToAction("ManageNewRequests");
            }
            try
            {
                Db db = new Db(DbServices.ConnectionString);

                LoanRequestVw       request    = LoanRequestVwServices.Get(RequestId.Value);
                RefundableProductVw refendable = RefundableProductVwServices.Get(RequestId.Value);
                int numOfGuarantorsNeeded      = 0;

                EmployeeProductCalculatorFilter f = new EmployeeProductCalculatorFilter();
                f.EmployeeId = request.RequestProductEmployeeId; f.ProductTypeId = (short)request.RequestProductProductTypeId;
                f.Amount     = (decimal)request.RequestAmount; f.Period = (short)refendable.PaymentPeriod;
                List <EmployeeProductCalculatorResult> result = db.EmployeeProductCalculator(f);
                if (result.Count > 0)
                {
                    numOfGuarantorsNeeded = result[0].GuarantorsCount.Value;
                }

                int numOfGuarantors = GuarantorVwServices.GetByRefundableProductProductId(RequestId.Value).Count;
                if (numOfGuarantors == numOfGuarantorsNeeded)
                {
                    Product p = ProductServices.Get(RequestId.Value, db);
                    p.Amount = Amount.Value;
                    ProductServices.Update(p);
                    Request r = RequestServices.Get(RequestId.Value);
                    r.RequestStatus = (int)RequestStatusEnum.Valid;
                    RequestServices.Update(r);
                }
                else
                {
                    //TempData["Failure"] = ResourceServices.GetString(Cf.Data.Resources.ResourceBase.Culture, "Guarantors", "NumberOfGuarantors");
                    return(ResourceServices.GetString(Cf.Data.Resources.ResourceBase.Culture, "Guarantors", "NumberOfGuarantors"));
                }
            }
            catch (Exception e)
            {
                //EventLog eventLog = DbServices.Log(CurrentUser.Id, db, (int)EventCategoryEnum.Department, (int)EventSourceEnum.Department, (int)EventStatusEnum.Insert, new DepartmentVwXmlFormatter(instance), handler);
                //if (eventLog != null)
                //    throw DbServices.LogError(eventLog, ex, "Department", db);
                //else
                //    throw ex;
            }
            return("Success");// RedirectToAction("ManageNewRequests");
        }
        public JsonResult Calculate(int employeeId, int productTypeId, float amount, int period, float netAmount, float deductions)
        {
            try
            {
                Db db = new Db(DbServices.ConnectionString);
                EmployeeProductCalculatorFilter f = new EmployeeProductCalculatorFilter()
                {
                    EmployeeId    = employeeId,
                    ProductTypeId = (short)productTypeId,
                    Amount        = (decimal)amount,
                    Period        = (short)period
                };

                EmployeeProductCalculatorResult result = db.EmployeeProductCalculatorFirstOrDefault(f);
                if (result == null)
                {
                    return(Json(new { error = "Error", message = "Invalide Id" }, JsonRequestBehavior.AllowGet));
                }
                else
                {
                    GetEmployeeSolvencyFilter filter = new GetEmployeeSolvencyFilter()
                    {
                        EmployeeId  = employeeId,
                        Amount      = (decimal)amount,
                        Date        = System.DateTime.Now,
                        Installment = result.Installment,
                        GrossSalary = (decimal)netAmount,
                        NetSalary   = (decimal)netAmount
                    };
                    GetEmployeeSolvencyResult solvencyResult = db.GetEmployeeSolvencyFirstOrDefault(filter);

                    return(Json(new { Calculator = result, Solevency = solvencyResult }, JsonRequestBehavior.AllowGet));
                }
            }
            catch (CfException cfex)
            {
                TempData["Failure"] = cfex.ErrorDefinition.LocalizedMessage;
            }
            catch (Exception ex)
            {
                TempData["Failure"] = ex.Message;
            }

            return(Json("Error", JsonRequestBehavior.AllowGet));
        }
        public ActionResult Details(int?id)
        {
            ViewBag.TitleGuarantor        = TitleGuarantor;
            ViewBag.TitleExceptionalAount = TitleExceptionalAount;
            ViewBag.ExceptionalIncome     = exceptionalIncome;
            ViewBag.ExceptionalDeduction  = exceptionalDeduction;
            ViewBag.NetDeduction          = netDeduction;

            // Details Of Products
            if (id == null)
            {
                return(RedirectToAction("Index"));
            }

            Db db = new Db(DbServices.ConnectionString);

            // Product
            ProductVwViewModel productVwViewModel = new ProductVwViewModel();

            productVwViewModel.Instance = ProductVwServices.GetChildren(id.Value, db);
            if (productVwViewModel.Instance == null)
            {
                return(HttpNotFound());
            }

            productVwViewModel.RequestVwViewModel.Instance = RequestVwServices.Get(id.Value);
            productVwViewModel.RequestVwViewModel.LoanRequestVwViewModel.Instance = LoanRequestVwServices.Get(id.Value);
            productVwViewModel.RefundableProductVwViewModel.Instance = RefundableProductVwServices.Get(id.Value);

            List <GuarantorVw> Guarantors = GuarantorVwServices.GetByRefundableProductProductId(id.Value);

            productVwViewModel.RefundableProductVwViewModel.GuarantorVwViewModel.List = Guarantors;


            productVwViewModel.RequestVwViewModel.LoanRequestVwViewModel.ExceptionalAmountVwViewModel.List = ExceptionalAmountVwServices.GetByLoanRequestRequestProductId(id.Value);

            List <ExceptionalAmountVw> NetDeduction         = ExceptionalAmountVwServices.GetByLoanRequestRequestProductId(id.Value).Where(c => c.ExceptionalAmountTypeId == (int)ExceptionalAmountTypeEnum.NetDeduction).ToList();
            List <ExceptionalAmountVw> ExceptionalIncome    = ExceptionalAmountVwServices.GetByLoanRequestRequestProductId(id.Value).Where(c => c.ExceptionalAmountTypeId == (int)ExceptionalAmountTypeEnum.ExceptionalIncome).ToList();
            List <ExceptionalAmountVw> ExceptionalDeduction = ExceptionalAmountVwServices.GetByLoanRequestRequestProductId(id.Value).Where(c => c.ExceptionalAmountTypeId == (int)ExceptionalAmountTypeEnum.ExceptionalDeduction).ToList();

            ViewBag.NetDeductionList         = NetDeduction;
            ViewBag.ExceptionalIncomeList    = ExceptionalIncome;
            ViewBag.ExceptionalDeductionList = ExceptionalDeduction;

            // Calculate Solvency and Boundries

            EmployeeProductCalculatorFilter f = new EmployeeProductCalculatorFilter()
            {
                EmployeeId    = productVwViewModel.Instance.EmployeeId,
                ProductTypeId = productVwViewModel.Instance.ProductTypeId,
                Amount        = productVwViewModel.RequestVwViewModel.Instance.Amount,
                Period        = productVwViewModel.RefundableProductVwViewModel.Instance.PaymentPeriod
            };

            EmployeeProductCalculatorResult result = db.EmployeeProductCalculatorFirstOrDefault(f);

            if (result != null)
            {
                GetEmployeeSolvencyFilter filter = new GetEmployeeSolvencyFilter()
                {
                    EmployeeId  = productVwViewModel.Instance.EmployeeId,
                    Amount      = productVwViewModel.RequestVwViewModel.Instance.Amount,
                    Date        = System.DateTime.Now,
                    Installment = result.Installment,
                    GrossSalary = productVwViewModel.RequestVwViewModel.LoanRequestVwViewModel.Instance.NetIncome,
                    NetSalary   = productVwViewModel.RequestVwViewModel.LoanRequestVwViewModel.Instance.NetIncome
                };
                GetEmployeeSolvencyResult solvencyResult = db.GetEmployeeSolvencyFirstOrDefault(filter);
                ViewBag.Calculator = result;
                ViewBag.Solvency   = solvencyResult;
            }


            return(View(productVwViewModel));
        }
        public ActionResult Edit(LoanRequestViewModel model)
        {
            Db db = new Db(DbServices.ConnectionString);

            if (ModelState.IsValid)
            {
                try
                {
                    EmployeeProductCalculatorFilter f = new EmployeeProductCalculatorFilter();
                    f.EmployeeId = model.RequestProduct.Employee; f.ProductTypeId = (short)model.RequestProduct.ProductType;
                    f.Amount     = (decimal)model.Request.Amount; f.Period = (short)model.RequestProductProductRefundableProduct.PaymentPeriod;
                    List <EmployeeProductCalculatorResult> result = db.EmployeeProductCalculator(f);
                    if (result.Count > 0)
                    {
                        model.RequestProductProductRefundableProduct.NetAmount    = result[0].NetAmount.Value;
                        model.RequestProductProductRefundableProduct.ProfitAmount = result[0].ProfitAmount.Value;
                        model.RequestProductProductRefundableProduct.Installment  = result[0].Installment.Value;
                    }
                    // 1- Add Prouct
                    // set the Amount of the Produt the same as Request
                    //model.RequestProduct.Amount = model.Request.Amount;
                    ProductServices.Update(CurrentUser.Id, model.RequestProduct, db);
                    ProductServices.Update(CurrentUser.Id, model.RequestProduct, db);

                    //2-Add Request
                    //model.Request.Product = model.RequestProduct.Id;
                    // model.Request.RequestStatus = (int)RequestStatusEnum.New;
                    //model.Request.Cost = 5;
                    RequestServices.Update(CurrentUser.Id, model.Request, db);

                    //3-Add LoanRequest
                    //model.LoanRequest.Request = model.RequestProduct.Id;
                    LoanRequestServices.Update(CurrentUser.Id, model.LoanRequest, db);

                    //4- Add RefundableProduct

                    //model.RequestProductProductRefundableProduct.Product = model.RequestProduct.Id;
                    RefundableProductServices.Update(CurrentUser.Id, model.RequestProductProductRefundableProduct, db);

                    TempData["Success"] = ResourceServices.GetString(Cf.Data.Resources.ResourceBase.Culture, "UI", "UpdateConfirmed");

                    return(RedirectToAction("Index", "LoanRequest"));
                }
                catch (CfException cfex)
                {
                    TempData["Failure"] = cfex.ErrorDefinition.LocalizedMessage;
                }
                catch (Exception ex)
                {
                    TempData["Failure"] = ex.Message;
                }
            }

            // For Product
            ViewBag.EmployeeList    = new SelectList(EmployeeServices.List(db), "Id", "FirstName", model.RequestProduct.Employee);
            ViewBag.ProductTypeList = new SelectList(ProductTypeServices.List(db), "Id", "Name", model.RequestProduct.ProductType);

            // For Request
            ViewBag.BypassStatusList  = new SelectList(BypassStatusServices.List(db), "Id", "Name", model.Request.BypassStatus);
            ViewBag.ProductList       = new SelectList(ProductServices.List(db), "Id", "Notes", model.Request.Product);
            ViewBag.RequestStatusList = new SelectList(RequestStatusServices.List(db), "Id", "Name", model.Request.RequestStatus);


            return(View(model));
        }
        public ActionResult Create(LoanRequestViewModel model)
        {
            int productId = 0;

            try
            {
                Db db = new Db(DbServices.ConnectionString);

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

                if (ModelState.IsValid)
                {
                    try
                    {
                        EmployeeProductCalculatorFilter f = new EmployeeProductCalculatorFilter();
                        f.EmployeeId = model.RequestProduct.Employee; f.ProductTypeId = (short)model.RequestProduct.ProductType;
                        f.Amount     = (decimal)model.Request.Amount; f.Period = (short)model.RequestProductProductRefundableProduct.PaymentPeriod;
                        List <EmployeeProductCalculatorResult> result = db.EmployeeProductCalculator(f);
                        if (result.Count > 0)
                        {
                            model.RequestProductProductRefundableProduct.NetAmount    = result[0].NetAmount.Value;
                            model.RequestProductProductRefundableProduct.ProfitAmount = result[0].ProfitAmount.Value;
                            model.RequestProductProductRefundableProduct.Installment  = result[0].Installment.Value;
                        }
                        // 1- Add Prouct
                        // set the Amount of the Produt the same as Request
                        model.RequestProduct.Amount = model.Request.Amount;
                        Product p = ProductServices.Insert(CurrentUser.Id, model.RequestProduct, db);
                        productId = p.Id;

                        //2-Add Request
                        model.Request.Product       = p.Id;
                        model.Request.RequestStatus = (int)RequestStatusEnum.New;
                        model.Request.Cost          = 5;
                        Request r = RequestServices.Insert(CurrentUser.Id, model.Request, db);

                        //3-Add LoanRequest
                        model.LoanRequest.Request = p.Id;
                        LoanRequestServices.Insert(CurrentUser.Id, model.LoanRequest, db);

                        //4- Add RefundableProduct

                        model.RequestProductProductRefundableProduct.Product = p.Id;
                        RefundableProductServices.Insert(CurrentUser.Id, model.RequestProductProductRefundableProduct, db);

                        TempData["Success"] = ResourceServices.GetString(Cf.Data.Resources.ResourceBase.Culture, "UI", "InsertConfirmed");
                    }
                    catch (CfException cfex)
                    {
                        TempData["Failure"] = cfex.ErrorDefinition.LocalizedMessage;
                    }
                    catch (Exception ex)
                    {
                        TempData["Failure"] = ex.Message;
                    }
                }

                if (db.Transaction != null)
                {
                    db.Transaction.Commit();
                }
                return(RedirectToAction("Details", new { id = productId }));
            }
            catch (CfException cfex)
            {
                TempData["Failure"] = cfex.ErrorDefinition.LocalizedMessage;
            }
            catch (Exception ex)
            {
                TempData["Failure"] = ex.Message;
            }
            return(View());
        }