using Biskilog_Accounting.Client.Pages.Product; using Biskilog_Accounting.Shared.CustomModels; using BlazorDateRangePicker; using Microsoft.AspNetCore.Components.Web; namespace Biskilog_Accounting.Client.Pages.Transactions { public partial class Sales { private IEnumerable m_salesItems { get; set; } = Enumerable.Empty(); private DateTimeOffset? m_startDate { get; set; } = DateTimeOffset.Now; private DateTimeOffset? m_endDate { get; set; } = DateTimeOffset.Now; private Dictionary m_ranges = new Dictionary(); private double m_totalRevenue { get; set; } private double m_totalCancelledRevenue { get; set; } private int m_totalNbrRevenue { get; set; } private int m_totalNbrCancelled { get; set; } private bool m_lookUpMode { get; set; } private bool m_fetching { get; set; } private bool m_recent { get; set; } protected override void OnInitialized() { PopulateRange(); m_salesRepo.TransactionsChanged += TransactionsChanged; m_searchControl.SearchValueChanged += SearchValueChanged; m_salesRepo.FetchStart += FetchStart; m_salesRepo.FetchComplete += FetchComplete; m_salesItems = m_salesRepo.GetRecentTransaction(); SetParams(); m_recent = true; base.OnInitialized(); } private void FetchComplete(object? sender, EventArgs e) { m_fetching = false; StateHasChanged(); } private void FetchStart(object? sender, EventArgs e) { m_fetching = true; StateHasChanged(); } void ResetClick(MouseEventArgs e, DateRangePicker picker) { m_startDate = DateTimeOffset.UtcNow; m_endDate = DateTimeOffset.UtcNow; // Close the picker picker.Close(); // Fire OnRangeSelectEvent picker.OnRangeSelect.InvokeAsync(new DateRange()); } private void PopulateRange() { m_ranges.Clear(); m_ranges.Add("Today", new DateRange() { Start = DateTime.Now, End = DateTime.Now.AddDays(1) }); m_ranges.Add("Yesterday", new DateRange() { Start = DateTime.Now.AddDays(-1), End = DateTime.Now }); m_ranges.Add("Last 7 Days", new DateRange() { Start = DateTime.Now.AddDays(-7), End = DateTime.Now }); m_ranges.Add("Last 30 Days", new DateRange() { Start = DateTime.Now.AddDays(-30), End = DateTime.Now }); } private void TransactionsChanged(object? sender, EventArgs e) { m_salesItems = m_salesRepo.GetTransactions(m_startDate.Value.Date, m_endDate.Value.Date); SetParams(); StateHasChanged(); } private void SearchValueChanged(string a_searchKey) { m_salesRepo.FetchReceipt(a_searchKey); m_lookUpMode = !string.IsNullOrEmpty(a_searchKey); } public void Dispose() { m_salesRepo.TransactionsChanged -= TransactionsChanged; m_searchControl.SearchValueChanged -= SearchValueChanged; m_salesRepo.FetchStart -= FetchStart; m_salesRepo.FetchComplete -= FetchComplete; } private void SetParams() { m_totalRevenue = (double)m_salesItems.Where(c => c.Status == "SOLD").Sum(t => t.Total).Value; m_totalNbrRevenue = m_salesItems.Where(c => c.Status == "SOLD").Count(); m_totalNbrCancelled = m_salesItems.Where(c => c.Status != "SOLD").Count(); m_totalCancelledRevenue = (double)m_salesItems.Where(c => c.Status != "SOLD").Sum(t => t.Total).Value; m_recent = false; } private void LoadTransactions() { m_searchControl.SearchValueChanged -= SearchValueChanged; m_searchControl.Clear(); m_lookUpMode = false; m_salesRepo.FetchTransaction(m_startDate.Value.Date, m_endDate.Value.Date); m_searchControl.SearchValueChanged += SearchValueChanged; } } }