2024-06-11 20:12:24 +02:00
|
|
|
|
/*
|
|
|
|
|
* This file is part of FirmTracker - Server.
|
|
|
|
|
*
|
|
|
|
|
* FirmTracker - Server is free software: you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* FirmTracker - Server is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
|
* along with FirmTracker - Server. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
using FirmTracker_Server.nHibernate.Reports;
|
2024-06-08 03:40:13 +02:00
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
using System.Text.Json.Serialization;
|
|
|
|
|
using System.Text.Json;
|
|
|
|
|
using NuGet.Protocol;
|
2024-06-08 15:06:29 +02:00
|
|
|
|
using FirmTracker_Server.nHibernate.Expenses;
|
|
|
|
|
using FirmTracker_Server.nHibernate.Products;
|
|
|
|
|
using FirmTracker_Server.nHibernate;
|
|
|
|
|
using NHibernate.Linq;
|
2024-06-08 03:40:13 +02:00
|
|
|
|
|
2024-06-09 23:35:15 +02:00
|
|
|
|
|
2024-06-08 03:40:13 +02:00
|
|
|
|
namespace FirmTracker_Server.Controllers
|
|
|
|
|
{
|
|
|
|
|
[Route("api/[controller]")]
|
|
|
|
|
[ApiController]
|
|
|
|
|
public class ReportController : ControllerBase
|
|
|
|
|
{
|
|
|
|
|
private readonly ReportCRUD _reportCRUD;
|
2024-06-08 15:06:29 +02:00
|
|
|
|
private readonly ProductCRUD _productCRUD;
|
2024-06-08 03:40:13 +02:00
|
|
|
|
|
|
|
|
|
public ReportController()
|
|
|
|
|
{
|
|
|
|
|
_reportCRUD = new ReportCRUD();
|
2024-06-08 15:06:29 +02:00
|
|
|
|
_productCRUD = new ProductCRUD();
|
2024-06-08 03:40:13 +02:00
|
|
|
|
}
|
|
|
|
|
[HttpPost]
|
|
|
|
|
[ProducesResponseType(201)] //Created
|
|
|
|
|
[ProducesResponseType(400)] //Bad request
|
|
|
|
|
public IActionResult CreateReport([FromBody] Report.DateRangeDto dateRange)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2024-06-08 15:06:29 +02:00
|
|
|
|
var fromDate = dateRange.FromDate;
|
|
|
|
|
var toDate = dateRange.ToDate;
|
2024-06-09 23:35:15 +02:00
|
|
|
|
if (fromDate >= toDate)
|
|
|
|
|
{
|
2024-06-14 13:46:20 +02:00
|
|
|
|
return BadRequest("Nieprawidłowy zakres dat.");
|
2024-06-09 23:35:15 +02:00
|
|
|
|
}
|
2024-06-08 15:06:29 +02:00
|
|
|
|
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 expenses = session.Query<Expense>()
|
|
|
|
|
.Where(e => e.Date >= fromDate && e.Date <= toDate)
|
|
|
|
|
.ToList();
|
|
|
|
|
|
2024-06-17 22:17:59 +02:00
|
|
|
|
|
2024-06-08 15:06:29 +02:00
|
|
|
|
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.");
|
|
|
|
|
}
|
2024-06-08 03:40:13 +02:00
|
|
|
|
|
2024-06-08 15:06:29 +02:00
|
|
|
|
var report = _reportCRUD.AddReport(dateRange.FromDate, dateRange.ToDate);
|
|
|
|
|
return CreatedAtAction(nameof(GetReport), new { id = report.Id }, report); // to change?*/
|
|
|
|
|
}
|
2024-06-08 03:40:13 +02:00
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
return BadRequest(ex.Message);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpGet("{id}")]
|
|
|
|
|
[ProducesResponseType(200)]
|
|
|
|
|
[ProducesResponseType(404)]
|
|
|
|
|
public IActionResult GetReport(int id)
|
|
|
|
|
{
|
|
|
|
|
var report = _reportCRUD.GetReport(id);
|
|
|
|
|
if (report == null)
|
|
|
|
|
return NotFound();
|
|
|
|
|
|
|
|
|
|
var options = new JsonSerializerOptions
|
|
|
|
|
{
|
2024-06-17 22:17:59 +02:00
|
|
|
|
ReferenceHandler = ReferenceHandler.Preserve
|
2024-06-08 03:40:13 +02:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var json = JsonSerializer.Serialize(report, options);
|
|
|
|
|
return Ok(json);
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-10 00:09:40 +02:00
|
|
|
|
[HttpGet("{id}/transactions")]
|
|
|
|
|
[ProducesResponseType(200)]
|
|
|
|
|
[ProducesResponseType(404)]
|
2024-06-10 02:40:40 +02:00
|
|
|
|
public IActionResult GetReportTransactions(int id)
|
2024-06-10 00:09:40 +02:00
|
|
|
|
{
|
2024-06-10 02:40:40 +02:00
|
|
|
|
var transactions = _reportCRUD.GetReportTransactions(id);
|
2024-06-10 00:09:40 +02:00
|
|
|
|
if (transactions == null)
|
|
|
|
|
{
|
|
|
|
|
return NotFound();
|
|
|
|
|
}
|
|
|
|
|
return Ok(transactions);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpGet("{id}/expenses")]
|
|
|
|
|
[ProducesResponseType(200)]
|
|
|
|
|
[ProducesResponseType(404)]
|
2024-06-10 02:40:40 +02:00
|
|
|
|
public IActionResult GetReportExpenses(int id)
|
2024-06-10 00:09:40 +02:00
|
|
|
|
{
|
2024-06-10 02:40:40 +02:00
|
|
|
|
var expenses = _reportCRUD.GetReportExpenses(id);
|
2024-06-10 00:09:40 +02:00
|
|
|
|
if (expenses == null)
|
|
|
|
|
{
|
|
|
|
|
return NotFound();
|
|
|
|
|
}
|
|
|
|
|
return Ok(expenses);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2024-06-08 03:40:13 +02:00
|
|
|
|
[HttpGet]
|
|
|
|
|
[ProducesResponseType(200)]
|
|
|
|
|
[ProducesResponseType(404)]
|
|
|
|
|
public IActionResult GetAllReports()
|
|
|
|
|
{
|
|
|
|
|
var reports = _reportCRUD.GetAllReports();
|
|
|
|
|
if (reports == null)
|
|
|
|
|
return NotFound();
|
|
|
|
|
|
|
|
|
|
return Ok(reports);
|
|
|
|
|
}
|
2024-06-09 23:35:15 +02:00
|
|
|
|
|
|
|
|
|
[HttpPut("{id}")]
|
|
|
|
|
[ProducesResponseType(204)]
|
|
|
|
|
[ProducesResponseType(400)]
|
|
|
|
|
[ProducesResponseType(404)]
|
2024-06-10 02:40:40 +02:00
|
|
|
|
public IActionResult UpdateReport(int id, [FromBody] Report.DateRangeDto dateRange)
|
2024-06-09 23:35:15 +02:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2024-06-10 02:40:40 +02:00
|
|
|
|
var report = _reportCRUD.GetReport(id);
|
2024-06-09 23:35:15 +02:00
|
|
|
|
if (report == null)
|
|
|
|
|
{
|
|
|
|
|
return NotFound();
|
|
|
|
|
}
|
|
|
|
|
using (var session = SessionFactory.OpenSession())
|
|
|
|
|
{
|
|
|
|
|
var fromDate = dateRange.FromDate;
|
|
|
|
|
var toDate = dateRange.ToDate;
|
|
|
|
|
if (fromDate >= toDate)
|
|
|
|
|
{
|
2024-06-14 13:46:20 +02:00
|
|
|
|
return BadRequest("Nieprawidłowy zakres dat.");
|
2024-06-09 23:35:15 +02:00
|
|
|
|
}
|
|
|
|
|
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();
|
|
|
|
|
|
2024-06-17 22:17:59 +02:00
|
|
|
|
|
2024-06-09 23:35:15 +02:00
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-17 22:17:59 +02:00
|
|
|
|
[HttpDelete("{id}")]
|
2024-06-09 23:35:15 +02:00
|
|
|
|
[ProducesResponseType(204)]
|
|
|
|
|
[ProducesResponseType(404)]
|
2024-06-10 02:40:40 +02:00
|
|
|
|
public IActionResult DeleteReport(int id)
|
2024-06-09 23:35:15 +02:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2024-06-10 02:40:40 +02:00
|
|
|
|
_reportCRUD.DeleteReport(id);
|
2024-06-09 23:35:15 +02:00
|
|
|
|
return NoContent();
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
return NotFound(ex.Message);
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-06-08 03:40:13 +02:00
|
|
|
|
}
|
|
|
|
|
}
|