コード例 #1
0
        public async Task <ActionResult <object> > DeleteGood(int id)
        {
            string msg;

            _logger.LogInformation($"Удаление номенклатуры {id}. Инициатор: " + _user.FullInfo);

            GoodObjectModel good = await _context.Goods.FindAsync(id);

            if (good == null)
            {
                msg = $"Запрашиваемая номенклатура не найдена: id={0}";
                _logger.LogError(msg);
                return(new ObjectResult(new ServerActionResult()
                {
                    Success = false,
                    Info = msg,
                    Status = StylesMessageEnum.danger.ToString()
                }));
            }

            if (good.isReadonly && _user.Role != AccessLevelUserRolesEnum.ROOT)
            {
                msg = $"Номенклатура находится статусе 'read only'. Ваш уровень доступа [{_user.Role}] не позволяет удалять/редактировать подобные объекты";
                _logger.LogError(msg);
                return(new ObjectResult(new ServerActionResult()
                {
                    Success = false,
                    Info = msg,
                    Status = StylesMessageEnum.danger.ToString()
                }));
            }

            if (await _context.GoodMovementDocumentRows.AnyAsync(x => x.GoodId == id) ||
                await _context.InventoryGoodsBalancesDeliveries.AnyAsync(x => x.GoodId == id) ||
                await _context.InventoryGoodsBalancesWarehouses.AnyAsync(x => x.GoodId == id))
            {
                msg = $"Запрашиваемая номенклатура не может быть удалена (есть ссылки в регистрах сущетсвуют остатки): id={id}";
                _logger.LogError(msg);
                return(new ObjectResult(new ServerActionResult()
                {
                    Success = false,
                    Info = msg,
                    Status = StylesMessageEnum.danger.ToString()
                }));
            }

            _context.Goods.Remove(good);
            await _context.SaveChangesAsync();

            msg = $"Номенклатура удалена: id={id}";
            _logger.LogWarning(msg);
            return(new ObjectResult(new ServerActionResult()
            {
                Success = true,
                Info = msg,
                Status = StylesMessageEnum.info.ToString()
            }));
        }
コード例 #2
0
        public async Task <ActionResult <object> > PostGood([FromRoute] int id, [FromBody] GoodObjectModel ajaxGood)
        {
            _logger.LogInformation("Создание номенклатуры. Инициатор: " + _user.FullInfo);

            ajaxGood.Name        = ajaxGood.Name.Trim();
            ajaxGood.Information = ajaxGood.Information.Trim();

            if (!ModelState.IsValid ||
                string.IsNullOrEmpty(ajaxGood.Name) ||
                ajaxGood.GroupId <= 0 ||
                ajaxGood.GroupId != id ||
                ajaxGood.UnitId <= 0 ||
                !await _context.GroupsGoods.AnyAsync(group => group.Id == ajaxGood.GroupId) ||
                !await _context.Units.AnyAsync(unit => unit.Id == ajaxGood.UnitId))
            {
                return(new ObjectResult(new ServerActionResult()
                {
                    Success = false,
                    Info = "Ошибка контроля валидности модели",
                    Status = StylesMessageEnum.danger.ToString(),
                    Tag = ModelState
                }));
            }

            if (await _context.Goods.AnyAsync(good => good.Name.ToLower() == ajaxGood.Name.ToLower()))
            {
                return(new ObjectResult(new ServerActionResult()
                {
                    Success = false,
                    Info = "Номенклатура с таким именем уже существует в бд. Придумайте уникальное",
                    Status = StylesMessageEnum.danger.ToString()
                }));
            }

            if (_user.Role != AccessLevelUserRolesEnum.ROOT)
            {
                ajaxGood.isDisabled       = false;
                ajaxGood.isGlobalFavorite = false;
                ajaxGood.isReadonly       = false;
            }

            await _context.Goods.AddAsync(ajaxGood);

            await _context.SaveChangesAsync();

            HttpContext.Response.Cookies.Append("rowsCount", (await _context.Goods.CountAsync()).ToString());
            return(new ObjectResult(new ServerActionResult()
            {
                Success = true,
                Info = "Номенклатура создана",
                Status = StylesMessageEnum.success.ToString(),
                Tag = ajaxGood.Id
            }));
        }
コード例 #3
0
        public async Task <ActionResult <object> > GetGood([FromQuery] PaginationParametersModel pagingParameters, int id)
        {
            GoodObjectModel good = await _context.Goods
                                   .Where(x => x.Id == id)
                                   .Include(x => x.Avatar)
                                   .Include(x => x.Unit)
                                   .Include(x => x.Group)
                                   .FirstOrDefaultAsync();

            if (good == null)
            {
                _logger.LogError("Номенклатура не найдена: id={0}", id);
                return(new ObjectResult(new ServerActionResult()
                {
                    Success = false,
                    Info = "Номенклатура не найдена",
                    Status = StylesMessageEnum.danger.ToString()
                }));
            }

            IQueryable <RowGoodMovementRegisterModel> goodRegisters = _context.GoodMovementDocumentRows.Where(x => x.GoodId == id);

            pagingParameters.Init(await goodRegisters.CountAsync());

            goodRegisters = goodRegisters.OrderBy(x => x.Id);
            if (pagingParameters.PageNum > 1)
            {
                goodRegisters = goodRegisters.Skip(pagingParameters.Skip);
            }

            HttpContext.Response.Cookies.Append("rowsCount", pagingParameters.CountAllElements.ToString());

            goodRegisters = goodRegisters
                            .Take(pagingParameters.PageSize)
                            .Include(x => x.BodyDocument).ThenInclude(x => x.Author);

            return(new ObjectResult(new ServerActionResult()
            {
                Success = true,
                Info = "Запрос номенклатуры обработан",
                Status = StylesMessageEnum.success.ToString(),
                Tag = new
                {
                    good.Id,
                    good.Name,
                    good.Information,
                    good.Price,
                    good.GroupId,
                    good.UnitId,

                    good.isReadonly,
                    good.isDisabled,
                    good.isGlobalFavorite,

                    noDelete = await _context.GoodMovementDocumentRows.AnyAsync(x => x.GoodId == id) || await _context.InventoryGoodsBalancesDeliveries.AnyAsync(x => x.GoodId == id) || await _context.InventoryGoodsBalancesWarehouses.AnyAsync(x => x.GoodId == id),

                    Avatar = new
                    {
                        id = good.AvatarId,
                        good.Avatar?.Name
                    },

                    Units = await _context.Units.Select(x => new
                    {
                        x.Id,
                        x.Name,
                        x.Information
                    }).ToListAsync(),
                    Groups = await _context.GroupsGoods.Select(x => new
                    {
                        x.Id,
                        x.Name,
                        x.Information
                    }).ToListAsync(),

                    Registers = await goodRegisters
                                .Select(x => new
                    {
                        x.Id,
                        Document = BodyGoodMovementDocumentModel.getDocument(x.BodyDocument, _context),
                        x.Quantity,
                        x.UnitId
                    }).ToListAsync()
                }
            }));
        }
コード例 #4
0
        public async Task <IActionResult> PutGoodModel([FromRoute] int id, [FromBody] GoodObjectModel ajaxGood)
        {
            _logger.LogInformation($"Редактирование номенклатуры [#{id}]. Инициатор: " + _user.FullInfo);
            string msg;

            ajaxGood.Name        = ajaxGood.Name.Trim();
            ajaxGood.Information = ajaxGood.Information.Trim();

            if (!ModelState.IsValid ||
                ajaxGood.GroupId < 1 ||
                string.IsNullOrEmpty(ajaxGood.Name) ||
                id != ajaxGood.Id ||
                !await _context.Goods.AnyAsync(x => x.Id == id) ||
                !await _context.GroupsGoods.AnyAsync(x => x.Id == ajaxGood.GroupId))
            {
                msg = "Ошибка в запросе";
                _logger.LogError(msg);
                return(new ObjectResult(new ServerActionResult()
                {
                    Success = false,
                    Info = msg,
                    Status = StylesMessageEnum.danger.ToString(),
                    Tag = ModelState
                }));
            }

            if (await _context.Goods.AnyAsync(x => x.Name.ToLower() == ajaxGood.Name.ToLower() && x.Id != id))
            {
                msg = "Номенклатура с таким именем уже существует в бд. Придумайте уникальное";
                _logger.LogError(msg);
                return(new ObjectResult(new ServerActionResult()
                {
                    Success = false,
                    Info = msg,
                    Status = StylesMessageEnum.danger.ToString()
                }));
            }

            GoodObjectModel goodDb = await _context.Goods.FindAsync(ajaxGood.Id);

            if (goodDb.isReadonly && _user.Role != AccessLevelUserRolesEnum.ROOT)
            {
                msg = $"Номенклатура находится статусе 'read only'. Ваш уровень доступа [{_user.Role}] не позволяет редактировать/удалять подобные объекты";
                _logger.LogError(msg);
                return(new ObjectResult(new ServerActionResult()
                {
                    Success = false,
                    Info = msg,
                    Status = StylesMessageEnum.danger.ToString()
                }));
            }

            goodDb.Name        = ajaxGood.Name;
            goodDb.Information = ajaxGood.Information;
            goodDb.Price       = ajaxGood.Price;
            goodDb.UnitId      = ajaxGood.UnitId;
            goodDb.GroupId     = ajaxGood.GroupId;

            if (_user.Role == AccessLevelUserRolesEnum.ROOT)
            {
                goodDb.isGlobalFavorite = ajaxGood.isGlobalFavorite;
                goodDb.isDisabled       = ajaxGood.isDisabled;
                goodDb.isReadonly       = ajaxGood.isReadonly;
            }

            _context.Goods.Update(goodDb);

            try
            {
                await _context.SaveChangesAsync();

                msg = $"Номенклатура [#{id}] сохранена";
                _logger.LogInformation(msg);
                return(new ObjectResult(new ServerActionResult()
                {
                    Success = true,
                    Info = msg,
                    Status = StylesMessageEnum.success.ToString()
                }));
            }
            catch (Exception ex)
            {
                msg = $"Ошибка SQL доступа во время обновления номенклатуры [#{id}]: {ex.Message}{(ex.InnerException is null ? "" : ". InnerException: " + ex.InnerException.Message)}";
                _logger.LogError(ex, msg);
                return(new ObjectResult(new ServerActionResult()
                {
                    Success = false,
                    Info = msg,
                    Status = StylesMessageEnum.danger.ToString()
                }));
            }
        }