IDEMPIERE-5127 MProduct: add IsSerial method (#1096)

* IDEMPIERE-5127 MProduct: add IsSerial method

* Update serialVersionUID

Co-authored-by: Carlos Ruiz <carg67@gmail.com>
This commit is contained in:
hengsin 2021-12-28 23:36:43 +08:00 committed by GitHub
parent 184d9c5603
commit b1446ecf65
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 221 additions and 1 deletions

View File

@ -53,7 +53,7 @@ public class MProduct extends X_M_Product implements ImmutablePOSupport
/**
*
*/
private static final long serialVersionUID = 8710213660955199146L;
private static final long serialVersionUID = 6847265056758898333L;
/**
* Get MProduct from Cache (immutable)
@ -998,4 +998,17 @@ public class MProduct extends X_M_Product implements ImmutablePOSupport
return this;
}
/**
* @return true if instance of product is managed with serial no
*/
public boolean isSerial() {
if (getM_AttributeSet_ID() == 0)
return false;
MAttributeSet as = MAttributeSet.get(getM_AttributeSet_ID());
if (as.isInstanceAttribute() && as.isSerNo())
return true;
else
return false;
}
} // MProduct

View File

@ -0,0 +1,207 @@
/***********************************************************************
* This file is part of iDempiere ERP Open Source *
* http://www.idempiere.org *
* *
* Copyright (C) Contributors *
* *
* 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, write to the Free Software *
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, *
* MA 02110-1301, USA. *
* *
* Contributors: *
* - hengsin *
**********************************************************************/
package org.idempiere.test.model;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.math.BigDecimal;
import java.sql.Timestamp;
import org.compiere.model.MAcctSchema;
import org.compiere.model.MAttributeInstance;
import org.compiere.model.MAttributeSet;
import org.compiere.model.MBPartner;
import org.compiere.model.MClient;
import org.compiere.model.MCost;
import org.compiere.model.MInOut;
import org.compiere.model.MInOutLine;
import org.compiere.model.MOrder;
import org.compiere.model.MOrderLine;
import org.compiere.model.MProduct;
import org.compiere.model.MUOM;
import org.compiere.process.DocAction;
import org.compiere.process.DocumentEngine;
import org.compiere.process.ProcessInfo;
import org.compiere.util.CacheMgt;
import org.compiere.util.Env;
import org.compiere.util.TimeUtil;
import org.compiere.wf.MWorkflow;
import org.idempiere.test.AbstractTestCase;
import org.junit.jupiter.api.Test;
/**
*
* @author hengsin
*
*/
public class MProductTest extends AbstractTestCase {
private static final int BP_PATIO = 121;
private static final int DOCTYPE_PO = 126;
private static final int DOCTYPE_RECEIPT = 122;
private static final int USER_GARDENADMIN = 101;
private static final int MULCH_PRODUCT_ID = 137;
private static final int TSHIRT_GL_PRODUCT_ID = 148;
private static final int COLOR_ATTRIBUTE_ID = 101;
private final static int HOUR_UOM_ID = 101;
private final static int FERTILIZER_LOT_ATTRIBUTESET_ID = 101;
private final static int PATIO_CHAIR_ATTRIBUTESET_ID = 102;
private static final int CHEMICALS_CATEGORY_ID = 109;
public MProductTest() {
}
private void createPOAndMRForProduct(int productId) {
MOrder order = new MOrder(Env.getCtx(), 0, getTrxName());
order.setBPartner(MBPartner.get(Env.getCtx(), BP_PATIO));
order.setC_DocTypeTarget_ID(DOCTYPE_PO);
order.setIsSOTrx(false);
order.setSalesRep_ID(USER_GARDENADMIN);
order.setDocStatus(DocAction.STATUS_Drafted);
order.setDocAction(DocAction.ACTION_Complete);
Timestamp today = TimeUtil.getDay(System.currentTimeMillis());
order.setDateOrdered(today);
order.setDatePromised(today);
order.saveEx();
MOrderLine line1 = new MOrderLine(order);
line1.setLine(10);
line1.setProduct(MProduct.get(Env.getCtx(), productId));
line1.setQty(new BigDecimal("1"));
line1.setDatePromised(today);
line1.saveEx();
ProcessInfo info = MWorkflow.runDocumentActionWorkflow(order, DocAction.ACTION_Complete);
assertFalse(info.isError());
order.load(getTrxName());
assertEquals(DocAction.STATUS_Completed, order.getDocStatus());
MInOut receipt1 = new MInOut(order, DOCTYPE_RECEIPT, order.getDateOrdered());
receipt1.setDocStatus(DocAction.STATUS_Drafted);
receipt1.setDocAction(DocAction.ACTION_Complete);
receipt1.saveEx();
MInOutLine receiptLine1 = new MInOutLine(receipt1);
receiptLine1.setOrderLine(line1, 0, new BigDecimal("1"));
receiptLine1.setQty(new BigDecimal("1"));
receiptLine1.saveEx();
info = MWorkflow.runDocumentActionWorkflow(receipt1, DocAction.ACTION_Complete);
assertFalse(info.isError());
receipt1.load(getTrxName());
assertEquals(DocAction.STATUS_Completed, receipt1.getDocStatus());
if (!receipt1.isPosted()) {
String error = DocumentEngine.postImmediate(Env.getCtx(), receipt1.getAD_Client_ID(), receipt1.get_Table_ID(), receipt1.get_ID(), false, getTrxName());
assertNull(error, error);
}
}
@Test
public void testIsAndGetMethods() {
MProduct product = MProduct.get(TSHIRT_GL_PRODUCT_ID);
MAttributeInstance attributeInstance = product.getAttributeInstance("Color (R-G-B)", getTrxName());
assertNotNull(attributeInstance);
assertEquals(COLOR_ATTRIBUTE_ID, attributeInstance.getM_Attribute_ID());
assertEquals("Green", attributeInstance.getValue());
MAcctSchema as = MClient.get(Env.getCtx()).getAcctSchema();
String costingLevel = product.getCostingLevel(as);
assertEquals(as.getCostingLevel(), costingLevel);
String costingMethod = product.getCostingMethod(as);
assertEquals(as.getCostingMethod(), costingMethod);
createPOAndMRForProduct(MULCH_PRODUCT_ID);
product = new MProduct(Env.getCtx(), MULCH_PRODUCT_ID, getTrxName());
MCost mcost = product.getCostingRecord(as, getAD_Org_ID(), 0);
assertNotNull(mcost);
assertEquals(product.get_ID(), mcost.getM_Product_ID());
assertEquals(mcost.getCostingMethod(), product.getCostingMethod(as));
MCost mcost1 = product.getCostingRecord(as, getAD_Org_ID(), 0, product.getCostingMethod(as));
assertEquals(mcost.getM_Cost_UU(), mcost1.getM_Cost_UU());
product = new MProduct(Env.getCtx(), 0, getTrxName());
product.setC_UOM_ID(HOUR_UOM_ID);
product.setM_Product_Category_ID(CHEMICALS_CATEGORY_ID);
assertEquals(MUOM.get(HOUR_UOM_ID).getStdPrecision(), product.getUOMPrecision());
assertEquals(MUOM.get(HOUR_UOM_ID).getUOMSymbol(), product.getUOMSymbol());
assertFalse(product.isInstanceAttribute());
product.setM_AttributeSet_ID(FERTILIZER_LOT_ATTRIBUTESET_ID);
assertTrue(product.isInstanceAttribute());
assertFalse(product.isSerial());
product.setM_AttributeSet_ID(PATIO_CHAIR_ATTRIBUTESET_ID);
assertTrue(product.isSerial());
product.setIsStocked(true);
product.setProductType(MProduct.PRODUCTTYPE_Item);
assertTrue(product.isItem());
assertFalse(product.isService());
assertTrue(product.isStocked());
product.setProductType(MProduct.PRODUCTTYPE_Service);
assertFalse(product.isItem());
assertTrue(product.isService());
assertFalse(product.isStocked());
product.setProductType(MProduct.PRODUCTTYPE_Resource);
assertFalse(product.isItem());
assertTrue(product.isService());
assertFalse(product.isStocked());
MAttributeSet attributeSet = new MAttributeSet(Env.getCtx(), PATIO_CHAIR_ATTRIBUTESET_ID, null);
String mandatoryType = attributeSet.getMandatoryType();
try {
attributeSet.setMandatoryType(MAttributeSet.MANDATORYTYPE_NotMandatory);
attributeSet.saveEx();
assertFalse(product.isASIMandatoryFor(null, false));
assertFalse(product.isASIMandatoryFor(null, true));
assertFalse(product.isASIMandatoryFor(MAttributeSet.MANDATORYTYPE_WhenShipping, true));
attributeSet.setMandatoryType(MAttributeSet.MANDATORYTYPE_AlwaysMandatory);
attributeSet.saveEx();
CacheMgt.get().reset(MAttributeSet.Table_Name, attributeSet.getM_AttributeSet_ID());
assertTrue(product.isASIMandatoryFor(null, false));
assertTrue(product.isASIMandatoryFor(null, true));
assertTrue(product.isASIMandatoryFor(MAttributeSet.MANDATORYTYPE_WhenShipping, true));
attributeSet.setMandatoryType(MAttributeSet.MANDATORYTYPE_WhenShipping);
attributeSet.saveEx();
CacheMgt.get().reset(MAttributeSet.Table_Name, attributeSet.getM_AttributeSet_ID());
assertFalse(product.isASIMandatoryFor(null, false));
assertFalse(product.isASIMandatoryFor(null, true));
assertTrue(product.isASIMandatoryFor(MAttributeSet.MANDATORYTYPE_WhenShipping, true));
assertFalse(product.isUseGuaranteeDateForMPolicy());
attributeSet.setUseGuaranteeDateForMPolicy(true);
attributeSet.saveEx();
CacheMgt.get().reset(MAttributeSet.Table_Name, attributeSet.getM_AttributeSet_ID());
assertTrue(product.isUseGuaranteeDateForMPolicy());
} finally {
attributeSet.setMandatoryType(mandatoryType);
attributeSet.setUseGuaranteeDateForMPolicy(false);
attributeSet.saveEx();
}
}
}