Search in sources :

Example 1 with ShippingProduct

use of com.salesmanager.core.model.shipping.ShippingProduct 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 2 with ShippingProduct

use of com.salesmanager.core.model.shipping.ShippingProduct in project shopizer by shopizer-ecommerce.

the class ShippingQuoteByWeightTest method testGetCustomShippingQuotesByWeight.

@Ignore
public // @Test
void testGetCustomShippingQuotesByWeight() throws ServiceException {
    Language en = languageService.getByCode("en");
    Country country = countryService.getByCode("CA");
    Zone zone = zoneService.getByCode("QC");
    MerchantStore store = merchantService.getByCode(MerchantStore.DEFAULT_STORE);
    ProductType generalType = productTypeService.getProductType(ProductType.GENERAL_TYPE);
    // set valid store postal code
    store.setStorepostalcode("J4B-9J9");
    Product product = new Product();
    product.setProductHeight(new BigDecimal(4));
    product.setProductLength(new BigDecimal(3));
    product.setProductWidth(new BigDecimal(5));
    product.setProductWeight(new BigDecimal(8));
    product.setSku("TESTSKU");
    product.setType(generalType);
    product.setMerchantStore(store);
    // Product description
    ProductDescription description = new ProductDescription();
    description.setName("Product 1");
    description.setLanguage(en);
    description.setProduct(product);
    product.getDescriptions().add(description);
    productService.create(product);
    // productService.saveOrUpdate(product);
    // Availability
    ProductAvailability availability = new ProductAvailability();
    availability.setProductDateAvailable(new Date());
    availability.setProductQuantity(100);
    availability.setRegion("*");
    // associate with product
    availability.setProduct(product);
    product.getAvailabilities().add(availability);
    productAvailabilityService.create(availability);
    ProductPrice dprice = new ProductPrice();
    dprice.setDefaultPrice(true);
    dprice.setProductPriceAmount(new BigDecimal(29.99));
    dprice.setProductAvailability(availability);
    ProductPriceDescription dpd = new ProductPriceDescription();
    dpd.setName("Base price");
    dpd.setProductPrice(dprice);
    dpd.setLanguage(en);
    dprice.getDescriptions().add(dpd);
    availability.getPrices().add(dprice);
    productPriceService.create(dprice);
    // get product
    product = productService.getByCode("TESTSKU", en);
    // check the product
    Set<ProductAvailability> avails = product.getAvailabilities();
    for (ProductAvailability as : avails) {
        Set<ProductPrice> availabilityPrices = as.getPrices();
        for (ProductPrice ps : availabilityPrices) {
            System.out.println(ps.getProductPriceAmount().toString());
        }
    }
    // check availability
    Set<ProductPrice> availabilityPrices = availability.getPrices();
    for (ProductPrice ps : availabilityPrices) {
        System.out.println(ps.getProductPriceAmount().toString());
    }
    // configure shipping
    ShippingConfiguration shippingConfiguration = new ShippingConfiguration();
    // based on shipping or billing address
    shippingConfiguration.setShippingBasisType(ShippingBasisType.SHIPPING);
    shippingConfiguration.setShippingType(ShippingType.INTERNATIONAL);
    // individual item pricing or box packaging (see unit test above)
    shippingConfiguration.setShippingPackageType(ShippingPackageType.ITEM);
    // only if package type is package
    shippingConfiguration.setBoxHeight(5);
    shippingConfiguration.setBoxLength(5);
    shippingConfiguration.setBoxWidth(5);
    shippingConfiguration.setBoxWeight(1);
    shippingConfiguration.setMaxWeight(10);
    List<String> supportedCountries = new ArrayList<String>();
    supportedCountries.add("CA");
    supportedCountries.add("US");
    supportedCountries.add("UK");
    supportedCountries.add("FR");
    shippingService.setSupportedCountries(store, supportedCountries);
    CustomShippingQuotesConfiguration customConfiguration = new CustomShippingQuotesConfiguration();
    customConfiguration.setModuleCode("weightBased");
    customConfiguration.setActive(true);
    CustomShippingQuotesRegion northRegion = new CustomShippingQuotesRegion();
    northRegion.setCustomRegionName("NORTH");
    List<String> countries = new ArrayList<String>();
    countries.add("CA");
    countries.add("US");
    northRegion.setCountries(countries);
    CustomShippingQuoteWeightItem caQuote4 = new CustomShippingQuoteWeightItem();
    caQuote4.setMaximumWeight(4);
    caQuote4.setPrice(new BigDecimal(20));
    CustomShippingQuoteWeightItem caQuote10 = new CustomShippingQuoteWeightItem();
    caQuote10.setMaximumWeight(10);
    caQuote10.setPrice(new BigDecimal(50));
    CustomShippingQuoteWeightItem caQuote100 = new CustomShippingQuoteWeightItem();
    caQuote100.setMaximumWeight(100);
    caQuote100.setPrice(new BigDecimal(120));
    List<CustomShippingQuoteWeightItem> quotes = new ArrayList<CustomShippingQuoteWeightItem>();
    quotes.add(caQuote4);
    quotes.add(caQuote10);
    quotes.add(caQuote100);
    northRegion.setQuoteItems(quotes);
    customConfiguration.getRegions().add(northRegion);
    // create an integration configuration - USPS
    IntegrationConfiguration configuration = new IntegrationConfiguration();
    configuration.setActive(true);
    configuration.setEnvironment(Environment.TEST.name());
    configuration.setModuleCode("weightBased");
    // configure module
    shippingService.saveShippingConfiguration(shippingConfiguration, store);
    // create the basic configuration
    shippingService.saveShippingQuoteModuleConfiguration(configuration, store);
    // and the custom configuration
    shippingService.saveCustomShippingConfiguration("weightBased", customConfiguration, store);
    // now create ShippingProduct
    ShippingProduct shippingProduct1 = new ShippingProduct(product);
    FinalPrice price = pricingService.calculateProductPrice(product);
    shippingProduct1.setFinalPrice(price);
    List<ShippingProduct> shippingProducts = new ArrayList<ShippingProduct>();
    shippingProducts.add(shippingProduct1);
    Customer customer = new Customer();
    customer.setMerchantStore(store);
    customer.setEmailAddress("test@test.com");
    customer.setGender(CustomerGender.M);
    customer.setDefaultLanguage(en);
    customer.setAnonymous(true);
    customer.setCompany("ifactory");
    customer.setDateOfBirth(new Date());
    customer.setNick("My nick");
    customer.setPassword("123456");
    Delivery delivery = new Delivery();
    delivery.setAddress("Shipping address");
    delivery.setCity("Boucherville");
    delivery.setCountry(country);
    delivery.setZone(zone);
    delivery.setPostalCode("J5C-6J4");
    // overwrite delivery to US
    /*	    delivery.setPostalCode("90002");
	    delivery.setCountry(us);
	    Zone california = zoneService.getByCode("CA");
	    delivery.setZone(california);*/
    Billing billing = new Billing();
    billing.setAddress("Billing address");
    billing.setCountry(country);
    billing.setZone(zone);
    billing.setPostalCode("J4B-8J9");
    billing.setFirstName("Carl");
    billing.setLastName("Samson");
    customer.setBilling(billing);
    customer.setDelivery(delivery);
    customerService.create(customer);
    // for correlation
    Long dummyCartId = 0L;
    ShippingQuote shippingQuote = shippingService.getShippingQuote(dummyCartId, store, delivery, shippingProducts, en);
    Assert.notNull(shippingQuote);
}
Also used : Customer(com.salesmanager.core.model.customer.Customer) ArrayList(java.util.ArrayList) ShippingProduct(com.salesmanager.core.model.shipping.ShippingProduct) Product(com.salesmanager.core.model.catalog.product.Product) ProductPrice(com.salesmanager.core.model.catalog.product.price.ProductPrice) ShippingConfiguration(com.salesmanager.core.model.shipping.ShippingConfiguration) Language(com.salesmanager.core.model.reference.language.Language) ProductAvailability(com.salesmanager.core.model.catalog.product.availability.ProductAvailability) CustomShippingQuoteWeightItem(com.salesmanager.core.modules.integration.shipping.model.CustomShippingQuoteWeightItem) MerchantStore(com.salesmanager.core.model.merchant.MerchantStore) ProductPriceDescription(com.salesmanager.core.model.catalog.product.price.ProductPriceDescription) FinalPrice(com.salesmanager.core.model.catalog.product.price.FinalPrice) CustomShippingQuotesConfiguration(com.salesmanager.core.modules.integration.shipping.model.CustomShippingQuotesConfiguration) Zone(com.salesmanager.core.model.reference.zone.Zone) CustomShippingQuotesRegion(com.salesmanager.core.modules.integration.shipping.model.CustomShippingQuotesRegion) IntegrationConfiguration(com.salesmanager.core.model.system.IntegrationConfiguration) ProductType(com.salesmanager.core.model.catalog.product.type.ProductType) ShippingProduct(com.salesmanager.core.model.shipping.ShippingProduct) BigDecimal(java.math.BigDecimal) Date(java.util.Date) ShippingQuote(com.salesmanager.core.model.shipping.ShippingQuote) Billing(com.salesmanager.core.model.common.Billing) Country(com.salesmanager.core.model.reference.country.Country) ProductDescription(com.salesmanager.core.model.catalog.product.description.ProductDescription) Delivery(com.salesmanager.core.model.common.Delivery) Ignore(org.junit.Ignore)

Example 3 with ShippingProduct

use of com.salesmanager.core.model.shipping.ShippingProduct in project shopizer by shopizer-ecommerce.

the class ShippingServiceImpl method calculateOrderTotal.

private BigDecimal calculateOrderTotal(List<ShippingProduct> products, MerchantStore store) throws Exception {
    BigDecimal total = new BigDecimal(0);
    for (ShippingProduct shippingProduct : products) {
        BigDecimal currentPrice = shippingProduct.getFinalPrice().getFinalPrice();
        currentPrice = currentPrice.multiply(new BigDecimal(shippingProduct.getQuantity()));
        total = total.add(currentPrice);
    }
    return total;
}
Also used : ShippingProduct(com.salesmanager.core.model.shipping.ShippingProduct) BigDecimal(java.math.BigDecimal)

Example 4 with ShippingProduct

use of com.salesmanager.core.model.shipping.ShippingProduct in project shopizer by shopizer-ecommerce.

the class OrderFacadeImpl method getShippingQuote.

@Override
public ShippingQuote getShippingQuote(Customer customer, ShoppingCart cart, MerchantStore store, Language language) throws Exception {
    Validate.notNull(customer, "Customer cannot be null");
    Validate.notNull(cart, "cart cannot be null");
    // create shipping products
    List<ShippingProduct> shippingProducts = shoppingCartService.createShippingProduct(cart);
    if (CollectionUtils.isEmpty(shippingProducts)) {
        // products are virtual
        return null;
    }
    Delivery delivery = new Delivery();
    Billing billing = new Billing();
    // default value
    billing.setCountry(store.getCountry());
    // adjust shipping and billing
    if (customer.getDelivery() == null || StringUtils.isBlank(customer.getDelivery().getPostalCode())) {
        if (customer.getBilling() != null) {
            billing = customer.getBilling();
        }
        delivery.setAddress(billing.getAddress());
        delivery.setCity(billing.getCity());
        delivery.setCompany(billing.getCompany());
        delivery.setPostalCode(billing.getPostalCode());
        delivery.setState(billing.getState());
        delivery.setCountry(billing.getCountry());
        delivery.setZone(billing.getZone());
    } else {
        delivery = customer.getDelivery();
    }
    ShippingQuote quote = shippingService.getShippingQuote(cart.getId(), store, delivery, shippingProducts, language);
    return quote;
}
Also used : ShippingQuote(com.salesmanager.core.model.shipping.ShippingQuote) Billing(com.salesmanager.core.model.common.Billing) ShippingProduct(com.salesmanager.core.model.shipping.ShippingProduct) Delivery(com.salesmanager.core.model.common.Delivery)

Example 5 with ShippingProduct

use of com.salesmanager.core.model.shipping.ShippingProduct in project shopizer by shopizer-ecommerce.

the class OrderFacadeImpl method getShippingQuote.

@Override
public ShippingQuote getShippingQuote(PersistableCustomer persistableCustomer, ShoppingCart cart, ShopOrder order, MerchantStore store, Language language) throws Exception {
    // create shipping products
    List<ShippingProduct> shippingProducts = shoppingCartService.createShippingProduct(cart);
    if (CollectionUtils.isEmpty(shippingProducts)) {
        // products are virtual
        return null;
    }
    Customer customer = customerFacade.getCustomerModel(persistableCustomer, store, language);
    Delivery delivery = new Delivery();
    // adjust shipping and billing
    if (order.isShipToBillingAdress() && !order.isShipToDeliveryAddress()) {
        Billing billing = customer.getBilling();
        String postalCode = billing.getPostalCode();
        postalCode = validatePostalCode(postalCode);
        delivery.setAddress(billing.getAddress());
        delivery.setCompany(billing.getCompany());
        delivery.setCity(billing.getCity());
        delivery.setPostalCode(billing.getPostalCode());
        delivery.setState(billing.getState());
        delivery.setCountry(billing.getCountry());
        delivery.setZone(billing.getZone());
    } else {
        delivery = customer.getDelivery();
    }
    ShippingQuote quote = shippingService.getShippingQuote(cart.getId(), store, delivery, shippingProducts, language);
    return quote;
}
Also used : ShippingQuote(com.salesmanager.core.model.shipping.ShippingQuote) ReadableCustomer(com.salesmanager.shop.model.customer.ReadableCustomer) Customer(com.salesmanager.core.model.customer.Customer) PersistableCustomer(com.salesmanager.shop.model.customer.PersistableCustomer) Billing(com.salesmanager.core.model.common.Billing) ShippingProduct(com.salesmanager.core.model.shipping.ShippingProduct) Delivery(com.salesmanager.core.model.common.Delivery)

Aggregations

ShippingProduct (com.salesmanager.core.model.shipping.ShippingProduct)8 Product (com.salesmanager.core.model.catalog.product.Product)4 Billing (com.salesmanager.core.model.common.Billing)4 Delivery (com.salesmanager.core.model.common.Delivery)4 ShippingQuote (com.salesmanager.core.model.shipping.ShippingQuote)4 BigDecimal (java.math.BigDecimal)4 ArrayList (java.util.ArrayList)3 ProductAttribute (com.salesmanager.core.model.catalog.product.attribute.ProductAttribute)2 Customer (com.salesmanager.core.model.customer.Customer)2 PackageDetails (com.salesmanager.core.model.shipping.PackageDetails)2 ShippingConfiguration (com.salesmanager.core.model.shipping.ShippingConfiguration)2 ServiceException (com.salesmanager.core.business.exception.ServiceException)1 ProductAvailability (com.salesmanager.core.model.catalog.product.availability.ProductAvailability)1 ProductDescription (com.salesmanager.core.model.catalog.product.description.ProductDescription)1 FinalPrice (com.salesmanager.core.model.catalog.product.price.FinalPrice)1 ProductPrice (com.salesmanager.core.model.catalog.product.price.ProductPrice)1 ProductPriceDescription (com.salesmanager.core.model.catalog.product.price.ProductPriceDescription)1 ProductType (com.salesmanager.core.model.catalog.product.type.ProductType)1 MerchantStore (com.salesmanager.core.model.merchant.MerchantStore)1 Country (com.salesmanager.core.model.reference.country.Country)1