Skip to content

Commit

Permalink
Set Sales Order Line's ProductDescription as Product BOM's description
Browse files Browse the repository at this point in the history
  • Loading branch information
teosarca committed Aug 30, 2018
1 parent f251f2f commit a8a62f3
Show file tree
Hide file tree
Showing 8 changed files with 193 additions and 21 deletions.
@@ -0,0 +1,9 @@
-- 2018-08-30T16:54:27.918
-- I forgot to set the DICTIONARY_ID_COMMENTS System Configurator
INSERT INTO AD_SysConfig (Value,EntityType,AD_Client_ID,CreatedBy,UpdatedBy,IsActive,ConfigurationLevel,AD_SysConfig_ID,Description,AD_Org_ID,Name,Created,Updated) VALUES ('N','D',0,100,100,'Y','S',541230,'See https://github.com/metasfresh/metasfresh/issues/4535',0,'de.metas.order.sales.line.SetBOMDescription',TO_TIMESTAMP('2018-08-30 16:54:27','YYYY-MM-DD HH24:MI:SS'),TO_TIMESTAMP('2018-08-30 16:54:27','YYYY-MM-DD HH24:MI:SS'))
;

-- 2018-08-30T16:54:32.319
-- I forgot to set the DICTIONARY_ID_COMMENTS System Configurator
-- UPDATE AD_SysConfig SET Value='Y',Updated=TO_TIMESTAMP('2018-08-30 16:54:32','YYYY-MM-DD HH24:MI:SS'),UpdatedBy=100 WHERE AD_SysConfig_ID=541230;

Expand Up @@ -223,4 +223,6 @@ default BigDecimal subtractDiscount(BigDecimal baseAmount, BigDecimal discount,
int getC_PaymentTerm_ID(org.compiere.model.I_C_OrderLine orderLine);

Map<OrderAndLineId, Quantity> getQtyToDeliver(Collection<OrderAndLineId> orderAndLineIds);

void updateProductDescriptionFromProductBOMIfConfigured(org.compiere.model.I_C_OrderLine orderLine);
}
Expand Up @@ -304,4 +304,11 @@ public void ensureOrderLineHasShipper(final I_C_OrderLine orderLine)
orderLineBL.setShipper(orderLine);
}
}

@ModelChange(timings = { ModelValidator.TYPE_BEFORE_NEW, ModelValidator.TYPE_BEFORE_CHANGE }, //
ifColumnsChanged = { I_C_OrderLine.COLUMNNAME_M_Product_ID })
public void updateProductDescriptionFromProductBOMIfConfigured(final I_C_OrderLine orderLine)
{
Services.get(IOrderLineBL.class).updateProductDescriptionFromProductBOMIfConfigured(orderLine);
}
}
Expand Up @@ -34,7 +34,6 @@

public interface IProductBOMBL extends ISingletonService
{

boolean isValidFromTo(I_PP_Product_BOM productBOM, Date date);

boolean isValidFromTo(I_PP_Product_BOMLine bomLine, Date date);
Expand Down Expand Up @@ -83,4 +82,6 @@ public interface IProductBOMBL extends ISingletonService
* @return If is percentage then QtyBatch / 100 will be returned, else QtyBOM.
*/
BigDecimal getQtyMultiplier(I_PP_Product_BOMLine productBomLine, ProductId endProductId);

String getBOMDescriptionForProductId(ProductId productId);
}
Expand Up @@ -13,16 +13,15 @@
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program. If not, see
* License along with this program. If not, see
* <http://www.gnu.org/licenses/gpl-2.0.html>.
* #L%
*/


import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
Expand Down Expand Up @@ -119,7 +118,6 @@ public BigDecimal calculateQtyWithScrap(final BigDecimal qty, final BigDecimal q
return qtyPlusScap;
}


@Override
public boolean isValidVariantGroup(final I_PP_Product_BOMLine bomLine)
{
Expand All @@ -143,7 +141,7 @@ public boolean isValidVariantGroup(final I_PP_Product_BOMLine bomLine)

return isComponentOrPacking;
}

@Override
public BigDecimal getQtyMultiplier(
@NonNull final I_PP_Product_BOMLine productBomLine,
Expand All @@ -168,4 +166,11 @@ public BigDecimal getQtyMultiplier(
final Percent qtyBatchPercent = Percent.of(productBomLine.getQtyBatch());
return qtyBatchPercent.multiply(bomToLineUOMMultiplier, 8);
}

@Override
public String getBOMDescriptionForProductId(@NonNull final ProductId productId)
{
return ProductBOMDescriptionBuilder.newInstance()
.build(productId);
}
}
@@ -0,0 +1,113 @@
package org.eevolution.api.impl;

import java.util.stream.Collectors;

import org.adempiere.uom.api.IUOMDAO;
import org.adempiere.util.NumberUtils;
import org.adempiere.util.Services;
import org.compiere.model.I_C_UOM;
import org.compiere.model.I_M_Product;
import org.eevolution.api.IProductBOMDAO;
import org.eevolution.model.I_PP_Product_BOM;
import org.eevolution.model.I_PP_Product_BOMLine;

import com.google.common.base.Predicates;

import de.metas.product.IProductDAO;
import de.metas.product.ProductId;
import lombok.NonNull;

/*
* #%L
* de.metas.business
* %%
* Copyright (C) 2018 metas GmbH
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 2 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/gpl-2.0.html>.
* #L%
*/

final class ProductBOMDescriptionBuilder
{
public static ProductBOMDescriptionBuilder newInstance()
{
return new ProductBOMDescriptionBuilder();
}

private final IProductDAO productsRepo = Services.get(IProductDAO.class);
private final IProductBOMDAO bomsRepo = Services.get(IProductBOMDAO.class);
private final IUOMDAO uomsRepo = Services.get(IUOMDAO.class);

private ProductBOMDescriptionBuilder()
{
}

public String build(@NonNull final ProductId productId)
{
final I_M_Product bomProduct = productsRepo.getById(productId);
final I_PP_Product_BOM bom = bomsRepo.retrieveDefaultBOM(bomProduct);
if (bom == null || !bom.isActive())
{
return null;
}

return bomsRepo.retrieveLines(bom)
.stream()
.map(this::toBOMLineString)
.filter(Predicates.notNull())
.collect(Collectors.joining("\r\n"));
}

private String toBOMLineString(final I_PP_Product_BOMLine bomLine)
{
if (!bomLine.isActive())
{
return null;
}

final I_M_Product product = productsRepo.getById(bomLine.getM_Product_ID());
final String qtyStr = toBOMLineQtyAndUOMString(bomLine);

return new StringBuilder()
.append(product.getValue())
.append(" ")
.append(product.getName())
.append(" ")
.append(qtyStr)
.toString();
}

private String toBOMLineQtyAndUOMString(final I_PP_Product_BOMLine bomLine)
{
if (bomLine.isQtyPercentage())
{
return NumberUtils.stripTrailingDecimalZeros(bomLine.getQtyBatch()) + "%";
}
else
{
final StringBuilder qtyStr = new StringBuilder();
qtyStr.append(NumberUtils.stripTrailingDecimalZeros(bomLine.getQtyBOM()));

final int uomId = bomLine.getC_UOM_ID();
if (uomId > 0)
{
final I_C_UOM uom = uomsRepo.getById(uomId);
qtyStr.append(" ").append(uom.getUOMSymbol());
}

return qtyStr.toString();
}
}
}
Expand Up @@ -16,20 +16,17 @@
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program. If not, see
* License along with this program. If not, see
* <http://www.gnu.org/licenses/gpl-2.0.html>.
* #L%
*/


import org.adempiere.ad.callout.annotations.Callout;
import org.adempiere.ad.callout.annotations.CalloutMethod;
import org.adempiere.ad.callout.api.ICalloutField;
import org.adempiere.model.InterfaceWrapperHelper;
import org.adempiere.util.Services;

import de.metas.interfaces.I_C_OrderLine;
Expand All @@ -42,16 +39,17 @@
@Callout(I_C_OrderLine.class)
public class C_OrderLine
{
@CalloutMethod(columnNames = { I_C_OrderLine.COLUMNNAME_M_Product_ID })
public void onProductChanged(final I_C_OrderLine orderLine)
{
resetManualFlags(orderLine);
Services.get(IOrderLineBL.class).updateProductDescriptionFromProductBOMIfConfigured(orderLine);
}


@CalloutMethod(columnNames = {I_C_OrderLine.COLUMNNAME_M_Product_ID })
public void resetManualFlags(final I_C_OrderLine orderLine, final ICalloutField field)
private void resetManualFlags(final I_C_OrderLine orderLine)
{
final I_C_OrderLine ol = InterfaceWrapperHelper.create(orderLine, I_C_OrderLine.class);
ol.setIsManualDiscount(false);
ol.setIsManualPrice(false);

// update prices : see taks 06727
Services.get(IOrderLineBL.class).updatePrices(orderLine);
orderLine.setIsManualDiscount(false);
orderLine.setIsManualPrice(false);
Services.get(IOrderLineBL.class).updatePrices(orderLine); // see task 06727
}
}
Expand Up @@ -34,6 +34,7 @@
import org.adempiere.ad.trx.api.ITrx;
import org.adempiere.exceptions.AdempiereException;
import org.adempiere.model.InterfaceWrapperHelper;
import org.adempiere.service.ISysConfigBL;
import org.adempiere.uom.api.IUOMConversionBL;
import org.adempiere.uom.api.IUOMConversionContext;
import org.adempiere.util.Check;
Expand All @@ -47,6 +48,7 @@
import org.compiere.model.I_M_Shipper;
import org.compiere.model.MTax;
import org.compiere.util.Env;
import org.eevolution.api.IProductBOMBL;
import org.slf4j.Logger;

import de.metas.adempiere.model.I_M_Product;
Expand All @@ -70,18 +72,20 @@
import de.metas.pricing.service.IPriceListDAO;
import de.metas.product.IProductBL;
import de.metas.product.IProductDAO;
import de.metas.product.ProductId;
import de.metas.quantity.Quantity;
import de.metas.tax.api.ITaxBL;
import lombok.NonNull;

public class OrderLineBL implements IOrderLineBL
{

private static final Logger logger = LogManager.getLogger(OrderLineBL.class);

public static final String SYSCONFIG_CountryAttribute = "de.metas.swat.CountryAttribute";
private static final String MSG_COUNTER_DOC_MISSING_MAPPED_PRODUCT = "de.metas.order.CounterDocMissingMappedProduct";

private static final String SYSCONFIG_SetBOMDescription = "de.metas.order.sales.line.SetBOMDescription";

private I_C_UOM getUOM(final org.compiere.model.I_C_OrderLine orderLine)
{
final I_C_UOM uom = orderLine.getC_UOM();
Expand Down Expand Up @@ -609,4 +613,37 @@ public PriceLimitRuleResult computePriceLimit(@NonNull final org.compiere.model.
.computePriceLimit();
}

/**
*
* @task https://github.com/metasfresh/metasfresh/issues/4535
*/
@Override
public void updateProductDescriptionFromProductBOMIfConfigured(final org.compiere.model.I_C_OrderLine orderLine)
{
if (!isSetBOMDescription())
{
return;
}

final ProductId productId = ProductId.ofRepoIdOrNull(orderLine.getM_Product_ID());
if (productId == null)
{
return;
}

final IProductBOMBL productBOMBL = Services.get(IProductBOMBL.class);
final String description = productBOMBL.getBOMDescriptionForProductId(productId);
if (Check.isEmpty(description, true))
{
return;
}

orderLine.setProductDescription(description);
}

private boolean isSetBOMDescription()
{
return Services.get(ISysConfigBL.class).getBooleanValue(SYSCONFIG_SetBOMDescription, false);
}

}

0 comments on commit a8a62f3

Please sign in to comment.