Пример #1
0
        /// <summary>
        /// Creates a <see cref="IShipment"/> without persisting it to the database.
        /// </summary>
        /// <param name="shipmentStatus">
        /// The shipment status.
        /// </param>
        /// <param name="origin">
        /// The origin.
        /// </param>
        /// <param name="destination">
        /// The destination.
        /// </param>
        /// <param name="items">
        /// The items.
        /// </param>
        /// <param name="raiseEvents">
        /// Optional boolean indicating whether or not to raise events
        /// </param>
        /// <returns>
        /// The <see cref="IShipment"/>.
        /// </returns>
        public IShipment CreateShipment(IShipmentStatus shipmentStatus, IAddress origin, IAddress destination, LineItemCollection items, bool raiseEvents = true)
        {
            Mandate.ParameterNotNull(shipmentStatus, "shipmentStatus");
            Mandate.ParameterNotNull(origin, "origin");
            Mandate.ParameterNotNull(destination, "destination");
            Mandate.ParameterNotNull(items, "items");

            // Use the visitor to filter out and validate shippable line items
            var visitor = new ShippableProductVisitor();

            items.Accept(visitor);

            var lineItemCollection = new LineItemCollection();

            foreach (var item in visitor.ShippableItems)
            {
                lineItemCollection.Add(item);
            }

            var shipment = new Shipment(shipmentStatus, origin, destination, lineItemCollection)
            {
                VersionKey = Guid.NewGuid()
            };

            if (!raiseEvents)
            {
                return(shipment);
            }

            Creating.RaiseEvent(new Events.NewEventArgs <IShipment>(shipment), this);
            return(shipment);
        }
Пример #2
0
        /// <summary>
        /// Creates a collection of shipments for the current basket
        /// </summary>
        /// <returns>
        /// A collection of <see cref="IShipment"/>.
        /// </returns>
        public override IEnumerable <IShipment> PackageShipments()
        {
            // filter basket items for shippable items
            var shippableVisitor = new ShippableProductVisitor();

            LineItemCollection.Accept(shippableVisitor);

            if (!shippableVisitor.ShippableItems.Any())
            {
                return(new List <IShipment>());
            }

            // the origin address will be the default warehouse
            // For the initial version we are only exposing a single warehouse
            var warehouse = MerchelloContext.Services.WarehouseService.GetDefaultWarehouse();
            var origin    = warehouse.AsAddress();

            ////For the initial version we are only exposing a single shipment
            var shipment = new Shipment(origin, Destination)
            {
                VersionKey = VersionKey     // this is used in cache keys
            };

            // get the variants for each of the shippable line items
            var variants =
                MerchelloContext.Services.ProductVariantService.GetByKeys(
                    shippableVisitor.ShippableItems
                    .Select(x => x.ExtendedData.GetProductVariantKey())
                    .Where(x => !Guid.Empty.Equals(x))).ToArray();

            foreach (var lineItem in shippableVisitor.ShippableItems)
            {
                // We need to know what Warehouse Catalog this product is associated with for shipping and inventory
                var variant = variants.FirstOrDefault(x => x.Key.Equals(lineItem.ExtendedData.GetProductVariantKey()));
                if (variant == null)
                {
                    throw new InvalidOperationException("This packaging strategy cannot handle null ProductVariants");
                }

                if (variant.CatalogInventories.FirstOrDefault() == null)
                {
                    LogHelper.Error <ShippableProductVisitor>(
                        "ProductVariant marked as shippable was not assoicated with a WarehouseCatalog.  Product was: "
                        + variant.Key.ToString() + " -  " + variant.Name,
                        new InvalidDataException());
                }
                else
                {
                    lineItem.ExtendedData.SetValue(
                        "merchWarehouseCatalogKey",
                        variant.CatalogInventories.First().CatalogKey.ToString());
                    shipment.Items.Add(lineItem);
                }
            }

            return(new List <IShipment> {
                shipment
            });
        }
        /// <summary>
        /// Creates a collection of shipments for the current basket
        /// </summary>
        /// <returns>
        /// A collection of <see cref="IShipment"/>.
        /// </returns>
        public override IEnumerable <IShipment> PackageShipments()
        {
            // All packaged shipments will start with a shipment status of "Quoted" as these are being used for the Shipment Rate Quote
            // NOTE:  the "Packaging" status to indicate the shipment is physically being packaged/boxed up.
            var quoted = MerchelloContext.Services.ShipmentService.GetShipmentStatusByKey(Constants.DefaultKeys.ShipmentStatus.Quoted);

            // filter basket items for shippable items
            var shippableVisitor = new ShippableProductVisitor();

            LineItemCollection.Accept(shippableVisitor);

            if (!shippableVisitor.ShippableItems.Any())
            {
                return(new List <IShipment>());
            }

            // the origin address will be the default warehouse
            // For the initial version we are only exposing a single warehouse
            var warehouse = MerchelloContext.Services.WarehouseService.GetDefaultWarehouse();
            var origin    = warehouse.AsAddress();

            ////For the initial version we are only exposing a single shipment
            var shipment = new Shipment(quoted, origin, Destination)
            {
                VersionKey = VersionKey     // this is used in cache keys
            };

            // get the variants for each of the shippable line items
            var variants =
                MerchelloContext.Services.ProductVariantService.GetByKeys(
                    shippableVisitor.ShippableItems
                    .Select(x => x.ExtendedData.GetProductVariantKey())
                    .Where(x => !Guid.Empty.Equals(x))).ToArray();

            foreach (var lineItem in shippableVisitor.ShippableItems)
            {
                // We need to know what Warehouse Catalog this product is associated with for shipping and inventory
                var variant = variants.FirstOrDefault(x => x.Key.Equals(lineItem.ExtendedData.GetProductVariantKey()));
                if (variant == null)
                {
                    throw new InvalidOperationException("This packaging strategy cannot handle null ProductVariants");
                }

                if (!variant.CatalogInventories.Any())
                {
                    MultiLogHelper.Error <ShippableProductVisitor>(
                        "ProductVariant marked as shippable was not assoicated with a WarehouseCatalog.  Product was: "
                        + variant.Key.ToString() + " -  " + variant.Name,
                        new InvalidDataException());
                }
                else
                {
                    if (lineItem.ExtendedData.GetWarehouseCatalogKey().Equals(Guid.Empty))
                    {
                        // TODO this needs to be refactored to look at the entire shipment
                        // since products could be in multiple catalogs which could have
                        // opposing shippng rules and we have the destination address.
                        lineItem.ExtendedData.SetValue(
                            Constants.ExtendedDataKeys.WarehouseCatalogKey,
                            variant.CatalogInventories.First().CatalogKey.ToString());
                    }

                    shipment.Items.Add(lineItem);
                }
            }

            return(new List <IShipment> {
                shipment
            });
        }
Пример #4
0
        /// <summary>
        /// Creates a <see cref="IShipment"/> without persisting it to the database.
        /// </summary>
        /// <param name="shipmentStatus">
        /// The shipment status.
        /// </param>
        /// <param name="origin">
        /// The origin.
        /// </param>
        /// <param name="destination">
        /// The destination.
        /// </param>
        /// <param name="items">
        /// The items.
        /// </param>
        /// <param name="raiseEvents">
        /// Optional boolean indicating whether or not to raise events
        /// </param>
        /// <returns>
        /// The <see cref="IShipment"/>.
        /// </returns>
        public IShipment CreateShipment(IShipmentStatus shipmentStatus, IAddress origin, IAddress destination, LineItemCollection items, bool raiseEvents = true)
        {
            Mandate.ParameterNotNull(shipmentStatus, "shipmentStatus");
            Mandate.ParameterNotNull(origin, "origin");
            Mandate.ParameterNotNull(destination, "destination");
            Mandate.ParameterNotNull(items, "items");

            // Use the visitor to filter out and validate shippable line items
            var visitor = new ShippableProductVisitor();
            items.Accept(visitor);

            var lineItemCollection = new LineItemCollection();

            foreach (var item in visitor.ShippableItems)
            {
                lineItemCollection.Add(item);
            }

            var shipment = new Shipment(shipmentStatus, origin, destination, lineItemCollection)
                               {
                                   VersionKey = Guid.NewGuid()
                               };

            if (!raiseEvents)
            {
                return shipment;
            }

            Creating.RaiseEvent(new Events.NewEventArgs<IShipment>(shipment), this);
            return shipment;
        }