using System.Net; using System.Net.Mail; using Microsoft.AspNetCore.Mvc; using Syncfusion.Drawing; using Syncfusion.XlsIO; using SytelineSaAppEfDataModel.Dtos; using SytelineSaAppEfDataModel.Services; namespace FaKrosnoApi.Controllers; [ApiController] [Route("api/[controller]")] public class ExcelGeneratorController(IWzHeaderService wzHeaderService, IWzClientService wzClientService, IMaterialTransactionService materialTransactionService, IConfiguration configuration) : Controller { [HttpGet("generate-meyle")] public async Task GeneratePackListForMeyle(Guid packListId) { WzHeaderDto wzHeader = await wzHeaderService.GetById(packListId); MaterialTransactionDto? materialTransaction = await materialTransactionService.GetByWzNumber(wzHeader.WzRowsMeyle.First().WzNumber); using ExcelEngine excelEngine = new ExcelEngine(); IApplication application = excelEngine.Excel; application.DefaultVersion = ExcelVersion.Xlsx; IWorkbook workbook = application.Workbooks.Create(1); IWorksheet worksheet = workbook.Worksheets[0]; IStyle boldFontStyle = workbook.Styles.Add("BoldFontStyle"); boldFontStyle.Font.Bold = true; var mainHeaders = new List { "Numer zamówienia Meyle", "Meyle Numer", "Ilość w dostawie", "Numer Palety", "Nr Wz", "Nr Partii" }; worksheet["B1"].Value = "Packing List"; worksheet["B1"].CellStyle = boldFontStyle; worksheet["B3"].Value = "Supplier Name"; worksheet["B3"].CellStyle = boldFontStyle; worksheet["D3"].Value = "FA KROSNO"; worksheet["D3"].CellStyle = boldFontStyle; worksheet["B4"].Value = "Packing List nr"; worksheet["B4"].CellStyle = boldFontStyle; worksheet["B6"].Value = "Related delivery note"; worksheet["B6"].CellStyle = boldFontStyle; worksheet["D6"].Value = string.Join(", ", wzHeader.WzRowsMeyle.Select(x => x.WzNumber).Distinct()); worksheet["D6"].CellStyle = boldFontStyle; worksheet["B9"].Value = "Forwarder"; worksheet["B9"].CellStyle = boldFontStyle; worksheet["B10"].Value = "Date"; worksheet["B10"].CellStyle = boldFontStyle; worksheet["D10"].DateTime = materialTransaction?.CreateDate ?? DateTime.Now; int currentRow = 12; for (int i = 0; i < mainHeaders.Count; i++) { string columnLetter = GetColumnLetter(i); worksheet.Range[$"{columnLetter}{currentRow}"].Text = mainHeaders[i]; worksheet.Range[$"{columnLetter}{currentRow}"].CellStyle = boldFontStyle; } currentRow++; foreach (var wzRow in wzHeader.WzRowsMeyle) { worksheet.Range[$"A{currentRow}"].Text = wzRow.OrderNumber; worksheet.Range[$"B{currentRow}"].Text = wzRow.ItemNumber; worksheet.Range[$"C{currentRow}"].Number = (double)wzRow.Quantity!; worksheet.Range[$"D{currentRow}"].Number = (double)wzRow.PalletNumber!; worksheet.Range[$"E{currentRow}"].Text = wzRow.WzNumber; worksheet.Range[$"F{currentRow}"].Text = wzRow.PartNumber; currentRow++; } for (int i = 0; i < 6; i++) { worksheet.AutofitColumn(i + 1); } using MemoryStream stream = new MemoryStream(); workbook.SaveAs(stream); stream.Position = 0; SendEmail(stream, wzHeader, "Meyle"); } [HttpGet("generate-marelli")] public async Task GeneratePackListForMarelli(Guid packListId) { WzHeaderDto wzHeader = await wzHeaderService.GetByIdMarelli(packListId); WzClientDto? wzClient = await wzClientService.GetById(wzHeader.FK_Client ?? Guid.NewGuid()); MaterialTransactionDto? materialTransaction = await materialTransactionService.GetByWzNumber(wzHeader.WzRowsMarelli.First().WzNumber); using ExcelEngine excelEngine = new ExcelEngine(); IApplication application = excelEngine.Excel; application.DefaultVersion = ExcelVersion.Xlsx; IWorkbook workbook = application.Workbooks.Create(1); IWorksheet worksheet = workbook.Worksheets[0]; IStyle boldFontStyle = workbook.Styles.Add("BoldFontStyle"); boldFontStyle.Font.Bold = true; var mainHeaders = new List { "", "ColliNr", "Magneti Marelli Sales nr", "Engineer number", "Quantity", "Order nr", "Supplier Comments" }; worksheet.Range["A1:B1"].Merge(); worksheet.Range["A1"].Text = "PACKING LIST"; worksheet.Range["A1"].CellStyle = boldFontStyle; worksheet["A3"].Value = "Supplier Name"; worksheet["A3"].CellStyle = boldFontStyle; worksheet["B3"].Value = "FA KROSNO"; worksheet["B3"].CellStyle = boldFontStyle; worksheet["A4"].Value = "Packing List nr"; worksheet["A4"].CellStyle = boldFontStyle; worksheet["B4"].Value = string.Join(", ", wzHeader.WzRowsMarelli.Select(x => x.WzNumber).Distinct()); worksheet["B4"].CellStyle = boldFontStyle; worksheet["A6"].Value = "Date"; worksheet["A6"].CellStyle = boldFontStyle; worksheet["B6"].DateTime = materialTransaction?.CreateDate ?? DateTime.Now; if (!string.IsNullOrEmpty(wzClient?.LogoBase64)) { byte[] logoBytes = Convert.FromBase64String(wzClient.LogoBase64); worksheet.Range["D1:F6"].Merge(); using MemoryStream imageStream = new MemoryStream(logoBytes); worksheet.Pictures.AddPicture(1, 4, imageStream); } int currentRow = 12; for (int i = 0; i < mainHeaders.Count; i++) { string columnLetter = GetColumnLetter(i); worksheet.Range[$"{columnLetter}{currentRow}"].Text = mainHeaders[i]; worksheet.Range[$"{columnLetter}{currentRow}"].CellStyle = boldFontStyle; } currentRow++; foreach (var wzRow in wzHeader.WzRowsMarelli.Where(x => x.PalletNumber != null)) { worksheet.Range[$"A{currentRow}"].Text = "MIX"; worksheet.Range[$"B{currentRow}"].Number = (double)wzRow.PalletNumber!; worksheet.Range[$"C{currentRow}"].Text = wzRow.ItemNumber; worksheet.Range[$"D{currentRow}"].Text = wzRow.EngineerNumber; worksheet.Range[$"E{currentRow}"].Number = (double)wzRow.Quantity!; worksheet.Range[$"F{currentRow}"].Text = wzRow.OrderNumber; worksheet.Range[$"G{currentRow}"].Text = wzRow.WzNumber; currentRow++; } for (int i = 0; i < 6; i++) { worksheet.AutofitColumn(i + 1); } using MemoryStream stream = new MemoryStream(); workbook.SaveAs(stream); stream.Position = 0; SendEmail(stream, wzHeader, "Marelli Magneti"); } private static string GetColumnLetter(int columnIndex) { string columnName = string.Empty; while (columnIndex >= 0) { columnName = (char)('A' + (columnIndex % 26)) + columnName; columnIndex = (columnIndex / 26) - 1; } return columnName; } private void SendEmail(MemoryStream stream, WzHeaderDto wzHeader, string client) { IConfigurationSection smtpSettings = configuration.GetSection("EmailSettings"); string smtpHost = smtpSettings["SmtpServer"] ?? string.Empty; int smtpPort = int.Parse(smtpSettings["Port"] ?? "0"); string smtpUsername = smtpSettings["SenderEmail"] ?? string.Empty; string smtpPassword = smtpSettings["SenderPassword"] ?? string.Empty; string fromEmail = smtpSettings["SenderEmail"] ?? string.Empty; List toEmail = wzHeader.EmailAddresses?.Split(',')?.ToList() ?? (smtpSettings["RecipientEmail"] ?? string.Empty).Split(',').ToList(); string subject = $"{client}: Packing List"; string body = $"W załączeniu znajduje się Packing List dla klienta ${client} wygenerowany {DateTime.Now:dd.MM.yyyy HH:mm:ss}"; using var mailMessage = new MailMessage(); mailMessage.From = new MailAddress(fromEmail); mailMessage.Subject = subject; mailMessage.Body = body; mailMessage.IsBodyHtml = false; mailMessage.Attachments.Add(new Attachment(stream, $"PackingList_{client}_{DateTime.Now:yyyyMMddHHmmss}.xlsx", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")); toEmail.ForEach(x => mailMessage.To.Add(x)); using var smtpClient = new SmtpClient(smtpHost, smtpPort); smtpClient.EnableSsl = true; smtpClient.Credentials = new NetworkCredential(smtpUsername, smtpPassword); smtpClient.Send(mailMessage); } }