Example #1
0
        public PhysicalPersonListResponse Sync(SyncPhysicalPersonRequest request)
        {
            PhysicalPersonListResponse response = new PhysicalPersonListResponse();

            try
            {
                response.PhysicalPersons = new List <PhysicalPersonViewModel>();

                if (request.LastUpdatedAt != null)
                {
                    response.PhysicalPersons.AddRange(unitOfWork.GetPhysicalPersonRepository()
                                                      .GetPhysicalPersonsNewerThen(request.CompanyId, (DateTime)request.LastUpdatedAt)
                                                      ?.ConvertToPhysicalPersonViewModelList() ?? new List <PhysicalPersonViewModel>());
                }
                else
                {
                    response.PhysicalPersons.AddRange(unitOfWork.GetPhysicalPersonRepository()
                                                      .GetPhysicalPersons(request.CompanyId)
                                                      ?.ConvertToPhysicalPersonViewModelList() ?? new List <PhysicalPersonViewModel>());
                }

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

            return(response);
        }
Example #2
0
        public PhysicalPersonListResponse GetPhysicalPersonsNewerThen(int companyId, DateTime?lastUpdateTime)
        {
            PhysicalPersonListResponse response = new PhysicalPersonListResponse();

            try
            {
                if (lastUpdateTime != null)
                {
                    response.PhysicalPersons = unitOfWork.GetPhysicalPersonRepository()
                                               .GetPhysicalPersonsNewerThen(companyId, (DateTime)lastUpdateTime)
                                               .ConvertToPhysicalPersonViewModelList();
                }
                else
                {
                    response.PhysicalPersons = unitOfWork.GetPhysicalPersonRepository()
                                               .GetPhysicalPersons(companyId)
                                               .ConvertToPhysicalPersonViewModelList();
                }
                response.Success = true;
            }
            catch (Exception ex)
            {
                response.PhysicalPersons = new List <PhysicalPersonViewModel>();
                response.Success         = false;
                response.Message         = ex.Message;
            }

            return(response);
        }
        public PhysicalPersonListResponse Sync(SyncPhysicalPersonRequest request)
        {
            PhysicalPersonListResponse response = new PhysicalPersonListResponse();

            try
            {
                response = WpfApiHandler.SendToApi <SyncPhysicalPersonRequest, PhysicalPersonViewModel, PhysicalPersonListResponse>(request, "Sync");
            }
            catch (Exception ex)
            {
                response.PhysicalPersons = new List <PhysicalPersonViewModel>();
                response.Success         = false;
                response.Message         = ex.Message;
            }

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

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

            return(Json(response, new Newtonsoft.Json.JsonSerializerSettings()
            {
                Formatting = Newtonsoft.Json.Formatting.Indented
            }));
        }
Example #5
0
        public PhysicalPersonListResponse GetPhysicalPersons(int companyId)
        {
            PhysicalPersonListResponse response = new PhysicalPersonListResponse();

            try
            {
                response.PhysicalPersons = unitOfWork.GetPhysicalPersonRepository()
                                           .GetPhysicalPersons(companyId)
                                           .ConvertToPhysicalPersonViewModelList();
                response.Success = true;
            }
            catch (Exception ex)
            {
                response.PhysicalPersons = new List <PhysicalPersonViewModel>();
                response.Success         = false;
                response.Message         = ex.Message;
            }

            return(response);
        }
        public PhysicalPersonListResponse GetPhysicalPersons(int companyId)
        {
            PhysicalPersonListResponse response = new PhysicalPersonListResponse();

            try
            {
                response = WpfApiHandler.GetFromApi <PhysicalPersonViewModel, PhysicalPersonListResponse>("GetPhysicalPersons",
                                                                                                          new Dictionary <string, string>()
                {
                    { "CompanyID", companyId.ToString() }
                });
            }
            catch (Exception ex)
            {
                response.Success = false;
                response.Message = ex.Message;
            }

            return(response);
        }
        public PhysicalPersonListResponse GetPhysicalPersonsNewerThen(int companyId, DateTime?lastUpdateTime)
        {
            PhysicalPersonListResponse response = new PhysicalPersonListResponse();

            try
            {
                response = WpfApiHandler.GetFromApi <List <PhysicalPersonViewModel>, PhysicalPersonListResponse>("GetPhysicalPersonsNewerThen",
                                                                                                                 new Dictionary <string, string>()
                {
                    { "CompanyId", companyId.ToString() },
                    { "LastUpdateTime", lastUpdateTime.ToString() }
                });
            }
            catch (Exception ex)
            {
                response.Success = false;
                response.Message = ex.Message;
            }

            return(response);
        }
        //public PhysicalPersonListResponse GetUnSyncedItems(int companyId)
        //{
        //	PhysicalPersonListResponse response = new PhysicalPersonListResponse();
        //	List<PhysicalPersonViewModel> viewModels = new List<PhysicalPersonViewModel>();

        //	using (SqliteConnection db = new SqliteConnection("Filename=SirmiumERPGFC.db"))
        //	{
        //		db.Open();
        //		try
        //		{
        //			SqliteCommand selectCommand = new SqliteCommand(
        //				SqlCommandSelectPart +
        //				"FROM PhysicalPersons " +
        //				"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;
        //				PhysicalPersonViewModel dbEntry = new PhysicalPersonViewModel();
        //				dbEntry.Id = SQLiteHelper.GetInt(query, ref counter);
        //				dbEntry.Identifier = SQLiteHelper.GetGuid(query, ref counter);
        //				dbEntry.Code = SQLiteHelper.GetString(query, ref counter);
        //				dbEntry.PhysicalPersonCode = SQLiteHelper.GetString(query, ref counter);
        //				dbEntry.Name = SQLiteHelper.GetString(query, ref counter);
        //				dbEntry.SurName = SQLiteHelper.GetString(query, ref counter);
        //				dbEntry.ConstructionSiteCode = SQLiteHelper.GetString(query, ref counter);
        //				dbEntry.ConstructionSiteName = SQLiteHelper.GetString(query, ref counter);

        //				dbEntry.DateOfBirth = SQLiteHelper.GetDateTime(query, ref counter);
        //				dbEntry.Gender = SQLiteHelper.GetInt(query, ref counter);
        //				dbEntry.Country = SQLiteHelper.GetCountry(query, ref counter);
        //				dbEntry.Region = SQLiteHelper.GetRegion(query, ref counter);
        //				dbEntry.Municipality = SQLiteHelper.GetMunicipality(query, ref counter);
        //				dbEntry.City = SQLiteHelper.GetCity(query, ref counter);
        //				dbEntry.Address = SQLiteHelper.GetString(query, ref counter);

        //				dbEntry.PassportCountry = SQLiteHelper.GetCountry(query, ref counter);
        //				dbEntry.PassportCity = SQLiteHelper.GetCity(query, ref counter);
        //				dbEntry.Passport = SQLiteHelper.GetString(query, ref counter);
        //				dbEntry.VisaFrom = SQLiteHelper.GetDateTime(query, ref counter);
        //				dbEntry.VisaTo = SQLiteHelper.GetDateTime(query, ref counter);
        //				dbEntry.ResidenceCountry = SQLiteHelper.GetCountry(query, ref counter);
        //				dbEntry.ResidenceCity = SQLiteHelper.GetCity(query, ref counter);
        //				dbEntry.ResidenceAddress = SQLiteHelper.GetString(query, ref counter);

        //				dbEntry.EmbassyDate = SQLiteHelper.GetDateTime(query, ref counter);
        //				dbEntry.VisaDate = SQLiteHelper.GetDateTimeNullable(query, ref counter);
        //				dbEntry.VisaValidFrom = SQLiteHelper.GetDateTimeNullable(query, ref counter);
        //				dbEntry.VisaValidTo = SQLiteHelper.GetDateTimeNullable(query, ref counter);
        //				dbEntry.WorkPermitFrom = SQLiteHelper.GetDateTime(query, ref counter);
        //				dbEntry.WorkPermitTo = 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.PhysicalPersons = new List<PhysicalPersonViewModel>();
        //			return response;
        //		}
        //		db.Close();
        //	}
        //	response.Success = true;
        //	response.PhysicalPersons = viewModels;
        //	return response;
        //}

        #endregion

        #region Sync

        public void Sync(IPhysicalPersonService physicalPersonService, Action <int, int> callback = null)
        {
            try
            {
                SyncPhysicalPersonRequest request = new SyncPhysicalPersonRequest();
                request.CompanyId     = MainWindow.CurrentCompanyId;
                request.LastUpdatedAt = GetLastUpdatedAt(MainWindow.CurrentCompanyId);

                int toSync      = 0;
                int syncedItems = 0;

                PhysicalPersonListResponse response = physicalPersonService.Sync(request);
                if (response.Success)
                {
                    toSync = response?.PhysicalPersons?.Count ?? 0;
                    var items = new List <PhysicalPersonViewModel>(response.PhysicalPersons);

                    using (SqliteConnection db = new SqliteConnection("Filename=SirmiumERPGFC.db"))
                    {
                        db.Open();
                        using (var transaction = db.BeginTransaction())
                        {
                            SqliteCommand deleteCommand = db.CreateCommand();
                            deleteCommand.CommandText = "DELETE FROM PhysicalPersons 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;
            }
        }
        public PhysicalPersonListResponse GetPhysicalPersonsByPage(int companyId, PhysicalPersonViewModel PhysicalPersonSearchObject, int currentPage = 1, int itemsPerPage = 50)
        {
            PhysicalPersonListResponse     response        = new PhysicalPersonListResponse();
            List <PhysicalPersonViewModel> PhysicalPersons = new List <PhysicalPersonViewModel>();

            using (SqliteConnection db = new SqliteConnection("Filename=SirmiumERPGFC.db"))
            {
                db.Open();
                try
                {
                    SqliteCommand selectCommand = new SqliteCommand(
                        SqlCommandSelectPart +
                        "FROM PhysicalPersons " +
                        "WHERE (@Name IS NULL OR @Name = '' OR Name LIKE @Name) " +
                        "AND (@SurName IS NULL OR @SurName = '' OR SurName LIKE @SurName) " +
                        "AND (@Passport IS NULL OR @Passport = '' OR Passport LIKE @Passport) " +
                        "AND (@ConstructionSite IS NULL OR @ConstructionSite = '' OR ConstructionSiteCode LIKE @ConstructionSite OR ConstructionSiteName LIKE @ConstructionSite) " +
                        "AND (@PhysicalPersonCode IS NULL OR @PhysicalPersonCode = '' OR PhysicalPersonCode LIKE @PhysicalPersonCode) " +
                        "AND CompanyId = @CompanyId " +
                        "ORDER BY IsSynced, Id DESC " +
                        "LIMIT @ItemsPerPage OFFSET @Offset;", db);

                    selectCommand.Parameters.AddWithValue("@Name", ((object)PhysicalPersonSearchObject.SearchBy_Name) != null ? "%" + PhysicalPersonSearchObject.SearchBy_Name + "%" : "");
                    selectCommand.Parameters.AddWithValue("@SurName", ((object)PhysicalPersonSearchObject.SearchBy_SurName) != null ? "%" + PhysicalPersonSearchObject.SearchBy_SurName + "%" : "");
                    selectCommand.Parameters.AddWithValue("@Passport", ((object)PhysicalPersonSearchObject.SearchBy_Passport) != null ? "%" + PhysicalPersonSearchObject.SearchBy_Passport + "%" : "");
                    selectCommand.Parameters.AddWithValue("@ConstructionSite", !String.IsNullOrEmpty(PhysicalPersonSearchObject.Search_ConstructionSite) ? "%" + PhysicalPersonSearchObject.Search_ConstructionSite + "%" : "");
                    selectCommand.Parameters.AddWithValue("@PhysicalPersonCode", ((object)PhysicalPersonSearchObject.Search_PhysicalPersonCode) != null ? "%" + PhysicalPersonSearchObject.Search_PhysicalPersonCode + "%" : "");
                    selectCommand.Parameters.AddWithValue("@CompanyId", companyId);
                    selectCommand.Parameters.AddWithValue("@ItemsPerPage", itemsPerPage);
                    selectCommand.Parameters.AddWithValue("@Offset", (currentPage - 1) * itemsPerPage);

                    SqliteDataReader query = selectCommand.ExecuteReader();

                    while (query.Read())
                    {
                        PhysicalPersonViewModel dbEntry = Read(query);
                        PhysicalPersons.Add(dbEntry);
                    }

                    response.PhysicalPersons = PhysicalPersons;

                    selectCommand = new SqliteCommand(
                        "SELECT Count(*) " +
                        "FROM PhysicalPersons " +
                        "WHERE (@Name IS NULL OR @Name = '' OR Name LIKE @Name) " +
                        "AND (@SurName IS NULL OR @SurName = '' OR SurName LIKE @SurName) " +
                        "AND (@Passport IS NULL OR @Passport = '' OR Passport LIKE @Passport) " +
                        "AND (@ConstructionSite IS NULL OR @ConstructionSite = '' OR ConstructionSiteCode LIKE @ConstructionSite OR ConstructionSiteName LIKE @ConstructionSite) " +
                        "AND (@PhysicalPersonCode IS NULL OR @PhysicalPersonCode = '' OR PhysicalPersonCode LIKE @PhysicalPersonCode) " +
                        "AND CompanyId = @CompanyId;", db);

                    selectCommand.Parameters.AddWithValue("@Name", ((object)PhysicalPersonSearchObject.SearchBy_Name) != null ? "%" + PhysicalPersonSearchObject.SearchBy_Name + "%" : "");
                    selectCommand.Parameters.AddWithValue("@SurName", ((object)PhysicalPersonSearchObject.SearchBy_SurName) != null ? "%" + PhysicalPersonSearchObject.SearchBy_SurName + "%" : "");
                    selectCommand.Parameters.AddWithValue("@Passport", ((object)PhysicalPersonSearchObject.SearchBy_Passport) != null ? "%" + PhysicalPersonSearchObject.SearchBy_Passport + "%" : "");
                    selectCommand.Parameters.AddWithValue("@ConstructionSite", !String.IsNullOrEmpty(PhysicalPersonSearchObject.Search_ConstructionSite) ? "%" + PhysicalPersonSearchObject.Search_ConstructionSite + "%" : "");
                    selectCommand.Parameters.AddWithValue("@PhysicalPersonCode", ((object)PhysicalPersonSearchObject.Search_PhysicalPersonCode) != null ? "%" + PhysicalPersonSearchObject.Search_PhysicalPersonCode + "%" : "");
                    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.PhysicalPersons = new List <PhysicalPersonViewModel>();
                    return(response);
                }
                db.Close();
            }
            response.Success         = true;
            response.PhysicalPersons = PhysicalPersons;
            return(response);
        }