de.hybris.platform.dfcheckoutservice.service.impl.DfDefaultCalculationService.java Source code

Java tutorial

Introduction

Here is the source code for de.hybris.platform.dfcheckoutservice.service.impl.DfDefaultCalculationService.java

Source

/*
 * [y] hybris Platform
 *
 * Copyright (c) 2000-2016 SAP SE
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of SAP
 * Hybris ("Confidential Information"). You shall not disclose such
 * Confidential Information and shall use it only in accordance with the
 * terms of the license agreement you entered into with SAP Hybris.
 */
package de.hybris.platform.dfcheckoutservice.service.impl;

import de.hybris.platform.core.model.order.AbstractOrderEntryModel;
import de.hybris.platform.core.model.order.AbstractOrderModel;
import de.hybris.platform.order.impl.DefaultCalculationService;
import de.hybris.platform.order.strategies.calculation.OrderRequiresCalculationStrategy;
import de.hybris.platform.servicelayer.i18n.CommonI18NService;
import de.hybris.platform.util.TaxValue;

import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Required;

/**
 *
 */
public class DfDefaultCalculationService extends DefaultCalculationService {

    private OrderRequiresCalculationStrategy orderRequiresCalculationStrategy2;
    private CommonI18NService commonI18NService2;

    @Override
    protected Map<TaxValue, Map<Set<TaxValue>, Double>> calculateSubtotal(final AbstractOrderModel order,
            final boolean recalculate) {
        System.out.println("hello ok");
        if (recalculate || orderRequiresCalculationStrategy2.requiresCalculation(order)) {
            double subtotal = 0.0;
            // entry grouping via map { tax code -> Double }
            final List<AbstractOrderEntryModel> entries = order.getEntries();
            final Map<TaxValue, Map<Set<TaxValue>, Double>> taxValueMap = new LinkedHashMap<TaxValue, Map<Set<TaxValue>, Double>>(
                    entries.size() * 2);

            for (final AbstractOrderEntryModel entry : entries) {
                if (entry.isChecked()) {
                    calculateTotals(entry, recalculate);
                    final double entryTotal = entry.getTotalPrice().doubleValue();
                    subtotal += entryTotal;
                    // use un-applied version of tax values!!!
                    final Collection<TaxValue> allTaxValues = entry.getTaxValues();
                    final Set<TaxValue> relativeTaxGroupKey = getUnappliedRelativeTaxValues(allTaxValues);
                    for (final TaxValue taxValue : allTaxValues) {
                        if (taxValue.isAbsolute()) {
                            addAbsoluteEntryTaxValue(entry.getQuantity().longValue(), taxValue.unapply(),
                                    taxValueMap);
                        } else {
                            addRelativeEntryTaxValue(entryTotal, taxValue.unapply(), relativeTaxGroupKey,
                                    taxValueMap);
                        }
                    }
                }
            }
            // store subtotal
            subtotal = commonI18NService2.roundCurrency(subtotal, order.getCurrency().getDigits().intValue());
            order.setSubtotal(Double.valueOf(subtotal));
            return taxValueMap;
        }
        return Collections.EMPTY_MAP;
    }

    private Set<TaxValue> getUnappliedRelativeTaxValues(final Collection<TaxValue> allTaxValues) {
        if (CollectionUtils.isNotEmpty(allTaxValues)) {
            final Set<TaxValue> ret = new LinkedHashSet<TaxValue>(allTaxValues.size());
            for (final TaxValue appliedTv : allTaxValues) {
                if (!appliedTv.isAbsolute()) {
                    ret.add(appliedTv.unapply());
                }
            }
            return ret;
        } else {
            return Collections.EMPTY_SET;
        }
    }

    @Required
    public void setCommonI18NService2(final CommonI18NService commonI18NService) {
        this.commonI18NService2 = commonI18NService;
    }

    @Required
    public void setOrderRequiresCalculationStrategy2(
            final OrderRequiresCalculationStrategy orderRequiresCalculationStrategy) {
        this.orderRequiresCalculationStrategy2 = orderRequiresCalculationStrategy;
    }

}