CourseWorkElectronicsShop/ElectronicsShop/ElectronicsShopBusinessLogic/BusinessLogic/ReportEmployeeLogic.cs

121 lines
5.3 KiB
C#
Raw Normal View History

2024-07-29 21:12:31 +04:00
using DocumentFormat.OpenXml.Office.CustomUI;
using ElectronicsShopBusinessLogic.OfficePackage;
2024-06-01 02:22:36 +04:00
using ElectronicsShopBusinessLogic.OfficePackage.HelperModels;
using ElectronicsShopContracts.BindingModels;
using ElectronicsShopContracts.BusinessLogicContracts;
2024-07-29 21:12:31 +04:00
using ElectronicsShopContracts.SearchModels;
2024-06-01 02:22:36 +04:00
using ElectronicsShopContracts.StorageContracts;
using ElectronicsShopContracts.ViewModels;
2024-08-03 22:10:14 +04:00
using PdfSharp.Pdf;
2024-06-01 02:22:36 +04:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ElectronicsShopBusinessLogic.BusinessLogic
{
public class ReportEmployeeLogic : IReportEmployeeLogic
{
private readonly IProductStorage _productStorage;
2024-07-29 21:12:31 +04:00
private readonly IPaymeantStorage _paymeantStorage;
private readonly IOrderStorage _orderStorage;
2024-08-01 15:04:29 +04:00
private readonly AbstractSaveToPdfEmployee _saveToPdf;
2024-06-01 02:22:36 +04:00
private readonly AbstractSaveToWordEmployee _saveToWord;
private readonly AbstractSaveToExcelEmployee _saveToExcel;
2024-07-29 21:12:31 +04:00
public ReportEmployeeLogic(AbstractSaveToExcelEmployee abstractSaveToExcelEmployee, AbstractSaveToWordEmployee abstractSaveToWordEmployee,
2024-08-01 15:04:29 +04:00
AbstractSaveToPdfEmployee abstractSaveToPdfEmployee ,IProductStorage productStorage,
IPaymeantStorage paymeantStorage, IOrderStorage orderStorage) {
2024-06-01 02:22:36 +04:00
_productStorage = productStorage;
2024-07-29 21:12:31 +04:00
_paymeantStorage = paymeantStorage;
_orderStorage = orderStorage;
_saveToExcel = abstractSaveToExcelEmployee;
2024-06-01 02:22:36 +04:00
_saveToWord = abstractSaveToWordEmployee;
2024-08-01 15:04:29 +04:00
_saveToPdf = abstractSaveToPdfEmployee;
2024-06-01 02:22:36 +04:00
}
2024-07-29 21:12:31 +04:00
public List<ReportProductInPaymeantsViewModel> GetProducts(ReportProductBindingModel model)
2024-06-01 02:22:36 +04:00
{
2024-07-29 21:12:31 +04:00
var paymeants = _paymeantStorage.GetFillteredList(new PaymeantSearchModel {
DateFrom = model.DateFrom,
DateTo = model.DateTo,
});
List<PaymeantProduct> paymeantProductList = new();
foreach (var paymeant in paymeants) {
var order = _orderStorage.GetElement(new OrderSearchModel { ID = paymeant.OrderID }) ?? throw new Exception("Ошибка получения данных");
foreach (var product in order.ProductList) {
paymeantProductList.Add(new(paymeant.ID, product.Key, product.Value.Item2));
}
}
paymeantProductList.OrderBy(x => x.ProductID);
List<ReportProductInPaymeantsViewModel> ansProductsList = new();
List<string> productNames = new();
foreach (var pp in paymeantProductList) {
var product = _productStorage.GetElement(new ProductSearchModel { ID = pp.ProductID})
?? throw new Exception("Ошибка получения данных");
if (productNames.Contains(product.ProductName) == false) {
productNames.Add(product.ProductName);
// создаем запись
var record = new ReportProductInPaymeantsViewModel {
ProductName = product.ProductName,
Values = new(),
Total = 0
};
ansProductsList.Add(record);
}
// Ищем запись и вносим изменения
int index = ansProductsList.IndexOf(ansProductsList.First(x => x.ProductName == product.ProductName));
var paymeant = _paymeantStorage.GetElement(new PaymeantSearchModel { ID = pp.PaymeantID })
?? throw new Exception("Ошибка получения данных");
ansProductsList[index].Values.Add(new(pp.PaymeantID, pp.Count, paymeant.PayOption.ToString(), product.Price * pp.Count));
ansProductsList[index].Total++;
}
return ansProductsList;
}
2024-06-01 02:22:36 +04:00
2024-07-30 15:36:42 +04:00
public byte[]? SaveProductsToExcelFile(ReportProductBindingModel model)
2024-06-01 02:22:36 +04:00
{
2024-07-30 15:36:42 +04:00
var document = _saveToExcel.CreateReport(new ExcelInfoEmployee
2024-06-01 02:22:36 +04:00
{
2024-08-01 15:04:29 +04:00
Title = "Список оплат электротоваров",
2024-07-30 15:36:42 +04:00
ListProduct = GetProducts(model),
2024-08-01 16:03:03 +04:00
DateTo = model.DateTo,
DateFrom = model.DateFrom
2024-06-01 02:22:36 +04:00
});
2024-07-30 15:36:42 +04:00
return document;
2024-06-01 02:22:36 +04:00
}
2024-08-03 22:10:14 +04:00
public PdfDocument SaveProductsToPdfFile(ReportProductBindingModel model) {
2024-08-01 15:04:29 +04:00
var document = _saveToPdf.CreateDoc(new PdfInfoEmployee {
Title = "Список оплат электротоваров",
DateFrom = model.DateFrom,
DateTo = model.DateTo,
ListProduct = GetProducts(model),
FileName = "Report"
});
2024-08-03 22:10:14 +04:00
return document;
2024-08-01 15:04:29 +04:00
}
public byte[]? SaveProductsToWordFile(ReportProductBindingModel model)
2024-06-01 02:22:36 +04:00
{
2024-07-29 21:12:31 +04:00
var document = _saveToWord.CreateDoc(new WordInfoEmployee {
Title = "Список оплат электротоваров",
2024-08-01 16:03:03 +04:00
ListProduct = GetProducts(model),
DateFrom = model.DateFrom,
DateTo = model.DateTo,
2024-06-01 02:22:36 +04:00
});
2024-07-29 21:12:31 +04:00
return document;
2024-06-01 02:22:36 +04:00
}
2024-07-29 21:12:31 +04:00
}
2024-06-01 02:22:36 +04:00
}