public async Task <IActionResult> CheckOutById(int entryId, int amount)
        {
            StockEntryValue entry = await _repo.GetStockEntryValue(entryId);

            if (entry == null)
            {
                return(NotFound());
            }

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

            if (amount == 0 || entry.AmountOnStock == 1 || entry.AmountOnStock == amount)
            {
                _repo.Delete(entry);
                if (!await _repo.SaveAll())
                {
                    throw new Exception("Failed to delete the stock entry");
                }

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

                return(NoContent());
            }

            await _repo.WriteActivityLog(ActivityLogAction.CheckOut, User, entry.EnvironmentId, entry.ArticleId,
                                         amount.ToString());

            entry.AmountOnStock -= amount;
            return(await _repo.SaveAll() ? NoContent() : throw new Exception("Failed to update stock entry"));
        }
        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 async Task <IActionResult> AddStockEntry(CheckInDto checkInDto)
        {
            if (!await _authService.IsPermitted(User, checkInDto.EnvironmentId,
                                                PermissionFlags.IsOwner | PermissionFlags.CanScan))
            {
                return(Unauthorized());
            }

            checkInDto.ExpireDate = checkInDto.ExpireDate.AddMinutes(checkInDto.ClientTimezoneOffset);
            var entryValues = _mapper.Map <StockEntryValue>(checkInDto);

            entryValues.AmountRemaining = 1;

            StockEntryValue newEntry = await _repo.AddStockEntry(entryValues, checkInDto.UsualLifetime);

            var ret = _mapper.Map <ArticleDto>(newEntry.Article);

            await _repo.WriteActivityLog(ActivityLogAction.CheckIn, User, checkInDto.EnvironmentId,
                                         checkInDto.ArticleId, checkInDto.AmountOnStock.ToString());

            return(CreatedAtRoute(nameof(LookupArticle),
                                  new { controller = "Articles", barcode = checkInDto.Barcode, environmentId = checkInDto.EnvironmentId }, ret));
        }
Beispiel #4
0
        /// <summary>
        /// Fügt einen neuen Lager eintrag hinzu
        /// </summary>
        /// <param name="artBarcode">Barcode des Artikels</param>
        /// <param name="environmentName">Name der Umgebung für die der Eintrag gelten soll</param>
        /// <param name="isOpened">true wenn geöffnet</param>
        /// <param name="amount">Anzahl an Lager</param>
        /// <param name="openedOn">Datum an dem der Artikel geöffnet wurde</param>
        /// <param name="expireDate">Ablaufdatum</param>
        /// <param name="amountRemaining">Restmenge wenn ein Artikel geöffnet wird</param>
        protected void SeedStockEntry(string artBarcode, string environmentName, bool isOpened, int amount, DateTime openedOn,
                                      DateTime expireDate, float amountRemaining)
        {
            Article         article     = DbContext.Articles.FirstOrDefault(a => a.Barcode == artBarcode);
            BeepEnvironment environment = DbContext.Environments.FirstOrDefault(en => en.Name == environmentName);

            if (article == null || environment == null)
            {
                throw new Exception("Environment oder Artikel nicht gefunden");
            }

            var stockEntry = DbContext.StockEntries
                             .FirstOrDefault(se => se.EnvironmentId == environment.Id && se.ArticleId == article.Id) ??
                             new StockEntry()
            {
                Article = article, Environment = environment
            };

            var values = new StockEntryValue()
            {
                StockEntry      = stockEntry,
                AmountOnStock   = amount,
                AmountRemaining = amountRemaining,
                ExpireDate      = expireDate,
                IsOpened        = isOpened,
                OpenedOn        = openedOn
            };

            if (stockEntry.EnvironmentId == 0)
            {
                DbContext.StockEntries.Add(stockEntry);
            }
            DbContext.StockEntryValues.Add(values);

            DbContext.SaveChanges();
        }