New source control repo for Biskilog POS - secure hub to store & manage source code. Streamlines dev process, tracks changes, & improves collaboration. Ensures reliable software.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

311 lines
13 KiB

using Biskilog_Accounting.Shared.CustomModels;
using System.Net.Http.Json;
using System.Text.Json;
using static System.Net.WebRequestMethods;
namespace Biskilog_Accounting.Client.Pages.Dashboard
{
public partial class Dashboard
{
#region Declarations
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 List<ProductPriceChange> m_ProductPriceChanges { get; set; } = new List<ProductPriceChange> { };
private IEnumerable<WeeklyCategorySummary> m_categorySummary { get; set; } = new List<WeeklyCategorySummary>();
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;
#region Loading Variables
private bool m_loadingWeeklySales = true;
private bool m_loadingPriceHistory = true;
private bool m_loadingMostPurchased { get; set; } = true;
private bool m_loadingTradeSummary { get; set; } = true;
private bool m_loadingDebtSummary { get; set; } = true;
private bool m_loadingLowStockSummary { get; set; } = true;
private bool m_loadingRecentTransactionsSummary { get; set; } = true;
private bool m_loadingCancelledSummary { get; set; } = true;
private bool m_loadingCreditSummary { get; set; } = true;
#endregion
#endregion
protected override async Task OnInitializedAsync()
{
m_username = m_tokenService.GetUserNameFromToken(await m_tokenService.GetToken())!;
await Task.Delay(2500);
await GetTradeSummary();
// Start the tasks without blocking the UI
_ = Task.Run(async () =>
{
var weeklySalesTask = GetWeeklySales();
var categoryWeekly = GetCategoryTradeSummary();
var cancelledSalesTask = GetCancelledSales();
var debtSummaryTask = GetDebtSummary();
var recentTransactionsTask = GetRecentTransactions();
var mostPurchasedTask = GetMostPurchased();
var lowStockItemsTask = GetLowStockItems();
var productPriceChangeHistoryTask = GetProductPriceChangeHistory();
// Wait for all tasks to complete
await Task.WhenAll(
weeklySalesTask,
categoryWeekly,
cancelledSalesTask,
debtSummaryTask,
recentTransactionsTask,
mostPurchasedTask,
lowStockItemsTask,
productPriceChangeHistoryTask
);
});
return;
}
/// <summary>
/// Gets the trade summary
/// </summary>
/// <returns></returns>
async Task GetTradeSummary()
{
try
{
m_loadingTradeSummary = true;
StateHasChanged();
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;
m_loadingTradeSummary = false;
StateHasChanged();
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
/// <summary>
/// Gets the trade summary
/// </summary>
/// <returns></returns>
async Task GetCategoryTradeSummary()
{
try
{
m_loadingWeeklySales = true;
StateHasChanged();
var response = await m_http.GetAsync("api/analytics/categorysummary");
if (response.IsSuccessStatusCode)
{
var jsonContent = await response.Content.ReadAsStringAsync();
var options = new JsonSerializerOptions { PropertyNameCaseInsensitive = true };
var tradeCategorySummary = JsonSerializer.Deserialize<IEnumerable<WeeklyCategorySummary>>(jsonContent, options);
m_categorySummary = tradeCategorySummary;
m_loadingWeeklySales = false;
StateHasChanged();
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
/// <summary>
/// Gets the debt summary
/// </summary>
/// <returns></returns>
async Task GetDebtSummary()
{
try
{
m_loadingDebtSummary = true;
StateHasChanged();
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<CustomerAccounts>>(jsonContent, options);
m_totalDebt = (double)debt.Sum(c => c.Debt);
m_loadingDebtSummary = false;
StateHasChanged();
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
/// <summary>
/// Gets the weekly sales
/// </summary>
/// <returns></returns>
async Task GetWeeklySales()
{
try
{
m_loadingWeeklySales = true;
StateHasChanged();
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;
}
response = await m_http.GetAsync("api/analytics/categorysummary");
if (response.IsSuccessStatusCode)
{
var jsonContent = await response.Content.ReadAsStringAsync();
var options = new JsonSerializerOptions { PropertyNameCaseInsensitive = true };
var tradeCategorySummary = JsonSerializer.Deserialize<IEnumerable<WeeklyCategorySummary>>(jsonContent, options);
m_categorySummary = tradeCategorySummary;
}
m_loadingWeeklySales = 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
{
m_loadingCancelledSummary = true;
StateHasChanged();
string start = m_tradeSummary.CurrentTradeDate.Date.AddDays(-7).ToString("yyyy-MM-dd");
string end = m_tradeSummary.CurrentTradeDate.ToString("yyyy-MM-dd");
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) * 100;
}
else
{
m_cancelledWeeklySale = 0;
m_cancelledPercentage = 0;
}
m_loadingCancelledSummary = false;
StateHasChanged();
}
}
catch { }
}
/// <summary>
/// Gets the recent transaction
/// </summary>
/// <returns></returns>
async Task GetRecentTransactions()
{
try
{
m_loadingRecentTransactionsSummary = true;
StateHasChanged();
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;
m_loadingRecentTransactionsSummary = false;
StateHasChanged();
}
}
catch { }
}
/// <summary>
/// Gets a collection of most purchased items within the past
/// </summary>
/// <returns></returns>
async Task GetMostPurchased()
{
try
{
m_loadingMostPurchased = true;
StateHasChanged();
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
{
m_loadingLowStockSummary = true;
StateHasChanged();
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;
m_loadingLowStockSummary = false;
StateHasChanged();
}
}
catch { }
}
/// <summary>
/// Gets a collection of items that are low on stock
/// </summary>
/// <returns></returns>
async Task GetProductPriceChangeHistory()
{
try
{
m_loadingPriceHistory = true;
StateHasChanged();
var response = await m_http.GetAsync($"api/analytics/pricechanges/product/history/{5}");
if (response.IsSuccessStatusCode)
{
var jsonContent = await response.Content.ReadAsStringAsync();
var options = new JsonSerializerOptions { PropertyNameCaseInsensitive = true };
var recent = JsonSerializer.Deserialize<List<ProductPriceChange>>(jsonContent, options);
m_ProductPriceChanges = recent;
m_loadingPriceHistory = false;
StateHasChanged();
}
}
catch { }
}
}
}