diff --git a/LibraryDBproject/LDBproj/AdditionalForms/BookListF.cs b/LibraryDBproject/LDBproj/AdditionalForms/BookListF.cs index 9d1ca7e..87e1c3b 100644 --- a/LibraryDBproject/LDBproj/AdditionalForms/BookListF.cs +++ b/LibraryDBproject/LDBproj/AdditionalForms/BookListF.cs @@ -60,7 +60,12 @@ public partial class BookListF : Form } } - private void ReloadList() => DataGV.DataSource = _bookR.GetBookList(); + private void ReloadList() + { + DataGV.DataSource = _bookR.GetBookList(); + // [Browsable(false)] * [ - ] + DataGV.Columns["MainBookInfo"].Visible = false; // [ + ] + } private void DelBtn_Click(object sender, EventArgs e) { diff --git a/LibraryDBproject/LDBproj/AdditionalForms/OrdersF.cs b/LibraryDBproject/LDBproj/AdditionalForms/OrdersF.cs index 455bc0f..f0bbc25 100644 --- a/LibraryDBproject/LDBproj/AdditionalForms/OrdersF.cs +++ b/LibraryDBproject/LDBproj/AdditionalForms/OrdersF.cs @@ -65,8 +65,11 @@ public partial class OrdersF : Form private void ReloadList() { + var res = _regR.GetOrdersInfo(); DataGV.DataSource = _regR.GetOrdersInfo(); - // DataGV.Columns["BorrowDate"].DefaultCellStyle.Format = "dd MMMM yyyy hh:mm"; + // DataGV.Columns["ID"].Visible = false; + // DataGV.Columns["OrderID"].Visible = false; + DataGV.Columns["BorrowDate"].DefaultCellStyle.Format = "dd MMMM yyyy"; } private bool GetIDFromRow(out int id) diff --git a/LibraryDBproject/LDBproj/Entities/Registration.cs b/LibraryDBproject/LDBproj/Entities/Registration.cs index fab7c59..0085c82 100644 --- a/LibraryDBproject/LDBproj/Entities/Registration.cs +++ b/LibraryDBproject/LDBproj/Entities/Registration.cs @@ -5,6 +5,7 @@ public class Registration public int ID { get; private set; } public int OrderID { get; private set; } public int BookID { get; private set; } + public string BookInfo { get; private set; } = string.Empty; public string Note { get; private set; } diff --git a/LibraryDBproject/LDBproj/Reports/ChartReport.cs b/LibraryDBproject/LDBproj/Reports/ChartReport.cs index 10dc074..c430a8f 100644 --- a/LibraryDBproject/LDBproj/Reports/ChartReport.cs +++ b/LibraryDBproject/LDBproj/Reports/ChartReport.cs @@ -18,13 +18,11 @@ internal class ChartReport { try { - var updates = _updR.GetUpdateList().ToList(); // Materialize the query + var updates = _updR.GetUpdateList(dateFrom: dateTime.Date, dateTo: dateTime.Date.AddDays(1)).ToList(); // Materialize the query var data = GetData(updates, dateTime); - System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance); - - new PdfBuilder(filePath).AddHeader("Card Updates") // More descriptive header - .AddPieChart("Number of Times Card Updated", data) // Corrected caption + new PdfBuilder(filePath).AddHeader("Card Updates") + .AddPieChart("Number of Times Card Updated", data) .Build(); return true; } @@ -34,23 +32,12 @@ internal class ChartReport return false; } } - private List<(string Caption, double Value)> GetData(List updates, DateTime date) { - var result = new List<(string Caption, double Value)>(); - - var distinctCards = updates.Where(u => u.LastUpdate.Date == date.Date) // Filter by date - .GroupBy(u => u.CardID) - .Select(g => new { CardID = g.Key, Count = g.Count() }) - .ToList(); - - - foreach (var cardData in distinctCards) - { - result.Add(($"Card {cardData.CardID}", cardData.Count)); - } - - return result; + return updates.GroupBy(x => x.CardID) + .Select(group => ( + Caption: $"Card n_{group.Key}", + Value: (double)group.Count() + )).ToList(); } - } diff --git a/LibraryDBproject/LDBproj/Reports/PdfBuilder.cs b/LibraryDBproject/LDBproj/Reports/PdfBuilder.cs index a3348c8..fdf68ca 100644 --- a/LibraryDBproject/LDBproj/Reports/PdfBuilder.cs +++ b/LibraryDBproject/LDBproj/Reports/PdfBuilder.cs @@ -29,8 +29,7 @@ internal class PdfBuilder // _document.AddSection().AddParagraph(header, "NormalBold"); // return this; } - public PdfBuilder AddPieChart(string title, List<(string Caption, double - Value)> data) + public PdfBuilder AddPieChart(string title, List<(string Caption, double Value)> data) { if (data == null || data.Count == 0) { diff --git a/LibraryDBproject/LDBproj/Reports/TableReport.cs b/LibraryDBproject/LDBproj/Reports/TableReport.cs index 5ba5b24..c6dbe15 100644 --- a/LibraryDBproject/LDBproj/Reports/TableReport.cs +++ b/LibraryDBproject/LDBproj/Reports/TableReport.cs @@ -22,7 +22,7 @@ namespace LDBproject.Reports new ExcelBuilder(filePath) .AddHeader("Report about borrowed books", 0, 5) // Updated header .AddParagraph($"Period: {startDate:yyyy-MM-dd} - {endDate:yyyy-MM-dd}", 0) - .AddTable(new[] { 4, 4, 7, 4, 7 }, GetData(startDate, endDate)) // Updated column widths + .AddTable([4, 4, 7, 4, 7], GetData(startDate, endDate)) // Updated column widths .Build(); return true; } @@ -32,27 +32,33 @@ namespace LDBproject.Reports return false; } } + private List 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 { item }; - var orders = _orderRep.GetOrdersInfo(); + 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(); - var flattenedData = orders - .SelectMany(order => order.Registrations - .Select(reg => new { order.LibrarianID, order.CardID, - order.BorrowDate, reg.BookID, reg.Note })) - .Where(x => startDate <= x.BorrowDate && x.BorrowDate <= endDate) - .ToList(); - - result.AddRange(flattenedData.Select(x => new string[] - { - x.LibrarianID.ToString(), - x.CardID.ToString(), - x.BorrowDate.ToString("yyyy-MM-dd"), - x.BookID.ToString(), - x.Note - }).ToList()); - int totalBookCount = flattenedData.Count; result.Add(new[] { "Total Books:", "", "", totalBookCount.ToString(), "" }); return result; diff --git a/LibraryDBproject/LDBproj/Repositories/IOrderRep.cs b/LibraryDBproject/LDBproj/Repositories/IOrderRep.cs index e8d419b..f9dce50 100644 --- a/LibraryDBproject/LDBproj/Repositories/IOrderRep.cs +++ b/LibraryDBproject/LDBproj/Repositories/IOrderRep.cs @@ -4,7 +4,8 @@ namespace LDBproject.Repositories; public interface IOrderRep { - IEnumerable GetOrdersInfo(); + IEnumerable GetOrdersInfo(DateTime? brDate = null, DateTime? tillDate = null, + int? cardID = null, int? librnID = null); void CreateOrder(Order order); diff --git a/LibraryDBproject/LDBproj/Repositories/IUpdateRep.cs b/LibraryDBproject/LDBproj/Repositories/IUpdateRep.cs index 444fc87..8f276b7 100644 --- a/LibraryDBproject/LDBproj/Repositories/IUpdateRep.cs +++ b/LibraryDBproject/LDBproj/Repositories/IUpdateRep.cs @@ -4,7 +4,7 @@ namespace LDBproject.Repositories; public interface IUpdateRep { - IEnumerable GetUpdateList(); + IEnumerable GetUpdateList(DateTime? dateFrom = null, DateTime? dateTo = null); UpdateC GetUpdateByID(int id); diff --git a/LibraryDBproject/LDBproj/Repositories/Implementations/OrderR.cs b/LibraryDBproject/LDBproj/Repositories/Implementations/OrderR.cs index 58682fc..ba30568 100644 --- a/LibraryDBproject/LDBproj/Repositories/Implementations/OrderR.cs +++ b/LibraryDBproject/LDBproj/Repositories/Implementations/OrderR.cs @@ -130,25 +130,45 @@ public class OrderR : IOrderRep } } - public IEnumerable GetOrdersInfo() + public IEnumerable GetOrdersInfo(DateTime? brDate = null, DateTime? tilldate = null, + int? cardID = null, int? librnID = null) { _logger.LogInformation("< Getting ORDERS >"); try { + var builder = new QueryBuilder(); + if (brDate.HasValue) + { + builder.AddCondition("orders.BorrowDate >= @brDate"); + } + if (brDate.HasValue) + { + builder.AddCondition("orders.BorrowDate <= @tilldate"); + } + if (cardID.HasValue) + { + builder.AddCondition("orders.CardID = @cardID"); + } + if (librnID.HasValue) + { + builder.AddCondition("orders.LibrarianID = @librnID"); + } + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); connection.Open(); - var querySelectAll = @"SELECT - orders.*, - lc.FIO as EmployeeName, - CONCAT(books.Title, ' ', books.Author) as BookInfo, - CONCAT(cc.CardID, ' ', cc.FIO) as ReaderName, - regs.BookID, regs.Note - FROM Orders orders - LEFT JOIN LibrarianCards lc ON lc.CardID = orders.LibrarianID - LEFT JOIN CustomerCards cc ON cc.CardID = orders.CardID - INNER JOIN Registrations regs ON regs.OrderID = orders.OrderID - LEFT JOIN Books books ON books.BookID = regs.BookID"; + var querySelectAll = @$"SELECT + orders.*, + lc.FIO as EmployeeName, + CONCAT(books.Title, ' ', books.Author) as BookInfo, + CONCAT(cc.CardID, ' ', cc.FIO) as ReaderName, + regs.BookID, regs.Note + FROM Orders orders + INNER JOIN LibrarianCards lc ON lc.CardID = orders.LibrarianID + INNER JOIN CustomerCards cc ON cc.CardID = orders.CardID + INNER JOIN Registrations regs ON regs.OrderID = orders.OrderID + LEFT JOIN Books books ON books.BookID = regs.BookID + {builder.Build()}"; var regsDict = new Dictionary>(); var orders = connection.Query(querySelectAll, (order, orders) => @@ -169,7 +189,7 @@ public class OrderR : IOrderRep var odr = orders.First(y => y.OrderID == x.Key); odr.SetRegs(x.Value); return odr; - }); + }).ToList(); } catch (Exception ex) { diff --git a/LibraryDBproject/LDBproj/Repositories/Implementations/UpdateR.cs b/LibraryDBproject/LDBproj/Repositories/Implementations/UpdateR.cs index 0a671b9..b3e13f2 100644 --- a/LibraryDBproject/LDBproj/Repositories/Implementations/UpdateR.cs +++ b/LibraryDBproject/LDBproj/Repositories/Implementations/UpdateR.cs @@ -70,7 +70,7 @@ public class UpdateR : IUpdateRep } } - public IEnumerable GetUpdateList() + public IEnumerable GetUpdateList(DateTime? dateFrom = null, DateTime? dateTo = null) { _logger.LogInformation("< Getting all UPDATES >"); try