NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io

using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using PdfSharpCore.Drawing;
using PdfSharpCore.Pdf;
using MigraDoc.DocumentObjectModel;
using MigraDoc.Rendering;
using System;
using System.IO;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Hosting;
using System.Globalization;

namespace SMXServicesConsulting.Web.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");

// Create a new PDF document using PdfSharpCore
PdfDocument pdfDocument = new PdfDocument();
PdfPage pdfPage = pdfDocument.AddPage();
XGraphics gfx = XGraphics.FromPdfPage(pdfPage);
XFont titleFont = new XFont("Arial", 16, XFontStyle.Bold);

// Draw the logo image at the top left
string logoPath = Path.Combine(_environment.WebRootPath, "images", "logo_white.jpeg");
if (System.IO.File.Exists(logoPath))
{
try
{
using (var imageStream = new FileStream(logoPath, FileMode.Open, FileAccess.Read))
{
XImage xImage = XImage.FromStream(imageStream);
gfx.DrawImage(xImage, 40, 40, 100, 60); // Adjust the position and size as needed
}
}
catch (Exception ex)
{
// Handle image loading exception
Console.WriteLine($"Error loading image: {ex.Message}");
}
}
else
{
Console.WriteLine("Logo image not found at path: " + logoPath);
}

// Add a title below the logo
gfx.DrawString("Commissions Eber Oroz: " + month, titleFont, XBrushes.Black, new XRect(0, 120, pdfPage.Width, pdfPage.Height), XStringFormats.TopCenter);

// Create a MigraDoc document to manage the rest of the content
Document doc = new Document();
Section section = doc.AddSection();
section.PageSetup.LeftMargin = Unit.FromCentimeter(1.1);

// Create a table for the data
var table = section.AddTable();
table.Borders.Width = 0.5;
table.Borders.Color = Colors.Black;

// Define columns
table.AddColumn("5cm");
table.AddColumn("5cm");
table.AddColumn("3cm");
table.AddColumn("3cm");
table.AddColumn("3cm");

// Add header row
var row = table.AddRow();
row.Shading.Color = Colors.CornflowerBlue;
row.HeadingFormat = true;
row.Format.Font.Bold = true;
row.Format.Font.Size = 11;
row.Cells[0].AddParagraph("Customer");
row.Cells[1].AddParagraph("ITEM");
row.Cells[2].AddParagraph("Margen");
row.Cells[3].AddParagraph("Coef. 5%");
row.Cells[4].AddParagraph("Eber");

// Add data rows and calculate total Eber
decimal totalEber = 0;
if (model.HourlyCommissionCompanyGroupModelList != null)
{
foreach (var companyGroup in model.HourlyCommissionCompanyGroupModelList)
{
if (companyGroup.HourlyCommissionModel != null)
{
foreach (var item in companyGroup.HourlyCommissionModel)
{
row = table.AddRow();
row.Cells[0].AddParagraph(item.Customer);
row.Cells[1].AddParagraph(item.ITEM);
row.Cells[2].AddParagraph(item?.Margen?.ToString());
row.Cells[3].AddParagraph(item?.Coeff5Percent?.ToString());
row.Cells[4].AddParagraph(item?.Eber?.ToString());

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

// Footer row for each company group
row = table.AddRow();
row.Shading.Color = Colors.LightGray;
row.Cells[0].AddParagraph(companyGroup.Customer);
row.Cells[1].AddParagraph(companyGroup.EmployeeCount.ToString());
row.Cells[2].AddParagraph(companyGroup.TotalMargen.ToString("C"));
row.Cells[3].AddParagraph(companyGroup.TotalCoeff5Percent.ToString("C"));
row.Cells[4].AddParagraph(companyGroup.TotalEber.ToString("C"));
}
}

// Add total Eber at the end of the table
row = table.AddRow();
row.Shading.Color = Colors.CornflowerBlue;
row.HeadingFormat = true;
row.Format.Font.Bold = true;
row.Format.Font.Size = 11;
row.Cells[0].MergeRight = 3;
row.Cells[0].AddParagraph("Total Eber");
row.Cells[4].AddParagraph(totalEber.ToString("C"));
}

// Render the MigraDoc document into the PDF page
var renderer = new DocumentRenderer(doc);
renderer.PrepareDocument();
renderer.RenderObject(gfx, XUnit.FromCentimeter(1), XUnit.FromCentimeter(10), pdfPage.Width, doc);

// Save the PDF to a memory stream
using (var memoryStream = new MemoryStream())
{
pdfDocument.Save(memoryStream);
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);
}
}
}
}
     
 
what is notes.io
 

Notes is a web-based application for online 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 14 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.