diff --git a/LibraryDBproject/LDBproj/AdditionalForms/FullReportsF.cs b/LibraryDBproject/LDBproj/AdditionalForms/FullReportsF.cs index 38ca6fe..36d1bf7 100644 --- a/LibraryDBproject/LDBproj/AdditionalForms/FullReportsF.cs +++ b/LibraryDBproject/LDBproj/AdditionalForms/FullReportsF.cs @@ -34,7 +34,7 @@ public partial class FullReportsF : Form if (_container.Resolve().CreateDoc(sfd.FileName, BookChBox.Checked, EmployeeChBox.Checked, ReadersChBox.Checked)) { - MessageBox.Show("The document was formed : Report done", "Process result", MessageBoxButtons.OK, MessageBoxIcon.Information); + MessageBox.Show("< The DOCument was made : Report done >", "Process result", MessageBoxButtons.OK, MessageBoxIcon.Information); } else { diff --git a/LibraryDBproject/LDBproj/AdditionalForms/OrdersReportF.Designer.cs b/LibraryDBproject/LDBproj/AdditionalForms/OrdersReportF.Designer.cs new file mode 100644 index 0000000..136c1c1 --- /dev/null +++ b/LibraryDBproject/LDBproj/AdditionalForms/OrdersReportF.Designer.cs @@ -0,0 +1,114 @@ +namespace LDBproject.AdditionalForms +{ + partial class OrdersReportF + { + /// + /// Required designer variable. + /// + private System.ComponentModel.IContainer components = null; + + /// + /// Clean up any resources being used. + /// + /// true if managed resources should be disposed; otherwise, false. + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Windows Form Designer generated code + + /// + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// + private void InitializeComponent() + { + OkBtn = new Button(); + FilePathTb = new TextBox(); + ComboBoxB = new ComboBox(); + BeginDTP = new DateTimePicker(); + FinDTP = new DateTimePicker(); + PathConfirmBtn = new Button(); + SuspendLayout(); + // + // OkBtn + // + OkBtn.Location = new Point(49, 362); + OkBtn.Name = "OkBtn"; + OkBtn.Size = new Size(513, 46); + OkBtn.TabIndex = 0; + OkBtn.Text = "Confirm"; + OkBtn.UseVisualStyleBackColor = true; + OkBtn.Click += MakeReportBtn_Click; + // + // FilePathTb + // + FilePathTb.Location = new Point(49, 56); + FilePathTb.Name = "FilePathTb"; + FilePathTb.Size = new Size(413, 39); + FilePathTb.TabIndex = 1; + // + // ComboBoxB + // + ComboBoxB.FormattingEnabled = true; + ComboBoxB.Location = new Point(49, 142); + ComboBoxB.Name = "ComboBoxB"; + ComboBoxB.Size = new Size(513, 40); + ComboBoxB.TabIndex = 2; + // + // BeginDTP + // + BeginDTP.Location = new Point(49, 215); + BeginDTP.Name = "BeginDTP"; + BeginDTP.Size = new Size(513, 39); + BeginDTP.TabIndex = 3; + // + // FinDTP + // + FinDTP.Location = new Point(49, 279); + FinDTP.Name = "FinDTP"; + FinDTP.Size = new Size(513, 39); + FinDTP.TabIndex = 4; + // + // PathConfirmBtn + // + PathConfirmBtn.Location = new Point(477, 53); + PathConfirmBtn.Name = "PathConfirmBtn"; + PathConfirmBtn.Size = new Size(85, 47); + PathConfirmBtn.TabIndex = 5; + PathConfirmBtn.Text = "../"; + PathConfirmBtn.UseVisualStyleBackColor = true; + PathConfirmBtn.Click += SelectFilePathBtn_Click; + // + // CardsReportF + // + AutoScaleDimensions = new SizeF(13F, 32F); + AutoScaleMode = AutoScaleMode.Font; + ClientSize = new Size(614, 452); + Controls.Add(PathConfirmBtn); + Controls.Add(FinDTP); + Controls.Add(BeginDTP); + Controls.Add(ComboBoxB); + Controls.Add(FilePathTb); + Controls.Add(OkBtn); + Name = "CardsReportF"; + Text = "CardsReportF"; + ResumeLayout(false); + PerformLayout(); + } + + #endregion + + private Button OkBtn; + private TextBox FilePathTb; + private ComboBox ComboBoxB; + private DateTimePicker BeginDTP; + private DateTimePicker FinDTP; + private Button PathConfirmBtn; + } +} \ No newline at end of file diff --git a/LibraryDBproject/LDBproj/AdditionalForms/OrdersReportF.cs b/LibraryDBproject/LDBproj/AdditionalForms/OrdersReportF.cs new file mode 100644 index 0000000..1570c7c --- /dev/null +++ b/LibraryDBproject/LDBproj/AdditionalForms/OrdersReportF.cs @@ -0,0 +1,87 @@ +using Unity; +using LDBproject.Repositories; +using LDBproject.Reports; +using LDBproject.Entities; + +namespace LDBproject.AdditionalForms +{ + public partial class OrdersReportF : Form + { + private readonly IUnityContainer _container; + private readonly IOrderRep _orderRep; + + public OrdersReportF(IUnityContainer container, IOrderRep orderRep) + { + InitializeComponent(); + _container = container ?? throw new ArgumentNullException(nameof(container)); + _orderRep = orderRep ?? throw new ArgumentNullException(nameof(orderRep)); + + var ordersInfo = _orderRep.GetOrdersInfo().ToList(); + + if (ordersInfo.Any()) + { + ComboBoxB.DataSource = ordersInfo; + ComboBoxB.DisplayMember = "DisplayInfo"; + ComboBoxB.ValueMember = "OrderID"; // Assuming the data model has this property + } + else + { + MessageBox.Show("No Orders Found.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); + Close(); + } + } + private void SelectFilePathBtn_Click(object sender, EventArgs e) + { + var sfd = new SaveFileDialog() { Filter = "Excel Files | *.xlsx" }; + if (sfd.ShowDialog() == DialogResult.OK) + { + FilePathTb.Text = sfd.FileName; + } + } + + private void MakeReportBtn_Click(object sender, EventArgs e) + { + try + { + string filePath = FilePathTb.Text; + if (string.IsNullOrWhiteSpace(filePath)) + { + MessageBox.Show("Please select a file path.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + + if (ComboBoxB.SelectedItem == null) + { + MessageBox.Show("Please select an order.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + + DateTime startDate = BeginDTP.Value; + DateTime endDate = FinDTP.Value; + + if (startDate >= endDate) + { + MessageBox.Show("Start date must be before end date.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + // Correctly get the selected OrderInfo object + if (ComboBoxB.SelectedItem is not Order selectedOrderInfo) + { + MessageBox.Show("Invalid order selection.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + // Resolve TableReport and pass the dates + var tableReport = _container.Resolve(); + + if (tableReport.CreateTable(filePath, startDate, endDate)) + { + MessageBox.Show("< Chart PDF report generated successfully >", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information); + } + } + catch (Exception ex) + { + MessageBox.Show($"An error occurred while generating the report: {ex.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + } +} \ No newline at end of file diff --git a/LibraryDBproject/LDBproj/AdditionalForms/OrdersReportF.resx b/LibraryDBproject/LDBproj/AdditionalForms/OrdersReportF.resx new file mode 100644 index 0000000..af32865 --- /dev/null +++ b/LibraryDBproject/LDBproj/AdditionalForms/OrdersReportF.resx @@ -0,0 +1,120 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git a/LibraryDBproject/LDBproj/Entities/Order.cs b/LibraryDBproject/LDBproj/Entities/Order.cs index 0c25bdd..41602c7 100644 --- a/LibraryDBproject/LDBproj/Entities/Order.cs +++ b/LibraryDBproject/LDBproj/Entities/Order.cs @@ -24,4 +24,16 @@ public class Order BorrowDate = borrow }; } + + public static Order NewOrder(TemprOrderReg order, IEnumerable regs) + { + return new Order + { + OrderID = order.OrderID, + CardID = order.CardID, + LibrarianID = order.LibrarianID, + Registrations = regs, + BorrowDate= order.BorrowDate + }; + } } diff --git a/LibraryDBproject/LDBproj/Entities/TemprOrderReg.cs b/LibraryDBproject/LDBproj/Entities/TemprOrderReg.cs index fcadf00..e46fdb4 100644 --- a/LibraryDBproject/LDBproj/Entities/TemprOrderReg.cs +++ b/LibraryDBproject/LDBproj/Entities/TemprOrderReg.cs @@ -1,6 +1,6 @@ namespace LDBproject.Entities; -internal class TemprOrderReg +public class TemprOrderReg { // from Order class public int OrderID { get; private set; } diff --git a/LibraryDBproject/LDBproj/MainForm.cs b/LibraryDBproject/LDBproj/MainForm.cs index 06e1af7..4e4fc4d 100644 --- a/LibraryDBproject/LDBproj/MainForm.cs +++ b/LibraryDBproject/LDBproj/MainForm.cs @@ -96,7 +96,7 @@ public partial class MainForm : Form { try { - + _container.Resolve().ShowDialog(); } catch (Exception ex) { @@ -118,4 +118,3 @@ public partial class MainForm : Form } } } - diff --git a/LibraryDBproject/LDBproj/Reports/TableReport.cs b/LibraryDBproject/LDBproj/Reports/TableReport.cs new file mode 100644 index 0000000..5ba5b24 --- /dev/null +++ b/LibraryDBproject/LDBproj/Reports/TableReport.cs @@ -0,0 +1,61 @@ +using Microsoft.Extensions.Logging; +using LDBproject.Repositories; + +namespace LDBproject.Reports +{ + internal class TableReport + { + private readonly IOrderRep _orderRep; + private readonly ILogger _logger; + internal static readonly string[] item = { "Librarian ID", "Card ID", "Borrow Date", "Book ID", "Note" }; + + public TableReport(IOrderRep orderRep, ILogger 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(new[] { 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 GetData(DateTime startDate, DateTime endDate) + { + var result = new List { item }; + var orders = _orderRep.GetOrdersInfo(); + + 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 cde2b59..e8d419b 100644 --- a/LibraryDBproject/LDBproj/Repositories/IOrderRep.cs +++ b/LibraryDBproject/LDBproj/Repositories/IOrderRep.cs @@ -4,8 +4,7 @@ namespace LDBproject.Repositories; public interface IOrderRep { - IEnumerable GetOrdersInfo( - int? librarianID = null, int? orderID = null, int? customerID = null); + IEnumerable GetOrdersInfo(); void CreateOrder(Order order); diff --git a/LibraryDBproject/LDBproj/Repositories/Implementations/OrderR.cs b/LibraryDBproject/LDBproj/Repositories/Implementations/OrderR.cs index 271a0f6..13f6ea0 100644 --- a/LibraryDBproject/LDBproj/Repositories/Implementations/OrderR.cs +++ b/LibraryDBproject/LDBproj/Repositories/Implementations/OrderR.cs @@ -130,21 +130,20 @@ public class OrderR : IOrderRep } } - // ----------------------------- [ REVIEWED (waits for further editing) ] ------------------------------- - - public IEnumerable GetOrdersInfo(int? orderID = null, int ? librarianID = null, int? customerID = null) + public IEnumerable GetOrdersInfo() { - _logger.LogInformation("< Getting all ORDERS >"); + _logger.LogInformation("< Getting ORDERS based on a date >"); try { using var connection = new NpgsqlConnection(_connectionString.ConnectionString); connection.Open(); - var querySelectAll = @"SELECT * FROM Orders"; - var orders = connection.Query(querySelectAll); + var querySelectAll = @"SELECT orders.*, regs.BookID, regs.Note FROM Orders orders + INNER JOIN Registrations regs ON regs.OrderID = orders.OrderID"; + var orders = connection.Query(querySelectAll); _logger.LogDebug("Aimed objects: {json}", JsonConvert.SerializeObject(orders)); - - return orders; + return orders.GroupBy(x => x.OrderID, y => y, (key, value) => Order.NewOrder(value.First(), + value.Select(z => Registration.OrderReg(z.OrderID, z.BookID, z.Note)))).ToList(); } catch (Exception ex) {