示例#1
0
        /// <summary>
        /// The perform task.
        /// </summary>
        /// <param name="value">
        /// The value.
        /// </param>
        /// <returns>
        /// The <see cref="Attempt"/>.
        /// </returns>
        public override Attempt <IShipment> PerformTask(IShipment value)
        {
            var unfulfilled = Order.UnfulfilledItems(MerchelloContext).Where(x => x.BackOrder == false && KeysToShip.Contains(x.Key)).ToArray();

            if (unfulfilled.Count() != Order.Items.Count(x => KeysToShip.Contains(x.Key)))
            {
                return(Attempt <IShipment> .Fail(new InvalidOperationException("The order contains items that are either on back order or cannot be shipped.")));
            }

            foreach (var item in unfulfilled)
            {
                value.Items.Add(item);
            }

            return(Attempt <IShipment> .Succeed(value));
        }
        /// <summary>
        /// The perform task.
        /// </summary>
        /// <param name="value">
        /// The value.
        /// </param>
        /// <returns>
        /// The <see cref="Attempt"/>.
        /// </returns>
        public override Attempt <IShipment> PerformTask(IShipment value)
        {
            var trackableItems = Order.InventoryTrackedItems().Where(x => KeysToShip.Contains(x.Key)).ToArray();

            var trackableKeys = trackableItems.Select(x => x.ExtendedData.GetProductVariantKey()).ToArray();

            var variants = trackableKeys.Any() ?
                           _productVariantService.GetByKeys(trackableKeys).ToArray() :
                           Enumerable.Empty <IProductVariant>().ToArray();

            if (variants.Any())
            {
                foreach (var item in trackableItems)
                {
                    var variant = variants.FirstOrDefault(x => x.Key == item.ExtendedData.GetProductVariantKey());
                    if (variant == null)
                    {
                        return(Attempt <IShipment> .Fail(new NullReferenceException("A ProductVariant reference in the order could not be found")));
                    }

                    var inventory = variant.CatalogInventories.FirstOrDefault(x => x.CatalogKey == item.ExtendedData.GetWarehouseCatalogKey());
                    if (inventory == null)
                    {
                        return(Attempt <IShipment> .Fail(new NullReferenceException("An inventory record could not be found for an order line item")));
                    }

                    inventory.Count -= item.Quantity;
                }

                if (trackableItems.Any())
                {
                    _productVariantService.Save(variants);
                }
            }

            // persist the shipment and update the line items
            if (value.ShipMethodKey == Guid.Empty)
            {
                value.ShipMethodKey = null;
            }
            _shipmentService.Save(value);

            return(Attempt <IShipment> .Succeed(value));
        }