CourseWorkElectronicsShop/ElectronicsShop/ElectronicsShopBusinessLogic/BusinessLogic/ReportClientLogic.cs
Илья Федотов adaa621ff6 беда
2024-07-24 13:02:15 +04:00

132 lines
5.6 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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<ReportProductsViewModel>? GetProducts (ReportBindingModel model) {
var paymeants = _paymeantstorage.GetFillteredList(new PaymeantSearchModel {
DateFrom = model.DateFrom,
DateTo = model.DateTo,
});
List<ReportProductsViewModel>? products = new();
foreach (var paymeant in paymeants) {
foreach (var product in paymeant.PayProductList) {
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
?? "Отсутствует"
});
}
}
return products;
}
// получение списка оплат за период
public List<ReportPaymeantsViewModel>? 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<ReportPaymeantProductsViewModel> GetPaymeantProducts(int _clientID) {
var products = _productstorage.GetFullList();
var paymeants = _paymeantstorage.GetFillteredList(new PaymeantSearchModel { ClientID = _clientID });
var list = new List<ReportPaymeantProductsViewModel>();
foreach (var paymeant in paymeants) {
var record = new ReportPaymeantProductsViewModel {
PaymeantID = paymeant.ID,
Products = new(),
TotalCount = 0
};
foreach (var product in paymeant.PayProductList) {
record.Products.Add(new(product.Value.Item1.ProductName, product.Value.Item2));
record.TotalCount += product.Value.Item2;
}
list.Add(record);
}
return list;
}
public byte[]? SavePaymeantToExcelFile(int _clientID)
{
var document = _saveToExcel.CreateReport(new ExcelInfoClient
{
Title = "Список оплат",
PaymeantProducts = GetPaymeantProducts(_clientID)
});
return document;
}
public byte[]? SavePaymeantToWordFile(int _clientID)
{
var document = _saveToWord.CreateDoc(new WordInfoClient {
Title = "Список оплат и товаров",
ListPaymeant = _paymeantstorage.GetFillteredList(new PaymeantSearchModel { ClientID = _clientID }),
});
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;
}
}
}