This commit is contained in:
Николай 2023-05-18 18:01:28 +04:00
parent f6657eaa0d
commit b686555cf7
8 changed files with 140 additions and 75 deletions

View File

@ -23,15 +23,17 @@ namespace HardwareShopContracts.BusinessLogicsContracts
private readonly AbstractSaveToWord _saveToWord;
private readonly AbstractSaveToWord _saveToPdf;
private readonly AbstractSaveToPdf _saveToPdf;
private readonly MailSender _mailSender;
public WorkerReportLogic(IPurchaseStorage purchaseStorage, IBuildStorage buildStorage, AbstractSaveToWord saveToPdf, AbstractSaveToExcel saveToExcel, AbstractSaveToWord saveToWord)
public WorkerReportLogic(IPurchaseStorage purchaseStorage, MailSender mailSender, IBuildStorage buildStorage, AbstractSaveToPdf saveToPdf, AbstractSaveToExcel saveToExcel, AbstractSaveToWord saveToWord)
{
_purchaseStorage = purchaseStorage;
_buildStorage = buildStorage;
_saveToExcel = saveToExcel;
_saveToWord = saveToWord;
_saveToPdf = saveToPdf;
_mailSender = mailSender;
}
/// <summary>
@ -149,9 +151,24 @@ namespace HardwareShopContracts.BusinessLogicsContracts
/// Сохранение отчёта по покупкам в файл-Pdf
/// </summary>
/// <param name="model"></param>
public byte[] SendByMailPurchaseReport(ReportBindingModel model)
public void SendByMailPurchaseReport(ReportBindingModel model)
{
throw new NotImplementedException();
byte[] file = _saveToPdf.GetEquipmentReportFile(new()
{
FileName = model.FileName,
Title = "Отчет по покупкам",
DateFrom = model.DateFrom,
DateTo = model.DateTo,
ReportPurchases = GetPurchase(model)
});
// _mailSender.SendMailAsync(new()
// {
// MailAddress = reportModel.UserEmail,
// Subject = "Отчет по покупкам",
// Text = $"За период с {reportModel.DateFrom.ToShortDateString()} " +
//$"по {reportModel.DateTo.ToShortDateString()}.",
// File = file
// });
}
}
}

View File

@ -1,65 +1,82 @@
using HardwareShopBusinessLogic.OfficePackage.HelperEnums;
using DocumentFormat.OpenXml.EMMA;
using HardwareShopBusinessLogic.OfficePackage.HelperEnums;
using HardwareShopBusinessLogic.OfficePackage.HelperModels;
using MigraDoc.Rendering;
namespace HardwareShopBusinessLogic.OfficePackage
{
public abstract class AbstractSaveToPdf
{
//public void CreateDoc(PdfInfo info)
//{
// CreatePdf(info);
// CreateParagraph(new PdfParagraph { Text = info.Title, Style = "NormalTitle", ParagraphAlignment = PdfParagraphAlignmentType.Center });
// CreateParagraph(new PdfParagraph { Text = $"с {info.DateFrom.ToShortDateString()} по {info.DateTo.ToShortDateString()}", Style = "Normal", ParagraphAlignment = PdfParagraphAlignmentType.Center });
public byte[] GetEquipmentReportFile(PdfInfo info)
{
CreatePdf(info);
// CreateTable(new List<string> { "2cm", "3cm", "6cm", "3cm", "3cm" });
CreateParagraph(new PdfParagraph
{
Text = info.Title,
Style = "NormalTitle",
ParagraphAlignment = PdfParagraphAlignmentType.Center
});
// CreateRow(new PdfRowParameters
// {
// Texts = new List<string> { "Номер", "Дата заказа", "Блюдо", "Сумма", "Статус" },
// Style = "NormalTitle",
// ParagraphAlignment = PdfParagraphAlignmentType.Center
// });
CreateParagraph(new PdfParagraph
{
Text = $"за период с {info.DateFrom.ToShortDateString()} " +
$"по {info.DateTo.ToShortDateString()}",
Style = "Normal",
ParagraphAlignment = PdfParagraphAlignmentType.Center
});
// foreach (var order in info.Orders)
// {
// CreateRow(new PdfRowParameters
// {
// Texts = new List<string> { order.Id.ToString(), order.DateCreate.ToShortDateString(), order.DishName, order.Sum.ToString(), order.OrderStatus },
// Style = "Normal",
// ParagraphAlignment = PdfParagraphAlignmentType.Left
// });
// }
// CreateParagraph(new PdfParagraph { Text = $"Итого: {info.Orders.Sum(x => x.Sum)}\t", Style = "Normal", ParagraphAlignment = PdfParagraphAlignmentType.Rigth });
CreateTable(new List<string> { "5cm", "5cm", "5cm" });
// SavePdf(info);
//}
CreateRow(new PdfRowParameters
{
Texts = new List<string> { "Комплектация", "Автомобили", "Работы" },
Style = "NormalTitle",
ParagraphAlignment = PdfParagraphAlignmentType.Center
});
//public void CreateOrdersGroupedByDateDoc(PdfInfo info)
//{
// CreatePdf(info);
// CreateParagraph(new PdfParagraph { Text = info.Title, Style = "NormalTitle", ParagraphAlignment = PdfParagraphAlignmentType.Center });
foreach (var record in info.ReportPurchases)
{
List<string> comments = record.Comments;
List<string> components = record.Components;
int recordHeight = Math.Max(comments.Count + 1, components.Count + 1);
for (int i = 0; i < recordHeight; i++)
{
List<string> cellsData = new() { "", "", "", "", "" };
if (i == 0)
{
cellsData[0] = record.Id.ToString();
cellsData[1] = record.PurchaseDate.ToShortDateString();
cellsData[2] = record.PurchaseSum.ToString("0.00") + " р.";
CreateRow(new PdfRowParameters
{
Texts = cellsData,
Style = "Normal",
ParagraphAlignment = PdfParagraphAlignmentType.Left
});
continue;
}
int k = i - 1;
if (k < comments.Count)
{
cellsData[3] = comments[k];
}
if (k < components.Count)
{
cellsData[4] = components[k];
}
CreateRow(new PdfRowParameters
{
Texts = cellsData,
Style = "Normal",
ParagraphAlignment = PdfParagraphAlignmentType.Left
});
}
}
// CreateTable(new List<string> { "3cm", "3cm", "3cm" });
return GetFile(info);
}
// CreateRow(new PdfRowParameters
// {
// Texts = new List<string> { "Дата заказов", "Количество заказов", "Сумма" },
// Style = "NormalTitle",
// ParagraphAlignment = PdfParagraphAlignmentType.Center
// });
// foreach (var order in info.OrdersGroupedByDate)
// {
// CreateRow(new PdfRowParameters
// {
// Texts = new List<string> { order.DateCreate.ToShortDateString(), order.Count.ToString(), order.Sum.ToString() },
// Style = "Normal",
// ParagraphAlignment = PdfParagraphAlignmentType.Left
// });
// }
// SavePdf(info);
//}
/// <summary>
/// Создание doc-файла
@ -92,5 +109,11 @@ namespace HardwareShopBusinessLogic.OfficePackage
/// </summary>
/// <param name="info"></param>
protected abstract void SavePdf(PdfInfo info);
/// <summary>
/// Сохранение отправляем файл
/// </summary>
/// <param name="info"></param>
protected abstract byte[] GetFile(PdfInfo info);
}
}

View File

@ -110,5 +110,13 @@ namespace HardwareShopBusinessLogic.OfficePackage.Implements
renderer.RenderDocument();
renderer.PdfDocument.Save(info.FileName);
}
protected override byte[] GetFile(PdfInfo info)
{
SavePdf(info);
byte[] file = File.ReadAllBytes(info.FileName);
File.Delete(info.FileName);
return file;
}
}
}

View File

@ -4,9 +4,9 @@
{
public string FileName { get; set; } = string.Empty;
public DateTime? DateFrom { get; set; }
public DateTime DateFrom { get; set; }
public DateTime? DateTo { get; set; }
public DateTime DateTo { get; set; }
public int UserId { get; set; }
}

View File

@ -35,6 +35,6 @@ namespace HardwareShopContracts.BusinessLogicsContracts
/// Сохранение отчёта по покупкам в файл-Pdf
/// </summary>
/// <param name="model"></param>
byte[] SendByMailPurchaseReport(ReportBindingModel model);
void SendByMailPurchaseReport(ReportBindingModel model);
}
}

View File

@ -89,6 +89,12 @@ namespace HardwareShopRestApi.Controllers
throw;
}
}
[HttpPost]
public void SendByMailPurchaseReport(ReportBindingModel reportModel)
{
_reportWorkerLogic.SendByMailPurchaseReport(reportModel);
}
}
}

View File

@ -553,5 +553,16 @@ namespace HardwareShopWorkerApp.Controllers
List<ReportPurchaseViewModel>? list = APIClient.PostRequestWithResult<ReportBindingModel, List<ReportPurchaseViewModel>>("api/report/getpurchasereportdata", reportModel);
return list;
}
[HttpPost]
public void SendByMailPurchaseReport([FromBody] ReportBindingModel reportModel)
{
if (APIClient.User == null)
{
return;
}
reportModel.UserId = APIClient.User.Id;
APIClient.PostRequest("api/report/SendByMailPurchaseReport", reportModel);
}
}
}

View File

@ -87,24 +87,24 @@
});
});
//sendByMailButton.addEventListener("click", () => {
// const dateFrom = new Date(dateFromInput.value);
// const dateTo = new Date(dateToInput.value);
// const reportModel = {
// "DateFrom": dateFrom,
// "DateTo": dateTo
// };
// if (!validate(reportModel)) {
// return;
// }
// $.ajax({
// url: "/home/sendbymailepurchasereport",
// type: "POST",
// contentType: "application/json",
// data: JSON.stringify(reportModel)
// }).done(() => {
// });
//});
sendByMailButton.addEventListener("click", () => {
const dateFrom = new Date(dateFromInput.value);
const dateTo = new Date(dateToInput.value);
const reportModel = {
"DateFrom": dateFrom,
"DateTo": dateTo
};
if (!validate(reportModel)) {
return;
}
$.ajax({
url: "/home/sendbymailepurchasereport",
type: "POST",
contentType: "application/json",
data: JSON.stringify(reportModel)
}).done(() => {
});
});
dateFromInput.addEventListener("input", () => {
errorDivShell.style.gridTemplateRows = "0fr";