private async Task PreparePriceModel(ProductOverviewModel model, Product product, Dictionary <string, string> res, bool forceRedirectionAfterAddingToCart, bool enableShoppingCart, bool displayPrices, bool enableWishlist, bool priceIncludesTax) { #region Prepare product price var priceModel = new ProductOverviewModel.ProductPriceModel { ForceRedirectionAfterAddingToCart = forceRedirectionAfterAddingToCart }; switch (product.ProductType) { case ProductType.GroupedProduct: { #region Grouped product var associatedProducts = await _productService.GetAssociatedProducts(product.Id, _storeContext.CurrentStore.Id); //add to cart button (ignore "DisableBuyButton" property for grouped products) priceModel.DisableBuyButton = !enableShoppingCart || !displayPrices; //add to wishlist button (ignore "DisableWishlistButton" property for grouped products) priceModel.DisableWishlistButton = !enableWishlist || !displayPrices; //compare products priceModel.DisableAddToCompareListButton = !_catalogSettings.CompareProductsEnabled; //catalog price, not used in views, but it's for front developer if (product.CatalogPrice > 0) { decimal catalogPrice = await _currencyService.ConvertFromPrimaryStoreCurrency(product.CatalogPrice, _workContext.WorkingCurrency); priceModel.CatalogPrice = _priceFormatter.FormatPrice(catalogPrice, true, _workContext.WorkingCurrency, _workContext.WorkingLanguage, priceIncludesTax); } switch (associatedProducts.Count) { case 0: { } break; default: { //we have at least one associated product //compare products priceModel.DisableAddToCompareListButton = !_catalogSettings.CompareProductsEnabled; if (displayPrices) { //find a minimum possible price decimal?minPossiblePrice = null; Product minPriceProduct = null; foreach (var associatedProduct in associatedProducts) { //calculate for the maximum quantity (in case if we have tier prices) var tmpPrice = (await _priceCalculationService.GetFinalPrice(associatedProduct, _workContext.CurrentCustomer, decimal.Zero, true, int.MaxValue)).finalPrice; if (!minPossiblePrice.HasValue || tmpPrice < minPossiblePrice.Value) { minPriceProduct = associatedProduct; minPossiblePrice = tmpPrice; } } if (minPriceProduct != null && !minPriceProduct.CustomerEntersPrice) { if (minPriceProduct.CallForPrice) { priceModel.OldPrice = null; priceModel.Price = res["Products.CallForPrice"]; } else if (minPossiblePrice.HasValue) { //calculate prices decimal finalPriceBase = (await _taxService.GetProductPrice(minPriceProduct, minPossiblePrice.Value, priceIncludesTax, _workContext.CurrentCustomer)).productprice; decimal finalPrice = await _currencyService.ConvertFromPrimaryStoreCurrency(finalPriceBase, _workContext.WorkingCurrency); priceModel.OldPrice = null; priceModel.Price = String.Format(res["Products.PriceRangeFrom"], _priceFormatter.FormatPrice(finalPrice, true, _workContext.WorkingCurrency, _workContext.WorkingLanguage, priceIncludesTax)); priceModel.PriceValue = finalPrice; //PAngV baseprice (used in Germany) if (product.BasepriceEnabled) { priceModel.BasePricePAngV = await product.FormatBasePrice(finalPrice, _localizationService, _measureService, _currencyService, _workContext, _priceFormatter); } } else { //Actually it's not possible (we presume that minimalPrice always has a value) //We never should get here Debug.WriteLine("Cannot calculate minPrice for product #{0}", product.Id); } } } else { //hide prices priceModel.OldPrice = null; priceModel.Price = null; } } break; } #endregion } break; case ProductType.SimpleProduct: case ProductType.Reservation: default: { #region Simple product //add to cart button priceModel.DisableBuyButton = product.DisableBuyButton || !enableShoppingCart || !displayPrices; //add to wishlist button priceModel.DisableWishlistButton = product.DisableWishlistButton || !enableWishlist || !displayPrices; //compare products priceModel.DisableAddToCompareListButton = !_catalogSettings.CompareProductsEnabled; //pre-order if (product.AvailableForPreOrder) { priceModel.AvailableForPreOrder = !product.PreOrderAvailabilityStartDateTimeUtc.HasValue || product.PreOrderAvailabilityStartDateTimeUtc.Value >= DateTime.UtcNow; priceModel.PreOrderAvailabilityStartDateTimeUtc = product.PreOrderAvailabilityStartDateTimeUtc; } //catalog price, not used in views, but it's for front developer if (product.CatalogPrice > 0) { decimal catalogPrice = await _currencyService.ConvertFromPrimaryStoreCurrency(product.CatalogPrice, _workContext.WorkingCurrency); priceModel.CatalogPrice = _priceFormatter.FormatPrice(catalogPrice, true, _workContext.WorkingCurrency, _workContext.WorkingLanguage, priceIncludesTax); } //start price for product auction if (product.StartPrice > 0) { decimal startPrice = await _currencyService.ConvertFromPrimaryStoreCurrency(product.StartPrice, _workContext.WorkingCurrency); priceModel.StartPrice = _priceFormatter.FormatPrice(startPrice, true, _workContext.WorkingCurrency, _workContext.WorkingLanguage, priceIncludesTax); priceModel.StartPriceValue = startPrice; } //highest bid for product auction if (product.HighestBid > 0) { decimal highestBid = await _currencyService.ConvertFromPrimaryStoreCurrency(product.HighestBid, _workContext.WorkingCurrency); priceModel.HighestBid = _priceFormatter.FormatPrice(highestBid, true, _workContext.WorkingCurrency, _workContext.WorkingLanguage, priceIncludesTax); priceModel.HighestBidValue = highestBid; } //prices if (displayPrices) { if (!product.CustomerEntersPrice) { if (product.CallForPrice) { //call for price priceModel.OldPrice = null; priceModel.Price = res["Products.CallForPrice"]; } else { //prices //calculate for the maximum quantity (in case if we have tier prices) var infoprice = (await _priceCalculationService.GetFinalPrice(product, _workContext.CurrentCustomer, decimal.Zero, true, int.MaxValue)); priceModel.AppliedDiscounts = infoprice.appliedDiscounts; priceModel.PreferredTierPrice = infoprice.preferredTierPrice; decimal minPossiblePrice = infoprice.finalPrice; decimal oldPriceBase = (await _taxService.GetProductPrice(product, product.OldPrice, priceIncludesTax, _workContext.CurrentCustomer)).productprice; decimal finalPriceBase = (await _taxService.GetProductPrice(product, minPossiblePrice, priceIncludesTax, _workContext.CurrentCustomer)).productprice; decimal oldPrice = await _currencyService.ConvertFromPrimaryStoreCurrency(oldPriceBase, _workContext.WorkingCurrency); decimal finalPrice = await _currencyService.ConvertFromPrimaryStoreCurrency(finalPriceBase, _workContext.WorkingCurrency); //do we have tier prices configured? var tierPrices = new List <TierPrice>(); if (product.TierPrices.Any()) { tierPrices.AddRange(product.TierPrices.OrderBy(tp => tp.Quantity) .FilterByStore(_storeContext.CurrentStore.Id) .FilterForCustomer(_workContext.CurrentCustomer) .FilterByDate() .RemoveDuplicatedQuantities()); } //When there is just one tier (with qty 1), //there are no actual savings in the list. bool displayFromMessage = tierPrices.Any() && !(tierPrices.Count == 1 && tierPrices[0].Quantity <= 1); if (displayFromMessage) { priceModel.OldPrice = null; priceModel.Price = String.Format(res["Products.PriceRangeFrom"], _priceFormatter.FormatPrice(finalPrice, true, _workContext.WorkingCurrency, _workContext.WorkingLanguage, priceIncludesTax)); priceModel.PriceValue = finalPrice; } else { if (finalPriceBase != oldPriceBase && oldPriceBase != decimal.Zero) { priceModel.OldPrice = _priceFormatter.FormatPrice(oldPrice, true, _workContext.WorkingCurrency, _workContext.WorkingLanguage, priceIncludesTax); priceModel.OldPriceValue = oldPrice; priceModel.Price = _priceFormatter.FormatPrice(finalPrice, true, _workContext.WorkingCurrency, _workContext.WorkingLanguage, priceIncludesTax); priceModel.PriceValue = finalPrice; } else { priceModel.OldPrice = null; priceModel.Price = _priceFormatter.FormatPrice(finalPrice, true, _workContext.WorkingCurrency, _workContext.WorkingLanguage, priceIncludesTax); priceModel.PriceValue = finalPrice; } } if (product.ProductType == ProductType.Reservation) { //rental product priceModel.OldPrice = _priceFormatter.FormatReservationProductPeriod(product, priceModel.OldPrice); priceModel.Price = _priceFormatter.FormatReservationProductPeriod(product, priceModel.Price); } //property for German market //we display tax/shipping info only with "shipping enabled" for this product //we also ensure this it's not free shipping priceModel.DisplayTaxShippingInfo = _catalogSettings.DisplayTaxShippingInfoProductBoxes && product.IsShipEnabled && !product.IsFreeShipping; //PAngV baseprice (used in Germany) if (product.BasepriceEnabled) { priceModel.BasePricePAngV = await product.FormatBasePrice(finalPrice, _localizationService, _measureService, _currencyService, _workContext, _priceFormatter); } } } } else { //hide prices priceModel.OldPrice = null; priceModel.Price = null; } #endregion } break; } model.ProductPrice = priceModel; #endregion }
protected virtual async Task <PdfPTable> PrepareProducts(Product product, Language language, int productNumber) { var productName = product.GetLocalized(x => x.Name, language.Id); var productDescription = product.GetLocalized(x => x.FullDescription, language.Id); var font = PdfExtensions.GetFont(_pdfSettings.FontFileName); var titleFont = PdfExtensions.PrepareTitleFont(_pdfSettings.FontFileName); var productTable = new PdfPTable(1); productTable.WidthPercentage = 100f; productTable.DefaultCell.Border = Rectangle.NO_BORDER; if (language.Rtl) { productTable.RunDirection = PdfWriter.RUN_DIRECTION_RTL; } productTable.AddCell(new Paragraph(String.Format("{0}. {1}", productNumber, productName), titleFont)); productTable.AddCell(new Paragraph(" ")); productTable.AddCell(new Paragraph(HtmlHelper.StripTags(HtmlHelper.ConvertHtmlToPlainText(productDescription, decode: true)), font)); productTable.AddCell(new Paragraph(" ")); if (product.ProductType != ProductType.GroupedProduct) { //simple product //render its properties such as price, weight, etc var priceStr = string.Format("{0} {1}", product.Price.ToString("0.00"), (await _currencyService.GetPrimaryStoreCurrency()).CurrencyCode); if (product.ProductType == ProductType.Reservation) { priceStr = _priceFormatter.FormatReservationProductPeriod(product, priceStr); } productTable.AddCell(new Paragraph(String.Format("{0}: {1}", _localizationService.GetResource("PDFProductCatalog.Price", language.Id), priceStr), font)); productTable.AddCell(new Paragraph(String.Format("{0}: {1}", _localizationService.GetResource("PDFProductCatalog.SKU", language.Id), product.Sku), font)); if (product.IsShipEnabled && product.Weight > Decimal.Zero) { productTable.AddCell(new Paragraph(String.Format("{0}: {1} {2}", _localizationService.GetResource("PDFProductCatalog.Weight", language.Id), product.Weight.ToString("0.00"), (await _measureService.GetMeasureWeightById(_measureSettings.BaseWeightId)).Name), font)); } if (product.ManageInventoryMethod == ManageInventoryMethod.ManageStock) { productTable.AddCell(new Paragraph(String.Format("{0}: {1}", _localizationService.GetResource("PDFProductCatalog.StockQuantity", language.Id), product.GetTotalStockQuantity(warehouseId: _storeContext.CurrentStore.DefaultWarehouseId)), font)); } productTable.AddCell(new Paragraph(" ")); } var pictures = product.ProductPictures; if (pictures.Count > 0) { var table = new PdfPTable(2); table.WidthPercentage = 100f; if (language.Rtl) { table.RunDirection = PdfWriter.RUN_DIRECTION_RTL; } foreach (var pic in pictures) { var pp = await _pictureService.GetPictureById(pic.PictureId); if (pp != null) { var picBinary = await _pictureService.LoadPictureBinary(pp); if (picBinary != null && picBinary.Length > 0) { var pictureLocalPath = await _pictureService.GetThumbLocalPath(pp, 200, false); var cell = new PdfPCell(Image.GetInstance(pictureLocalPath)); cell.HorizontalAlignment = Element.ALIGN_LEFT; cell.Border = Rectangle.NO_BORDER; table.AddCell(cell); } } } if (pictures.Count % 2 > 0) { var cell = new PdfPCell(new Phrase(" ")); cell.Border = Rectangle.NO_BORDER; table.AddCell(cell); } productTable.AddCell(table); productTable.AddCell(new Paragraph(" ")); } if (product.ProductType == ProductType.GroupedProduct) { //grouped product. render its associated products int pvNum = 1; var associatedProducts = await _productService.GetAssociatedProducts(product.Id, showHidden : true); foreach (var associatedProduct in associatedProducts) { productTable.AddCell(new Paragraph(String.Format("{0}-{1}. {2}", productNumber, pvNum, associatedProduct.GetLocalized(x => x.Name, language.Id)), font)); productTable.AddCell(new Paragraph(" ")); productTable.AddCell(new Paragraph(String.Format("{0}: {1} {2}", _localizationService.GetResource("PDFProductCatalog.Price", language.Id), associatedProduct.Price.ToString("0.00"), (await _currencyService.GetPrimaryStoreCurrency()).CurrencyCode), font)); productTable.AddCell(new Paragraph(String.Format("{0}: {1}", _localizationService.GetResource("PDFProductCatalog.SKU", language.Id), associatedProduct.Sku), font)); if (associatedProduct.IsShipEnabled && associatedProduct.Weight > Decimal.Zero) { productTable.AddCell(new Paragraph(String.Format("{0}: {1} {2}", _localizationService.GetResource("PDFProductCatalog.Weight", language.Id), associatedProduct.Weight.ToString("0.00"), (await _measureService.GetMeasureWeightById(_measureSettings.BaseWeightId)).Name), font)); } if (associatedProduct.ManageInventoryMethod == ManageInventoryMethod.ManageStock) { productTable.AddCell(new Paragraph(String.Format("{0}: {1}", _localizationService.GetResource("PDFProductCatalog.StockQuantity", language.Id), associatedProduct.GetTotalStockQuantity()), font)); } productTable.AddCell(new Paragraph(" ")); pvNum++; } } return(productTable); }