demo_trade/Models/Ticket.cs
2024-10-04 15:41:04 +03:00

55 lines
2.3 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using demo_trade.UI.Presenters;
using iText.IO.Font.Constants;
using iText.Kernel.Font;
using iTextSharp.text;
using iTextSharp.text.pdf;
using Microsoft.EntityFrameworkCore.Metadata.Internal;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using PdfFont = iText.Kernel.Font.PdfFont;
namespace demo_trade.Models
{
public class Ticket
{
public int OrderID { get; set; }
public string PickupPointName{ get; set; }
public decimal OrderSumCost{ get; set; }
public string? ClientName { get; set; } = null;
public DateOnly OrderDate { get; set; }
public DateOnly OrderDelivery { get; set; }
public List<OrderProductPresenter> Products { get; set; }
public int OrderCode { get; set; }
public void GeneratePDF(string path) {
using var document = new Document();
PdfWriter.GetInstance(document, new FileStream(path, FileMode.OpenOrCreate));
document.Open();
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
BaseFont baseFont = BaseFont.CreateFont("C:\\Windows\\Fonts\\arial.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
Font font = new Font(baseFont, iTextSharp.text.Font.DEFAULTSIZE, iTextSharp.text.Font.NORMAL);
Font fontCode = new Font(baseFont, iTextSharp.text.Font.DEFAULTSIZE, iTextSharp.text.Font.BOLD);
Paragraph paragraph = new Paragraph(String.Format("Order №{0} Create {1} Data delivery {2}\nПункт выдачи: {3}\nСумма заказа: {4}", OrderID, OrderDate, OrderDelivery, PickupPointName, OrderSumCost), font);
if (ClientName != null) paragraph.Add(String.Format("\nClient name: {0}", ClientName));
document.Add(paragraph);
foreach (OrderProductPresenter presenter in Products) {
document.Add(new Phrase(String.Format("Article: {0}, Name: {2}, Count: {1}\n", presenter.ArticleNumber, presenter.ProductCount, presenter.Name), font));
}
GenerateCode();
document.Add(new Paragraph(String.Format("Code: {0}", OrderCode), fontCode));
document.Close();
}
private void GenerateCode() {
OrderCode = new Random().Next(100, 999);
}
}
}