Наконец-то работает

This commit is contained in:
DyCTaTOR 2024-04-10 01:01:51 +04:00
parent 579ba5fbd8
commit a5ae5119e5
11 changed files with 76 additions and 46 deletions

View File

@ -11,6 +11,7 @@ using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Windows.Forms; using System.Windows.Forms;
using Microsoft.Reporting.WinForms; using Microsoft.Reporting.WinForms;
using System.Windows.Forms.Design;
namespace PlumbingRepairView namespace PlumbingRepairView
{ {
@ -48,8 +49,8 @@ namespace PlumbingRepairView
{ {
var dataSource = _logic.GetOrders(new ReportBindingModel var dataSource = _logic.GetOrders(new ReportBindingModel
{ {
DateFrom = dateTimePickerFrom.Value, DateFrom = DateTime.SpecifyKind(dateTimePickerFrom.Value, DateTimeKind.Utc),
DateTo = dateTimePickerTo.Value DateTo = DateTime.SpecifyKind(dateTimePickerTo.Value, DateTimeKind.Utc)
}); });
var source = new ReportDataSource("DataSetOrders", dataSource); var source = new ReportDataSource("DataSetOrders", dataSource);
reportViewer.LocalReport.DataSources.Clear(); reportViewer.LocalReport.DataSources.Clear();
@ -80,11 +81,12 @@ namespace PlumbingRepairView
{ {
try try
{ {
System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance);
_logic.SaveOrdersToPdfFile(new ReportBindingModel _logic.SaveOrdersToPdfFile(new ReportBindingModel
{ {
FileName = dialog.FileName, FileName = dialog.FileName,
DateFrom = dateTimePickerFrom.Value, DateFrom = DateTime.SpecifyKind(dateTimePickerFrom.Value, DateTimeKind.Utc),
DateTo = dateTimePickerTo.Value DateTo = DateTime.SpecifyKind(dateTimePickerTo.Value, DateTimeKind.Utc)
}); });
_logger.LogInformation("Saving list of orders for period {From}-{To}", dateTimePickerFrom.Value.ToShortDateString(), dateTimePickerTo.Value.ToShortDateString()); _logger.LogInformation("Saving list of orders for period {From}-{To}", dateTimePickerFrom.Value.ToShortDateString(), dateTimePickerTo.Value.ToShortDateString());
MessageBox.Show("Выполнено", "Успех", MessageBoxButtons.OK, MessageBoxIcon.Information); MessageBox.Show("Выполнено", "Успех", MessageBoxButtons.OK, MessageBoxIcon.Information);

View File

@ -20,6 +20,7 @@
<PackageReference Include="Microsoft.Extensions.Configuration" Version="7.0.0" /> <PackageReference Include="Microsoft.Extensions.Configuration" Version="7.0.0" />
<PackageReference Include="NLog.Extensions.Logging" Version="5.3.8" /> <PackageReference Include="NLog.Extensions.Logging" Version="5.3.8" />
<PackageReference Include="ReportViewerCore.WinForms" Version="15.1.19" /> <PackageReference Include="ReportViewerCore.WinForms" Version="15.1.19" />
<PackageReference Include="System.Text.Encoding" Version="4.3.0" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>

View File

@ -94,8 +94,8 @@ namespace PlumbingRepairBusinessLogic.BusinessLogics
{ {
FileName = model.FileName, FileName = model.FileName,
Title = "Список заказов", Title = "Список заказов",
DateFrom = model.DateFrom!.Value, DateFrom = DateTime.SpecifyKind(model.DateFrom!.Value, DateTimeKind.Utc),
DateTo = model.DateTo!.Value, DateTo = DateTime.SpecifyKind(model.DateTo!.Value, DateTimeKind.Utc),
Orders = GetOrders(model) Orders = GetOrders(model)
}); });
} }

View File

@ -1,10 +1,5 @@
using PlumbingRepairBusinessLogic.OfficePackage.HelperEnum; using PlumbingRepairBusinessLogic.OfficePackage.HelperEnum;
using PlumbingRepairBusinessLogic.OfficePackage.HelperModels; using PlumbingRepairBusinessLogic.OfficePackage.HelperModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace PlumbingRepairBusinessLogic.OfficePackage namespace PlumbingRepairBusinessLogic.OfficePackage
{ {
@ -13,48 +8,62 @@ namespace PlumbingRepairBusinessLogic.OfficePackage
public void CreateDoc(PdfInfo info) public void CreateDoc(PdfInfo info)
{ {
CreatePdf(info); CreatePdf(info);
CreateParagraph(new PdfParagraph 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 });
Text = info.Title,
Style = CreateTable(new List<string> { "2cm", "3cm", "6cm", "3cm", "4 cm" });
"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" });
CreateRow(new PdfRowParameters CreateRow(new PdfRowParameters
{ {
Texts = new List<string> { "Номер", "Дата заказа", "Изделие", "Сумма" }, Texts = new List<string> { "Номер", "Дата заказа", "Работа", "Статус", "Сумма" },
Style = "NormalTitle", Style = "NormalTitle",
ParagraphAlignment = PdfParagraphAlignmentType.Center ParagraphAlignment = PdfParagraphAlignmentType.Center
}); });
foreach (var order in info.Orders) foreach (var order in info.Orders)
{ {
CreateRow(new PdfRowParameters CreateRow(new PdfRowParameters
{ {
Texts = new List<string> { order.Id.ToString(), Texts = new List<string> { order.Id.ToString(), order.DateCreate.ToShortDateString(), order.WorkName, order.OrderStatus.ToString(), order.Sum.ToString() },
order.DateCreate.ToShortDateString(), order.WorkName, order.Sum.ToString() },
Style = "Normal", Style = "Normal",
ParagraphAlignment = PdfParagraphAlignmentType.Left ParagraphAlignment = PdfParagraphAlignmentType.Left
}); });
} }
CreateParagraph(new PdfParagraph CreateParagraph(new PdfParagraph { Text = $"Итого: {info.Orders.Sum(x => x.Sum)}\t", Style = "Normal", ParagraphAlignment = PdfParagraphAlignmentType.Right });
{
Text = $"Итого: {info.Orders.Sum(x => x.Sum)}\t",
Style = "Normal",
ParagraphAlignment =
PdfParagraphAlignmentType.Rigth
});
SavePdf(info); SavePdf(info);
} }
/// <summary>
/// Создание doc-файла
/// </summary>
/// <param name="info"></param>
protected abstract void CreatePdf(PdfInfo info); protected abstract void CreatePdf(PdfInfo info);
/// <summary>
/// Создание параграфа с текстом
/// </summary>
/// <param name="title"></param>
/// <param name="style"></param>
protected abstract void CreateParagraph(PdfParagraph paragraph); protected abstract void CreateParagraph(PdfParagraph paragraph);
/// <summary>
/// Создание таблицы
/// </summary>
/// <param name="title"></param>
/// <param name="style"></param>
protected abstract void CreateTable(List<string> columns); protected abstract void CreateTable(List<string> columns);
/// <summary>
/// Создание и заполнение строки
/// </summary>
/// <param name="rowParameters"></param>
protected abstract void CreateRow(PdfRowParameters rowParameters); protected abstract void CreateRow(PdfRowParameters rowParameters);
/// <summary>
/// Сохранение файла
/// </summary>
/// <param name="info"></param>
protected abstract void SavePdf(PdfInfo info); protected abstract void SavePdf(PdfInfo info);
} }
} }

View File

@ -12,6 +12,6 @@ namespace PlumbingRepairBusinessLogic.OfficePackage.HelperEnum
Left, Left,
Rigth Right
} }
} }

View File

@ -9,32 +9,44 @@ namespace PlumbingRepairBusinessLogic.OfficePackage.Implements
public class SaveToPdf : AbstractSaveToPdf public class SaveToPdf : AbstractSaveToPdf
{ {
private Document? _document; private Document? _document;
private Section? _section; private Section? _section;
private Table? _table; private Table? _table;
private static ParagraphAlignment GetParagraphAlignment(PdfParagraphAlignmentType type) private static ParagraphAlignment GetParagraphAlignment(PdfParagraphAlignmentType type)
{ {
return type switch return type switch
{ {
PdfParagraphAlignmentType.Center => ParagraphAlignment.Center, PdfParagraphAlignmentType.Center => ParagraphAlignment.Center,
PdfParagraphAlignmentType.Left => ParagraphAlignment.Left, PdfParagraphAlignmentType.Left => ParagraphAlignment.Left,
PdfParagraphAlignmentType.Rigth => ParagraphAlignment.Right, PdfParagraphAlignmentType.Right => ParagraphAlignment.Right,
_ => ParagraphAlignment.Justify, _ => ParagraphAlignment.Justify,
}; };
} }
/// <summary>
/// Создание стилей для документа
/// </summary>
/// <param name="document"></param>
private static void DefineStyles(Document document) private static void DefineStyles(Document document)
{ {
var style = document.Styles["Normal"]; var style = document.Styles["Normal"];
style.Font.Name = "Times New Roman"; style.Font.Name = "Times New Roman";
style.Font.Size = 14; style.Font.Size = 14;
style = document.Styles.AddStyle("NormalTitle", "Normal"); style = document.Styles.AddStyle("NormalTitle", "Normal");
style.Font.Bold = true; style.Font.Bold = true;
} }
protected override void CreatePdf(PdfInfo info) protected override void CreatePdf(PdfInfo info)
{ {
_document = new Document(); _document = new Document();
DefineStyles(_document); DefineStyles(_document);
_section = _document.AddSection(); _section = _document.AddSection();
} }
protected override void CreateParagraph(PdfParagraph pdfParagraph) protected override void CreateParagraph(PdfParagraph pdfParagraph)
{ {
if (_section == null) if (_section == null)
@ -43,10 +55,10 @@ namespace PlumbingRepairBusinessLogic.OfficePackage.Implements
} }
var paragraph = _section.AddParagraph(pdfParagraph.Text); var paragraph = _section.AddParagraph(pdfParagraph.Text);
paragraph.Format.SpaceAfter = "1cm"; paragraph.Format.SpaceAfter = "1cm";
paragraph.Format.Alignment = paragraph.Format.Alignment = GetParagraphAlignment(pdfParagraph.ParagraphAlignment);
GetParagraphAlignment(pdfParagraph.ParagraphAlignment);
paragraph.Style = pdfParagraph.Style; paragraph.Style = pdfParagraph.Style;
} }
protected override void CreateTable(List<string> columns) protected override void CreateTable(List<string> columns)
{ {
if (_document == null) if (_document == null)
@ -54,11 +66,13 @@ namespace PlumbingRepairBusinessLogic.OfficePackage.Implements
return; return;
} }
_table = _document.LastSection.AddTable(); _table = _document.LastSection.AddTable();
foreach (var elem in columns) foreach (var elem in columns)
{ {
_table.AddColumn(elem); _table.AddColumn(elem);
} }
} }
protected override void CreateRow(PdfRowParameters rowParameters) protected override void CreateRow(PdfRowParameters rowParameters)
{ {
if (_table == null) if (_table == null)
@ -69,20 +83,24 @@ namespace PlumbingRepairBusinessLogic.OfficePackage.Implements
for (int i = 0; i < rowParameters.Texts.Count; ++i) for (int i = 0; i < rowParameters.Texts.Count; ++i)
{ {
row.Cells[i].AddParagraph(rowParameters.Texts[i]); row.Cells[i].AddParagraph(rowParameters.Texts[i]);
if (!string.IsNullOrEmpty(rowParameters.Style)) if (!string.IsNullOrEmpty(rowParameters.Style))
{ {
row.Cells[i].Style = rowParameters.Style; row.Cells[i].Style = rowParameters.Style;
} }
Unit borderWidth = 0.5; Unit borderWidth = 0.5;
row.Cells[i].Borders.Left.Width = borderWidth; row.Cells[i].Borders.Left.Width = borderWidth;
row.Cells[i].Borders.Right.Width = borderWidth; row.Cells[i].Borders.Right.Width = borderWidth;
row.Cells[i].Borders.Top.Width = borderWidth; row.Cells[i].Borders.Top.Width = borderWidth;
row.Cells[i].Borders.Bottom.Width = borderWidth; row.Cells[i].Borders.Bottom.Width = borderWidth;
row.Cells[i].Format.Alignment =
GetParagraphAlignment(rowParameters.ParagraphAlignment); row.Cells[i].Format.Alignment = GetParagraphAlignment(rowParameters.ParagraphAlignment);
row.Cells[i].VerticalAlignment = VerticalAlignment.Center; row.Cells[i].VerticalAlignment = VerticalAlignment.Center;
} }
} }
protected override void SavePdf(PdfInfo info) protected override void SavePdf(PdfInfo info)
{ {
var renderer = new PdfDocumentRenderer(true) var renderer = new PdfDocumentRenderer(true)
@ -93,4 +111,4 @@ namespace PlumbingRepairBusinessLogic.OfficePackage.Implements
renderer.PdfDocument.Save(info.FileName); renderer.PdfDocument.Save(info.FileName);
} }
} }
} }

View File

@ -9,7 +9,7 @@
<ItemGroup> <ItemGroup>
<PackageReference Include="DocumentFormat.OpenXml" Version="3.0.2" /> <PackageReference Include="DocumentFormat.OpenXml" Version="3.0.2" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="8.0.0" /> <PackageReference Include="Microsoft.Extensions.Logging" Version="8.0.0" />
<PackageReference Include="PDFsharp-MigraDoc" Version="6.0.0" /> <PackageReference Include="PdfSharp.MigraDoc.Standard" Version="1.51.15" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>

View File

@ -9,7 +9,7 @@ namespace PlumbingRepairContracts.BindingModels
public class ReportBindingModel public class ReportBindingModel
{ {
public string FileName { get; set; } = string.Empty; public string FileName { get; set; } = string.Empty;
public DateTime? DateFrom { get; set; } = DateTime.SpecifyKind(, DateTimeKind.Utc); public DateTime? DateFrom { get; set; }
public DateTime? DateTo { get; set; } public DateTime? DateTo { get; set; }
} }

View File

@ -29,7 +29,7 @@ namespace PlumbingRepairDataBaseImplement.Models
public OrderStatus Status { get; private set; } = OrderStatus.Неизвестен; public OrderStatus Status { get; private set; } = OrderStatus.Неизвестен;
[Required] [Required]
public DateTime DateCreate { get; private set; } = DateTime.SpecifyKind(DateTime.Now, DateTimeKind.Utc); public DateTime DateCreate { get; private set; }
public DateTime? DateImplement { get; private set; } public DateTime? DateImplement { get; private set; }

View File

@ -19,7 +19,7 @@ namespace PlumbingRepairFileImplement.Models
public int Count { get; private set; } public int Count { get; private set; }
public double Sum { get; private set; } public double Sum { get; private set; }
public OrderStatus Status { get; private set; } = OrderStatus.Неизвестен; public OrderStatus Status { get; private set; } = OrderStatus.Неизвестен;
public DateTime DateCreate { get; private set; } = DateTime.SpecifyKind(DateTime.Now, DateTimeKind.Utc); public DateTime DateCreate { get; private set; }
public DateTime? DateImplement { get; private set; } public DateTime? DateImplement { get; private set; }
public static Order? Create(OrderBindingModel? model) public static Order? Create(OrderBindingModel? model)

View File

@ -18,7 +18,7 @@ namespace PlumbingRepairListImplement.Models
public int Count { get; private set; } public int Count { get; private set; }
public double Sum { get; private set; } public double Sum { get; private set; }
public OrderStatus Status { get; private set; } public OrderStatus Status { get; private set; }
public DateTime DateCreate { get; private set; } = DateTime.SpecifyKind(DateTime.Now, DateTimeKind.Utc); public DateTime DateCreate { get; private set; }
public DateTime? DateImplement { get; private set; } public DateTime? DateImplement { get; private set; }
public static Order? Create(OrderBindingModel? model) public static Order? Create(OrderBindingModel? model)
{ {