przeniesienie obliczeń z CRUDa do controllera
This commit is contained in:
parent
cdc924243b
commit
ffbcb447a6
@ -3,6 +3,10 @@ using Microsoft.AspNetCore.Mvc;
|
||||
using System.Text.Json.Serialization;
|
||||
using System.Text.Json;
|
||||
using NuGet.Protocol;
|
||||
using FirmTracker_Server.nHibernate.Expenses;
|
||||
using FirmTracker_Server.nHibernate.Products;
|
||||
using FirmTracker_Server.nHibernate;
|
||||
using NHibernate.Linq;
|
||||
|
||||
namespace FirmTracker_Server.Controllers
|
||||
{
|
||||
@ -11,10 +15,12 @@ namespace FirmTracker_Server.Controllers
|
||||
public class ReportController : ControllerBase
|
||||
{
|
||||
private readonly ReportCRUD _reportCRUD;
|
||||
private readonly ProductCRUD _productCRUD;
|
||||
|
||||
public ReportController()
|
||||
{
|
||||
_reportCRUD = new ReportCRUD();
|
||||
_productCRUD = new ProductCRUD();
|
||||
}
|
||||
[HttpPost]
|
||||
[ProducesResponseType(201)] //Created
|
||||
@ -23,14 +29,62 @@ namespace FirmTracker_Server.Controllers
|
||||
{
|
||||
try
|
||||
{
|
||||
var fromDate = dateRange.FromDate;
|
||||
var toDate = dateRange.ToDate;
|
||||
using (var session = SessionFactory.OpenSession())
|
||||
{
|
||||
var transactions = session.Query<nHibernate.Transactions.Transaction>()
|
||||
.Where(t => t.Date >= fromDate && t.Date <= toDate)
|
||||
.FetchMany(t => t.TransactionProducts)
|
||||
.ToList();
|
||||
|
||||
if (dateRange == null || dateRange.FromDate >= dateRange.ToDate)
|
||||
{
|
||||
return BadRequest("Invalid date range.");
|
||||
}
|
||||
var expenses = session.Query<Expense>()
|
||||
.Where(e => e.Date >= fromDate && e.Date <= toDate)
|
||||
.ToList();
|
||||
|
||||
var report = _reportCRUD.AddReport(dateRange.FromDate, dateRange.ToDate);
|
||||
return CreatedAtAction(nameof(GetReport), new { id = report.Id }, report); // to change?
|
||||
// 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)
|
||||
{
|
||||
|
@ -10,14 +10,14 @@ namespace FirmTracker_Server.nHibernate.Reports
|
||||
{
|
||||
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 sessionTransaction = session.BeginTransaction())
|
||||
{
|
||||
try
|
||||
{
|
||||
var transactions = session.Query<nHibernate.Transactions.Transaction>()
|
||||
/*var transactions = session.Query<nHibernate.Transactions.Transaction>()
|
||||
.Where(t => t.Date >= fromDate && t.Date <= toDate)
|
||||
.ToList();
|
||||
|
||||
@ -37,29 +37,38 @@ namespace FirmTracker_Server.nHibernate.Reports
|
||||
TotalExpenses = totalExpenses,
|
||||
TotalBalance = totalBalance
|
||||
|
||||
};
|
||||
};*/
|
||||
|
||||
session.Save(report);
|
||||
|
||||
foreach (var transactionItem in transactions)
|
||||
{
|
||||
//var trans = session.Load<nHibernate.Transactions.Transaction>(transactionItem);
|
||||
var reportTransaction = new ReportTransaction
|
||||
{
|
||||
ReportId = report.Id,
|
||||
TransactionId = transactionItem.Id
|
||||
Report = report,
|
||||
Transaction = transactionItem
|
||||
};
|
||||
report.ReportTransactions.Add(reportTransaction);
|
||||
session.Save(reportTransaction);
|
||||
//report.ReportTransactions.Add(reportTransaction);
|
||||
//report.TotalIncome += trans.TotalPrice;
|
||||
}
|
||||
|
||||
foreach (var expenseItem in expenses)
|
||||
{
|
||||
//var expense = session.Load<Expense>(expenseItem);
|
||||
var reportExpense = new ReportExpense
|
||||
{
|
||||
ReportId = report.Id,
|
||||
ExpenseId = expenseItem.Id
|
||||
Report = report,
|
||||
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();
|
||||
return report;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user