@Override protected boolean afterSave(boolean newRecord, boolean success) { if (!success) return success; // Value/Name change in Account if (!newRecord && (is_ValueChanged("Value") || is_ValueChanged("Name"))) MAccount.updateValueDescription(getCtx(), "M_Product_ID=" + getM_Product_ID(), get_TrxName()); // Name/Description Change in Asset MAsset.setValueNameDescription if (!newRecord && (is_ValueChanged("Name") || is_ValueChanged("Description"))) { String sql = "UPDATE A_Asset a " + "SET (Name, Description)=" + "(SELECT SUBSTR((SELECT bp.Name FROM C_BPartner bp WHERE bp.C_BPartner_ID=a.C_BPartner_ID) || ' - ' || p.Name,1,60), p.Description " + "FROM M_Product p " + "WHERE p.M_Product_ID=a.M_Product_ID) " + "WHERE IsActive='Y'" // + " AND GuaranteeDate > SysDate" + " AND M_Product_ID=" + getM_Product_ID(); int no = DB.executeUpdate(sql, get_TrxName()); log.fine("Asset Description updated #" + no); } // New - Acct, Tree, Old Costing if (newRecord) { insert_Accounting( "M_Product_Acct", "M_Product_Category_Acct", "p.M_Product_Category_ID=" + getM_Product_Category_ID()); insert_Tree(X_AD_Tree.TREETYPE_Product); // MAcctSchema[] mass = MAcctSchema.getClientAcctSchema(getCtx(), getAD_Client_ID(), get_TrxName()); for (int i = 0; i < mass.length; i++) { // Old MProductCosting pcOld = new MProductCosting(this, mass[i].getC_AcctSchema_ID()); pcOld.saveEx(); } } // New Costing if (newRecord || is_ValueChanged("M_Product_Category_ID")) MCost.create(this); return success; } // afterSave
@Override protected boolean beforeDelete() { if (PRODUCTTYPE_Resource.equals(getProductType()) && getS_Resource_ID() > 0) { throw new AdempiereException("@S_Resource_ID@<>0"); } // Check Storage if (isStocked() || PRODUCTTYPE_Item.equals(getProductType())) { MStorage[] storages = MStorage.getOfProduct(getCtx(), get_ID(), get_TrxName()); BigDecimal OnHand = Env.ZERO; BigDecimal Ordered = Env.ZERO; BigDecimal Reserved = Env.ZERO; for (int i = 0; i < storages.length; i++) { OnHand = OnHand.add(storages[i].getQtyOnHand()); Ordered = OnHand.add(storages[i].getQtyOrdered()); Reserved = OnHand.add(storages[i].getQtyReserved()); } String errMsg = ""; if (OnHand.signum() != 0) errMsg = "@QtyOnHand@ = " + OnHand; if (Ordered.signum() != 0) errMsg += " - @QtyOrdered@ = " + Ordered; if (Reserved.signum() != 0) errMsg += " - @QtyReserved@" + Reserved; if (errMsg.length() > 0) { log.saveError("Error", Msg.parseTranslation(getCtx(), errMsg)); return false; } } // delete costing MProductCosting[] costings = MProductCosting.getOfProduct(getCtx(), get_ID(), get_TrxName()); for (int i = 0; i < costings.length; i++) costings[i].delete(true, get_TrxName()); MCost.delete(this); // [ 1674225 ] Delete Product: Costing deletion error /*MAcctSchema[] mass = MAcctSchema.getClientAcctSchema(getCtx(),getAD_Client_ID(), get_TrxName()); for(int i=0; i<mass.length; i++) { // Get Cost Elements MCostElement[] ces = MCostElement.getMaterialWithCostingMethods(this); MCostElement ce = null; for(int j=0; j<ces.length; j++) { if(MCostElement.COSTINGMETHOD_StandardCosting.equals(ces[i].getCostingMethod())) { ce = ces[i]; break; } } if(ce == null) continue; MCost mcost = MCost.get(this, 0, mass[i], 0, ce.getM_CostElement_ID()); mcost.delete(true, get_TrxName()); }*/ // @Trifon Delete Product UOM Conversion final String whereClause = MProduct.COLUMNNAME_M_Product_ID + "=?"; List<MUOMConversion> conversions = new Query(getCtx(), I_C_UOM_Conversion.Table_Name, whereClause, get_TrxName()) .setClient_ID() .setParameters(get_ID()) .setOnlyActiveRecords(false) .list(); for (MUOMConversion conversion : conversions) { conversion.deleteEx(true); } // @Trifon Delete Product Downloads List<MProductDownload> downloads = new Query(getCtx(), I_M_ProductDownload.Table_Name, whereClause, get_TrxName()) .setClient_ID() .setParameters(get_ID()) .setOnlyActiveRecords(false) .list(); for (MProductDownload download : downloads) { download.deleteEx(true); } // return delete_Accounting("M_Product_Acct"); } // beforeDelete