Search in sources :

Example 26 with Zone

use of com.salesmanager.core.model.reference.zone.Zone in project shopizer by shopizer-ecommerce.

the class EmailTemplatesUtils method sendOrderEmail.

/**
 * Sends an email to the customer after a completed order
 * @param customer
 * @param order
 * @param customerLocale
 * @param language
 * @param merchantStore
 * @param contextPath
 */
@Async
public void sendOrderEmail(String toEmail, Customer customer, Order order, Locale customerLocale, Language language, MerchantStore merchantStore, String contextPath) {
    /**
     * issue with putting that elsewhere *
     */
    LOGGER.info("Sending welcome email to customer");
    try {
        Map<String, Zone> zones = zoneService.getZones(language);
        Map<String, Country> countries = countryService.getCountriesMap(language);
        // format Billing address
        StringBuilder billing = new StringBuilder();
        if (StringUtils.isBlank(order.getBilling().getCompany())) {
            billing.append(order.getBilling().getFirstName()).append(" ").append(order.getBilling().getLastName()).append(LINE_BREAK);
        } else {
            billing.append(order.getBilling().getCompany()).append(LINE_BREAK);
        }
        billing.append(order.getBilling().getAddress()).append(LINE_BREAK);
        billing.append(order.getBilling().getCity()).append(", ");
        if (order.getBilling().getZone() != null) {
            Zone zone = zones.get(order.getBilling().getZone().getCode());
            if (zone != null) {
                billing.append(zone.getName());
            }
            billing.append(LINE_BREAK);
        } else if (!StringUtils.isBlank(order.getBilling().getState())) {
            billing.append(order.getBilling().getState()).append(LINE_BREAK);
        }
        Country country = countries.get(order.getBilling().getCountry().getIsoCode());
        if (country != null) {
            billing.append(country.getName()).append(" ");
        }
        billing.append(order.getBilling().getPostalCode());
        // format shipping address
        StringBuilder shipping = null;
        if (order.getDelivery() != null && !StringUtils.isBlank(order.getDelivery().getFirstName())) {
            shipping = new StringBuilder();
            if (StringUtils.isBlank(order.getDelivery().getCompany())) {
                shipping.append(order.getDelivery().getFirstName()).append(" ").append(order.getDelivery().getLastName()).append(LINE_BREAK);
            } else {
                shipping.append(order.getDelivery().getCompany()).append(LINE_BREAK);
            }
            shipping.append(order.getDelivery().getAddress()).append(LINE_BREAK);
            shipping.append(order.getDelivery().getCity()).append(", ");
            if (order.getDelivery().getZone() != null) {
                Zone zone = zones.get(order.getDelivery().getZone().getCode());
                if (zone != null) {
                    shipping.append(zone.getName());
                }
                shipping.append(LINE_BREAK);
            } else if (!StringUtils.isBlank(order.getDelivery().getState())) {
                shipping.append(order.getDelivery().getState()).append(LINE_BREAK);
            }
            Country deliveryCountry = countries.get(order.getDelivery().getCountry().getIsoCode());
            if (country != null) {
                shipping.append(deliveryCountry.getName()).append(" ");
            }
            shipping.append(order.getDelivery().getPostalCode());
        }
        if (shipping == null && StringUtils.isNotBlank(order.getShippingModuleCode())) {
            // TODO IF HAS NO SHIPPING
            shipping = billing;
        }
        // format order
        // String storeUri = FilePathUtils.buildStoreUri(merchantStore, contextPath);
        StringBuilder orderTable = new StringBuilder();
        orderTable.append(TABLE);
        for (OrderProduct product : order.getOrderProducts()) {
            // Product productModel = productService.getByCode(product.getSku(), language);
            orderTable.append(TR);
            orderTable.append(TD).append(product.getProductName()).append(" - ").append(product.getSku()).append(CLOSING_TD);
            orderTable.append(TD).append(messages.getMessage("label.quantity", customerLocale)).append(": ").append(product.getProductQuantity()).append(CLOSING_TD);
            orderTable.append(TD).append(pricingService.getDisplayAmount(product.getOneTimeCharge(), merchantStore)).append(CLOSING_TD);
            orderTable.append(CLOSING_TR);
        }
        // order totals
        for (OrderTotal total : order.getOrderTotal()) {
            orderTable.append(TR_BORDER);
            // orderTable.append(TD);
            // orderTable.append(CLOSING_TD);
            orderTable.append(TD);
            orderTable.append(CLOSING_TD);
            orderTable.append(TD);
            orderTable.append("<strong>");
            if (total.getModule().equals("tax")) {
                orderTable.append(total.getText()).append(": ");
            } else {
                // if(total.getModule().equals("total") || total.getModule().equals("subtotal")) {
                // }
                orderTable.append(messages.getMessage(total.getOrderTotalCode(), customerLocale)).append(": ");
            // if(total.getModule().equals("total") || total.getModule().equals("subtotal")) {
            // }
            }
            orderTable.append("</strong>");
            orderTable.append(CLOSING_TD);
            orderTable.append(TD);
            orderTable.append("<strong>");
            orderTable.append(pricingService.getDisplayAmount(total.getValue(), merchantStore));
            orderTable.append("</strong>");
            orderTable.append(CLOSING_TD);
            orderTable.append(CLOSING_TR);
        }
        orderTable.append(CLOSING_TABLE);
        Map<String, String> templateTokens = emailUtils.createEmailObjectsMap(contextPath, merchantStore, messages, customerLocale);
        templateTokens.put(EmailConstants.LABEL_HI, messages.getMessage("label.generic.hi", customerLocale));
        templateTokens.put(EmailConstants.EMAIL_CUSTOMER_FIRSTNAME, order.getBilling().getFirstName());
        templateTokens.put(EmailConstants.EMAIL_CUSTOMER_LASTNAME, order.getBilling().getLastName());
        String[] params = { String.valueOf(order.getId()) };
        String[] dt = { DateUtil.formatDate(order.getDatePurchased()) };
        templateTokens.put(EmailConstants.EMAIL_ORDER_NUMBER, messages.getMessage("email.order.confirmation", params, customerLocale));
        templateTokens.put(EmailConstants.EMAIL_ORDER_DATE, messages.getMessage("email.order.ordered", dt, customerLocale));
        templateTokens.put(EmailConstants.EMAIL_ORDER_THANKS, messages.getMessage("email.order.thanks", customerLocale));
        templateTokens.put(EmailConstants.ADDRESS_BILLING, billing.toString());
        templateTokens.put(EmailConstants.ORDER_PRODUCTS_DETAILS, orderTable.toString());
        templateTokens.put(EmailConstants.EMAIL_ORDER_DETAILS_TITLE, messages.getMessage("label.order.details", customerLocale));
        templateTokens.put(EmailConstants.ADDRESS_BILLING_TITLE, messages.getMessage("label.customer.billinginformation", customerLocale));
        templateTokens.put(EmailConstants.PAYMENT_METHOD_TITLE, messages.getMessage("label.order.paymentmode", customerLocale));
        templateTokens.put(EmailConstants.PAYMENT_METHOD_DETAILS, messages.getMessage(new StringBuilder().append("payment.type.").append(order.getPaymentType().name()).toString(), customerLocale, order.getPaymentType().name()));
        if (StringUtils.isNotBlank(order.getShippingModuleCode())) {
            // templateTokens.put(EmailConstants.SHIPPING_METHOD_DETAILS, messages.getMessage(new StringBuilder().append("module.shipping.").append(order.getShippingModuleCode()).toString(),customerLocale,order.getShippingModuleCode()));
            templateTokens.put(EmailConstants.SHIPPING_METHOD_DETAILS, messages.getMessage(new StringBuilder().append("module.shipping.").append(order.getShippingModuleCode()).toString(), new String[] { merchantStore.getStorename() }, customerLocale));
            templateTokens.put(EmailConstants.ADDRESS_SHIPPING_TITLE, messages.getMessage("label.order.shippingmethod", customerLocale));
            templateTokens.put(EmailConstants.ADDRESS_DELIVERY_TITLE, messages.getMessage("label.customer.shippinginformation", customerLocale));
            templateTokens.put(EmailConstants.SHIPPING_METHOD_TITLE, messages.getMessage("label.customer.shippinginformation", customerLocale));
            templateTokens.put(EmailConstants.ADDRESS_DELIVERY, shipping.toString());
        } else {
            templateTokens.put(EmailConstants.SHIPPING_METHOD_DETAILS, "");
            templateTokens.put(EmailConstants.ADDRESS_SHIPPING_TITLE, "");
            templateTokens.put(EmailConstants.ADDRESS_DELIVERY_TITLE, "");
            templateTokens.put(EmailConstants.SHIPPING_METHOD_TITLE, "");
            templateTokens.put(EmailConstants.ADDRESS_DELIVERY, "");
        }
        String status = messages.getMessage("label.order." + order.getStatus().name(), customerLocale, order.getStatus().name());
        String[] statusMessage = { DateUtil.formatDate(order.getDatePurchased()), status };
        templateTokens.put(EmailConstants.ORDER_STATUS, messages.getMessage("email.order.status", statusMessage, customerLocale));
        String[] title = { merchantStore.getStorename(), String.valueOf(order.getId()) };
        Email email = new Email();
        email.setFrom(merchantStore.getStorename());
        email.setFromEmail(merchantStore.getStoreEmailAddress());
        email.setSubject(messages.getMessage("email.order.title", title, customerLocale));
        email.setTo(toEmail);
        email.setTemplateName(EmailConstants.EMAIL_ORDER_TPL);
        email.setTemplateTokens(templateTokens);
        LOGGER.debug("Sending email to {} for order id {} ", customer.getEmailAddress(), order.getId());
        emailService.sendHtmlEmail(merchantStore, email);
    } catch (Exception e) {
        LOGGER.error("Error occured while sending order confirmation email ", e);
    }
}
Also used : Email(com.salesmanager.core.business.modules.email.Email) OrderProduct(com.salesmanager.core.model.order.orderproduct.OrderProduct) Zone(com.salesmanager.core.model.reference.zone.Zone) Country(com.salesmanager.core.model.reference.country.Country) OrderTotal(com.salesmanager.core.model.order.OrderTotal) Async(org.springframework.scheduling.annotation.Async)

Example 27 with Zone

use of com.salesmanager.core.model.reference.zone.Zone in project shopizer by shopizer-ecommerce.

the class ReadableCustomerDeliveryAddressPopulator method populate.

@Override
public ReadableDelivery populate(Delivery source, ReadableDelivery target, MerchantStore store, Language language) throws ConversionException {
    if (countryService == null) {
        throw new ConversionException("countryService must be set");
    }
    if (zoneService == null) {
        throw new ConversionException("zoneService must be set");
    }
    target.setLatitude(source.getLatitude());
    target.setLongitude(source.getLongitude());
    if (StringUtils.isNotBlank(source.getCity())) {
        target.setCity(source.getCity());
    }
    if (StringUtils.isNotBlank(source.getCompany())) {
        target.setCompany(source.getCompany());
    }
    if (StringUtils.isNotBlank(source.getAddress())) {
        target.setAddress(source.getAddress());
    }
    if (StringUtils.isNotBlank(source.getFirstName())) {
        target.setFirstName(source.getFirstName());
    }
    if (StringUtils.isNotBlank(source.getLastName())) {
        target.setLastName(source.getLastName());
    }
    if (StringUtils.isNotBlank(source.getPostalCode())) {
        target.setPostalCode(source.getPostalCode());
    }
    if (StringUtils.isNotBlank(source.getTelephone())) {
        target.setPhone(source.getTelephone());
    }
    target.setStateProvince(source.getState());
    if (source.getTelephone() == null) {
        target.setPhone(source.getTelephone());
    }
    target.setAddress(source.getAddress());
    if (source.getCountry() != null) {
        target.setCountry(source.getCountry().getIsoCode());
        // resolve country name
        try {
            Map<String, Country> countries = countryService.getCountriesMap(language);
            Country c = countries.get(source.getCountry().getIsoCode());
            if (c != null) {
                target.setCountryName(c.getName());
            }
        } catch (ServiceException e) {
            // TODO Auto-generated catch block
            throw new ConversionException(e);
        }
    }
    if (source.getZone() != null) {
        target.setZone(source.getZone().getCode());
        // resolve zone name
        try {
            Map<String, Zone> zones = zoneService.getZones(language);
            Zone z = zones.get(source.getZone().getCode());
            if (z != null) {
                target.setProvinceName(z.getName());
            }
        } catch (ServiceException e) {
            // TODO Auto-generated catch block
            throw new ConversionException(e);
        }
    }
    return target;
}
Also used : ConversionException(com.salesmanager.core.business.exception.ConversionException) ServiceException(com.salesmanager.core.business.exception.ServiceException) Zone(com.salesmanager.core.model.reference.zone.Zone) Country(com.salesmanager.core.model.reference.country.Country)

Example 28 with Zone

use of com.salesmanager.core.model.reference.zone.Zone in project shopizer by shopizer-ecommerce.

the class ReadableCountryPopulator method populate.

@Override
public ReadableCountry populate(Country source, ReadableCountry target, MerchantStore store, Language language) throws ConversionException {
    if (target == null) {
        target = new ReadableCountry();
    }
    target.setId(new Long(source.getId()));
    target.setCode(source.getIsoCode());
    target.setSupported(source.getSupported());
    if (!CollectionUtils.isEmpty(source.getDescriptions())) {
        target.setName(source.getDescriptions().iterator().next().getName());
    }
    if (!CollectionUtils.isEmpty(source.getZones())) {
        for (Zone z : source.getZones()) {
            ReadableZone readableZone = new ReadableZone();
            readableZone.setCountryCode(target.getCode());
            readableZone.setId(z.getId());
            if (!CollectionUtils.isEmpty(z.getDescriptions())) {
                for (ZoneDescription d : z.getDescriptions()) {
                    if (d.getLanguage().getId() == language.getId()) {
                        readableZone.setName(d.getName());
                        continue;
                    }
                }
            }
            target.getZones().add(readableZone);
        }
    }
    return target;
}
Also used : Zone(com.salesmanager.core.model.reference.zone.Zone) ReadableZone(com.salesmanager.shop.model.references.ReadableZone) ReadableZone(com.salesmanager.shop.model.references.ReadableZone) ReadableCountry(com.salesmanager.shop.model.references.ReadableCountry) ZoneDescription(com.salesmanager.core.model.reference.zone.ZoneDescription)

Example 29 with Zone

use of com.salesmanager.core.model.reference.zone.Zone in project shopizer by shopizer-ecommerce.

the class ShoppingOrderConfirmationController method displayConfirmation.

/**
 * Invoked once the payment is complete and order is fulfilled
 * @param model
 * @param request
 * @param response
 * @param locale
 * @return
 * @throws Exception
 */
@RequestMapping("/confirmation.html")
public String displayConfirmation(Model model, HttpServletRequest request, HttpServletResponse response, Locale locale) throws Exception {
    Language language = (Language) request.getAttribute("LANGUAGE");
    MerchantStore store = (MerchantStore) request.getAttribute(Constants.MERCHANT_STORE);
    Long orderId = super.getSessionAttribute(Constants.ORDER_ID, request);
    if (orderId == null) {
        return new StringBuilder().append("redirect:").append(Constants.SHOP_URI).toString();
    }
    // get the order
    Order order = orderService.getById(orderId);
    if (order == null) {
        LOGGER.warn("Order id [" + orderId + "] does not exist");
        throw new Exception("Order id [" + orderId + "] does not exist");
    }
    if (order.getMerchant().getId().intValue() != store.getId().intValue()) {
        LOGGER.warn("Store id [" + store.getId() + "] differs from order.store.id [" + order.getMerchant().getId() + "]");
        return new StringBuilder().append("redirect:").append(Constants.SHOP_URI).toString();
    }
    if (super.getSessionAttribute(Constants.ORDER_ID_TOKEN, request) != null) {
        // set this unique token for performing unique operations in the confirmation
        model.addAttribute("confirmation", "confirmation");
    }
    // remove unique token
    super.removeAttribute(Constants.ORDER_ID_TOKEN, request);
    String[] orderMessageParams = { store.getStorename() };
    String orderMessage = messages.getMessage("label.checkout.text", orderMessageParams, locale);
    model.addAttribute("ordermessage", orderMessage);
    String[] orderIdParams = { String.valueOf(order.getId()) };
    String orderMessageId = messages.getMessage("label.checkout.orderid", orderIdParams, locale);
    model.addAttribute("ordermessageid", orderMessageId);
    String[] orderEmailParams = { order.getCustomerEmailAddress() };
    String orderEmailMessage = messages.getMessage("label.checkout.email", orderEmailParams, locale);
    model.addAttribute("orderemail", orderEmailMessage);
    ReadableOrder readableOrder = orderFacade.getReadableOrder(orderId, store, language);
    // resolve country and Zone for GA
    String countryCode = readableOrder.getCustomer().getBilling().getCountry();
    Map<String, Country> countriesMap = countryService.getCountriesMap(language);
    Country billingCountry = countriesMap.get(countryCode);
    if (billingCountry != null) {
        readableOrder.getCustomer().getBilling().setCountry(billingCountry.getName());
    }
    String zoneCode = readableOrder.getCustomer().getBilling().getZone();
    Map<String, Zone> zonesMap = zoneService.getZones(language);
    Zone billingZone = zonesMap.get(zoneCode);
    if (billingZone != null) {
        readableOrder.getCustomer().getBilling().setZone(billingZone.getName());
    }
    model.addAttribute("order", readableOrder);
    // check if any downloads exist for this order
    List<OrderProductDownload> orderProductDownloads = orderProdctDownloadService.getByOrderId(order.getId());
    if (CollectionUtils.isNotEmpty(orderProductDownloads)) {
        ReadableOrderProductDownloadPopulator populator = new ReadableOrderProductDownloadPopulator();
        List<ReadableOrderProductDownload> downloads = new ArrayList<ReadableOrderProductDownload>();
        for (OrderProductDownload download : orderProductDownloads) {
            ReadableOrderProductDownload view = new ReadableOrderProductDownload();
            populator.populate(download, view, store, language);
            downloads.add(view);
        }
        model.addAttribute("downloads", downloads);
    }
    /**
     * template *
     */
    StringBuilder template = new StringBuilder().append(ControllerConstants.Tiles.Checkout.confirmation).append(".").append(store.getStoreTemplate());
    return template.toString();
}
Also used : Order(com.salesmanager.core.model.order.Order) ReadableOrder(com.salesmanager.shop.model.order.v0.ReadableOrder) ReadableOrderProductDownloadPopulator(com.salesmanager.shop.populator.order.ReadableOrderProductDownloadPopulator) Zone(com.salesmanager.core.model.reference.zone.Zone) ReadableOrderProductDownload(com.salesmanager.shop.model.order.ReadableOrderProductDownload) ArrayList(java.util.ArrayList) ReadableOrder(com.salesmanager.shop.model.order.v0.ReadableOrder) Language(com.salesmanager.core.model.reference.language.Language) Country(com.salesmanager.core.model.reference.country.Country) OrderProductDownload(com.salesmanager.core.model.order.orderproduct.OrderProductDownload) ReadableOrderProductDownload(com.salesmanager.shop.model.order.ReadableOrderProductDownload) MerchantStore(com.salesmanager.core.model.merchant.MerchantStore) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

Zone (com.salesmanager.core.model.reference.zone.Zone)29 Country (com.salesmanager.core.model.reference.country.Country)20 ServiceException (com.salesmanager.core.business.exception.ServiceException)15 Language (com.salesmanager.core.model.reference.language.Language)13 ZoneDescription (com.salesmanager.core.model.reference.zone.ZoneDescription)8 ArrayList (java.util.ArrayList)8 Delivery (com.salesmanager.core.model.common.Delivery)7 MerchantStore (com.salesmanager.core.model.merchant.MerchantStore)7 Billing (com.salesmanager.core.model.common.Billing)6 ConversionException (com.salesmanager.core.business.exception.ConversionException)5 Customer (com.salesmanager.core.model.customer.Customer)5 BigDecimal (java.math.BigDecimal)5 HashMap (java.util.HashMap)5 Map (java.util.Map)4 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)4 Product (com.salesmanager.core.model.catalog.product.Product)3 OrderProduct (com.salesmanager.core.model.order.orderproduct.OrderProduct)3 Currency (com.salesmanager.core.model.reference.currency.Currency)3 ShippingQuote (com.salesmanager.core.model.shipping.ShippingQuote)3 PersistableCustomer (com.salesmanager.shop.model.customer.PersistableCustomer)3