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 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 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 //} /// /// Gets the trade summary /// /// 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(jsonContent, options); m_tradeSummary = tradeSummary; StateHasChanged(); } } catch (Exception ex) { Console.WriteLine(ex.Message); } } /// /// Gets the debt summary /// /// 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>(jsonContent, options); m_totalDebt = (double)debt.Sum(c => c.Debt); StateHasChanged(); } } catch (Exception ex) { Console.WriteLine(ex.Message); } } /// /// Gets the weekly sales /// /// 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>(jsonContent, options); m_weeklySaleItems = sales; loadWeeklySales = false; StateHasChanged(); } } catch (Exception ex) { Console.WriteLine(ex.Message); } } /// /// Gets the summary of cancelled sales for the past week /// /// 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>(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 = (double)loadCancelledSales.FirstOrDefault().Value; m_cancelledPercentage = 0; } StateHasChanged(); } } catch (Exception ex) { Console.WriteLine(ex.Message); } } /// /// Gets the recent transaction /// /// 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>(jsonContent, options); m_recentTransaction = recent; StateHasChanged(); } } catch { } } /// /// Gets a collection of most purchased items within the past /// /// 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>(jsonContent, options); m_mostPurchased = recent; m_loadingMostPurchased = false; StateHasChanged(); } } catch { } } /// /// Gets a collection of items that are low on stock /// /// 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>(jsonContent, options); m_lowstock = recent; StateHasChanged(); } } catch (Exception ex) { } } } }