Search in sources :

Example 6 with Country

use of com.salesmanager.core.model.reference.country.Country in project shopizer by shopizer-ecommerce.

the class ReferenceController method countryName.

@RequestMapping(value = "/shop/reference/countryName")
@ResponseBody
public String countryName(@RequestParam String countryCode, HttpServletRequest request, HttpServletResponse response) {
    try {
        Language language = languageUtils.getRequestLanguage(request, response);
        if (language == null) {
            return countryCode;
        }
        Map<String, Country> countriesMap = countryService.getCountriesMap(language);
        if (countriesMap != null) {
            Country c = countriesMap.get(countryCode);
            if (c != null) {
                return c.getName();
            }
        }
    } catch (ServiceException e) {
        LOGGER.error("Error while looking up country " + countryCode);
    }
    return StringEscapeUtils.escapeHtml4(countryCode);
}
Also used : Language(com.salesmanager.core.model.reference.language.Language) ServiceException(com.salesmanager.core.business.exception.ServiceException) Country(com.salesmanager.core.model.reference.country.Country) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 7 with Country

use of com.salesmanager.core.model.reference.country.Country in project shopizer by shopizer-ecommerce.

the class ShippingServiceImpl method getShippingQuote.

@Override
public ShippingQuote getShippingQuote(Long shoppingCartId, MerchantStore store, Delivery delivery, List<ShippingProduct> products, Language language) throws ServiceException {
    // ShippingConfiguration -> Global configuration of a given store
    // IntegrationConfiguration -> Configuration of a given module
    // IntegrationModule -> The concrete module as defined in integrationmodules.properties
    // delivery without postal code is accepted
    Validate.notNull(store, "MerchantStore must not be null");
    Validate.notNull(delivery, "Delivery must not be null");
    Validate.notEmpty(products, "products must not be empty");
    Validate.notNull(language, "Language must not be null");
    ShippingQuote shippingQuote = new ShippingQuote();
    ShippingQuoteModule shippingQuoteModule = null;
    try {
        if (StringUtils.isBlank(delivery.getPostalCode())) {
            shippingQuote.getWarnings().add("No postal code in delivery address");
            shippingQuote.setShippingReturnCode(ShippingQuote.NO_POSTAL_CODE);
        }
        // get configuration
        ShippingConfiguration shippingConfiguration = getShippingConfiguration(store);
        ShippingType shippingType = ShippingType.INTERNATIONAL;
        /**
         * get shipping origin *
         */
        ShippingOrigin shippingOrigin = shippingOriginService.getByStore(store);
        if (shippingOrigin == null || !shippingOrigin.isActive()) {
            shippingOrigin = new ShippingOrigin();
            shippingOrigin.setAddress(store.getStoreaddress());
            shippingOrigin.setCity(store.getStorecity());
            shippingOrigin.setCountry(store.getCountry());
            shippingOrigin.setPostalCode(store.getStorepostalcode());
            shippingOrigin.setState(store.getStorestateprovince());
            shippingOrigin.setZone(store.getZone());
        }
        if (shippingConfiguration == null) {
            shippingConfiguration = new ShippingConfiguration();
        }
        if (shippingConfiguration.getShippingType() != null) {
            shippingType = shippingConfiguration.getShippingType();
        }
        // look if customer country code excluded
        Country shipCountry = delivery.getCountry();
        // a ship to country is required
        Validate.notNull(shipCountry, "Ship to Country cannot be null");
        Validate.notNull(store.getCountry(), "Store Country canot be null");
        if (shippingType.name().equals(ShippingType.NATIONAL.name())) {
            // customer country must match store country
            if (!shipCountry.getIsoCode().equals(store.getCountry().getIsoCode())) {
                shippingQuote.setShippingReturnCode(ShippingQuote.NO_SHIPPING_TO_SELECTED_COUNTRY + " " + shipCountry.getIsoCode());
                return shippingQuote;
            }
        } else if (shippingType.name().equals(ShippingType.INTERNATIONAL.name())) {
            // customer shipping country code must be in accepted list
            List<String> supportedCountries = this.getSupportedCountries(store);
            if (!supportedCountries.contains(shipCountry.getIsoCode())) {
                shippingQuote.setShippingReturnCode(ShippingQuote.NO_SHIPPING_TO_SELECTED_COUNTRY + " " + shipCountry.getIsoCode());
                return shippingQuote;
            }
        }
        // must have a shipping module configured
        Map<String, IntegrationConfiguration> modules = this.getShippingModulesConfigured(store);
        if (modules == null) {
            shippingQuote.setShippingReturnCode(ShippingQuote.NO_SHIPPING_MODULE_CONFIGURED);
            return shippingQuote;
        }
        /**
         * uses this module name *
         */
        String moduleName = null;
        IntegrationConfiguration configuration = null;
        for (String module : modules.keySet()) {
            moduleName = module;
            configuration = modules.get(module);
            // use the first active module
            if (configuration.isActive()) {
                shippingQuoteModule = shippingModules.get(module);
                if (shippingQuoteModule instanceof ShippingQuotePrePostProcessModule) {
                    shippingQuoteModule = null;
                    continue;
                } else {
                    break;
                }
            }
        }
        if (shippingQuoteModule == null) {
            shippingQuote.setShippingReturnCode(ShippingQuote.NO_SHIPPING_MODULE_CONFIGURED);
            return shippingQuote;
        }
        /**
         * merchant module configs *
         */
        List<IntegrationModule> shippingMethods = this.getShippingMethods(store);
        IntegrationModule shippingModule = null;
        for (IntegrationModule mod : shippingMethods) {
            if (mod.getCode().equals(moduleName)) {
                shippingModule = mod;
                break;
            }
        }
        /**
         * general module configs *
         */
        if (shippingModule == null) {
            shippingQuote.setShippingReturnCode(ShippingQuote.NO_SHIPPING_MODULE_CONFIGURED);
            return shippingQuote;
        }
        // calculate order total
        BigDecimal orderTotal = calculateOrderTotal(products, store);
        List<PackageDetails> packages = getPackagesDetails(products, store);
        // free shipping ?
        boolean freeShipping = false;
        if (shippingConfiguration.isFreeShippingEnabled()) {
            BigDecimal freeShippingAmount = shippingConfiguration.getOrderTotalFreeShipping();
            if (freeShippingAmount != null) {
                if (orderTotal.doubleValue() > freeShippingAmount.doubleValue()) {
                    if (shippingConfiguration.getFreeShippingType() == ShippingType.NATIONAL) {
                        if (store.getCountry().getIsoCode().equals(shipCountry.getIsoCode())) {
                            freeShipping = true;
                            shippingQuote.setFreeShipping(true);
                            shippingQuote.setFreeShippingAmount(freeShippingAmount);
                            return shippingQuote;
                        }
                    } else {
                        // international all
                        freeShipping = true;
                        shippingQuote.setFreeShipping(true);
                        shippingQuote.setFreeShippingAmount(freeShippingAmount);
                        return shippingQuote;
                    }
                }
            }
        }
        // handling fees
        BigDecimal handlingFees = shippingConfiguration.getHandlingFees();
        if (handlingFees != null) {
            shippingQuote.setHandlingFees(handlingFees);
        }
        // tax basis
        shippingQuote.setApplyTaxOnShipping(shippingConfiguration.isTaxOnShipping());
        Locale locale = languageService.toLocale(language, store);
        // also available distance calculation
        if (!CollectionUtils.isEmpty(shippingModulePreProcessors)) {
            for (ShippingQuotePrePostProcessModule preProcessor : shippingModulePreProcessors) {
                // System.out.println("Using pre-processor " + preProcessor.getModuleCode());
                preProcessor.prePostProcessShippingQuotes(shippingQuote, packages, orderTotal, delivery, shippingOrigin, store, configuration, shippingModule, shippingConfiguration, shippingMethods, locale);
                // TODO switch module if required
                if (shippingQuote.getCurrentShippingModule() != null && !shippingQuote.getCurrentShippingModule().getCode().equals(shippingModule.getCode())) {
                    // determines the shipping module
                    shippingModule = shippingQuote.getCurrentShippingModule();
                    configuration = modules.get(shippingModule.getCode());
                    if (configuration != null) {
                        if (configuration.isActive()) {
                            moduleName = shippingModule.getCode();
                            shippingQuoteModule = this.shippingModules.get(shippingModule.getCode());
                            configuration = modules.get(shippingModule.getCode());
                        }
                    // TODO use default
                    }
                }
            }
        }
        // invoke module
        List<ShippingOption> shippingOptions = null;
        try {
            shippingOptions = shippingQuoteModule.getShippingQuotes(shippingQuote, packages, orderTotal, delivery, shippingOrigin, store, configuration, shippingModule, shippingConfiguration, locale);
        } catch (Exception e) {
            LOGGER.error("Error while calculating shipping : " + e.getMessage(), e);
        /*				merchantLogService.save(
						new MerchantLog(store,
								"Can't process " + shippingModule.getModule()
								+ " -> "
								+ e.getMessage()));
				shippingQuote.setQuoteError(e.getMessage());
				shippingQuote.setShippingReturnCode(ShippingQuote.ERROR);
				return shippingQuote;*/
        }
        if (shippingOptions == null && !StringUtils.isBlank(delivery.getPostalCode())) {
            // absolutely need to use in this case store pickup or other default shipping quote
            shippingQuote.setShippingReturnCode(ShippingQuote.NO_SHIPPING_TO_SELECTED_COUNTRY);
        }
        shippingQuote.setShippingModuleCode(moduleName);
        // filter shipping options
        ShippingOptionPriceType shippingOptionPriceType = shippingConfiguration.getShippingOptionPriceType();
        ShippingOption selectedOption = null;
        if (shippingOptions != null) {
            for (ShippingOption option : shippingOptions) {
                if (selectedOption == null) {
                    selectedOption = option;
                }
                // set price text
                String priceText = pricingService.getDisplayAmount(option.getOptionPrice(), store);
                option.setOptionPriceText(priceText);
                option.setShippingModuleCode(moduleName);
                if (StringUtils.isBlank(option.getOptionName())) {
                    String countryName = delivery.getCountry().getName();
                    if (countryName == null) {
                        Map<String, Country> deliveryCountries = countryService.getCountriesMap(language);
                        Country dCountry = deliveryCountries.get(delivery.getCountry().getIsoCode());
                        if (dCountry != null) {
                            countryName = dCountry.getName();
                        } else {
                            countryName = delivery.getCountry().getIsoCode();
                        }
                    }
                    option.setOptionName(countryName);
                }
                if (shippingOptionPriceType.name().equals(ShippingOptionPriceType.HIGHEST.name())) {
                    if (option.getOptionPrice().longValue() > selectedOption.getOptionPrice().longValue()) {
                        selectedOption = option;
                    }
                }
                if (shippingOptionPriceType.name().equals(ShippingOptionPriceType.LEAST.name())) {
                    if (option.getOptionPrice().longValue() < selectedOption.getOptionPrice().longValue()) {
                        selectedOption = option;
                    }
                }
                if (shippingOptionPriceType.name().equals(ShippingOptionPriceType.ALL.name())) {
                    if (option.getOptionPrice().longValue() < selectedOption.getOptionPrice().longValue()) {
                        selectedOption = option;
                    }
                }
            }
            shippingQuote.setSelectedShippingOption(selectedOption);
            if (selectedOption != null && !shippingOptionPriceType.name().equals(ShippingOptionPriceType.ALL.name())) {
                shippingOptions = new ArrayList<ShippingOption>();
                shippingOptions.add(selectedOption);
            }
        }
        /**
         * set final delivery address *
         */
        shippingQuote.setDeliveryAddress(delivery);
        shippingQuote.setShippingOptions(shippingOptions);
        // invoke pre processors
        if (!CollectionUtils.isEmpty(shippingModulePostProcessors)) {
            for (ShippingQuotePrePostProcessModule postProcessor : shippingModulePostProcessors) {
                // get module info
                // get module configuration
                IntegrationConfiguration integrationConfiguration = modules.get(postProcessor.getModuleCode());
                IntegrationModule postProcessModule = null;
                for (IntegrationModule mod : shippingMethods) {
                    if (mod.getCode().equals(postProcessor.getModuleCode())) {
                        postProcessModule = mod;
                        break;
                    }
                }
                IntegrationModule module = postProcessModule;
                if (integrationConfiguration != null) {
                    postProcessor.prePostProcessShippingQuotes(shippingQuote, packages, orderTotal, delivery, shippingOrigin, store, integrationConfiguration, module, shippingConfiguration, shippingMethods, locale);
                }
            }
        }
        String ipAddress = null;
        UserContext context = UserContext.getCurrentInstance();
        if (context != null) {
            ipAddress = context.getIpAddress();
        }
        if (shippingQuote != null && CollectionUtils.isNotEmpty(shippingQuote.getShippingOptions())) {
            // save SHIPPING OPTIONS
            List<ShippingOption> finalShippingOptions = shippingQuote.getShippingOptions();
            for (ShippingOption option : finalShippingOptions) {
                // transform to Quote
                Quote q = new Quote();
                q.setCartId(shoppingCartId);
                q.setDelivery(delivery);
                if (!StringUtils.isBlank(ipAddress)) {
                    q.setIpAddress(ipAddress);
                }
                if (!StringUtils.isBlank(option.getEstimatedNumberOfDays())) {
                    try {
                        q.setEstimatedNumberOfDays(Integer.valueOf(option.getEstimatedNumberOfDays()));
                    } catch (Exception e) {
                        LOGGER.error("Cannot cast to integer " + option.getEstimatedNumberOfDays());
                    }
                }
                if (freeShipping) {
                    q.setFreeShipping(true);
                    q.setPrice(new BigDecimal(0));
                    q.setModule("FREE");
                    q.setOptionCode("FREE");
                    q.setOptionName("FREE");
                } else {
                    q.setModule(option.getShippingModuleCode());
                    q.setOptionCode(option.getOptionCode());
                    if (!StringUtils.isBlank(option.getOptionDeliveryDate())) {
                        try {
                            q.setOptionDeliveryDate(DateUtil.formatDate(option.getOptionDeliveryDate()));
                        } catch (Exception e) {
                            LOGGER.error("Cannot transform to date " + option.getOptionDeliveryDate());
                        }
                    }
                    q.setOptionName(option.getOptionName());
                    q.setOptionShippingDate(new Date());
                    q.setPrice(option.getOptionPrice());
                }
                if (handlingFees != null) {
                    q.setHandling(handlingFees);
                }
                q.setQuoteDate(new Date());
                shippingQuoteService.save(q);
                option.setShippingQuoteOptionId(q.getId());
            }
        }
    } catch (Exception e) {
        LOGGER.error(e.getMessage(), e);
        throw new ServiceException(e);
    }
    return shippingQuote;
}
Also used : Locale(java.util.Locale) ShippingConfiguration(com.salesmanager.core.model.shipping.ShippingConfiguration) List(java.util.List) ArrayList(java.util.ArrayList) IntegrationModule(com.salesmanager.core.model.system.IntegrationModule) ShippingType(com.salesmanager.core.model.shipping.ShippingType) UserContext(com.salesmanager.core.model.common.UserContext) CustomIntegrationConfiguration(com.salesmanager.core.model.system.CustomIntegrationConfiguration) IntegrationConfiguration(com.salesmanager.core.model.system.IntegrationConfiguration) BigDecimal(java.math.BigDecimal) ServiceException(com.salesmanager.core.business.exception.ServiceException) IntegrationException(com.salesmanager.core.modules.integration.IntegrationException) Date(java.util.Date) ShippingOption(com.salesmanager.core.model.shipping.ShippingOption) Quote(com.salesmanager.core.model.shipping.Quote) ShippingQuote(com.salesmanager.core.model.shipping.ShippingQuote) ShippingOptionPriceType(com.salesmanager.core.model.shipping.ShippingOptionPriceType) ShippingQuote(com.salesmanager.core.model.shipping.ShippingQuote) ServiceException(com.salesmanager.core.business.exception.ServiceException) ShippingQuoteModule(com.salesmanager.core.modules.integration.shipping.model.ShippingQuoteModule) Country(com.salesmanager.core.model.reference.country.Country) ShippingQuotePrePostProcessModule(com.salesmanager.core.modules.integration.shipping.model.ShippingQuotePrePostProcessModule) ShippingOrigin(com.salesmanager.core.model.shipping.ShippingOrigin) PackageDetails(com.salesmanager.core.model.shipping.PackageDetails)

Example 8 with Country

use of com.salesmanager.core.model.reference.country.Country in project shopizer by shopizer-ecommerce.

the class InitializationDatabaseImpl method createMerchant.

private void createMerchant() throws ServiceException {
    LOGGER.info(String.format("%s : Creating merchant ", name));
    Date date = new Date(System.currentTimeMillis());
    Language en = languageService.getByCode("en");
    Country ca = countryService.getByCode("CA");
    Currency currency = currencyService.getByCode("CAD");
    Zone qc = zoneService.getByCode("QC");
    List<Language> supportedLanguages = new ArrayList<Language>();
    supportedLanguages.add(en);
    // create a merchant
    MerchantStore store = new MerchantStore();
    store.setCountry(ca);
    store.setCurrency(currency);
    store.setDefaultLanguage(en);
    store.setInBusinessSince(date);
    store.setZone(qc);
    store.setStorename("Default store");
    store.setStorephone("888-888-8888");
    store.setCode(MerchantStore.DEFAULT_STORE);
    store.setStorecity("My city");
    store.setStoreaddress("1234 Street address");
    store.setStorepostalcode("H2H-2H2");
    store.setStoreEmailAddress("john@test.com");
    store.setDomainName("localhost:8080");
    store.setStoreTemplate("december");
    store.setRetailer(true);
    store.setLanguages(supportedLanguages);
    merchantService.create(store);
    TaxClass taxclass = new TaxClass(TaxClass.DEFAULT_TAX_CLASS);
    taxclass.setMerchantStore(store);
    taxClassService.create(taxclass);
    // create default manufacturer
    Manufacturer defaultManufacturer = new Manufacturer();
    defaultManufacturer.setCode("DEFAULT");
    defaultManufacturer.setMerchantStore(store);
    ManufacturerDescription manufacturerDescription = new ManufacturerDescription();
    manufacturerDescription.setLanguage(en);
    manufacturerDescription.setName("DEFAULT");
    manufacturerDescription.setManufacturer(defaultManufacturer);
    manufacturerDescription.setDescription("DEFAULT");
    defaultManufacturer.getDescriptions().add(manufacturerDescription);
    manufacturerService.create(defaultManufacturer);
    Optin newsletter = new Optin();
    newsletter.setCode(OptinType.NEWSLETTER.name());
    newsletter.setMerchant(store);
    newsletter.setOptinType(OptinType.NEWSLETTER);
    optinService.create(newsletter);
}
Also used : Language(com.salesmanager.core.model.reference.language.Language) Optin(com.salesmanager.core.model.system.optin.Optin) Zone(com.salesmanager.core.model.reference.zone.Zone) Currency(com.salesmanager.core.model.reference.currency.Currency) ArrayList(java.util.ArrayList) Manufacturer(com.salesmanager.core.model.catalog.product.manufacturer.Manufacturer) Country(com.salesmanager.core.model.reference.country.Country) ManufacturerDescription(com.salesmanager.core.model.catalog.product.manufacturer.ManufacturerDescription) TaxClass(com.salesmanager.core.model.tax.taxclass.TaxClass) MerchantStore(com.salesmanager.core.model.merchant.MerchantStore) Date(java.sql.Date)

Example 9 with Country

use of com.salesmanager.core.model.reference.country.Country in project shopizer by shopizer-ecommerce.

the class ZonesLoader method mapZone.

// internal complex mapping stuff, don't try this at home ...
private void mapZone(Language l, Map<String, List<ZoneDescription>> zonesDescriptionsMap, Map<String, Country> countriesMap, Map<String, Zone> zonesMap, Map<String, String> zonesMark, Map<String, String> list) {
    String zoneCode = list.get("zoneCode");
    ZoneDescription zoneDescription = new ZoneDescription();
    zoneDescription.setLanguage(l);
    zoneDescription.setName(list.get("zoneName"));
    Zone zone = null;
    List<ZoneDescription> descriptions = null;
    if (!zonesMap.containsKey(zoneCode)) {
        zone = new Zone();
        Country country = countriesMap.get(list.get("countryCode"));
        if (country == null) {
            LOGGER.warn("Country is null for " + zoneCode + " and country code " + list.get("countryCode"));
            return;
        }
        zone.setCountry(country);
        zone.setCode(zoneCode);
        zonesMap.put(zoneCode, zone);
    }
    if (zonesMark.containsKey(l.getCode() + "_" + zoneCode)) {
        LOGGER.warn("This zone seems to be a duplicate !  " + zoneCode + " and language code " + l.getCode());
        return;
    }
    zonesMark.put(l.getCode() + "_" + zoneCode, l.getCode() + "_" + zoneCode);
    if (zonesDescriptionsMap.containsKey(zoneCode)) {
        descriptions = zonesDescriptionsMap.get(zoneCode);
    } else {
        descriptions = new ArrayList<ZoneDescription>();
        zonesDescriptionsMap.put(zoneCode, descriptions);
    }
    descriptions.add(zoneDescription);
}
Also used : Zone(com.salesmanager.core.model.reference.zone.Zone) Country(com.salesmanager.core.model.reference.country.Country) ZoneDescription(com.salesmanager.core.model.reference.zone.ZoneDescription)

Example 10 with Country

use of com.salesmanager.core.model.reference.country.Country in project shopizer by shopizer-ecommerce.

the class CountryServiceImpl method getCountriesMap.

@Override
@Cacheable("countriesMap")
public Map<String, Country> getCountriesMap(Language language) throws ServiceException {
    List<Country> countries = getCountries(language);
    Map<String, Country> returnMap = new LinkedHashMap<String, Country>();
    for (Country country : countries) {
        returnMap.put(country.getIsoCode(), country);
    }
    return returnMap;
}
Also used : Country(com.salesmanager.core.model.reference.country.Country) LinkedHashMap(java.util.LinkedHashMap) Cacheable(org.springframework.cache.annotation.Cacheable)

Aggregations

Country (com.salesmanager.core.model.reference.country.Country)33 Zone (com.salesmanager.core.model.reference.zone.Zone)21 Language (com.salesmanager.core.model.reference.language.Language)18 ArrayList (java.util.ArrayList)15 ServiceException (com.salesmanager.core.business.exception.ServiceException)14 MerchantStore (com.salesmanager.core.model.merchant.MerchantStore)11 Delivery (com.salesmanager.core.model.common.Delivery)10 ShippingQuote (com.salesmanager.core.model.shipping.ShippingQuote)9 BigDecimal (java.math.BigDecimal)8 List (java.util.List)7 ConversionException (com.salesmanager.core.business.exception.ConversionException)6 Billing (com.salesmanager.core.model.common.Billing)6 Customer (com.salesmanager.core.model.customer.Customer)6 ShippingOption (com.salesmanager.core.model.shipping.ShippingOption)6 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)6 PackageDetails (com.salesmanager.core.model.shipping.PackageDetails)5 ShippingConfiguration (com.salesmanager.core.model.shipping.ShippingConfiguration)5 ShippingOrigin (com.salesmanager.core.model.shipping.ShippingOrigin)5 IntegrationConfiguration (com.salesmanager.core.model.system.IntegrationConfiguration)5 Date (java.util.Date)5