From c41839b2580c902173136d6f79c85d8cf7d442e8 Mon Sep 17 00:00:00 2001 From: barhen Date: Sat, 16 Dec 2023 12:08:53 -0500 Subject: [PATCH] Price change history bug divide by zero fix --- .../Elements/ProductPriceHistory.razor.cs | 74 +++++++++++-------- Server/Services/AnalyticalService.cs | 2 +- 2 files changed, 45 insertions(+), 31 deletions(-) diff --git a/Client/Pages/Dashboard/Elements/ProductPriceHistory.razor.cs b/Client/Pages/Dashboard/Elements/ProductPriceHistory.razor.cs index 54056c9..3c40e41 100644 --- a/Client/Pages/Dashboard/Elements/ProductPriceHistory.razor.cs +++ b/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 ApexChart chart; private Random m_random = new Random(); + private int tries = 10; protected override void OnInitialized() { base.OnInitialized(); @@ -37,35 +38,61 @@ namespace Biskilog_Accounting.Client.Pages.Dashboard.Elements private void TimerElapsed(object sender, ElapsedEventArgs e) { + tries = 0; // Code to execute every 10 seconds InvokeAsync(async () => { - if (m_productHistory.Count > 0) - { - int randomNumber = m_random.Next(ProductHistory.Count); - m_currentProduct = ProductHistory[randomNumber].Pcode; - m_productHistory = ProductHistory.Where(t => t.Pcode == m_currentProduct).ToList(); + await GetPriceChange(); + StateHasChanged(); + await chart.RenderAsync(); + }); + } + 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_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() { // Dispose of the timer when the component is disposed m_timer?.Stop(); m_timer?.Dispose(); } - protected override void OnParametersSet() + protected override async void OnParametersSet() { 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) - { - 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"; - } + await GetPriceChange(); } base.OnParametersSet(); } diff --git a/Server/Services/AnalyticalService.cs b/Server/Services/AnalyticalService.cs index 3e15c38..48a3a34 100644 --- a/Server/Services/AnalyticalService.cs +++ b/Server/Services/AnalyticalService.cs @@ -89,7 +89,7 @@ namespace Biskilog_Accounting.Server.Services yield return new CustomerAccounts { 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, }; } }