private async Task <PriceSchedule> UpdateRelatedPriceSchedules(PriceSchedule updated, string token)
        {
            var ocAuth = await _oc.AuthenticateAsync();

            var initial = await _oc.PriceSchedules.GetAsync(updated.ID);

            if (initial.MaxQuantity != updated.MaxQuantity ||
                initial.MinQuantity != updated.MinQuantity ||
                initial.UseCumulativeQuantity != updated.UseCumulativeQuantity ||
                initial.RestrictedQuantity != updated.RestrictedQuantity ||
                initial.ApplyShipping != updated.ApplyShipping ||
                initial.ApplyTax != updated.ApplyTax)
            {
                var patch = new PartialPriceSchedule()
                {
                    MinQuantity           = updated.MinQuantity,
                    MaxQuantity           = updated.MaxQuantity,
                    UseCumulativeQuantity = updated.UseCumulativeQuantity,
                    RestrictedQuantity    = updated.RestrictedQuantity,
                    ApplyShipping         = updated.ApplyShipping,
                    ApplyTax = updated.ApplyTax
                };
                var relatedPriceSchedules = await _oc.PriceSchedules.ListAllAsync(filters : $"ID={initial.ID}*");

                var priceSchedulesToUpdate = relatedPriceSchedules.Where(p => p.ID != updated.ID);
                await Throttler.RunAsync(priceSchedulesToUpdate, 100, 5, p =>
                {
                    return(_oc.PriceSchedules.PatchAsync(p.ID, patch, ocAuth.AccessToken));
                });
            }
            return(await _oc.PriceSchedules.SaveAsync <PriceSchedule>(updated.ID, updated, token));
        }
        public async Task <SuperHSProduct> Get(string id, string token)
        {
            var _product = await _oc.Products.GetAsync <HSProduct>(id, token);

            var _priceSchedule = new PriceSchedule();

            try
            {
                _priceSchedule = await _oc.PriceSchedules.GetAsync <PriceSchedule>(_product.ID, token);
            }
            catch
            {
                _priceSchedule = new PriceSchedule();
            }
            var _specs    = _oc.Products.ListSpecsAsync(id, null, null, null, 1, 100, null, token);
            var _variants = _oc.Products.ListVariantsAsync <HSVariant>(id, null, null, null, 1, 100, null, token);

            try
            {
                return(new SuperHSProduct
                {
                    Product = _product,
                    PriceSchedule = _priceSchedule,
                    Specs = (await _specs).Items,
                    Variants = (await _variants).Items,
                });
            }
            catch (Exception e)
            {
                throw e;
            }
        }
Exemple #3
0
        public async Task <PriceSchedule> BuildPriceScheduleOC(PriceSchedule ps, ILogger logger)
        {
            try
            {
                logger.LogInformation("PS: " + ps.ID);
                var p = await _oc.PriceSchedules.SaveAsync <PriceSchedule>(ps.ID, ps);

                return(p);
            }
            catch (Exception ex)
            {
                logger.LogInformation($"Build Price Schedule failed {ex.Message}: {ps.ID}");
                return(null);
            }
        }
Exemple #4
0
        public static PriceSchedule MapOCPriceSchedule(SampleProduct product)
        {
            var priceSchedule = new PriceSchedule()
            {
                ApplyShipping         = true,
                ApplyTax              = true,
                ID                    = CleanOrderCloudID(product.id),
                Name                  = product.title,
                RestrictedQuantity    = true,
                UseCumulativeQuantity = false,
                PriceBreaks           = MapPriceBreaks(product)
            };

            return(priceSchedule);
        }
Exemple #5
0
        private decimal GetPrice(PriceSchedule schedule, Variant variant)
        {
            decimal totalPrice = 0;

            if (variant == null || !variant.Specs.HasItem())
            {
                if (!schedule.PriceBreaks.HasItem())
                {
                    return(totalPrice);
                }
                return(schedule.PriceBreaks[0].Price);
            }
            foreach (VariantSpec spec in variant.Specs)
            {
                if (spec.PriceMarkup == null)
                {
                    continue;
                }
                switch (spec.PriceMarkupType)
                {
                case PriceMarkupType.AmountPerQuantity:
                    totalPrice += (decimal)spec.PriceMarkup;
                    break;

                case PriceMarkupType.AmountTotal:
                    totalPrice += (decimal)spec.PriceMarkup;
                    break;

                case PriceMarkupType.NoMarkup:
                    break;

                default:
                    totalPrice += (decimal)spec.PriceMarkup;
                    break;
                }
            }
            return(totalPrice);
        }
        public async Task <SuperHSProduct> Get(string id, string token)
        {
            var _product = await _oc.Products.GetAsync <HSProduct>(id, token);

            // Get the price schedule, if it exists, if not - send empty price schedule
            var _priceSchedule = new PriceSchedule();

            try
            {
                _priceSchedule = await _oc.PriceSchedules.GetAsync <PriceSchedule>(_product.ID, token);
            }
            catch
            {
                _priceSchedule = new PriceSchedule();
            }
            var _specs       = _oc.Products.ListSpecsAsync(id, null, null, null, 1, 100, null, token);
            var _variants    = _oc.Products.ListVariantsAsync <HSVariant>(id, null, null, null, 1, 100, null, token);
            var _images      = GetProductImages(id, token);
            var _attachments = GetProductAttachments(id, token);

            try
            {
                return(new SuperHSProduct
                {
                    Product = _product,
                    PriceSchedule = _priceSchedule,
                    Specs = (await _specs).Items,
                    Variants = (await _variants).Items,
                    Images = await _images,
                    Attachments = await _attachments
                });
            }
            catch (Exception e)
            {
                throw e;
            }
        }
        public async Task <SuperHSProduct> Post(SuperHSProduct superProduct, VerifiedUserContext user)
        {
            // Determine ID up front so price schedule ID can match
            superProduct.Product.ID = superProduct.Product.ID ?? CosmosInteropID.New();

            await ValidateVariantsAsync(superProduct, user.AccessToken);

            // Create Specs
            var defaultSpecOptions = new List <DefaultOptionSpecAssignment>();
            var specRequests       = await Throttler.RunAsync(superProduct.Specs, 100, 5, s =>
            {
                defaultSpecOptions.Add(new DefaultOptionSpecAssignment {
                    SpecID = s.ID, OptionID = s.DefaultOptionID
                });
                s.DefaultOptionID = null;
                return(_oc.Specs.SaveAsync <Spec>(s.ID, s, accessToken: user.AccessToken));
            });

            // Create Spec Options
            foreach (Spec spec in superProduct.Specs)
            {
                await Throttler.RunAsync(spec.Options, 100, 5, o => _oc.Specs.SaveOptionAsync(spec.ID, o.ID, o, accessToken: user.AccessToken));
            }
            // Patch Specs with requested DefaultOptionID
            await Throttler.RunAsync(defaultSpecOptions, 100, 10, a => _oc.Specs.PatchAsync(a.SpecID, new PartialSpec {
                DefaultOptionID = a.OptionID
            }, accessToken: user.AccessToken));

            // Create Price Schedule
            PriceSchedule _priceSchedule = null;

            //All products must have a price schedule for orders to be submitted.  The front end provides a default Price of $0 for quote products that don't have one.
            superProduct.PriceSchedule.ID = superProduct.Product.ID;
            try
            {
                _priceSchedule = await _oc.PriceSchedules.CreateAsync <PriceSchedule>(superProduct.PriceSchedule, user.AccessToken);
            }
            catch (OrderCloudException ex)
            {
                if (ex.HttpStatus == System.Net.HttpStatusCode.Conflict)
                {
                    throw new Exception($"Product SKU {superProduct.PriceSchedule.ID} already exists.  Please try a different SKU.");
                }
            }
            superProduct.Product.DefaultPriceScheduleID = _priceSchedule.ID;
            // Create Product
            if (user.Supplier != null)
            {
                var supplierName = await GetSupplierNameForXpFacet(user.Supplier.ID, user.AccessToken);

                superProduct.Product.xp.Facets.Add("supplier", new List <string>()
                {
                    supplierName
                });
            }
            var _product = await _oc.Products.CreateAsync <HSProduct>(superProduct.Product, user.AccessToken);

            // Make Spec Product Assignments
            await Throttler.RunAsync(superProduct.Specs, 100, 5, s => _oc.Specs.SaveProductAssignmentAsync(new SpecProductAssignment {
                ProductID = _product.ID, SpecID = s.ID
            }, accessToken: user.AccessToken));

            // Generate Variants
            await _oc.Products.GenerateVariantsAsync(_product.ID, accessToken : user.AccessToken);

            // Patch Variants with the User Specified ID(SKU) AND necessary display xp values
            await Throttler.RunAsync(superProduct.Variants, 100, 5, v =>
            {
                var oldVariantID = v.ID;
                v.ID             = v.xp.NewID ?? v.ID;
                v.Name           = v.xp.NewID ?? v.ID;

                if ((superProduct?.Product?.Inventory?.VariantLevelTracking) == true && v.Inventory == null)
                {
                    v.Inventory = new PartialVariantInventory {
                        QuantityAvailable = 0
                    };
                }
                if (superProduct.Product?.Inventory == null)
                {
                    //If Inventory doesn't exist on the product, don't patch variants with inventory either.
                    return(_oc.Products.PatchVariantAsync(_product.ID, oldVariantID, new PartialVariant {
                        ID = v.ID, Name = v.Name, xp = v.xp
                    }, accessToken: user.AccessToken));
                }
                else
                {
                    return(_oc.Products.PatchVariantAsync(_product.ID, oldVariantID, new PartialVariant {
                        ID = v.ID, Name = v.Name, xp = v.xp, Inventory = v.Inventory
                    }, accessToken: user.AccessToken));
                }
            });


            // List Variants
            var _variants = await _oc.Products.ListVariantsAsync <HSVariant>(_product.ID, accessToken : user.AccessToken);

            // List Product Specs
            var _specs = await _oc.Products.ListSpecsAsync <Spec>(_product.ID, accessToken : user.AccessToken);

            // Return the SuperProduct
            return(new SuperHSProduct
            {
                Product = _product,
                PriceSchedule = _priceSchedule,
                Specs = _specs.Items,
                Variants = _variants.Items,
            });
        }
Exemple #8
0
        private async Task <HSProductDetail> FlattenProductDataDetailAsync(Product product, Supplier supplier, Variant variant = null)
        {
            HSProductDetail result    = new HSProductDetail();
            dynamic         productXp = product.xp;

            PriceSchedule schedule = await GetPriceSchedule(product.DefaultPriceScheduleID);

            result.SizeTier = "";
            result.Active   = product?.Active.ToString();
            if (PropertyExists(productXp, "Status"))
            {
                result.Status = productXp.Status?.ToString();
            }
            if (PropertyExists(productXp, "Note"))
            {
                result.Note = productXp.Note;
            }
            if (PropertyExists(productXp, "Tax"))
            {
                result.TaxCode        = productXp?.Tax?.Code;
                result.TaxDescription = productXp?.Tax?.Description;
                result.TaxCategory    = productXp?.Tax?.Category;
            }
            if (PropertyExists(productXp, "UnitOfMeasure"))
            {
                result.UnitOfMeasureQty = productXp?.UnitOfMeasure?.Qty;
                result.UnitOfMeasure    = productXp?.UnitOfMeasure?.Unit;
            }
            if (PropertyExists(productXp, "ProductType"))
            {
                result.ProductType = productXp.ProductType;
            }
            if (PropertyExists(productXp, "SizeTier"))
            {
                result.SizeTier = productXp.SizeTier;
            }
            if (PropertyExists(productXp, "IsResale"))
            {
                result.Resale = productXp.IsResale;
            }
            if (PropertyExists(productXp, "Currency"))
            {
                result.Currency = productXp.Currency;
            }
            if (PropertyExists(productXp, "ArtworkRequired"))
            {
                result.ArtworkRequired = productXp.ArtworkRequired;
            }
            if (supplier != null)
            {
                result.SupplierID   = supplier?.ID;
                result.SupplierName = supplier?.Name;
            }

            decimal price = GetPrice(schedule, variant);

            result.Price = price * (decimal)1.06; //SEB markup of 6%
            result.Cost  = price;
            if (product.Inventory != null)
            {
                result.VariantLevelTracking    = product.Inventory.VariantLevelTracking;
                result.InventoryOrderCanExceed = product.Inventory.OrderCanExceed;
            }
            if (schedule != null)
            {
                result.PriceScheduleID   = schedule?.ID;
                result.PriceScheduleName = schedule?.Name;
            }

            if (variant != null)
            {
                result.VariantID            = variant.ID;
                result.VariantName          = variant.Name;
                result.VariantActive        = variant.Active;
                result.SpecCombo            = variant.xp?.SpecCombo;
                result.SpecName             = variant.xp?.SpecCombo;
                result.SpecOptionValue      = GetSpecOptionValue(variant.xp?.SpecValues).Trim();
                result.SpecPriceMarkup      = GetSpecPriceMarkup(variant.xp?.SpecValues).Trim();
                result.InventoryQuantity    = variant.Inventory?.QuantityAvailable;
                result.InventoryLastUpdated = variant.Inventory?.LastUpdated;
            }
            else
            {
                result.InventoryQuantity    = product.Inventory?.QuantityAvailable;
                result.InventoryLastUpdated = product.Inventory?.LastUpdated;
            }

            return(result);
        }