Files
rmutr_report/Controllers/SummaryInvest.Controller.cs
kamonwan taengsuk 106a978269
All checks were successful
continuous-integration/drone/push Build is passing
Bug Fixed
2024-09-19 13:09:51 +07:00

238 lines
9.4 KiB
C#

using System;
using System.Collections.Generic;
using System.IO;
using System.Text.RegularExpressions;
using FastReport;
using FastReport.Export.OoXML;
using FastReport.Export.Pdf;
using Microsoft.AspNetCore.Mvc;
using rmutr_report.Models;
using Swashbuckle.AspNetCore.Annotations;
namespace rmutr_report.Controllers
{
[SwaggerTag("สำหรับรายงาน invest")]
public class SummaryInvest : Controller
{
readonly Setting _setting;
public SummaryInvest(Setting setting)
{
_setting = setting;
}
[SwaggerOperation("จัดทำค่าครุภัณฑ์ Key e-Budgeting")]
[HttpPost, Route("reports/summary_invest/{type}")]
[ApiExplorerSettings(GroupName = "reports")]
public IActionResult GetEBudgetingReport([FromRoute] string type, [FromBody] summary_invest summaryInvest)
{
var summaryInvests = new List<summary_invest>() { summaryInvest };
foreach (var summaryInvestYear in summaryInvest.years)
{
foreach (var plan in summaryInvestYear.plans)
{
if (plan.total_budget<=10)
{
plan.total_budget = plan.unit_price;
}
foreach (var project in plan.projects)
{
if (project.total_budget<=10)
{
project.total_budget = project.unit_price;
}
foreach (var faculty in project.facultys)
{
if (faculty.total_budget<=10)
{
faculty.total_budget = faculty.unit_price;
}
foreach (var assetList in faculty.asset_lists)
{
if (assetList.total<=10)
{
assetList.total = assetList.unit_price;
}
if (assetList.total==null&&assetList.amount==1)
{
assetList.total = assetList.unit_price;
}
}
}
}
}
}
Report report = new Report();
report.Load(_setting.report_path + "summary_invest.frx");
report.RegisterData(summaryInvests, "summary_invest");
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.openxmlformats-officedocument.spreadsheetml.sheet",
"summary_invest" + ".xlsx");
break;
}
return Ok();
}
[SwaggerOperation("จัดทำค่าครุภัณฑ์ (ใบเสนอราคา)")]
[HttpPost, Route("reports/summary_invest_preview/{type}")]
[ApiExplorerSettings(GroupName = "reports")]
public IActionResult GetSummaryInvestPreviewReport([FromRoute] string type,
[FromBody] summary_invest_v2 summaryInvest)
{
var summaryInvests = new List<summary_invest_v2>() { summaryInvest };
if (summaryInvest != null)
{
foreach (var summaryInvestYear in summaryInvest.years)
{
foreach (var plan in summaryInvestYear.plans)
{
foreach (var assetList in plan.asset_lists)
{
string formattedSeller = assetList.seller.Replace("<br>", " ");
string pattern = @"<a href=.*?>(.*?)<\/a>";
MatchCollection matches = Regex.Matches(assetList.file, pattern);
string formattedFile = "";
if (assetList.is_approve == true)
{
assetList.approve_name = "อนุมัติแล้ว";
}
assetList.seller2 = formattedSeller;
foreach (Match match in matches)
{
formattedFile += match.Groups[1].Value + " ";
}
formattedFile = formattedFile.Trim();
assetList.file2 = formattedFile;
//Console.WriteLine("seller output: " + formattedSeller);
//Console.WriteLine("file output: " + formattedFile);
}
}
}
}
Report report = new Report();
report.Load(_setting.report_path + "summary_invest_preview.frx");
report.RegisterData(summaryInvests, "summary_invest");
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.openxmlformats-officedocument.spreadsheetml.sheet",
"summary_invest_preview" + ".xlsx");
break;
}
return Ok();
}
[SwaggerOperation("จัดสรรค่าครุภัณฑ์")]
[HttpPost, Route("reports/summary_invest_approve/{type}")]
[ApiExplorerSettings(GroupName = "reports")]
public IActionResult GetSummaryInvestApproveReport([FromRoute] string type,
[FromBody] summary_invest_v2 summaryInvest)
{
var summaryInvests = new List<summary_invest_v2>() { summaryInvest };
if (summaryInvest != null)
{
foreach (var summaryInvestYear in summaryInvest.years)
{
foreach (var plan in summaryInvestYear.plans)
{
foreach (var assetList in plan.asset_lists)
{
string formattedSeller = assetList.seller.Replace("<br>", " ");
string pattern = @"<a href=.*?>(.*?)<\/a>";
MatchCollection matches = Regex.Matches(assetList.file, pattern);
string formattedFile = "";
if (assetList.is_approve == true)
{
assetList.approve_name = "อนุมัติแล้ว";
}
assetList.seller2 = formattedSeller;
foreach (Match match in matches)
{
formattedFile += match.Groups[1].Value + " ";
}
formattedFile = formattedFile.Trim();
assetList.file2 = formattedFile;
//Console.WriteLine("seller output: " + formattedSeller);
//Console.WriteLine("file output: " + formattedFile);
}
}
}
}
Report report = new Report();
report.Load(_setting.report_path + "summary_invest_approve.frx");
report.RegisterData(summaryInvests, "summary_invest");
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.openxmlformats-officedocument.spreadsheetml.sheet",
"summary_invest_approve" + ".xlsx");
break;
}
return Ok();
}
}
}