Browse Source

Dashboard commit 5

pull/10/head
Benjamin Arhen 2 years ago
parent
commit
f82fca377e
  1. 18
      Client/Pages/Dashboard/Dashboard.razor
  2. 67
      Client/Pages/Dashboard/Dashboard.razor.cs
  3. 29
      Client/Pages/Dashboard/Elements/MostPurchasedElement.razor
  4. 18
      Client/Pages/Dashboard/Elements/ProductPriceHistory.razor
  5. 75
      Client/Pages/Dashboard/Elements/ProductPriceHistory.razor.cs
  6. 9
      Server/Controllers/AnalyticsController.cs
  7. 33
      Server/Services/AnalyticalService.cs

18
Client/Pages/Dashboard/Dashboard.razor

@ -35,23 +35,7 @@
<!-- </div>
<div class="row"> -->
<div class="col-12 mb-4">
<div class="card">
<div class="card-body">
<div class="d-flex justify-content-between flex-sm-row flex-column gap-3">
<div class="d-flex flex-sm-column flex-row align-items-start justify-content-between">
<div class="card-title">
<h5 class="text-nowrap mb-2">Profile Report</h5>
<span class="badge bg-label-warning rounded-pill">Year 2021</span>
</div>
<div class="mt-sm-auto">
<small class="text-success text-nowrap fw-semibold"><i class="bx bx-chevron-up"></i> 68.2%</small>
<h3 class="mb-0">$84,686k</h3>
</div>
</div>
<div id="profileReportChart"></div>
</div>
</div>
</div>
<ProductPriceHistory ProductHistory="@m_ProductPriceChanges" IsLoading="@loadingPriceHistory" />
</div>
</div>
</div>

67
Client/Pages/Dashboard/Dashboard.razor.cs

@ -12,26 +12,48 @@ namespace Biskilog_Accounting.Client.Pages.Dashboard
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 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 loadingPriceHistory = true;
private bool m_loadingMostPurchased { get; set; } = true;
protected override async Task OnInitializedAsync()
{
m_username = m_tokenService.GetUserNameFromToken(await m_tokenService.GetToken())!;
await Task.Delay(2500);
await GetTradeSummary();
await GetWeeklySales();
await GetCancelledSales();
await GetDebtSummary();
await GetRecentTransactions();
await GetMostPurchased();
await GetLowStockItems();
// 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
);
});
// Rest of the code...
return;
}
// {a_limit
//}
/// <summary>
/// Gets the trade summary
/// </summary>
@ -168,9 +190,10 @@ namespace Biskilog_Accounting.Client.Pages.Dashboard
{
try
{
m_loadingMostPurchased = true;
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}");
var response = await m_http.GetAsync($"api/analytics/mostpurchaseditem/" + start + "/" + end);
if (response.IsSuccessStatusCode)
{
var jsonContent = await response.Content.ReadAsStringAsync();
@ -201,7 +224,31 @@ namespace Biskilog_Accounting.Client.Pages.Dashboard
StateHasChanged();
}
}
catch (Exception ex) {
catch (Exception ex)
{
}
}
/// <summary>
/// Gets a collection of items that are low on stock
/// </summary>
/// <returns></returns>
async Task GetProductPriceChangeHistory()
{
try
{
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;
loadingPriceHistory = false;
StateHasChanged();
}
}
catch (Exception ex)
{
}
}
}

29
Client/Pages/Dashboard/Elements/MostPurchasedElement.razor

@ -3,35 +3,12 @@
@inject ICalculator m_calculator
@if (!IsLoading)
{
@if (MostPurchasedItems.Count() > 5)
{
<div class="card h-100">
<div class="card-body" style="padding:5px !important;">
<div class="d-flex justify-content-between flex-sm-row flex-column gap-3">
<div class="d-flex flex-sm-column flex-row align-items-start justify-content-between">
<div class="card-title" style="padding:10px;">
<div class="card-header d-flex align-items-center justify-content-between pb-0">
<div class="card-title mb-0">
<h5 class="text-nowrap mb-2">Top 50 most purchased items</h5>
</div>
<div class="mt-sm-auto">
@foreach (MostPurchasedItem purchasedItem in m_items.Take(5))
{
<small class="text-nowrap fw-semibold">
@($"{purchasedItem.ProductName} x{purchasedItem.NbrTimesSold}")
</small> <br />
}
</div>
</div>
<ApexChart TItem="MostPurchasedItem" Options="@m_options" Width="150" Height="150">
<ApexPointSeries TItem="MostPurchasedItem"
Items="m_items"
SeriesType="SeriesType.Donut"
Name="Top 5 performers"
XValue="@(e => e.ProductName)"
YValue="@(e => e.NbrTimesSold)"
OrderByDescending="e=>e.Y" />
</ApexChart>
</div>
<div class="card-body" style="padding:5px !important;">
<RadzenDataGrid Data="@MostPurchasedItems" TItem="MostPurchasedItem" AllowPaging="true" AllowSorting="true" PageSize="5">
@ -54,8 +31,6 @@
</RadzenDataGrid>
</div>
</div>
</div>
}
}
else
{

18
Client/Pages/Dashboard/Elements/ProductPriceHistory.razor

@ -0,0 +1,18 @@
@using Biskilog_Accounting.Shared.CustomModels;
@using Biskilog_Accounting.Shared.Interfaces;
@inject ICalculator m_calculator
@if (!IsLoading)
{
<div class="card">
<ApexChart TItem="ProductPriceChange" Options="options" Title="@m_title">
<ApexPointSeries TItem="ProductPriceChange" Items="m_productHistory" Name="@m_subtitle"
SeriesType="SeriesType.Area" XValue="@(e => e.ChangeDate.Value.ToString("dd MMM,yy"))"
YValue="@(e => e.CurrentPrice)" />
</ApexChart>
</div>
}
else
{
}

75
Client/Pages/Dashboard/Elements/ProductPriceHistory.razor.cs

@ -0,0 +1,75 @@
using ApexCharts;
using Biskilog_Accounting.Shared.CustomModels;
using Microsoft.AspNetCore.Components;
namespace Biskilog_Accounting.Client.Pages.Dashboard.Elements
{
public partial class ProductPriceHistory
{
[Parameter]
public List<ProductPriceChange> ProductHistory { get; set; }
[Parameter]
public bool IsLoading { get; set; } = true;
private List<ProductPriceChange> m_productHistory { get; set; } = new List<ProductPriceChange>();
private string m_currentProduct = string.Empty;
private double m_percentage { get; set; } = 0;
private bool m_increase { get; set; } = false;
private double m_currentPrice { get; set; } = 0;
private ApexChartOptions<ProductPriceChange> options = new ApexChartOptions<ProductPriceChange>();
private string m_productName { get; set; }= string.Empty;
private string m_subtitle { get;set; } = string.Empty;
private string m_title { get;set; } = string.Empty;
protected override void OnInitialized()
{
base.OnInitialized();
}
protected override void OnParametersSet()
{
if (!IsLoading)
{
options.Colors = new List<string> { "#f4c414" };
options.Fill = new Fill
{
Type = new List<FillType> { FillType.Gradient, FillType.Gradient },
Gradient = new FillGradient
{
ShadeIntensity = 1,
OpacityFrom = 0.2,
OpacityTo = 0.9,
}
};
options.Yaxis = new List<YAxis>
{
new YAxis
{
Labels = new YAxisLabels
{
Formatter = @"function (value, index, w) {
return Number(value).toLocaleString();}"
}
}
};
Random random = new Random();
int randomNumber = random.Next(ProductHistory.Count);
m_currentProduct = ProductHistory[randomNumber].Pcode;
m_productHistory = ProductHistory.Where(t => t.Pcode == m_currentProduct).ToList();
if (m_productHistory.Count > 0)
{
ProductPriceChange change = m_productHistory.First();
m_percentage = (double)((change.CurrentPrice - change.PreviousPrice) / change.PreviousPrice * 100);
m_increase = change.CurrentPrice > change.PreviousPrice;
m_currentPrice = (double)change.CurrentPrice;
m_productName = change.ProductName;
m_subtitle = $"Price :{m_calculator.GetCurrencyCode().CurrencySymbol}";
m_title = $"{m_productName} Price Changes";
}
}
base.OnParametersSet();
}
}
}

9
Server/Controllers/AnalyticsController.cs

@ -132,5 +132,14 @@ namespace Biskilog_Accounting.Server.Controllers
{
return m_analyticService.GetRecentPriceChanges(a_limit);
}
/// <summary>
/// Endpoint to return analysis on product price change history
/// </summary>
[Authorize]
[HttpGet, Route("pricechanges/product/history/{a_limit}")]
public IEnumerable<ProductPriceChange> GetPriceChangeHistory(int a_limit)
{
return m_analyticService.GetProductPriceChangeHistory(a_limit);
}
}
}

33
Server/Services/AnalyticalService.cs

@ -181,7 +181,38 @@ namespace Biskilog_Accounting.Server.Services
public IEnumerable<ProductPriceChange> GetProductPriceChangeHistory(int a_limit)
{
throw new NotImplementedException();
string token = m_httpContext.Request.Headers[HeaderNames.Authorization]!;
if (AuthEnums.Valid == m_tokenService.ValidateToken(token))
{
IEnumerable<string> accessiblebranches = m_tokenService.BranchIds(token);
using (var command = m_context.Database.GetDbConnection().CreateCommand())
{
command.CommandText = "CALL GetProductPriceChangeHistory(@p0,@p1)";
command.Parameters.Add(new MySqlParameter("@p0", string.Join(", ", accessiblebranches.ToArray())));
command.Parameters.Add(new MySqlParameter("@p1", a_limit));
m_context.Database.OpenConnection();
using (var reader = command.ExecuteReader())
{
while (reader.Read())
{
yield return new ProductPriceChange
{
Pcode = reader.GetString(0),
ProductName = reader.GetString(1),
PreviousPrice = reader.GetDecimal(2),
CurrentPrice = reader.GetDecimal(3),
ChangeDate = reader.GetDateTime(4),
BranchId = reader.GetString(5),
CountId = reader.GetString(6),
};
}
}
}
}
}
public IEnumerable<ProductPriceChange> GetRecentPriceChanges(int a_limit)

Loading…
Cancel
Save