Search in sources :

Example 1 with PersistableOrderProduct

use of com.salesmanager.shop.model.order.PersistableOrderProduct in project shopizer by shopizer-ecommerce.

the class PersistableOrderPopulator method populate.

@Override
public Order populate(PersistableOrder source, Order target, MerchantStore store, Language language) throws ConversionException {
    Validate.notNull(productService, "productService must be set");
    Validate.notNull(digitalProductService, "digitalProductService must be set");
    Validate.notNull(productAttributeService, "productAttributeService must be set");
    Validate.notNull(customerService, "customerService must be set");
    Validate.notNull(countryService, "countryService must be set");
    Validate.notNull(zoneService, "zoneService must be set");
    Validate.notNull(currencyService, "currencyService must be set");
    try {
        Map<String, Country> countriesMap = countryService.getCountriesMap(language);
        Map<String, Zone> zonesMap = zoneService.getZones(language);
        /**
         * customer *
         */
        PersistableCustomer customer = source.getCustomer();
        if (customer != null) {
            if (customer.getId() != null && customer.getId() > 0) {
                Customer modelCustomer = customerService.getById(customer.getId());
                if (modelCustomer == null) {
                    throw new ConversionException("Customer id " + customer.getId() + " does not exists");
                }
                if (modelCustomer.getMerchantStore().getId().intValue() != store.getId().intValue()) {
                    throw new ConversionException("Customer id " + customer.getId() + " does not exists for store " + store.getCode());
                }
                target.setCustomerId(modelCustomer.getId());
                target.setBilling(modelCustomer.getBilling());
                target.setDelivery(modelCustomer.getDelivery());
                target.setCustomerEmailAddress(source.getCustomer().getEmailAddress());
            }
        }
        target.setLocale(LocaleUtils.getLocale(store));
        CreditCard creditCard = source.getCreditCard();
        if (creditCard != null) {
            String maskedNumber = CreditCardUtils.maskCardNumber(creditCard.getCcNumber());
            creditCard.setCcNumber(maskedNumber);
            target.setCreditCard(creditCard);
        }
        Currency currency = null;
        try {
            currency = currencyService.getByCode(source.getCurrency());
        } catch (Exception e) {
            throw new ConversionException("Currency not found for code " + source.getCurrency());
        }
        if (currency == null) {
            throw new ConversionException("Currency not found for code " + source.getCurrency());
        }
        target.setCurrency(currency);
        target.setDatePurchased(source.getDatePurchased());
        // target.setCurrency(store.getCurrency());
        target.setCurrencyValue(new BigDecimal(0));
        target.setMerchant(store);
        target.setStatus(source.getOrderStatus());
        target.setPaymentModuleCode(source.getPaymentModule());
        target.setPaymentType(source.getPaymentType());
        target.setShippingModuleCode(source.getShippingModule());
        target.setCustomerAgreement(source.isCustomerAgreed());
        target.setConfirmedAddress(source.isConfirmedAddress());
        if (source.getPreviousOrderStatus() != null) {
            List<OrderStatus> orderStatusList = source.getPreviousOrderStatus();
            for (OrderStatus status : orderStatusList) {
                OrderStatusHistory statusHistory = new OrderStatusHistory();
                statusHistory.setStatus(status);
                statusHistory.setOrder(target);
                target.getOrderHistory().add(statusHistory);
            }
        }
        if (!StringUtils.isBlank(source.getComments())) {
            OrderStatusHistory statusHistory = new OrderStatusHistory();
            statusHistory.setStatus(null);
            statusHistory.setOrder(target);
            statusHistory.setComments(source.getComments());
            target.getOrderHistory().add(statusHistory);
        }
        List<PersistableOrderProduct> products = source.getOrderProductItems();
        if (CollectionUtils.isEmpty(products)) {
            throw new ConversionException("Requires at least 1 PersistableOrderProduct");
        }
        com.salesmanager.shop.populator.order.PersistableOrderProductPopulator orderProductPopulator = new PersistableOrderProductPopulator();
        orderProductPopulator.setProductAttributeService(productAttributeService);
        orderProductPopulator.setProductService(productService);
        orderProductPopulator.setDigitalProductService(digitalProductService);
        for (PersistableOrderProduct orderProduct : products) {
            OrderProduct modelOrderProduct = new OrderProduct();
            orderProductPopulator.populate(orderProduct, modelOrderProduct, store, language);
            target.getOrderProducts().add(modelOrderProduct);
        }
        List<OrderTotal> orderTotals = source.getTotals();
        if (CollectionUtils.isNotEmpty(orderTotals)) {
            for (OrderTotal total : orderTotals) {
                com.salesmanager.core.model.order.OrderTotal totalModel = new com.salesmanager.core.model.order.OrderTotal();
                totalModel.setModule(total.getModule());
                totalModel.setOrder(target);
                totalModel.setOrderTotalCode(total.getCode());
                totalModel.setTitle(total.getTitle());
                totalModel.setValue(total.getValue());
                target.getOrderTotal().add(totalModel);
            }
        }
    } catch (Exception e) {
        throw new ConversionException(e);
    }
    return target;
}
Also used : OrderProduct(com.salesmanager.core.model.order.orderproduct.OrderProduct) PersistableOrderProduct(com.salesmanager.shop.model.order.PersistableOrderProduct) Customer(com.salesmanager.core.model.customer.Customer) PersistableCustomer(com.salesmanager.shop.model.customer.PersistableCustomer) PersistableCustomer(com.salesmanager.shop.model.customer.PersistableCustomer) PersistableOrderProduct(com.salesmanager.shop.model.order.PersistableOrderProduct) OrderStatus(com.salesmanager.core.model.order.orderstatus.OrderStatus) Currency(com.salesmanager.core.model.reference.currency.Currency) ConversionException(com.salesmanager.core.business.exception.ConversionException) Zone(com.salesmanager.core.model.reference.zone.Zone) CreditCard(com.salesmanager.core.model.order.payment.CreditCard) ConversionException(com.salesmanager.core.business.exception.ConversionException) BigDecimal(java.math.BigDecimal) Country(com.salesmanager.core.model.reference.country.Country) OrderStatusHistory(com.salesmanager.core.model.order.orderstatus.OrderStatusHistory) OrderTotal(com.salesmanager.shop.model.order.total.OrderTotal)

Example 2 with PersistableOrderProduct

use of com.salesmanager.shop.model.order.PersistableOrderProduct in project shopizer by shopizer-ecommerce.

the class ShoppingCartItemPopulator method populate.

@Override
public ShoppingCartItem populate(PersistableOrderProduct source, /**
 * TODO: Fix, target not used possible future bug ! *
 */
ShoppingCartItem target, MerchantStore store, Language language) throws ConversionException {
    Validate.notNull(productService, "Requires to set productService");
    Validate.notNull(productAttributeService, "Requires to set productAttributeService");
    Validate.notNull(shoppingCartService, "Requires to set shoppingCartService");
    Product product = productService.getById(source.getProduct().getId());
    if (source.getAttributes() != null) {
        for (com.salesmanager.shop.model.catalog.product.attribute.ProductAttribute attr : source.getAttributes()) {
            ProductAttribute attribute = productAttributeService.getById(attr.getId());
            if (attribute == null) {
                throw new ConversionException("ProductAttribute with id " + attr.getId() + " is null");
            }
            if (attribute.getProduct().getId().longValue() != source.getProduct().getId().longValue()) {
                throw new ConversionException("ProductAttribute with id " + attr.getId() + " is not assigned to Product id " + source.getProduct().getId());
            }
            product.getAttributes().add(attribute);
        }
    }
    try {
        return shoppingCartService.populateShoppingCartItem(product);
    } catch (ServiceException e) {
        throw new ConversionException(e);
    }
}
Also used : ConversionException(com.salesmanager.core.business.exception.ConversionException) ServiceException(com.salesmanager.core.business.exception.ServiceException) Product(com.salesmanager.core.model.catalog.product.Product) PersistableOrderProduct(com.salesmanager.shop.model.order.PersistableOrderProduct) ProductAttribute(com.salesmanager.core.model.catalog.product.attribute.ProductAttribute)

Example 3 with PersistableOrderProduct

use of com.salesmanager.shop.model.order.PersistableOrderProduct in project shopizer by shopizer-ecommerce.

the class OrderFacadeImpl method calculateOrderTotal.

@Override
public OrderTotalSummary calculateOrderTotal(MerchantStore store, com.salesmanager.shop.model.order.v0.PersistableOrder order, Language language) throws Exception {
    List<PersistableOrderProduct> orderProducts = order.getOrderProductItems();
    ShoppingCartItemPopulator populator = new ShoppingCartItemPopulator();
    populator.setProductAttributeService(productAttributeService);
    populator.setProductService(productService);
    populator.setShoppingCartService(shoppingCartService);
    List<ShoppingCartItem> items = new ArrayList<ShoppingCartItem>();
    for (PersistableOrderProduct orderProduct : orderProducts) {
        ShoppingCartItem item = populator.populate(orderProduct, new ShoppingCartItem(), store, language);
        items.add(item);
    }
    Customer customer = customer(order.getCustomer(), store, language);
    OrderTotalSummary summary = this.calculateOrderTotal(store, customer, order, language);
    return summary;
}
Also used : ShoppingCartItemPopulator(com.salesmanager.shop.populator.order.ShoppingCartItemPopulator) ReadableCustomer(com.salesmanager.shop.model.customer.ReadableCustomer) Customer(com.salesmanager.core.model.customer.Customer) PersistableCustomer(com.salesmanager.shop.model.customer.PersistableCustomer) OrderTotalSummary(com.salesmanager.core.model.order.OrderTotalSummary) ArrayList(java.util.ArrayList) ShoppingCartItem(com.salesmanager.core.model.shoppingcart.ShoppingCartItem) PersistableOrderProduct(com.salesmanager.shop.model.order.PersistableOrderProduct)

Example 4 with PersistableOrderProduct

use of com.salesmanager.shop.model.order.PersistableOrderProduct in project shopizer by shopizer-ecommerce.

the class PersistableOrderProductPopulator method populate.

/**
 * Converts a ShoppingCartItem carried in the ShoppingCart to an OrderProduct
 * that will be saved in the system
 */
@Override
public OrderProduct populate(PersistableOrderProduct source, OrderProduct target, MerchantStore store, Language language) throws ConversionException {
    Validate.notNull(productService, "productService must be set");
    Validate.notNull(digitalProductService, "digitalProductService must be set");
    Validate.notNull(productAttributeService, "productAttributeService must be set");
    try {
        Product modelProduct = productService.getById(source.getProduct().getId());
        if (modelProduct == null) {
            throw new ConversionException("Cannot get product with id (productId) " + source.getProduct().getId());
        }
        if (modelProduct.getMerchantStore().getId().intValue() != store.getId().intValue()) {
            throw new ConversionException("Invalid product id " + source.getProduct().getId());
        }
        DigitalProduct digitalProduct = digitalProductService.getByProduct(store, modelProduct);
        if (digitalProduct != null) {
            OrderProductDownload orderProductDownload = new OrderProductDownload();
            orderProductDownload.setOrderProductFilename(digitalProduct.getProductFileName());
            orderProductDownload.setOrderProduct(target);
            orderProductDownload.setDownloadCount(0);
            orderProductDownload.setMaxdays(ApplicationConstants.MAX_DOWNLOAD_DAYS);
            target.getDownloads().add(orderProductDownload);
        }
        target.setOneTimeCharge(source.getPrice());
        target.setProductName(source.getProduct().getDescription().getName());
        target.setProductQuantity(source.getOrderedQuantity());
        target.setSku(source.getProduct().getSku());
        OrderProductPrice orderProductPrice = new OrderProductPrice();
        orderProductPrice.setDefaultPrice(true);
        orderProductPrice.setProductPrice(source.getPrice());
        orderProductPrice.setOrderProduct(target);
        Set<OrderProductPrice> prices = new HashSet<OrderProductPrice>();
        prices.add(orderProductPrice);
        /**
         * DO NOT SUPPORT MUTIPLE PRICES *
         */
        /*			//Other prices
			List<FinalPrice> otherPrices = finalPrice.getAdditionalPrices();
			if(otherPrices!=null) {
				for(FinalPrice otherPrice : otherPrices) {
					OrderProductPrice other = orderProductPrice(otherPrice);
					other.setOrderProduct(target);
					prices.add(other);
				}
			}*/
        target.setPrices(prices);
        // OrderProductAttribute
        List<ProductAttribute> attributeItems = source.getAttributes();
        if (!CollectionUtils.isEmpty(attributeItems)) {
            Set<OrderProductAttribute> attributes = new HashSet<OrderProductAttribute>();
            for (ProductAttribute attribute : attributeItems) {
                OrderProductAttribute orderProductAttribute = new OrderProductAttribute();
                orderProductAttribute.setOrderProduct(target);
                Long id = attribute.getId();
                com.salesmanager.core.model.catalog.product.attribute.ProductAttribute attr = productAttributeService.getById(id);
                if (attr == null) {
                    throw new ConversionException("Attribute id " + id + " does not exists");
                }
                if (attr.getProduct().getMerchantStore().getId().intValue() != store.getId().intValue()) {
                    throw new ConversionException("Attribute id " + id + " invalid for this store");
                }
                orderProductAttribute.setProductAttributeIsFree(attr.getProductAttributeIsFree());
                orderProductAttribute.setProductAttributeName(attr.getProductOption().getDescriptionsSettoList().get(0).getName());
                orderProductAttribute.setProductAttributeValueName(attr.getProductOptionValue().getDescriptionsSettoList().get(0).getName());
                orderProductAttribute.setProductAttributePrice(attr.getProductAttributePrice());
                orderProductAttribute.setProductAttributeWeight(attr.getProductAttributeWeight());
                orderProductAttribute.setProductOptionId(attr.getProductOption().getId());
                orderProductAttribute.setProductOptionValueId(attr.getProductOptionValue().getId());
                attributes.add(orderProductAttribute);
            }
            target.setOrderAttributes(attributes);
        }
    } catch (Exception e) {
        throw new ConversionException(e);
    }
    return target;
}
Also used : ConversionException(com.salesmanager.core.business.exception.ConversionException) Product(com.salesmanager.core.model.catalog.product.Product) OrderProduct(com.salesmanager.core.model.order.orderproduct.OrderProduct) PersistableOrderProduct(com.salesmanager.shop.model.order.PersistableOrderProduct) DigitalProduct(com.salesmanager.core.model.catalog.product.file.DigitalProduct) OrderProductAttribute(com.salesmanager.core.model.order.orderproduct.OrderProductAttribute) ProductAttribute(com.salesmanager.shop.model.catalog.product.attribute.ProductAttribute) ConversionException(com.salesmanager.core.business.exception.ConversionException) OrderProductPrice(com.salesmanager.core.model.order.orderproduct.OrderProductPrice) OrderProductAttribute(com.salesmanager.core.model.order.orderproduct.OrderProductAttribute) OrderProductDownload(com.salesmanager.core.model.order.orderproduct.OrderProductDownload) DigitalProduct(com.salesmanager.core.model.catalog.product.file.DigitalProduct) HashSet(java.util.HashSet)

Aggregations

PersistableOrderProduct (com.salesmanager.shop.model.order.PersistableOrderProduct)4 ConversionException (com.salesmanager.core.business.exception.ConversionException)3 Product (com.salesmanager.core.model.catalog.product.Product)2 Customer (com.salesmanager.core.model.customer.Customer)2 OrderProduct (com.salesmanager.core.model.order.orderproduct.OrderProduct)2 PersistableCustomer (com.salesmanager.shop.model.customer.PersistableCustomer)2 ServiceException (com.salesmanager.core.business.exception.ServiceException)1 ProductAttribute (com.salesmanager.core.model.catalog.product.attribute.ProductAttribute)1 DigitalProduct (com.salesmanager.core.model.catalog.product.file.DigitalProduct)1 OrderTotalSummary (com.salesmanager.core.model.order.OrderTotalSummary)1 OrderProductAttribute (com.salesmanager.core.model.order.orderproduct.OrderProductAttribute)1 OrderProductDownload (com.salesmanager.core.model.order.orderproduct.OrderProductDownload)1 OrderProductPrice (com.salesmanager.core.model.order.orderproduct.OrderProductPrice)1 OrderStatus (com.salesmanager.core.model.order.orderstatus.OrderStatus)1 OrderStatusHistory (com.salesmanager.core.model.order.orderstatus.OrderStatusHistory)1 CreditCard (com.salesmanager.core.model.order.payment.CreditCard)1 Country (com.salesmanager.core.model.reference.country.Country)1 Currency (com.salesmanager.core.model.reference.currency.Currency)1 Zone (com.salesmanager.core.model.reference.zone.Zone)1 ShoppingCartItem (com.salesmanager.core.model.shoppingcart.ShoppingCartItem)1