New source control repo for Biskilog POS - secure hub to store & manage source code. Streamlines dev process, tracks changes, & improves collaboration. Ensures reliable software.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

99 lines
4.0 KiB

using Biskilog_Accounting.Shared.CustomModels;
using Biskilog_Accounting.Shared.Interfaces;
using Biskilog_Accounting.Shared.POSModels;
using System.Text.Json;
namespace Biskilog_Accounting.Client.Repos
{
public class SalesRepository : ISalesInterface
{
private IEnumerable<SaleItem> m_transactions;
private IEnumerable<SaleItem> m_recentTransactions;
private readonly HttpClient m_http;
public event EventHandler TransactionsChanged;
public event EventHandler FetchComplete;
public event EventHandler FetchStart;
public SalesRepository(HttpClient a_http)
{
m_recentTransactions = m_transactions = new List<SaleItem>();
m_http = a_http;
}
public async Task FetchRecentTransaction(int a_limit)
{
FetchStart?.Invoke(this, EventArgs.Empty);
var response = await m_http.GetAsync($"api/analytics/sales/recent/{a_limit}");
if (response.IsSuccessStatusCode)
{
var jsonContent = await response.Content.ReadAsStringAsync();
var options = new JsonSerializerOptions { PropertyNameCaseInsensitive = true };
var recent = JsonSerializer.Deserialize<List<SaleItem>>(jsonContent, options);
m_recentTransactions = recent;
}
FetchComplete?.Invoke(this, EventArgs.Empty);
}
public async Task FetchTransaction(DateTime a_start, DateTime a_end)
{
FetchStart?.Invoke(this, EventArgs.Empty);
string start = a_start.ToString("yyyy-MM-dd");
string end = a_end.ToString("yyyy-MM-dd");
var response = await m_http.GetAsync($"api/sales/transactions/{start}/{end}");
if (response.IsSuccessStatusCode)
{
var jsonContent = await response.Content.ReadAsStringAsync();
var options = new JsonSerializerOptions { PropertyNameCaseInsensitive = true };
var recent = JsonSerializer.Deserialize<List<SaleItem>>(jsonContent, options);
m_transactions = recent;
TransactionsChanged?.Invoke(this, new EventArgs());
}
FetchComplete?.Invoke(this, EventArgs.Empty);
}
public IEnumerable<SaleItem> GetTransactions(DateTime a_start, DateTime a_end)
{
return m_transactions;
}
public IEnumerable<SaleItem> GetRecentTransaction()
{
return m_recentTransactions;
}
public async Task FetchReceipt(string a_receiptId)
{
FetchStart?.Invoke(this, EventArgs.Empty);
var response = await m_http.GetAsync($"api/sales/transactions/lookup/{a_receiptId}");
if (response.IsSuccessStatusCode)
{
var jsonContent = await response.Content.ReadAsStringAsync();
var options = new JsonSerializerOptions { PropertyNameCaseInsensitive = true };
var recent = JsonSerializer.Deserialize<List<SaleItem>>(jsonContent, options);
m_transactions = recent;
TransactionsChanged?.Invoke(this, new EventArgs());
}
FetchComplete?.Invoke(this, EventArgs.Empty);
}
public IEnumerable<SaleItem> GetReceipt(string a_receiptId)
{
throw new NotImplementedException();
}
public async Task<IEnumerable<Tblcart>> GetReceiptDetail(string a_receiptId)
{
var response = await m_http.GetAsync($"api/sales/receipt/lookup/{a_receiptId}");
if (response.IsSuccessStatusCode)
{
var jsonContent = await response.Content.ReadAsStringAsync();
var options = new JsonSerializerOptions { PropertyNameCaseInsensitive = true };
var recent = JsonSerializer.Deserialize<List<Tblcart>>(jsonContent, options);
return recent;
}
return null;
}
}
}