Biskilog local server manager for the Biskilog POS desktop application
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.

341 lines
16 KiB

using System.Net.Http.Headers;
using Biskilog_Cloud.Shared.Interfaces;
using Biskilog_Cloud.Shared.Models;
using Biskilog_Cloud.Shared.CustomModels;
namespace ServerManager.SyncMethods
{
public class SalesSync
{
private readonly ISalesInterface m_salesService;
private HttpClient m_httpClient;
public SalesSync(ISalesInterface a_salesService)
{
m_salesService = a_salesService;
m_httpClient = new HttpClient();
}
/// <summary>
/// Returns a collection of tasks to perform a sync in the SalesInterface
/// </summary>
/// <returns></returns>
public IEnumerable<Task> GetSalesSyncTask(HttpClient httpClient)
{
m_httpClient = httpClient;
return new Task[] {
SyncCartTable(),
SyncInvoice(),
SyncDiscountLogs(),
SyncCancelledTransactionTable(),
SyncDeliveryRecipient(),
SyncCreditPurchase(),
SyncCustomerAccounts(),
SyncCustomerPurchases(),
SyncDeliveryDetails(),
SynctblDeliveryhead(),
};
}
private async Task SyncCartTable()
{
var responseMessage = await m_httpClient.GetAsync("/api/SyncSales/lastsyncdate/tblcart");
if (!responseMessage.IsSuccessStatusCode)
{
return;
}
DateTime date = await responseMessage.Content.ReadFromJsonAsync<DateTime>();
IEnumerable<TblCart> modifiedCart = await m_salesService.FetchCartTbl(date, "BRID0");
SyncTimestamp syncTimestamp = new SyncTimestamp
{
TableName = "TblCart",
Timestamp = DateTime.Now.AddSeconds(-10),
};
int batchSize = 200;
int totalItems = modifiedCart.Count();
int batches = (totalItems + batchSize - 1) / batchSize; // Calculate total batches
for (int batchIndex = 0; batchIndex < batches; batchIndex++)
{
List<TblCart> batch = modifiedCart.Skip(batchIndex * batchSize).Take(batchSize).ToList();
var response = await m_httpClient.PostAsJsonAsync("/api/SyncSales/publish/tblCart", batch);
if (response.IsSuccessStatusCode)
{
Console.WriteLine($"Sent batch {batchIndex + 1}, Count: {batch.Count}");
}
}
//Set last sync date
await m_httpClient.PostAsJsonAsync("/api/SyncSales/setsyncdate", syncTimestamp);
}
private async Task SyncCancelledTransactionTable()
{
var responseMessage = await m_httpClient.GetAsync("/api/SyncSales/lastsyncdate/tblcancelledtransactions");
if (!responseMessage.IsSuccessStatusCode)
{
return;
}
DateTime date = await responseMessage.Content.ReadFromJsonAsync<DateTime>();
IEnumerable<TblCancelledTransaction> modifiedCart = await m_salesService.FetchCancelledTransaction(date, "BRID0");
SyncTimestamp syncTimestamp = new SyncTimestamp
{
TableName = "tblcancelledtransactions",
Timestamp = DateTime.Now.AddSeconds(-10),
};
int batchSize = 200;
int totalItems = modifiedCart.Count();
int batches = (totalItems + batchSize - 1) / batchSize; // Calculate total batches
for (int batchIndex = 0; batchIndex < batches; batchIndex++)
{
List<TblCancelledTransaction> batch = modifiedCart.Skip(batchIndex * batchSize).Take(batchSize).ToList();
var response = await m_httpClient.PostAsJsonAsync("/api/SyncSales/publish/tblcancelledtransaction", batch);
if (response.IsSuccessStatusCode)
{
Console.WriteLine($"Sent batch {batchIndex + 1}, Count: {batch.Count}");
}
}
//Set last sync date
await m_httpClient.PostAsJsonAsync("/api/SyncSales/setsyncdate", syncTimestamp);
}
private async Task SyncCreditPurchase()
{
var responseMessage = await m_httpClient.GetAsync("/api/SyncSales/lastsyncdate/CreditPurchases");
if (!responseMessage.IsSuccessStatusCode)
{
return;
}
DateTime date = await responseMessage.Content.ReadFromJsonAsync<DateTime>();
IEnumerable<CreditPurchase> modifiedCart = await m_salesService.FetchCreditPurchase(date, "BRID0");
SyncTimestamp syncTimestamp = new SyncTimestamp
{
TableName = "CreditPurchases",
Timestamp = DateTime.Now.AddSeconds(-10),
};
int batchSize = 200;
int totalItems = modifiedCart.Count();
int batches = (totalItems + batchSize - 1) / batchSize; // Calculate total batches
for (int batchIndex = 0; batchIndex < batches; batchIndex++)
{
List<CreditPurchase> batch = modifiedCart.Skip(batchIndex * batchSize).Take(batchSize).ToList();
var response = await m_httpClient.PostAsJsonAsync("/api/SyncSales/publish/tblCreditpurchase", batch);
if (response.IsSuccessStatusCode)
{
Console.WriteLine($"Sent batch {batchIndex + 1}, Count: {batch.Count}");
}
}
//Set last sync date
await m_httpClient.PostAsJsonAsync("/api/SyncSales/setsyncdate", syncTimestamp);
}
private async Task SyncCustomerAccounts()
{
var responseMessage = await m_httpClient.GetAsync("/api/SyncSales/lastsyncdate/customeraccounts");
if (!responseMessage.IsSuccessStatusCode)
{
return;
}
DateTime date = await responseMessage.Content.ReadFromJsonAsync<DateTime>();
IEnumerable<CustomerAccount> modifiedCart = await m_salesService.FetchCustomerAccount(date, "BRID0");
SyncTimestamp syncTimestamp = new SyncTimestamp { TableName = "customeraccounts", Timestamp = DateTime.Now.AddSeconds(-10) };
int batchSize = 200;
int totalItems = modifiedCart.Count();
int batches = (totalItems + batchSize - 1) / batchSize; // Calculate total batches
for (int batchIndex = 0; batchIndex < batches; batchIndex++)
{
List<CustomerAccount> batch = modifiedCart.Skip(batchIndex * batchSize).Take(batchSize).ToList();
var response = await m_httpClient.PostAsJsonAsync("/api/SyncSales/publish/tblCustomerAccount", batch);
if (response.IsSuccessStatusCode)
{
Console.WriteLine($"Sent batch {batchIndex + 1}, Count: {batch.Count}");
}
}
//Set last sync date
await m_httpClient.PostAsJsonAsync("/api/SyncSales/setsyncdate", syncTimestamp);
}
private async Task SyncCustomerPurchases()
{
var responseMessage = await m_httpClient.GetAsync("/api/SyncSales/lastsyncdate/tblcustomerpurchases");
if (!responseMessage.IsSuccessStatusCode)
{
return;
}
DateTime date = await responseMessage.Content.ReadFromJsonAsync<DateTime>();
IEnumerable<TblCustomerPurchase> modifiedCart = await m_salesService.FetchCustomerPurchase(date, "BRID0");
SyncTimestamp syncTimestamp = new SyncTimestamp { TableName = "tblcustomerpurchases", Timestamp = DateTime.Now.AddSeconds(-10) };
int batchSize = 200;
int totalItems = modifiedCart.Count();
int batches = (totalItems + batchSize - 1) / batchSize; // Calculate total batches
for (int batchIndex = 0; batchIndex < batches; batchIndex++)
{
List<TblCustomerPurchase> batch = modifiedCart.Skip(batchIndex * batchSize).Take(batchSize).ToList();
var response = await m_httpClient.PostAsJsonAsync("/api/SyncSales/publish/CustomerPurchase", batch);
if (response.IsSuccessStatusCode)
{
Console.WriteLine($"Sent batch {batchIndex + 1}, Count: {batch.Count}");
}
}
//Set last sync date
await m_httpClient.PostAsJsonAsync("/api/SyncSales/setsyncdate", syncTimestamp);
}
private async Task SyncDiscountLogs()
{
var responseMessage = await m_httpClient.GetAsync("/api/SyncSales/lastsyncdate/tbldiscountlogs");
if (!responseMessage.IsSuccessStatusCode)
{
return;
}
DateTime date = await responseMessage.Content.ReadFromJsonAsync<DateTime>();
IEnumerable<TblDiscountLog> modifiedCart = await m_salesService.FetchDiscountLogs(date, "BRID0");
SyncTimestamp syncTimestamp = new SyncTimestamp { TableName = "tbldiscountlogs", Timestamp = DateTime.Now.AddSeconds(-10) };
int batchSize = 200;
int totalItems = modifiedCart.Count();
int batches = (totalItems + batchSize - 1) / batchSize; // Calculate total batches
for (int batchIndex = 0; batchIndex < batches; batchIndex++)
{
List<TblDiscountLog> batch = modifiedCart.Skip(batchIndex * batchSize).Take(batchSize).ToList();
var response = await m_httpClient.PostAsJsonAsync("/api/SyncSales/publish/DiscountLogs", batch);
if (response.IsSuccessStatusCode)
{
Console.WriteLine($"Sent batch {batchIndex + 1}, Count: {batch.Count}");
}
}
//Set last sync date
await m_httpClient.PostAsJsonAsync("/api/SyncSales/setsyncdate", syncTimestamp);
}
private async Task SyncDeliveryDetails()
{
var responseMessage = await m_httpClient.GetAsync("/api/SyncSales/lastsyncdate/tblDeliverydetails");
if (!responseMessage.IsSuccessStatusCode)
{
return;
}
DateTime date = await responseMessage.Content.ReadFromJsonAsync<DateTime>();
IEnumerable<TblDeliveryDetail> modifiedCart = await m_salesService.FetchDeliveryDetails(date, "BRID0");
SyncTimestamp syncTimestamp = new SyncTimestamp { TableName = "tbldeliverydetails", Timestamp = DateTime.Now.AddSeconds(-10) };
int batchSize = 200;
int totalItems = modifiedCart.Count();
int batches = (totalItems + batchSize - 1) / batchSize; // Calculate total batches
for (int batchIndex = 0; batchIndex < batches; batchIndex++)
{
List<TblDeliveryDetail> batch = modifiedCart.Skip(batchIndex * batchSize).Take(batchSize).ToList();
var response = await m_httpClient.PostAsJsonAsync("/api/SyncSales/publish/tblDeliverydetails", batch);
if (response.IsSuccessStatusCode)
{
Console.WriteLine($"Sent batch {batchIndex + 1}, Count: {batch.Count}");
}
}
//Set last sync date
await m_httpClient.PostAsJsonAsync("/api/SyncSales/setsyncdate", syncTimestamp);
}
private async Task SynctblDeliveryhead()
{
var responseMessage = await m_httpClient.GetAsync("/api/SyncSales/lastsyncdate/tblDeliveryhead");
if (!responseMessage.IsSuccessStatusCode)
{
return;
}
DateTime date = await responseMessage.Content.ReadFromJsonAsync<DateTime>();
IEnumerable<TblDeliveryHead> modifiedCart = await m_salesService.FetchDeliveryHead(date, "BRID0");
SyncTimestamp syncTimestamp = new SyncTimestamp { TableName = "tbldeliveryhead", Timestamp = DateTime.Now.AddSeconds(-10) };
int batchSize = 200;
int totalItems = modifiedCart.Count();
int batches = (totalItems + batchSize - 1) / batchSize; // Calculate total batches
for (int batchIndex = 0; batchIndex < batches; batchIndex++)
{
List<TblDeliveryHead> batch = modifiedCart.Skip(batchIndex * batchSize).Take(batchSize).ToList();
var response = await m_httpClient.PostAsJsonAsync("/api/SyncSales/publish/tblDeliveryhead", batch);
if (response.IsSuccessStatusCode)
{
Console.WriteLine($"Sent batch {batchIndex + 1}, Count: {batch.Count}");
}
}
//Set last sync date
await m_httpClient.PostAsJsonAsync("/api/SyncSales/setsyncdate", syncTimestamp);
}
private async Task SyncDeliveryRecipient()
{
var responseMessage = await m_httpClient.GetAsync("/api/SyncSales/lastsyncdate/tblDeliveryrecipients");
if (!responseMessage.IsSuccessStatusCode)
{
return;
}
DateTime date = await responseMessage.Content.ReadFromJsonAsync<DateTime>();
IEnumerable<TblDeliveryRecipient> modifiedCart = await m_salesService.FetchDeliveryRecipients(date, "BRID0");
SyncTimestamp syncTimestamp = new SyncTimestamp { TableName = "tbldeliveryrecipients", Timestamp = DateTime.Now.AddSeconds(-10) };
int batchSize = 200;
int totalItems = modifiedCart.Count();
int batches = (totalItems + batchSize - 1) / batchSize; // Calculate total batches
for (int batchIndex = 0; batchIndex < batches; batchIndex++)
{
List<TblDeliveryRecipient> batch = modifiedCart.Skip(batchIndex * batchSize).Take(batchSize).ToList();
var response = await m_httpClient.PostAsJsonAsync("/api/SyncSales/publish/tblDeliveryrecipient", batch);
if (response.IsSuccessStatusCode)
{
Console.WriteLine($"Sent batch {batchIndex + 1}, Count: {batch.Count}");
}
}
//Set last sync date
await m_httpClient.PostAsJsonAsync("/api/SyncSales/setsyncdate", syncTimestamp);
}
private async Task SyncInvoice()
{
var responseMessage = await m_httpClient.GetAsync("/api/SyncSales/lastsyncdate/tblinvoice");
if (!responseMessage.IsSuccessStatusCode)
{
return;
}
DateTime date = await responseMessage.Content.ReadFromJsonAsync<DateTime>();
IEnumerable<TblInvoice> modifiedCart = await m_salesService.FetchInvoice(date, "BRID0");
SyncTimestamp syncTimestamp = new SyncTimestamp { TableName = "tblinvoice", Timestamp = DateTime.Now.AddSeconds(-10) };
int batchSize = 200;
int totalItems = modifiedCart.Count();
int batches = (totalItems + batchSize - 1) / batchSize; // Calculate total batches
for (int batchIndex = 0; batchIndex < batches; batchIndex++)
{
List<TblInvoice> batch = modifiedCart.Skip(batchIndex * batchSize).Take(batchSize).ToList();
var response = await m_httpClient.PostAsJsonAsync("/api/SyncSales/publish/tblinvoice", batch);
if (response.IsSuccessStatusCode)
{
Console.WriteLine($"Sent batch {batchIndex + 1}, Count: {batch.Count}");
}
}
//Set last sync date
await m_httpClient.PostAsJsonAsync("/api/SyncSales/setsyncdate", syncTimestamp);
}
}
}