NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io

using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using PdfSharpCore.Pdf;
using System;
using System.Globalization;
using System.IO;
using System.Threading.Tasks;
using YourNamespace.Services;
using Microsoft.AspNetCore.Hosting;
using VetCV.HtmlRendererCore.PdfSharpCore;
using System.Text;

namespace YourNamespace.Controllers
{
public class CommissionController : Controller
{
private readonly ICommissionService _commissionService;
private readonly IWebHostEnvironment _environment;

public CommissionController(ICommissionService commissionService, IWebHostEnvironment environment)
{
_commissionService = commissionService;
_environment = environment;
}

[HttpPost]
[Authorize]
public async Task<IActionResult> GeneratePdfOfEber(string startDate, string endDate)
{
try
{
string managerId = "5303747171857";
var model = await _commissionService.GetHourlyCommissionForAccountManagersAsync(startDate, endDate, managerId);

DateTime.TryParseExact(endDate, "MM/dd/yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out DateTime parsedEndDate);
var month = parsedEndDate.ToString("MMMM yyyy");

// Load the HTML template from wwwroot/template directory
var templatePath = Path.Combine(_environment.WebRootPath, "template", "EberCommissionTemplate.html");
var htmlTemplate = await System.IO.File.ReadAllTextAsync(templatePath);

// Replace image paths with base64 strings
htmlTemplate = ReplaceImagePathsWithBase64(htmlTemplate);

// Replace placeholders in the HTML template with actual data
var htmlContent = htmlTemplate
.Replace("{{Month}}", month)
.Replace("{{TableRows}}", GenerateTableRows(model));

// Convert HTML to PDF using HtmlRenderer
var pdf = TheArtOfDev.HtmlRenderer.PdfSharp.PdfGenerator.GeneratePdf(htmlContent, PdfSharpCore.PageSize.A4);

// Add watermark
AddWatermark(pdf);

using (var memoryStream = new MemoryStream())
{
pdf.Save(memoryStream);
memoryStream.Position = 0;
return File(memoryStream.ToArray(), "application/pdf", $"EO_Commissions_{month}.pdf");
}
}
catch (Exception ex)
{
// Log the error and return a bad request response
Console.WriteLine("Error generating PDF: " + ex.Message);
return BadRequest("Error generating PDF: " + ex.Message);
}
}

private string GenerateTableRows(YourModelType model)
{
var sb = new StringBuilder();
decimal totalEber = 0;

if (model.HourlyCommissionCompanyGroupModelList != null)
{
foreach (var companyGroup in model.HourlyCommissionCompanyGroupModelList)
{
if (companyGroup.HourlyCommissionModel != null)
{
foreach (var item in companyGroup.HourlyCommissionModel)
{
sb.AppendLine("<tr>");
sb.AppendLine($"<td>{item.Customer}</td>");
sb.AppendLine($"<td>{item.ITEM}</td>");
sb.AppendLine($"<td>{item?.Margen?.ToString()}</td>");
sb.AppendLine($"<td>{item?.Coeff5Percent?.ToString()}</td>");
sb.AppendLine($"<td>{item?.Eber?.ToString()}</td>");
sb.AppendLine("</tr>");

// Sum total Eber
if (decimal.TryParse(item.Eber, out var eber))
{
totalEber += eber;
}
}

// Footer row for each company group
sb.AppendLine("<tr style='background-color: #f2f2f2;'>");
sb.AppendLine($"<td>{companyGroup.Customer}</td>");
sb.AppendLine($"<td>{companyGroup.EmployeeCount}</td>");
sb.AppendLine($"<td>{companyGroup.TotalMargen.ToString("C")}</td>");
sb.AppendLine($"<td>{companyGroup.TotalCoeff5Percent.ToString("C")}</td>");
sb.AppendLine($"<td>{companyGroup.TotalEber.ToString("C")}</td>");
sb.AppendLine("</tr>");
}
}

// Add total Eber at the end of the table
sb.AppendLine("<tr style='background-color: #d9e2f3; font-weight: bold;'>");
sb.AppendLine("<td colspan='4'>Total Eber</td>");
sb.AppendLine($"<td>{totalEber.ToString("C")}</td>");
sb.AppendLine("</tr>");
}

return sb.ToString();
}

private string ReplaceImagePathsWithBase64(string htmlContent)
{
var imagePath = Path.Combine(_environment.WebRootPath, "images/logo.png");
if (System.IO.File.Exists(imagePath))
{
var base64Image = Convert.ToBase64String(System.IO.File.ReadAllBytes(imagePath));
var imageSrc = $"data:image/png;base64,{base64Image}";
htmlContent = htmlContent.Replace("/images/logo.png", imageSrc);
}

return htmlContent;
}

private void AddWatermark(PdfDocument pdfDocument)
{
foreach (var page in pdfDocument.Pages)
{
var gfx = XGraphics.FromPdfPage(page, XGraphicsPdfPageOptions.Append);
var font = new XFont("Verdana", 50, XFontStyle.Bold);
gfx.DrawString("Confidential", font, XBrushes.Red,
new XRect(0, 0, page.Width, page.Height),
XStringFormats.Center);
}
}
}
}

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>EO Commissions</title>
<style>
body { font-family: Arial, sans-serif; }
.header { text-align: center; margin-bottom: 20px; }
.header img { width: 100px; }
.title { font-size: 24px; font-weight: bold; margin-top: 20px; }
.table { width: 100%; border-collapse: collapse; }
.table th, .table td { border: 1px solid black; padding: 8px; text-align: left; }
.table th { background-color: #f2f2f2; }
.footer { margin-top: 20px; font-weight: bold; }
</style>
</head>
<body>
<div class="header">
<img src="/images/logo.png" alt="Logo" />
<div class="title">Commissions Eber Oroz: {{Month}}</div>
</div>
<table class="table">
<thead>
<tr>
<th>Customer</th>
<th>ITEM</th>
<th>Margen</th>
<th>Coef. 5%</th>
<th>Eber</th>
</tr>
</thead>
<tbody>
{{TableRows}}
</tbody>
</table>
</body>
</html>

     
 
what is notes.io
 

Notes.io is a web-based application for taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000 notes created and continuing...

With notes.io;

  • * You can take a note from anywhere and any device with internet connection.
  • * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
  • * You can quickly share your contents without website, blog and e-mail.
  • * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
  • * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.

Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.

Easy: Notes.io doesn’t require installation. Just write and share note!

Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )

Free: Notes.io works for 12 years and has been free since the day it was started.


You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;


Email: [email protected]

Twitter: http://twitter.com/notesio

Instagram: http://instagram.com/notes.io

Facebook: http://facebook.com/notesio



Regards;
Notes.io Team

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.