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 14 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
@@ -0,0 +1,25 @@
-- 2020-01-09T11:59:09.418Z
-- I forgot to set the DICTIONARY_ID_COMMENTS System Configurator
INSERT INTO AD_Message (AD_Client_ID,AD_Message_ID,AD_Org_ID,Created,CreatedBy,EntityType,IsActive,MsgText,MsgType,Updated,UpdatedBy,Value) VALUES (0,544955,0,TO_TIMESTAMP('2020-01-09 13:59:09','YYYY-MM-DD HH24:MI:SS'),100,'de.metas.pricing','Y','Für die ausgewählte Preis-Maßeinheit ist keine Umrechnung verfügbar.','E',TO_TIMESTAMP('2020-01-09 13:59:09','YYYY-MM-DD HH24:MI:SS'),100,'de.metas.pricing.service.product.MissingUOMConversion')
;

-- 2020-01-09T11:59:09.422Z
-- I forgot to set the DICTIONARY_ID_COMMENTS System Configurator
INSERT INTO AD_Message_Trl (AD_Language,AD_Message_ID, MsgText,MsgTip, IsTranslated,AD_Client_ID,AD_Org_ID,Created,Createdby,Updated,UpdatedBy) SELECT l.AD_Language, t.AD_Message_ID, t.MsgText,t.MsgTip, 'N',t.AD_Client_ID,t.AD_Org_ID,t.Created,t.Createdby,t.Updated,t.UpdatedBy FROM AD_Language l, AD_Message t WHERE l.IsActive='Y'AND (l.IsSystemLanguage='Y' AND l.IsBaseLanguage='N') AND t.AD_Message_ID=544955 AND NOT EXISTS (SELECT 1 FROM AD_Message_Trl tt WHERE tt.AD_Language=l.AD_Language AND tt.AD_Message_ID=t.AD_Message_ID)
;

-- 2020-01-09T11:59:19.637Z
-- I forgot to set the DICTIONARY_ID_COMMENTS System Configurator
UPDATE AD_Message_Trl SET MsgText='No conversion is available for the selected price unit.',Updated=TO_TIMESTAMP('2020-01-09 13:59:19','YYYY-MM-DD HH24:MI:SS'),UpdatedBy=100 WHERE AD_Language='en_US' AND AD_Message_ID=544955
;

-- 2020-01-09T12:01:14.534Z
-- I forgot to set the DICTIONARY_ID_COMMENTS System Configurator
UPDATE AD_Message_Trl SET IsTranslated='Y', MsgText='Für die ausgewählte Preis-Masseinheit ist keine Umrechnung verfügbar.',Updated=TO_TIMESTAMP('2020-01-09 14:01:14','YYYY-MM-DD HH24:MI:SS'),UpdatedBy=100 WHERE AD_Language='de_CH' AND AD_Message_ID=544955
;

-- 2020-01-09T12:01:35.173Z
-- I forgot to set the DICTIONARY_ID_COMMENTS System Configurator
UPDATE AD_Message_Trl SET IsTranslated='Y',Updated=TO_TIMESTAMP('2020-01-09 14:01:35','YYYY-MM-DD HH24:MI:SS'),UpdatedBy=100 WHERE AD_Language='en_US' AND AD_Message_ID=544955
;

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_AFTER_NEW, ModelValidator.TYPE_AFTER_CHANGE }, ifColumnsChanged = { I_M_ProductPrice.COLUMNNAME_C_UOM_ID, I_M_ProductPrice.COLUMNNAME_IsInvalidPrice, I_M_ProductPrice.COLUMNNAME_IsActive })
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,10 @@

import javax.annotation.Nullable;

import de.metas.i18n.ITranslatableString;
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 @@ -62,6 +66,8 @@

public class ProductPrices
{
private static final String MSG_NO_UOM_CONVERSION_AVAILABLE = "de.metas.pricing.service.product.MissingUOMConversion";

private static final CopyOnWriteArrayList<IProductPriceQueryMatcher> MATCHERS_MainProductPrice = new CopyOnWriteArrayList<>();

private static final Logger logger = LogManager.getLogger(ProductPrices.class);
Expand All @@ -76,7 +82,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 +108,7 @@ public static void assertMainProductPriceIsNotDuplicate(final I_M_ProductPrice p
return;
}

if(productPrice.isInvalidPrice())
if (productPrice.isInvalidPrice())
{
return;
}
Expand All @@ -124,6 +130,39 @@ 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())
{
final IMsgBL msgBL = Services.get(IMsgBL.class);
final ITranslatableString message = msgBL.getTranslatableMsgText(MSG_NO_UOM_CONVERSION_AVAILABLE);
throw new AdempiereException(message).markAsUserValidationError();
}
}

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 +294,6 @@ public static <T extends I_M_ProductPrice> T iterateAllPriceListVersionsAndFindP
return productPrice;
}


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

Expand Down