using Biskilog_Accounting.Shared.CustomModels ;
using System.Net.Http.Json ;
using System.Text.Json ;
namespace Biskilog_Accounting.Client.Pages.Dashboard
{
public partial class Dashboard
{
private TradeSummary m_tradeSummary { get ; set ; } = new TradeSummary ( ) ;
private List < WeeklySaleItem > m_weeklySaleItems { get ; set ; } = new List < WeeklySaleItem > { } ;
private List < CancelledSales > m_weeklyCancelledSales { get ; set ; } = new List < CancelledSales > { } ;
private List < SaleItem > m_recentTransaction { get ; set ; } = new List < SaleItem > { } ;
private List < MostPurchasedItem > m_mostPurchased { get ; set ; } = new List < MostPurchasedItem > { } ;
private List < ProductItem > m_lowstock { get ; set ; } = new List < ProductItem > { } ;
private double m_cancelledWeeklySale { get ; set ; } = 0 ;
private double m_cancelledPercentage { get ; set ; } = 0 ;
private double m_totalDebt { get ; set ; } = 0 ;
private string m_username { get ; set ; } = string . Empty ;
private bool loadWeeklySales = true ;
private bool m_loadingMostPurchased { get ; set ; } = true ;
protected override async Task OnInitializedAsync ( )
{
m_username = m_tokenService . GetUserNameFromToken ( await m_tokenService . GetToken ( ) ) ! ;
await GetTradeSummary ( ) ;
await GetWeeklySales ( ) ;
await GetCancelledSales ( ) ;
await GetDebtSummary ( ) ;
await GetRecentTransactions ( ) ;
await GetMostPurchased ( ) ;
await GetLowStockItems ( ) ;
return ;
}
// {a_limit
//}
/// <summary>
/// Gets the trade summary
/// </summary>
/// <returns></returns>
async Task GetTradeSummary ( )
{
try
{
var response = await m_http . GetAsync ( "api/analytics/tradesummary" ) ;
if ( response . IsSuccessStatusCode )
{
var jsonContent = await response . Content . ReadAsStringAsync ( ) ;
var options = new JsonSerializerOptions { PropertyNameCaseInsensitive = true } ;
var tradeSummary = JsonSerializer . Deserialize < TradeSummary > ( jsonContent , options ) ;
m_tradeSummary = tradeSummary ;
StateHasChanged ( ) ;
}
}
catch ( Exception ex )
{
Console . WriteLine ( ex . Message ) ;
}
}
/// <summary>
/// Gets the debt summary
/// </summary>
/// <returns></returns>
async Task GetDebtSummary ( )
{
try
{
var response = await m_http . GetAsync ( "api/analytics/debtors" ) ;
if ( response . IsSuccessStatusCode )
{
var jsonContent = await response . Content . ReadAsStringAsync ( ) ;
var options = new JsonSerializerOptions { PropertyNameCaseInsensitive = true } ;
var debt = JsonSerializer . Deserialize < List < InDebtCustomers > > ( jsonContent , options ) ;
m_totalDebt = ( double ) debt . Sum ( c = > c . Debt ) ;
StateHasChanged ( ) ;
}
}
catch ( Exception ex )
{
Console . WriteLine ( ex . Message ) ;
}
}
/// <summary>
/// Gets the weekly sales
/// </summary>
/// <returns></returns>
async Task GetWeeklySales ( )
{
try
{
var response = await m_http . GetAsync ( "api/analytics/sales/weekly" ) ;
if ( response . IsSuccessStatusCode )
{
var jsonContent = await response . Content . ReadAsStringAsync ( ) ;
var options = new JsonSerializerOptions { PropertyNameCaseInsensitive = true } ;
var sales = JsonSerializer . Deserialize < List < WeeklySaleItem > > ( jsonContent , options ) ;
m_weeklySaleItems = sales ;
loadWeeklySales = false ;
StateHasChanged ( ) ;
}
}
catch ( Exception ex )
{
Console . WriteLine ( ex . Message ) ;
}
}
/// <summary>
/// Gets the summary of cancelled sales for the past week
/// </summary>
/// <returns></returns>
async Task GetCancelledSales ( )
{
try
{
DateTime start = DateTime . Now . AddDays ( - 7 ) . Date ;
DateTime end = DateTime . Now . Date ;
var response = await m_http . GetAsync ( $"api/analytics/cancelledsales/{start}/{end}" ) ;
if ( response . IsSuccessStatusCode )
{
var jsonContent = await response . Content . ReadAsStringAsync ( ) ;
var options = new JsonSerializerOptions { PropertyNameCaseInsensitive = true } ;
var weekly = JsonSerializer . Deserialize < List < CancelledSales > > ( jsonContent , options ) ;
m_weeklyCancelledSales = weekly ;
var loadCancelledSales = m_weeklyCancelledSales . OrderByDescending ( t = > t . CancelledTransaction . DateCancelled ) ;
if ( loadCancelledSales . Count ( ) > 1 )
{
m_cancelledWeeklySale = ( double ) loadCancelledSales . ToList ( ) [ 0 ] . Value ;
m_cancelledPercentage = ( ( ( double ) loadCancelledSales . ToList ( ) [ 0 ] . Value - ( double ) loadCancelledSales . ToList ( ) [ 1 ] . Value ) / ( double ) loadCancelledSales . ToList ( ) [ 0 ] . Value ) * 1 0 0 ;
}
else
{
m_cancelledWeeklySale = ( double ) loadCancelledSales . FirstOrDefault ( ) . Value ;
m_cancelledPercentage = 0 ;
}
StateHasChanged ( ) ;
}
}
catch ( Exception ex )
{
Console . WriteLine ( ex . Message ) ;
}
}
/// <summary>
/// Gets the recent transaction
/// </summary>
/// <returns></returns>
async Task GetRecentTransactions ( )
{
try
{
var response = await m_http . GetAsync ( $"api/analytics/sales/recent/{50}" ) ;
if ( response . IsSuccessStatusCode )
{
var jsonContent = await response . Content . ReadAsStringAsync ( ) ;
var options = new JsonSerializerOptions { PropertyNameCaseInsensitive = true } ;
var recent = JsonSerializer . Deserialize < List < SaleItem > > ( jsonContent , options ) ;
m_recentTransaction = recent ;
StateHasChanged ( ) ;
}
}
catch { }
}
/// <summary>
/// Gets a collection of most purchased items within the past
/// </summary>
/// <returns></returns>
async Task GetMostPurchased ( )
{
try
{
string start = m_tradeSummary . LastTradeDate . ToString ( "yyyy-MM-dd" ) ;
string end = m_tradeSummary . CurrentTradeDate . ToString ( "yyyy-MM-dd" ) ;
var response = await m_http . GetAsync ( $"api/analytics/mostpurchaseditem/{start}/{end}" ) ;
if ( response . IsSuccessStatusCode )
{
var jsonContent = await response . Content . ReadAsStringAsync ( ) ;
var options = new JsonSerializerOptions { PropertyNameCaseInsensitive = true } ;
var recent = JsonSerializer . Deserialize < List < MostPurchasedItem > > ( jsonContent , options ) ;
m_mostPurchased = recent ;
m_loadingMostPurchased = false ;
StateHasChanged ( ) ;
}
}
catch { }
}
/// <summary>
/// Gets a collection of items that are low on stock
/// </summary>
/// <returns></returns>
async Task GetLowStockItems ( )
{
try
{
var response = await m_http . GetAsync ( $"api/analytics/lowonstock" ) ;
if ( response . IsSuccessStatusCode )
{
var jsonContent = await response . Content . ReadAsStringAsync ( ) ;
var options = new JsonSerializerOptions { PropertyNameCaseInsensitive = true } ;
var recent = JsonSerializer . Deserialize < List < ProductItem > > ( jsonContent , options ) ;
m_lowstock = recent ;
StateHasChanged ( ) ;
}
}
catch ( Exception ex ) {
}
}
}
}