public InputInvoiceDocumentListResponse Sync(SyncInputInvoiceDocumentRequest request)
        {
            InputInvoiceDocumentListResponse response = new InputInvoiceDocumentListResponse();

            try
            {
                response.InputInvoiceDocuments = new List <InputInvoiceDocumentViewModel>();

                if (request.LastUpdatedAt != null)
                {
                    response.InputInvoiceDocuments.AddRange(unitOfWork.GetInputInvoiceDocumentRepository()
                                                            .GetInputInvoiceDocumentsNewerThen(request.CompanyId, (DateTime)request.LastUpdatedAt)
                                                            ?.ConvertToInputInvoiceDocumentViewModelList() ?? new List <InputInvoiceDocumentViewModel>());
                }
                else
                {
                    response.InputInvoiceDocuments.AddRange(unitOfWork.GetInputInvoiceDocumentRepository()
                                                            .GetInputInvoiceDocuments(request.CompanyId)
                                                            ?.ConvertToInputInvoiceDocumentViewModelList() ?? new List <InputInvoiceDocumentViewModel>());
                }

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

            return(response);
        }
Example #2
0
        public InputInvoiceDocumentListResponse Sync(SyncInputInvoiceDocumentRequest request)
        {
            InputInvoiceDocumentListResponse response = new InputInvoiceDocumentListResponse();

            try
            {
                response = WpfApiHandler.SendToApi <SyncInputInvoiceDocumentRequest, InputInvoiceDocumentViewModel, InputInvoiceDocumentListResponse>(request, "Sync");
            }
            catch (Exception ex)
            {
                response.InputInvoiceDocuments = new List <InputInvoiceDocumentViewModel>();
                response.Success = false;
                response.Message = ex.Message;
            }

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

            try
            {
                response = this.InputInvoiceDocumentService.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 InputInvoiceDocumentListResponse GetInputInvoiceDocumentsByInputInvoice(int companyId, Guid InputInvoiceIdentifier)
        {
            InputInvoiceDocumentListResponse     response = new InputInvoiceDocumentListResponse();
            List <InputInvoiceDocumentViewModel> InputInvoiceDocuments = new List <InputInvoiceDocumentViewModel>();

            using (SqliteConnection db = new SqliteConnection("Filename=SirmiumERPGFC.db"))
            {
                db.Open();
                try
                {
                    SqliteCommand selectCommand = new SqliteCommand(
                        SqlCommandSelectPart +
                        "FROM InputInvoiceDocuments " +
                        "WHERE InputInvoiceIdentifier = @InputInvoiceIdentifier " +
                        "AND CompanyId = @CompanyId " +
                        "ORDER BY IsSynced, Id DESC;", db);

                    selectCommand.Parameters.AddWithValue("@InputInvoiceIdentifier", InputInvoiceIdentifier);
                    selectCommand.Parameters.AddWithValue("@CompanyId", companyId);

                    SqliteDataReader query = selectCommand.ExecuteReader();

                    while (query.Read())
                    {
                        InputInvoiceDocumentViewModel dbEntry = Read(query);
                        InputInvoiceDocuments.Add(dbEntry);
                    }
                }
                catch (SqliteException error)
                {
                    MainWindow.ErrorMessage        = error.Message;
                    response.Success               = false;
                    response.Message               = error.Message;
                    response.InputInvoiceDocuments = new List <InputInvoiceDocumentViewModel>();
                    return(response);
                }
                db.Close();
            }
            response.Success = true;
            response.InputInvoiceDocuments = InputInvoiceDocuments;
            return(response);
        }
        public void Sync(IInputInvoiceDocumentService InputInvoiceDocumentService, Action <int, int> callback = null)
        {
            try
            {
                SyncInputInvoiceDocumentRequest request = new SyncInputInvoiceDocumentRequest();
                request.CompanyId     = MainWindow.CurrentCompanyId;
                request.LastUpdatedAt = GetLastUpdatedAt(MainWindow.CurrentCompanyId);

                int toSync      = 0;
                int syncedItems = 0;

                InputInvoiceDocumentListResponse response = InputInvoiceDocumentService.Sync(request);
                if (response.Success)
                {
                    toSync = response?.InputInvoiceDocuments?.Count ?? 0;
                    List <InputInvoiceDocumentViewModel> items = response.InputInvoiceDocuments;

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

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

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

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

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