diff --git a/ComputerStoreBusinessLogic/BusinessLogic/EmployeeReportByDateLogic.cs b/ComputerStoreBusinessLogic/BusinessLogic/EmployeeReportByDateLogic.cs deleted file mode 100644 index ee2c3c0..0000000 --- a/ComputerStoreBusinessLogic/BusinessLogic/EmployeeReportByDateLogic.cs +++ /dev/null @@ -1,67 +0,0 @@ -using ComputerStoreContracts.BindingModels; -using ComputerStoreContracts.BusinessLogicContracts; -using ComputerStoreContracts.SearchModels; -using ComputerStoreContracts.StorageContracts; -using ComputerStoreContracts.ViewModels; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Runtime.CompilerServices; -using System.Text; -using System.Threading.Tasks; - -namespace ComputerStoreBusinessLogic.BusinessLogic -{ - public class EmployeeReportByDateLogic : IEmployeeReportByDateLogic - { - private readonly IPCStorage _pcStorage; - private readonly IProductStorage _productStorage; - - public EmployeeReportByDateLogic(IPCStorage pcStorage, IProductStorage productStorage) - { - _pcStorage = pcStorage; - _productStorage = productStorage; - } - - public List GetPCs(ReportDateBindingModel model) - { - return _pcStorage.GetFilteredList(new PCSearchModel - { - DateFrom = model.DateFrom, - DateTo = model.DateTo - }) - .Select(x => new ReportPCByDateViewModel - { - PCName = x.Name, - Components = x.PCComponents.Values.Select(x => (x.Item1.Name, x.Item2)).ToList(), - EmployeeUsername = x.EmployeeUsername - }).ToList(); - } - - public List GetProducts(ReportDateBindingModel model) - { - return _productStorage.GetFilteredList(new ProductSearchModel - { - DateFrom = model.DateFrom, - DateTo = model.DateTo - }) - .Select(x => new ReportProductByDateViewModel - { - ProductName = x.Name, - Components = x.ProductComponents.Values.Select(x => (x.Item1.Name, x.Item2)).ToList() - - }).ToList(); - - } - - public void SaveProductsToPdfFile(ReportDateBindingModel model) - { - //will be implemented in the future! - } - - public void SavePCsToPdfFile(ReportDateBindingModel model) - { - //will be implemented in the future! - } - } -} diff --git a/ComputerStoreBusinessLogic/BusinessLogic/EmployeeReportLogic.cs b/ComputerStoreBusinessLogic/BusinessLogic/EmployeeReportLogic.cs new file mode 100644 index 0000000..65889a1 --- /dev/null +++ b/ComputerStoreBusinessLogic/BusinessLogic/EmployeeReportLogic.cs @@ -0,0 +1,137 @@ +using ComputerStoreContracts.BindingModels; +using ComputerStoreContracts.BusinessLogicContracts; +using ComputerStoreContracts.SearchModels; +using ComputerStoreContracts.StorageContracts; +using ComputerStoreContracts.ViewModels; +using ComputerStoreDataModels.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Runtime.CompilerServices; +using System.Security.Cryptography.X509Certificates; +using System.Text; +using System.Threading.Tasks; + +namespace ComputerStoreBusinessLogic.BusinessLogic +{ + public class EmployeeReportLogic : IEmployeeReportLogic + { + private readonly IPCStorage _pcStorage; + private readonly IProductStorage _productStorage; + private readonly IConsignmentStorage _consignmentStorage; + private readonly IOrderStorage _orderStorage; + + public EmployeeReportLogic(IPCStorage pcStorage, IProductStorage productStorage, IConsignmentStorage consignmentStorage, IOrderStorage orderStorage) + { + _pcStorage = pcStorage; + _productStorage = productStorage; + _consignmentStorage = consignmentStorage; + _orderStorage = orderStorage; + } + + public List GetPCsByDate(ReportDateBindingModel model) + { + return _pcStorage.GetFilteredList(new PCSearchModel + { + DateFrom = model.DateFrom, + DateTo = model.DateTo + }) + .Select(x => new ReportPCByDateViewModel + { + PCName = x.Name, + Components = x.PCComponents.Values.Select(x => (x.Item1.Name, x.Item2)).ToList(), + EmployeeUsername = x.EmployeeUsername + }).ToList(); + + } + + public List GetProductsByDate(ReportDateBindingModel model) + { + return _productStorage.GetFilteredList(new ProductSearchModel + { + DateFrom = model.DateFrom, + DateTo = model.DateTo + }) + .Select(x => new ReportProductByDateViewModel + { + ProductName = x.Name, + Components = x.ProductComponents.Values.Select(x => (x.Item1.Name, x.Item2)).ToList() + + }).ToList(); + } + + public List GetConsignmentsByComponents(ReportComponentsBindingModel model) + { + var list = new List(); + + Dictionary specifiedProducts = new Dictionary(); + + foreach(var component in model.Components) + { + var products = _productStorage.GetFilteredList(new ProductSearchModel + { + ComponentID = component.ID + }); + + foreach(var product in products) + { + if(!specifiedProducts.ContainsKey(product.ID)) + { + specifiedProducts.Add(product.ID, product); + } + } + } + + Dictionary specifiedConsignments = new Dictionary(); + + foreach (var product in specifiedProducts) + { + var consignments = _consignmentStorage.GetFilteredList(new ConsignmentSearchModel + { + ProductID = product.Key + }); + + if(!specifiedConsignments.Any()) + { + foreach (var consignment in consignments) + { + specifiedConsignments.Add(consignment.ID, consignment); + } + } + + foreach(var consignment in specifiedConsignments.Values) + { + if(!consignments.Contains(consignment)) + { + specifiedConsignments.Remove(consignment.ID); + } + } + + } + + foreach(var consignment in specifiedConsignments) + { + var record = new ReportConsignmentsViewModel + { + ID = consignment.Key, + Products = consignment.Value.ConsignmentProducts.Values.Select(x => (x.Item1.Name,x.Item2)).ToList() + }; + list.Add(record); + } + + return list; + } + + public void SaveProductsToPdfFile(ReportDateBindingModel model) + { + //will be implemented in the future! + } + + public void SavePCsToPdfFile(ReportDateBindingModel model) + { + //will be implemented in the future! + } + + + } +} diff --git a/ComputerStoreContracts/BindingModels/ReportComponentsBindingModel.cs b/ComputerStoreContracts/BindingModels/ReportComponentsBindingModel.cs new file mode 100644 index 0000000..7f439c8 --- /dev/null +++ b/ComputerStoreContracts/BindingModels/ReportComponentsBindingModel.cs @@ -0,0 +1,15 @@ +using ComputerStoreContracts.SearchModels; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ComputerStoreContracts.BindingModels +{ + public class ReportComponentsBindingModel + { + public string FileName { get; set; } = string.Empty; + public List Components { get; set; } = new List(); + } +} diff --git a/ComputerStoreContracts/BusinessLogicContracts/IEmployeeReportByDateLogic.cs b/ComputerStoreContracts/BusinessLogicContracts/IEmployeeReportLogic.cs similarity index 55% rename from ComputerStoreContracts/BusinessLogicContracts/IEmployeeReportByDateLogic.cs rename to ComputerStoreContracts/BusinessLogicContracts/IEmployeeReportLogic.cs index 2bab877..d245602 100644 --- a/ComputerStoreContracts/BusinessLogicContracts/IEmployeeReportByDateLogic.cs +++ b/ComputerStoreContracts/BusinessLogicContracts/IEmployeeReportLogic.cs @@ -8,10 +8,11 @@ using System.Threading.Tasks; namespace ComputerStoreContracts.BusinessLogicContracts { - public interface IEmployeeReportByDateLogic + public interface IEmployeeReportLogic { - List GetProducts(ReportDateBindingModel model); - List GetPCs(ReportDateBindingModel model); + List GetProductsByDate(ReportDateBindingModel model); + List GetPCsByDate(ReportDateBindingModel model); + List GetConsignmentsByComponents(ReportComponentsBindingModel model); void SaveProductsToPdfFile(ReportDateBindingModel model); void SavePCsToPdfFile(ReportDateBindingModel model); diff --git a/ComputerStoreContracts/SearchModels/ProductSearchModel.cs b/ComputerStoreContracts/SearchModels/ProductSearchModel.cs index a6c0e75..e29146f 100644 --- a/ComputerStoreContracts/SearchModels/ProductSearchModel.cs +++ b/ComputerStoreContracts/SearchModels/ProductSearchModel.cs @@ -11,6 +11,7 @@ namespace ComputerStoreContracts.SearchModels public int? ID { get; set; } public string? Name { get; set; } public int? EmployeeID { get; set; } + public int? ComponentID { get; set; } public DateTime? DateTo { get; set; } public DateTime? DateFrom { get; set; } } diff --git a/ComputerStoreContracts/ViewModels/ReportConsignmentsViewModel.cs b/ComputerStoreContracts/ViewModels/ReportConsignmentsViewModel.cs new file mode 100644 index 0000000..5578c67 --- /dev/null +++ b/ComputerStoreContracts/ViewModels/ReportConsignmentsViewModel.cs @@ -0,0 +1,15 @@ +using ComputerStoreDataModels.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ComputerStoreContracts.ViewModels +{ + public class ReportConsignmentsViewModel + { + public int ID { get; set; } + public List<(string product, int count)> Products { get; set; } = new List<(string, int)>(); + } +} diff --git a/ComputerStoreDatabaseImplement/Implements/PCStorage.cs b/ComputerStoreDatabaseImplement/Implements/PCStorage.cs index 1184a85..16c1ec9 100644 --- a/ComputerStoreDatabaseImplement/Implements/PCStorage.cs +++ b/ComputerStoreDatabaseImplement/Implements/PCStorage.cs @@ -36,7 +36,7 @@ namespace ComputerStoreDatabaseImplement.Implements { return context.PCs.Include(x => x.Employee).Where(x => x.EmployeeID == model.EmployeeID).Select(x => x.GetViewModel).ToList(); } - return context.PCs.Include(x => x.Components).ThenInclude(x => x.Component).Where(p => p.RequestID == context.Requests.First(r => r.OrderID == context.Orders.First(o => o.DateCreate >= model.DateFrom && o.DateImplement <= model.DateTo ).ID).ID).ToList().Select(x => x.GetViewModel).ToList(); + return context.PCs.Include(x => x.Components).ThenInclude(x => x.Component).Where(p => context.Requests.Where(r => context.Orders.Where(o => o.DateCreate >= model.DateFrom && o.DateCreate <= model.DateTo).Select(o => o.ID).Contains(r.OrderID)).Select(r => r.ID).Contains(p.RequestID)).ToList().Select(x => x.GetViewModel).ToList(); } public List GetFullList() diff --git a/ComputerStoreDatabaseImplement/Implements/ProductStorage.cs b/ComputerStoreDatabaseImplement/Implements/ProductStorage.cs index 5b422e8..c78a017 100644 --- a/ComputerStoreDatabaseImplement/Implements/ProductStorage.cs +++ b/ComputerStoreDatabaseImplement/Implements/ProductStorage.cs @@ -24,7 +24,7 @@ namespace ComputerStoreDatabaseImplement.Implements public List GetFilteredList(ProductSearchModel model) { - if(string.IsNullOrEmpty(model.Name) && !model.EmployeeID.HasValue && !model.DateTo.HasValue && !model.DateFrom.HasValue) + if(string.IsNullOrEmpty(model.Name) && !model.EmployeeID.HasValue && !model.DateTo.HasValue && !model.DateFrom.HasValue && !model.ComponentID.HasValue) { return new(); } @@ -34,7 +34,11 @@ namespace ComputerStoreDatabaseImplement.Implements { return context.Products.Include(x => x.Employee).Where(x => x.EmployeeID == model.EmployeeID).Select(x => x.GetViewModel).ToList(); } - return context.Products.Include(x => x.Components).ThenInclude(x => x.Component).Where(p => p.ID == context.Consignments.First(r => r.OrderID == context.Orders.First(o => o.DateCreate >= model.DateFrom && o.DateImplement <= model.DateTo).ID).ID).ToList().Select(x => x.GetViewModel).ToList(); + if(model.ComponentID.HasValue) + { + return context.Products.Include(x => x.Components).ThenInclude(x => x.Component).Where(p => context.ProductComponents.Where(pc => pc.ComponentID == model.ComponentID).Select(pc => pc.ProductID).Contains(p.ID)).Select(x => x.GetViewModel).ToList(); + } + return context.Products.Include(x => x.Components).ThenInclude(x => x.Component).Where(p => context.ConsignmentProducts.Where(cp => context.Consignments.Where(c => context.Orders.Where(o => o.DateCreate >= model.DateFrom && o.DateCreate <= model.DateTo).Select(o => o.ID).Contains(c.OrderID)).Select(c => c.ID).Contains(cp.ConsignmentID)).Select(cp => cp.ProductID).Contains(p.ID)).ToList().Select(x => x.GetViewModel).ToList(); } public List GetFullList()