<commit_before>﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using Nwazet.Commerce.Models;
using Nwazet.Commerce.Services;
using Orchard;
using Orchard.ContentManagement;
using Orchard.ContentManagement.Drivers;
using Orchard.ContentManagement.Handlers;
using Orchard.Environment.Extensions;
using Nwazet.Commerce.ViewModels;

namespace Nwazet.Commerce.Drivers {
    [OrchardFeature("Nwazet.Commerce")]
    public class ProductPartDriver : ContentPartDriver<ProductPart> {
        private readonly IWorkContextAccessor _wca;
        private readonly IPriceService _priceService;
        private readonly IEnumerable<IProductAttributesDriver> _attributeProviders;
        private readonly ITieredPriceProvider _tieredPriceProvider;

        public ProductPartDriver(
            IWorkContextAccessor wca,
            IPriceService priceService,
            IEnumerable<IProductAttributesDriver> attributeProviders,
            ITieredPriceProvider tieredPriceProvider = null) {

            _wca = wca;
            _priceService = priceService;
            _attributeProviders = attributeProviders;
            _tieredPriceProvider = tieredPriceProvider;
        }

        protected override string Prefix {
            get { return "NwazetCommerceProduct"; }
        }

        protected override DriverResult Display(
            ProductPart part, string displayType, dynamic shapeHelper) {
            var inventory = GetInventory(part);
            var discountedPriceQuantity = _priceService.GetDiscountedPrice(new ShoppingCartQuantityProduct(1, part));
            var priceTiers = _tieredPriceProvider != null ? _tieredPriceProvider.GetPriceTiers(part) : null;
            var discountedPriceTiers = _priceService.GetDiscountedPriceTiers(part);
            var shapes = new List<DriverResult>();

            shapes.Add(ContentShape(
                "Parts_Product",
                () => shapeHelper.Parts_Product(
                    Sku: part.Sku,
                    Price: part.Price,
                    DiscountedPrice: discountedPriceQuantity.Price,
                    DiscountComment: discountedPriceQuantity.Comment,
                    Inventory: inventory,
                    OutOfStockMessage: part.OutOfStockMessage,
                    AllowBackOrder: part.AllowBackOrder,
                    Weight: part.Weight,
                    Size: part.Size,
                    ShippingCost: part.ShippingCost,
                    IsDigital: part.IsDigital,
                    MinimumOrderQuantity: part.MinimumOrderQuantity,
                    ContentPart: part
                    )
                ));
            if (part.Inventory > 0 || part.AllowBackOrder) {
                shapes.Add(ContentShape(
                        "Parts_Product_AddButton",
                        () => {
                            // Get attributes and add them to the add to cart shape
                            var attributeShapes = _attributeProviders
                                .Select(p => p.GetAttributeDisplayShape(part.ContentItem, shapeHelper))
                                .ToList();
                            return shapeHelper.Parts_Product_AddButton(
                                ProductId: part.Id,
                                MinimumOrderQuantity: part.MinimumOrderQuantity,
                                ProductAttributes: attributeShapes);
                        })
                    );
            }
            if (priceTiers != null) {
                shapes.Add(ContentShape(
                        "Parts_Product_PriceTiers",
                        () => {
                            return shapeHelper.Parts_Product_PriceTiers(
                                PriceTiers: priceTiers,
                                DiscountedPriceTiers: discountedPriceTiers
                                );
                        })
                    );
            }
            return Combined(shapes.ToArray());
        }

        private int GetInventory(ProductPart part) {
            IBundleService bundleService;
            var inventory = part.Inventory;
            if (_wca.GetContext().TryResolve(out bundleService) && part.Has<BundlePart>()) {
                var bundlePart = part.As<BundlePart>();
                var ids = bundlePart.ProductIds.ToList();
                if (!ids.Any()) return 0;
                part.Inventory = inventory =
                    bundleService
                        .GetProductQuantitiesFor(bundlePart)
                        .Min(p => p.Product.Inventory/p.Quantity);
            }
            return inventory;
        }

        //GET
        protected override DriverResult Editor(ProductPart part, dynamic shapeHelper) {
            var allowTieredPricingOverride = false;
            var productSettings = _wca.GetContext().CurrentSite.As<ProductSettingsPart>();

            if (productSettings != null) allowTieredPricingOverride = productSettings.AllowProductOverrides;

            part.Weight = Math.Round(part.Weight, 3);
            part.Inventory = GetInventory(part);
            return ContentShape("Parts_Product_Edit",
                () => shapeHelper.EditorTemplate(
                    TemplateName: "Parts/Product",
                    Model: new ProductEditorViewModel {
                        Product = part,
                        AllowProductOverrides = allowTieredPricingOverride,
                        PriceTiers = part.PriceTiers<commit_msg>Not persisting inventory info for bundles, as it's recalculated every time.<commit_after>﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using Nwazet.Commerce.Models;
using Nwazet.Commerce.Services;
using Orchard;
using Orchard.ContentManagement;
using Orchard.ContentManagement.Drivers;
using Orchard.ContentManagement.Handlers;
using Orchard.Environment.Extensions;
using Nwazet.Commerce.ViewModels;

namespace Nwazet.Commerce.Drivers {
    [OrchardFeature("Nwazet.Commerce")]
    public class ProductPartDriver : ContentPartDriver<ProductPart> {
        private readonly IWorkContextAccessor _wca;
        private readonly IPriceService _priceService;
        private readonly IEnumerable<IProductAttributesDriver> _attributeProviders;
        private readonly ITieredPriceProvider _tieredPriceProvider;

        public ProductPartDriver(
            IWorkContextAccessor wca,
            IPriceService priceService,
            IEnumerable<IProductAttributesDriver> attributeProviders,
            ITieredPriceProvider tieredPriceProvider = null) {

            _wca = wca;
            _priceService = priceService;
            _attributeProviders = attributeProviders;
            _tieredPriceProvider = tieredPriceProvider;
        }

        protected override string Prefix {
            get { return "NwazetCommerceProduct"; }
        }

        protected override DriverResult Display(
            ProductPart part, string displayType, dynamic shapeHelper) {
            var inventory = GetInventory(part);
            var discountedPriceQuantity = _priceService.GetDiscountedPrice(new ShoppingCartQuantityProduct(1, part));
            var priceTiers = _tieredPriceProvider != null ? _tieredPriceProvider.GetPriceTiers(part) : null;
            var discountedPriceTiers = _priceService.GetDiscountedPriceTiers(part);
            var shapes = new List<DriverResult>();

            shapes.Add(ContentShape(
                "Parts_Product",
                () => shapeHelper.Parts_Product(
                    Sku: part.Sku,
                    Price: part.Price,
                    DiscountedPrice: discountedPriceQuantity.Price,
                    DiscountComment: discountedPriceQuantity.Comment,
                    Inventory: inventory,
                    OutOfStockMessage: part.OutOfStockMessage,
                    AllowBackOrder: part.AllowBackOrder,
                    Weight: part.Weight,
                    Size: part.Size,
                    ShippingCost: part.ShippingCost,
                    IsDigital: part.IsDigital,
                    MinimumOrderQuantity: part.MinimumOrderQuantity,
                    ContentPart: part
                    )
                ));
            if (part.Inventory > 0 || part.AllowBackOrder) {
                shapes.Add(ContentShape(
                        "Parts_Product_AddButton",
                        () => {
                            // Get attributes and add them to the add to cart shape
                            var attributeShapes = _attributeProviders
                                .Select(p => p.GetAttributeDisplayShape(part.ContentItem, shapeHelper))
                                .ToList();
                            return shapeHelper.Parts_Product_AddButton(
                                ProductId: part.Id,
                                MinimumOrderQuantity: part.MinimumOrderQuantity,
                                ProductAttributes: attributeShapes);
                        })
                    );
            }
            if (priceTiers != null) {
                shapes.Add(ContentShape(
                        "Parts_Product_PriceTiers",
                        () => {
                            return shapeHelper.Parts_Product_PriceTiers(
                                PriceTiers: priceTiers,
                                DiscountedPriceTiers: discountedPriceTiers
                                );
                        })
                    );
            }
            return Combined(shapes.ToArray());
        }

        private int GetInventory(ProductPart part) {
            IBundleService bundleService;
            var inventory = part.Inventory;
            if (_wca.GetContext().TryResolve(out bundleService) && part.Has<BundlePart>()) {
                var bundlePart = part.As<BundlePart>();
                var ids = bundlePart.ProductIds.ToList();
                if (!ids.Any()) return 0;
                inventory =
                    bundleService
                        .GetProductQuantitiesFor(bundlePart)
                        .Min(p => p.Product.Inventory/p.Quantity);
            }
            return inventory;
        }

        //GET
        protected override DriverResult Editor(ProductPart part, dynamic shapeHelper) {
            var allowTieredPricingOverride = false;
            var productSettings = _wca.GetContext().CurrentSite.As<ProductSettingsPart>();

            if (productSettings != null) allowTieredPricingOverride = productSettings.AllowProductOverrides;

            part.Weight = Math.Round(part.Weight, 3);
            part.Inventory = GetInventory(part);
            return ContentShape("Parts_Product_Edit",
                () => shapeHelper.EditorTemplate(
                    TemplateName: "Parts/Product",
                    Model: new ProductEditorViewModel {
                        Product = part,
                        AllowProductOverrides = allowTieredPricingOverride,
                        PriceTiers = part.PriceTiers
{'fraction_non_alphanumeric': 0.06826584903958878, 'fraction_numerical': 0.0007214356569573452, 'mean_word_length': 1.655015561407709, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '214271', 'n_tokens_mistral': 2650, 'n_tokens_neox': 2470, 'n_words': 502}