using Biskilog_Accounting.Server.POSModels;
using Biskilog_Accounting.Server.Services;
using Biskilog_Accounting.Shared.CustomModels;
using Biskilog_Accounting.Shared.Interfaces;
using Biskilog_Accounting.Shared.POSModels;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.Net.Http.Headers;
using System.ComponentModel.DataAnnotations;
using System.Data.Entity;
namespace Biskilog_Accounting.Server.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class AnalyticsController : ControllerBase
{
private readonly IConnectionService m_connection;
private readonly ITokenService m_tokenService;
public AnalyticsController(ITokenService tokenService, IConnectionService connection)
{
m_tokenService = tokenService;
m_connection = connection;
}
///
/// Endpoint to return analysis on CancelledSales within a specified period
///
///
///
[Authorize]
[HttpGet, Route("cancelledsales/{a_start}/{a_end}")]
public IEnumerable GetCancelledSalesAsync(DateTime a_start, DateTime a_end)
{
string token = Request.Headers[HeaderNames.Authorization]!;
int? databaseId = m_tokenService.GetDatabaseIdFromToken(token);
string connectionString = m_connection.GetClientConnectionString(databaseId!.Value);
bool? comparisonMode = m_tokenService.GetComparison(token);
string? currentBranch = m_tokenService.GetBaseBranch(token);
//Creates a new db context
BiskAcdbContext newContext = (BiskAcdbContext)m_connection.PrepareDBContext(connectionString);
AnalyticalService analysis = new(newContext, comparisonMode!.Value, currentBranch!);
var result = analysis.GetCancelledSales(a_start, a_end);
return result;
}
///
/// Endpoint to return analysis on Sales within a specified period
///
///
///
[Authorize]
[HttpGet, Route("sales/{a_start}/{a_end}")]
public IEnumerable GetSalesAsync(DateTime a_start, DateTime a_end)
{
string token = Request.Headers[HeaderNames.Authorization]!;
int? databaseId = m_tokenService.GetDatabaseIdFromToken(token);
string connectionString = m_connection.GetClientConnectionString(databaseId!.Value);
bool? comparisonMode = m_tokenService.GetComparison(token);
string? currentBranch = m_tokenService.GetBaseBranch(token);
//Creates a new db context
BiskAcdbContext newContext = (BiskAcdbContext)m_connection.PrepareDBContext(connectionString);
AnalyticalService analysis = new(newContext, comparisonMode!.Value, currentBranch!);
return analysis.GetSalesTransaction(a_start, a_end);
}
///
/// Endpoint to return analysis on in-debt customers
///
///
///
[Authorize]
[HttpGet, Route("debtors")]
public IEnumerable GetInDebtCustomers()
{
string token = Request.Headers[HeaderNames.Authorization]!;
int? databaseId = m_tokenService.GetDatabaseIdFromToken(token);
string connectionString = m_connection.GetClientConnectionString(databaseId!.Value);
bool? comparisonMode = m_tokenService.GetComparison(token);
string? currentBranch = m_tokenService.GetBaseBranch(token);
//Creates a new db context
BiskAcdbContext newContext = (BiskAcdbContext)m_connection.PrepareDBContext(connectionString);
AnalyticalService analysis = new(newContext, comparisonMode!.Value, currentBranch!);
return analysis.GetInDebtCustomers();
}
}
}