public async Task ProceedUpdateCommand() { //update items to local db try { await salesLineDb.DeleteSalesLineWhere(GlobalVars.selectedSales.DocumentNo); ObservableCollection <Models.SalesLineTable> updatedSalesLines = new ObservableCollection <Models.SalesLineTable>(); int lineNoCounter = 1; foreach (SalesItem data in UpdateSalesItemsList) { updatedSalesLines.Add(new Models.SalesLineTable(lineNoCounter, data.DocumentNo, data.ItemId, data.ItemName, data.ActualPrice, data.Quantity, data.UnitMeasurement, data.DiscountAmount, data.DiscountPercentage)); lineNoCounter++; } await salesLineDb.InsertSalesLines(updatedSalesLines); await salesDb.Update(new Models.SalesTable(GlobalVars.selectedSales.DocumentNo, GlobalVars.selectedSales.DateCreated, GlobalVars.selectedSales.Location, GlobalVars.selectedSales.Latitude, GlobalVars.selectedSales.Longitude, totalDiscountAmount, total, GlobalVars.myDetail.SalesmanId, GlobalVars.selectedSales.CustomerId, GlobalVars.selectedSales.isGPSEnabled)); UpdateSalesItemsList.Clear(); Close(this); } catch (Exception ex) { await dialog.Show(ex.Message, "Update Failed"); } }
public async Task syncSales() { var serverDb = new ServerDatabaseService(); var salesToBeTransferred = await salesDb.GetNotTransferredSales(); if (salesToBeTransferred.Count() > 0) { var result = await serverDb.insertSales(salesToBeTransferred); if (result == 1) { //continue by inserting sales lines foreach (SalesTable x in salesToBeTransferred) { var saleslines = await salesLineDb.GetAllSalesLinesWhere(x.DocumentNo); var sltresult = await serverDb.insertSalesLines(saleslines); if (sltresult == 1) { var updateresult = await salesDb.UpdateTransferred(x.DocumentNo); x.Transferred(); } else if (sltresult == 0) { await dialog.Show("Insert Sales Lines to server database failed", "Insert to Server Failed"); } } } else { await dialog.Show("There is something wrong with uploading sales to the server", "Upload Failed"); } } //get deleted data from server var fetchedData = await serverDb.getMySales(GlobalVars.myDetail.SalesmanId); if (fetchedData != null) { var mysales = await salesDb.GetAllSalesWhere(GlobalVars.myDetail.SalesmanId); List <SalesTable> mySalesList = new List <SalesTable>(mysales); var filteredData = mySalesList.Where(x => x.isTransferred == true).ToList(); var deletedData = filteredData.Except(fetchedData.ToList()).ToList(); if (deletedData != null) { if (deletedData.Count() > 0) { try { foreach (SalesTable deletedSales in deletedData) { await salesLineDb.DeleteSalesLineWhere(deletedSales.DocumentNo); await salesDb.DeleteSales(deletedSales); } } catch (System.Exception e) { await dialog.Show(e.Message, "Delete Failed"); } } } } await loadSales(); }