Browse Source

Price change history bug divide by zero fix

pull/13/head
Benjamin Arhen 1 year ago
parent
commit
c41839b258
  1. 74
      Client/Pages/Dashboard/Elements/ProductPriceHistory.razor.cs
  2. 2
      Server/Services/AnalyticalService.cs

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

@ -25,6 +25,7 @@ namespace Biskilog_Accounting.Client.Pages.Dashboard.Elements
private Timer m_timer { get; set; } private Timer m_timer { get; set; }
private ApexChart<ProductPriceChange> chart; private ApexChart<ProductPriceChange> chart;
private Random m_random = new Random(); private Random m_random = new Random();
private int tries = 10;
protected override void OnInitialized() protected override void OnInitialized()
{ {
base.OnInitialized(); base.OnInitialized();
@ -37,35 +38,61 @@ namespace Biskilog_Accounting.Client.Pages.Dashboard.Elements
private void TimerElapsed(object sender, ElapsedEventArgs e) private void TimerElapsed(object sender, ElapsedEventArgs e)
{ {
tries = 0;
// Code to execute every 10 seconds // Code to execute every 10 seconds
InvokeAsync(async () => InvokeAsync(async () =>
{ {
if (m_productHistory.Count > 0) await GetPriceChange();
{ StateHasChanged();
int randomNumber = m_random.Next(ProductHistory.Count); await chart.RenderAsync();
m_currentProduct = ProductHistory[randomNumber].Pcode; });
m_productHistory = ProductHistory.Where(t => t.Pcode == m_currentProduct).ToList(); }
private async Task GetPriceChange()
{
tries++;
if (m_productHistory.Count > 0)
{
int randomNumber = m_random.Next(ProductHistory.Count);
ProductPriceChange change = m_productHistory.First(); m_productHistory = ProductHistory.Where(t => t.Pcode == m_currentProduct).ToList();
ProductPriceChange change = m_productHistory.First();
double actualChange = (double)((change.CurrentPrice - change.PreviousPrice));
if (actualChange == 0)
{
// if randomly selected doesn't have any product change history
if (!string.IsNullOrEmpty(m_currentProduct))
{
return;
}
else
{
if (tries < 3)
{
GetPriceChange();
}
return;
}
}
else
{
m_percentage = (double)((change.CurrentPrice - change.PreviousPrice) / change.PreviousPrice > 0 ? change.PreviousPrice * 100 : 1); m_percentage = (double)((change.CurrentPrice - change.PreviousPrice) / change.PreviousPrice > 0 ? change.PreviousPrice * 100 : 1);
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";
StateHasChanged();
await chart.RenderAsync();
} }
});
}
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";
}
}
public void Dispose() public void Dispose()
{ {
// Dispose of the timer when the component is disposed // Dispose of the timer when the component is disposed
m_timer?.Stop(); m_timer?.Stop();
m_timer?.Dispose(); m_timer?.Dispose();
} }
protected override void OnParametersSet() protected override async void OnParametersSet()
{ {
if (!IsLoading) if (!IsLoading)
{ {
@ -101,21 +128,8 @@ namespace Biskilog_Accounting.Client.Pages.Dashboard.Elements
} }
} }
}; };
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) await GetPriceChange();
{
ProductPriceChange change = m_productHistory.First();
m_percentage = (double)((change.CurrentPrice - change.PreviousPrice) / change.PreviousPrice > 0 ? change.PreviousPrice * 100 : 1);
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(); base.OnParametersSet();
} }

2
Server/Services/AnalyticalService.cs

@ -89,7 +89,7 @@ namespace Biskilog_Accounting.Server.Services
yield return new CustomerAccounts yield return new CustomerAccounts
{ {
Customer = m_context.Tblcustomers.FirstOrDefault(i => i.CustomerId == customerId), Customer = m_context.Tblcustomers.FirstOrDefault(i => i.CustomerId == customerId),
Debt = m_context.Customeraccounts.OrderByDescending(d => d.Date).FirstOrDefault(t => t.Balance < 0 && t.CustomerId == customerId).Balance, Debt = m_context.Customeraccounts.AsEnumerable().OrderByDescending(d => d.Date).FirstOrDefault(t => t.Balance < 0 && t.CustomerId == customerId).Balance,
}; };
} }
} }

Loading…
Cancel
Save