dodanie tabel łączących raporty z transakcją/wydatkiem
This commit is contained in:
parent
5b13f70572
commit
80f5f30a01
@ -1,5 +1,6 @@
|
|||||||
using FirmTracker_Server.nHibernate.Expenses;
|
using FirmTracker_Server.nHibernate.Expenses;
|
||||||
using FirmTracker_Server.nHibernate.Transactions;
|
using FirmTracker_Server.nHibernate.Transactions;
|
||||||
|
using Newtonsoft.Json;
|
||||||
|
|
||||||
namespace FirmTracker_Server.nHibernate.Reports
|
namespace FirmTracker_Server.nHibernate.Reports
|
||||||
{
|
{
|
||||||
@ -8,16 +9,21 @@ namespace FirmTracker_Server.nHibernate.Reports
|
|||||||
public virtual int Id { get; set; }
|
public virtual int Id { get; set; }
|
||||||
public virtual DateTime FromDate { get; set; }
|
public virtual DateTime FromDate { get; set; }
|
||||||
public virtual DateTime ToDate { 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 TotalIncome { get; set; }
|
||||||
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 Report() {
|
|
||||||
Transactions = new List<Transaction>();
|
public virtual IList<ReportTransaction> ReportTransactions { get; set; } = new List<ReportTransaction>();
|
||||||
Expenses = new List<Expense>();
|
public virtual IList<ReportExpense> ReportExpenses { get; set; } = new List<ReportExpense>();
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
public class DateRangeDto
|
||||||
|
{
|
||||||
|
public DateTime FromDate { get; set; }
|
||||||
|
public DateTime ToDate { get; set; }
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -13,17 +13,14 @@ namespace FirmTracker_Server.nHibernate.Reports
|
|||||||
Map(x => x.TotalExpenses);
|
Map(x => x.TotalExpenses);
|
||||||
Map(x => x.TotalBalance);
|
Map(x => x.TotalBalance);
|
||||||
|
|
||||||
HasManyToMany(x => x.Transactions)
|
HasMany(x => x.ReportTransactions)
|
||||||
.Cascade.All()
|
.Cascade.All()
|
||||||
.Table("ReportTransactions")
|
.Inverse();
|
||||||
.ParentKeyColumn("ReportId")
|
|
||||||
.ChildKeyColumn("TransactionId");
|
|
||||||
|
|
||||||
HasManyToMany(x => x.Expenses)
|
HasMany(x => x.ReportExpenses)
|
||||||
.Cascade.All()
|
.Cascade.All()
|
||||||
.Table("ReportExpenses")
|
.Inverse();
|
||||||
.ParentKeyColumn("ReportId")
|
|
||||||
.ChildKeyColumn("ExpenseId");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -34,7 +34,9 @@ namespace FirmTracker_Server.nHibernate
|
|||||||
.AddFromAssemblyOf<Transactions.Transaction2Mapping>()
|
.AddFromAssemblyOf<Transactions.Transaction2Mapping>()
|
||||||
.AddFromAssemblyOf<Transactions.TransactionWithProductsMapping>()
|
.AddFromAssemblyOf<Transactions.TransactionWithProductsMapping>()
|
||||||
.AddFromAssemblyOf<Expenses.ExpenseMapping>()
|
.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
|
.ExposeConfiguration(cfg => new SchemaExport(cfg).Create(true, true)) //SchemaUpdate . Execute dla only update
|
||||||
.BuildSessionFactory();
|
.BuildSessionFactory();
|
||||||
|
93
nHibernate/Reports/ReportCRUD.cs
Normal file
93
nHibernate/Reports/ReportCRUD.cs
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
26
nHibernate/Reports/ReportExpense.cs
Normal file
26
nHibernate/Reports/ReportExpense.cs
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
15
nHibernate/Reports/ReportExpenseMapping.cs
Normal file
15
nHibernate/Reports/ReportExpenseMapping.cs
Normal 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");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
26
nHibernate/Reports/ReportTransaction.cs
Normal file
26
nHibernate/Reports/ReportTransaction.cs
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
15
nHibernate/Reports/ReportTransactionMapping.cs
Normal file
15
nHibernate/Reports/ReportTransactionMapping.cs
Normal 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");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user