Ejemplo n.º 1
0
        public ActionResult Save(WorkerDTO entity, HttpPostedFileBase logoUpload)
        {
            try
            {
                using (TransactionScope scope = new TransactionScope(TransactionScopeOption.Required))
                {
                    if (ModelState.IsValid)
                    {
                        ImageEntity imageSaving = new ImageEntity();
                        if (logoUpload != null && logoUpload.ContentLength > 0)
                        {
                            imageSaving.Status    = GenericStatus.ACTIVE;
                            imageSaving.UpdatedBy = CurrentUserId;

                            byte[] cover = null;
                            using (var memoryStream = new MemoryStream())
                            {
                                logoUpload.InputStream.CopyTo(memoryStream);
                                if (memoryStream.Length > 0)
                                {
                                    using (Image image = Bitmap.FromStream(memoryStream))
                                    {
                                        logoUpload.InputStream.CopyTo(memoryStream);
                                        if (memoryStream.Length > 0)
                                        {
                                            cover = memoryStream.ToArray();
                                        }
                                    }
                                }
                            }

                            //if (entity.LogoId > 0)
                            {
                                //imageSaving.Id = entity.LogoId;
                            }
                            //else
                            {
                                imageSaving = ImageRepository.Instance.CreateImage(imageSaving);
                            }

                            ImageRepository.Instance.SaveOrReplaceLogo(imageSaving.Id, cover);

                            entity.LogoId = imageSaving.Id;
                        }

                        if (entity.IdWorker > 0)
                        {
                            PlayerEngineDTO  player     = PlayerEngineService.Instance.GetById(entity.ExternalId);
                            WorkerTypeEntity workerType = WorkerTypeRepository.Instance.GetById(entity.WorkerTypeId);

                            UserProfileEntity userProfile = new UserProfileEntity();

                            userProfile.Id    = entity.IdUser;
                            userProfile.Name  = entity.Name;
                            userProfile.Email = entity.Email;
                            userProfile.CPF   = entity.Cpf.Replace(".", "").Replace("-", "");
                            userProfile.Phone = entity.Phone;

                            ValidateModel(userProfile);

                            WorkerEntity worker = new WorkerEntity();

                            worker.Status         = GenericStatus.ACTIVE;
                            worker.FirmId         = CurrentFirm.Id;
                            worker.ExternalFirmId = CurrentFirm.ExternalId;
                            worker.WorkerTypeId   = entity.WorkerTypeId;
                            worker.UserId         = entity.IdUser;
                            worker.Id             = entity.IdWorker;
                            worker.LogoId         = entity.LogoId;
                            worker.UpdatedBy      = CurrentUserId;
                            worker.ExternalId     = player.Id;

                            ValidateModel(worker);

                            WorkerRepository.Instance.UpdateWorker(worker);

                            UserProfileRepository.Instance.UpdateUserProfile(userProfile);

                            UserAccountEntity acc = AccountRepository.Instance.GetById(entity.IdUser);

                            acc.UserName = userProfile.Email;

                            AccountRepository.Instance.Update(acc);

                            player.Nick     = userProfile.Name;
                            player.Role     = workerType.ProfileName.ToString();
                            player.LogoId   = worker.LogoId;
                            player.Xp       = entity.TotalXp;
                            player.Email    = entity.Email;
                            player.Cpf      = entity.Cpf;
                            player.LogoPath = CurrentURL + player.LogoId;
                            player.Active   = true;
                            player.GameId   = CurrentFirm.ExternalId;
                            PlayerEngineService.Instance.CreateOrUpdate(player);

                            Success("Funcionário atualizado com sucesso.");
                            scope.Complete();
                        }
                        else
                        {
                            WorkerTypeEntity workerType = WorkerTypeRepository.Instance.GetById(entity.WorkerTypeId);

                            NewRequest request = new NewRequest();
                            AuthResult result  = new AuthResult();

                            request.Cpf      = entity.Cpf.Replace("-", "").Replace(".", "");
                            request.Name     = entity.Name;
                            request.Phone    = entity.Phone;
                            request.Email    = entity.Email;
                            request.Username = entity.Email;
                            request.Password = request.Cpf;

                            result = AccountHandler.CreateFirmUser(request, Roles.WORKER);

                            if (!AuthStatus.OK.Equals(result.AuthStatus))
                            {
                                Error(AccountHelper.HandleError(result));

                                ViewBag.Types = GetWorkerTypesToSelect(entity.WorkerTypeId);

                                ModelState.AddModelError("", "Ocorreu um erro ao salvar o funcionário.");

                                return(PartialView("_Edit", entity));
                            }

                            WorkerEntity worker = new WorkerEntity();

                            worker.Status         = GenericStatus.ACTIVE;
                            worker.FirmId         = CurrentFirm.Id;
                            worker.ExternalFirmId = CurrentFirm.ExternalId;
                            worker.UserId         = result.UserId;
                            worker.WorkerTypeId   = entity.WorkerTypeId;
                            worker.LogoId         = entity.LogoId;
                            worker.UpdatedBy      = CurrentUserId;

                            ValidateModel(worker);

                            PlayerEngineDTO player = PlayerEngineService.Instance.CreateOrUpdate(
                                new PlayerEngineDTO
                            {
                                GameId   = worker.ExternalFirmId,
                                Nick     = request.Name,
                                Role     = workerType.ProfileName.ToString(),
                                Level    = 1,
                                LogoId   = worker.LogoId,
                                Cpf      = entity.Cpf.Replace(".", "").Replace("-", ""),
                                Email    = entity.Email,
                                Xp       = 1,
                                LogoPath = CurrentURL + worker.LogoId,
                                Active   = true
                            });

                            worker.ExternalId = player.Id;

                            WorkerRepository.Instance.CreateWorker(worker);

                            Success("Funcionário criado com sucesso.");
                            scope.Complete();
                        }
                    }
                    else
                    {
                        ViewBag.Types = GetWorkerTypesToSelect(entity.WorkerTypeId);

                        ModelState.AddModelError("", "Alguns campos são obrigatórios para salvar o funcionário.");

                        return(PartialView("_Edit", entity));
                    }
                }
            }
            catch (Exception ex)
            {
                Error("Houve um erro ao salvar funcionário.");

                Logger.LogException(ex);

                ModelState.AddModelError("", "Ocorreu um erro ao tentar salvar o funcionário.");

                ViewBag.Types = GetWorkerTypesToSelect(entity.WorkerTypeId);

                return(PartialView("_Edit", entity));
            }

            return(new EmptyResult());
        }
Ejemplo n.º 2
0
        public ActionResult SaveWorkersArchive(HttpPostedFileBase workersArchive)
        {
            try
            {
                string gameId = CurrentFirm.ExternalId;

                workersArchive.SaveAs(Path.Combine(Server.MapPath("~/App_Data"), workersArchive.FileName));

                string path = Path.Combine(Server.MapPath("~/App_Data"), workersArchive.FileName);

                var archive = new ExcelQueryFactory(path);

                var rows = from x in archive.WorksheetRange("A1", "E" + rowsCount, "Workers")
                           select x;

                foreach (var row in rows)
                {
                    if (!string.IsNullOrWhiteSpace(row[0].ToString()) && !string.IsNullOrWhiteSpace(row[1].ToString()) && !string.IsNullOrWhiteSpace(row[2].ToString()) && !string.IsNullOrWhiteSpace(row[4].ToString()))
                    {
                        NewRequest request = new NewRequest();

                        AuthResult result = new AuthResult();

                        request.Cpf      = row[3].ToString();
                        request.Name     = row[0].ToString();
                        request.Phone    = row[2].ToString();
                        request.Password = "******";
                        request.Email    = row[1].ToString();
                        request.Username = row[1].ToString();

                        result = AccountHandler.CreateFirmUser(request, Roles.WORKER);

                        WorkerTypeEntity workerType = WorkerTypeRepository.Instance.GetByGameIdAndTypeName(gameId, row[4].ToString());

                        if (AuthStatus.OK.Equals(result.AuthStatus))
                        {
                            WorkerEntity worker = new WorkerEntity();

                            worker.Status         = GenericStatus.ACTIVE;
                            worker.FirmId         = CurrentFirm.Id;
                            worker.UserId         = result.UserId;
                            worker.ExternalFirmId = CurrentFirm.ExternalId;
                            worker.WorkerTypeId   = workerType.Id;
                            worker.UpdatedBy      = CurrentUserId;

                            ValidateModel(worker);

                            PlayerEngineDTO dto = new PlayerEngineDTO
                            {
                                Level    = 1,
                                Xp       = 1,
                                Nick     = request.Name,
                                Role     = workerType.ProfileName.ToString(),
                                GameId   = worker.ExternalFirmId,
                                LogoId   = worker.LogoId,
                                Cpf      = request.Cpf,
                                Email    = request.Email,
                                LogoPath = CurrentURL + worker.LogoId,
                                Active   = true
                            };

                            dto = PlayerEngineService.Instance.CreateOrUpdate(dto);

                            worker.ExternalId = dto.Id;

                            worker = WorkerRepository.Instance.CreateWorker(worker);
                        }
                    }
                }

                Success("Funcionários criados com sucesso.");
            }
            catch (Exception ex)
            {
                Logger.LogException(ex);

                ModelState.AddModelError("", "Ocorreu um erro ao tentar salvar os funcionários via arquivo.");

                return(PartialView("_WorkersArchive"));
            }

            return(new EmptyResult());
        }
Ejemplo n.º 3
0
        public ActionResult Save(FirmDTO entity, HttpPostedFileBase logoUpload)
        {
            try
            {
                using (TransactionScope scope = new TransactionScope(TransactionScopeOption.Required))
                {
                    if (ModelState.IsValid)
                    {
                        ImageEntity imageSaving = new ImageEntity();
                        if (logoUpload != null && logoUpload.ContentLength > 0)
                        {
                            imageSaving.Status    = GenericStatus.ACTIVE;
                            imageSaving.UpdatedBy = CurrentUserId;

                            byte[] cover = null;
                            using (var memoryStream = new MemoryStream())
                            {
                                logoUpload.InputStream.CopyTo(memoryStream);
                                if (memoryStream.Length > 0)
                                {
                                    using (Image image = Bitmap.FromStream(memoryStream))
                                    {
                                        logoUpload.InputStream.CopyTo(memoryStream);
                                        if (memoryStream.Length > 0)
                                        {
                                            cover = memoryStream.ToArray();
                                        }
                                    }
                                }
                            }

                            if (entity.DataInfo.LogoId > 0)
                            {
                                imageSaving.Id = entity.DataInfo.LogoId;
                            }
                            else
                            {
                                imageSaving = ImageRepository.Instance.CreateImage(imageSaving);
                            }

                            ImageRepository.Instance.SaveOrReplaceLogo(imageSaving.Id, cover);

                            entity.DataInfo.LogoId = imageSaving.Id;
                        }

                        entity.DataInfo.UpdatedBy = CurrentUserId;

                        if (entity.DataInfo.Id > 0)
                        {
                            if (entity.Status == 0)
                            {
                                entity.DataInfo.Status = GenericStatus.INACTIVE;
                            }
                            else
                            {
                                entity.DataInfo.Status = GenericStatus.ACTIVE;
                            }

                            ValidateModel(entity.DataInfo);

                            GameEngineDTO game = new GameEngineDTO
                            {
                                Adress       = entity.DataInfo.Adress,
                                City         = entity.DataInfo.City,
                                LogoId       = entity.DataInfo.LogoId,
                                Name         = entity.DataInfo.FirmName,
                                Neighborhood = entity.DataInfo.Neighborhood,
                                Phone        = entity.DataInfo.Phone,
                                Id           = entity.DataInfo.ExternalId,
                                LogoPath     = CurrentURL + entity.DataInfo.LogoId,
                                Description  = entity.DataInfo.Cnpj
                            };

                            try
                            {
                                game = GameEngineService.Instance.CreateOrUpdate(game, "*****@*****.**");
                            }
                            catch (Exception e)
                            {
                                Logger.LogError(e.Message);
                            }


                            List <WorkerDTO> workers = WorkerRepository.Instance.GetAllFromFirm(entity.DataInfo.Id);

                            if (entity.DataInfo.Status == GenericStatus.ACTIVE)
                            {
                                foreach (WorkerDTO item in workers)
                                {
                                    UserAccountEntity acc = AccountRepository.Instance.GetById(item.IdUser);

                                    acc.Status = GenericStatus.ACTIVE;

                                    AccountRepository.Instance.Update(acc);
                                }
                            }
                            else
                            {
                                foreach (WorkerDTO item in workers)
                                {
                                    UserAccountEntity acc = AccountRepository.Instance.GetById(item.IdUser);

                                    acc.Status = GenericStatus.INACTIVE;

                                    AccountRepository.Instance.Update(acc);
                                }
                            }

                            DataRepository.Instance.UpdateFirm(entity.DataInfo);
                        }
                        else
                        {
                            if (!entity.Password.Equals(entity.PasswordConfirmation))
                            {
                                Warning("As duas senhas digitadas não conferem.");
                            }

                            NewRequest request = new NewRequest();

                            AuthResult result = new AuthResult();

                            request.Cpf      = entity.ProfileInfo.CPF;
                            request.Name     = entity.ProfileInfo.Name;
                            request.Phone    = entity.ProfileInfo.Phone;
                            request.Password = entity.Password;
                            request.Email    = entity.ProfileInfo.Email;
                            request.Username = entity.Username;

                            result = AccountHandler.CreateFirmUser(request, Roles.WORKER);

                            if (!AuthStatus.OK.Equals(result.AuthStatus))
                            {
                                Error(AccountHelper.HandleError(result));

                                return(View("Create", entity));
                            }

                            ValidateModel(entity.DataInfo);

                            GameEngineDTO game = new GameEngineDTO
                            {
                                Adress       = entity.DataInfo.Adress,
                                City         = entity.DataInfo.City,
                                LogoId       = entity.DataInfo.LogoId,
                                Name         = entity.DataInfo.FirmName,
                                Neighborhood = entity.DataInfo.Neighborhood,
                                Phone        = entity.DataInfo.Phone,
                                Id           = entity.DataInfo.ExternalId,
                                Description  = entity.DataInfo.Cnpj
                            };
                            game = GameEngineService.Instance.CreateOrUpdate(game, "*****@*****.**");



                            entity.DataInfo.ExternalId = game.Id;

                            entity.DataInfo.Status = GenericStatus.ACTIVE;

                            entity.DataInfo = DataRepository.Instance.CreateFirm(entity.DataInfo);

                            WorkerEntity worker = new WorkerEntity();

                            WorkerTypeEntity workerType = new WorkerTypeEntity();

                            workerType.FirmId      = entity.DataInfo.Id;
                            workerType.ProfileName = Profiles.ADMINISTRADOR;
                            workerType.Status      = GenericStatus.ACTIVE;
                            workerType.TypeName    = "ADMINISTRADOR";
                            workerType.UpdatedBy   = CurrentUserId;

                            workerType = WorkerTypeRepository.Instance.CreateWorkerType(workerType);

                            worker.WorkerTypeId = workerType.Id;
                            worker.UserId       = result.UserId;
                            worker.UpdatedBy    = CurrentUserId;
                            worker.FirmId       = entity.DataInfo.Id;
                            worker.Status       = GenericStatus.ACTIVE;
                            worker.LogoId       = entity.DataInfo.LogoId;

                            PlayerEngineDTO player = new PlayerEngineDTO
                            {
                                Nick   = request.Name,
                                Xp     = 1,
                                Level  = 1,
                                Role   = workerType.TypeName,
                                GameId = game.Id,
                                LogoId = worker.LogoId,
                                Email  = entity.ProfileInfo.Email,
                                Cpf    = entity.ProfileInfo.CPF,
                                Active = true
                            };
                            player = PlayerEngineService.Instance.CreateOrUpdate(player, "*****@*****.**");

                            worker.ExternalId     = player.Id;
                            worker.ExternalFirmId = game.Id;

                            worker = WorkerRepository.Instance.CreateWorker(worker);
                        }

                        Success("Empresa salva com sucesso.");
                        scope.Complete();
                    }
                    else
                    {
                        Warning("Alguns campos são obrigatórios para salvar a empresa.");

                        if (entity.DataInfo.Id > 0)
                        {
                            ViewBag.Status = GetStatusToSelect(entity.DataInfo.Status == GenericStatus.ACTIVE ? 1 : 0);

                            return(View("Edit", entity));
                        }
                        else
                        {
                            return(View("Create", entity));
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Logger.LogException(ex);
                Error("Ocorreu um erro ao tentar salvar a empresa.", ex);

                if (entity.DataInfo.Id > 0)
                {
                    ViewBag.Status = GetStatusToSelect(entity.DataInfo.Status == GenericStatus.ACTIVE ? 1 : 0);

                    return(View("Edit", entity));
                }
                else
                {
                    return(View("Create", entity));
                }
            }

            return(View("Index"));
        }