using DocumentFormat.OpenXml.Packaging; using ElectronicsShopBusinessLogic.OfficePackage; using ElectronicsShopBusinessLogic.OfficePackage.HelperModels; using ElectronicsShopContracts.BindingModels; using ElectronicsShopContracts.BusinessLogicContracts; using ElectronicsShopContracts.SearchModels; using ElectronicsShopContracts.StorageContracts; using ElectronicsShopContracts.ViewModels; using MigraDoc.DocumentObjectModel; using PdfSharp.Pdf; using System.Collections.Generic; namespace ElectronicsShopBusinessLogic.BusinessLogic { public class ReportClientLogic : IReportClientLogic { private readonly IPaymeantStorage _paymeantstorage; private readonly IProductStorage _productstorage; private readonly IOrderStorage _orderStorage; private readonly ICostItemStorage _costItemStorage; private readonly AbstractSaveToExcelClient _saveToExcel; private readonly AbstractSaveToWordClient _saveToWord; private readonly AbstractSaveToPdfClient _saveToPdf; public ReportClientLogic(AbstractSaveToExcelClient abstractSaveToExcelClient, AbstractSaveToWordClient abstractSaveToWordClient, IPaymeantStorage paymeantStorage, IProductStorage productStorage, IOrderStorage orderStorage, AbstractSaveToPdfClient abstractSaveToPdfClient, ICostItemStorage costItemStorage) { _saveToExcel = abstractSaveToExcelClient; _saveToWord= abstractSaveToWordClient; _paymeantstorage = paymeantStorage; _productstorage = productStorage; _orderStorage = orderStorage; _costItemStorage = costItemStorage; _saveToPdf = abstractSaveToPdfClient; } // Получение списка оплаченных товаров за период public List? GetProducts (ReportBindingModel model) { var paymeants = _paymeantstorage.GetFillteredList(new PaymeantSearchModel { DateFrom = model.DateFrom, DateTo = model.DateTo, ClientID = model.ClientID, }); List? products = new(); foreach (var paymeant in paymeants) { var order = _orderStorage.GetElement(new OrderSearchModel { ID = paymeant.OrderID }) ?? throw new Exception("Ошибка полуения данных"); foreach (var product in order.ProductList) { products.Add(new ReportProductsViewModel { ID = product.Value.Item1.ID, ProductName = product.Value.Item1.ProductName, Price = product.Value.Item1.Price, CostItemName = _costItemStorage.GetElement(new CostItemSearchModel { ID = product.Value.Item1.CostItemID })?.Name ?? "Отсутствует", PaymentID = paymeant.ID, count = product.Value.Item2 }); } } return products; } // получение списка оплат за период public List? GetPaymeants(ReportBindingModel model) { return _paymeantstorage.GetFillteredList(new PaymeantSearchModel { DateFrom = model.DateFrom, DateTo = model.DateTo }).Select(x => new ReportPaymeantsViewModel { ID = x.ID, DatePaymeant = x.DatePaymeant, OrderID = x.OrderID, ClientID = x.ClientID, SumPayment = x.SumPayment, PayOption = x.PayOption, }).ToList(); } // Получение списка товаров с указанием, в какие оплаты товар входит public List GetPaymeantProducts(ReportBindingModel model) { var paymeants = _paymeantstorage.GetFillteredList(new PaymeantSearchModel { ClientID = model.ClientID, DateFrom = model.DateFrom, DateTo = model.DateTo, }); var list = new List(); foreach (var paymeant in paymeants) { var record = new ReportPaymeantProductsViewModel { PaymeantID = paymeant.ID, Products = new(), TotalCount = 0, PaymeantPrice = paymeant.SumPayment, }; var order = _orderStorage.GetElement(new OrderSearchModel { ID = paymeant.OrderID }) ?? throw new Exception("Ошибка полуения данных"); foreach (var product in order.ProductList) { record.Products.Add(new(product.Value.Item1.ProductName, product.Value.Item2, _costItemStorage.GetElement (new CostItemSearchModel { ID = product.Value.Item1.CostItemID})?.Name ?? throw new Exception("Ошиюак получения данных"), product.Value.Item1.Price * product.Value.Item2)); record.TotalCount += product.Value.Item2; } list.Add(record); } return list; } public byte[]? SavePaymeantToExcelFile(ReportBindingModel model) { var document = _saveToExcel.CreateReport(new ExcelInfoClient { Title = "Список оплат и товаров", PaymeantProducts = GetPaymeantProducts(model), DateFrom = model.DateFrom, DateTo = model.DateTo }); return document; } public byte[]? SavePaymeantToWordFile(ReportBindingModel model) { var document = _saveToWord.CreateDoc(new WordInfoClient { Title = "Список оплат", DateFrom = model.DateFrom, DateTo = model.DateTo, Products = GetProducts(model) }); return document; } public PdfDocument SaveProductToPdfFile(ReportBindingModel model) { var document = _saveToPdf.CreteDoc(new PdfInfoClient { Title = "Список оплаченных товаров", FileName = "Report", DateFrom = model.DateFrom, DateTo = model.DateTo, Products = GetProducts(model) }); return document; } } }