public OutputInvoiceNoteListResponse Sync(SyncOutputInvoiceNoteRequest request) { OutputInvoiceNoteListResponse response = new OutputInvoiceNoteListResponse(); try { response.OutputInvoiceNotes = new List <OutputInvoiceNoteViewModel>(); if (request.LastUpdatedAt != null) { response.OutputInvoiceNotes.AddRange(unitOfWork.GetOutputInvoiceNoteRepository() .GetOutputInvoiceNotesNewerThen(request.CompanyId, (DateTime)request.LastUpdatedAt) ?.ConvertToOutputInvoiceNoteViewModelList() ?? new List <OutputInvoiceNoteViewModel>()); } else { response.OutputInvoiceNotes.AddRange(unitOfWork.GetOutputInvoiceNoteRepository() .GetOutputInvoiceNotes(request.CompanyId) ?.ConvertToOutputInvoiceNoteViewModelList() ?? new List <OutputInvoiceNoteViewModel>()); } response.Success = true; } catch (Exception ex) { response.OutputInvoiceNotes = new List <OutputInvoiceNoteViewModel>(); response.Success = false; response.Message = ex.Message; } return(response); }
public OutputInvoiceNoteListResponse Sync(SyncOutputInvoiceNoteRequest request) { OutputInvoiceNoteListResponse response = new OutputInvoiceNoteListResponse(); try { response = WpfApiHandler.SendToApi <SyncOutputInvoiceNoteRequest, OutputInvoiceNoteViewModel, OutputInvoiceNoteListResponse>(request, "Sync"); } catch (Exception ex) { response.OutputInvoiceNotes = new List <OutputInvoiceNoteViewModel>(); response.Success = false; response.Message = ex.Message; } return(response); }
public JsonResult Sync([FromBody] SyncOutputInvoiceNoteRequest request) { OutputInvoiceNoteListResponse response = new OutputInvoiceNoteListResponse(); try { response = this.OutputInvoiceNoteService.Sync(request); } catch (Exception ex) { response.Success = false; response.Message = ex.Message; } return(Json(response, new Newtonsoft.Json.JsonSerializerSettings() { Formatting = Newtonsoft.Json.Formatting.Indented })); }
//public OutputInvoiceNoteListResponse GetUnSyncedNotes(int companyId) //{ // OutputInvoiceNoteListResponse response = new OutputInvoiceNoteListResponse(); // List<OutputInvoiceNoteViewModel> viewModels = new List<OutputInvoiceNoteViewModel>(); // using (SqliteConnection db = new SqliteConnection("Filename=SirmiumERPGFC.db")) // { // db.Open(); // try // { // SqliteCommand selectCommand = new SqliteCommand( // SqlCommandSelectPart + // "FROM OutputInvoiceNotes " + // "WHERE CompanyId = @CompanyId AND IsSynced = 0 " + // "ORDER BY Id DESC;", db); // selectCommand.Parameters.AddWithValue("@CompanyId", companyId); // SqliteDataReader query = selectCommand.ExecuteReader(); // while (query.Read()) // { // int counter = 0; // OutputInvoiceNoteViewModel dbEntry = new OutputInvoiceNoteViewModel(); // dbEntry.Id = SQLiteHelper.GetInt(query, ref counter); // dbEntry.Identifier = SQLiteHelper.GetGuid(query, ref counter); // dbEntry.OutputInvoice = SQLiteHelper.GetOutputInvoice(query, ref counter); // dbEntry.Note = SQLiteHelper.GetString(query, ref counter); // dbEntry.NoteDate = SQLiteHelper.GetDateTime(query, ref counter); // dbEntry.IsSynced = SQLiteHelper.GetBoolean(query, ref counter); // dbEntry.UpdatedAt = SQLiteHelper.GetDateTime(query, ref counter); // dbEntry.CreatedBy = SQLiteHelper.GetCreatedBy(query, ref counter); // dbEntry.Company = SQLiteHelper.GetCompany(query, ref counter); // viewModels.Add(dbEntry); // } // } // catch (SqliteException error) // { // MainWindow.ErrorMessage = error.Message; // response.Success = false; // response.Message = error.Message; // response.OutputInvoiceNotes = new List<OutputInvoiceNoteViewModel>(); // return response; // } // db.Close(); // } // response.Success = true; // response.OutputInvoiceNotes = viewModels; // return response; //} #endregion #region Sync public void Sync(IOutputInvoiceNoteService OutputInvoiceNoteService, Action <int, int> callback = null) { try { SyncOutputInvoiceNoteRequest request = new SyncOutputInvoiceNoteRequest(); request.CompanyId = MainWindow.CurrentCompanyId; request.LastUpdatedAt = GetLastUpdatedAt(MainWindow.CurrentCompanyId); int toSync = 0; int syncedItems = 0; OutputInvoiceNoteListResponse response = OutputInvoiceNoteService.Sync(request); if (response.Success) { toSync = response?.OutputInvoiceNotes?.Count ?? 0; List <OutputInvoiceNoteViewModel> outputInvoiceNotesFromDB = response.OutputInvoiceNotes; using (SqliteConnection db = new SqliteConnection(SQLiteHelper.SqLiteTableName)) { db.Open(); using (var transaction = db.BeginTransaction()) { SqliteCommand deleteCommand = db.CreateCommand(); deleteCommand.CommandText = "DELETE FROM OutputInvoiceNotes WHERE Identifier = @Identifier"; SqliteCommand insertCommand = db.CreateCommand(); insertCommand.CommandText = SqlCommandInsertPart; foreach (var outputInvoiceNote in outputInvoiceNotesFromDB) { deleteCommand.Parameters.AddWithValue("@Identifier", outputInvoiceNote.Identifier); deleteCommand.ExecuteNonQuery(); deleteCommand.Parameters.Clear(); if (outputInvoiceNote.IsActive) { outputInvoiceNote.IsSynced = true; insertCommand = AddCreateParameters(insertCommand, outputInvoiceNote); insertCommand.ExecuteNonQuery(); insertCommand.Parameters.Clear(); syncedItems++; callback?.Invoke(syncedItems, toSync); } } transaction.Commit(); } db.Close(); } } else { throw new Exception(response.Message); } } catch (Exception ex) { MainWindow.ErrorMessage = ex.Message; } }