public override async void OnNavigatingTo(NavigationParameters parameters)
        {
            await NavigationHelper.ShowLoading();

            GetGamificationPointsResponse currentPoints = new GetGamificationPointsResponse();

            try
            {
                currentPoints.Entity = await CacheAccess.GetSecure <Points>(CacheKeys.POINTS);
            }
            catch
            {
                currentPoints = await _gamificationPointsAppService.GetCurrentGamificationPoints();

                await CacheAccess.InsertSecure <Points>(CacheKeys.POINTS, currentPoints.Entity);
            }
            if (currentPoints.Entity != null)
            {
                GamificationInvitePoints   = currentPoints.Entity.InviteFlower;
                GamificationRegisterPoints = currentPoints.Entity.RegisterNewClients;
                GamificationSalesPoints    = currentPoints.Entity.SalesNumber;
            }

            await NavigationHelper.PopLoading();

            base.OnNavigatingTo(parameters);
        }
        private async void RegisterNewClient()
        {
            if (!string.IsNullOrEmpty(ClientSelectedForEdition.Name) && !string.IsNullOrEmpty(ClientSelectedForEdition.Email) && !string.IsNullOrEmpty(ClientSelectedForEdition.Telephone) && !string.IsNullOrEmpty(ClientSelectedForEdition.Birthdate.ToString()))
            {
                try
                {
                    await NavigationHelper.ShowLoading();

                    var user = await CacheAccess.GetSecure <User>(CacheKeys.USER_KEY);

                    var userGuid = user.UsuarioId;

                    if (RegisterOrEditText.Contains("CADASTRAR"))
                    {
                        await _clientsAppService.RegisterClient(new RegisterClientRequest()
                        {
                            UsuarioId = userGuid,
                            Birthdate = ClientSelectedForEdition.Birthdate,
                            Cep       = ClientSelectedForEdition.Cep,
                            Email     = ClientSelectedForEdition.Email,
                            Name      = ClientSelectedForEdition.Name,
                            Telephone = ClientSelectedForEdition.Telephone
                        });

                        var myClients = await _clientsAppService.GetClientsByUserId(user.UsuarioId.ToString());

                        await CacheAccess.Insert <List <Client> >(CacheKeys.CLIENTS, myClients.Clients);

                        var points = new GetGamificationPointsResponse();
                        points = await _gamificationPointsAppService.GetCurrentGamificationPoints();

                        await CacheAccess.InsertSecure <Points>(CacheKeys.POINTS, points.Entity);

                        user.RegisterClientsPoints += (int)points.Entity.RegisterNewClients;

                        await _userAppService.UpdateUserPoints(new UpdateUserPointsRequest()
                        {
                            UsuarioId = userGuid,
                            AverageItensPerSalePoints = user.AverageItensPerSalePoints,
                            AverageTicketPoints       = user.AverageTicketPoints,
                            RegisterClientsPoints     = user.RegisterClientsPoints,
                            InviteAllyFlowersPoints   = user.InviteAllyFlowersPoints,
                            SalesNumberPoints         = user.SalesNumberPoints
                        });

                        await CacheAccess.InsertSecure <User>(CacheKeys.USER_KEY, user);
                        await GetParametersForChallenge();

                        TimeSpan duration = new TimeSpan(0, 0, 3);
                        if (!_hasWonTrophy)
                        {
                            UserDialogs.Instance.Toast("Parabéns! Você ganhou " + points.Entity.RegisterNewClients + " Sementes com esse Cadastro!", duration);
                        }
                        try
                        {
                            Analytics.TrackEvent(InsightsTypeEvents.ActionView, new Dictionary <string, string>
                            {
                                { InsightsPagesNames.RegisterClientsPage, InsightsActionNames.AddNewClient }
                            });
                        }
                        catch { }
                    }
                    else
                    {
                        try
                        {
                            await _clientsAppService.UpdateClientInfo(new UpdateClientInfoRequest()
                            {
                                ClienteId = ClientSelectedForEdition.ClienteId,
                                Birthdate = ClientSelectedForEdition.Birthdate,
                                Cep       = ClientSelectedForEdition.Cep,
                                Email     = ClientSelectedForEdition.Email,
                                Name      = ClientSelectedForEdition.Name,
                                Telephone = ClientSelectedForEdition.Telephone
                            });

                            var myClients = await _clientsAppService.GetClientsByUserId(user.UsuarioId.ToString());

                            await CacheAccess.Insert <List <Client> >(CacheKeys.CLIENTS, myClients.Clients);
                        }
                        catch
                        {
                            UserDialogs.Instance.Toast("Não foi possível editar sua cliente, verifique sua conexão!");
                        }
                        UserDialogs.Instance.Toast("Colecionadora editada com sucesso!");
                        try
                        {
                            Analytics.TrackEvent(InsightsTypeEvents.ActionView, new Dictionary <string, string>
                            {
                                { InsightsPagesNames.RegisterClientsPage, InsightsActionNames.EditClient }
                            });
                        }
                        catch { }
                    }
                }
                catch (Exception ex)
                {
                    if (RegisterOrEditText.Contains("CADASTRAR"))
                    {
                        UserDialogs.Instance.Toast(ex.Message);
                    }
                    else
                    {
                        UserDialogs.Instance.Toast("Não foi possível editar a Colecionadora.");
                    }
                }
                finally
                {
                    await NavigationHelper.PopLoading();

                    if (!_cameFromClientsForSale)
                    {
                        await _navigationService.NavigateAsync(NavigationSettings.MenuPrincipal);
                    }
                    else
                    {
                        await _navigationService.GoBackAsync();
                    }
                }
            }
            else
            {
                TimeSpan duration = new TimeSpan(0, 0, 2);
                _userDialogs.Toast("Insira todos os dados obrigatórios.", duration);
            }
        }
Пример #3
0
        private async void SaveSale()
        {
            TimeSpan duration = new TimeSpan(0, 0, 3);

            if (IsSaleValid())
            {
                await NavigationHelper.ShowLoading();

                if (!_isEditSale)
                {
                    CanExecuteInitial();
                    try
                    {
                        var user = await CacheAccess.GetSecure <User>(CacheKeys.USER_KEY);

                        var userGuid = user.UsuarioId;

                        Analytics.TrackEvent(InsightsTypeEvents.ActionView, new Dictionary <string, string>
                        {
                            { InsightsPagesNames.RegisterSalePage, InsightsActionNames.SaveSale }
                        });

                        await _salesAppService.InsertNewSale(new InsertNewSaleRequest
                        {
                            UsuarioId        = userGuid,
                            UserName         = user.Name,
                            ClienteId        = SaleRegistered.ClienteId,
                            ClientName       = SaleRegistered.ClientName,
                            NumberSoldPieces = SaleRegistered.NumberSoldPieces,
                            SaleDate         = DateTime.Now,
                            SalePaidValue    = SaleRegistered.SalePaidValue,
                            SaleValue        = SaleRegistered.SaleValue,
                            Description      = SaleRegistered.Description
                        });

                        var userSales = await SaveNewSaleInCache();

                        float salesValue        = 0;
                        int   salesCount        = 0;
                        int   salesNumberPieces = 0;
                        foreach (var sale in userSales.Sales)
                        {
                            salesValue        += sale.SaleValue;
                            salesNumberPieces += sale.NumberSoldPieces;
                            salesCount++;
                        }
                        int AverageTicketPoints = (int)salesValue / salesCount;
                        int AverageItensPerSale = salesNumberPieces / salesCount;

                        GetGamificationPointsResponse currentPoints = new GetGamificationPointsResponse();
                        try
                        {
                            currentPoints.Entity = await CacheAccess.GetSecure <Points>(CacheKeys.POINTS);
                        }
                        catch
                        {
                            currentPoints = await _gamificationPointsAppService.GetCurrentGamificationPoints();

                            await CacheAccess.InsertSecure <Points>(CacheKeys.POINTS, currentPoints.Entity);
                        }
                        user.AverageItensPerSalePoints = AverageItensPerSale;
                        user.AverageTicketPoints      += AverageTicketPoints;
                        user.SalesNumberPoints        += (int)currentPoints.Entity.SalesNumber;

                        await _userAppService.UpdateUserPoints(new UpdateUserPointsRequest()
                        {
                            UsuarioId = userGuid,
                            AverageItensPerSalePoints = user.AverageItensPerSalePoints,
                            AverageTicketPoints       = user.AverageTicketPoints,
                            RegisterClientsPoints     = user.RegisterClientsPoints,
                            InviteAllyFlowersPoints   = user.InviteAllyFlowersPoints,
                            SalesNumberPoints         = user.SalesNumberPoints
                        });

                        await CacheAccess.InsertSecure <User>(CacheKeys.USER_KEY, user);
                        await GetParametersForChallenge();

                        if (!_hasWonTrophy)
                        {
                            UserDialogs.Instance.Toast("Parabéns! Você ganhou " + currentPoints.Entity.SalesNumber + " Sementes com essa Venda!", duration);
                        }
                        await _navigationService.NavigateAsync(NavigationSettings.MenuPrincipal);
                    }
                    catch
                    {
                        _userDialogs.Toast("Não foi possível registrar sua venda");
                    }
                    finally
                    {
                        CanExecuteEnd();
                        await NavigationHelper.PopLoading();
                    }
                }
                else
                {
                    try
                    {
                        await _salesAppService.UpdateSale(SaleRegistered.VendaId.ToString(), SaleRegistered.SaleValue, SaleRegistered.SalePaidValue, SaleRegistered.NumberSoldPieces, SaleRegistered.Description);

                        var userSales = await SaveNewSaleInCache();

                        _userDialogs.Toast("Venda Editada com sucesso!");
                    }
                    catch
                    {
                        _userDialogs.Toast("Não foi possível editar sua venda!");
                    }
                    finally
                    {
                        await NavigationHelper.PopLoading();

                        await _navigationService.GoBackAsync();
                    }
                }
            }
            else
            {
                _userDialogs.Toast("Faltam Dados para preencher!", duration);
            }
        }