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 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 JsonResult GetEmployeeSolvency(int employeeId, float amount, float installment, float netSalary, float grossSalary)
        {
            try
            {
                // get employee information

                EmployeeVw employee = EmployeeVwServices.Get(employeeId);
                if (employee == null)
                {
                    return(Json(new { message = "الرقم الذاتي غير صحيح" }, JsonRequestBehavior.AllowGet));
                }

                GetEmployeeSolvencyFilter filter = new GetEmployeeSolvencyFilter()
                {
                    EmployeeId  = employeeId,
                    Amount      = (decimal)amount,
                    Date        = System.DateTime.Now,
                    Installment = (decimal)installment,
                    GrossSalary = (decimal)grossSalary,
                    NetSalary   = (decimal)netSalary
                };
                GetEmployeeSolvencyResult solvencyResult = db.GetEmployeeSolvencyFirstOrDefault(filter);

                return(Json(new { Employee = employee, 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 CreateGuarantorWithStatement(GuarantorWithStatmentViewModel model)
        {
            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
                    {
                        model.Guarantor.GuarantorStatus = (int)GuarantorStatusEnum.New;
                        // 1- Add Guaratntor
                        Guarantor g = GuarantorServices.Insert(CurrentUser.Id, model.Guarantor, db);

                        //2-Add GuarantorStatement
                        model.GuarantorStatement.Guarantor = g.Id;
                        GuarantorStatement gs = GuarantorStatementServices.Insert(CurrentUser.Id, model.GuarantorStatement, db);

                        Request           r  = db.RequestGet(model.Guarantor.RefundableProduct);
                        RefundableProduct rp = db.RefundableProductGet(model.Guarantor.RefundableProduct);

                        //Calculate Solvency
                        GetEmployeeSolvencyFilter filter = new GetEmployeeSolvencyFilter()
                        {
                            EmployeeId  = model.Guarantor.Employee,
                            Amount      = r.Amount,
                            Date        = r.Date,
                            Installment = rp.Installment,
                            GrossSalary = model.GuarantorStatement.GrossSalary,
                            NetSalary   = model.GuarantorStatement.NetSalary
                        };
                        GetEmployeeSolvencyResult solvencyResult = db.GetEmployeeSolvencyFirstOrDefault(filter);

                        // update notes field at Guarantor
                        string result = getResult(solvencyResult);
                        g.Notes = result;
                        GuarantorServices.Update(CurrentUser.Id, g, 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 = model.Guarantor.RefundableProduct }));
            }
            catch
            {
                return(View("Details"));
            }
        }
        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));
        }