Search in sources :

Example 1 with ReadableShoppingCartItem

use of com.salesmanager.shop.model.shoppingcart.ReadableShoppingCartItem in project shopizer by shopizer-ecommerce.

the class ReadableShoppingCartMapper method merge.

@Override
public ReadableShoppingCart merge(ShoppingCart source, ReadableShoppingCart destination, MerchantStore store, Language language) {
    Validate.notNull(source, "ShoppingCart cannot be null");
    Validate.notNull(destination, "ReadableShoppingCart cannot be null");
    Validate.notNull(store, "MerchantStore cannot be null");
    Validate.notNull(language, "Language cannot be null");
    destination.setCode(source.getShoppingCartCode());
    int cartQuantity = 0;
    destination.setCustomer(source.getCustomerId());
    try {
        if (!StringUtils.isBlank(source.getPromoCode())) {
            // promo valid 1 day
            Date promoDateAdded = source.getPromoAdded();
            if (promoDateAdded == null) {
                promoDateAdded = new Date();
            }
            Instant instant = promoDateAdded.toInstant();
            ZonedDateTime zdt = instant.atZone(ZoneId.systemDefault());
            LocalDate date = zdt.toLocalDate();
            // date added < date + 1 day
            LocalDate tomorrow = LocalDate.now().plusDays(1);
            if (date.isBefore(tomorrow)) {
                destination.setPromoCode(source.getPromoCode());
            }
        }
        Set<com.salesmanager.core.model.shoppingcart.ShoppingCartItem> items = source.getLineItems();
        if (items != null) {
            for (com.salesmanager.core.model.shoppingcart.ShoppingCartItem item : items) {
                ReadableShoppingCartItem shoppingCartItem = new ReadableShoppingCartItem();
                readableMinimalProductMapper.merge(item.getProduct(), shoppingCartItem, store, language);
                // ReadableProductPopulator readableProductPopulator = new
                // ReadableProductPopulator();
                // readableProductPopulator.setPricingService(pricingService);
                // readableProductPopulator.setimageUtils(imageUtils);
                // readableProductPopulator.populate(item.getProduct(), shoppingCartItem, store,
                // language);
                shoppingCartItem.setPrice(item.getItemPrice());
                shoppingCartItem.setFinalPrice(pricingService.getDisplayAmount(item.getItemPrice(), store));
                shoppingCartItem.setQuantity(item.getQuantity());
                cartQuantity = cartQuantity + item.getQuantity();
                BigDecimal subTotal = pricingService.calculatePriceQuantity(item.getItemPrice(), item.getQuantity());
                // calculate sub total (price * quantity)
                shoppingCartItem.setSubTotal(subTotal);
                shoppingCartItem.setDisplaySubTotal(pricingService.getDisplayAmount(subTotal, store));
                Set<com.salesmanager.core.model.shoppingcart.ShoppingCartAttributeItem> attributes = item.getAttributes();
                if (attributes != null) {
                    for (com.salesmanager.core.model.shoppingcart.ShoppingCartAttributeItem attribute : attributes) {
                        ProductAttribute productAttribute = productAttributeService.getById(attribute.getProductAttributeId());
                        if (productAttribute == null) {
                            LOG.warn("Product attribute with ID " + attribute.getId() + " not found, skipping cart attribute " + attribute.getId());
                            continue;
                        }
                        ReadableShoppingCartAttribute cartAttribute = new ReadableShoppingCartAttribute();
                        cartAttribute.setId(attribute.getId());
                        ProductOption option = productAttribute.getProductOption();
                        ProductOptionValue optionValue = productAttribute.getProductOptionValue();
                        List<ProductOptionDescription> optionDescriptions = option.getDescriptionsSettoList();
                        List<ProductOptionValueDescription> optionValueDescriptions = optionValue.getDescriptionsSettoList();
                        String optName = null;
                        String optValue = null;
                        if (!CollectionUtils.isEmpty(optionDescriptions) && !CollectionUtils.isEmpty(optionValueDescriptions)) {
                            optName = optionDescriptions.get(0).getName();
                            optValue = optionValueDescriptions.get(0).getName();
                            for (ProductOptionDescription optionDescription : optionDescriptions) {
                                if (optionDescription.getLanguage() != null && optionDescription.getLanguage().getId().intValue() == language.getId().intValue()) {
                                    optName = optionDescription.getName();
                                    break;
                                }
                            }
                            for (ProductOptionValueDescription optionValueDescription : optionValueDescriptions) {
                                if (optionValueDescription.getLanguage() != null && optionValueDescription.getLanguage().getId().intValue() == language.getId().intValue()) {
                                    optValue = optionValueDescription.getName();
                                    break;
                                }
                            }
                        }
                        if (optName != null) {
                            ReadableShoppingCartAttributeOption attributeOption = new ReadableShoppingCartAttributeOption();
                            attributeOption.setCode(option.getCode());
                            attributeOption.setId(option.getId());
                            attributeOption.setName(optName);
                            cartAttribute.setOption(attributeOption);
                        }
                        if (optValue != null) {
                            ReadableShoppingCartAttributeOptionValue attributeOptionValue = new ReadableShoppingCartAttributeOptionValue();
                            attributeOptionValue.setCode(optionValue.getCode());
                            attributeOptionValue.setId(optionValue.getId());
                            attributeOptionValue.setName(optValue);
                            cartAttribute.setOptionValue(attributeOptionValue);
                        }
                        shoppingCartItem.getCartItemattributes().add(cartAttribute);
                    }
                }
                destination.getProducts().add(shoppingCartItem);
            }
        }
        // Calculate totals using shoppingCartService
        // OrderSummary contains ShoppingCart items
        OrderSummary summary = new OrderSummary();
        List<com.salesmanager.core.model.shoppingcart.ShoppingCartItem> productsList = new ArrayList<com.salesmanager.core.model.shoppingcart.ShoppingCartItem>();
        productsList.addAll(source.getLineItems());
        summary.setProducts(productsList);
        // OrdetTotalSummary contains all calculations
        OrderTotalSummary orderSummary = shoppingCartCalculationService.calculate(source, store, language);
        if (CollectionUtils.isNotEmpty(orderSummary.getTotals())) {
            if (orderSummary.getTotals().stream().filter(t -> Constants.OT_DISCOUNT_TITLE.equals(t.getOrderTotalCode())).count() == 0) {
                // no promo coupon applied
                destination.setPromoCode(null);
            }
            List<ReadableOrderTotal> totals = new ArrayList<ReadableOrderTotal>();
            for (com.salesmanager.core.model.order.OrderTotal t : orderSummary.getTotals()) {
                ReadableOrderTotal total = new ReadableOrderTotal();
                total.setCode(t.getOrderTotalCode());
                total.setValue(t.getValue());
                total.setText(t.getText());
                totals.add(total);
            }
            destination.setTotals(totals);
        }
        destination.setSubtotal(orderSummary.getSubTotal());
        destination.setDisplaySubTotal(pricingService.getDisplayAmount(orderSummary.getSubTotal(), store));
        destination.setTotal(orderSummary.getTotal());
        destination.setDisplayTotal(pricingService.getDisplayAmount(orderSummary.getTotal(), store));
        destination.setQuantity(cartQuantity);
        destination.setId(source.getId());
        if (source.getOrderId() != null) {
            destination.setOrder(source.getOrderId());
        }
    } catch (Exception e) {
        throw new ConversionRuntimeException("An error occured while converting ReadableShoppingCart", e);
    }
    return destination;
}
Also used : ReadableShoppingCartAttribute(com.salesmanager.shop.model.shoppingcart.ReadableShoppingCartAttribute) OrderSummary(com.salesmanager.core.model.order.OrderSummary) OrderTotalSummary(com.salesmanager.core.model.order.OrderTotalSummary) ArrayList(java.util.ArrayList) ProductAttribute(com.salesmanager.core.model.catalog.product.attribute.ProductAttribute) LocalDate(java.time.LocalDate) ProductOptionDescription(com.salesmanager.core.model.catalog.product.attribute.ProductOptionDescription) ReadableShoppingCartItem(com.salesmanager.shop.model.shoppingcart.ReadableShoppingCartItem) ProductOptionValue(com.salesmanager.core.model.catalog.product.attribute.ProductOptionValue) ZonedDateTime(java.time.ZonedDateTime) ReadableShoppingCartAttributeOption(com.salesmanager.shop.model.shoppingcart.ReadableShoppingCartAttributeOption) ConversionRuntimeException(com.salesmanager.shop.store.api.exception.ConversionRuntimeException) ProductOption(com.salesmanager.core.model.catalog.product.attribute.ProductOption) ReadableOrderTotal(com.salesmanager.shop.model.order.total.ReadableOrderTotal) Instant(java.time.Instant) Date(java.util.Date) LocalDate(java.time.LocalDate) BigDecimal(java.math.BigDecimal) ConversionRuntimeException(com.salesmanager.shop.store.api.exception.ConversionRuntimeException) ReadableShoppingCartAttributeOptionValue(com.salesmanager.shop.model.shoppingcart.ReadableShoppingCartAttributeOptionValue) ReadableShoppingCartItem(com.salesmanager.shop.model.shoppingcart.ReadableShoppingCartItem) ProductOptionValueDescription(com.salesmanager.core.model.catalog.product.attribute.ProductOptionValueDescription)

Aggregations

ProductAttribute (com.salesmanager.core.model.catalog.product.attribute.ProductAttribute)1 ProductOption (com.salesmanager.core.model.catalog.product.attribute.ProductOption)1 ProductOptionDescription (com.salesmanager.core.model.catalog.product.attribute.ProductOptionDescription)1 ProductOptionValue (com.salesmanager.core.model.catalog.product.attribute.ProductOptionValue)1 ProductOptionValueDescription (com.salesmanager.core.model.catalog.product.attribute.ProductOptionValueDescription)1 OrderSummary (com.salesmanager.core.model.order.OrderSummary)1 OrderTotalSummary (com.salesmanager.core.model.order.OrderTotalSummary)1 ReadableOrderTotal (com.salesmanager.shop.model.order.total.ReadableOrderTotal)1 ReadableShoppingCartAttribute (com.salesmanager.shop.model.shoppingcart.ReadableShoppingCartAttribute)1 ReadableShoppingCartAttributeOption (com.salesmanager.shop.model.shoppingcart.ReadableShoppingCartAttributeOption)1 ReadableShoppingCartAttributeOptionValue (com.salesmanager.shop.model.shoppingcart.ReadableShoppingCartAttributeOptionValue)1 ReadableShoppingCartItem (com.salesmanager.shop.model.shoppingcart.ReadableShoppingCartItem)1 ConversionRuntimeException (com.salesmanager.shop.store.api.exception.ConversionRuntimeException)1 BigDecimal (java.math.BigDecimal)1 Instant (java.time.Instant)1 LocalDate (java.time.LocalDate)1 ZonedDateTime (java.time.ZonedDateTime)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1