Files
rmutr_report/Controllers/SummaryInvest.Controller.cs
kamonwan taengsuk 335c760260
Some checks failed
continuous-integration/drone/push Build is failing
continuous-integration/drone Build is passing
Bug Fixed
2025-03-19 16:02:28 +07:00

341 lines
13 KiB
C#

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
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 };
if (summaryInvest.total_budget != summaryInvest.unit_price)
{
summaryInvest.total_budget = summaryInvest.unit_price;
}
foreach (var summaryInvestYear in summaryInvest.years)
{
if (summaryInvestYear.total_budget != summaryInvestYear.unit_price)
{
summaryInvestYear.total_budget = summaryInvestYear.unit_price;
}
foreach (var plan in summaryInvestYear.plans)
{
if (plan.total_budget != plan.unit_price)
{
plan.total_budget = plan.unit_price;
}
foreach (var project in plan.projects)
{
if (project.total_budget != project.unit_price)
{
project.total_budget = project.unit_price;
}
foreach (var faculty in project.facultys)
{
if (faculty.total_budget != faculty.unit_price)
{
faculty.total_budget = faculty.unit_price;
}
foreach (var assetList in faculty.asset_lists)
{
if (assetList.total != assetList.unit_price)
{
assetList.total = assetList.unit_price;
}
if (assetList.total == null && assetList.amount == 1)
{
assetList.total = assetList.unit_price;
}
if (assetList.amount > 1)
{
assetList.total = assetList.unit_price * assetList.amount;
}
// if ((assetList.unit_price ?? 0) == 0 && (assetList.amount ?? 1) == 1)
// {
// assetList.total = 0;
// }
// else if ((assetList.unit_price ?? 0) > 0 && (assetList.amount ?? 1) > 1)
// {
// assetList.total = assetList.unit_price * assetList.amount;
// }
// else if (assetList.total == null)
// {
// assetList.total = assetList.unit_price;
// }
}
foreach (var constructList in faculty.construct_lists)
{
if (constructList.total != constructList.unit_price)
{
constructList.total = constructList.unit_price;
}
if (constructList.total == null && constructList.amount == 1)
{
constructList.total = constructList.unit_price;
}
if (constructList.amount > 1)
{
constructList.total = constructList.unit_price * constructList.amount;
}
}
}
}
}
}
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)
{
if (summaryInvest.total_budget != summaryInvest.unit_price)
{
summaryInvest.total_budget = summaryInvest.unit_price;
}
foreach (var summaryInvestYear in summaryInvest.years)
{
if (summaryInvestYear.total_budget != summaryInvestYear.unit_price)
{
summaryInvestYear.total_budget = summaryInvestYear.unit_price;
}
foreach (var plan in summaryInvestYear.plans)
{
if (plan.total_budget != plan.unit_price)
{
plan.total_budget = plan.unit_price;
}
if (!plan.asset_lists?.Any() ?? true)
{
plan.asset_lists = plan.construct_lists?.ToList() ?? new List<summary_invest_list_v2>();
}
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;
if (assetList.total != assetList.unit_price)
{
assetList.total = assetList.unit_price;
}
if (assetList.total == null && assetList.amount == 1)
{
assetList.total = assetList.unit_price;
}
if (assetList.amount > 1)
{
assetList.total = assetList.unit_price * assetList.amount;
}
}
}
}
}
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!)
{
if (!plan.asset_lists?.Any() ?? true)
{
plan.asset_lists = plan.construct_lists?.ToList() ?? new List<summary_invest_list_v2>();
}
var validAssets = plan.asset_lists.Where(x => x.unit_price.HasValue && x.unit_price > 0);
if (validAssets.Any())
{
plan.unit_price = validAssets.Sum(x => x.unit_price) ?? 0;
plan.total_budget = validAssets.Sum(x => x.total_budget) ?? 0;
}
else
{
plan.unit_price = 0;
plan.total_budget = 0;
}
if (summaryInvestYear.plans != null && summaryInvestYear.plans.Any())
{
var validPlanAssets = summaryInvestYear.plans.SelectMany(p => p.asset_lists)
.Where(x => x.unit_price.HasValue && x.unit_price > 0);
if (validPlanAssets.Any())
{
summaryInvest.unit_price = validPlanAssets.Sum(x => x.unit_price) ?? 0;
summaryInvest.total_budget = validPlanAssets.Sum(x => x.total_budget) ?? 0;
summaryInvestYear.unit_price = validPlanAssets.Sum(x => x.unit_price) ?? 0;
summaryInvestYear.total_budget = validPlanAssets.Sum(x => x.total_budget) ?? 0;
}
else
{
summaryInvest.unit_price = 0;
summaryInvest.total_budget = 0;
summaryInvestYear.unit_price = 0;
summaryInvestYear.total_budget = 0;
}
}
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 + " ";
}
assetList.file2 = formattedFile.Trim();
}
}
}
}
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");
}
return Ok();
}
}
}