dodanie tabel łączących raporty z transakcją/wydatkiem

This commit is contained in:
Kamil Ryżek 2024-06-08 03:39:54 +02:00
parent 5b13f70572
commit 80f5f30a01
8 changed files with 196 additions and 16 deletions

View File

@ -1,5 +1,6 @@
using FirmTracker_Server.nHibernate.Expenses;
using FirmTracker_Server.nHibernate.Transactions;
using Newtonsoft.Json;
namespace FirmTracker_Server.nHibernate.Reports
{
@ -8,16 +9,21 @@ namespace FirmTracker_Server.nHibernate.Reports
public virtual int Id { get; set; }
public virtual DateTime FromDate { get; set; }
public virtual DateTime ToDate { get; set; }
public virtual IList<Transaction> Transactions { get; set;} = new List<Transaction>();
public virtual IList<Expense> Expenses { get; set; } = new List<Expense>();
public virtual decimal TotalIncome { get; set; }
public virtual decimal TotalExpenses { get; set; }
public virtual decimal TotalBalance { get; set; }
public Report() {
Transactions = new List<Transaction>();
Expenses = new List<Expense>();
}
public virtual IList<ReportTransaction> ReportTransactions { get; set; } = new List<ReportTransaction>();
public virtual IList<ReportExpense> ReportExpenses { get; set; } = new List<ReportExpense>();
public class DateRangeDto
{
public DateTime FromDate { get; set; }
public DateTime ToDate { get; set; }
}
}
}

View File

@ -13,17 +13,14 @@ namespace FirmTracker_Server.nHibernate.Reports
Map(x => x.TotalExpenses);
Map(x => x.TotalBalance);
HasManyToMany(x => x.Transactions)
HasMany(x => x.ReportTransactions)
.Cascade.All()
.Table("ReportTransactions")
.ParentKeyColumn("ReportId")
.ChildKeyColumn("TransactionId");
.Inverse();
HasManyToMany(x => x.Expenses)
HasMany(x => x.ReportExpenses)
.Cascade.All()
.Table("ReportExpenses")
.ParentKeyColumn("ReportId")
.ChildKeyColumn("ExpenseId");
.Inverse();
}
}

View File

@ -34,7 +34,9 @@ namespace FirmTracker_Server.nHibernate
.AddFromAssemblyOf<Transactions.Transaction2Mapping>()
.AddFromAssemblyOf<Transactions.TransactionWithProductsMapping>()
.AddFromAssemblyOf<Expenses.ExpenseMapping>()
.AddFromAssemblyOf<Reports.ReportMapping>();
.AddFromAssemblyOf<Reports.ReportMapping>()
.AddFromAssemblyOf<Reports.ReportTransactionMapping>()
.AddFromAssemblyOf<Reports.ReportExpenseMapping>();
})
.ExposeConfiguration(cfg => new SchemaExport(cfg).Create(true, true)) //SchemaUpdate . Execute dla only update
.BuildSessionFactory();

View File

@ -0,0 +1,93 @@
using FirmTracker_Server.nHibernate.Expenses;
using FirmTracker_Server.nHibernate.Transactions;
using NHibernate;
using NHibernate.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Transactions;
namespace FirmTracker_Server.nHibernate.Reports
{
public class ReportCRUD
{
public Report AddReport(DateTime fromDate, DateTime toDate)
{
using (var session = SessionFactory.OpenSession())
using (var sessionTransaction = session.BeginTransaction())
{
try
{
var transactions = session.Query<nHibernate.Transactions.Transaction>()
.Where(t => t.Date >= fromDate && t.Date <= toDate)
.ToList();
var expenses = session.Query<Expense>()
.Where(e => e.Date >= fromDate && e.Date <= toDate)
.ToList();
var totalIncome = transactions.Sum(t => t.TotalPrice);
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
};
foreach (var transactionItem in transactions)
{
var reportTransaction = new ReportTransaction
{
ReportId = report.Id,
TransactionId = transactionItem.Id
};
report.ReportTransactions.Add(reportTransaction);
}
foreach (var expenseItem in expenses)
{
var reportExpense = new ReportExpense
{
ReportId = report.Id,
ExpenseId = expenseItem.Id
};
report.ReportExpenses.Add(reportExpense);
}
session.Save(report);
sessionTransaction.Commit();
return report;
}
catch
{
sessionTransaction.Rollback();
throw;
}
}
}
public Report GetReport(int reportId)
{
using (var session = SessionFactory.OpenSession())
{
return session.Get<Report>(reportId);
}
}
public IList<Report> GetAllReports()
{
using (var session = SessionFactory.OpenSession())
{
var reports = session.Query<Report>()
.ToList();
return reports;
}
}
}
}

View File

@ -0,0 +1,26 @@
using FirmTracker_Server.nHibernate.Expenses;
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 override bool Equals(object obj)
{
if (obj == null || GetType() != obj.GetType())
return false;
var other = (ReportExpense)obj;
return ReportId == other.ReportId && ExpenseId == other.ExpenseId;
}
public override int GetHashCode()
{
return HashCode.Combine(ReportId, ExpenseId);
}
}
}

View File

@ -0,0 +1,15 @@
using FluentNHibernate.Mapping;
namespace FirmTracker_Server.nHibernate.Reports
{
public class ReportExpenseMapping : ClassMap<ReportExpense>
{
public ReportExpenseMapping()
{
Table("ReportExpenses");
Id(x => x.Id);
References(x => x.ReportId, "ReportId");
References(x => x.ExpenseId, "ExpenseId");
}
}
}

View File

@ -0,0 +1,26 @@
using FirmTracker_Server.nHibernate.Transactions;
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 override bool Equals(object obj)
{
if (obj == null || GetType() != obj.GetType())
return false;
var other = (ReportTransaction)obj;
return ReportId == other.ReportId && TransactionId == other.TransactionId;
}
public override int GetHashCode()
{
return HashCode.Combine(ReportId, TransactionId);
}
}
}

View File

@ -0,0 +1,15 @@
using FluentNHibernate.Mapping;
namespace FirmTracker_Server.nHibernate.Reports
{
public class ReportTransactionMapping : ClassMap<ReportTransaction>
{
public ReportTransactionMapping()
{
Table("ReportTransactions");
Id(x => x.Id);
References(x => x.ReportId, "ReportId");
References(x => x.TransactionId, "TransactionId");
}
}
}