Ejemplo n.º 1
0
        public ActionResult Add(string name, string itemId, string parentItemId, decimal?quantity, string[] relatedItemId = null)
        {
            decimal qty = 1;

            if (quantity.HasValue)
            {
                qty = quantity.Value;
            }

            if (String.IsNullOrEmpty(name))
            {
                name = CartHelper.CartName;
            }

            var helper = GetCartHelper(name);

            var catalogItem = _catalogClient.GetItem(itemId);
            var parentItem  = !string.IsNullOrEmpty(parentItemId) ? _catalogClient.GetItem(parentItemId) : null;

            var addedLineItems = new List <LineItemModel>();

            var addedLineItem = DoAddToCart(name, qty, catalogItem, parentItem);

            if (addedLineItem != null)
            {
                addedLineItems.Add(new LineItemModel(addedLineItem, catalogItem, parentItem, helper.Cart.BillingCurrency));
            }

            if (relatedItemId != null && relatedItemId.Length > 0)
            {
                addedLineItems.AddRange(from relItemId in relatedItemId
                                        let relItem = _catalogClient.GetItem(relItemId)
                                                      let relatedItem = DoAddToCart(name, 1, relItem, null)
                                                                        where relatedItem != null
                                                                        select new LineItemModel(relatedItem, relItem, null, helper.Cart.BillingCurrency));
            }

            if (Request.UrlReferrer != null)
            {
                UserHelper.CustomerSession.LastShoppingPage = Request.UrlReferrer.AbsoluteUri;
            }

            //helper.ClearCache();
            //helper = GetCartHelper(name);

            var results = new CartJsonModel
            {
                CartSubTotal  = StoreHelper.FormatCurrency(helper.Cart.Subtotal, helper.Cart.BillingCurrency),
                CartTotal     = StoreHelper.FormatCurrency(helper.Cart.Total, helper.Cart.BillingCurrency),
                CartCount     = helper.LineItems.Count(),
                LineItemsView = null,
                CartName      = name
            };

            results.Messages.Add(new MessageModel(string.Format("{0} items added to your {1}".Localize(), addedLineItems.Sum(li => li.Quantity), name.Localize())));

            return(Json(results));
        }
Ejemplo n.º 2
0
        private void AdjustStockItemQuantity(LineItem lineItem)
        {
            if (String.IsNullOrEmpty(lineItem.CatalogItemId) ||
                lineItem.CatalogItemId.StartsWith("@"))
            {
                return;
            }

            var catalogHelper = new CatalogClient(CatalogRepository, null, CustomerSessionService, CacheRepository, InventoryRepository);
            var item          = catalogHelper.GetItem(lineItem.CatalogItemId);

            if (item != null && item.TrackInventory)
            {
                var repo      = InventoryRepository;
                var inventory = catalogHelper.GetItemInventory(lineItem.CatalogItemId, lineItem.FulfillmentCenterId);
                if (inventory != null)
                {
                    if (AdjustStockInventoryQuantity(lineItem, inventory))
                    {
                        repo.UnitOfWork.Commit();
                    }
                    else
                    {
                        throw new InvalidWorkflowException(string.Format("Failed to adjust inventory for lineItem {0}", lineItem.LineItemId));
                    }
                }
            }
        }
Ejemplo n.º 3
0
        public ActionResult ItemVariations(string itemId, string name, string[] selections = null,
                                           string variation = null)
        {
            var variations        = _catalogClient.GetItemRelations(itemId);
            var selectedVariation = string.IsNullOrEmpty(variation) ? null : _catalogClient.GetItem(variation, StoreHelper.CustomerSession.CatalogId);
            var model             = new VariationsModel(variations, selections, selectedVariation);

            return(PartialView(name, model));
        }
        private void ValidateItems()
        {
            //We don't need to validate quantity in the wish list
            var orderForms     = CurrentOrderGroup.OrderForms.ToArray();
            var lineItems      = orderForms.SelectMany(x => x.LineItems.ToArray());
            var validLineItems = lineItems.Where(x => !String.IsNullOrEmpty(x.CatalogItemId) && !x.CatalogItemId.StartsWith("@"));

            foreach (var lineItem in validLineItems)
            {
                var  changeQtyReason = new List <string>();
                bool isUsingBackordersAndPreorders;
                var  newQty = GetNewLineItemQty(lineItem, changeQtyReason, out isUsingBackordersAndPreorders);
                if (newQty == 0)
                {
                    // Remove item if it reached this stage
                    RegisterWarning(WorkflowMessageCodes.ITEM_NOT_AVAILABLE, lineItem, String.Format("Item \"{0}\" has been removed from the cart because it is no longer available", lineItem.DisplayName));
                    DeleteLineItemFromShipments(lineItem);
                    lineItem.OrderForm.LineItems.Remove(lineItem);
                }
                else
                {
                    var delta = lineItem.Quantity - newQty;
                    if (delta != 0)
                    {
                        lineItem.Quantity -= delta;
                        ChangeShipmentsLineItemQty(lineItem, delta);
                        RegisterWarning(WorkflowMessageCodes.ITEM_QTY_CHANGED, lineItem, String.Format("Item \"{0}\" quantity has been changed", lineItem.DisplayName));
                    }

                    // update Shipment Status if LineItem InStockQuantity was insufficient and Backorders/Preorders were used.
                    if (isUsingBackordersAndPreorders)
                    {
                        var catalogHelper = new CatalogClient(CatalogRepository, null, null, CacheRepository, null);
                        var catalogItem   = catalogHelper.GetItem(lineItem.CatalogItemId);
                        if (catalogItem != null && catalogItem.TrackInventory)
                        {
                            var allShipmentContainingLineItem = orderForms
                                                                .SelectMany(x => x.Shipments)
                                                                .Where(x => x.ShipmentItems.Select(si => si.LineItem).Contains(lineItem))
                                                                .ToList();
                            allShipmentContainingLineItem.ForEach(x => x.Status = ShipmentStatus.AwaitingInventory.ToString());
                        }
                    }
                }
            }
        }
Ejemplo n.º 5
0
        private void CalculateTaxes(OrderGroup order)
        {
            var catalogHelper = new CatalogClient(CatalogRepository, null, null, CacheRepository, null);

            foreach (var form in order.OrderForms)
            {
                decimal totalTaxes = 0;

                foreach (var shipment in form.Shipments)
                {
                    decimal itemTax     = 0;
                    decimal shippingTax = 0;
                    foreach (var shipItem in shipment.ShipmentItems)
                    {
                        var lineItem = shipItem.LineItem;

                        if (lineItem == null)
                        {
                            continue;
                        }

                        decimal lineItemTaxTotal = 0m;

                        // Try getting an address
                        var address = GetAddressByName(form, shipment.ShippingAddressId);
                        if (address != null)                         // no taxes if there is no address
                        {
                            // Try getting an item
                            var item = catalogHelper.GetItem(lineItem.CatalogItemId);
                            if (item != null)                             // no entry, no tax category, no tax
                            {
                                var taxCategory = item.TaxCategory;
                                // calls the method that returns all the tax values
                                var taxes = GetTaxes(taxCategory, Thread.CurrentThread.CurrentCulture.Name, address.CountryCode, address.StateProvince, address.PostalCode, address.RegionName, string.Empty, string.Empty, address.City);

                                if (taxes != null && taxes.Any())
                                {
                                    foreach (var tax in taxes)
                                    {
                                        if (tax != null)
                                        {
                                            var taxAmount = Math.Round(lineItem.PlacedPrice * (tax.Percentage / 100), 2) * lineItem.Quantity;
                                            if (tax.Tax.TaxType == (int)TaxTypes.SalesTax)
                                            {
                                                itemTax          += taxAmount;
                                                lineItemTaxTotal += taxAmount;
                                                totalTaxes       += taxAmount;
                                            }
                                            else if (tax.Tax.TaxType == (int)TaxTypes.ShippingTax)
                                            {
                                                shippingTax += Math.Round(shipment.ShippingCost * (tax.Percentage / 100), 2);
                                                totalTaxes  += shippingTax;
                                            }
                                        }
                                    }
                                }
                            }
                        }
                        lineItem.TaxTotal = lineItemTaxTotal;
                    }

                    //TODO Round taxes to money
                    shipment.ItemTaxTotal     = itemTax;
                    shipment.ShippingTaxTotal = shippingTax;
                }
                form.TaxTotal = totalTaxes;
            }
            order.TaxTotal = order.OrderForms.Sum(x => x.TaxTotal);
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Creates the catalog model.
        /// </summary>
        /// <param name="itemId">The item identifier.</param>
        /// <param name="parentItemId">The parent item identifier.</param>
        /// <param name="associationType">Type of the association.</param>
        /// <param name="forcedActive">if set to <c>true</c> [forced active].</param>
        /// <param name="responseGroups">The response groups.</param>
        /// <param name="display">The display.</param>
        /// <param name="byItemCode">if set to <c>true</c> gets item by code.</param>
        /// <returns>
        /// CatalogItemWithPriceModel.
        /// </returns>
        public static CatalogItemWithPriceModel CreateCatalogModel(string itemId,
            string parentItemId = null,
            string associationType = null,
            bool forcedActive = false,
            ItemResponseGroups responseGroups = ItemResponseGroups.ItemLarge,
            ItemDisplayOptions display = ItemDisplayOptions.ItemLarge,
            bool byItemCode = false)
        {

            var dbItem = CatalogClient.GetItem(itemId, responseGroups,
                                              UserHelper.CustomerSession.CatalogId, bycode: byItemCode);
            if (dbItem != null)
            {

                if (dbItem.IsActive || forcedActive)
                {
                    PriceModel priceModel = null;
                    PropertySet propertySet = null;
                    //ItemRelation[] variations = null;
                    ItemAvailabilityModel itemAvaiability = null;

                    if (display.HasFlag(ItemDisplayOptions.ItemPropertySets))
                    {
                        propertySet = CatalogClient.GetPropertySet(dbItem.PropertySetId);
                        //variations = CatalogClient.GetItemRelations(itemId);
                    }

                    var itemModel = CreateItemModel(dbItem, propertySet);

                    if (display.HasFlag(ItemDisplayOptions.ItemAvailability))
                    {
                        var fulfillmentCenter = UserHelper.StoreClient.GetCurrentStore().FulfillmentCenterId;
                        var availability = CatalogClient.GetItemAvailability(dbItem.ItemId, fulfillmentCenter);
                        itemAvaiability = new ItemAvailabilityModel(availability);
                    }

                    if (display.HasFlag(ItemDisplayOptions.ItemPrice))
                    {
                        var lowestPrice = PriceListClient.GetLowestPrice(dbItem.ItemId, itemAvaiability != null ? itemAvaiability.MinQuantity : 1);
                        var outlines = OutlineBuilder.BuildCategoryOutline(CatalogClient.CustomerSession.CatalogId, dbItem.ItemId);
                        var tags = new Hashtable
							{
								{
									"Outline",
                                    outlines.ToString()
                                }
							};
                        priceModel = MarketingHelper.GetItemPriceModel(dbItem, lowestPrice, tags);
                        itemModel.CatalogOutlines = outlines;

                        // get the category name
                        if (outlines.Count > 0)
                        {
                            var outline = outlines[0];
                            if (outline.Categories.Count > 0)
                            {
                                var category = outline.Categories.OfType<Category>().Reverse().FirstOrDefault();
                                if (category != null)
                                {
                                    itemModel.CategoryName = category.Name;
                                }
                            }
                        }
                    }

                    itemModel.ParentItemId = parentItemId;

                    return string.IsNullOrEmpty(associationType)
                               ? new CatalogItemWithPriceModel(itemModel, priceModel, itemAvaiability)
                               : new AssociatedCatalogItemWithPriceModel(itemModel, priceModel, itemAvaiability, associationType);
                }
            }

            return null;
        }