/// <summary>
        /// Creates a line item of a particular type for a shipment rate quote
        /// </summary>
        /// <typeparam name="T">The type of the line item to create</typeparam>
        /// <param name="shipmentRateQuote">The <see cref="ShipmentRateQuote"/> to be translated to a line item</param>
        /// <returns>A <see cref="LineItemBase"/> of type T</returns>
        public static T AsLineItemOf <T>(this IShipmentRateQuote shipmentRateQuote) where T : LineItemBase
        {
            var extendedData = new ExtendedDataCollection();

            extendedData.AddShipment(shipmentRateQuote.Shipment);

            var ctrValues = new object[]
            {
                EnumTypeFieldConverter.LineItemType.Shipping.TypeKey,
                shipmentRateQuote.ShipmentLineItemName(),
                shipmentRateQuote.ShipMethod.ServiceCode,     // TODO this may not be unique (SKU) once multiple shipments are exposed
                1,
                shipmentRateQuote.Rate,
                extendedData
            };

            var attempt = ActivatorHelper.CreateInstance <LineItemBase>(typeof(T), ctrValues);

            if (attempt.Success)
            {
                return(attempt.Result as T);
            }

            MultiLogHelper.Error <ILineItem>("Failed instiating a line item from shipmentRateQuote", attempt.Exception);

            throw attempt.Exception;
        }
        /// <summary>
        /// Converts a line item of one type to a line item of another type
        /// </summary>
        /// <typeparam name="T">The specific type of <see cref="ILineItem"/></typeparam>
        /// <param name="lineItem">The line item</param>
        /// <returns>A <see cref="LineItemBase"/> of type T</returns>
        public static T AsLineItemOf <T>(this ILineItem lineItem) where T : class, ILineItem
        {
            var ctrValues = new object[]
            {
                lineItem.LineItemTfKey,
                lineItem.Name,
                lineItem.Sku,
                lineItem.Quantity,
                lineItem.Price,
                lineItem.ExtendedData
            };


            var attempt = ActivatorHelper.CreateInstance <LineItemBase>(typeof(T), ctrValues);

            if (!attempt.Success)
            {
                LogHelper.Error <ILineItem>("Failed to convertion ILineItem", attempt.Exception);
                throw attempt.Exception;
            }

            attempt.Result.Exported = lineItem.Exported;

            return(attempt.Result as T);
        }
Exemplo n.º 3
0
 /// <summary>
 /// Mandates that the specified condition is true, otherwise throws an exception specified in <typeparamref name="TException"/>.
 /// </summary>
 /// <typeparam name="TException">The type of the exception.</typeparam>
 /// <param name="condition">if set to <c>true</c>, throws exception <typeparamref name="TException"/>.</param>
 /// <exception cref="Exception">An exception of type <typeparamref name="TException"/> is raised if the condition is false.</exception>
 public static void That <TException>(bool condition) where TException : Exception, new()
 {
     if (!condition)
     {
         throw ActivatorHelper.CreateInstance <TException>();
     }
 }
        /// <summary>
        /// Itemizes the items in an invoice.
        /// </summary>
        /// <param name="invoice">
        /// The invoice.
        /// </param>
        /// <returns>
        /// The <see cref="InvoiceItemItemization"/>.
        /// </returns>
        /// <exception cref="Exception">
        /// Throws an exception if the itemization strategy could not be instantiated.
        /// </exception>
        public static InvoiceItemItemization ItemizeItems(this IInvoice invoice)
        {
            var type = MerchelloConfiguration.Current.GetStrategyElement(Constants.StrategyTypeAlias.InvoiceItemizationStrategy).Type;

            var ctrArgValues = new object[] { invoice };

            var strategy = ActivatorHelper.CreateInstance <InvoiceItemizationStrategyBase>(type, ctrArgValues);


            if (strategy.Success)
            {
                return(strategy.Result.Itemize());
            }


            MultiLogHelper.Error(typeof(Extensions), "Failed to instantiate the InvoiceItemizationStrategy.", strategy.Exception);

            throw strategy.Exception;
        }
        /// <summary>
        /// Creates an instance of <see cref="LineItemCollection"/> from a serialized collection in the ExtendedDataCollection
        /// </summary>
        /// <typeparam name="T">The type of the <see cref="ILineItem"/></typeparam>
        /// <param name="extendedData">The extended data collection</param>
        /// <returns><see cref="LineItemCollection"/></returns>
        public static LineItemCollection GetLineItemCollection <T>(this ExtendedDataCollection extendedData) where T : LineItemBase
        {
            if (!extendedData.ContainsKey(Constants.ExtendedDataKeys.LineItemCollection))
            {
                return(null);
            }

            var xdoc = XDocument.Parse(extendedData.GetValue(Constants.ExtendedDataKeys.LineItemCollection));
            var lineItemCollection = new LineItemCollection();

            foreach (var element in xdoc.Descendants(Constants.ExtendedDataKeys.LineItem))
            {
                var dictionary = GetLineItemXmlValues(element.ToString());

                var extendData = new ExtendedDataCollection(dictionary[Constants.ExtendedDataKeys.ExtendedData]);

                var ctrValues = new object[]
                {
                    new Guid(dictionary[Constants.ExtendedDataKeys.LineItemTfKey]),
                    dictionary[Constants.ExtendedDataKeys.Name],
                    dictionary[Constants.ExtendedDataKeys.Sku],
                    int.Parse(dictionary[Constants.ExtendedDataKeys.Quantity]),
                    decimal.Parse(dictionary[Constants.ExtendedDataKeys.Price], NumberStyles.Any, CultureInfo.InvariantCulture),
                    new ExtendedDataCollection(dictionary[Constants.ExtendedDataKeys.ExtendedData])
                };

                var attempt = ActivatorHelper.CreateInstance <LineItemBase>(typeof(T).FullName, ctrValues);

                if (!attempt.Success)
                {
                    MultiLogHelper.Error <LineItemCollection>("Failed to instantiate a LineItemCollection from ExtendedData", attempt.Exception);
                    throw attempt.Exception;
                }

                attempt.Result.ContainerKey = new Guid(dictionary[Constants.ExtendedDataKeys.ContainerKey]);

                lineItemCollection.Add(attempt.Result);
            }

            return(lineItemCollection);
        }
        /// <summary>
        /// Creates a line item of a particular type for a invoiceTaxResult
        /// </summary>
        /// <typeparam name="T">The type of the line item to be created</typeparam>
        /// <param name="taxCalculationResult">The <see cref="ITaxCalculationResult"/> to be converted to a line item</param>
        /// <returns>A <see cref="ILineItem"/> representing the <see cref="ITaxCalculationResult"/></returns>
        public static T AsLineItemOf <T>(this ITaxCalculationResult taxCalculationResult) where T : LineItemBase
        {
            var ctrValues = new object[]
            {
                EnumTypeFieldConverter.LineItemType.Tax.TypeKey,
                taxCalculationResult.Name,
                "Tax", // TODO this may not e unqiue (SKU),
                1,
                taxCalculationResult.TaxAmount,
                taxCalculationResult.ExtendedData
            };

            var attempt = ActivatorHelper.CreateInstance <LineItemBase>(typeof(T), ctrValues);

            if (attempt.Success)
            {
                return(attempt.Result as T);
            }

            MultiLogHelper.Error <ILineItem>("Failed instiating a line item from invoiceTaxResult", attempt.Exception);

            throw attempt.Exception;
        }