public async Task <IActionResult> OpenArticle(StockEntryValueDto stockEntryDto)
        {
            StockEntryValue existingEntry = await _repo.GetStockEntryValue(stockEntryDto.Id);

            if (!await _authService.IsPermitted(User, existingEntry.EnvironmentId,
                                                PermissionFlags.IsOwner | PermissionFlags.CanScan | PermissionFlags.EditArticleSettings))
            {
                return(Unauthorized());
            }

            stockEntryDto.OpenedOn = stockEntryDto.OpenedOn.AddMinutes(stockEntryDto.ClientTimezoneOffset);
            if (existingEntry.AmountOnStock == 1)
            {
                _mapper.Map(stockEntryDto, existingEntry);
                if (!await _repo.SaveAll())
                {
                    throw new Exception("Error saving the Data");
                }

                await _repo.WriteActivityLog(ActivityLogAction.Open, User, existingEntry.EnvironmentId, existingEntry.ArticleId,
                                             existingEntry.AmountRemaining.ToString(CultureInfo.CurrentCulture));

                return(NoContent());
            }

            var newEntry = _mapper.Map <StockEntryValue>(stockEntryDto);

            newEntry.Id            = 0;
            newEntry.AmountOnStock = 1;

            existingEntry.AmountOnStock--;

            await _repo.WriteActivityLog(ActivityLogAction.Open, User, existingEntry.EnvironmentId, existingEntry.ArticleId,
                                         newEntry.AmountRemaining.ToString(CultureInfo.CurrentCulture));

            if (await _repo.CreateStockEntryValue(newEntry))
            {
                return(NoContent());
            }

            throw new Exception("Error saving the Data");
        }
        public void OpenArticle()
        {
            ResetDb();
            SeedNewArticle("9999", "xxx", "keine", "Stk.", "Migros");
            SeedArticleUserSetting("9999", "Zu Hause von Tom", 1, 1);
            SeedStockEntry("9999", "Zu Hause von Tom", false, 2, DateTime.Now, DateTime.Now, 1);

            var sevDto = new StockEntryValueDto
            {
                Id                   = 1,
                EnvironmentId        = 2,
                AmountOnStock        = 2,
                ExpireDate           = DateTime.Now,
                ArticleId            = 1,
                IsOpened             = false,
                OpenedOn             = DateTime.Now,
                AmountRemaining      = 0.5f,
                ClientTimezoneOffset = 0
            };

            LoginResponseObject loginSepp = WebClient.Login("sepp", "P@ssw0rd");
            HttpResponseMessage resultA   = WebClient.PutAsJsonAsync("articles/OpenArticle", sevDto).Result;

            LoginResponseObject loginFritz = WebClient.Login("fritz", "P@ssw0rd");
            HttpResponseMessage resultB    = WebClient.PutAsJsonAsync("articles/OpenArticle", sevDto).Result;

            LoginResponseObject loginTom = WebClient.Login("tom", "P@ssw0rd");
            HttpResponseMessage resultC  = WebClient.PutAsJsonAsync("articles/OpenArticle", sevDto).Result;

            Assert.NotNull(loginSepp);
            Assert.NotNull(loginTom);
            Assert.NotNull(loginFritz);
            Assert.Equal(HttpStatusCode.Unauthorized, resultA.StatusCode);
            Assert.Equal(HttpStatusCode.Unauthorized, resultB.StatusCode);
            Assert.Equal(HttpStatusCode.NoContent, resultC.StatusCode);
        }