NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io

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

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");

// Create a new MigraDoc document
Document document = new Document();
Section section = document.AddSection();

// Add the logo image
string logoPath = Path.Combine(_environment.WebRootPath, "images", "logo_white.jpeg");
if (System.IO.File.Exists(logoPath))
{
try
{
var image = section.AddImage(logoPath);
image.LockAspectRatio = true;
image.Width = "2cm"; // Adjust the width as needed
image.Left = ShapePosition.Left;
}
catch (Exception ex)
{
Console.WriteLine($"Error loading image: {ex.Message}");
}
}
else
{
Console.WriteLine("Logo image not found at path: " + logoPath);
}

// Add a title below the logo
var titleParagraph = section.AddParagraph();
titleParagraph.AddFormattedText("Commissions Eber Oroz: " + month, TextFormat.Bold);
titleParagraph.Format.FontSize = 16;
titleParagraph.Format.SpaceBefore = "1cm";
titleParagraph.Format.SpaceAfter = "1cm";
titleParagraph.Format.Alignment = ParagraphAlignment.Center;

// 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 a PDF document
var pdfRenderer = new PdfDocumentRenderer(true)
{
Document = document
};
pdfRenderer.RenderDocument();

// Save the PDF to a memory stream
using (var memoryStream = new MemoryStream())
{
pdfRenderer.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.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.