Search in sources :

Example 1 with ReadableProductPrice

use of com.salesmanager.shop.model.catalog.product.ReadableProductPrice 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 ReadableProductPrice

use of com.salesmanager.shop.model.catalog.product.ReadableProductPrice in project shopizer by shopizer-ecommerce.

the class ReadableInventoryMapper method prices.

private List<ReadableProductPrice> prices(ProductAvailability source, MerchantStore store, Language language) throws ConversionException {
    ReadableProductPricePopulator populator = null;
    List<ReadableProductPrice> prices = new ArrayList<ReadableProductPrice>();
    for (ProductPrice price : source.getPrices()) {
        populator = new ReadableProductPricePopulator();
        populator.setPricingService(pricingService);
        ReadableProductPrice p = populator.populate(price, new ReadableProductPrice(), store, language);
        prices.add(p);
    }
    return prices;
}
Also used : ReadableProductPricePopulator(com.salesmanager.shop.populator.catalog.ReadableProductPricePopulator) ArrayList(java.util.ArrayList) ProductPrice(com.salesmanager.core.model.catalog.product.price.ProductPrice) ReadableProductPrice(com.salesmanager.shop.model.catalog.product.ReadableProductPrice) ReadableProductPrice(com.salesmanager.shop.model.catalog.product.ReadableProductPrice)

Example 3 with ReadableProductPrice

use of com.salesmanager.shop.model.catalog.product.ReadableProductPrice in project shopizer by shopizer-ecommerce.

the class ReadableInventoryMapper method merge.

@Override
public ReadableInventory merge(ProductAvailability source, ReadableInventory destination, MerchantStore store, Language language) {
    Validate.notNull(destination, "Destination Product availability cannot be null");
    Validate.notNull(source, "Source Product availability cannot be null");
    try {
        destination.setQuantity(source.getProductQuantity() != null ? source.getProductQuantity().intValue() : 0);
        destination.setProductQuantityOrderMax(source.getProductQuantityOrderMax() != null ? source.getProductQuantityOrderMax().intValue() : 0);
        destination.setProductQuantityOrderMin(source.getProductQuantityOrderMin() != null ? source.getProductQuantityOrderMin().intValue() : 0);
        destination.setOwner(source.getOwner());
        destination.setId(source.getId());
        destination.setRegion(source.getRegion());
        destination.setRegionVariant(source.getRegionVariant());
        destination.setStore(store(store, language));
        if (source.getAvailable() != null) {
            if (source.getProductDateAvailable() != null) {
                boolean isAfter = LocalDate.parse(DateUtil.getPresentDate()).isAfter(LocalDate.parse(DateUtil.formatDate(source.getProductDateAvailable())));
                if (isAfter && source.getAvailable().booleanValue()) {
                    destination.setAvailable(true);
                }
                destination.setDateAvailable(DateUtil.formatDate(source.getProductDateAvailable()));
            } else {
                destination.setAvailable(source.getAvailable().booleanValue());
            }
        }
        if (source.getAuditSection() != null) {
            if (source.getAuditSection().getDateCreated() != null) {
                destination.setCreationDate(DateUtil.formatDate(source.getAuditSection().getDateCreated()));
            }
        }
        List<ReadableProductPrice> prices = prices(source, store, language);
        destination.setPrices(prices);
    // not necessary when getting an inventory
    // if(source.getProductInstance() != null) {
    // destination.setInstance(readableProductInstanceMapper.convert(source.getProductInstance(), store, language));
    // }
    } catch (Exception e) {
        throw new ConversionRuntimeException("Error while converting Inventory", e);
    }
    return destination;
}
Also used : ReadableProductPrice(com.salesmanager.shop.model.catalog.product.ReadableProductPrice) ConversionRuntimeException(com.salesmanager.shop.store.api.exception.ConversionRuntimeException) ConversionException(com.salesmanager.core.business.exception.ConversionException) ConversionRuntimeException(com.salesmanager.shop.store.api.exception.ConversionRuntimeException)

Example 4 with ReadableProductPrice

use of com.salesmanager.shop.model.catalog.product.ReadableProductPrice 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 5 with ReadableProductPrice

use of com.salesmanager.shop.model.catalog.product.ReadableProductPrice 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)

Aggregations

ReadableProductPrice (com.salesmanager.shop.model.catalog.product.ReadableProductPrice)9 ArrayList (java.util.ArrayList)8 Product (com.salesmanager.core.model.catalog.product.Product)7 ProductAttribute (com.salesmanager.core.model.catalog.product.attribute.ProductAttribute)7 FinalPrice (com.salesmanager.core.model.catalog.product.price.FinalPrice)7 MerchantStore (com.salesmanager.core.model.merchant.MerchantStore)5 Language (com.salesmanager.core.model.reference.language.Language)5 ReadableProduct (com.salesmanager.shop.model.catalog.product.ReadableProduct)5 ReadableFinalPricePopulator (com.salesmanager.shop.populator.catalog.ReadableFinalPricePopulator)5 PricingService (com.salesmanager.core.business.services.catalog.product.PricingService)4 ImageFilePath (com.salesmanager.shop.utils.ImageFilePath)4 List (java.util.List)4 Collectors (java.util.stream.Collectors)4 CollectionUtils (org.apache.commons.collections4.CollectionUtils)4 Validate (org.apache.commons.lang3.Validate)4 ConversionException (com.salesmanager.core.business.exception.ConversionException)3 ProductAvailability (com.salesmanager.core.model.catalog.product.availability.ProductAvailability)3 ProductPrice (com.salesmanager.core.model.catalog.product.price.ProductPrice)3 Comparator (java.util.Comparator)3 StringUtils (org.apache.commons.lang3.StringUtils)3