Generowanie raportu PDF - rozwinięcie
This commit is contained in:
parent
717eb6ed1f
commit
95513913e3
@ -4,6 +4,7 @@ using System.IO;
|
|||||||
using System.Linq;
|
using System.Linq;
|
||||||
using FirmTracker_Server.nHibernate;
|
using FirmTracker_Server.nHibernate;
|
||||||
using FirmTracker_Server.nHibernate.Expenses;
|
using FirmTracker_Server.nHibernate.Expenses;
|
||||||
|
using FirmTracker_Server.nHibernate.Transactions;
|
||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
using QuestPDF.Fluent;
|
using QuestPDF.Fluent;
|
||||||
using QuestPDF.Helpers;
|
using QuestPDF.Helpers;
|
||||||
@ -16,36 +17,86 @@ namespace FirmTracker_Server.Controllers
|
|||||||
public class PdfController : ControllerBase
|
public class PdfController : ControllerBase
|
||||||
{
|
{
|
||||||
private readonly IExpenseRepository _expenseRepository;
|
private readonly IExpenseRepository _expenseRepository;
|
||||||
|
private readonly ITransactionRepository _transactionRepository;
|
||||||
|
|
||||||
public PdfController(IExpenseRepository expenseRepository)
|
public PdfController(IExpenseRepository expenseRepository, ITransactionRepository transactionRepository)
|
||||||
{
|
{
|
||||||
_expenseRepository = expenseRepository;
|
_expenseRepository = expenseRepository;
|
||||||
|
_transactionRepository = transactionRepository;
|
||||||
}
|
}
|
||||||
|
|
||||||
[HttpGet("download")]
|
[HttpGet("download")]
|
||||||
public IActionResult DownloadExpenseReport()
|
public IActionResult DownloadReport(
|
||||||
|
[FromQuery] string reportType, // "expenses" or "transactions"
|
||||||
|
[FromQuery] DateTime? startDate,
|
||||||
|
[FromQuery] DateTime? endDate)
|
||||||
{
|
{
|
||||||
// Fetch expense data from the repository
|
try
|
||||||
List<Expense> expenses = _expenseRepository.GetAllExpenses();
|
{
|
||||||
|
// Validate date inputs and set default values
|
||||||
|
DateTime start = startDate ?? DateTime.MinValue;
|
||||||
|
DateTime end = endDate ?? DateTime.MaxValue;
|
||||||
|
|
||||||
// Generate the PDF file
|
// Validate report type
|
||||||
byte[] pdfBytes = GeneratePdf(expenses);
|
if (string.IsNullOrEmpty(reportType) ||
|
||||||
string date = DateTime.Now.ToString("yyyy-MM-dd");
|
(reportType.ToLower() != "expenses" && reportType.ToLower() != "transactions"))
|
||||||
string fileName = $"ExpenseReport_{date}.pdf";
|
{
|
||||||
|
return BadRequest("Invalid report type. Please specify 'expenses' or 'transactions'.");
|
||||||
|
}
|
||||||
|
|
||||||
// Return the PDF as a downloadable file
|
if (reportType.ToLower() == "expenses")
|
||||||
|
{
|
||||||
|
return GenerateExpenseReport(start, end);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return GenerateTransactionReport(start, end);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
return StatusCode(500, $"Internal server error: {ex.Message}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private IActionResult GenerateExpenseReport(DateTime start, DateTime end)
|
||||||
|
{
|
||||||
|
var expenses = _expenseRepository.GetAllExpenses()
|
||||||
|
.Where(e => e.Date >= start && e.Date <= end)
|
||||||
|
.ToList();
|
||||||
|
|
||||||
|
if (!expenses.Any())
|
||||||
|
{
|
||||||
|
return BadRequest($"No expenses found between {start:yyyy-MM-dd} and {end:yyyy-MM-dd}.");
|
||||||
|
}
|
||||||
|
|
||||||
|
var pdfBytes = GenerateExpensePdf(expenses, start, end);
|
||||||
|
string fileName = $"ExpenseReport_{start:yyyy-MM-dd}_to_{end:yyyy-MM-dd}.pdf";
|
||||||
return File(pdfBytes, "application/pdf", fileName);
|
return File(pdfBytes, "application/pdf", fileName);
|
||||||
}
|
}
|
||||||
|
|
||||||
private byte[] GeneratePdf(List<Expense> expenses)
|
private IActionResult GenerateTransactionReport(DateTime start, DateTime end)
|
||||||
|
{
|
||||||
|
var transactions = _transactionRepository.GetTransactionsByDateRange(start, end);
|
||||||
|
|
||||||
|
if (!transactions.Any())
|
||||||
|
{
|
||||||
|
return BadRequest($"No transactions found between {start:yyyy-MM-dd} and {end:yyyy-MM-dd}.");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Fetch transaction products for all transactions in one query
|
||||||
|
var transactionIds = transactions.Select(t => t.Id).ToList();
|
||||||
|
var transactionProducts = _transactionRepository.GetTransactionProductsForTransactions(transactionIds);
|
||||||
|
|
||||||
|
var pdfBytes = GenerateTransactionPdf(transactions, transactionProducts, start, end);
|
||||||
|
string fileName = $"TransactionReport_{start:yyyy-MM-dd}_to_{end:yyyy-MM-dd}.pdf";
|
||||||
|
return File(pdfBytes, "application/pdf", fileName);
|
||||||
|
}
|
||||||
|
|
||||||
|
private byte[] GenerateTransactionPdf(List<Transaction> transactions, List<TransactionProduct> transactionProducts, DateTime startDate, DateTime endDate)
|
||||||
{
|
{
|
||||||
using (var ms = new MemoryStream())
|
using (var ms = new MemoryStream())
|
||||||
{
|
{
|
||||||
// Calculate total and average expenses
|
|
||||||
decimal totalExpenses = expenses.Sum(e => e.Value);
|
|
||||||
decimal averageExpense = expenses.Count > 0 ? totalExpenses / expenses.Count : 0;
|
|
||||||
|
|
||||||
// Define the document using QuestPDF
|
|
||||||
Document.Create(container =>
|
Document.Create(container =>
|
||||||
{
|
{
|
||||||
container.Page(page =>
|
container.Page(page =>
|
||||||
@ -57,7 +108,95 @@ namespace FirmTracker_Server.Controllers
|
|||||||
|
|
||||||
// Main header
|
// Main header
|
||||||
page.Header()
|
page.Header()
|
||||||
.Text("Expense Report")
|
.Text("Raport transakcji")
|
||||||
|
.FontSize(20)
|
||||||
|
.SemiBold()
|
||||||
|
.AlignCenter();
|
||||||
|
|
||||||
|
// Summary section
|
||||||
|
page.Content().PaddingVertical(1, Unit.Centimetre).Column(column =>
|
||||||
|
{
|
||||||
|
column.Spacing(10);
|
||||||
|
|
||||||
|
column.Item().Text($"Transakcje od ({startDate:yyyy-MM-dd} do {endDate:yyyy-MM-dd})")
|
||||||
|
.FontSize(16).Underline();
|
||||||
|
|
||||||
|
// Add table header
|
||||||
|
column.Item().Row(row =>
|
||||||
|
{
|
||||||
|
row.RelativeItem().Text("Data").SemiBold();
|
||||||
|
row.RelativeItem().Text("Typ płatności").SemiBold();
|
||||||
|
row.RelativeItem().Text("Kwota razem").SemiBold();
|
||||||
|
row.RelativeItem().Text("Rabat").SemiBold();
|
||||||
|
row.RelativeItem().Text("Opis").SemiBold();
|
||||||
|
});
|
||||||
|
|
||||||
|
// Populate table rows with transaction data
|
||||||
|
foreach (var transaction in transactions)
|
||||||
|
{
|
||||||
|
column.Item().Row(row =>
|
||||||
|
{
|
||||||
|
row.RelativeItem().Text(transaction.Date.ToString("yyyy-MM-dd"));
|
||||||
|
row.RelativeItem().Text(transaction.PaymentType);
|
||||||
|
row.RelativeItem().Text(transaction.TotalPrice.ToString("C"));
|
||||||
|
row.RelativeItem().Text(transaction.Discount.ToString("C"));
|
||||||
|
row.RelativeItem().Text(transaction.Description);
|
||||||
|
});
|
||||||
|
|
||||||
|
// Fetch and display transaction products for this transaction
|
||||||
|
var products = transactionProducts
|
||||||
|
.Where(tp => tp.TransactionId == transaction.Id)
|
||||||
|
.ToList();
|
||||||
|
|
||||||
|
if (products.Any())
|
||||||
|
{
|
||||||
|
column.Item().Text("Produkty:").SemiBold();
|
||||||
|
foreach (var product in products)
|
||||||
|
{
|
||||||
|
column.Item().Row(productRow =>
|
||||||
|
{
|
||||||
|
productRow.RelativeItem().Text($"Nazwa produktu: {product.ProductName}");
|
||||||
|
productRow.RelativeItem().Text($"Ilość: {product.Quantity}");
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// Footer with generation date
|
||||||
|
page.Footer()
|
||||||
|
.AlignCenter()
|
||||||
|
.Text(text =>
|
||||||
|
{
|
||||||
|
text.Span("Wygenerowano przez automat FT: ");
|
||||||
|
text.Span(DateTime.Now.ToString("yyyy-MM-dd")).SemiBold();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}).GeneratePdf(ms);
|
||||||
|
|
||||||
|
return ms.ToArray();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private byte[] GenerateExpensePdf(List<Expense> expenses, DateTime startDate, DateTime endDate)
|
||||||
|
{
|
||||||
|
using (var ms = new MemoryStream())
|
||||||
|
{
|
||||||
|
decimal totalExpenses = expenses.Sum(e => e.Value);
|
||||||
|
decimal averageExpense = expenses.Any() ? totalExpenses / expenses.Count : 0;
|
||||||
|
|
||||||
|
Document.Create(container =>
|
||||||
|
{
|
||||||
|
container.Page(page =>
|
||||||
|
{
|
||||||
|
page.Size(PageSizes.A4);
|
||||||
|
page.Margin(2, Unit.Centimetre);
|
||||||
|
page.PageColor(Colors.White);
|
||||||
|
page.DefaultTextStyle(x => x.FontSize(12));
|
||||||
|
|
||||||
|
// Main header
|
||||||
|
page.Header()
|
||||||
|
.Text("Raport wydatków")
|
||||||
.FontSize(20)
|
.FontSize(20)
|
||||||
.SemiBold()
|
.SemiBold()
|
||||||
.AlignCenter();
|
.AlignCenter();
|
||||||
@ -69,21 +208,20 @@ namespace FirmTracker_Server.Controllers
|
|||||||
|
|
||||||
column.Item().Row(row =>
|
column.Item().Row(row =>
|
||||||
{
|
{
|
||||||
row.RelativeItem().Text($"Total Expenses: {totalExpenses:C}").FontSize(14).Bold();
|
row.RelativeItem().Text($"Łączne wydatki: {totalExpenses:C}").FontSize(14).Bold();
|
||||||
row.RelativeItem().Text($"Average Expense: {averageExpense:C}").FontSize(14).Bold();
|
row.RelativeItem().Text($"Średnie wydatki dzienne: {averageExpense:C}").FontSize(14).Bold();
|
||||||
});
|
});
|
||||||
|
|
||||||
column.Item().Text("Expense Details").FontSize(16).Underline();
|
column.Item().Text($"Szczegóły wydatków od ({startDate:yyyy-MM-dd} do {endDate:yyyy-MM-dd})")
|
||||||
|
.FontSize(16).Underline();
|
||||||
|
|
||||||
// Add a table header
|
|
||||||
column.Item().Row(row =>
|
column.Item().Row(row =>
|
||||||
{
|
{
|
||||||
row.RelativeItem().Text("Date").SemiBold();
|
row.RelativeItem().Text("Data").SemiBold();
|
||||||
row.RelativeItem().Text("Value").SemiBold();
|
row.RelativeItem().Text("Kwota").SemiBold();
|
||||||
row.RelativeItem().Text("Description").SemiBold();
|
row.RelativeItem().Text("Opis").SemiBold();
|
||||||
});
|
});
|
||||||
|
|
||||||
// Populate table rows with expense data
|
|
||||||
foreach (var expense in expenses)
|
foreach (var expense in expenses)
|
||||||
{
|
{
|
||||||
column.Item().Row(row =>
|
column.Item().Row(row =>
|
||||||
@ -95,12 +233,11 @@ namespace FirmTracker_Server.Controllers
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// Footer with generation date
|
|
||||||
page.Footer()
|
page.Footer()
|
||||||
.AlignCenter()
|
.AlignCenter()
|
||||||
.Text(text =>
|
.Text(text =>
|
||||||
{
|
{
|
||||||
text.Span("Generated on ");
|
text.Span("Wygenerowano przez automat FT: ");
|
||||||
text.Span(DateTime.Now.ToString("yyyy-MM-dd")).SemiBold();
|
text.Span(DateTime.Now.ToString("yyyy-MM-dd")).SemiBold();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
@ -177,6 +177,7 @@ namespace FirmTracker_Server
|
|||||||
services.AddScoped<ErrorHandling>();
|
services.AddScoped<ErrorHandling>();
|
||||||
services.AddScoped<IPasswordHasher<User>, PasswordHasher<User>>();
|
services.AddScoped<IPasswordHasher<User>, PasswordHasher<User>>();
|
||||||
services.AddScoped<IExpenseRepository, ExpenseRepository>();
|
services.AddScoped<IExpenseRepository, ExpenseRepository>();
|
||||||
|
services.AddScoped<ITransactionRepository, TransactionRepository>();
|
||||||
// services.AddScoped<IWorkdayRepository, WorkdayRepository>();
|
// services.AddScoped<IWorkdayRepository, WorkdayRepository>();
|
||||||
services.AddMvc();
|
services.AddMvc();
|
||||||
}
|
}
|
||||||
|
@ -157,7 +157,7 @@ namespace FirmTracker_Server
|
|||||||
};
|
};
|
||||||
var expense2 = new Expense
|
var expense2 = new Expense
|
||||||
{
|
{
|
||||||
Date = DateTime.Now,
|
Date = DateTime.Parse("2024-09-10 16:11:17.6232408"),
|
||||||
Value = 990.99m,
|
Value = 990.99m,
|
||||||
Description = "naprawa pieca - 25.05.2024"
|
Description = "naprawa pieca - 25.05.2024"
|
||||||
};
|
};
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using FirmTracker_Server.nHibernate.Expenses;
|
using FirmTracker_Server.nHibernate.Expenses;
|
||||||
|
using FirmTracker_Server.nHibernate.Transactions;
|
||||||
using NHibernate;
|
using NHibernate;
|
||||||
|
|
||||||
namespace FirmTracker_Server.nHibernate
|
namespace FirmTracker_Server.nHibernate
|
||||||
@ -13,7 +14,128 @@ namespace FirmTracker_Server.nHibernate
|
|||||||
void UpdateExpense(Expense expense);
|
void UpdateExpense(Expense expense);
|
||||||
void DeleteExpense(int expenseId);
|
void DeleteExpense(int expenseId);
|
||||||
}
|
}
|
||||||
|
public interface ITransactionRepository
|
||||||
|
{
|
||||||
|
List<Transaction> GetAllTransactions();
|
||||||
|
Transaction GetTransaction(int transactionId);
|
||||||
|
List<Transaction> GetTransactionsByDateRange(DateTime startDate, DateTime endDate);
|
||||||
|
List<TransactionProduct> GetTransactionProducts(int transactionId);
|
||||||
|
void AddTransaction(Transaction transaction);
|
||||||
|
void UpdateTransaction(Transaction transaction);
|
||||||
|
void DeleteTransaction(int transactionId);
|
||||||
|
List<TransactionProduct> GetTransactionProductsForTransactions(List<int> transactionIds);
|
||||||
|
}
|
||||||
|
public class TransactionRepository : ITransactionRepository
|
||||||
|
{
|
||||||
|
// Retrieve all transactions
|
||||||
|
public List<Transaction> GetAllTransactions()
|
||||||
|
{
|
||||||
|
using (var session = SessionFactory.OpenSession())
|
||||||
|
{
|
||||||
|
return session.Query<Transaction>().ToList();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public List<TransactionProduct> GetTransactionProductsForTransactions(List<int> transactionIds)
|
||||||
|
{
|
||||||
|
using (var session = SessionFactory.OpenSession())
|
||||||
|
{
|
||||||
|
return session.Query<TransactionProduct>()
|
||||||
|
.Where(tp => transactionIds.Contains(tp.TransactionId))
|
||||||
|
.ToList();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Retrieve a specific transaction by ID
|
||||||
|
public Transaction GetTransaction(int transactionId)
|
||||||
|
{
|
||||||
|
using (var session = SessionFactory.OpenSession())
|
||||||
|
{
|
||||||
|
return session.Get<Transaction>(transactionId);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Retrieve transactions within a specific date range
|
||||||
|
public List<Transaction> GetTransactionsByDateRange(DateTime startDate, DateTime endDate)
|
||||||
|
{
|
||||||
|
using (var session = SessionFactory.OpenSession())
|
||||||
|
{
|
||||||
|
return session.Query<Transaction>()
|
||||||
|
.Where(t => t.Date >= startDate && t.Date <= endDate)
|
||||||
|
.ToList();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Retrieve all products for a specific transaction
|
||||||
|
public List<TransactionProduct> GetTransactionProducts(int transactionId)
|
||||||
|
{
|
||||||
|
using (var session = SessionFactory.OpenSession())
|
||||||
|
{
|
||||||
|
return session.Query<TransactionProduct>()
|
||||||
|
.Where(tp => tp.TransactionId == transactionId)
|
||||||
|
.ToList();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add a new transaction
|
||||||
|
public void AddTransaction(Transaction transaction)
|
||||||
|
{
|
||||||
|
using (var session = SessionFactory.OpenSession())
|
||||||
|
using (var transactionScope = session.BeginTransaction())
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
session.Save(transaction);
|
||||||
|
transactionScope.Commit();
|
||||||
|
}
|
||||||
|
catch
|
||||||
|
{
|
||||||
|
transactionScope.Rollback();
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update an existing transaction
|
||||||
|
public void UpdateTransaction(Transaction transaction)
|
||||||
|
{
|
||||||
|
using (var session = SessionFactory.OpenSession())
|
||||||
|
using (var transactionScope = session.BeginTransaction())
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
session.Update(transaction);
|
||||||
|
transactionScope.Commit();
|
||||||
|
}
|
||||||
|
catch
|
||||||
|
{
|
||||||
|
transactionScope.Rollback();
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Delete a transaction by ID
|
||||||
|
public void DeleteTransaction(int transactionId)
|
||||||
|
{
|
||||||
|
using (var session = SessionFactory.OpenSession())
|
||||||
|
using (var transactionScope = session.BeginTransaction())
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var transaction = session.Get<Transaction>(transactionId);
|
||||||
|
if (transaction != null)
|
||||||
|
{
|
||||||
|
session.Delete(transaction);
|
||||||
|
}
|
||||||
|
transactionScope.Commit();
|
||||||
|
}
|
||||||
|
catch
|
||||||
|
{
|
||||||
|
transactionScope.Rollback();
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
public class ExpenseRepository : IExpenseRepository
|
public class ExpenseRepository : IExpenseRepository
|
||||||
{
|
{
|
||||||
// Retrieve all expenses
|
// Retrieve all expenses
|
||||||
|
Loading…
Reference in New Issue
Block a user