/****************************************************************************** * Product: Adempiere ERP & CRM Smart Business Solution * * Copyright (C) 1999-2006 ComPiere, Inc. All Rights Reserved. * * This program is free software; you can redistribute it and/or modify it * * under the terms version 2 of the GNU General Public License as published * * by the Free Software Foundation. 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., * * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. * * For the text or an alternative of this public license, you may reach us * * ComPiere, Inc., 2620 Augustine Dr. #245, Santa Clara, CA 95054, USA * * or via info@compiere.org or http://www.compiere.org/license.html * *****************************************************************************/ package org.compiere.model; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Timestamp; import java.text.DateFormat; import java.util.Properties; import java.util.logging.Level; import org.compiere.util.CLogger; import org.compiere.util.DB; import org.compiere.util.DisplayType; import org.compiere.util.KeyNamePair; import org.compiere.util.TimeUtil; /** * Product Attribute Set Instance * * @author Jorg Janke * @version $Id: MAttributeSetInstance.java,v 1.3 2006/07/30 00:51:03 jjanke Exp $ * * @author Teo Sarca, www.arhipac.ro *
* Set Description. * - Product Values * - Instance Values * - SerNo = #123 * - Lot = \u00ab123\u00bb * - GuaranteeDate = 10/25/2003 **/ public void setDescription() { // Make sure we have a Attribute Set getMAttributeSet(); if (m_mas == null) { setDescription (""); return; } StringBuilder sb = new StringBuilder(); // Instance Attribute Values MAttribute[] attributes = m_mas.getMAttributes(true); for (int i = 0; i < attributes.length; i++) { MAttributeInstance mai = attributes[i].getMAttributeInstance(getM_AttributeSetInstance_ID()); if (mai != null && mai.getValue() != null) { if (sb.length() > 0) sb.append("_"); sb.append(mai.getValue()); } } // SerNo if (m_mas.isSerNo() && getSerNo() != null) { if (sb.length() > 0) sb.append("_"); sb.append(m_mas.getSerNoCharStart()).append(getSerNo()).append(m_mas.getSerNoCharEnd()); } // Lot if (m_mas.isLot() && getLot() != null) { if (sb.length() > 0) sb.append("_"); sb.append(m_mas.getLotCharStart()).append(getLot()).append(m_mas.getLotCharEnd()); } // GuaranteeDate if (m_mas.isGuaranteeDate() && getGuaranteeDate() != null) { if (sb.length() > 0) sb.append("_"); sb.append (m_dateFormat.format(getGuaranteeDate())); } // Product Attribute Values attributes = m_mas.getMAttributes(false); for (int i = 0; i < attributes.length; i++) { MAttributeInstance mai = attributes[i].getMAttributeInstance(getM_AttributeSetInstance_ID()); if (mai != null) { if (sb.length() > 0) sb.append("_"); if (mai.getValue() != null) sb.append(mai.getValue()); else sb.append(""); } } setDescription (sb.toString()); } // setDescription /** * Get Guarantee Date * @param getNew if true calculates/sets guarantee date * @return guarantee date or null if days = 0 */ public Timestamp getGuaranteeDate(boolean getNew) { if (getNew) { int days = getMAttributeSet().getGuaranteeDays(); if (days > 0) { Timestamp ts = TimeUtil.addDays(new Timestamp(System.currentTimeMillis()), days); setGuaranteeDate(ts); } } return getGuaranteeDate(); } // getGuaranteeDate /** * Get Lot No * @param getNew if true create/set new lot * @param M_Product_ID product used if new * @return lot */ public String getLot (boolean getNew, int M_Product_ID) { if (getNew) createLot(M_Product_ID); return getLot(); } // getLot /** * Create Lot * @param M_Product_ID product * @return KeyNamePair(M_Lot_ID,Name) */ public KeyNamePair createLot (int M_Product_ID) { KeyNamePair retValue = null; int M_LotCtl_ID = getMAttributeSet().getM_LotCtl_ID(); if (M_LotCtl_ID != 0) { MLotCtl ctl = new MLotCtl (getCtx(), M_LotCtl_ID, null); MLot lot = ctl.createLot(M_Product_ID); setM_Lot_ID (lot.getM_Lot_ID()); setLot (lot.getName()); retValue = new KeyNamePair (lot.getM_Lot_ID(), lot.getName()); } return retValue; } // createLot /** * To find lot and set Lot/ID * @param Lot lot * @param M_Product_ID product */ public void setLot (String Lot, int M_Product_ID) { // Try to find it MLot mLot = MLot.getProductLot(getCtx(), M_Product_ID, Lot, get_TrxName()); if (mLot != null) setM_Lot_ID(mLot.getM_Lot_ID()); setLot (Lot); } // setLot /** * Check is Lot creation excluded for a table * @param AD_Column_ID column of table to check * @param isSOTrx true for sales transaction, false otherwise * @return true if excluded */ public boolean isExcludeLot (int AD_Column_ID, boolean isSOTrx) { getMAttributeSet(); if (m_mas != null) return m_mas.isExcludeLot (AD_Column_ID, isSOTrx); return false; } // isExcludeLot /** * Get Serial No * @param getNew if true create/set new Ser No * @return Serial Number */ public String getSerNo (boolean getNew) { if (getNew) { int M_SerNoCtl_ID = getMAttributeSet().getM_SerNoCtl_ID(); if (M_SerNoCtl_ID != 0) { MSerNoCtl ctl = new MSerNoCtl (getCtx(), M_SerNoCtl_ID, get_TrxName()); setSerNo(ctl.createSerNo()); } } return getSerNo(); } // getSerNo /** * Check is SerNo creation excluded for a table * @param AD_Column_ID column of table to check * @param isSOTrx true for sales transaction, false otherwise * @return true if excluded */ public boolean isExcludeSerNo (int AD_Column_ID, boolean isSOTrx) { getMAttributeSet(); if (m_mas != null) return m_mas.isExcludeSerNo (AD_Column_ID, isSOTrx); return false; } // isExcludeSerNo @Override protected boolean afterSave(boolean newRecord, boolean success) { if (super.afterSave(newRecord, success)) { if (newRecord && success) { //use id as description when description is empty String desc = this.getDescription(); if (desc == null || desc.trim().length() == 0) { this.set_ValueNoCheck("Description", Integer.toString(getM_AttributeSetInstance_ID())); String sql = "UPDATE M_AttributeSetInstance SET Description = ? WHERE M_AttributeSetInstance_ID = ?"; int no = DB.executeUpdateEx(sql, new Object[]{Integer.toString(getM_AttributeSetInstance_ID()), getM_AttributeSetInstance_ID()}, get_TrxName()); if (no <= 0) { log.log(Level.SEVERE, "Failed to update description."); return false; } } } return true; } return false; } /** * Create and save a new ASI for given product. * Automatically creates Lot#, Serial# and Guarantee Date. * @param ctx * @param product * @param trxName * @return newly created ASI */ public static MAttributeSetInstance create(Properties ctx, MProduct product, String trxName) { MAttributeSetInstance asi = new MAttributeSetInstance(ctx, 0, trxName); asi.setClientOrg(product.getAD_Client_ID(), 0); asi.setM_AttributeSet_ID(product.getM_AttributeSet_ID()); // Create new Lot, Serial# and Guarantee Date if (asi.getM_AttributeSet_ID() > 0) { asi.getLot(true, product.get_ID()); asi.getSerNo(true); asi.getGuaranteeDate(true); } // asi.saveEx(); return asi; } /** * AutoGenerate and save a new ASI for given product. * Automatically creates Lot#. * @param ctx * @param product * @param trxName * @return newly created ASI */ public static MAttributeSetInstance generateLot(Properties ctx, MProduct product, String trxName) { MAttributeSetInstance asi = new MAttributeSetInstance(ctx, 0, trxName); asi.setClientOrg(product.getAD_Client_ID(), 0); asi.setM_AttributeSet_ID(product.getM_AttributeSet_ID()); // Create new Lot if (asi.getM_AttributeSet_ID() > 0) { asi.getLot(true, product.get_ID()); } // asi.setDescription(); asi.saveEx(); return asi; } } // MAttributeSetInstance