Exemplo n.º 1
0
        public async Task <IActionResult> MoveStocks([FromBody] StockMoveRequestViewModel model)
        {
            var user = await userRepo.GetByLogin(User.Identity.Name);

            if (model.shopFrom == 0 || model.shopTo == 0)
            {
                return(BadRequest("Необходимо указать склады."));
            }
            try
            {
                await service.MoveStocks(user, model);

                return(Ok("Товары перемещены."));
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
Exemplo n.º 2
0
        public async Task MoveStocks(UserProfile user, StockMoveRequestViewModel model)
        {
            var dtNow = DateTime.Now;
            var order = new Order
            {
                isOrder     = false,
                report_date = dtNow,
                shop_id     = model.shopFrom
            };

            order.OrderDetails = model.products.Select(p => new OrderDetail
            {
                prod_id = p.id,
                cost    = 0,
                count   = p.value
            }).ToList();

            try
            {
                var id = await ordersRepo.AddCancellationAsync(order);

                var orderDal = (await ordersRepo.GetCancellationsByShopIdInDateRange(order.shop_id, dtNow.AddSeconds(-1), dtNow)).Last(p => p.id == id);

                foreach (var orderDetail in order.OrderDetails)
                {
                    var cost = costRepo.GetByProdId(orderDetail.prod_id).FirstOrDefault();
                    orderDetail.cost = cost != null && cost.value.HasValue ? cost.value.Value : 0;
                }

                await strategy.UpdateAverageCost(Direction.Cancellation, orderDal);

                order.isOrder = true;
                order.shop_id = model.shopTo;
                id            = await ordersRepo.AddCancellationAsync(order);

                orderDal = (await ordersRepo.GetCancellationsByShopIdInDateRange(order.shop_id, dtNow.AddSeconds(-1), dtNow)).Last(p => p.id == id);
                await strategy.UpdateAverageCost(Direction.Order, orderDal);
            }
            catch (Exception)
            {
                throw new Exception("Перемещение не удалось.");
            }
        }