This commit is contained in:
the 2023-05-24 20:22:06 +04:00
parent 458c19d192
commit 343788051b
8 changed files with 211 additions and 17 deletions

View File

@ -55,7 +55,7 @@ namespace ComputerShopBusinessLogic.BusinessLogics
var receivings = new List<EquipmentReceivingViewModel>();
foreach(var supply in supplies)
foreach (var supply in supplies)
{
if (supply.ReceivingId.HasValue)
{
@ -73,7 +73,7 @@ namespace ComputerShopBusinessLogic.BusinessLogics
{
receivingnames.Add($"Id: {receiving.Id}. Status: {Enum.GetName(typeof(EquipmentReceivingStatus), receiving.Status)}.");
}
if(receivingnames.Count > 0 && component != null)
if (receivingnames.Count > 0 && component != null)
{
result.Add(new()
{
@ -82,7 +82,7 @@ namespace ComputerShopBusinessLogic.BusinessLogics
});
}
}
return result;
}
@ -90,12 +90,12 @@ namespace ComputerShopBusinessLogic.BusinessLogics
public void SaveReceivingComponentsToWordFile(ReportBindingModel model)
{
if (model.Ids != null)
_saveToWord.CreateDoc(new WordInfoProvider
{
FileName = model.FileName,
Title = "Список получений по компонентам",
ComponentReceivings = GetComponentReceivings(model.Ids)
});
_saveToWord.CreateDoc(new WordInfoProvider
{
FileName = model.FileName,
Title = "Список получений по компонентам",
ComponentReceivings = GetComponentReceivings(model.Ids)
});
}
public void SaveReceivingComponentsToXmlFile(ReportBindingModel model)
@ -109,6 +109,20 @@ namespace ComputerShopBusinessLogic.BusinessLogics
});
}
public void SavePurchaseSuppliesToPdfFile(ReportBindingModel model)
{
if (model.DateFrom == null)
{
throw new ArgumentException("Дата начала не задана");
}
if (model.DateTo == null)
{
throw new ArgumentException("Дата окончания не задана");
}
}
public List<ReportPurchaseReceivingViewModel> GetPurchaseReceiving()
{
throw new NotImplementedException();

View File

@ -16,6 +16,7 @@
</PackageReference>
<PackageReference Include="Microsoft.Extensions.Logging" Version="7.0.0" />
<PackageReference Include="NLog.Extensions.Logging" Version="5.2.3" />
<PackageReference Include="PdfSharp.MigraDoc.Standard" Version="1.51.15" />
</ItemGroup>
<ItemGroup>

View File

@ -1,4 +1,6 @@
using System;
using ComputerShopBusinessLogic.OfficePackage.HelperEnums;
using ComputerShopBusinessLogic.OfficePackage.HelperModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
@ -6,7 +8,45 @@ using System.Threading.Tasks;
namespace ComputerShopBusinessLogic.OfficePackage
{
public class AbstractSaveToPdf
public abstract class AbstractSaveToPdf
{
public void CreateDoc(PdfInfoProvider 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> { "4cm", "5cm", "3cm", "4cm", "2cm" });
CreateRow(new PdfRowParameters
{
Texts = new List<string> { "ID закупки", "Название компонента", "Дата создания закупки", "Дата создания поставки", "Статус поставки" },
Style = "NormalTitle",
ParagraphAlignment = PdfParagraphAlignmentType.Center
});
foreach (var supplyPurchase in info.SupplyPurchases)
{
CreateRow(new PdfRowParameters
{
Texts = new List<string> { supplyPurchase.Purchase.Id.ToString(), supplyPurchase.Purchase.ComponentName, supplyPurchase.Purchase.DateCreate.ToString(), supplyPurchase.Supply.DateCreate.ToString(), supplyPurchase.Supply.Status.ToString()},
Style = "Normal",
ParagraphAlignment = PdfParagraphAlignmentType.Left
});
}
SavePdf(info);
}
protected abstract void CreatePdf(PdfInfoProvider info);
protected abstract void CreateParagraph(PdfParagraph paragraph);
protected abstract void CreateTable(List<string> columns);
protected abstract void CreateRow(PdfRowParameters rowParameters);
protected abstract void SavePdf(PdfInfoProvider info);
}
}

View File

@ -0,0 +1,18 @@
using ComputerShopContracts.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ComputerShopBusinessLogic.OfficePackage.HelperModels
{
public class PdfInfoProvider
{
public string FileName { get; set; } = "F:\\ReportsCourseWork\\pdffile.pdf";
public string Title { get; set; } = string.Empty;
public DateTime DateFrom { get; set; }
public DateTime DateTo { get; set; }
public List<ReportPurchaseSupplyViewModel> SupplyPurchases { get; set; } = new();
}
}

View File

@ -0,0 +1,16 @@
using ComputerShopBusinessLogic.OfficePackage.HelperEnums;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ComputerShopBusinessLogic.OfficePackage.HelperModels
{
public class PdfParagraph
{
public string Text { get; set; } = string.Empty;
public string Style { get; set; } = string.Empty;
public PdfParagraphAlignmentType ParagraphAlignment { get; set; }
}
}

View File

@ -0,0 +1,16 @@
using ComputerShopBusinessLogic.OfficePackage.HelperEnums;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ComputerShopBusinessLogic.OfficePackage.HelperModels
{
public class PdfRowParameters
{
public List<string> Texts { get; set; } = new();
public string Style { get; set; } = string.Empty;
public PdfParagraphAlignmentType ParagraphAlignment { get; set; }
}
}

View File

@ -1,4 +1,9 @@
using System;
using ComputerShopBusinessLogic.OfficePackage.HelperEnums;
using ComputerShopBusinessLogic.OfficePackage.HelperModels;
using MigraDoc.DocumentObjectModel;
using MigraDoc.DocumentObjectModel.Tables;
using MigraDoc.Rendering;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
@ -8,5 +13,92 @@ namespace ComputerShopBusinessLogic.OfficePackage.Implements
{
public class SaveToPdf : AbstractSaveToPdf
{
private Document? _document;
private Section? _section;
private Table? _table;
private static ParagraphAlignment GetParagraphAlignment(PdfParagraphAlignmentType type)
{
return type switch
{
PdfParagraphAlignmentType.Center => ParagraphAlignment.Center,
PdfParagraphAlignmentType.Left => ParagraphAlignment.Left,
PdfParagraphAlignmentType.Right => ParagraphAlignment.Right,
_ => ParagraphAlignment.Justify,
};
}
private static void DefineStyles(Document document)
{
var style = document.Styles["Normal"];
style.Font.Name = "Times New Roman";
style.Font.Size = 14;
style = document.Styles.AddStyle("NormalTitle", "Normal");
style.Font.Bold = true;
}
protected override void CreateParagraph(PdfParagraph pdfParagraph)
{
if (_section == null)
{
return;
}
var paragraph = _section.AddParagraph(pdfParagraph.Text);
paragraph.Format.SpaceAfter = "1cm";
paragraph.Format.Alignment = GetParagraphAlignment(pdfParagraph.ParagraphAlignment);
paragraph.Style = pdfParagraph.Style;
}
protected override void CreatePdf(PdfInfoProvider info)
{
_document = new Document();
DefineStyles(_document);
_section = _document.AddSection();
}
protected override void CreateRow(PdfRowParameters rowParameters)
{
if (_table == null)
{
return;
}
var row = _table.AddRow();
for (int i = 0; i < rowParameters.Texts.Count; ++i)
{
row.Cells[i].AddParagraph(rowParameters.Texts[i]);
if (!string.IsNullOrEmpty(rowParameters.Style))
{
row.Cells[i].Style = rowParameters.Style;
}
Unit borderWidth = 0.5;
row.Cells[i].Borders.Left.Width = borderWidth;
row.Cells[i].Borders.Right.Width = borderWidth;
row.Cells[i].Borders.Top.Width = borderWidth;
row.Cells[i].Borders.Bottom.Width = borderWidth;
row.Cells[i].Format.Alignment = GetParagraphAlignment(rowParameters.ParagraphAlignment);
row.Cells[i].VerticalAlignment = VerticalAlignment.Center;
}
}
protected override void CreateTable(List<string> columns)
{
if (_document == null)
{
return;
}
_table = _document.LastSection.AddTable();
foreach (var elem in columns)
{
_table.AddColumn(elem);
}
}
protected override void SavePdf(PdfInfoProvider info)
{
var renderer = new PdfDocumentRenderer(true)
{
Document = _document
};
System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance);
renderer.RenderDocument();
renderer.PdfDocument.Save(info.FileName);
}
}
}

View File

@ -8,10 +8,7 @@ namespace ComputerShopContracts.ViewModels
{
public class ReportPurchaseSupplyViewModel
{
public int UserId { get; set; }
public int ComponentId { get; set; }
public string ComponentName { get; } = String.Empty;
public DateTime PurchaseDateCreating { get; set; }
public DateTime SupplyDateCreating { get; set; }
public PurchaseViewModel Purchase { get; set; }
public SupplyViewModel Supply { get; set; }
}
}