public byte[] GenerateScenarioConfigurationReportAsync(Model2MLResponse modelResult)
        {
            DataSet ds = new DataSet();

            ds.Tables.Add(JsonConvert.DeserializeObject <DataTable>(JsonConvert.SerializeObject(modelResult.objToOptimize.cropsSelected)));
            ReportDataSource rds1 = new ReportDataSource {
                Name = "CropsToOptimizeDataSet", Value = ds.Tables[0]
            };

            ds = new DataSet();
            ds.Tables.Add(JsonConvert.DeserializeObject <DataTable>(JsonConvert.SerializeObject(modelResult.objToOptimize.locationsSelected)));
            ReportDataSource rds2 = new ReportDataSource {
                Name = "LocationsToOptimizeDataSet", Value = ds.Tables[0]
            };

            List <string> cropsSelected = new List <string>();

            foreach (CropsConfigurationModel crop in modelResult.objToOptimize.cropsSelected)
            {
                cropsSelected.Add(crop.crop);
            }

            //string locationSelected = modelResult.objToOptimize.locationSelected.;
            List <string> locationSelected = new List <string>();

            foreach (LocationsConfigurationModel location in modelResult.objToOptimize.locationsSelected)
            {
                locationSelected.Add(location.location);
            }

            ReportParameter[] parametros =
            {
                new ReportParameter("locationSelected", String.Join(", ", locationSelected)),
                new ReportParameter("cropsSelected",    String.Join(", ", cropsSelected))
            };

            LocalReport lr         = new LocalReport();
            var         Reportpath = @".\Reports\ScenarioConfiguration.rdlc";

            lr.ReportPath  = Reportpath;
            lr.DisplayName = "Scenario Configuration.pdf";

            lr.SetParameters(parametros);
            lr.DataSources.Add(rds1);
            lr.DataSources.Add(rds2);

            var    reportType = "pdf";
            string mimeType;
            string encoding;
            string fileNameExtension;

            Warning[] warnings;
            string[]  streams;
            var       renderedBytes = lr.Render(reportType, null, out mimeType, out encoding, out fileNameExtension, out streams, out warnings);

            return(renderedBytes);
        }
        public byte[] GenerateLaborResultReportAsync(Model2MLResponse modelResult)
        {
            List <string> locationSelected = new List <string>();

            foreach (LocationsConfigurationModel location in modelResult.objToOptimize.locationsSelected)
            {
                locationSelected.Add(location.location);
            }

            List <byte[]> docs = new List <byte[]>();

            locationSelected.ForEach(location =>
            {
                DataSet ds = new DataSet();
                ds         = new DataSet();
                ds.Tables.Add(JsonConvert.DeserializeObject <DataTable>(JsonConvert.SerializeObject(modelResult.listLaborResult.listLaborPerWeeks.FindAll(lp => lp.location == location))));
                ReportDataSource rds = new ReportDataSource {
                    Name = "LaborDataSet2", Value = ds.Tables[0]
                };

                ds = new DataSet();
                ds.Tables.Add(JsonConvert.DeserializeObject <DataTable>(JsonConvert.SerializeObject(modelResult.listLaborResult.listLaborRequirementSummary.FindAll(lp => lp.location == location))));
                ReportDataSource rds2 = new ReportDataSource {
                    Name = "LaborDataSet", Value = ds.Tables[0]
                };

                ReportParameter[] parametros =
                {
                    new ReportParameter("locationSelected", location)
                };

                LocalReport lr = new LocalReport();
                var Reportpath = @".\Reports\LaborResult.rdlc";
                lr.ReportPath  = Reportpath;
                lr.DisplayName = "Labor Result.pdf";

                lr.SetParameters(parametros);
                lr.DataSources.Add(rds);
                lr.DataSources.Add(rds2);

                var reportType = "pdf";
                string mimeType;
                string encoding;
                string fileNameExtension;
                Warning[] warnings;
                string[] streams;
                var renderedBytes = lr.Render(reportType, null, out mimeType, out encoding, out fileNameExtension, out streams, out warnings);
                //return renderedBytes;
                docs.Add(renderedBytes);
            });

            byte[] final = docs.SelectMany(a => a).ToArray();

            return(concatAndAddContent(docs));
        }
        public byte[] GenerateEconomicAnalysisReportAsync(Model2MLResponse modelResult)
        {
            DataSet ds = new DataSet();

            ds = new DataSet();
            ds.Tables.Add(JsonConvert.DeserializeObject <DataTable>(JsonConvert.SerializeObject(modelResult.incomeCostResult.listIncomeCostResultByCrops)));
            ReportDataSource rds1 = new ReportDataSource {
                Name = "IncomesByCropsDataSet", Value = ds.Tables[0]
            };

            ds = new DataSet();
            ds.Tables.Add(JsonConvert.DeserializeObject <DataTable>(JsonConvert.SerializeObject(modelResult.incomeCostResult.listIncomeCostResultByLocations)));
            ReportDataSource rds2 = new ReportDataSource {
                Name = "IncomesByLocationsDataSet", Value = ds.Tables[0]
            };

            ReportParameter[] parametros =
            {
                new ReportParameter("incomes",      modelResult.incomeCostResult.incomes.ToString()),
                new ReportParameter("plantingCost", modelResult.incomeCostResult.plantCost.ToString()),
                new ReportParameter("laborCost",    modelResult.incomeCostResult.laborCost.ToString())
            };

            LocalReport lr         = new LocalReport();
            var         Reportpath = @".\Reports\EconomicAnalysis.rdlc";

            lr.ReportPath  = Reportpath;
            lr.DisplayName = "Economic Analysis.pdf";

            lr.SetParameters(parametros);
            lr.DataSources.Add(rds1);
            lr.DataSources.Add(rds2);

            var    reportType = "pdf";
            string mimeType;
            string encoding;
            string fileNameExtension;

            Warning[] warnings;
            string[]  streams;
            var       renderedBytes = lr.Render(reportType, null, out mimeType, out encoding, out fileNameExtension, out streams, out warnings);

            return(renderedBytes);
        }
Exemplo n.º 4
0
        public ActionResult CreateReportModel2ML([FromBody] Model2MLResponse modelResult)
        {
            try
            {
                var _reportService = new Services.ReportService();
                var scenarioConfigurationBytesReport = _reportService.GenerateScenarioConfigurationReportAsync(modelResult);
                //ActionResult document = File(returnString, System.Net.Mime.MediaTypeNames.Application.Octet, "Scenario Configuration.pdf");

                var economicAnalysisBytesReport = _reportService.GenerateEconomicAnalysisReportAsync(modelResult);
                //ActionResult documentEA = File(returnString2, System.Net.Mime.MediaTypeNames.Application.Octet, "Economic Analysis.pdf");

                var contractResponseBytesReport = _reportService.GenerateContractsResponseReportAsync(modelResult);

                var plantingDecisionsBytesReport = _reportService.GeneratePlantingDecisionsReportAsync(modelResult);

                var expectedHarvestingBytesReport = _reportService.GenerateExpectedHarvestingReportAsync(modelResult);

                var laborResultBytesReport = _reportService.GenerateLaborResultReportAsync(modelResult);

                using (MemoryStream ms = new MemoryStream())
                {
                    using (var archive = new ZipArchive(ms, ZipArchiveMode.Create, true))
                    {
                        var zipArchiveEntry = archive.CreateEntry("1.Scenario Configuration.pdf", CompressionLevel.Fastest);
                        using (var zipStream = zipArchiveEntry.Open()) zipStream.Write(scenarioConfigurationBytesReport, 0, scenarioConfigurationBytesReport.Length);
                        zipArchiveEntry = archive.CreateEntry("2.Economic Analysis.pdf", CompressionLevel.Fastest);
                        using (var zipStream = zipArchiveEntry.Open()) zipStream.Write(economicAnalysisBytesReport, 0, economicAnalysisBytesReport.Length);
                        zipArchiveEntry = archive.CreateEntry("3.Contract Response.pdf", CompressionLevel.Fastest);
                        using (var zipStream = zipArchiveEntry.Open()) zipStream.Write(contractResponseBytesReport, 0, contractResponseBytesReport.Length);
                        zipArchiveEntry = archive.CreateEntry("4.Planting Decisions.pdf", CompressionLevel.Fastest);
                        using (var zipStream = zipArchiveEntry.Open()) zipStream.Write(plantingDecisionsBytesReport, 0, plantingDecisionsBytesReport.Length);
                        zipArchiveEntry = archive.CreateEntry("5.Expected Harvesting.pdf", CompressionLevel.Fastest);
                        using (var zipStream = zipArchiveEntry.Open()) zipStream.Write(expectedHarvestingBytesReport, 0, expectedHarvestingBytesReport.Length);
                        zipArchiveEntry = archive.CreateEntry("6.Labor Result.pdf", CompressionLevel.Fastest);
                        using (var zipStream = zipArchiveEntry.Open()) zipStream.Write(laborResultBytesReport, 0, laborResultBytesReport.Length);
                    }
                    //return File(ms.ToArray(), "application/zip", "Result Data.zip");

                    ActionResult document = File(ms.ToArray(), "application/zip", "Result Data.zip");

                    DocumentResponseModel response = new DocumentResponseModel()
                    {
                        hasError     = false,
                        messageError = null,
                        document     = document
                    };

                    return(Content(JsonConvert.SerializeObject(response), "application/json"));
                }
            }
            catch (Exception ex)
            {
                DocumentResponseModel response = new DocumentResponseModel()
                {
                    hasError     = true,
                    messageError = ex.Message,
                    document     = null
                };
                return(Content(JsonConvert.SerializeObject(response), "application/json"));
            }
        }
        public byte[] GenerateContractsResponseReportAsync(Model2MLResponse modelResult)
        {
            DataSet ds = new DataSet();

            ds = new DataSet();
            ds.Tables.Add(JsonConvert.DeserializeObject <DataTable>(JsonConvert.SerializeObject(modelResult.ContractsResult.listLandAvailableResult)));
            ReportDataSource rds1 = new ReportDataSource {
                Name = "LandAvailableDataSet", Value = ds.Tables[0]
            };

            LocalReport lr         = new LocalReport();
            var         Reportpath = @".\Reports\ContractHead.rdlc";

            lr.ReportPath  = Reportpath;
            lr.DisplayName = "Contract Response.pdf";

            //lr.SetParameters(parametros);
            lr.DataSources.Add(rds1);
            //lr.DataSources.Add(rds2);

            var    reportType = "pdf";
            string mimeType;
            string encoding;
            string fileNameExtension;

            Warning[] warnings;
            string[]  streams;
            var       renderedBytes = lr.Render(reportType, null, out mimeType, out encoding, out fileNameExtension, out streams, out warnings);
            //return renderedBytes;

            List <string> cropsSelected = new List <string>();
            int           indexContent  = 0;

            foreach (CropsConfigurationModel crop in modelResult.ContractsResult.cropsSelected)
            {
                cropsSelected.Add(crop.crop);
            }

            List <byte[]> docs = new List <byte[]>();

            docs.Add(renderedBytes);

            cropsSelected.ForEach(c => {
                indexContent++;
                DataSet ds2 = new DataSet();
                ds2         = new DataSet();
                ds2.Tables.Add(JsonConvert.DeserializeObject <DataTable>(JsonConvert.SerializeObject(modelResult.ContractsResult.listContractsResult.FindAll(lcr => lcr.crop == c))));
                ReportDataSource rds2 = new ReportDataSource {
                    Name = "ContractForCropByLocationsDataSet", Value = ds2.Tables[0]
                };


                ReportParameter[] parametros =
                {
                    new ReportParameter("cropSelected", c),
                    new ReportParameter("indexContent", indexContent.ToString())
                };

                lr             = new LocalReport();
                Reportpath     = @".\Reports\ContractByCrop.rdlc";
                lr.ReportPath  = Reportpath;
                lr.DisplayName = "Contract Response.pdf";

                lr.SetParameters(parametros);
                lr.DataSources.Add(rds2);

                var reportType2 = "pdf";
                string mimeType2;
                string encoding2;
                string fileNameExtension2;
                Warning[] warnings2;
                string[] streams2;
                renderedBytes = lr.Render(reportType2, null, out mimeType2, out encoding2, out fileNameExtension2, out streams2, out warnings2);

                docs.Add(renderedBytes);
            });

            byte[] final = docs.SelectMany(a => a).ToArray();

            return(concatAndAddContent(docs));
        }