Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#409 Aded UOM conversion check when adding a new price to a product. #6008

Merged
merged 16 commits into from
Jan 10, 2020
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
*/

/**
*
* @author metas-dev <dev@metasfresh.com>
* @task Prevent users from creating duplicate main prices https://github.com/metasfresh/metasfresh/issues/2510
*/
Expand All @@ -57,4 +56,10 @@ public void assertMainProductPriceIsNotDuplicate(@NonNull final I_M_ProductPrice
{
ProductPrices.assertMainProductPriceIsNotDuplicate(productPrice);
}

@ModelChange(timings = { ModelValidator.TYPE_BEFORE_NEW, ModelValidator.TYPE_BEFORE_CHANGE }, ifColumnsChanged = { I_M_ProductPrice.COLUMNNAME_C_UOM_ID })
public void assertUomConversionExists(@NonNull final I_M_ProductPrice productPrice)
{
ProductPrices.assertUomConversionExists(productPrice);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@

import javax.annotation.Nullable;

import de.metas.uom.IUOMConversionDAO;
import de.metas.uom.UOMConversionsMap;
import de.metas.uom.UomId;
import org.adempiere.exceptions.AdempiereException;
import org.adempiere.model.InterfaceWrapperHelper;
import org.compiere.model.I_M_PriceList;
Expand Down Expand Up @@ -76,7 +79,7 @@ public static final ProductPriceQuery newQuery(@NonNull final I_M_PriceList_Vers
/**
* Convenient method to check if the main product price exists.
*
* @param plv price list version or null
* @param plv price list version or null
* @param productId product (negative values are tolerated)
* @return true if exists
*/
Expand All @@ -102,7 +105,7 @@ public static void assertMainProductPriceIsNotDuplicate(final I_M_ProductPrice p
return;
}

if(productPrice.isInvalidPrice())
if (productPrice.isInvalidPrice())
{
return;
}
Expand All @@ -124,6 +127,38 @@ public static void assertMainProductPriceIsNotDuplicate(final I_M_ProductPrice p
getFirstOrThrowExceptionIfMoreThanOne(allMainPrices);
}

public static void assertUomConversionExists(final I_M_ProductPrice productPrice)
{
if (productPrice == null || !productPrice.isActive())
{
return;
}

if (productPrice.isInvalidPrice())
{
return;
}

final IProductDAO productDAO = Services.get(IProductDAO.class);
metas-ts marked this conversation as resolved.
Show resolved Hide resolved
final org.compiere.model.I_M_Product product = productDAO.getById(productPrice.getM_Product_ID());

if (UomId.ofRepoId(product.getC_UOM_ID()).equals(UomId.ofRepoId(productPrice.getC_UOM_ID())))
{
return;
}

final IUOMConversionDAO uomConversionRepo = Services.get(IUOMConversionDAO.class);
final ProductId productId = ProductId.ofRepoId(productPrice.getM_Product_ID());

UOMConversionsMap conversionsMap = uomConversionRepo.getProductConversions(productId);

if (conversionsMap.getRateIfExists(UomId.ofRepoId(product.getC_UOM_ID()), UomId.ofRepoId(productPrice.getC_UOM_ID())).isPresent())
{
return;
}
throw new AdempiereException("UOM Conversion to the selected UOM doesn't exist").markAsUserValidationError();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Create an AD_Message record with translations (we'll need en_US, de_DE and de_CH) and use IMsg (Services.get(IMsg.class)) to get a translatable string instance.
Passing hardcoded strings generally doesn't make sense for user-errors

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fully agree

}

public static final I_M_ProductPrice retrieveMainProductPriceOrNull(final I_M_PriceList_Version plv, final ProductId productId)
{
final List<I_M_ProductPrice> allMainPrices = retrieveAllMainPrices(plv, productId);
Expand Down Expand Up @@ -255,7 +290,6 @@ public static <T extends I_M_ProductPrice> T iterateAllPriceListVersionsAndFindP
return productPrice;
}


currentPriceListVersion = priceListsRepo.getBasePriceListVersionForPricingCalculationOrNull(currentPriceListVersion, priceDate);
}

Expand Down