PI2024-23 #2
@ -3,6 +3,10 @@ using Microsoft.AspNetCore.Mvc;
|
|||||||
using System.Text.Json.Serialization;
|
using System.Text.Json.Serialization;
|
||||||
using System.Text.Json;
|
using System.Text.Json;
|
||||||
using NuGet.Protocol;
|
using NuGet.Protocol;
|
||||||
|
using FirmTracker_Server.nHibernate.Expenses;
|
||||||
|
using FirmTracker_Server.nHibernate.Products;
|
||||||
|
using FirmTracker_Server.nHibernate;
|
||||||
|
using NHibernate.Linq;
|
||||||
|
|
||||||
namespace FirmTracker_Server.Controllers
|
namespace FirmTracker_Server.Controllers
|
||||||
{
|
{
|
||||||
@ -11,10 +15,12 @@ namespace FirmTracker_Server.Controllers
|
|||||||
public class ReportController : ControllerBase
|
public class ReportController : ControllerBase
|
||||||
{
|
{
|
||||||
private readonly ReportCRUD _reportCRUD;
|
private readonly ReportCRUD _reportCRUD;
|
||||||
|
private readonly ProductCRUD _productCRUD;
|
||||||
|
|
||||||
public ReportController()
|
public ReportController()
|
||||||
{
|
{
|
||||||
_reportCRUD = new ReportCRUD();
|
_reportCRUD = new ReportCRUD();
|
||||||
|
_productCRUD = new ProductCRUD();
|
||||||
}
|
}
|
||||||
[HttpPost]
|
[HttpPost]
|
||||||
[ProducesResponseType(201)] //Created
|
[ProducesResponseType(201)] //Created
|
||||||
@ -23,14 +29,62 @@ namespace FirmTracker_Server.Controllers
|
|||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
var fromDate = dateRange.FromDate;
|
||||||
if (dateRange == null || dateRange.FromDate >= dateRange.ToDate)
|
var toDate = dateRange.ToDate;
|
||||||
{
|
using (var session = SessionFactory.OpenSession())
|
||||||
return BadRequest("Invalid date range.");
|
{
|
||||||
}
|
var transactions = session.Query<nHibernate.Transactions.Transaction>()
|
||||||
|
.Where(t => t.Date >= fromDate && t.Date <= toDate)
|
||||||
|
.FetchMany(t => t.TransactionProducts)
|
||||||
|
.ToList();
|
||||||
|
|
||||||
var report = _reportCRUD.AddReport(dateRange.FromDate, dateRange.ToDate);
|
var expenses = session.Query<Expense>()
|
||||||
return CreatedAtAction(nameof(GetReport), new { id = report.Id }, report); // to change?
|
.Where(e => e.Date >= fromDate && e.Date <= toDate)
|
||||||
|
.ToList();
|
||||||
|
|
||||||
|
// Calculate total income from transactions
|
||||||
|
decimal totalIncome = 0;
|
||||||
|
foreach (var transaction in transactions)
|
||||||
|
{
|
||||||
|
foreach (var product in transaction.TransactionProducts)
|
||||||
|
{
|
||||||
|
decimal price = _productCRUD.GetProductPrice(product.ProductID);
|
||||||
|
int type = _productCRUD.GetProductType(product.ProductID);
|
||||||
|
if (type == 1)
|
||||||
|
{
|
||||||
|
totalIncome += ((product.Quantity * price) * ((1 - (transaction.Discount / 100))));
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
totalIncome += (price * ((1 - (transaction.Discount / 100))));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var totalExpenses = expenses.Sum(e => e.Value);
|
||||||
|
var totalBalance = totalIncome - totalExpenses;
|
||||||
|
|
||||||
|
var report = new Report
|
||||||
|
{
|
||||||
|
FromDate = fromDate,
|
||||||
|
ToDate = toDate,
|
||||||
|
TotalIncome = totalIncome,
|
||||||
|
TotalExpenses = totalExpenses,
|
||||||
|
TotalBalance = totalBalance
|
||||||
|
};
|
||||||
|
|
||||||
|
_reportCRUD.AddReport(report, transactions, expenses);
|
||||||
|
|
||||||
|
return CreatedAtAction(nameof(GetReport), new { id = report.Id }, report);
|
||||||
|
|
||||||
|
/*if (dateRange == null || dateRange.FromDate >= dateRange.ToDate)
|
||||||
|
{
|
||||||
|
return BadRequest("Invalid date range.");
|
||||||
|
}
|
||||||
|
|
||||||
|
var report = _reportCRUD.AddReport(dateRange.FromDate, dateRange.ToDate);
|
||||||
|
return CreatedAtAction(nameof(GetReport), new { id = report.Id }, report); // to change?*/
|
||||||
|
}
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
|
@ -10,14 +10,14 @@ namespace FirmTracker_Server.nHibernate.Reports
|
|||||||
{
|
{
|
||||||
public class ReportCRUD
|
public class ReportCRUD
|
||||||
{
|
{
|
||||||
public Report AddReport(DateTime fromDate, DateTime toDate)
|
public Report AddReport(Report report, IList<nHibernate.Transactions.Transaction> transactions, IList<Expense> expenses /*DateTime fromDate, DateTime toDate*/)
|
||||||
{
|
{
|
||||||
using (var session = SessionFactory.OpenSession())
|
using (var session = SessionFactory.OpenSession())
|
||||||
using (var sessionTransaction = session.BeginTransaction())
|
using (var sessionTransaction = session.BeginTransaction())
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
var transactions = session.Query<nHibernate.Transactions.Transaction>()
|
/*var transactions = session.Query<nHibernate.Transactions.Transaction>()
|
||||||
.Where(t => t.Date >= fromDate && t.Date <= toDate)
|
.Where(t => t.Date >= fromDate && t.Date <= toDate)
|
||||||
.ToList();
|
.ToList();
|
||||||
|
|
||||||
@ -37,29 +37,38 @@ namespace FirmTracker_Server.nHibernate.Reports
|
|||||||
TotalExpenses = totalExpenses,
|
TotalExpenses = totalExpenses,
|
||||||
TotalBalance = totalBalance
|
TotalBalance = totalBalance
|
||||||
|
|
||||||
};
|
};*/
|
||||||
|
|
||||||
|
session.Save(report);
|
||||||
|
|
||||||
foreach (var transactionItem in transactions)
|
foreach (var transactionItem in transactions)
|
||||||
{
|
{
|
||||||
|
//var trans = session.Load<nHibernate.Transactions.Transaction>(transactionItem);
|
||||||
var reportTransaction = new ReportTransaction
|
var reportTransaction = new ReportTransaction
|
||||||
{
|
{
|
||||||
ReportId = report.Id,
|
Report = report,
|
||||||
TransactionId = transactionItem.Id
|
Transaction = transactionItem
|
||||||
};
|
};
|
||||||
report.ReportTransactions.Add(reportTransaction);
|
session.Save(reportTransaction);
|
||||||
|
//report.ReportTransactions.Add(reportTransaction);
|
||||||
|
//report.TotalIncome += trans.TotalPrice;
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach (var expenseItem in expenses)
|
foreach (var expenseItem in expenses)
|
||||||
{
|
{
|
||||||
|
//var expense = session.Load<Expense>(expenseItem);
|
||||||
var reportExpense = new ReportExpense
|
var reportExpense = new ReportExpense
|
||||||
{
|
{
|
||||||
ReportId = report.Id,
|
Report = report,
|
||||||
ExpenseId = expenseItem.Id
|
Expense = expenseItem
|
||||||
};
|
};
|
||||||
report.ReportExpenses.Add(reportExpense);
|
session.Save(reportExpense);
|
||||||
|
//report.TotalExpenses += expense.Value;
|
||||||
|
//report.ReportExpenses.Add(reportExpense);
|
||||||
}
|
}
|
||||||
|
|
||||||
session.Save(report);
|
|
||||||
|
//session.Save(report);
|
||||||
sessionTransaction.Commit();
|
sessionTransaction.Commit();
|
||||||
return report;
|
return report;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user