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 _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; _purchaseStorage = purchaseStorage;
_buildStorage = buildStorage; _buildStorage = buildStorage;
_saveToExcel = saveToExcel; _saveToExcel = saveToExcel;
_saveToWord = saveToWord; _saveToWord = saveToWord;
_saveToPdf = saveToPdf; _saveToPdf = saveToPdf;
_mailSender = mailSender;
} }
/// <summary> /// <summary>
@ -149,9 +151,24 @@ namespace HardwareShopContracts.BusinessLogicsContracts
/// Сохранение отчёта по покупкам в файл-Pdf /// Сохранение отчёта по покупкам в файл-Pdf
/// </summary> /// </summary>
/// <param name="model"></param> /// <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 HardwareShopBusinessLogic.OfficePackage.HelperModels;
using MigraDoc.Rendering;
namespace HardwareShopBusinessLogic.OfficePackage namespace HardwareShopBusinessLogic.OfficePackage
{ {
public abstract class AbstractSaveToPdf public abstract class AbstractSaveToPdf
{ {
//public void CreateDoc(PdfInfo info) public byte[] GetEquipmentReportFile(PdfInfo info)
//{ {
// CreatePdf(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 });
// CreateTable(new List<string> { "2cm", "3cm", "6cm", "3cm", "3cm" }); CreateParagraph(new PdfParagraph
{
Text = info.Title,
Style = "NormalTitle",
ParagraphAlignment = PdfParagraphAlignmentType.Center
});
// CreateRow(new PdfRowParameters CreateParagraph(new PdfParagraph
// { {
// Texts = new List<string> { "Номер", "Дата заказа", "Блюдо", "Сумма", "Статус" }, Text = $"за период с {info.DateFrom.ToShortDateString()} " +
// Style = "NormalTitle", $"по {info.DateTo.ToShortDateString()}",
// ParagraphAlignment = PdfParagraphAlignmentType.Center Style = "Normal",
// }); ParagraphAlignment = PdfParagraphAlignmentType.Center
});
// foreach (var order in info.Orders) CreateTable(new List<string> { "5cm", "5cm", "5cm" });
// {
// 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 });
// SavePdf(info); CreateRow(new PdfRowParameters
//} {
Texts = new List<string> { "Комплектация", "Автомобили", "Работы" },
Style = "NormalTitle",
ParagraphAlignment = PdfParagraphAlignmentType.Center
});
//public void CreateOrdersGroupedByDateDoc(PdfInfo info) foreach (var record in info.ReportPurchases)
//{ {
// CreatePdf(info); List<string> comments = record.Comments;
// CreateParagraph(new PdfParagraph { Text = info.Title, Style = "NormalTitle", ParagraphAlignment = PdfParagraphAlignmentType.Center }); 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> /// <summary>
/// Создание doc-файла /// Создание doc-файла
@ -92,5 +109,11 @@ namespace HardwareShopBusinessLogic.OfficePackage
/// </summary> /// </summary>
/// <param name="info"></param> /// <param name="info"></param>
protected abstract void SavePdf(PdfInfo info); 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.RenderDocument();
renderer.PdfDocument.Save(info.FileName); 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 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; } public int UserId { get; set; }
} }

View File

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

View File

@ -89,6 +89,12 @@ namespace HardwareShopRestApi.Controllers
throw; 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); List<ReportPurchaseViewModel>? list = APIClient.PostRequestWithResult<ReportBindingModel, List<ReportPurchaseViewModel>>("api/report/getpurchasereportdata", reportModel);
return list; 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", () => { sendByMailButton.addEventListener("click", () => {
// const dateFrom = new Date(dateFromInput.value); const dateFrom = new Date(dateFromInput.value);
// const dateTo = new Date(dateToInput.value); const dateTo = new Date(dateToInput.value);
// const reportModel = { const reportModel = {
// "DateFrom": dateFrom, "DateFrom": dateFrom,
// "DateTo": dateTo "DateTo": dateTo
// }; };
// if (!validate(reportModel)) { if (!validate(reportModel)) {
// return; return;
// } }
// $.ajax({ $.ajax({
// url: "/home/sendbymailepurchasereport", url: "/home/sendbymailepurchasereport",
// type: "POST", type: "POST",
// contentType: "application/json", contentType: "application/json",
// data: JSON.stringify(reportModel) data: JSON.stringify(reportModel)
// }).done(() => { }).done(() => {
// }); });
//}); });
dateFromInput.addEventListener("input", () => { dateFromInput.addEventListener("input", () => {
errorDivShell.style.gridTemplateRows = "0fr"; errorDivShell.style.gridTemplateRows = "0fr";