using Biskilog_Accounting.Shared.CustomModels; using System.Net.Http.Json; using System.Text.Json; namespace Biskilog_Accounting.Client.Pages.Dashboard { public partial class Dashboard { #region Declarations private TradeSummary m_tradeSummary { get; set; } = new TradeSummary(); private List m_weeklySaleItems { get; set; } = new List { }; private List m_weeklyCancelledSales { get; set; } = new List { }; private List m_recentTransaction { get; set; } = new List { }; private List m_mostPurchased { get; set; } = new List { }; private List m_lowstock { get; set; } = new List { }; private List m_ProductPriceChanges { get; set; } = new List { }; 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 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, cancelledSalesTask, debtSummaryTask, recentTransactionsTask, mostPurchasedTask, lowStockItemsTask, productPriceChangeHistoryTask ); }); return; } /// /// Gets the trade summary /// /// 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(jsonContent, options); m_tradeSummary = tradeSummary; m_loadingTradeSummary = false; StateHasChanged(); } } catch (Exception ex) { Console.WriteLine(ex.Message); } } /// /// Gets the debt summary /// /// 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>(jsonContent, options); m_totalDebt = (double)debt.Sum(c => c.Debt); m_loadingDebtSummary = false; StateHasChanged(); } } catch (Exception ex) { Console.WriteLine(ex.Message); } } /// /// Gets the weekly sales /// /// 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>(jsonContent, options); m_weeklySaleItems = sales; m_loadingWeeklySales = false; StateHasChanged(); } } catch (Exception ex) { Console.WriteLine(ex.Message); } } /// /// Gets the summary of cancelled sales for the past week /// /// 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>(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 { } } /// /// Gets the recent transaction /// /// 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>(jsonContent, options); m_recentTransaction = recent; m_loadingRecentTransactionsSummary = false; StateHasChanged(); } } catch { } } /// /// Gets a collection of most purchased items within the past /// /// 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>(jsonContent, options); m_mostPurchased = recent; m_loadingMostPurchased = false; StateHasChanged(); } } catch { } } /// /// Gets a collection of items that are low on stock /// /// 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>(jsonContent, options); m_lowstock = recent; m_loadingLowStockSummary = false; StateHasChanged(); } } catch { } } /// /// Gets a collection of items that are low on stock /// /// 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>(jsonContent, options); m_ProductPriceChanges = recent; m_loadingPriceHistory = false; StateHasChanged(); } } catch { } } } }