Ejemplo n.º 1
0
        public ActionResult GetReportSummaryDependantList([DataSourceRequest] DataSourceRequest request, Hre_ReportSummaryDependantModel model)
        {
            string status = string.Empty;
            var service = new BaseService();
            HeaderInfo headerInfo1 = new HeaderInfo() { Name = "MonthOfExpiry", Value = model.MonthOfExpiry };
            List<HeaderInfo> listHeaderInfo = new List<HeaderInfo>() { headerInfo1 };
            ListQueryModel lstModel = new ListQueryModel
            {
                PageSize = int.MaxValue - 1,
                PageIndex = 1,
                Filters = ExtractFilterAttributes(request),
                Sorts = ExtractSortAttributes(request),
                AdvanceFilters = ExtractAdvanceFilterAttributes(model)
            };

            var ReportServices = new Hre_ReportServices();
            var actionService = new ActionService(UserLogin);
            List<object> paraDependant = new List<object>();
            paraDependant.AddRange(new object[11]);
            paraDependant[0] = model.ProfileName;
            paraDependant[1] = model.CodeEmp;
            paraDependant[2] = model.OrgStructureID;
            paraDependant[3] = model.JobTitleID;
            paraDependant[4] = model.PositionID;
            paraDependant[5] = model.DependantName;
            paraDependant[6] = model.RelationID;
            paraDependant[7] = null;
            paraDependant[8] = null;
            paraDependant[9] = 1;
            paraDependant[10] = int.MaxValue - 1;
            var lstDependant = actionService.GetData<Hre_DependantEntity>(paraDependant, ConstantSql.hrm_hr_sp_get_Dependant, ref status);

            if (model.MonthOfExpiry != null && lstDependant != null)
            {
                lstDependant = lstDependant.Where(s => (s.MonthOfEffect <= model.MonthOfExpiry && model.MonthOfExpiry <= s.MonthOfExpiry)
                    || (s.MonthOfEffect <= model.MonthOfExpiry && s.MonthOfExpiry == null)).ToList();
            }

            var result = ReportServices.GetReportSummaryDependant(lstDependant, model.IsCreateTemplate);

            var isDataTable = false;
            object obj = new DataTable();
            if (model.IsCreateTemplateForDynamicGrid)
            {
                var col = result.Columns.Count;
                result.Columns.RemoveAt(col - 1);
                obj = result;
                isDataTable = true;
            }
            if (model != null && model.IsCreateTemplate)
            {
                var path = Common.GetPath("Templates");
                ExportService exportService = new ExportService();
                ConfigExport cfgExport = new ConfigExport()
                {
                    Object = obj,
                    FileName = "Hre_ReportSummaryDependantEntity",
                    OutPutPath = path,
                    HeaderInfo = listHeaderInfo,
                    DownloadPath = Hrm_Main_Web + "Templates",
                    IsDataTable = isDataTable

                };
                var str = exportService.CreateTemplate(cfgExport);
                return Json(str);
            }

            #region Validate
            string message = string.Empty;
            var checkValidate = ValidatorService.OnValidateData<Hre_ReportSummaryDependantModel>(model, "Hre_ReportSummaryDependant", ref message);
            if (!checkValidate)
            {
                return Json(message, JsonRequestBehavior.AllowGet);
            }
            #endregion

            if (model.ExportId != Guid.Empty)
            {
                string fullPath = "";
                if (model.MonthOfExpiry != null)
                {
                    fullPath = ExportService.Export(model.ExportId, result, listHeaderInfo, model.ExportType);
                }
                else
                {
                    fullPath = ExportService.Export(model.ExportId, result, null, model.ExportType);
                }

                return Json(fullPath);
            }
            #region mapping dataTable to dataList
            List<Hre_ReportSummaryDependantModel> dataList = new List<Hre_ReportSummaryDependantModel>();
            Hre_ReportSummaryDependantModel aTSource = null;

            if (result.Rows.Count > 0)
            {
                const BindingFlags flags = BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic;
                var objFieldNames = (from PropertyInfo aProp in typeof(Hre_ReportSummaryDependantModel).GetProperties(flags)
                                     select new
                                     {
                                         Name = aProp.Name,
                                         Type = Nullable.GetUnderlyingType(aProp.PropertyType) ?? aProp.PropertyType
                                     }).ToList();
                var dataTblFieldNames = (from DataColumn aHeader in result.Columns
                                         select new { Name = aHeader.ColumnName, Type = aHeader.DataType }).ToList();
                var commonFields = objFieldNames.Intersect(dataTblFieldNames).ToList();
                foreach (DataRow dataRow in result.AsEnumerable().ToList())
                {
                    aTSource = new Hre_ReportSummaryDependantModel();
                    foreach (var aField in commonFields)
                    {
                        PropertyInfo propertyInfos = aTSource.GetType().GetProperty(aField.Name);
                        if (dataRow[aField.Name] == DBNull.Value)
                            continue;
                        propertyInfos.SetValue(aTSource, dataRow[aField.Name], null);
                    }
                    dataList.Add(aTSource);
                }
            }
            #endregion
            return Json(dataList.ToDataSourceResult(request));
        }