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;
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
{
if (dateRange == null || dateRange.FromDate >= dateRange.ToDate)
{
return BadRequest("Invalid date range.");
}
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();
var report = _reportCRUD.AddReport(dateRange.FromDate, dateRange.ToDate);
return CreatedAtAction(nameof(GetReport), new { id = report.Id }, report); // to change?
var expenses = session.Query<Expense>()
.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)
{

View File

@ -13,10 +13,15 @@ namespace FirmTracker_Server.nHibernate.Reports
public virtual decimal TotalExpenses { 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 Report()
{
ReportTransactions = new List<ReportTransaction>();
ReportExpenses = new List<ReportExpense>();
}*/
public class DateRangeDto

View File

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

View File

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

View File

@ -4,22 +4,32 @@ namespace FirmTracker_Server.nHibernate.Reports
{
public class ReportExpense
{
public virtual int Id { get; set; }
public virtual int ReportId { get; set; }
public virtual int ExpenseId { get; set; }
//public virtual int Id { get; set; }
public virtual Report Report { get; set; }
public virtual Expense Expense { get; set; }
public override bool Equals(object obj)
{
if (obj == null || GetType() != obj.GetType())
{
return false;
}
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()
{
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()
{
Table("ReportExpenses");
Id(x => x.Id);
References(x => x.ReportId, "ReportId");
References(x => x.ExpenseId, "ExpenseId");
CompositeId()
.KeyReference(x => x.Report, "ReportId")
.KeyReference(x => x.Expense, "ExpenseId");
}
}
}

View File

@ -4,22 +4,32 @@ namespace FirmTracker_Server.nHibernate.Reports
{
public class ReportTransaction
{
public virtual int Id { get; set; }
public virtual int ReportId { get; set; }
public virtual int TransactionId { get; set; }
//public virtual int Id { get; set; }
public virtual Report Report { get; set; }
public virtual Transaction Transaction { get; set; }
public override bool Equals(object obj)
{
if (obj == null || GetType() != obj.GetType())
{
return false;
}
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()
{
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 ReportTransactionMapping()
public ReportTransactionMapping()
{
Table("ReportTransactions");
Id(x => x.Id);
References(x => x.ReportId, "ReportId");
References(x => x.TransactionId, "TransactionId");
CompositeId()
.KeyReference(x => x.Report, "ReportId")
.KeyReference(x => x.Transaction, "TransactionId");
}
}
}