payToPartyId = productStore.getString("payToPartyId");
}
}
// store expr
EntityCondition storeCond = null;
if (productStore != null) {
storeCond = EntityCondition.makeCondition(
EntityCondition.makeCondition("productStoreId", EntityOperator.EQUALS, productStore.get("productStoreId")),
EntityOperator.OR,
EntityCondition.makeCondition("productStoreId", EntityOperator.EQUALS, null));
} else {
storeCond = EntityCondition.makeCondition("productStoreId", EntityOperator.EQUALS, null);
}
// build the TaxAuthority expressions (taxAuthGeoId, taxAuthPartyId)
List<EntityCondition> taxAuthCondOrList = FastList.newInstance();
// start with the _NA_ TaxAuthority...
taxAuthCondOrList.add(EntityCondition.makeCondition(
EntityCondition.makeCondition("taxAuthPartyId", EntityOperator.EQUALS, "_NA_"),
EntityOperator.AND,
EntityCondition.makeCondition("taxAuthGeoId", EntityOperator.EQUALS, "_NA_")));
Iterator<GenericValue> taxAuthorityIter = taxAuthoritySet.iterator();
while (taxAuthorityIter.hasNext()) {
GenericValue taxAuthority = taxAuthorityIter.next();
EntityCondition taxAuthCond = EntityCondition.makeCondition(
EntityCondition.makeCondition("taxAuthPartyId", EntityOperator.EQUALS, taxAuthority.getString("taxAuthPartyId")),
EntityOperator.AND,
EntityCondition.makeCondition("taxAuthGeoId", EntityOperator.EQUALS, taxAuthority.getString("taxAuthGeoId")));
taxAuthCondOrList.add(taxAuthCond);
}
EntityCondition taxAuthoritiesCond = EntityCondition.makeCondition(taxAuthCondOrList, EntityOperator.OR);
try {
EntityCondition productCategoryCond = null;
if (product != null) {
// find the tax categories associated with the product and filter by those, with an IN clause or some such
// if this product is variant, find the virtual product id and consider also the categories of the virtual
// question: get all categories, or just a special type? for now let's do all categories...
String virtualProductId = null;
if ("Y".equals(product.getString("isVariant"))) {
virtualProductId = ProductWorker.getVariantVirtualId(product);
}
Set<String> productCategoryIdSet = FastSet.newInstance();
EntityCondition productIdCond = null;
if (virtualProductId != null) {
productIdCond = EntityCondition.makeCondition(
EntityCondition.makeCondition("productId", EntityOperator.EQUALS, product.getString("productId")),
EntityOperator.OR,
EntityCondition.makeCondition("productId", EntityOperator.EQUALS, virtualProductId));
} else {
productIdCond = EntityCondition.makeCondition("productId", EntityOperator.EQUALS, product.getString("productId"));
}
List<GenericValue> pcmList = delegator.findList("ProductCategoryMember", productIdCond, UtilMisc.toSet("productCategoryId", "fromDate", "thruDate"), null, null, true);
pcmList = EntityUtil.filterByDate(pcmList, true);
Iterator<GenericValue> pcmIter = pcmList.iterator();
while (pcmIter.hasNext()) {
GenericValue pcm = pcmIter.next();
productCategoryIdSet.add(pcm.getString("productCategoryId"));
}
if (productCategoryIdSet.size() == 0) {
productCategoryCond = EntityCondition.makeCondition("productCategoryId", EntityOperator.EQUALS, null);
} else {
productCategoryCond = EntityCondition.makeCondition(
EntityCondition.makeCondition("productCategoryId", EntityOperator.EQUALS, null),
EntityOperator.OR,
EntityCondition.makeCondition("productCategoryId", EntityOperator.IN, productCategoryIdSet));
}
} else {
productCategoryCond = EntityCondition.makeCondition("productCategoryId", EntityOperator.EQUALS, null);
}
// FIXME handles shipping and promo tax. Simple solution, see https://issues.apache.org/jira/browse/OFBIZ-4160 for a better one
if (product == null && shippingAmount != null) {
EntityCondition taxShippingCond = EntityCondition.makeCondition(
EntityCondition.makeCondition("taxShipping", EntityOperator.EQUALS, null),
EntityOperator.OR,
EntityCondition.makeCondition("taxShipping", EntityOperator.EQUALS, "Y"));
if (productCategoryCond != null) {
productCategoryCond = EntityCondition.makeCondition(productCategoryCond,
EntityOperator.OR,
taxShippingCond);
}
}
if (product == null && orderPromotionsAmount != null) {
EntityCondition taxOrderPromotionsCond = EntityCondition.makeCondition(
EntityCondition.makeCondition("taxPromotions", EntityOperator.EQUALS, null),
EntityOperator.OR,
EntityCondition.makeCondition("taxPromotions", EntityOperator.EQUALS, "Y"));
if (productCategoryCond != null) {
productCategoryCond = EntityCondition.makeCondition(productCategoryCond,
EntityOperator.OR,
taxOrderPromotionsCond);
}
}
// build the main condition clause
List<EntityCondition> mainExprs = UtilMisc.toList(storeCond, taxAuthoritiesCond, productCategoryCond);
mainExprs.add(EntityCondition.makeCondition(EntityCondition.makeCondition("minItemPrice", EntityOperator.EQUALS, null), EntityOperator.OR, EntityCondition.makeCondition("minItemPrice", EntityOperator.LESS_THAN_EQUAL_TO, itemPrice)));
mainExprs.add(EntityCondition.makeCondition(EntityCondition.makeCondition("minPurchase", EntityOperator.EQUALS, null), EntityOperator.OR, EntityCondition.makeCondition("minPurchase", EntityOperator.LESS_THAN_EQUAL_TO, itemAmount)));
EntityCondition mainCondition = EntityCondition.makeCondition(mainExprs, EntityOperator.AND);
// create the orderby clause
List<String> orderList = UtilMisc.<String>toList("minItemPrice", "minPurchase", "fromDate");
// finally ready... do the rate query
List<GenericValue> lookupList = delegator.findList("TaxAuthorityRateProduct", mainCondition, null, orderList, null, false);
List<GenericValue> filteredList = EntityUtil.filterByDate(lookupList, true);
if (filteredList.size() == 0) {
Debug.logWarning("In TaxAuthority Product Rate no records were found for condition:" + mainCondition.toString(), module);
return adjustments;
}
// find the right entry(s) based on purchase amount
Iterator<GenericValue> flIt = filteredList.iterator();