* Added ExcelGenerator and EmailSender
This commit is contained in:
136
FaKrosnoApi/Controllers/ExcelGeneratorController.cs
Normal file
136
FaKrosnoApi/Controllers/ExcelGeneratorController.cs
Normal file
@@ -0,0 +1,136 @@
|
||||
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, 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<string>
|
||||
{ "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);
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
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<string> toEmail = (smtpSettings["RecipientEmail"] ?? string.Empty).Split(',').ToList();
|
||||
string subject = "MEYLE: Packing List";
|
||||
string body =
|
||||
$"W załączeniu znajduje się Packing List dla klienta MEYLE 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_Meyle_{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);
|
||||
}
|
||||
}
|
||||
@@ -35,4 +35,11 @@ public class WzHeaderController(IWzHeaderService service, IMaterialTransactionSe
|
||||
await service.CreateHeader(wzHeader);
|
||||
return CreatedAtAction(nameof(CreateHeader), wzHeader);
|
||||
}
|
||||
|
||||
[HttpGet("by-id")]
|
||||
public async Task<ActionResult<WzHeaderDto>> GetById([FromQuery] Guid id)
|
||||
{
|
||||
WzHeaderDto? wzHeader = await service.GetById(id);
|
||||
return wzHeader != null ? Ok(wzHeader) : NotFound();
|
||||
}
|
||||
}
|
||||
@@ -18,6 +18,7 @@
|
||||
</PackageReference>
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="9.0.0" />
|
||||
<PackageReference Include="NSwag.AspNetCore" Version="14.2.0" />
|
||||
<PackageReference Include="Syncfusion.XlsIO.Net.Core" Version="29.2.4" />
|
||||
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="8.3.0" />
|
||||
</ItemGroup>
|
||||
|
||||
|
||||
@@ -108,6 +108,8 @@ builder.Services.AddScoped<IItemCustService, ItemCustService>();
|
||||
|
||||
builder.Services.AddHostedService<TimedHostedService>();
|
||||
|
||||
Syncfusion.Licensing.SyncfusionLicenseProvider.RegisterLicense("NRAiBiAaIQQuGjN/V09+XU9HdVRDX3xKf0x/TGpQb19xflBPallYVBYiSV9jS3tTckVgWHldc3ZUR2lfVE90Vg==");
|
||||
|
||||
var app = builder.Build();
|
||||
|
||||
app.UseOpenApi();
|
||||
|
||||
@@ -24,8 +24,8 @@
|
||||
<ToolbarItems>
|
||||
<ToolbarItem Type="ItemType.Button" Text="Zapisz" Id="SaveButton"
|
||||
PrefixIcon="e-icons e-save" OnClick="SaveChanges"/>
|
||||
<ToolbarItem Type="ItemType.Button" Id="Generuj XLS" PrefixIcon="e-icons export-xls"
|
||||
Text="Usuń Zapisane Filtry" OnClick="ExportXls"/>
|
||||
<ToolbarItem Type="ItemType.Button" Id="Generuj XLS" PrefixIcon="e-icons e-export-xls"
|
||||
Text="Generuj XLS" OnClick="ExportXls"/>
|
||||
</ToolbarItems>
|
||||
</SfToolbar>
|
||||
<GridColumns>
|
||||
@@ -99,6 +99,6 @@
|
||||
|
||||
private async Task ExportXls()
|
||||
{
|
||||
await _grid.ExportToExcelAsync();
|
||||
await WarehouseService.GenerateXlsForMeyleAsync(WzHeader);
|
||||
}
|
||||
}
|
||||
@@ -80,4 +80,14 @@ public class WarehouseService(IHttpClientFactory httpClientFactory)
|
||||
var response = await _httpClient.PutAsJsonAsync("api/WzRowMeyle", wzRowsMeyle);
|
||||
response.EnsureSuccessStatusCode();
|
||||
}
|
||||
|
||||
public async Task GenerateXlsForMeyleAsync(Guid wzHeaderId)
|
||||
{
|
||||
var response = await _httpClient.GetAsync($"api/ExcelGenerator/generate-meyle?packListId={wzHeaderId}");
|
||||
response.EnsureSuccessStatusCode();
|
||||
if (response.StatusCode != System.Net.HttpStatusCode.OK)
|
||||
{
|
||||
throw new Exception("Failed to generate XLS for Mayle");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -5,4 +5,6 @@ public class WzHeaderDto
|
||||
public Guid ID { get; set; }
|
||||
public Guid? FK_Client { get; set; }
|
||||
public DateTime CreatedDate { get; set; }
|
||||
|
||||
public IEnumerable<WzRowMeyleDto> WzRowsMeyle { get; set; } = new List<WzRowMeyleDto>();
|
||||
}
|
||||
@@ -6,4 +6,5 @@ public interface IWzHeaderService
|
||||
{
|
||||
Task<IEnumerable<WzHeaderDto>> GetAll();
|
||||
Task CreateHeader(WzHeaderDto wzHeader);
|
||||
Task<WzHeaderDto> GetById(Guid id);
|
||||
}
|
||||
@@ -18,4 +18,16 @@ public class WzHeaderService(SytelineSaAppDbContext context, IMapper mapper) : I
|
||||
await context.WzHeaders.AddAsync(entity);
|
||||
await context.SaveChangesAsync();
|
||||
}
|
||||
|
||||
public async Task<WzHeaderDto> GetById(Guid id)
|
||||
{
|
||||
var entity = await context.WzHeaders.FindAsync(id);
|
||||
var items = await context.WzRowsMeyle.Where(x => x.FK_Header == id).Select(x => mapper.Map<WzRowMeyleDto>(x))
|
||||
.ToListAsync();
|
||||
|
||||
WzHeaderDto wzHeader = mapper.Map<WzHeaderDto>(entity);
|
||||
wzHeader.WzRowsMeyle = items;
|
||||
|
||||
return wzHeader;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user