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(); } /// /// Returns a collection of tasks to perform a sync in the SalesInterface /// /// public IEnumerable 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(); IEnumerable 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 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(); IEnumerable 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 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(); IEnumerable 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 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(); IEnumerable 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 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(); IEnumerable 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 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(); IEnumerable 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 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(); IEnumerable 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 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(); IEnumerable 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 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(); IEnumerable 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 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(); IEnumerable 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 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); } } }