From ac028b54eed51362ba5100f748b34a3d69d4f37d Mon Sep 17 00:00:00 2001 From: Denis Date: Thu, 22 Jun 2023 02:38:03 +0400 Subject: [PATCH] report complete --- .../BusinessLogics/ReportLogic.cs | 20 ++- .../OfficePackage/AbstractSaveToExcel.cs | 11 +- .../OfficePackage/AbstractSaveToWord.cs | 11 +- .../OfficePackage/Implements/SaveToExcel.cs | 12 +- .../OfficePackage/Implements/SaveToWord.cs | 12 +- .../BindingModels/ReportBindingModel.cs | 5 +- .../BusinessLogicsContracts/IReportLogic.cs | 11 +- Canteen/CanteenManagerApp/APIClient.cs | 19 ++ .../Controllers/HomeController.cs | 36 ++-- .../Views/Home/Report.cshtml | 147 ++++++++++++---- Canteen/CanteenManagerApp/wwwroot/js/site.js | 73 +++++++- .../Controllers/MainController.cs | 142 +++++++++------ Canteen/CanteenVisitorApp/APIClient.cs | 19 ++ .../Controllers/HomeController.cs | 23 +-- .../Views/Home/Report.cshtml | 162 ++++++++++++++---- Canteen/CanteenVisitorApp/wwwroot/js/site.js | 73 +++++++- 16 files changed, 581 insertions(+), 195 deletions(-) diff --git a/Canteen/CanteenBusinessLogic/BusinessLogics/ReportLogic.cs b/Canteen/CanteenBusinessLogic/BusinessLogics/ReportLogic.cs index e9e4ee9..e1baa5d 100644 --- a/Canteen/CanteenBusinessLogic/BusinessLogics/ReportLogic.cs +++ b/Canteen/CanteenBusinessLogic/BusinessLogics/ReportLogic.cs @@ -211,37 +211,41 @@ namespace CanteenBusinessLogic.BusinessLogics Cooks = GetCooksPCView(model) }); } - public void saveCooksToExcel(ReportBindingModel model) + public byte[] saveCooksToExcel(ReportBindingModel model) { - saveToExcel.CreateCooksReport(new ExcelInfo() + byte[] report = saveToExcel.CreateCooksReport(new ExcelInfo() { Title = "Список поваров:", Cooks = GetCooksByLunches(model) }); + return report; } - public void saveOrdersToExcel(ReportBindingModel model) + public byte[] saveOrdersToExcel(ReportBindingModel model) { - saveToExcel.CreateOrdersReport(new ExcelInfo() + byte[] report = saveToExcel.CreateOrdersReport(new ExcelInfo() { Title = "Список заказов:", Orders = GetOrdersByProducts(model) }); + return report; } - public void saveCooksToWord(ReportBindingModel model) + public byte[] saveCooksToWord(ReportBindingModel model) { - saveToWord.CreateCooksDoc(new WordInfo() + byte[] report = saveToWord.CreateCooksDoc(new WordInfo() { Title = "Список поваров", Cooks = GetCooksByLunches(model) }); + return report; } - public void saveOrdersToWord(ReportBindingModel model) + public byte[] saveOrdersToWord(ReportBindingModel model) { - saveToWord.CreateOrdersDoc(new WordInfo() + byte[] report = saveToWord.CreateOrdersDoc(new WordInfo() { Title = "Список заказов", Orders = GetOrdersByProducts(model) }); + return report; } } } diff --git a/Canteen/CanteenBusinessLogic/OfficePackage/AbstractSaveToExcel.cs b/Canteen/CanteenBusinessLogic/OfficePackage/AbstractSaveToExcel.cs index de7adda..b108fcc 100644 --- a/Canteen/CanteenBusinessLogic/OfficePackage/AbstractSaveToExcel.cs +++ b/Canteen/CanteenBusinessLogic/OfficePackage/AbstractSaveToExcel.cs @@ -11,7 +11,7 @@ namespace CanteenBusinessLogic.OfficePackage { public abstract class AbstractSaveToExcel { - public void CreateCooksReport(ExcelInfo info) + public byte[] CreateCooksReport(ExcelInfo info) { CreateExcel(info); InsertCellInWorksheet(new ExcelCellParameters @@ -75,10 +75,10 @@ namespace CanteenBusinessLogic.OfficePackage rowIndex++; } } - SaveExcel(info); + return GetFile(); } - public void CreateOrdersReport(ExcelInfo info) + public byte[] CreateOrdersReport(ExcelInfo info) { CreateExcel(info); InsertCellInWorksheet(new ExcelCellParameters @@ -141,11 +141,12 @@ namespace CanteenBusinessLogic.OfficePackage rowIndex++; } } - SaveExcel(info); + return GetFile(); } protected abstract void CreateExcel(ExcelInfo info); protected abstract void InsertCellInWorksheet(ExcelCellParameters excelParams); protected abstract void MergeCells(ExcelMergeParameters excelParams); - protected abstract void SaveExcel(ExcelInfo info); + protected abstract void SaveExcel(); + protected abstract byte[] GetFile(); } } diff --git a/Canteen/CanteenBusinessLogic/OfficePackage/AbstractSaveToWord.cs b/Canteen/CanteenBusinessLogic/OfficePackage/AbstractSaveToWord.cs index 9089ee6..e6341ae 100644 --- a/Canteen/CanteenBusinessLogic/OfficePackage/AbstractSaveToWord.cs +++ b/Canteen/CanteenBusinessLogic/OfficePackage/AbstractSaveToWord.cs @@ -11,7 +11,7 @@ namespace CanteenBusinessLogic.OfficePackage { public abstract class AbstractSaveToWord { - public void CreateCooksDoc(WordInfo info) + public byte[] CreateCooksDoc(WordInfo info) { CreateWord(info); CreateParagraph(new WordParagraph @@ -72,9 +72,9 @@ namespace CanteenBusinessLogic.OfficePackage } } - SaveWord(info); + return GetFile(); } - public void CreateOrdersDoc(WordInfo info) + public byte[] CreateOrdersDoc(WordInfo info) { CreateWord(info); CreateParagraph(new WordParagraph @@ -136,10 +136,11 @@ namespace CanteenBusinessLogic.OfficePackage } } - SaveWord(info); + return GetFile(); } protected abstract void CreateWord(WordInfo info); protected abstract void CreateParagraph(WordParagraph paragraph); - protected abstract void SaveWord(WordInfo info); + protected abstract void SaveWord(); + protected abstract byte[] GetFile(); } } diff --git a/Canteen/CanteenBusinessLogic/OfficePackage/Implements/SaveToExcel.cs b/Canteen/CanteenBusinessLogic/OfficePackage/Implements/SaveToExcel.cs index 1d579e5..4729a4f 100644 --- a/Canteen/CanteenBusinessLogic/OfficePackage/Implements/SaveToExcel.cs +++ b/Canteen/CanteenBusinessLogic/OfficePackage/Implements/SaveToExcel.cs @@ -15,6 +15,7 @@ namespace CanteenBusinessLogic.OfficePackage.Implements { public class SaveToExcel : AbstractSaveToExcel { + private readonly string tempFileName = "temp.docx"; private SpreadsheetDocument spreadsheetDocument; private SharedStringTablePart shareStringPart; private Worksheet worksheet; @@ -192,7 +193,7 @@ namespace CanteenBusinessLogic.OfficePackage.Implements } protected override void CreateExcel(ExcelInfo info) { - spreadsheetDocument = SpreadsheetDocument.Create(info.FileName, SpreadsheetDocumentType.Workbook); + spreadsheetDocument = SpreadsheetDocument.Create(tempFileName, SpreadsheetDocumentType.Workbook); // Создаем книгу (в ней хранятся листы) var workbookpart = spreadsheetDocument.AddWorkbookPart(); workbookpart.Workbook = new Workbook(); @@ -289,10 +290,17 @@ namespace CanteenBusinessLogic.OfficePackage.Implements }; mergeCells.Append(mergeCell); } - protected override void SaveExcel(ExcelInfo info) + protected override void SaveExcel() { spreadsheetDocument.WorkbookPart.Workbook.Save(); spreadsheetDocument.Close(); } + protected override byte[] GetFile() + { + SaveExcel(); + byte[] file = File.ReadAllBytes(tempFileName); + File.Delete(tempFileName); + return file; + } } } diff --git a/Canteen/CanteenBusinessLogic/OfficePackage/Implements/SaveToWord.cs b/Canteen/CanteenBusinessLogic/OfficePackage/Implements/SaveToWord.cs index d496af8..7727b84 100644 --- a/Canteen/CanteenBusinessLogic/OfficePackage/Implements/SaveToWord.cs +++ b/Canteen/CanteenBusinessLogic/OfficePackage/Implements/SaveToWord.cs @@ -13,6 +13,7 @@ namespace CanteenBusinessLogic.OfficePackage.Implements { public class SaveToWord : AbstractSaveToWord { + private readonly string tempFileName = "temp.docx"; private WordprocessingDocument wordDocument; private Body docBody; private static JustificationValues GetJustificationValues(WordJustificationType type) @@ -54,7 +55,7 @@ namespace CanteenBusinessLogic.OfficePackage.Implements } protected override void CreateWord(WordInfo info) { - wordDocument = WordprocessingDocument.Create(info.FileName, WordprocessingDocumentType.Document); + wordDocument = WordprocessingDocument.Create(tempFileName, WordprocessingDocumentType.Document); MainDocumentPart mainPart = wordDocument.AddMainDocumentPart(); mainPart.Document = new Document(); docBody = mainPart.Document.AppendChild(new Body()); @@ -85,11 +86,18 @@ namespace CanteenBusinessLogic.OfficePackage.Implements docBody.AppendChild(docParagraph); } } - protected override void SaveWord(WordInfo info) + protected override void SaveWord() { docBody.AppendChild(CreateSectionProperties()); wordDocument.MainDocumentPart.Document.Save(); wordDocument.Close(); } + protected override byte[] GetFile() + { + SaveWord(); + byte[] file = File.ReadAllBytes(tempFileName); + File.Delete(tempFileName); + return file; + } } } diff --git a/Canteen/CanteenContracts/BindingModels/ReportBindingModel.cs b/Canteen/CanteenContracts/BindingModels/ReportBindingModel.cs index 68b6f5d..8cf194e 100644 --- a/Canteen/CanteenContracts/BindingModels/ReportBindingModel.cs +++ b/Canteen/CanteenContracts/BindingModels/ReportBindingModel.cs @@ -10,10 +10,11 @@ namespace CanteenContracts.BindingModels public class ReportBindingModel { public string? FileName { get; set; } + public string? FileType { get; set; } public DateTime? DateAfter { get; set; } public DateTime? DateBefore { get; set; } public int UserId { get; set; } - public List? LunchId { get; set; } - public List? ProductId { get; set; } + public int[]? LunchId { get; set; } + public int[]? ProductId { get; set; } } } diff --git a/Canteen/CanteenContracts/BusinessLogicsContracts/IReportLogic.cs b/Canteen/CanteenContracts/BusinessLogicsContracts/IReportLogic.cs index e1cbac4..577b824 100644 --- a/Canteen/CanteenContracts/BusinessLogicsContracts/IReportLogic.cs +++ b/Canteen/CanteenContracts/BusinessLogicsContracts/IReportLogic.cs @@ -12,12 +12,13 @@ namespace CanteenContracts.BusinessLogicsContracts public interface IReportLogic { public List GetCooksByLunches(ReportBindingModel model); - List GetLunchesPCView(ReportBindingModel model); + public List GetLunchesPCView(ReportBindingModel model); + public List GetCooksPCView(ReportBindingModel model); void saveLunchesToPdfFile(ReportBindingModel model); void saveCooksToPdfFile(ReportBindingModel model); - void saveCooksToWord(ReportBindingModel model); - void saveCooksToExcel(ReportBindingModel model); - public void saveOrdersToExcel(ReportBindingModel model); - public void saveOrdersToWord(ReportBindingModel model); + byte[] saveCooksToWord(ReportBindingModel model); + byte[] saveCooksToExcel(ReportBindingModel model); + public byte[] saveOrdersToExcel(ReportBindingModel model); + public byte[] saveOrdersToWord(ReportBindingModel model); } } diff --git a/Canteen/CanteenManagerApp/APIClient.cs b/Canteen/CanteenManagerApp/APIClient.cs index 2151e9e..0334004 100644 --- a/Canteen/CanteenManagerApp/APIClient.cs +++ b/Canteen/CanteenManagerApp/APIClient.cs @@ -43,5 +43,24 @@ namespace CanteenManagerApp throw new Exception(result); } } + public static O? PostRequestWithResult(string requestUrl, I model) + { + var json = JsonConvert.SerializeObject(model); + var data = new StringContent(json, Encoding.UTF8, "application/json"); + + var response = _client.PostAsync(requestUrl, data); + + var result = response.Result.Content.ReadAsStringAsync().Result; + + if (response.Result.IsSuccessStatusCode) + { + var temp = JsonConvert.DeserializeObject(result); + return temp; + } + else + { + return default; + } + } } } diff --git a/Canteen/CanteenManagerApp/Controllers/HomeController.cs b/Canteen/CanteenManagerApp/Controllers/HomeController.cs index a632222..2a68c19 100644 --- a/Canteen/CanteenManagerApp/Controllers/HomeController.cs +++ b/Canteen/CanteenManagerApp/Controllers/HomeController.cs @@ -487,36 +487,23 @@ namespace CanteenManagerApp.Controllers return View(new ReportBindingModel()); } [HttpPost] - public void ReportPdf(ReportBindingModel model) + public List ReportPdf([FromBody] ReportBindingModel model) { model.UserId = APIClient.Manager.Id; - APIClient.PostRequest("api/main/SaveCooksToPDF", model); - Response.Redirect("Index"); + List lunches = APIClient.PostRequestWithResult>("api/main/getCooksForPdf", model); + return lunches; + } + [HttpPost] + public int[]? SaveReport([FromBody] ReportBindingModel model) + { + model.UserId = APIClient.Manager.Id; + byte[] file = APIClient.PostRequestWithResult("api/main/SaveOrdersToFile", model); + return file!.Select(b => (int)b).ToArray(); } [HttpPost] - public void ReportXsl(ReportBindingModel model) + public void ReportEmail([FromBody] ReportBindingModel model) { - model.UserId = APIClient.Manager.Id; - APIClient.PostRequest("api/main/SaveOrdersToXSL", model); - Response.Redirect("Index"); - } - - [HttpPost] - public void ReportWord(ReportBindingModel model) - { - model.UserId = APIClient.Manager.Id; - APIClient.PostRequest("api/main/SaveOrdersToWORD", model); - Response.Redirect("Index"); - } - - [HttpPost] - public void ReportEmail(ReportBindingModel model) - { - if (APIClient.Manager == null) - { - throw new Exception("Доступ возможен только авторизованным пользователям"); - } model.UserId = APIClient.Manager.Id; APIClient.PostRequest($"api/manager/SendEmail", new MailSendInfoBindingModel { @@ -524,7 +511,6 @@ namespace CanteenManagerApp.Controllers Subject = "Отчет", report = model }); - Response.Redirect("Report"); } } diff --git a/Canteen/CanteenManagerApp/Views/Home/Report.cshtml b/Canteen/CanteenManagerApp/Views/Home/Report.cshtml index b12edfb..e8122e2 100644 --- a/Canteen/CanteenManagerApp/Views/Home/Report.cshtml +++ b/Canteen/CanteenManagerApp/Views/Home/Report.cshtml @@ -1,42 +1,123 @@ -@using CanteenContracts.BindingModels; -@model ReportBindingModel - -@{ +@{ ViewBag.Title = "Report"; } -

Отчеты

- -@using (Html.BeginForm("Report", "Home", FormMethod.Post)) -{ -
-

Pdf

-
-
- @Html.LabelFor(m => m.DateAfter, "От") - @Html.TextBoxFor(m => m.DateAfter, new { type = "date", @class = "form-control" }) -
-
- -
-
- @Html.LabelFor(m => m.DateBefore, "До") - @Html.TextBoxFor(m => m.DateBefore, new { type = "date", @class = "form-control" }) -
-
-
-
- - -
+ +

Отчеты

Word, Excel

- @Html.LabelFor(m => m.ProductId, "Выбранные продукты") - @Html.DropDownListFor(m => m.ProductId, new SelectList(ViewBag.ProductList, "Id", "ProductName"), new { @class = "form-control", multiple = "multiple" }) + +
- - + +
-} \ No newline at end of file + +
+

Pdf

+
+
+ + +
+
+ +
+
+ + +
+
+
+
+ + +
+ + + + + + + + + + +
Id повараФИО повараId обеда
+ + + + + + + + diff --git a/Canteen/CanteenManagerApp/wwwroot/js/site.js b/Canteen/CanteenManagerApp/wwwroot/js/site.js index ac49c18..69726eb 100644 --- a/Canteen/CanteenManagerApp/wwwroot/js/site.js +++ b/Canteen/CanteenManagerApp/wwwroot/js/site.js @@ -1,4 +1,71 @@ -// Please see documentation at https://docs.microsoft.com/aspnet/core/client-side/bundling-and-minification -// for details on configuring this project to bundle and minify static web assets. +const wordButton = document.getElementById("word-button"); +const excelButton = document.getElementById("excel-button"); +const productIdSelect = document.getElementById("ProductId"); +const wordMIME = "application/vnd.openxmlformats-officedocument.wordprocessingml.document"; +const excelMIME = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; -// Write your JavaScript code. +if (wordButton) { + wordButton.addEventListener("click", () => { + saveList("docx"); + console.log("сохранить в docx") + }); +} + +if (excelButton) { + excelButton.addEventListener("click", () => { + saveList("xlsx"); + }); +} + +const saveList = async function (fileType) { + let productIdSelect = document.getElementById("ProductId"); + if (!productIdSelect) { + console.log("Element with id 'ProductId' not found."); + return; + } + + let listModel = { + "ProductId": Array.from(productIdSelect.selectedOptions).map(option => option.value), + "FileType": fileType + }; + + $.ajax({ + url: `/home/SaveReport`, + type: 'POST', + contentType: 'application/json', + headers: { "Content-Type": "application/json" }, + data: JSON.stringify(listModel) + }).done((file) => { + let byteArray = new Uint8Array(file); + saveFile(byteArray, fileType); + }); +}; + +const saveFile = async function (bytes, fileType) { + if (window.showSaveFilePicker) { + let type; + if (fileType == "docx") { + type = { + description: "Microsoft Word (OpenXML)", + accept: { [wordMIME]: [".docx"] } + }; + } else if (fileType == "xlsx") { + type = { + description: "Microsoft Excel (OpenXML)", + accept: { [excelMIME]: [".xlsx"] } + }; + } + const opts = { + suggestedName: `equipment-purchase-list.${fileType}`, + types: [type], + }; + try { + const handle = await showSaveFilePicker(opts); + const writable = await handle.createWritable(); + await writable.write(bytes); + await writable.close(); + } catch (error) { + console.error("Error saving file:", error); + } + } +}; diff --git a/Canteen/CanteenRestApi/Controllers/MainController.cs b/Canteen/CanteenRestApi/Controllers/MainController.cs index 211b43c..44583cc 100644 --- a/Canteen/CanteenRestApi/Controllers/MainController.cs +++ b/Canteen/CanteenRestApi/Controllers/MainController.cs @@ -37,11 +37,11 @@ namespace CanteenRestApi.Controllers } [HttpPost] - public void SaveLunchesToPDF(ReportBindingModel model) + public List getLunchesForPdf(ReportBindingModel model) { try { - _reportLogic.saveLunchesToPdfFile(new ReportBindingModel() + return _reportLogic.GetLunchesPCView(new ReportBindingModel() { DateAfter = model.DateAfter, DateBefore = model.DateBefore, @@ -55,11 +55,11 @@ namespace CanteenRestApi.Controllers } } [HttpPost] - public void SaveCooksToPDF(ReportBindingModel model) + public List getCooksForPdf(ReportBindingModel model) { try { - _reportLogic.saveCooksToPdfFile(new ReportBindingModel() + return _reportLogic.GetCooksPCView(new ReportBindingModel() { DateAfter = model.DateAfter, DateBefore = model.DateBefore, @@ -74,81 +74,115 @@ namespace CanteenRestApi.Controllers } [HttpPost] - public void SaveCooksToXSL(ReportBindingModel model) + public byte[] SaveCooksToFile(ReportBindingModel model) { + byte[] report = Array.Empty(); try { - _reportLogic.saveCooksToExcel(new ReportBindingModel() + if (model.FileType.Equals("xlsx")) { - DateAfter = model.DateAfter, - DateBefore = model.DateBefore, - UserId = model.UserId, - LunchId = model.LunchId - }); + report = _reportLogic.saveCooksToExcel(new ReportBindingModel() + { + DateAfter = model.DateAfter, + DateBefore = model.DateBefore, + UserId = model.UserId, + LunchId = model.LunchId + }); + } + else if (model.FileType.Equals("docx")) + { + report = _reportLogic.saveCooksToWord(new ReportBindingModel() + { + DateAfter = model.DateAfter, + DateBefore = model.DateBefore, + UserId = model.UserId, + LunchId = model.LunchId + }); + } + return report; } catch (Exception ex) { + return report; _logger.LogError(ex, "Error during loading list of bouquets"); throw; } } + //[HttpPost] + //public void SaveCooksToWORD(ReportBindingModel model) + //{ + // byte[] report = Array.Empty(); + // try + // { + // _reportLogic.saveCooksToWord(new ReportBindingModel() + // { + // DateAfter = model.DateAfter, + // DateBefore = model.DateBefore, + // UserId = model.UserId, + // LunchId = model.LunchId + // }); + // } + // catch (Exception ex) + // { + // _logger.LogError(ex, "Error during loading list of bouquets"); + // throw; + // } + //} + [HttpPost] - public void SaveCooksToWORD(ReportBindingModel model) + public byte[] SaveOrdersToFile(ReportBindingModel model) { + byte[] report = Array.Empty(); try { - _reportLogic.saveCooksToWord(new ReportBindingModel() + if (model.FileType.Equals("xlsx")) { - DateAfter = model.DateAfter, - DateBefore = model.DateBefore, - UserId = model.UserId, - LunchId = model.LunchId - }); + report = _reportLogic.saveOrdersToExcel(new ReportBindingModel() + { + DateAfter = model.DateAfter, + DateBefore = model.DateBefore, + UserId = model.UserId, + ProductId = model.ProductId + }); + } + else if (model.FileType.Equals("docx")) + { + report = _reportLogic.saveOrdersToWord(new ReportBindingModel() + { + DateAfter = model.DateAfter, + DateBefore = model.DateBefore, + UserId = model.UserId, + ProductId = model.ProductId + }); + } + return report; } catch (Exception ex) { + return report; _logger.LogError(ex, "Error during loading list of bouquets"); throw; } } - [HttpPost] - public void SaveOrdersToXSL(ReportBindingModel model) - { - try - { - _reportLogic.saveOrdersToExcel(new ReportBindingModel() - { - UserId = model.UserId, - ProductId = model.ProductId - }); - - } - catch (Exception ex) - { - _logger.LogError(ex, "Error during loading list of bouquets"); - throw; - } - } - - [HttpPost] - public void SaveOrdersToWORD(ReportBindingModel model) - { - try - { - _reportLogic.saveOrdersToWord(new ReportBindingModel() - { - UserId = model.UserId, - ProductId = model.ProductId - }); - } - catch (Exception ex) - { - _logger.LogError(ex, "Error during loading list of bouquets"); - throw; - } - } + //[HttpPost] + //public void SaveOrdersToWORD(ReportBindingModel model) + //{ + // try + // { + // _reportLogic.saveOrdersToWord(new ReportBindingModel() + // { + // UserId = model.UserId, + // ProductId = model.ProductId + // }); + // } + // catch (Exception ex) + // { + // _logger.LogError(ex, "Error during loading list of bouquets"); + // throw; + // } + //} [HttpGet] public List? GetTablewareList(int visitorId) diff --git a/Canteen/CanteenVisitorApp/APIClient.cs b/Canteen/CanteenVisitorApp/APIClient.cs index 3fb1b19..cb2937e 100644 --- a/Canteen/CanteenVisitorApp/APIClient.cs +++ b/Canteen/CanteenVisitorApp/APIClient.cs @@ -40,5 +40,24 @@ namespace CanteenVisitorApp throw new Exception(result); } } + public static O? PostRequestWithResult(string requestUrl, I model) + { + var json = JsonConvert.SerializeObject(model); + var data = new StringContent(json, Encoding.UTF8, "application/json"); + + var response = _client.PostAsync(requestUrl, data); + + var result = response.Result.Content.ReadAsStringAsync().Result; + + if (response.Result.IsSuccessStatusCode) + { + var temp = JsonConvert.DeserializeObject(result); + return temp; + } + else + { + return default; + } + } } } diff --git a/Canteen/CanteenVisitorApp/Controllers/HomeController.cs b/Canteen/CanteenVisitorApp/Controllers/HomeController.cs index 9ec48a9..e58cd53 100644 --- a/Canteen/CanteenVisitorApp/Controllers/HomeController.cs +++ b/Canteen/CanteenVisitorApp/Controllers/HomeController.cs @@ -1,5 +1,6 @@ using CanteenContracts.BindingModels; using CanteenContracts.View; +using CanteenContracts.ViewModels; using CanteenVisitorApp.Models; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.Rendering; @@ -541,31 +542,23 @@ namespace CanteenVisitorApp.Controllers return View(new ReportBindingModel()); } [HttpPost] - public void ReportPdf(ReportBindingModel model) + public List ReportPdf([FromBody] ReportBindingModel model) { model.UserId = APIClient.Visitor.Id; - APIClient.PostRequest("api/main/SaveLunchesToPDF", model); - Response.Redirect("Index"); + List lunches = APIClient.PostRequestWithResult>("api/main/getLunchesForPdf", model); + return lunches; } [HttpPost] - public void ReportXsl(ReportBindingModel model) + public int[]? SaveReport([FromBody] ReportBindingModel model) { model.UserId = APIClient.Visitor.Id; - APIClient.PostRequest("api/main/SaveCooksToXSL", model); - Response.Redirect("Index"); + byte[] file = APIClient.PostRequestWithResult("api/main/SaveCooksToFile", model); + return file!.Select(b => (int)b).ToArray(); } [HttpPost] - public void ReportWord(ReportBindingModel model) - { - model.UserId = APIClient.Visitor.Id; - APIClient.PostRequest("api/main/SaveCooksToWORD", model); - Response.Redirect("Index"); - } - - [HttpPost] - public void ReportEmail(ReportBindingModel model) + public void ReportEmail([FromBody] ReportBindingModel model) { model.UserId = APIClient.Visitor.Id; APIClient.PostRequest($"api/visitor/SendEmail", new MailSendInfoBindingModel diff --git a/Canteen/CanteenVisitorApp/Views/Home/Report.cshtml b/Canteen/CanteenVisitorApp/Views/Home/Report.cshtml index 3bfc90b..2df6888 100644 --- a/Canteen/CanteenVisitorApp/Views/Home/Report.cshtml +++ b/Canteen/CanteenVisitorApp/Views/Home/Report.cshtml @@ -1,42 +1,138 @@ -@using CanteenContracts.BindingModels; -@model ReportBindingModel - -@{ +@{ ViewBag.Title = "Report"; } -

Отчеты

- -@using (Html.BeginForm("Report", "Home", FormMethod.Post)) -{ -
-

Pdf

-
-
- @Html.LabelFor(m => m.DateAfter, "От") - @Html.TextBoxFor(m => m.DateAfter, new { type = "date", @class = "form-control" }) -
-
- -
-
- @Html.LabelFor(m => m.DateBefore, "До") - @Html.TextBoxFor(m => m.DateBefore, new { type = "date", @class = "form-control" }) -
-
-
-
- - -
+ +

Отчеты

Word, Excel

- @Html.LabelFor(m => m.LunchId, "Выбранные обеды") - @Html.DropDownListFor(m => m.LunchId, new SelectList(ViewBag.LunchList, "Id", "LunchName"), new { @class = "form-control", multiple = "multiple" }) + +
- - + +
-} \ No newline at end of file + +
+

Pdf

+
+
+ + +
+
+ +
+
+ + +
+
+
+
+ + +
+ + + + + + + + + + + +
Дата обедаСумма обедаId заказаФИО повара
+ + + + + + + + diff --git a/Canteen/CanteenVisitorApp/wwwroot/js/site.js b/Canteen/CanteenVisitorApp/wwwroot/js/site.js index ac49c18..9ec0e40 100644 --- a/Canteen/CanteenVisitorApp/wwwroot/js/site.js +++ b/Canteen/CanteenVisitorApp/wwwroot/js/site.js @@ -1,4 +1,71 @@ -// Please see documentation at https://docs.microsoft.com/aspnet/core/client-side/bundling-and-minification -// for details on configuring this project to bundle and minify static web assets. +const wordButton = document.getElementById("word-button"); +const excelButton = document.getElementById("excel-button"); +const lunchIdSelect = document.getElementById("LunchId"); +const wordMIME = "application/vnd.openxmlformats-officedocument.wordprocessingml.document"; +const excelMIME = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; -// Write your JavaScript code. +if (wordButton) { + wordButton.addEventListener("click", () => { + saveList("docx"); + console.log("сохранить в docx") + }); +} + +if (excelButton) { + excelButton.addEventListener("click", () => { + saveList("xlsx"); + }); +} + +const saveList = async function (fileType) { + let lunchIdSelect = document.getElementById("LunchId"); + if (!lunchIdSelect) { + console.log("Element with id 'LunchId' not found."); + return; + } + + let listModel = { + "LunchId": Array.from(lunchIdSelect.selectedOptions).map(option => option.value), + "FileType": fileType + }; + + $.ajax({ + url: `/home/SaveReport`, + type: 'POST', + contentType: 'application/json', + headers: { "Content-Type": "application/json" }, + data: JSON.stringify(listModel) + }).done((file) => { + let byteArray = new Uint8Array(file); + saveFile(byteArray, fileType); + }); +}; + +const saveFile = async function (bytes, fileType) { + if (window.showSaveFilePicker) { + let type; + if (fileType == "docx") { + type = { + description: "Microsoft Word (OpenXML)", + accept: { [wordMIME]: [".docx"] } + }; + } else if (fileType == "xlsx") { + type = { + description: "Microsoft Excel (OpenXML)", + accept: { [excelMIME]: [".xlsx"] } + }; + } + const opts = { + suggestedName: `equipment-purchase-list.${fileType}`, + types: [type], + }; + try { + const handle = await showSaveFilePicker(opts); + const writable = await handle.createWritable(); + await writable.write(bytes); + await writable.close(); + } catch (error) { + console.error("Error saving file:", error); + } + } +};