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 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 ;
}
#region Unimplemented
public IEnumerable < SaleItem > GetReceipt ( string a_receiptId )
{
throw new NotImplementedException ( ) ;
}
public Task SyncCart ( List < Tblcart > a_item )
{
throw new NotImplementedException ( ) ;
}
public DateTime GetLastSyncDate ( string a_tablename )
{
throw new NotImplementedException ( ) ;
}
public void SetLastSyncDate ( string a_tableName , DateTime a_timestamp )
{
throw new NotImplementedException ( ) ;
}
public Task SyncCancelledTransaction ( List < Tblcancelledtransaction > a_item )
{
throw new NotImplementedException ( ) ;
}
public Task SyncCreditPurchase ( List < Creditpurchase > a_item )
{
throw new NotImplementedException ( ) ;
}
public Task SyncCustomerAccount ( List < Customeraccount > a_customerAccounts )
{
throw new NotImplementedException ( ) ;
}
public Task SyncCustomerPurchase ( List < Tblcustomerpurchase > a_customerPurchase )
{
throw new NotImplementedException ( ) ;
}
public Task SyncDiscountLogs ( List < Tbldiscountlog > a_discountLog )
{
throw new NotImplementedException ( ) ;
}
public Task SyncDeliveryDetails ( List < Tbldeliverydetail > a_details )
{
throw new NotImplementedException ( ) ;
}
public Task SyncDeliveryHead ( List < Tbldeliveryhead > a_heads )
{
throw new NotImplementedException ( ) ;
}
public Task SyncDeliveryRecipients ( List < Tbldeliveryrecipient > a_recipients )
{
throw new NotImplementedException ( ) ;
}
public Task SyncInvoice ( List < Tblinvoice > a_invoice )
{
throw new NotImplementedException ( ) ;
}
#endregion
}
}