示例#1
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);
        }