Compare commits

...

2 Commits

8 changed files with 130 additions and 40 deletions

View File

@ -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)
{ {

View File

@ -13,10 +13,15 @@ namespace FirmTracker_Server.nHibernate.Reports
public virtual decimal TotalExpenses { get; set; } public virtual decimal TotalExpenses { get; set; }
public virtual decimal TotalBalance { get; set; } public virtual decimal TotalBalance { get; set; }
public virtual IList<ReportTransaction> ReportTransactions { get; set; } = new List<ReportTransaction>(); /*public virtual IList<ReportTransaction> ReportTransactions { get; set; } = new List<ReportTransaction>();
public virtual IList<ReportExpense> ReportExpenses { get; set; } = new List<ReportExpense>(); public virtual IList<ReportExpense> ReportExpenses { get; set; } = new List<ReportExpense>();
public Report()
{
ReportTransactions = new List<ReportTransaction>();
ReportExpenses = new List<ReportExpense>();
}*/
public class DateRangeDto public class DateRangeDto

View File

@ -13,13 +13,15 @@ namespace FirmTracker_Server.nHibernate.Reports
Map(x => x.TotalExpenses); Map(x => x.TotalExpenses);
Map(x => x.TotalBalance); Map(x => x.TotalBalance);
HasMany(x => x.ReportTransactions) /*HasMany(x => x.ReportTransactions)
.Cascade.All() .KeyColumn("ReportId")
.Inverse(); .Cascade.AllDeleteOrphan()
.Inverse();
HasMany(x => x.ReportExpenses) HasMany(x => x.ReportExpenses)
.Cascade.All() .KeyColumn("ReportId")
.Inverse(); .Cascade.AllDeleteOrphan()
.Inverse();*/
} }

View File

@ -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;
} }

View File

@ -4,22 +4,32 @@ namespace FirmTracker_Server.nHibernate.Reports
{ {
public class ReportExpense public class ReportExpense
{ {
public virtual int Id { get; set; } //public virtual int Id { get; set; }
public virtual int ReportId { get; set; } public virtual Report Report { get; set; }
public virtual int ExpenseId { get; set; } public virtual Expense Expense { get; set; }
public override bool Equals(object obj) public override bool Equals(object obj)
{ {
if (obj == null || GetType() != obj.GetType()) if (obj == null || GetType() != obj.GetType())
{
return false; return false;
}
var other = (ReportExpense)obj; var other = (ReportExpense)obj;
return ReportId == other.ReportId && ExpenseId == other.ExpenseId; return Report != null && Expense != null &&
Report.Id == other.Report.Id &&
Expense.Id == other.Expense.Id;
} }
public override int GetHashCode() public override int GetHashCode()
{ {
return HashCode.Combine(ReportId, ExpenseId); unchecked // Overflow is fine, just wrap
{
int hash = 17;
hash = hash * 23 + (Report?.Id.GetHashCode() ?? 0);
hash = hash * 23 + (Expense?.Id.GetHashCode() ?? 0);
return hash;
}
} }
} }

View File

@ -7,9 +7,9 @@ namespace FirmTracker_Server.nHibernate.Reports
public ReportExpenseMapping() public ReportExpenseMapping()
{ {
Table("ReportExpenses"); Table("ReportExpenses");
Id(x => x.Id); CompositeId()
References(x => x.ReportId, "ReportId"); .KeyReference(x => x.Report, "ReportId")
References(x => x.ExpenseId, "ExpenseId"); .KeyReference(x => x.Expense, "ExpenseId");
} }
} }
} }

View File

@ -4,22 +4,32 @@ namespace FirmTracker_Server.nHibernate.Reports
{ {
public class ReportTransaction public class ReportTransaction
{ {
public virtual int Id { get; set; } //public virtual int Id { get; set; }
public virtual int ReportId { get; set; } public virtual Report Report { get; set; }
public virtual int TransactionId { get; set; } public virtual Transaction Transaction { get; set; }
public override bool Equals(object obj) public override bool Equals(object obj)
{ {
if (obj == null || GetType() != obj.GetType()) if (obj == null || GetType() != obj.GetType())
{
return false; return false;
}
var other = (ReportTransaction)obj; var other = (ReportTransaction)obj;
return ReportId == other.ReportId && TransactionId == other.TransactionId; return Report != null && Transaction != null &&
Report.Id == other.Report.Id &&
Transaction.Id == other.Transaction.Id;
} }
public override int GetHashCode() public override int GetHashCode()
{ {
return HashCode.Combine(ReportId, TransactionId); unchecked // Overflow is fine, just wrap
{
int hash = 17;
hash = hash * 23 + (Report?.Id.GetHashCode() ?? 0);
hash = hash * 23 + (Transaction?.Id.GetHashCode() ?? 0);
return hash;
}
} }
} }

View File

@ -4,12 +4,12 @@ namespace FirmTracker_Server.nHibernate.Reports
{ {
public class ReportTransactionMapping : ClassMap<ReportTransaction> public class ReportTransactionMapping : ClassMap<ReportTransaction>
{ {
public ReportTransactionMapping() public ReportTransactionMapping()
{ {
Table("ReportTransactions"); Table("ReportTransactions");
Id(x => x.Id); CompositeId()
References(x => x.ReportId, "ReportId"); .KeyReference(x => x.Report, "ReportId")
References(x => x.TransactionId, "TransactionId"); .KeyReference(x => x.Transaction, "TransactionId");
} }
} }
} }