NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io

using Microsoft.AspNetCore.Mvc;
using MigraDoc.DocumentObjectModel;
using MigraDoc.DocumentObjectModel.Tables;
using MigraDoc.Rendering;
using PdfSharpCore.Pdf;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using YourNamespace.Models;
using YourNamespace.Services;

namespace YourNamespace.Controllers
{
public class CommissionController : Controller
{
private readonly ICommissionService _commissionService;
private readonly IEmailService _emailService;

public CommissionController(ICommissionService commissionService, IEmailService emailService)
{
_commissionService = commissionService;
_emailService = emailService;
}

[HttpPost]
public async Task<IActionResult> CloseCommission(string startDate, string endDate)
{
var accountManagers = await _commissionService.GetAccountManagersAsync();

foreach (var manager in accountManagers)
{
var model = await _commissionService.GetHourlyCommissionForAccountManagersAsync(startDate, endDate, manager.Id);

byte[] pdfBytes = GeneratePdfForAccountManager(model, manager.Name);

string fileName = $"EO_Commissions_{endDate:MMM_yyyy}.pdf";
await _emailService.SendEmailWithAttachmentAsync(manager.Email, "Commission Report", "Please find attached your commission report.", pdfBytes, fileName);
}

return Ok("Commission process completed.");
}

private byte[] GeneratePdfForAccountManager(CommissionMasterModelForHourlyEmployee model, string managerName)
{
Document document = new Document();
Section section = document.AddSection();

// Adding logo
string logoPath = Path.Combine("wwwroot", "images", "logo.png"); // Adjust path as needed
Image logo = section.AddImage(logoPath);
logo.LockAspectRatio = true;
logo.Width = "3cm";

Paragraph titleParagraph = section.AddParagraph($"{managerName}'s Commission Report");
titleParagraph.Format.FontSize = 18;
titleParagraph.Format.Bold = true;
titleParagraph.Format.SpaceAfter = "1cm";
titleParagraph.Format.Alignment = ParagraphAlignment.Center;

// Adding table
Table table = section.AddTable();
table.Borders.Width = 0.25;
table.Borders.Color = Colors.Black;
table.Rows.LeftIndent = 0;

// Define columns
table.AddColumn("4cm");
table.AddColumn("4cm");
table.AddColumn("4cm");
table.AddColumn("4cm");
table.AddColumn("4cm");

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

// Add data rows
double totalEber = 0;
if (model.CommissionModelListForHourlyEmployee != null)
{
foreach (var companyGroup in model.CommissionModelListForHourlyEmployee)
{
if (companyGroup.HourlyCommissionModel != null)
{
foreach (var item in companyGroup.HourlyCommissionModel)
{
Row row = table.AddRow();
row.Cells[0].AddParagraph(item.Customer ?? "N/A");
row.Cells[1].AddParagraph(item.ITEM ?? "N/A");
row.Cells[2].AddParagraph(item.Margen ?? "N/A");
row.Cells[3].AddParagraph(item.Coeff5Percent ?? "N/A");
row.Cells[4].AddParagraph(item.Eber ?? "N/A");

if (double.TryParse(item.Eber, out double eberValue))
{
totalEber += eberValue;
}
}

// Add company footer row
Row companyFooterRow = table.AddRow();
companyFooterRow.Shading.Color = Colors.LightGray;
companyFooterRow.Cells[0].AddParagraph(companyGroup.Customer ?? "N/A");
companyFooterRow.Cells[1].AddParagraph(companyGroup.EmployeeCount.ToString() ?? "N/A");
companyFooterRow.Cells[2].AddParagraph(companyGroup.TotalMargen.ToString("C"));
companyFooterRow.Cells[3].AddParagraph(companyGroup.TotalCoeff5Percent.ToString("C"));
companyFooterRow.Cells[4].AddParagraph(companyGroup.TotalEber.ToString("C"));
}
}

// Add total Eber row at the end
Row totalRow = table.AddRow();
totalRow.Cells[0].MergeRight = 3;
totalRow.Cells[0].AddParagraph("Total Eber");
totalRow.Cells[0].Format.Alignment = ParagraphAlignment.Right;
totalRow.Cells[4].AddParagraph(totalEber.ToString("N2"));
totalRow.Cells[4].Format.Font.Bold = true;
}
else
{
Row row = table.AddRow();
row.Cells[0].MergeRight = 4;
row.Cells[0].AddParagraph("No data available for the selected period.");
}

PdfDocumentRenderer pdfRenderer = new PdfDocumentRenderer(false)
{
Document = document
};
pdfRenderer.RenderDocument();

using (var memoryStream = new MemoryStream())
{
pdfRenderer.PdfDocument.Save(memoryStream);
return memoryStream.ToArray();
}
}
}
}
     
 
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.