Search in sources :

Example 1 with ReadableFinalPricePopulator

use of com.salesmanager.shop.populator.catalog.ReadableFinalPricePopulator in project shopizer by shopizer-ecommerce.

the class ProductVariantApi method calculateVariant.

/**
 * Calculates the price based on selected options if any
 * @param id
 * @param options
 * @param merchantStore
 * @param language
 * @param response
 * @return
 * @throws Exception
 */
@RequestMapping(value = "/products/{id}/variant", method = RequestMethod.POST)
@ResponseStatus(HttpStatus.OK)
@ApiOperation(httpMethod = "POST", value = "Get product price variation based on selected product", notes = "", produces = "application/json", response = ReadableProductPrice.class)
@ResponseBody
@ApiImplicitParams({ @ApiImplicitParam(name = "store", dataType = "String", defaultValue = "DEFAULT"), @ApiImplicitParam(name = "lang", dataType = "String", defaultValue = "en") })
public ReadableProductPrice calculateVariant(@PathVariable final Long id, @RequestBody ReadableSelectedProductVariant options, @ApiIgnore MerchantStore merchantStore, @ApiIgnore Language language, HttpServletResponse response) throws Exception {
    Product product = productService.getById(id);
    if (product == null) {
        response.sendError(404, "Product not fount for id " + id);
        return null;
    }
    List<ReadableProductVariantValue> ids = options.getOptions();
    if (CollectionUtils.isEmpty(ids)) {
        return null;
    }
    List<ReadableProductVariantValue> variants = options.getOptions();
    List<ProductAttribute> attributes = new ArrayList<ProductAttribute>();
    Set<ProductAttribute> productAttributes = product.getAttributes();
    for (ProductAttribute attribute : productAttributes) {
        Long option = attribute.getProductOption().getId();
        Long optionValue = attribute.getProductOptionValue().getId();
        for (ReadableProductVariantValue v : variants) {
            if (v.getOption().longValue() == option.longValue() && v.getValue().longValue() == optionValue.longValue()) {
                attributes.add(attribute);
            }
        }
    }
    FinalPrice price = pricingService.calculateProductPrice(product, attributes);
    ReadableProductPrice readablePrice = new ReadableProductPrice();
    ReadableFinalPricePopulator populator = new ReadableFinalPricePopulator();
    populator.setPricingService(pricingService);
    populator.populate(price, readablePrice, merchantStore, language);
    return readablePrice;
}
Also used : ArrayList(java.util.ArrayList) Product(com.salesmanager.core.model.catalog.product.Product) ProductAttribute(com.salesmanager.core.model.catalog.product.attribute.ProductAttribute) ReadableFinalPricePopulator(com.salesmanager.shop.populator.catalog.ReadableFinalPricePopulator) ReadableProductPrice(com.salesmanager.shop.model.catalog.product.ReadableProductPrice) ReadableProductVariantValue(com.salesmanager.shop.model.catalog.product.attribute.ReadableProductVariantValue) FinalPrice(com.salesmanager.core.model.catalog.product.price.FinalPrice) ApiImplicitParams(io.swagger.annotations.ApiImplicitParams) ResponseStatus(org.springframework.web.bind.annotation.ResponseStatus) ApiOperation(io.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 2 with ReadableFinalPricePopulator

use of com.salesmanager.shop.populator.catalog.ReadableFinalPricePopulator in project shopizer by shopizer-ecommerce.

the class ProductVariantApi method calculateVariant.

/**
 * Calculates the price based on selected options if any
 * @param id
 * @param options
 * @param merchantStore
 * @param language
 * @param response
 * @return
 * @throws Exception
 */
@RequestMapping(value = "/products/{id}/variant", method = RequestMethod.POST)
@ResponseStatus(HttpStatus.OK)
@ApiOperation(httpMethod = "POST", value = "Get product price variation based on selected product", notes = "", produces = "application/json", response = ReadableProductPrice.class)
@ResponseBody
@ApiImplicitParams({ @ApiImplicitParam(name = "store", dataType = "String", defaultValue = "DEFAULT"), @ApiImplicitParam(name = "lang", dataType = "String", defaultValue = "en") })
public ReadableProductPrice calculateVariant(@PathVariable final Long id, @RequestBody ReadableSelectedProductVariant options, @ApiIgnore MerchantStore merchantStore, @ApiIgnore Language language, HttpServletResponse response) throws Exception {
    Product product = productService.getById(id);
    if (product == null) {
        response.sendError(404, "Product not fount for id " + id);
        return null;
    }
    List<ReadableProductVariantValue> ids = options.getOptions();
    if (CollectionUtils.isEmpty(ids)) {
        return null;
    }
    List<ReadableProductVariantValue> variants = options.getOptions();
    List<ProductAttribute> attributes = new ArrayList<ProductAttribute>();
    Set<ProductAttribute> productAttributes = product.getAttributes();
    for (ProductAttribute attribute : productAttributes) {
        Long option = attribute.getProductOption().getId();
        Long optionValue = attribute.getProductOptionValue().getId();
        for (ReadableProductVariantValue v : variants) {
            if (v.getOption().longValue() == option.longValue() && v.getValue().longValue() == optionValue.longValue()) {
                attributes.add(attribute);
            }
        }
    }
    FinalPrice price = pricingService.calculateProductPrice(product, attributes);
    ReadableProductPrice readablePrice = new ReadableProductPrice();
    ReadableFinalPricePopulator populator = new ReadableFinalPricePopulator();
    populator.setPricingService(pricingService);
    populator.populate(price, readablePrice, merchantStore, language);
    return readablePrice;
}
Also used : ArrayList(java.util.ArrayList) Product(com.salesmanager.core.model.catalog.product.Product) ProductAttribute(com.salesmanager.core.model.catalog.product.attribute.ProductAttribute) ReadableFinalPricePopulator(com.salesmanager.shop.populator.catalog.ReadableFinalPricePopulator) ReadableProductPrice(com.salesmanager.shop.model.catalog.product.ReadableProductPrice) ReadableProductVariantValue(com.salesmanager.shop.model.catalog.product.attribute.ReadableProductVariantValue) FinalPrice(com.salesmanager.core.model.catalog.product.price.FinalPrice) ApiImplicitParams(io.swagger.annotations.ApiImplicitParams) ResponseStatus(org.springframework.web.bind.annotation.ResponseStatus) ApiOperation(io.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 3 with ReadableFinalPricePopulator

use of com.salesmanager.shop.populator.catalog.ReadableFinalPricePopulator in project shopizer by shopizer-ecommerce.

the class ProductFacadeV2Impl method getProductPrice.

@Override
public ReadableProductPrice getProductPrice(Long id, ProductPriceRequest priceRequest, MerchantStore store, Language language) {
    Validate.notNull(id, "Product id cannot be null");
    Validate.notNull(priceRequest, "Product price request cannot be null");
    Validate.notNull(store, "MerchantStore cannot be null");
    Validate.notNull(language, "Language cannot be null");
    try {
        Product model = productService.findOne(id, store);
        List<ProductAttribute> attributes = null;
        if (!CollectionUtils.isEmpty(priceRequest.getOptions())) {
            List<Long> attrinutesIds = priceRequest.getOptions().stream().map(p -> p.getId()).collect(Collectors.toList());
            attributes = productAttributeService.getByAttributeIds(store, model, attrinutesIds);
            for (ProductAttribute attribute : attributes) {
                if (attribute.getProduct().getId().longValue() != id.longValue()) {
                    // throw unauthorized
                    throw new OperationNotAllowedException("Attribute with id [" + attribute.getId() + "] is not attached to product id [" + id + "]");
                }
            }
        }
        if (!StringUtils.isBlank(priceRequest.getSku())) {
            // change default availability with sku (instance availability)
            List<ProductAvailability> availabilityList = productAvailabilityService.getBySku(priceRequest.getSku(), store);
            if (CollectionUtils.isNotEmpty(availabilityList)) {
                model.setAvailabilities(new HashSet(availabilityList));
            }
        }
        FinalPrice price;
        // attributes can be null;
        price = pricingService.calculateProductPrice(model, attributes);
        ReadableProductPrice readablePrice = new ReadableProductPrice();
        ReadableFinalPricePopulator populator = new ReadableFinalPricePopulator();
        populator.setPricingService(pricingService);
        return populator.populate(price, readablePrice, store, language);
    } catch (Exception e) {
        throw new ServiceRuntimeException("An error occured while getting product price", e);
    }
}
Also used : ProductService(com.salesmanager.core.business.services.catalog.product.ProductService) ReadableProductInstance(com.salesmanager.shop.model.catalog.product.product.instance.ReadableProductInstance) Autowired(org.springframework.beans.factory.annotation.Autowired) ProductFacade(com.salesmanager.shop.store.controller.product.facade.ProductFacade) StringUtils(org.apache.commons.lang3.StringUtils) CollectionUtils(org.apache.commons.collections4.CollectionUtils) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Inject(javax.inject.Inject) Language(com.salesmanager.core.model.reference.language.Language) ReadableFinalPricePopulator(com.salesmanager.shop.populator.catalog.ReadableFinalPricePopulator) MerchantStore(com.salesmanager.core.model.merchant.MerchantStore) FinalPrice(com.salesmanager.core.model.catalog.product.price.FinalPrice) ProductInstanceService(com.salesmanager.core.business.services.catalog.product.instance.ProductInstanceService) ResourceNotFoundException(com.salesmanager.shop.store.api.exception.ResourceNotFoundException) Service(org.springframework.stereotype.Service) Qualifier(org.springframework.beans.factory.annotation.Qualifier) PricingService(com.salesmanager.core.business.services.catalog.product.PricingService) CategoryService(com.salesmanager.core.business.services.catalog.category.CategoryService) ReadableProductPrice(com.salesmanager.shop.model.catalog.product.ReadableProductPrice) ProductAttributeService(com.salesmanager.core.business.services.catalog.product.attribute.ProductAttributeService) ReadableProduct(com.salesmanager.shop.model.catalog.product.ReadableProduct) OperationNotAllowedException(com.salesmanager.shop.store.api.exception.OperationNotAllowedException) ProductAttribute(com.salesmanager.core.model.catalog.product.attribute.ProductAttribute) Product(com.salesmanager.core.model.catalog.product.Product) ProductCriteria(com.salesmanager.core.model.catalog.product.ProductCriteria) ProductPriceRequest(com.salesmanager.shop.model.catalog.product.ProductPriceRequest) ProductRelationshipService(com.salesmanager.core.business.services.catalog.product.relationship.ProductRelationshipService) ImageFilePath(com.salesmanager.shop.utils.ImageFilePath) ProductAvailabilityService(com.salesmanager.core.business.services.catalog.product.availability.ProductAvailabilityService) Page(org.springframework.data.domain.Page) ProductInstance(com.salesmanager.core.model.catalog.product.instance.ProductInstance) Collectors(java.util.stream.Collectors) Profile(org.springframework.context.annotation.Profile) List(java.util.List) LocaleUtils(com.salesmanager.shop.utils.LocaleUtils) Validate(org.apache.commons.lang3.Validate) ProductRelationship(com.salesmanager.core.model.catalog.product.relationship.ProductRelationship) ReadableProductPopulator(com.salesmanager.shop.populator.catalog.ReadableProductPopulator) ServiceRuntimeException(com.salesmanager.shop.store.api.exception.ServiceRuntimeException) ProductRelationshipType(com.salesmanager.core.model.catalog.product.relationship.ProductRelationshipType) ReadableProductMapper(com.salesmanager.shop.mapper.catalog.ReadableProductMapper) ProductAvailability(com.salesmanager.core.model.catalog.product.availability.ProductAvailability) Comparator(java.util.Comparator) ReadableProductList(com.salesmanager.shop.model.catalog.product.ReadableProductList) ReadableProductInstanceMapper(com.salesmanager.shop.mapper.catalog.product.ReadableProductInstanceMapper) ReadableProduct(com.salesmanager.shop.model.catalog.product.ReadableProduct) Product(com.salesmanager.core.model.catalog.product.Product) ProductAttribute(com.salesmanager.core.model.catalog.product.attribute.ProductAttribute) ReadableFinalPricePopulator(com.salesmanager.shop.populator.catalog.ReadableFinalPricePopulator) OperationNotAllowedException(com.salesmanager.shop.store.api.exception.OperationNotAllowedException) ReadableProductPrice(com.salesmanager.shop.model.catalog.product.ReadableProductPrice) ResourceNotFoundException(com.salesmanager.shop.store.api.exception.ResourceNotFoundException) OperationNotAllowedException(com.salesmanager.shop.store.api.exception.OperationNotAllowedException) ServiceRuntimeException(com.salesmanager.shop.store.api.exception.ServiceRuntimeException) ServiceRuntimeException(com.salesmanager.shop.store.api.exception.ServiceRuntimeException) ProductAvailability(com.salesmanager.core.model.catalog.product.availability.ProductAvailability) HashSet(java.util.HashSet) FinalPrice(com.salesmanager.core.model.catalog.product.price.FinalPrice)

Example 4 with ReadableFinalPricePopulator

use of com.salesmanager.shop.populator.catalog.ReadableFinalPricePopulator in project shopizer by shopizer-ecommerce.

the class ProductFacadeImpl method getProductPrice.

@Override
public ReadableProductPrice getProductPrice(Long id, ProductPriceRequest priceRequest, MerchantStore store, Language language) {
    Validate.notNull(id, "Product id cannot be null");
    Validate.notNull(priceRequest, "Product price request cannot be null");
    Validate.notNull(store, "MerchantStore cannot be null");
    Validate.notNull(language, "Language cannot be null");
    try {
        Product model = productService.findOne(id, store);
        // TODO check if null
        List<Long> attrinutesIds = priceRequest.getOptions().stream().map(p -> p.getId()).collect(Collectors.toList());
        List<ProductAttribute> attributes = productAttributeService.getByAttributeIds(store, model, attrinutesIds);
        for (ProductAttribute attribute : attributes) {
            if (attribute.getProduct().getId().longValue() != id.longValue()) {
                // throw unauthorized
                throw new OperationNotAllowedException("Attribute with id [" + attribute.getId() + "] is not attached to product id [" + id + "]");
            }
        }
        FinalPrice price;
        price = pricingService.calculateProductPrice(model, attributes);
        ReadableProductPrice readablePrice = new ReadableProductPrice();
        ReadableFinalPricePopulator populator = new ReadableFinalPricePopulator();
        populator.setPricingService(pricingService);
        return populator.populate(price, readablePrice, store, language);
    } catch (Exception e) {
        throw new ServiceRuntimeException("An error occured while getting product price", e);
    }
}
Also used : ProductService(com.salesmanager.core.business.services.catalog.product.ProductService) ProductFacade(com.salesmanager.shop.store.controller.product.facade.ProductFacade) CollectionUtils(org.apache.commons.collections4.CollectionUtils) ArrayList(java.util.ArrayList) Inject(javax.inject.Inject) Language(com.salesmanager.core.model.reference.language.Language) PersistableProduct(com.salesmanager.shop.model.catalog.product.PersistableProduct) ReadableFinalPricePopulator(com.salesmanager.shop.populator.catalog.ReadableFinalPricePopulator) MerchantStore(com.salesmanager.core.model.merchant.MerchantStore) ConversionRuntimeException(com.salesmanager.shop.store.api.exception.ConversionRuntimeException) FinalPrice(com.salesmanager.core.model.catalog.product.price.FinalPrice) Service(org.springframework.stereotype.Service) Qualifier(org.springframework.beans.factory.annotation.Qualifier) PricingService(com.salesmanager.core.business.services.catalog.product.PricingService) CategoryService(com.salesmanager.core.business.services.catalog.category.CategoryService) ReadableProductPrice(com.salesmanager.shop.model.catalog.product.ReadableProductPrice) ProductAttributeService(com.salesmanager.core.business.services.catalog.product.attribute.ProductAttributeService) ReadableProduct(com.salesmanager.shop.model.catalog.product.ReadableProduct) OperationNotAllowedException(com.salesmanager.shop.store.api.exception.OperationNotAllowedException) ProductAttribute(com.salesmanager.core.model.catalog.product.attribute.ProductAttribute) Product(com.salesmanager.core.model.catalog.product.Product) ProductCriteria(com.salesmanager.core.model.catalog.product.ProductCriteria) ProductPriceRequest(com.salesmanager.shop.model.catalog.product.ProductPriceRequest) ProductRelationshipService(com.salesmanager.core.business.services.catalog.product.relationship.ProductRelationshipService) ImageFilePath(com.salesmanager.shop.utils.ImageFilePath) Page(org.springframework.data.domain.Page) Collectors(java.util.stream.Collectors) Profile(org.springframework.context.annotation.Profile) List(java.util.List) LocaleUtils(com.salesmanager.shop.utils.LocaleUtils) Validate(org.apache.commons.lang3.Validate) ProductRelationship(com.salesmanager.core.model.catalog.product.relationship.ProductRelationship) ReadableProductPopulator(com.salesmanager.shop.populator.catalog.ReadableProductPopulator) ServiceRuntimeException(com.salesmanager.shop.store.api.exception.ServiceRuntimeException) ProductRelationshipType(com.salesmanager.core.model.catalog.product.relationship.ProductRelationshipType) ConversionException(com.salesmanager.core.business.exception.ConversionException) Comparator(java.util.Comparator) ReadableProductList(com.salesmanager.shop.model.catalog.product.ReadableProductList) PersistableProduct(com.salesmanager.shop.model.catalog.product.PersistableProduct) ReadableProduct(com.salesmanager.shop.model.catalog.product.ReadableProduct) Product(com.salesmanager.core.model.catalog.product.Product) ProductAttribute(com.salesmanager.core.model.catalog.product.attribute.ProductAttribute) ReadableFinalPricePopulator(com.salesmanager.shop.populator.catalog.ReadableFinalPricePopulator) OperationNotAllowedException(com.salesmanager.shop.store.api.exception.OperationNotAllowedException) ReadableProductPrice(com.salesmanager.shop.model.catalog.product.ReadableProductPrice) ConversionRuntimeException(com.salesmanager.shop.store.api.exception.ConversionRuntimeException) OperationNotAllowedException(com.salesmanager.shop.store.api.exception.OperationNotAllowedException) ServiceRuntimeException(com.salesmanager.shop.store.api.exception.ServiceRuntimeException) ConversionException(com.salesmanager.core.business.exception.ConversionException) FinalPrice(com.salesmanager.core.model.catalog.product.price.FinalPrice) ServiceRuntimeException(com.salesmanager.shop.store.api.exception.ServiceRuntimeException)

Example 5 with ReadableFinalPricePopulator

use of com.salesmanager.shop.populator.catalog.ReadableFinalPricePopulator in project shopizer by shopizer-ecommerce.

the class ShopProductController method calculatePrice.

@RequestMapping(value = { "/{productId}/calculatePrice.json" }, method = RequestMethod.POST)
@ResponseBody
public ReadableProductPrice calculatePrice(@RequestParam(value = "attributeIds[]") Long[] attributeIds, @PathVariable final Long productId, final HttpServletRequest request, final HttpServletResponse response, final Locale locale) throws Exception {
    MerchantStore store = (MerchantStore) request.getAttribute(Constants.MERCHANT_STORE);
    Language language = (Language) request.getAttribute("LANGUAGE");
    Product product = productService.getById(productId);
    @SuppressWarnings("unchecked") List<Long> ids = new ArrayList<Long>(Arrays.asList(attributeIds));
    List<ProductAttribute> attributes = productAttributeService.getByAttributeIds(store, product, ids);
    for (ProductAttribute attribute : attributes) {
        if (attribute.getProduct().getId().longValue() != productId.longValue()) {
            return null;
        }
    }
    FinalPrice price = pricingService.calculateProductPrice(product, attributes);
    ReadableProductPrice readablePrice = new ReadableProductPrice();
    ReadableFinalPricePopulator populator = new ReadableFinalPricePopulator();
    populator.setPricingService(pricingService);
    populator.populate(price, readablePrice, store, language);
    return readablePrice;
}
Also used : Language(com.salesmanager.core.model.reference.language.Language) ArrayList(java.util.ArrayList) ReadableProduct(com.salesmanager.shop.model.catalog.product.ReadableProduct) Product(com.salesmanager.core.model.catalog.product.Product) ProductAttribute(com.salesmanager.core.model.catalog.product.attribute.ProductAttribute) ReadableFinalPricePopulator(com.salesmanager.shop.populator.catalog.ReadableFinalPricePopulator) MerchantStore(com.salesmanager.core.model.merchant.MerchantStore) ReadableProductPrice(com.salesmanager.shop.model.catalog.product.ReadableProductPrice) FinalPrice(com.salesmanager.core.model.catalog.product.price.FinalPrice) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Aggregations

Product (com.salesmanager.core.model.catalog.product.Product)5 ProductAttribute (com.salesmanager.core.model.catalog.product.attribute.ProductAttribute)5 FinalPrice (com.salesmanager.core.model.catalog.product.price.FinalPrice)5 ReadableProductPrice (com.salesmanager.shop.model.catalog.product.ReadableProductPrice)5 ReadableFinalPricePopulator (com.salesmanager.shop.populator.catalog.ReadableFinalPricePopulator)5 ArrayList (java.util.ArrayList)5 MerchantStore (com.salesmanager.core.model.merchant.MerchantStore)3 Language (com.salesmanager.core.model.reference.language.Language)3 ReadableProduct (com.salesmanager.shop.model.catalog.product.ReadableProduct)3 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)3 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)3 CategoryService (com.salesmanager.core.business.services.catalog.category.CategoryService)2 PricingService (com.salesmanager.core.business.services.catalog.product.PricingService)2 ProductService (com.salesmanager.core.business.services.catalog.product.ProductService)2 ProductAttributeService (com.salesmanager.core.business.services.catalog.product.attribute.ProductAttributeService)2 ProductRelationshipService (com.salesmanager.core.business.services.catalog.product.relationship.ProductRelationshipService)2 ProductCriteria (com.salesmanager.core.model.catalog.product.ProductCriteria)2 ProductRelationship (com.salesmanager.core.model.catalog.product.relationship.ProductRelationship)2 ProductRelationshipType (com.salesmanager.core.model.catalog.product.relationship.ProductRelationshipType)2 ProductPriceRequest (com.salesmanager.shop.model.catalog.product.ProductPriceRequest)2