Пример #1
0
        public VatListResponse Sync(SyncVatRequest request)
        {
            VatListResponse response = new VatListResponse();

            try
            {
                response.Vats = new List <VatViewModel>();

                if (request.LastUpdatedAt != null)
                {
                    response.Vats.AddRange(unitOfWork.GetVatRepository()
                                           .GetVatsNewerThen(request.CompanyId, (DateTime)request.LastUpdatedAt)
                                           ?.ConvertToVatViewModelList() ?? new List <VatViewModel>());
                }
                else
                {
                    response.Vats.AddRange(unitOfWork.GetVatRepository()
                                           .GetVats(request.CompanyId)
                                           ?.ConvertToVatViewModelList() ?? new List <VatViewModel>());
                }

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

            return(response);
        }
        public VatListResponse GetVatsByPage(int companyId, VatViewModel vatSearchObject, int currentPage = 1, int itemsPerPage = 50)
        {
            VatListResponse     response = new VatListResponse();
            List <VatViewModel> vats     = new List <VatViewModel>();

            using (SqliteConnection db = new SqliteConnection("Filename=SirmiumERPGFC.db"))
            {
                db.Open();
                try
                {
                    SqliteCommand selectCommand = new SqliteCommand(
                        SqlCommandSelectPart +
                        "FROM Vats " +
                        "WHERE (@Description IS NULL OR @Description = '' OR Description LIKE @Description) " +
                        "AND CompanyId = @CompanyId " +
                        "ORDER BY IsSynced, ServerId " +
                        "LIMIT @ItemsPerPage OFFSET @Offset;", db);

                    selectCommand.Parameters.AddWithValue("@Description", ((object)vatSearchObject.Search_Description) != null ? "%" + vatSearchObject.Search_Description + "%" : "");
                    selectCommand.Parameters.AddWithValue("@CompanyId", companyId);
                    selectCommand.Parameters.AddWithValue("@ItemsPerPage", itemsPerPage);
                    selectCommand.Parameters.AddWithValue("@Offset", (currentPage - 1) * itemsPerPage);

                    SqliteDataReader query = selectCommand.ExecuteReader();

                    while (query.Read())
                    {
                        vats.Add(Read(query));
                    }

                    selectCommand = new SqliteCommand(
                        "SELECT Count(*) " +
                        "FROM Vats " +
                        "WHERE (@Description IS NULL OR @Description = '' OR Description LIKE @Description) " +
                        "AND CompanyId = @CompanyId;", db);

                    selectCommand.Parameters.AddWithValue("@Description", ((object)vatSearchObject.Search_Description) != null ? "%" + vatSearchObject.Search_Description + "%" : "");
                    selectCommand.Parameters.AddWithValue("@CompanyId", companyId);

                    query = selectCommand.ExecuteReader();

                    if (query.Read())
                    {
                        response.TotalItems = query.GetInt32(0);
                    }
                }
                catch (SqliteException error)
                {
                    MainWindow.ErrorMessage = error.Message;
                    response.Success        = false;
                    response.Message        = error.Message;
                    response.Vats           = new List <VatViewModel>();
                    return(response);
                }
                db.Close();
            }
            response.Success = true;
            response.Vats    = vats;
            return(response);
        }
Пример #3
0
        public VatListResponse Sync(SyncVatRequest request)
        {
            VatListResponse response = new VatListResponse();

            try
            {
                response = WpfApiHandler.SendToApi <SyncVatRequest, VatViewModel, VatListResponse>(request, "Sync");
            }
            catch (Exception ex)
            {
                response.Vats    = new List <VatViewModel>();
                response.Success = false;
                response.Message = ex.Message;
            }

            return(response);
        }
        public VatListResponse GetVatsForPopup(int companyId, string filterString)
        {
            VatListResponse     response = new VatListResponse();
            List <VatViewModel> vats     = new List <VatViewModel>();

            using (SqliteConnection db = new SqliteConnection("Filename=SirmiumERPGFC.db"))
            {
                db.Open();
                try
                {
                    SqliteCommand selectCommand = new SqliteCommand(
                        SqlCommandSelectPart +
                        "FROM Vats " +
                        "WHERE (@Description IS NULL OR @Description = '' OR Description LIKE @Description OR Code LIKE @Description) " +
                        "AND CompanyId = @CompanyId " +
                        "ORDER BY IsSynced, Id DESC " +
                        "LIMIT @ItemsPerPage;", db);

                    selectCommand.Parameters.AddWithValue("@Description", ((object)filterString) != null ? "%" + filterString + "%" : "");
                    selectCommand.Parameters.AddWithValue("@CompanyId", ((object)filterString) != null ? companyId : 0);
                    selectCommand.Parameters.AddWithValue("@ItemsPerPage", 100);

                    SqliteDataReader query = selectCommand.ExecuteReader();

                    while (query.Read())
                    {
                        vats.Add(Read(query));
                    }
                }
                catch (SqliteException error)
                {
                    MainWindow.ErrorMessage = error.Message;
                    response.Success        = false;
                    response.Message        = error.Message;
                    response.Vats           = new List <VatViewModel>();
                    return(response);
                }
                db.Close();
            }
            response.Success = true;
            response.Vats    = vats;
            return(response);
        }
Пример #5
0
        public VatListResponse GetVats(int companyId)
        {
            VatListResponse response = new VatListResponse();

            try
            {
                response.Vats = unitOfWork.GetVatRepository().GetVats(companyId)
                                .ConvertToVatViewModelList();
                response.Success = true;
            }
            catch (Exception ex)
            {
                response.Vats    = new List <VatViewModel>();
                response.Success = false;
                response.Message = ex.Message;
            }

            return(response);
        }
Пример #6
0
        public VatListResponse GetVats(int companyId)
        {
            VatListResponse response = new VatListResponse();

            try
            {
                response = WpfApiHandler.GetFromApi <List <VatViewModel>, VatListResponse>("GetVats", new Dictionary <string, string>()
                {
                    { "CompanyId", companyId.ToString() }
                });
            }
            catch (Exception ex)
            {
                response.Vats    = new List <VatViewModel>();
                response.Success = false;
                response.Message = ex.Message;
            }

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

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

            JsonResult result = Json(response, new Newtonsoft.Json.JsonSerializerSettings()
            {
                Formatting            = Newtonsoft.Json.Formatting.Indented,
                ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Serialize
            });

            return(result);
        }
        public JsonResult GetVats(int companyId)
        {
            VatListResponse response = new VatListResponse();

            try
            {
                response = VatService.GetVats(companyId);
            }
            catch (Exception ex)
            {
                response.Success = false;
                response.Message = ex.Message;
                Console.WriteLine(ex.Message);
            }

            JsonResult result = Json(response, new Newtonsoft.Json.JsonSerializerSettings()
            {
                Formatting            = Newtonsoft.Json.Formatting.Indented,
                ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Serialize
            });

            return(result);
        }
        public void Sync(IVatService vatService, Action <int, int> callback = null)
        {
            try
            {
                SyncVatRequest request = new SyncVatRequest();
                request.CompanyId     = MainWindow.CurrentCompanyId;
                request.LastUpdatedAt = GetLastUpdatedAt(MainWindow.CurrentCompanyId);

                int toSync      = 0;
                int syncedItems = 0;

                VatListResponse response = vatService.Sync(request);
                if (response.Success)
                {
                    toSync = response?.Vats?.Count ?? 0;
                    List <VatViewModel> vatsFromDB = response.Vats;

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

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

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

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

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