示例#1
0
        public async Task UpdateAllInventories(int orderId, UserModified userModified)
        {
            var orderItems = await _unitOfWork.OrderItemRepository
                             .FindByCondition(oi => oi.OrderId == orderId)
                             .AsNoTracking()
                             .ToListAsync();

            var inventories = await _unitOfWork.InventoryRepository
                              .FindAll()
                              .AsNoTracking()
                              .OrderByDescending(t => t.CreatedDate)
                              .ToListAsync();

            var iventoryModels        = new List <Inventory>();
            var listProductIdsUpdated = inventories.Select(i => i.ProductId).ToList();

            if (inventories.Any())
            {
                inventories = inventories.GroupBy(i => i.ProductId)
                              .Select(grp => grp.FirstOrDefault())
                              .ToList();
                foreach (var inventory in inventories)
                {
                    var orderItemsFound = orderItems.Where(o => o.ProductId == inventory.ProductId).ToList();
                    iventoryModels.Add(CreateInventoryModel(inventory.ProductId,
                                                            inventory.AmountOfImport,
                                                            orderItemsFound.Sum(o => o.Amount),
                                                            userModified));
                }
            }
            if (orderItems.Any())
            {
                var orderItemsGroupByProductId = orderItems.Where(o => !listProductIdsUpdated.Contains(o.ProductId))
                                                 .GroupBy(i => i.ProductId)
                                                 .Select(grp => grp.FirstOrDefault())
                                                 .ToList();
                foreach (var inventory in orderItemsGroupByProductId)
                {
                    var orderItemsFound = orderItems.Where(o => o.ProductId == inventory.ProductId).ToList();
                    iventoryModels.Add(CreateInventoryModel(inventory.ProductId,
                                                            0,
                                                            orderItemsFound.Sum(o => o.Amount),
                                                            userModified));
                }
            }

            _unitOfWork.InventoryRepository.AddRange(iventoryModels);
            await _unitOfWork.Commit();
        }
示例#2
0
        public async Task <ApiJsonResult> Create(int?tenantId, long userId, OrderItemDto data)
        {
            var result          = new ApiJsonResult();
            var orderItemModels = new List <OrderItem>();

            foreach (var product in data.Products)
            {
                var orderItem = new OrderItem
                {
                    TenantId       = tenantId.Value,
                    ProductId      = product.Id,
                    Amount         = product.Amount,
                    Price          = product.Price,
                    CreatedDate    = DateTime.Now,
                    UserModifiedId = userId
                };
                orderItemModels.Add(orderItem);
            }
            var orderModel = new Order
            {
                CustomerId = data.CustomerId,
                SellerId   = data.SellerId,
                TenantId   = tenantId.Value,
                OrderNote  = new OrderNote
                {
                    TenantId       = tenantId.Value,
                    Note           = data.OrderNote,
                    CreatedDate    = DateTime.Now,
                    UserModifiedId = userId
                },
                OrderCode      = Constants.GenerateCode(),
                OrderItems     = orderItemModels,
                SaleDate       = data.SaleDateUtc,
                CreatedDate    = DateTime.Now,
                UserModifiedId = userId,
                OrderType      = data.OrderType,
                OrderTotal     = data.OrderTotal,
                OrderDiscount  = data.OrderDiscount,
                OrderPaid      = data.OrderPaid,
                OrderDebt      = data.OrderDebt,
                StoreId        = data.StoreId,
                OrderStatusId  = OrderStatus.Ordered
            };

            switch (data.OrderType)
            {
            case OrderType.Order:
                if (data.CarrierId.HasValue)
                {
                    var shipment = new Shipment
                    {
                        TenantId        = tenantId.Value,
                        DeliveryDateUtc = data.DeliveryDate,
                        Note            = data.ShipmentNote,
                        Shipper         = data.Shipper,
                        CarrierId       = data.CarrierId,
                        CreatedDate     = DateTime.Now,
                        UserModifiedId  = userId
                    };
                    orderModel.Shipment = shipment;
                }
                break;

            case OrderType.Sale:
            case OrderType.ReturnSupplier:
                orderModel.PaymentMethodId = data.PaymentMethodId;
                break;

            default:
                break;
            }
            _salesDbContext.Add(orderModel);
            await _unitOfWork.Commit();

            var userModified = new UserModified
            {
                TenantId = tenantId,
                UserId   = userId
            };
            await _inventoryService.UpdateAllInventories(orderModel.Id, userModified);

            return(result);
        }