PI2024-23 #2
@ -30,7 +30,7 @@ namespace FirmTracker_Server.Controllers
|
|||||||
// GET: api/Expenses
|
// GET: api/Expenses
|
||||||
[HttpGet("{id}")]
|
[HttpGet("{id}")]
|
||||||
[ProducesResponseType(200)] // Created
|
[ProducesResponseType(200)] // Created
|
||||||
[ProducesResponseType(400)] // Bad Request
|
[ProducesResponseType(404)] // Bad Request
|
||||||
public IActionResult GetExpense(int id)
|
public IActionResult GetExpense(int id)
|
||||||
{
|
{
|
||||||
var expense = _expenseCrud.GetExpense(id);
|
var expense = _expenseCrud.GetExpense(id);
|
||||||
@ -43,8 +43,8 @@ namespace FirmTracker_Server.Controllers
|
|||||||
|
|
||||||
//PUT: api/Expenses
|
//PUT: api/Expenses
|
||||||
[HttpPut("{id}")]
|
[HttpPut("{id}")]
|
||||||
[ProducesResponseType(200)]
|
[ProducesResponseType(204)]
|
||||||
[ProducesResponseType(200)]
|
[ProducesResponseType(400)]
|
||||||
public IActionResult UpdateExpense(int id, [FromBody] Expense expense)
|
public IActionResult UpdateExpense(int id, [FromBody] Expense expense)
|
||||||
{
|
{
|
||||||
if (id != expense.Id)
|
if (id != expense.Id)
|
||||||
@ -63,8 +63,8 @@ namespace FirmTracker_Server.Controllers
|
|||||||
}
|
}
|
||||||
|
|
||||||
[HttpDelete("{id}")]
|
[HttpDelete("{id}")]
|
||||||
[ProducesResponseType(200)]
|
[ProducesResponseType(204)]
|
||||||
[ProducesResponseType(400)]
|
[ProducesResponseType(404)]
|
||||||
public IActionResult DeleteExpense(int id)
|
public IActionResult DeleteExpense(int id)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
|
@ -8,6 +8,7 @@ using FirmTracker_Server.nHibernate.Products;
|
|||||||
using FirmTracker_Server.nHibernate;
|
using FirmTracker_Server.nHibernate;
|
||||||
using NHibernate.Linq;
|
using NHibernate.Linq;
|
||||||
|
|
||||||
|
|
||||||
namespace FirmTracker_Server.Controllers
|
namespace FirmTracker_Server.Controllers
|
||||||
{
|
{
|
||||||
[Route("api/[controller]")]
|
[Route("api/[controller]")]
|
||||||
@ -31,6 +32,10 @@ namespace FirmTracker_Server.Controllers
|
|||||||
{
|
{
|
||||||
var fromDate = dateRange.FromDate;
|
var fromDate = dateRange.FromDate;
|
||||||
var toDate = dateRange.ToDate;
|
var toDate = dateRange.ToDate;
|
||||||
|
if (fromDate >= toDate)
|
||||||
|
{
|
||||||
|
return BadRequest();
|
||||||
|
}
|
||||||
using (var session = SessionFactory.OpenSession())
|
using (var session = SessionFactory.OpenSession())
|
||||||
{
|
{
|
||||||
var transactions = session.Query<nHibernate.Transactions.Transaction>()
|
var transactions = session.Query<nHibernate.Transactions.Transaction>()
|
||||||
@ -125,5 +130,90 @@ namespace FirmTracker_Server.Controllers
|
|||||||
|
|
||||||
return Ok(reports);
|
return Ok(reports);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[HttpPut("{id}")]
|
||||||
|
[ProducesResponseType(204)]
|
||||||
|
[ProducesResponseType(400)]
|
||||||
|
[ProducesResponseType(404)]
|
||||||
|
public IActionResult UpdateReport(int reportId, [FromBody] Report.DateRangeDto dateRange)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var report = _reportCRUD.GetReport(reportId);
|
||||||
|
if (report == null)
|
||||||
|
{
|
||||||
|
return NotFound();
|
||||||
|
}
|
||||||
|
using (var session = SessionFactory.OpenSession())
|
||||||
|
{
|
||||||
|
var fromDate = dateRange.FromDate;
|
||||||
|
var toDate = dateRange.ToDate;
|
||||||
|
if (fromDate >= toDate)
|
||||||
|
{
|
||||||
|
return BadRequest();
|
||||||
|
}
|
||||||
|
var transactions = session.Query<nHibernate.Transactions.Transaction>()
|
||||||
|
.Where(t => t.Date >= fromDate && t.Date <= toDate)
|
||||||
|
.FetchMany(t => t.TransactionProducts)
|
||||||
|
.ToList();
|
||||||
|
|
||||||
|
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;
|
||||||
|
|
||||||
|
report.FromDate = fromDate;
|
||||||
|
report.ToDate = toDate;
|
||||||
|
report.TotalIncome = totalIncome;
|
||||||
|
report.TotalExpenses = totalExpenses;
|
||||||
|
report.TotalBalance = totalBalance;
|
||||||
|
|
||||||
|
_reportCRUD.UpdateReport(report, transactions, expenses);
|
||||||
|
|
||||||
|
return NoContent();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
return BadRequest(ex.Message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpDelete]
|
||||||
|
[ProducesResponseType(204)]
|
||||||
|
[ProducesResponseType(404)]
|
||||||
|
public IActionResult DeleteReport(int reportId)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
_reportCRUD.DeleteReport(reportId);
|
||||||
|
return NoContent();
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
return NotFound(ex.Message);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -98,5 +98,84 @@ namespace FirmTracker_Server.nHibernate.Reports
|
|||||||
return reports;
|
return reports;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Report UpdateReport(Report report, IList<nHibernate.Transactions.Transaction> transactions, IList<Expense> expenses)
|
||||||
|
{
|
||||||
|
using (var session = SessionFactory.OpenSession())
|
||||||
|
using (var transaction = session.BeginTransaction())
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
session.Update(report);
|
||||||
|
|
||||||
|
|
||||||
|
session.CreateQuery("delete from ReportTransaction where Report.Id = :reportId")
|
||||||
|
.SetParameter("reportId", report.Id)
|
||||||
|
.ExecuteUpdate();
|
||||||
|
session.CreateQuery("delete from ReportExpense where Report.Id = :reportId")
|
||||||
|
.SetParameter("reportId", report.Id)
|
||||||
|
.ExecuteUpdate();
|
||||||
|
|
||||||
|
foreach (var transactionItem in transactions)
|
||||||
|
{
|
||||||
|
var reportTransaction = new ReportTransaction
|
||||||
|
{
|
||||||
|
Report = report,
|
||||||
|
Transaction = transactionItem
|
||||||
|
};
|
||||||
|
session.Save(reportTransaction);
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach (var expenseItem in expenses)
|
||||||
|
{
|
||||||
|
var reportExpense = new ReportExpense
|
||||||
|
{
|
||||||
|
Report = report,
|
||||||
|
Expense = expenseItem
|
||||||
|
};
|
||||||
|
session.Save(reportExpense);
|
||||||
|
}
|
||||||
|
|
||||||
|
transaction.Commit();
|
||||||
|
return report;
|
||||||
|
}
|
||||||
|
catch
|
||||||
|
{
|
||||||
|
transaction.Rollback();
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void DeleteReport(int reportId)
|
||||||
|
{
|
||||||
|
using (var session = SessionFactory.OpenSession())
|
||||||
|
using (var transaction = session.BeginTransaction())
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
session.CreateQuery("delete from ReportTransaction where Report.Id = :reportId")
|
||||||
|
.SetParameter("reportId", reportId)
|
||||||
|
.ExecuteUpdate();
|
||||||
|
session.CreateQuery("delete from ReportExpense where Report.Id = :reportId")
|
||||||
|
.SetParameter("reportId", reportId)
|
||||||
|
.ExecuteUpdate();
|
||||||
|
|
||||||
|
var report = session.Get<Report>(reportId);
|
||||||
|
if (report != null)
|
||||||
|
{
|
||||||
|
session.Delete(report);
|
||||||
|
}
|
||||||
|
|
||||||
|
transaction.Commit();
|
||||||
|
}
|
||||||
|
catch
|
||||||
|
{
|
||||||
|
transaction.Rollback();
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user