Esempio n. 1
0
        public OutputInvoiceDocumentListResponse Sync(SyncOutputInvoiceDocumentRequest request)
        {
            OutputInvoiceDocumentListResponse response = new OutputInvoiceDocumentListResponse();

            try
            {
                response.OutputInvoiceDocuments = new List <OutputInvoiceDocumentViewModel>();

                if (request.LastUpdatedAt != null)
                {
                    response.OutputInvoiceDocuments.AddRange(unitOfWork.GetOutputInvoiceDocumentRepository()
                                                             .GetOutputInvoiceDocumentsNewerThen(request.CompanyId, (DateTime)request.LastUpdatedAt)
                                                             ?.ConvertToOutputInvoiceDocumentViewModelList() ?? new List <OutputInvoiceDocumentViewModel>());
                }
                else
                {
                    response.OutputInvoiceDocuments.AddRange(unitOfWork.GetOutputInvoiceDocumentRepository()
                                                             .GetOutputInvoiceDocuments(request.CompanyId)
                                                             ?.ConvertToOutputInvoiceDocumentViewModelList() ?? new List <OutputInvoiceDocumentViewModel>());
                }

                response.Success = true;
            }
            catch (Exception ex)
            {
                response.OutputInvoiceDocuments = new List <OutputInvoiceDocumentViewModel>();
                response.Success = false;
                response.Message = ex.Message;
            }

            return(response);
        }
        public OutputInvoiceDocumentListResponse Sync(SyncOutputInvoiceDocumentRequest request)
        {
            OutputInvoiceDocumentListResponse response = new OutputInvoiceDocumentListResponse();

            try
            {
                response = WpfApiHandler.SendToApi <SyncOutputInvoiceDocumentRequest, OutputInvoiceDocumentViewModel, OutputInvoiceDocumentListResponse>(request, "Sync");
            }
            catch (Exception ex)
            {
                response.OutputInvoiceDocuments = new List <OutputInvoiceDocumentViewModel>();
                response.Success = false;
                response.Message = ex.Message;
            }

            return(response);
        }
        public JsonResult Sync([FromBody] SyncOutputInvoiceDocumentRequest request)
        {
            OutputInvoiceDocumentListResponse response = new OutputInvoiceDocumentListResponse();

            try
            {
                response = this.OutputInvoiceDocumentService.Sync(request);
            }
            catch (Exception ex)
            {
                response.Success = false;
                response.Message = ex.Message;
            }

            return(Json(response, new Newtonsoft.Json.JsonSerializerSettings()
            {
                Formatting = Newtonsoft.Json.Formatting.Indented
            }));
        }
Esempio n. 4
0
        public void Sync(IOutputInvoiceDocumentService OutputInvoiceDocumentService, Action <int, int> callback = null)
        {
            try
            {
                SyncOutputInvoiceDocumentRequest request = new SyncOutputInvoiceDocumentRequest();
                request.CompanyId     = MainWindow.CurrentCompanyId;
                request.LastUpdatedAt = GetLastUpdatedAt(MainWindow.CurrentCompanyId);

                int toSync      = 0;
                int syncedItems = 0;

                OutputInvoiceDocumentListResponse response = OutputInvoiceDocumentService.Sync(request);
                if (response.Success)
                {
                    toSync = response?.OutputInvoiceDocuments?.Count ?? 0;
                    List <OutputInvoiceDocumentViewModel> outputInvoiceDocumentsFromDB = response.OutputInvoiceDocuments;

                    using (SqliteConnection db = new SqliteConnection(SQLiteHelper.SqLiteTableName))
                    {
                        db.Open();
                        using (var transaction = db.BeginTransaction())
                        {
                            SqliteCommand deleteCommand = db.CreateCommand();
                            deleteCommand.CommandText = "DELETE FROM OutputInvoiceDocuments WHERE Identifier = @Identifier";

                            SqliteCommand insertCommand = db.CreateCommand();
                            insertCommand.CommandText = SqlCommandInsertPart;

                            foreach (var outputInvoiceDocument in outputInvoiceDocumentsFromDB)
                            {
                                deleteCommand.Parameters.AddWithValue("@Identifier", outputInvoiceDocument.Identifier);
                                deleteCommand.ExecuteNonQuery();
                                deleteCommand.Parameters.Clear();

                                if (outputInvoiceDocument.IsActive)
                                {
                                    outputInvoiceDocument.IsSynced = true;

                                    insertCommand = AddCreateParameters(insertCommand, outputInvoiceDocument);
                                    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;
            }
        }