Exemplo n.º 1
0
        public void OnUpdated(Shipment entity)
        {
            var orderItems = entity.ShipmentItems.Select(x => x.OrderItem).ToList();

            foreach (var orderItem in orderItems)
            {
                _purchaseAccountant.EvaluateOrderStatus(orderItem.Order);
            }

            //if the shipment status is InTransit/Returned, that means we should update the inventory, as the item has been shipped/returned
            if (entity.ShipmentStatus == ShipmentStatus.InTransit || entity.ShipmentStatus == ShipmentStatus.Returned)
            {
                var warehouseId       = entity.WarehouseId;
                var shippedProductIds = orderItems.Select(x => x.ProductId).ToList();
                var inventories       = _warehouseInventoryService.GetByProducts(shippedProductIds, warehouseId).ToList();
                foreach (var shippedItem in entity.ShipmentItems)
                {
                    var quantityShipped  = shippedItem.Quantity;
                    var shippedProductId = shippedItem.OrderItem.ProductId;
                    var variantId        = shippedItem.OrderItem.ProductVariantId;
                    var inventory        = shippedItem.OrderItem.ProductVariantId > 0
                        ? inventories.FirstOrDefault(x => x.ProductVariantId == variantId)
                        : inventories.FirstOrDefault(x => x.ProductId == shippedProductId);
                    if (inventory == null)
                    {
                        continue; //weird it's not there. but we can't do anything for this. something is wrong. this shouldn't have hit
                    }
                    if (entity.ShipmentStatus == ShipmentStatus.InTransit)
                    {
                        inventory.ReservedQuantity -= quantityShipped;
                        inventory.TotalQuantity    -= quantityShipped;
                    }
                    else
                    {
                        inventory.TotalQuantity += quantityShipped;
                    }
                    _warehouseInventoryService.Update(inventory);
                }
            }

            //update shipment history
            //get the history
            var shipmentHistoryItems = _shipmentStatusHistoryService.Get(x => x.ShipmentId == entity.Id).OrderByDescending(x => x.Id).ToList();

            //if the current status is already there as the latest one, no need to do anything,
            if (shipmentHistoryItems.Any() && shipmentHistoryItems.First().ShipmentStatus ==
                entity.ShipmentStatus)
            {
                return;
            }
            //we'll add this to the history now
            var shipmentHistory = new ShipmentHistory()
            {
                CreatedOn      = DateTime.UtcNow,
                ShipmentStatus = entity.ShipmentStatus,
                ShipmentId     = entity.Id
            };

            _shipmentStatusHistoryService.Insert(shipmentHistory);
        }
Exemplo n.º 2
0
        private bool CanReturnOrder(Order order, out IList <OrderItem> orderItems, out DateTime lastReturnDate)
        {
            orderItems     = new List <OrderItem>();
            lastReturnDate = DateTime.UtcNow;
            if (order.DisableReturns || order.OrderStatus != OrderStatus.Complete)
            {
                return(false);
            }
            //get previous return requests for this order
            var returnRequests = _returnRequestService.Get(x => x.OrderId == order.Id).ToList();

            //check if there are ready return request available
            if (order.OrderStatus == OrderStatus.Complete)
            {
                //get the date of shipment that was delivered last
                //get the shipmenthistory
                var orderShipmentIds = order.Shipments.Select(x => x.Id).ToList();
                var historyItems     = _shipmentStatusHistoryService.Get(x => orderShipmentIds.Contains(x.ShipmentId)).ToList();
                var deliveryDate     = historyItems.Where(x => x.ShipmentStatus == ShipmentStatus.Delivered)
                                       .Max(x => x.CreatedOn);

                //check if any order item is returnable
                foreach (var orderItem in order.OrderItems)
                {
                    if (!orderItem.Product.AllowReturns)
                    {
                        continue;
                    }
                    //are there any return requests created for this order item already?
                    if (returnRequests.Any(x => x.OrderItemId == orderItem.Id))
                    {
                        continue;
                    }
                    var allowReturnDays = orderItem.Product.DaysForReturn;
                    var passedDays      = DateTime.UtcNow.Subtract(deliveryDate).Days;
                    if (allowReturnDays < passedDays)
                    {
                        continue;
                    }
                    orderItems.Add(orderItem);
                }

                lastReturnDate = orderItems.Any() ? deliveryDate.AddDays(orderItems.Max(x => x.Product.DaysForReturn)) : DateTime.UtcNow;
            }
            return(orderItems.Any());
        }