Merge branch 'develop' into task/173

# Conflicts:
#	BMA.EHR.Leave/Controllers/LeaveController.cs
This commit is contained in:
kittapath 2025-10-09 21:44:58 +07:00
commit 559765dd86
25 changed files with 987 additions and 114 deletions

View file

@ -15,6 +15,7 @@ using Microsoft.AspNetCore.Routing.Template;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using OfficeOpenXml;
using OfficeOpenXml.Style;
using Swashbuckle.AspNetCore.Annotations;
using System.Globalization;
using System.Security.Claims;
@ -283,7 +284,12 @@ namespace BMA.EHR.Leave.Service.Controllers
var startFiscalYear = new DateTime(data.LeaveStartDate.Year - 1, 10, 1);
var endFiscalYear = data.LeaveStartDate.Date.AddDays(-1); // นับจากวันที่ยื่นลา
var leaveData = await _leaveBeginningRepository.GetByYearAndTypeIdForUserAsync(data.LeaveStartDate.Year, data.Type.Id, data.KeycloakUserId);
var thisYear = data.LeaveStartDate.Year;
var toDay = data.LeaveStartDate.Date;
if(toDay >= new DateTime(toDay.Year, 10, 1) && toDay <= new DateTime(toDay.Year, 12, 31))
thisYear = thisYear + 1;
var leaveData = await _leaveBeginningRepository.GetByYearAndTypeIdForUserAsync(thisYear, data.Type.Id, data.KeycloakUserId);
//var sumLeave = leaveData == null ? 0 : leaveData.LeaveDaysUsed;
var sumLeave = await _leaveRequestRepository.GetSumApproveLeaveTotalByTypeAndRangeForUser(data.KeycloakUserId, data.Type.Id, startFiscalYear, endFiscalYear);
@ -2167,12 +2173,15 @@ namespace BMA.EHR.Leave.Service.Controllers
var templatePath = Path.Combine(_hostingEnvironment.ContentRootPath, "Reports", "TimeStampRecords.xlsx");
byte[] templateBytes = System.IO.File.ReadAllBytes(templatePath);
//using (var package = new ExcelPackage(fileInfo))
using (var stream = new MemoryStream(templateBytes))
using (var package = new ExcelPackage(stream))
{
//var worksheet = package.Workbook.Worksheets.Add("Sheet1");
var worksheet = package.Workbook.Worksheets["Sheet1"] ?? package.Workbook.Worksheets[0];
// กำหนดให้ใช้ฟอนต์ TH SarabunPSK ซึ่งเป็นฟอนต์มาตรฐานราชการไทย
// **ข้อควรระวัง:** หากเครื่องผู้ใช้ไม่มีฟอนต์นี้ติดตั้งอยู่ การจัดหน้าเอกสารจะเพี้ยน
worksheet.Cells.Style.Font.Name = "TH SarabunPSK";
worksheet.Cells["A1:J1"].Merge = true;
worksheet.Cells["A2:J2"].Merge = true;
@ -2184,56 +2193,65 @@ namespace BMA.EHR.Leave.Service.Controllers
range.Style.VerticalAlignment = OfficeOpenXml.Style.ExcelVerticalAlignment.Center;
range.Style.Font.Bold = true;
}
//worksheet.Cells[1, 1].Value = "แบบการลงเวลาปฏิบัติราชการ";
worksheet.Cells[2, 1].Value = organizationName;
worksheet.Cells[3, 1].Value = dateTimeStamp;
using (var range = worksheet.Cells[4, 1, 4, 10])
{
range.Style.Border.Top.Style = OfficeOpenXml.Style.ExcelBorderStyle.Thin;
range.Style.Border.Bottom.Style = OfficeOpenXml.Style.ExcelBorderStyle.Thin;
range.Style.Border.Left.Style = OfficeOpenXml.Style.ExcelBorderStyle.Thin;
range.Style.Border.Right.Style = OfficeOpenXml.Style.ExcelBorderStyle.Thin;
range.Style.HorizontalAlignment = OfficeOpenXml.Style.ExcelHorizontalAlignment.Center;
range.Style.VerticalAlignment = OfficeOpenXml.Style.ExcelVerticalAlignment.Center;
range.Style.Font.Bold = true;
}
//worksheet.Cells[4, 1].Value = "ลำดับที่";
//worksheet.Cells[4, 2].Value = "ชื่อ - สกุล";
//worksheet.Cells[4, 3].Value = "รอบ";
//worksheet.Cells[4, 4].Value = "วันที่เข้างาน";
//worksheet.Cells[4, 5].Value = "พิกัด";
//worksheet.Cells[4, 6].Value = "เวลามา";
//worksheet.Cells[4, 7].Value = "วันที่ออกงาน";
//worksheet.Cells[4, 8].Value = "พิกัด";
//worksheet.Cells[4, 9].Value = "เวลากลับ";
//worksheet.Cells[4, 10].Value = "หมายเหตุ";
int startRow = 5;
foreach (var emp in employees)
int colCount = 10;
int totalRows = employees.Count;
// เตรียม List<object[]> สำหรับใช้กับ LoadFromArrays
var data = new List<object[]>(totalRows);
for (int i = 0; i < totalRows; i++)
{
worksheet.Cells[startRow, 1].Value = emp.no;
worksheet.Cells[startRow, 2].Value = emp.fullName;
worksheet.Cells[startRow, 3].Value = emp.dutyTimeName;
worksheet.Cells[startRow, 4].Value = emp.checkInDate;
worksheet.Cells[startRow, 5].Value = emp.checkInLocation;
worksheet.Cells[startRow, 6].Value = emp.checkInTime;
worksheet.Cells[startRow, 7].Value = emp.checkedOutDate;
worksheet.Cells[startRow, 8].Value = emp.checkOutLocation;
worksheet.Cells[startRow, 9].Value = emp.checkOutTime;
worksheet.Cells[startRow, 10].Value = emp.remark;
startRow++;
var emp = employees[i];
data.Add(new object[]
{
emp.no,
emp.fullName,
emp.dutyTimeName,
emp.checkInDate,
emp.checkInLocation,
emp.checkInTime,
emp.checkedOutDate,
emp.checkOutLocation,
emp.checkOutTime,
emp.remark
});
}
// ใส่กรอบให้ตาราง
using (var range = worksheet.Cells[5, 1, startRow - 1, 10])
// เขียนข้อมูลลง Excel ครั้งเดียว
worksheet.Cells[startRow, 1].LoadFromArrays(data);
// กำหนดสไตล์ตัวบาง + ขอบ
using (var range = worksheet.Cells[startRow, 1, startRow + totalRows - 1, colCount])
{
range.Style.Font.Bold = false;
range.Style.Border.Top.Style = OfficeOpenXml.Style.ExcelBorderStyle.Thin;
range.Style.Border.Bottom.Style = OfficeOpenXml.Style.ExcelBorderStyle.Thin;
range.Style.Border.Left.Style = OfficeOpenXml.Style.ExcelBorderStyle.Thin;
range.Style.Border.Right.Style = OfficeOpenXml.Style.ExcelBorderStyle.Thin;
range.Style.VerticalAlignment = OfficeOpenXml.Style.ExcelVerticalAlignment.Top;
// Center Align: คอลัมน์ 1 (A), 3 (C), 4 (D), 6 (F), 7 (G), 9 (I)
var centerColumns = new[] { 1, 3, 4, 6, 7, 9 };
foreach (var col in centerColumns)
{
worksheet.Cells[startRow, col, startRow + totalRows - 1, col].Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
}
// Left Align: คอลัมน์ 2 (B), 5 (E), 8 (H), 10 (J)
var leftColumns = new[] { 2, 5, 8, 10 };
foreach (var col in leftColumns)
{
worksheet.Cells[startRow, col, startRow + totalRows - 1, col].Style.HorizontalAlignment = ExcelHorizontalAlignment.Left;
}
}
int lastRow = startRow + 2;
// ส่วนสรุปท้ายตาราง
int lastRow = startRow + totalRows + 2;
worksheet.Cells[lastRow, 2].Value = type.Trim().ToUpper() == "OFFICER" ? "ข้าราชการทั้งหมด" : "ลูกจ้างประจำทั้งหมด";
worksheet.Cells[lastRow, 5].Value = profile?.Count;
worksheet.Cells[lastRow, 6].Value = "คน";
@ -2259,6 +2277,8 @@ namespace BMA.EHR.Leave.Service.Controllers
worksheet.Cells[lastRow + 6, 2].Value = "เรียน";
worksheet.Cells[lastRow + 7, 2].Value = "เพื่อโปรดทราบ";
worksheet.Cells[lastRow + 7, 9].Value = "ทราบ";
worksheet.Cells[lastRow + 7, 9].Style.Font.Bold = true;
worksheet.Cells[lastRow + 7, 9].Style.Font.Size = 22;
worksheet.Cells[lastRow + 8, 2].Value = "................................";
worksheet.Cells[lastRow + 8, 9].Value = "................................";
worksheet.Cells[worksheet.Dimension.Address].AutoFitColumns();