From 1de0fc12391a8bdad976da30909dc7ef6b931728 Mon Sep 17 00:00:00 2001 From: Zakharov_Rostislav Date: Mon, 27 May 2024 02:35:01 +0400 Subject: [PATCH 1/3] add sending pdf report to email --- .../MailWorker/MailKitWorker.cs | 2 +- .../OfficePackage/AbstractSaveToPdf.cs | 147 +++++++++++++++++- .../Controllers/HomeController.cs | 4 +- .../TransfersWithdrawalsListReport.cshtml | 2 +- 4 files changed, 144 insertions(+), 11 deletions(-) diff --git a/Bank/BankBusinessLogic/MailWorker/MailKitWorker.cs b/Bank/BankBusinessLogic/MailWorker/MailKitWorker.cs index 5c0799b..96403d7 100644 --- a/Bank/BankBusinessLogic/MailWorker/MailKitWorker.cs +++ b/Bank/BankBusinessLogic/MailWorker/MailKitWorker.cs @@ -28,7 +28,7 @@ namespace BankBusinessLogic.MailWorker objMailMessage.Body = info.Text; objMailMessage.SubjectEncoding = Encoding.UTF8; objMailMessage.BodyEncoding = Encoding.UTF8; - Attachment attachment = new Attachment("C:\\Users\\pdffile.pdf", new ContentType(MediaTypeNames.Application.Pdf)); + Attachment attachment = new Attachment("C:\\forpdf\\pdffile.pdf", new ContentType(MediaTypeNames.Application.Pdf)); objMailMessage.Attachments.Add(attachment); objSmtpClient.UseDefaultCredentials = false; objSmtpClient.EnableSsl = true; diff --git a/Bank/BankBusinessLogic/OfficePackage/AbstractSaveToPdf.cs b/Bank/BankBusinessLogic/OfficePackage/AbstractSaveToPdf.cs index e83290c..10da23d 100644 --- a/Bank/BankBusinessLogic/OfficePackage/AbstractSaveToPdf.cs +++ b/Bank/BankBusinessLogic/OfficePackage/AbstractSaveToPdf.cs @@ -14,6 +14,7 @@ namespace BankBusinessLogic.OfficePackage public void CreateCreateTransfersDocDoc(PdfInfo info) { CreatePdf(info); + //title CreateParagraph(new PdfParagraph { Text = info.Title, @@ -24,30 +25,162 @@ namespace BankBusinessLogic.OfficePackage { Text = $"с{ info.DateFrom.ToShortDateString() } по { info.DateTo.ToShortDateString() }", Style = "Normal", ParagraphAlignment = PdfParagraphAlignmentType.Center }); - CreateTable(new List { "2cm", "3cm", "6cm", "3cm", "3cm" }); + //sender transfers + CreateParagraph(new PdfParagraph + { + Text = "Переводы на другие счета", + Style = "NormalTitle", + ParagraphAlignment = PdfParagraphAlignmentType.Left + }); + CreateTable(new List { "2cm", "3cm", "6cm", "3cm", "3cm" }); CreateRow(new PdfRowParameters { - Texts = new List { "Номер", "Дата заказа", "Компьютер", "Сумма", "Статус" }, + Texts = new List { "Номер счёта", "Номер перевода", "Время перевода", "Сумма", "Номер счёта получателя" }, Style = "NormalTitle", ParagraphAlignment = PdfParagraphAlignmentType.Center }); - foreach (var entry in info.OperationsRequests) + foreach (var report in info.TransfersWithdrawals) { CreateRow(new PdfRowParameters { - Texts = new List { entry.CardNumber.ToString(), }, + Texts = new List + { + report.AccountNumber.ToString(), + "", + "", + "", + "", + }, Style = "Normal", ParagraphAlignment = PdfParagraphAlignmentType.Left }); + foreach(var senderTransfer in report.SenderTransfers) + { + CreateRow(new PdfRowParameters + { + Texts = new List + { + "", + senderTransfer.Id.ToString(), + senderTransfer.TransferTime.ToString(), + senderTransfer.Sum.ToString(), + senderTransfer.RecipientAccountNumber.ToString(), + }, + Style = "Normal", + ParagraphAlignment = PdfParagraphAlignmentType.Left + }); + } } CreateParagraph(new PdfParagraph { Text = "", Style = "Normal", - ParagraphAlignment = - PdfParagraphAlignmentType.Right + ParagraphAlignment = PdfParagraphAlignmentType.Right }); - SavePdf(info); + //recipient transfers + CreateParagraph(new PdfParagraph + { + Text = "Переводы с других счетов", + Style = "NormalTitle", + ParagraphAlignment = PdfParagraphAlignmentType.Left + }); + CreateTable(new List { "2cm", "3cm", "6cm", "3cm", "3cm" }); + CreateRow(new PdfRowParameters + { + Texts = new List { "Номер счёта", "Номер перевода", "Время перевода", "Сумма", "Номер счёта отправителя" }, + Style = "NormalTitle", + ParagraphAlignment = PdfParagraphAlignmentType.Center + }); + foreach (var report in info.TransfersWithdrawals) + { + CreateRow(new PdfRowParameters + { + Texts = new List + { + report.AccountNumber.ToString(), + "", + "", + "", + "", + }, + Style = "Normal", + ParagraphAlignment = PdfParagraphAlignmentType.Left + }); + foreach (var recipientTransfer in report.RecipientTransfers) + { + CreateRow(new PdfRowParameters + { + Texts = new List + { + "", + recipientTransfer.Id.ToString(), + recipientTransfer.TransferTime.ToString(), + recipientTransfer.Sum.ToString(), + recipientTransfer.SenderAccountId.ToString(), + }, + Style = "Normal", + ParagraphAlignment = PdfParagraphAlignmentType.Left + }); + } + } + CreateParagraph(new PdfParagraph + { + Text = "", + Style = "Normal", + ParagraphAlignment = PdfParagraphAlignmentType.Right + }); + //withdrawals + CreateParagraph(new PdfParagraph + { + Text = "Выдачи наличных", + Style = "NormalTitle", + ParagraphAlignment = PdfParagraphAlignmentType.Left + }); + CreateTable(new List { "2cm", "3cm", "6cm", "3cm", "3cm" }); + CreateRow(new PdfRowParameters + { + Texts = new List { "Номер счёта", "Номер выдачи", "Время выдачи", "Сумма" }, + Style = "NormalTitle", + ParagraphAlignment = PdfParagraphAlignmentType.Center + }); + foreach (var report in info.TransfersWithdrawals) + { + CreateRow(new PdfRowParameters + { + Texts = new List + { + report.AccountNumber.ToString(), + "", + "", + "", + }, + Style = "Normal", + ParagraphAlignment = PdfParagraphAlignmentType.Left + }); + foreach (var withdrawal in report.Withdrawals) + { + CreateRow(new PdfRowParameters + { + Texts = new List + { + "", + withdrawal.Id.ToString(), + withdrawal.WithdrawalTime.ToString(), + withdrawal.Sum.ToString(), + }, + Style = "Normal", + ParagraphAlignment = PdfParagraphAlignmentType.Left + }); + } + } + CreateParagraph(new PdfParagraph + { + Text = "", + Style = "Normal", + ParagraphAlignment = PdfParagraphAlignmentType.Right + }); + // + SavePdf(info); } public void CreateOperationsRequestsDoc(PdfInfo info) { diff --git a/Bank/BankManagersClientApp/Controllers/HomeController.cs b/Bank/BankManagersClientApp/Controllers/HomeController.cs index dbfdee0..a22d14e 100644 --- a/Bank/BankManagersClientApp/Controllers/HomeController.cs +++ b/Bank/BankManagersClientApp/Controllers/HomeController.cs @@ -522,12 +522,12 @@ namespace BankManagersClientApp.Controllers } APIClient.PostRequest("api/report/sendtransferswithdrawalstoemail", new ReportBindingModel { - FileName = "C:\\Users\\pdffile.pdf", + FileName = "C:\\forpdf\\pdffile.pdf", DateFrom = dateFrom, DateTo = dateTo, Email = APIClient.Client.Email, }); - Response.Redirect("TransfersWithdrawalsListReport"); + Response.Redirect("Index"); } #endregion diff --git a/Bank/BankManagersClientApp/Views/Home/TransfersWithdrawalsListReport.cshtml b/Bank/BankManagersClientApp/Views/Home/TransfersWithdrawalsListReport.cshtml index f6300c9..cff1994 100644 --- a/Bank/BankManagersClientApp/Views/Home/TransfersWithdrawalsListReport.cshtml +++ b/Bank/BankManagersClientApp/Views/Home/TransfersWithdrawalsListReport.cshtml @@ -30,7 +30,7 @@ Номер - Дата/th> + Дата Счёт Перевод Выдача From a4942de30edf8d02457726655b5fc84370a44d50 Mon Sep 17 00:00:00 2001 From: Zakharov_Rostislav Date: Mon, 27 May 2024 02:48:42 +0400 Subject: [PATCH 2/3] fix sending pdf report to email --- .../OfficePackage/AbstractSaveToPdf.cs | 2 +- .../Implements/AccountStorage.cs | 33 +++++----- .../TransfersWithdrawalsListReport.cshtml | 66 +++++++++---------- 3 files changed, 51 insertions(+), 50 deletions(-) diff --git a/Bank/BankBusinessLogic/OfficePackage/AbstractSaveToPdf.cs b/Bank/BankBusinessLogic/OfficePackage/AbstractSaveToPdf.cs index 10da23d..be4aee8 100644 --- a/Bank/BankBusinessLogic/OfficePackage/AbstractSaveToPdf.cs +++ b/Bank/BankBusinessLogic/OfficePackage/AbstractSaveToPdf.cs @@ -116,7 +116,7 @@ namespace BankBusinessLogic.OfficePackage recipientTransfer.Id.ToString(), recipientTransfer.TransferTime.ToString(), recipientTransfer.Sum.ToString(), - recipientTransfer.SenderAccountId.ToString(), + recipientTransfer.SenderAccountNumber, }, Style = "Normal", ParagraphAlignment = PdfParagraphAlignmentType.Left diff --git a/Bank/BankDatabaseImplement/Implements/AccountStorage.cs b/Bank/BankDatabaseImplement/Implements/AccountStorage.cs index 1dd6224..3c568db 100644 --- a/Bank/BankDatabaseImplement/Implements/AccountStorage.cs +++ b/Bank/BankDatabaseImplement/Implements/AccountStorage.cs @@ -137,31 +137,34 @@ namespace BankDatabaseImplement.Implements { using var context = new BankDatabase(); return context.Accounts - .Select(a => new ReportTransfersWithdrawalsViewModel() + .Select(account => new ReportTransfersWithdrawalsViewModel() { - AccountNumber = a.Number, + AccountNumber = account.Number, SenderTransfers = context.Transfers + .Include(x => x.RecipientAccount) .Where(t => t.TransferTime <= model.DateTo && t.TransferTime >= model.DateFrom && - t.SenderAccountId == a.Id) + t.SenderAccountId == account.Id) .Select(t => t.GetViewModel) .ToList(), RecipientTransfers = context.Transfers - .Where(t => t.TransferTime <= model.DateTo && + .Include(x => x.SenderAccount) + .Where(t => t.TransferTime <= model.DateTo && t.TransferTime >= model.DateFrom && - t.RecipientAccountId == a.Id) + t.RecipientAccountId == account.Id) .Select(t => t.GetViewModel) .ToList(), - Withdrawals = context.Withdrawals - .Include(w => w.Accounts) - .Where(w => w.WithdrawalTime <= model.DateTo && - w.WithdrawalTime >= model.DateFrom && w.Accounts - .Select(x => x.AccountId) - .ToList() - .Contains(a.Id)) - .Select(w => w.GetViewModel) - .ToList() - }) + Withdrawals = context.AccountWithdrawals + .Include(x => x.Withdrawal) + .ThenInclude(x => x.Accounts) + .Where(x => x.AccountId == account.Id) + .Select(x => x.Withdrawal) + .Where(w => + w.WithdrawalTime <= model.DateTo && + w.WithdrawalTime >= model.DateFrom) + .Select(w => w.GetViewModel) + .ToList(), + }) .ToList(); } } diff --git a/Bank/BankManagersClientApp/Views/Home/TransfersWithdrawalsListReport.cshtml b/Bank/BankManagersClientApp/Views/Home/TransfersWithdrawalsListReport.cshtml index cff1994..8d44ff4 100644 --- a/Bank/BankManagersClientApp/Views/Home/TransfersWithdrawalsListReport.cshtml +++ b/Bank/BankManagersClientApp/Views/Home/TransfersWithdrawalsListReport.cshtml @@ -4,39 +4,37 @@

Список счетов с расшифровкой по переводам и выдачам по периоду

-
- @{ -
-
Начальная дата:
-
- -
+
+
+
Начальная дата:
+
+
-
-
Конечная дата:
-
- -
+
+
+
Конечная дата:
+
+
-
-
-
-
-
-
-
-
- - - - - - - - - - - -
НомерДатаСчётПереводВыдача
- } -
\ No newline at end of file +
+
+
+
+
+
+
+
+
+ + + + + + + + + + + +
НомерДатаСчётПереводВыдача
+
\ No newline at end of file From ed31093f33833e13a55f2d65e54da520b5556272 Mon Sep 17 00:00:00 2001 From: Zakharov_Rostislav Date: Mon, 27 May 2024 03:40:24 +0400 Subject: [PATCH 3/3] add demonstrating pdf report in view --- .../Controllers/HomeController.cs | 150 +++++++++++++++++- .../TransfersWithdrawalsListReport.cshtml | 42 +++-- .../Controllers/ReportController.cs | 21 +++ 3 files changed, 194 insertions(+), 19 deletions(-) diff --git a/Bank/BankManagersClientApp/Controllers/HomeController.cs b/Bank/BankManagersClientApp/Controllers/HomeController.cs index a22d14e..812afc2 100644 --- a/Bank/BankManagersClientApp/Controllers/HomeController.cs +++ b/Bank/BankManagersClientApp/Controllers/HomeController.cs @@ -6,6 +6,7 @@ using BankDataModels.Models; using BankManagersClientApp.Models; using Microsoft.AspNetCore.Mvc; using System.Diagnostics; +using System.Globalization; namespace BankManagersClientApp.Controllers { @@ -528,11 +529,152 @@ namespace BankManagersClientApp.Controllers Email = APIClient.Client.Email, }); Response.Redirect("Index"); - } - #endregion + } - #region//Error - [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)] + [HttpGet] + public string GetTransfersWithdrawals(DateTime dateFrom, DateTime dateTo) + { + if (APIClient.Client == null) + { + throw new Exception("Вы как суда попали? Суда вход только авторизованным"); + } + List result; + try + { + string dateFromS = dateFrom.ToString("s", CultureInfo.InvariantCulture); + string dateToS = dateTo.ToString("s", CultureInfo.InvariantCulture); + result = APIClient.GetRequest> + ($"api/report/gettransferswithdrawals?datefrom={dateFromS}&dateto={dateToS}")!; + } + catch + { + _logger.LogError("Ошибка создания отчета"); + throw; + } + //sender transfers + string senderTransfersTable = ""; + senderTransfersTable += "

Переводы на другие счета

"; + senderTransfersTable += "
"; + senderTransfersTable += ""; + senderTransfersTable += ""; + senderTransfersTable += ""; + senderTransfersTable += ""; + senderTransfersTable += ""; + senderTransfersTable += ""; + senderTransfersTable += ""; + senderTransfersTable += ""; + senderTransfersTable += ""; + senderTransfersTable += ""; + senderTransfersTable += ""; + foreach (var report in result) + { + senderTransfersTable += ""; + senderTransfersTable += $""; + senderTransfersTable += $""; + senderTransfersTable += $""; + senderTransfersTable += $""; + senderTransfersTable += $""; + senderTransfersTable += ""; + foreach (var senderTransfer in report.SenderTransfers) + { + senderTransfersTable += ""; + senderTransfersTable += $""; + senderTransfersTable += $""; + senderTransfersTable += $""; + senderTransfersTable += $""; + senderTransfersTable += $""; + senderTransfersTable += ""; + } + } + senderTransfersTable += ""; + senderTransfersTable += "
Номер счётаНомер переводаВремя переводаСуммаНомер счёта получателя
{report.AccountNumber}
{senderTransfer.Id}{senderTransfer.TransferTime}{senderTransfer.Sum}{senderTransfer.RecipientAccountNumber}
"; + senderTransfersTable += "
"; + //recipient transfers + string recipientTransfersTable = ""; + recipientTransfersTable += "

Переводы с других счетов

"; + recipientTransfersTable += "
"; + recipientTransfersTable += ""; + recipientTransfersTable += ""; + recipientTransfersTable += ""; + recipientTransfersTable += ""; + recipientTransfersTable += ""; + recipientTransfersTable += ""; + recipientTransfersTable += ""; + recipientTransfersTable += ""; + recipientTransfersTable += ""; + recipientTransfersTable += ""; + recipientTransfersTable += ""; + foreach (var report in result) + { + recipientTransfersTable += ""; + recipientTransfersTable += $""; + recipientTransfersTable += $""; + recipientTransfersTable += $""; + recipientTransfersTable += $""; + recipientTransfersTable += $""; + recipientTransfersTable += ""; + foreach (var recipientTransfer in report.RecipientTransfers) + { + recipientTransfersTable += ""; + recipientTransfersTable += $""; + recipientTransfersTable += $""; + recipientTransfersTable += $""; + recipientTransfersTable += $""; + recipientTransfersTable += $""; + recipientTransfersTable += ""; + } + } + recipientTransfersTable += ""; + recipientTransfersTable += "
Номер счётаНомер переводаВремя переводаСуммаНомер счёта отправителя
{report.AccountNumber}
{recipientTransfer.Id}{recipientTransfer.TransferTime}{recipientTransfer.Sum}{recipientTransfer.SenderAccountNumber}
"; + recipientTransfersTable += "
"; + //withdrawals + string withdrawalsTable = ""; + withdrawalsTable += "

Выдачи наличных

"; + withdrawalsTable += "
"; + withdrawalsTable += ""; + withdrawalsTable += ""; + withdrawalsTable += ""; + withdrawalsTable += ""; + withdrawalsTable += ""; + withdrawalsTable += ""; + withdrawalsTable += ""; + withdrawalsTable += ""; + withdrawalsTable += ""; + withdrawalsTable += ""; + foreach (var report in result) + { + withdrawalsTable += ""; + withdrawalsTable += $""; + withdrawalsTable += $""; + withdrawalsTable += $""; + withdrawalsTable += $""; + withdrawalsTable += ""; + foreach (var withdrawal in report.Withdrawals) + { + withdrawalsTable += ""; + withdrawalsTable += $""; + withdrawalsTable += $""; + withdrawalsTable += $""; + withdrawalsTable += $""; + withdrawalsTable += ""; + } + } + withdrawalsTable += ""; + withdrawalsTable += "
Номер счётаНомер выдачиВремя выдачиСумма
{report.AccountNumber}
{withdrawal.Id}{withdrawal.WithdrawalTime}{withdrawal.Sum}
"; + withdrawalsTable += "
"; + //title + string tables = ""; + tables += "

Отчет по счетам

"; + tables += $"
с {dateFrom.ToShortDateString()} по {dateTo.ToShortDateString()}
"; + tables += senderTransfersTable; + tables += recipientTransfersTable; + tables += withdrawalsTable; + return tables; + } + #endregion + + #region//Error + [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)] public IActionResult Error() { return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier }); diff --git a/Bank/BankManagersClientApp/Views/Home/TransfersWithdrawalsListReport.cshtml b/Bank/BankManagersClientApp/Views/Home/TransfersWithdrawalsListReport.cshtml index 8d44ff4..6008576 100644 --- a/Bank/BankManagersClientApp/Views/Home/TransfersWithdrawalsListReport.cshtml +++ b/Bank/BankManagersClientApp/Views/Home/TransfersWithdrawalsListReport.cshtml @@ -17,24 +17,36 @@
-
+
-
+
- - - - - - - - - - - -
НомерДатаСчётПереводВыдача
- \ No newline at end of file +
+ + +@section Scripts { + +} \ No newline at end of file diff --git a/Bank/BankRestApi/Controllers/ReportController.cs b/Bank/BankRestApi/Controllers/ReportController.cs index 1a09273..9cc6209 100644 --- a/Bank/BankRestApi/Controllers/ReportController.cs +++ b/Bank/BankRestApi/Controllers/ReportController.cs @@ -1,6 +1,8 @@ using BankBusinessLogic.MailWorker; using BankContracts.BindingModels; using BankContracts.BusinessLogicsContracts; +using BankContracts.SearchModels; +using BankContracts.ViewModels; using Microsoft.AspNetCore.Mvc; namespace BankRestApi.Controllers @@ -111,5 +113,24 @@ namespace BankRestApi.Controllers throw; } } + [HttpGet] + public List GetTransfersWithdrawals(string dateFrom, string dateTo) + { + try + { + DateTime DateFrom = DateTime.Parse(dateFrom); + DateTime DateTo = DateTime.Parse(dateTo); + return _logic.CreateReportTransfersWithdrawals(new AccountSearchModel + { + DateFrom = DateFrom, + DateTo = DateTo, + }); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка сохранения счетов в pdf"); + throw; + } + } } }