using System; using System.Collections.Generic; using System.IO; using System.Linq; using FastReport; using FastReport.Export.Csv; using FastReport.Export.Mht; using FastReport.Export.OoXML; using FastReport.Export.Pdf; using Microsoft.AspNetCore.Mvc; using rmutr_report.Models; using rmutr_report.Models.Hr; using Swashbuckle.AspNetCore.Annotations; namespace rmutr_report.Controllers { [SwaggerTag("สำหรับรายงาน_budget")] public class Budget: Controller { readonly Setting _setting; public Budget(Setting setting) { this._setting = setting; } [HttpPost, Route("reports/budget_report/{type}")] [ApiExplorerSettings(GroupName = "reports")] public IActionResult GetSum1Report([FromRoute] string type, [FromBody] List budget_reports) { foreach (var x in budget_reports) { int sum1 = budget_reports.Sum(g => int.Parse(g.salaya)); x.sum1 = sum1; int sum2 = budget_reports.Sum(g => int.Parse(g.bophitphimuk)); x.sum2 = sum2; int sum3 = budget_reports.Sum(g => int.Parse(g.pohchang)); x.sum3 = sum3; int sum4 = budget_reports.Sum(g => int.Parse(g.klai_kangwon)); x.sum4 = sum4; int sum5 = budget_reports.Sum(g => int.Parse(g.total)); x.sum5 = sum5; } Report report = new Report(); report.Load(_setting.report_path + "budget_report.frx"); report.RegisterData(budget_reports, "budget_report"); report.Prepare(); MemoryStream stream = new MemoryStream(); switch (type) { case "pdf": PDFExport pdf = new PDFExport(); report.Export(pdf, stream); stream.Seek(0, SeekOrigin.Begin); return File(stream, "application/pdf"); case "xls": Excel2007Export excel = new Excel2007Export(); report.Export(excel, stream); stream.Seek(0, SeekOrigin.Begin); return File(stream, "application/vnd.ms-excel"); break; case "mht": MHTExport mht = new MHTExport(); report.Export(mht, stream); stream.Seek(0, SeekOrigin.Begin); return File(stream, "multipart/related"); break; case "csv": CSVExport csv = new CSVExport(); report.Export(csv, stream); stream.Seek(0, SeekOrigin.Begin); return File(stream, "text/csv"); break; } return Ok(); } [HttpPost, Route("reports/budget_summary_report/{type}")] [ApiExplorerSettings(GroupName = "reports")] public IActionResult GetSumReport([FromRoute] string type, [FromBody] budget_summary_report budget_summary_reports) { var _budget_summary_report = new List() { budget_summary_reports }; var s1 = budget_summary_reports.summary.Sum(d => d.budget_1); var s2 = budget_summary_reports.summary.Sum(d => d.budget_2); var s3 = budget_summary_reports.summary.Sum(d => d.budget_3); var s4 = budget_summary_reports.summary.Sum(d => d.budget_4); var s5 = budget_summary_reports.summary.Sum(d => d.budget_5); if (s1 != null || s2 != null || s3 != null || s4 != null || s5 != null) { var s6 = s1 + s2 + s3 + s4 + s5; budget_summary_reports.total_budget = s6; } if (s1!=null || s2!=null||s3 !=null|| s4!= null|| s5!=null) { budget_summary_reports.budget_1 = s1; budget_summary_reports.budget_2 = s2; budget_summary_reports.budget_3 = s3; budget_summary_reports.budget_4 = s4; budget_summary_reports.budget_5 = s5; //budget_summary_reports.total_budget = s6; } if (s1==null) { budget_summary_reports.budget_1 = null; } if (s2==null) { budget_summary_reports.budget_2 = null; } if (s3==null) { budget_summary_reports.budget_3 = null; } if (s4==null) { budget_summary_reports.budget_4 = null; } if (s5==null) { budget_summary_reports.budget_5 = null; } foreach (var budget in budget_summary_reports.summary) { if (budget.budget_1 != null || budget.budget_2 != null || budget.budget_3 != null || budget.budget_4 != null || budget.budget_5 != null) { budget.total_budget = budget.budget_1 + budget.budget_2 + budget.budget_3 + budget.budget_4 + budget.budget_5; } else { return null; } } Report report = new Report(); report.Load(_setting.report_path + "budget_summary_report.frx"); report.RegisterData(_budget_summary_report, "budget_summary_report"); report.Prepare(); MemoryStream stream = new MemoryStream(); switch (type) { case "pdf": PDFExport pdf = new PDFExport(); report.Export(pdf, stream); stream.Seek(0, SeekOrigin.Begin); return File(stream, "application/pdf"); case "xls":case "xlsx": Excel2007Export excel = new Excel2007Export(); report.Export(excel, stream); stream.Seek(0, SeekOrigin.Begin); return File(stream, "application/vnd.ms-excel"); break; case "mht": MHTExport mht = new MHTExport(); report.Export(mht, stream); stream.Seek(0, SeekOrigin.Begin); return File(stream, "multipart/related"); break; case "csv": CSVExport csv = new CSVExport(); report.Export(csv, stream); stream.Seek(0, SeekOrigin.Begin); return File(stream, "text/csv"); break; } return Ok(); } } }