Search in sources :

Example 16 with Product

use of com.salesmanager.core.model.catalog.product.Product in project shopizer by shopizer-ecommerce.

the class PackingBox method getBoxPackagesDetails.

@Override
public List<PackageDetails> getBoxPackagesDetails(List<ShippingProduct> products, MerchantStore store) throws ServiceException {
    if (products == null) {
        throw new ServiceException("Product list cannot be null !!");
    }
    double width = 0;
    double length = 0;
    double height = 0;
    double weight = 0;
    double maxweight = 0;
    // int treshold = 0;
    ShippingConfiguration shippingConfiguration = shippingService.getShippingConfiguration(store);
    if (shippingConfiguration == null) {
        throw new ServiceException("ShippingConfiguration not found for merchant " + store.getCode());
    }
    width = (double) shippingConfiguration.getBoxWidth();
    length = (double) shippingConfiguration.getBoxLength();
    height = (double) shippingConfiguration.getBoxHeight();
    weight = shippingConfiguration.getBoxWeight();
    maxweight = shippingConfiguration.getMaxWeight();
    List<PackageDetails> boxes = new ArrayList<PackageDetails>();
    // maximum number of boxes
    int maxBox = 100;
    int iterCount = 0;
    List<Product> individualProducts = new ArrayList<Product>();
    // need to put items individually
    for (ShippingProduct shippingProduct : products) {
        Product product = shippingProduct.getProduct();
        if (product.isProductVirtual()) {
            continue;
        }
        int qty = shippingProduct.getQuantity();
        Set<ProductAttribute> attrs = shippingProduct.getProduct().getAttributes();
        // set attributes values
        BigDecimal w = product.getProductWeight();
        BigDecimal h = product.getProductHeight();
        BigDecimal l = product.getProductLength();
        BigDecimal wd = product.getProductWidth();
        if (w == null) {
            w = new BigDecimal(defaultWeight);
        }
        if (h == null) {
            h = new BigDecimal(defaultHeight);
        }
        if (l == null) {
            l = new BigDecimal(defaultLength);
        }
        if (wd == null) {
            wd = new BigDecimal(defaultWidth);
        }
        if (attrs != null && attrs.size() > 0) {
            for (ProductAttribute attribute : attrs) {
                if (attribute.getProductAttributeWeight() != null) {
                    w = w.add(attribute.getProductAttributeWeight());
                }
            }
        }
        if (qty > 1) {
            for (int i = 1; i <= qty; i++) {
                Product temp = new Product();
                temp.setProductHeight(h);
                temp.setProductLength(l);
                temp.setProductWidth(wd);
                temp.setProductWeight(w);
                temp.setAttributes(product.getAttributes());
                temp.setDescriptions(product.getDescriptions());
                individualProducts.add(temp);
            }
        } else {
            Product temp = new Product();
            temp.setProductHeight(h);
            temp.setProductLength(l);
            temp.setProductWidth(wd);
            temp.setProductWeight(w);
            temp.setAttributes(product.getAttributes());
            temp.setDescriptions(product.getDescriptions());
            individualProducts.add(temp);
        }
        iterCount++;
    }
    if (iterCount == 0) {
        return null;
    }
    int productCount = individualProducts.size();
    List<PackingBox> boxesList = new ArrayList<PackingBox>();
    // start the creation of boxes
    PackingBox box = new PackingBox();
    // set box max volume
    double maxVolume = width * length * height;
    if (maxVolume == 0 || maxweight == 0) {
        merchantLogService.save(new MerchantLog(store, "shipping", "Check shipping box configuration, it has a volume of " + maxVolume + " and a maximum weight of " + maxweight + ". Those values must be greater than 0."));
        throw new ServiceException("Product configuration exceeds box configuraton");
    }
    box.setVolumeLeft(maxVolume);
    box.setWeightLeft(maxweight);
    // assign first box
    boxesList.add(box);
    // int boxCount = 1;
    List<Product> assignedProducts = new ArrayList<Product>();
    // calculate the volume for the next object
    if (assignedProducts.size() > 0) {
        individualProducts.removeAll(assignedProducts);
        assignedProducts = new ArrayList<Product>();
    }
    boolean productAssigned = false;
    for (Product p : individualProducts) {
        // Set<ProductAttribute> attributes = p.getAttributes();
        productAssigned = false;
        double productWeight = p.getProductWeight().doubleValue();
        // validate if product fits in the box
        if (p.getProductWidth().doubleValue() > width || p.getProductHeight().doubleValue() > height || p.getProductLength().doubleValue() > length) {
            // log message to customer
            merchantLogService.save(new MerchantLog(store, "shipping", "Product " + p.getSku() + " has a demension larger than the box size specified. Will use per item calculation."));
            throw new ServiceException("Product configuration exceeds box configuraton");
        }
        if (productWeight > maxweight) {
            merchantLogService.save(new MerchantLog(store, "shipping", "Product " + p.getSku() + " has a weight larger than the box maximum weight specified. Will use per item calculation."));
            throw new ServiceException("Product configuration exceeds box configuraton");
        }
        double productVolume = (p.getProductWidth().doubleValue() * p.getProductHeight().doubleValue() * p.getProductLength().doubleValue());
        if (productVolume == 0) {
            merchantLogService.save(new MerchantLog(store, "shipping", "Product " + p.getSku() + " has one of the dimension set to 0 and therefore cannot calculate the volume"));
            throw new ServiceException("Product configuration exceeds box configuraton");
        }
        if (productVolume > maxVolume) {
            throw new ServiceException("Product configuration exceeds box configuraton");
        }
        // Iterator boxIter = boxesList.iterator();
        for (PackingBox pbox : boxesList) {
            double volumeLeft = pbox.getVolumeLeft();
            double weightLeft = pbox.getWeightLeft();
            if ((volumeLeft * .75) >= productVolume && pbox.getWeightLeft() >= productWeight) {
                // fit the item
                // in this
                // box
                // fit in the current box
                volumeLeft = volumeLeft - productVolume;
                pbox.setVolumeLeft(volumeLeft);
                weightLeft = weightLeft - productWeight;
                pbox.setWeightLeft(weightLeft);
                assignedProducts.add(p);
                productCount--;
                double w = pbox.getWeight();
                w = w + productWeight;
                pbox.setWeight(w);
                productAssigned = true;
                maxBox--;
                break;
            }
        }
        if (!productAssigned) {
            // create a new box
            box = new PackingBox();
            // set box max volume
            box.setVolumeLeft(maxVolume);
            box.setWeightLeft(maxweight);
            boxesList.add(box);
            double volumeLeft = box.getVolumeLeft() - productVolume;
            box.setVolumeLeft(volumeLeft);
            double weightLeft = box.getWeightLeft() - productWeight;
            box.setWeightLeft(weightLeft);
            assignedProducts.add(p);
            productCount--;
            double w = box.getWeight();
            w = w + productWeight;
            box.setWeight(w);
            maxBox--;
        }
    }
    // now prepare the shipping info
    // number of boxes
    // Iterator ubIt = usedBoxesList.iterator();
    System.out.println("###################################");
    System.out.println("Number of boxes " + boxesList.size());
    System.out.println("###################################");
    for (PackingBox pb : boxesList) {
        PackageDetails details = new PackageDetails();
        details.setShippingHeight(height);
        details.setShippingLength(length);
        details.setShippingWeight(weight + box.getWeight());
        details.setShippingWidth(width);
        details.setItemName(store.getCode());
        boxes.add(details);
    }
    return boxes;
}
Also used : ArrayList(java.util.ArrayList) Product(com.salesmanager.core.model.catalog.product.Product) ShippingProduct(com.salesmanager.core.model.shipping.ShippingProduct) ShippingProduct(com.salesmanager.core.model.shipping.ShippingProduct) ProductAttribute(com.salesmanager.core.model.catalog.product.attribute.ProductAttribute) BigDecimal(java.math.BigDecimal) ShippingConfiguration(com.salesmanager.core.model.shipping.ShippingConfiguration) ServiceException(com.salesmanager.core.business.exception.ServiceException) PackageDetails(com.salesmanager.core.model.shipping.PackageDetails) MerchantLog(com.salesmanager.core.model.system.MerchantLog)

Example 17 with Product

use of com.salesmanager.core.model.catalog.product.Product in project shopizer by shopizer-ecommerce.

the class ProductRepositoryImpl method getByCode.

@Override
public Product getByCode(String productCode, MerchantStore store) {
    try {
        StringBuilder qs = new StringBuilder();
        qs.append("select distinct p from Product as p ");
        qs.append("join fetch p.descriptions pd ");
        qs.append("join fetch p.merchantStore pm ");
        qs.append("left join fetch p.categories categs ");
        qs.append("left join fetch categs.descriptions categsd ");
        // options
        qs.append("left join fetch p.attributes pattr ");
        qs.append("left join fetch pattr.productOption po ");
        qs.append("left join fetch po.descriptions pod ");
        qs.append("left join fetch pattr.productOptionValue pov ");
        qs.append("left join fetch pov.descriptions povd ");
        qs.append("left join fetch p.relationships pr ");
        // other lefts
        qs.append("left join fetch p.manufacturer manuf ");
        qs.append("left join fetch manuf.descriptions manufd ");
        qs.append("left join fetch p.type type ");
        qs.append("where p.sku=:code and pm.id=:id");
        String hql = qs.toString();
        Query q = this.em.createQuery(hql);
        q.setParameter("code", productCode);
        q.setParameter("id", store.getId());
        return (Product) q.getSingleResult();
    } catch (javax.persistence.NoResultException ers) {
        return null;
    }
}
Also used : Query(javax.persistence.Query) Product(com.salesmanager.core.model.catalog.product.Product) NoResultException(javax.persistence.NoResultException)

Example 18 with Product

use of com.salesmanager.core.model.catalog.product.Product in project shopizer by shopizer-ecommerce.

the class ProductRepositoryImpl method getProductsListByCategories.

@Override
public List<Product> getProductsListByCategories(Set<Long> categoryIds, Language language) {
    // List regionList = new ArrayList();
    // regionList.add("*");
    // regionList.add(locale.getCountry());
    // TODO Test performance
    /**
     * Testing in debug mode takes a long time with this query running in
     * normal mode is fine
     */
    StringBuilder qs = new StringBuilder();
    qs.append("select distinct p from Product as p ");
    qs.append("join fetch p.merchantStore merch ");
    qs.append("join fetch p.availabilities pa ");
    qs.append("left join fetch pa.prices pap ");
    qs.append("join fetch p.descriptions pd ");
    qs.append("join fetch p.categories categs ");
    qs.append("left join fetch pap.descriptions papd ");
    // images
    qs.append("left join fetch p.images images ");
    // options (do not need attributes for listings)
    qs.append("left join fetch p.attributes pattr ");
    qs.append("left join fetch pattr.productOption po ");
    qs.append("left join fetch po.descriptions pod ");
    qs.append("left join fetch pattr.productOptionValue pov ");
    qs.append("left join fetch pov.descriptions povd ");
    // other lefts
    qs.append("left join fetch p.manufacturer manuf ");
    qs.append("left join fetch manuf.descriptions manufd ");
    qs.append("left join fetch p.type type ");
    qs.append("left join fetch p.taxClass tx ");
    // RENTAL
    // qs.append("left join fetch p.owner owner ");
    // qs.append("where pa.region in (:lid) ");
    qs.append("where categs.id in (:cid) ");
    // qs.append("and pd.language.id=:lang and papd.language.id=:lang and
    // manufd.language.id=:lang ");
    qs.append("and pd.language.id=:lang and papd.language.id=:lang ");
    qs.append("and p.available=true and p.dateAvailable<=:dt ");
    String hql = qs.toString();
    Query q = this.em.createQuery(hql);
    q.setParameter("cid", categoryIds);
    q.setParameter("lang", language.getId());
    q.setParameter("dt", new Date());
    @SuppressWarnings("unchecked") List<Product> products = q.getResultList();
    return products;
}
Also used : Query(javax.persistence.Query) Product(com.salesmanager.core.model.catalog.product.Product) Date(java.util.Date)

Example 19 with Product

use of com.salesmanager.core.model.catalog.product.Product in project shopizer by shopizer-ecommerce.

the class ProductRepositoryImpl method getProductWithOnlyMerchantStoreById.

@Override
public Product getProductWithOnlyMerchantStoreById(Long productId) {
    final String hql = "select distinct p from Product as p " + "join fetch p.merchantStore merch " + "where p.id=:pid";
    final Query q = this.em.createQuery(hql);
    q.setParameter("pid", productId);
    try {
        return (Product) q.getSingleResult();
    } catch (NoResultException ignored) {
        return null;
    }
}
Also used : Query(javax.persistence.Query) Product(com.salesmanager.core.model.catalog.product.Product) NoResultException(javax.persistence.NoResultException)

Example 20 with Product

use of com.salesmanager.core.model.catalog.product.Product in project shopizer by shopizer-ecommerce.

the class ProductRepositoryImpl method getProductForLocale.

@Override
public Product getProductForLocale(long productId, Language language, Locale locale) {
    List regionList = new ArrayList();
    regionList.add("*");
    regionList.add(locale.getCountry());
    StringBuilder qs = new StringBuilder();
    qs.append("select distinct p from Product as p ");
    qs.append("join fetch p.availabilities pa ");
    qs.append("join fetch p.descriptions pd ");
    qs.append("join fetch p.merchantStore pm ");
    qs.append("left join fetch pa.prices pap ");
    qs.append("left join fetch pap.descriptions papd ");
    // images
    qs.append("left join fetch p.images images ");
    // options
    qs.append("left join fetch p.attributes pattr ");
    qs.append("left join fetch pattr.productOption po ");
    qs.append("left join fetch po.descriptions pod ");
    qs.append("left join fetch pattr.productOptionValue pov ");
    qs.append("left join fetch pov.descriptions povd ");
    qs.append("left join fetch p.relationships pr ");
    // other lefts
    qs.append("left join fetch p.manufacturer manuf ");
    qs.append("left join fetch manuf.descriptions manufd ");
    qs.append("left join fetch p.type type ");
    qs.append("left join fetch p.taxClass tx ");
    // RENTAL
    // qs.append("left join fetch p.owner owner ");
    qs.append("where p.id=:pid and pa.region in (:lid) ");
    qs.append("and pd.language.id=:lang and papd.language.id=:lang ");
    qs.append("and p.available=true and p.dateAvailable<=:dt ");
    // this cannot be done on child elements from left join
    // qs.append("and pod.languageId=:lang and povd.languageId=:lang");
    String hql = qs.toString();
    Query q = this.em.createQuery(hql);
    q.setParameter("pid", productId);
    q.setParameter("lid", regionList);
    q.setParameter("dt", new Date());
    q.setParameter("lang", language.getId());
    @SuppressWarnings("unchecked") List<Product> results = q.getResultList();
    if (results.isEmpty())
        return null;
    else if (results.size() == 1)
        return results.get(0);
    throw new NonUniqueResultException();
}
Also used : NonUniqueResultException(javax.persistence.NonUniqueResultException) Query(javax.persistence.Query) ArrayList(java.util.ArrayList) Product(com.salesmanager.core.model.catalog.product.Product) ProductList(com.salesmanager.core.model.catalog.product.ProductList) ArrayList(java.util.ArrayList) List(java.util.List) GenericEntityList(com.salesmanager.core.model.common.GenericEntityList) Date(java.util.Date)

Aggregations

Product (com.salesmanager.core.model.catalog.product.Product)120 ReadableProduct (com.salesmanager.shop.model.catalog.product.ReadableProduct)53 ArrayList (java.util.ArrayList)45 Language (com.salesmanager.core.model.reference.language.Language)42 MerchantStore (com.salesmanager.core.model.merchant.MerchantStore)41 ResourceNotFoundException (com.salesmanager.shop.store.api.exception.ResourceNotFoundException)36 ServiceException (com.salesmanager.core.business.exception.ServiceException)35 ServiceRuntimeException (com.salesmanager.shop.store.api.exception.ServiceRuntimeException)35 ProductAvailability (com.salesmanager.core.model.catalog.product.availability.ProductAvailability)33 ReadableProductPopulator (com.salesmanager.shop.populator.catalog.ReadableProductPopulator)33 PersistableProduct (com.salesmanager.shop.model.catalog.product.PersistableProduct)29 ProductAttribute (com.salesmanager.core.model.catalog.product.attribute.ProductAttribute)28 List (java.util.List)25 Date (java.util.Date)23 LightPersistableProduct (com.salesmanager.shop.model.catalog.product.LightPersistableProduct)22 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)21 Category (com.salesmanager.core.model.catalog.category.Category)20 Collectors (java.util.stream.Collectors)20 FinalPrice (com.salesmanager.core.model.catalog.product.price.FinalPrice)19 ConversionException (com.salesmanager.core.business.exception.ConversionException)17