Example #1
0
        public async Task <OperationDataResult <FileStreamModel> > GenerateSingleListResults(
            ItemListCasesPnRequestModel requestModel)
        {
            string excelFile = null;

            try
            {
                var reportDataResult = await GetTableData(requestModel);

                if (reportDataResult == null)
                {
                    return(new OperationDataResult <FileStreamModel>(false, "ERROR"));
                }

                var itemList = await _dbContext.ItemLists.SingleOrDefaultAsync(x => x.Id == requestModel.ListId);

                excelFile = _excelService.CopyTemplateForNewAccount("report_template_lists");
                var writeResult = _excelService.WriteTableToExcel(itemList.Name, itemList.Description,
                                                                  reportDataResult,
                                                                  requestModel,
                                                                  excelFile);

                if (!writeResult)
                {
                    throw new Exception($"Error while writing excel file {excelFile}");
                }

                var result = new FileStreamModel()
                {
                    FilePath   = excelFile,
                    FileStream = new FileStream(excelFile, FileMode.Open),
                };

                return(new OperationDataResult <FileStreamModel>(true, result));
            }
            catch (Exception e)
            {
                if (!string.IsNullOrEmpty(excelFile) && File.Exists(excelFile))
                {
                    File.Delete(excelFile);
                }

                Trace.TraceError(e.Message);
                //_logger.LogError(e.Message);
                return(new OperationDataResult <FileStreamModel>(
                           false,
                           _itemsPlanningLocalizationService.GetString("ErrorWhileGeneratingReportFile")));
            }
        }
        public async Task <OperationDataResult <FileStreamModel> > GenerateReportFile(GenerateReportModel model)
        {
            string excelFile = null;

            try
            {
                OperationDataResult <ReportModel> reportDataResult = await GenerateReport(model);

                if (!reportDataResult.Success)
                {
                    return(new OperationDataResult <FileStreamModel>(false, reportDataResult.Message));
                }

                excelFile = _excelService.CopyTemplateForNewAccount("report_template");
                bool writeResult = _excelService.WriteRecordsExportModelsToExcelFile(
                    reportDataResult.Model,
                    model,
                    excelFile);

                if (!writeResult)
                {
                    throw new Exception($"Error while writing excel file {excelFile}");
                }

                FileStreamModel result = new FileStreamModel()
                {
                    FilePath   = excelFile,
                    FileStream = new FileStream(excelFile, FileMode.Open),
                };

                return(new OperationDataResult <FileStreamModel>(true, result));
            }
            catch (Exception e)
            {
                if (!string.IsNullOrEmpty(excelFile) && File.Exists(excelFile))
                {
                    File.Delete(excelFile);
                }

                Trace.TraceError(e.Message);
                _logger.LogError(e.Message);
                return(new OperationDataResult <FileStreamModel>(
                           false,
                           _itemsPlanningLocalizationService.GetString("ErrorWhileGeneratingReportFile")));
            }
        }
Example #3
0
        public async Task <OperationDataResult <FileStreamModel> > GenerateReportFile(GenerateReportModel model)
        {
            string excelFile = null;

            try
            {
                OperationDataResult <ReportModel> reportDataResult = await GenerateReport(model);

                if (!reportDataResult.Success)
                {
                    return(new OperationDataResult <FileStreamModel>(false, reportDataResult.Message));
                }

                string outerResourceName = "";
                string innerResourceName = "";
                try
                {
                    outerResourceName = _dbContext.PluginConfigurationValues.SingleOrDefault(x => x.Name == "OuterInnerResourceSettings:OuterResourceName")?.Value;
                    innerResourceName = _dbContext.PluginConfigurationValues.SingleOrDefault(x => x.Name == "OuterInnerResourceSettings:InnerResourceName")?.Value;
                }
                catch
                {
                }

                switch (reportDataResult.Model.Relationship)
                {
                case ReportRelationshipType.Employee:
                    reportDataResult.Model.HumanReadableName =
                        _outerInnerResourceLocalizationService.GetString("Employee");
                    break;

                case ReportRelationshipType.InnerResource:
                    reportDataResult.Model.HumanReadableName = innerResourceName;
                    break;

                case ReportRelationshipType.OuterResource:
                    reportDataResult.Model.HumanReadableName = outerResourceName;
                    break;

                case ReportRelationshipType.EmployeeInnerResource:
                    reportDataResult.Model.HumanReadableName =
                        _outerInnerResourceLocalizationService.GetString("Employee") + "-" + innerResourceName;
                    break;

                case ReportRelationshipType.EmployeeOuterResource:
                    reportDataResult.Model.HumanReadableName =
                        _outerInnerResourceLocalizationService.GetString("Employee") + "-" + outerResourceName;
                    break;

                case ReportRelationshipType.EmployeeTotal:
                    reportDataResult.Model.HumanReadableName =
                        _outerInnerResourceLocalizationService.GetString("Employee" + "-Total");
                    break;

                case ReportRelationshipType.OuterInnerResource:
                    reportDataResult.Model.HumanReadableName =
                        $"{outerResourceName} {innerResourceName}";
                    break;

                case ReportRelationshipType.InnerOuterResource:
                    reportDataResult.Model.HumanReadableName =
                        $"{innerResourceName} {outerResourceName}";
                    break;
                }

                excelFile = _excelService.CopyTemplateForNewAccount("report_template");
                bool writeResult = _excelService.WriteRecordsExportModelsToExcelFile(
                    reportDataResult.Model,
                    model,
                    excelFile);

                if (!writeResult)
                {
                    throw new Exception($"Error while writing excel file {excelFile}");
                }

                FileStreamModel result = new FileStreamModel()
                {
                    FilePath   = excelFile,
                    FileStream = new FileStream(excelFile, FileMode.Open),
                };

                return(new OperationDataResult <FileStreamModel>(true, result));
            }
            catch (Exception e)
            {
                if (!string.IsNullOrEmpty(excelFile) && File.Exists(excelFile))
                {
                    File.Delete(excelFile);
                }

                Trace.TraceError(e.Message);
                _logger.LogError(e.Message);
                return(new OperationDataResult <FileStreamModel>(
                           false,
                           _outerInnerResourceLocalizationService.GetString("ErrorWhileGeneratingReportFile")));
            }
        }