68 lines
2.3 KiB
C#
68 lines
2.3 KiB
C#
using Microsoft.Extensions.Logging;
|
|
using LDBproject.Repositories;
|
|
|
|
namespace LDBproject.Reports
|
|
{
|
|
internal class TableReport
|
|
{
|
|
private readonly IOrderRep _orderRep;
|
|
private readonly ILogger<TableReport> _logger;
|
|
internal static readonly string[] item = { "Librarian ID", "Card ID", "Borrow Date", "Book ID", "Note" };
|
|
|
|
public TableReport(IOrderRep orderRep, ILogger<TableReport> logger)
|
|
{
|
|
_orderRep = orderRep ?? throw new ArgumentNullException(nameof(orderRep));
|
|
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
|
|
}
|
|
|
|
public bool CreateTable(string filePath, DateTime startDate, DateTime endDate)
|
|
{
|
|
try
|
|
{
|
|
new ExcelBuilder(filePath)
|
|
.AddHeader("Report about borrowed books", 0, 5) // Updated header
|
|
.AddParagraph($"Period: {startDate:yyyy-MM-dd} - {endDate:yyyy-MM-dd}", 0)
|
|
.AddTable([4, 4, 7, 4, 7], GetData(startDate, endDate)) // Updated column widths
|
|
.Build();
|
|
return true;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Error while forming document");
|
|
return false;
|
|
}
|
|
}
|
|
|
|
private List<string[]> GetData(DateTime startDate, DateTime endDate)
|
|
{
|
|
var data = _orderRep.GetOrdersInfo(brDate: startDate, tillDate: endDate)
|
|
.SelectMany(order => order.Registrations
|
|
.Select(reg => new
|
|
{
|
|
order.EmployeeName,
|
|
order.ReaderName,
|
|
order.BorrowDate,
|
|
order.BookInfo,
|
|
reg.Note
|
|
}))
|
|
.OrderBy(x => x.BorrowDate);
|
|
|
|
|
|
var result = new List<string[]> { item };
|
|
result.AddRange(data.Select(x => new string[]
|
|
{
|
|
x.EmployeeName,
|
|
x.ReaderName,
|
|
x.BorrowDate.ToString("dd.MM.yyyy"),
|
|
x.BookInfo,
|
|
x.Note
|
|
}));
|
|
int totalBookCount = data.Count();
|
|
|
|
result.Add(new[] { "Total Books:", "", "", totalBookCount.ToString(), "" });
|
|
|
|
return result;
|
|
}
|
|
}
|
|
}
|