/** * Invoice Line - Quantity. - called from C_UOM_ID, QtyEntered, QtyInvoiced - enforces qty UOM * relationship * * @param ctx context * @param WindowNo window no * @param mTab tab * @param mField field * @param value value * @return null or error message */ public String qty(Properties ctx, int WindowNo, GridTab mTab, GridField mField, Object value) { if (isCalloutActive() || value == null) return ""; int M_Product_ID = Env.getContextAsInt(ctx, WindowNo, "M_Product_ID"); // log.log(Level.WARNING,"qty - init - M_Product_ID=" + M_Product_ID); BigDecimal QtyInvoiced, QtyEntered, PriceActual, PriceEntered; // No Product if (M_Product_ID == 0) { QtyEntered = (BigDecimal) mTab.getValue("QtyEntered"); mTab.setValue("QtyInvoiced", QtyEntered); } // UOM Changed - convert from Entered -> Product else if (mField.getColumnName().equals("C_UOM_ID")) { int C_UOM_To_ID = ((Integer) value).intValue(); QtyEntered = (BigDecimal) mTab.getValue("QtyEntered"); BigDecimal QtyEntered1 = QtyEntered.setScale(MUOM.getPrecision(ctx, C_UOM_To_ID), BigDecimal.ROUND_HALF_UP); if (QtyEntered.compareTo(QtyEntered1) != 0) { log.fine( "Corrected QtyEntered Scale UOM=" + C_UOM_To_ID + "; QtyEntered=" + QtyEntered + "->" + QtyEntered1); QtyEntered = QtyEntered1; mTab.setValue("QtyEntered", QtyEntered); } QtyInvoiced = MUOMConversion.convertProductFrom(ctx, M_Product_ID, C_UOM_To_ID, QtyEntered); if (QtyInvoiced == null) QtyInvoiced = QtyEntered; boolean conversion = QtyEntered.compareTo(QtyInvoiced) != 0; PriceActual = (BigDecimal) mTab.getValue("PriceActual"); PriceEntered = MUOMConversion.convertProductFrom(ctx, M_Product_ID, C_UOM_To_ID, PriceActual); if (PriceEntered == null) PriceEntered = PriceActual; log.fine( "qty - UOM=" + C_UOM_To_ID + ", QtyEntered/PriceActual=" + QtyEntered + "/" + PriceActual + " -> " + conversion + " QtyInvoiced/PriceEntered=" + QtyInvoiced + "/" + PriceEntered); Env.setContext(ctx, WindowNo, "UOMConversion", conversion ? "Y" : "N"); mTab.setValue("QtyInvoiced", QtyInvoiced); mTab.setValue("PriceEntered", PriceEntered); } // QtyEntered changed - calculate QtyInvoiced else if (mField.getColumnName().equals("QtyEntered")) { int C_UOM_To_ID = Env.getContextAsInt(ctx, WindowNo, "C_UOM_ID"); QtyEntered = (BigDecimal) value; BigDecimal QtyEntered1 = QtyEntered.setScale(MUOM.getPrecision(ctx, C_UOM_To_ID), BigDecimal.ROUND_HALF_UP); if (QtyEntered.compareTo(QtyEntered1) != 0) { log.fine( "Corrected QtyEntered Scale UOM=" + C_UOM_To_ID + "; QtyEntered=" + QtyEntered + "->" + QtyEntered1); QtyEntered = QtyEntered1; mTab.setValue("QtyEntered", QtyEntered); } QtyInvoiced = MUOMConversion.convertProductFrom(ctx, M_Product_ID, C_UOM_To_ID, QtyEntered); if (QtyInvoiced == null) QtyInvoiced = QtyEntered; boolean conversion = QtyEntered.compareTo(QtyInvoiced) != 0; log.fine( "qty - UOM=" + C_UOM_To_ID + ", QtyEntered=" + QtyEntered + " -> " + conversion + " QtyInvoiced=" + QtyInvoiced); Env.setContext(ctx, WindowNo, "UOMConversion", conversion ? "Y" : "N"); mTab.setValue("QtyInvoiced", QtyInvoiced); } // QtyInvoiced changed - calculate QtyEntered (should not happen) else if (mField.getColumnName().equals("QtyInvoiced")) { int C_UOM_To_ID = Env.getContextAsInt(ctx, WindowNo, "C_UOM_ID"); QtyInvoiced = (BigDecimal) value; int precision = MProduct.get(ctx, M_Product_ID).getUOMPrecision(); BigDecimal QtyInvoiced1 = QtyInvoiced.setScale(precision, BigDecimal.ROUND_HALF_UP); if (QtyInvoiced.compareTo(QtyInvoiced1) != 0) { log.fine("Corrected QtyInvoiced Scale " + QtyInvoiced + "->" + QtyInvoiced1); QtyInvoiced = QtyInvoiced1; mTab.setValue("QtyInvoiced", QtyInvoiced); } QtyEntered = MUOMConversion.convertProductTo(ctx, M_Product_ID, C_UOM_To_ID, QtyInvoiced); if (QtyEntered == null) QtyEntered = QtyInvoiced; boolean conversion = QtyInvoiced.compareTo(QtyEntered) != 0; log.fine( "qty - UOM=" + C_UOM_To_ID + ", QtyInvoiced=" + QtyInvoiced + " -> " + conversion + " QtyEntered=" + QtyEntered); Env.setContext(ctx, WindowNo, "UOMConversion", conversion ? "Y" : "N"); mTab.setValue("QtyEntered", QtyEntered); } // return ""; } // qty
@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
/** * Invoice - Amount. - called from QtyInvoiced, PriceActual - calculates LineNetAmt * * @param ctx context * @param WindowNo window no * @param mTab tab * @param mField field * @param value value * @return null or error message */ public String amt(Properties ctx, int WindowNo, GridTab mTab, GridField mField, Object value) { if (isCalloutActive() || value == null) return ""; // log.log(Level.WARNING,"amt - init"); int C_UOM_To_ID = Env.getContextAsInt(ctx, WindowNo, "C_UOM_ID"); int M_Product_ID = Env.getContextAsInt(ctx, WindowNo, "M_Product_ID"); int M_PriceList_ID = Env.getContextAsInt(ctx, WindowNo, "M_PriceList_ID"); int StdPrecision = MPriceList.getStandardPrecision(ctx, M_PriceList_ID); BigDecimal QtyEntered, QtyInvoiced, PriceEntered, PriceActual, PriceLimit, Discount, PriceList; // get values QtyEntered = (BigDecimal) mTab.getValue("QtyEntered"); QtyInvoiced = (BigDecimal) mTab.getValue("QtyInvoiced"); log.fine("QtyEntered=" + QtyEntered + ", Invoiced=" + QtyInvoiced + ", UOM=" + C_UOM_To_ID); // PriceEntered = (BigDecimal) mTab.getValue("PriceEntered"); PriceActual = (BigDecimal) mTab.getValue("PriceActual"); // Discount = (BigDecimal)mTab.getValue("Discount"); PriceLimit = (BigDecimal) mTab.getValue("PriceLimit"); PriceList = (BigDecimal) mTab.getValue("PriceList"); log.fine("PriceList=" + PriceList + ", Limit=" + PriceLimit + ", Precision=" + StdPrecision); log.fine( "PriceEntered=" + PriceEntered + ", Actual=" + PriceActual); // + ", Discount=" + Discount); // No Product if (M_Product_ID == 0) { // if price change sync price actual and entered // else ignore if (mField.getColumnName().equals("PriceActual")) { PriceEntered = (BigDecimal) value; mTab.setValue("PriceEntered", value); } else if (mField.getColumnName().equals("PriceEntered")) { PriceActual = (BigDecimal) value; mTab.setValue("PriceActual", value); } } // Product Qty changed - recalc price else if ((mField.getColumnName().equals("QtyInvoiced") || mField.getColumnName().equals("QtyEntered") || mField.getColumnName().equals("C_UOM_ID") || mField.getColumnName().equals("M_Product_ID")) && !"N".equals(Env.getContext(ctx, WindowNo, "DiscountSchema"))) { int C_BPartner_ID = Env.getContextAsInt(ctx, WindowNo, "C_BPartner_ID"); if (mField.getColumnName().equals("QtyEntered")) QtyInvoiced = MUOMConversion.convertProductFrom(ctx, M_Product_ID, C_UOM_To_ID, QtyEntered); if (QtyInvoiced == null) QtyInvoiced = QtyEntered; boolean IsSOTrx = Env.getContext(ctx, WindowNo, "IsSOTrx").equals("Y"); MProductPricing pp = new MProductPricing(M_Product_ID, C_BPartner_ID, QtyInvoiced, IsSOTrx); pp.setM_PriceList_ID(M_PriceList_ID); int M_PriceList_Version_ID = Env.getContextAsInt(ctx, WindowNo, "M_PriceList_Version_ID"); pp.setM_PriceList_Version_ID(M_PriceList_Version_ID); Timestamp date = (Timestamp) mTab.getValue("DateInvoiced"); pp.setPriceDate(date); // PriceEntered = MUOMConversion.convertProductFrom(ctx, M_Product_ID, C_UOM_To_ID, pp.getPriceStd()); if (PriceEntered == null) PriceEntered = pp.getPriceStd(); // log.fine( "amt - QtyChanged -> PriceActual=" + pp.getPriceStd() + ", PriceEntered=" + PriceEntered + ", Discount=" + pp.getDiscount()); PriceActual = pp.getPriceStd(); mTab.setValue("PriceActual", pp.getPriceStd()); // mTab.setValue("Discount", pp.getDiscount()); mTab.setValue("PriceEntered", PriceEntered); Env.setContext(ctx, WindowNo, "DiscountSchema", pp.isDiscountSchema() ? "Y" : "N"); } else if (mField.getColumnName().equals("PriceActual")) { PriceActual = (BigDecimal) value; PriceEntered = MUOMConversion.convertProductFrom(ctx, M_Product_ID, C_UOM_To_ID, PriceActual); if (PriceEntered == null) PriceEntered = PriceActual; // log.fine("amt - PriceActual=" + PriceActual + " -> PriceEntered=" + PriceEntered); mTab.setValue("PriceEntered", PriceEntered); } else if (mField.getColumnName().equals("PriceEntered")) { PriceEntered = (BigDecimal) value; PriceActual = MUOMConversion.convertProductTo(ctx, M_Product_ID, C_UOM_To_ID, PriceEntered); if (PriceActual == null) PriceActual = PriceEntered; // log.fine("amt - PriceEntered=" + PriceEntered + " -> PriceActual=" + PriceActual); mTab.setValue("PriceActual", PriceActual); } /** * Discount entered - Calculate Actual/Entered if (mField.getColumnName().equals("Discount")) { * PriceActual = new BigDecimal ((100.0 - Discount.doubleValue()) / 100.0 * * PriceList.doubleValue()); if (PriceActual.scale() > StdPrecision) PriceActual = * PriceActual.setScale(StdPrecision, BigDecimal.ROUND_HALF_UP); PriceEntered = * MUOMConversion.convertProductFrom (ctx, M_Product_ID, C_UOM_To_ID, PriceActual); if * (PriceEntered == null) PriceEntered = PriceActual; mTab.setValue("PriceActual", PriceActual); * mTab.setValue("PriceEntered", PriceEntered); } // calculate Discount else { if * (PriceList.intValue() == 0) Discount = Env.ZERO; else Discount = new BigDecimal * ((PriceList.doubleValue() - PriceActual.doubleValue()) / PriceList.doubleValue() * 100.0); if * (Discount.scale() > 2) Discount = Discount.setScale(2, BigDecimal.ROUND_HALF_UP); * mTab.setValue("Discount", Discount); } log.fine("amt = PriceEntered=" + PriceEntered + ", * Actual" + PriceActual + ", Discount=" + Discount); /* */ // Check PriceLimit String epl = Env.getContext(ctx, WindowNo, "EnforcePriceLimit"); boolean enforce = Env.isSOTrx(ctx, WindowNo) && epl != null && epl.equals("Y"); if (enforce && MRole.getDefault().isOverwritePriceLimit()) enforce = false; // Check Price Limit? if (enforce && PriceLimit.doubleValue() != 0.0 && PriceActual.compareTo(PriceLimit) < 0) { PriceActual = PriceLimit; PriceEntered = MUOMConversion.convertProductFrom(ctx, M_Product_ID, C_UOM_To_ID, PriceLimit); if (PriceEntered == null) PriceEntered = PriceLimit; log.fine("amt =(under) PriceEntered=" + PriceEntered + ", Actual" + PriceLimit); mTab.setValue("PriceActual", PriceLimit); mTab.setValue("PriceEntered", PriceEntered); mTab.fireDataStatusEEvent("UnderLimitPrice", "", false); // Repeat Discount calc if (PriceList.intValue() != 0) { Discount = new BigDecimal( (PriceList.doubleValue() - PriceActual.doubleValue()) / PriceList.doubleValue() * 100.0); if (Discount.scale() > 2) Discount = Discount.setScale(2, BigDecimal.ROUND_HALF_UP); // mTab.setValue ("Discount", Discount); } } // Line Net Amt BigDecimal LineNetAmt = QtyInvoiced.multiply(PriceActual); if (LineNetAmt.scale() > StdPrecision) LineNetAmt = LineNetAmt.setScale(StdPrecision, BigDecimal.ROUND_HALF_UP); log.info("amt = LineNetAmt=" + LineNetAmt); mTab.setValue("LineNetAmt", LineNetAmt); // Calculate Tax Amount for PO boolean IsSOTrx = "Y".equals(Env.getContext(Env.getCtx(), WindowNo, "IsSOTrx")); if (!IsSOTrx) { BigDecimal TaxAmt = Env .ZERO; // teo_sarca: [ 1656829 ] Problem when there is not tax selected in vendor // invoice if (mField.getColumnName().equals("TaxAmt")) { TaxAmt = (BigDecimal) mTab.getValue("TaxAmt"); } else { Integer taxID = (Integer) mTab.getValue("C_Tax_ID"); if (taxID != null) { int C_Tax_ID = taxID.intValue(); MTax tax = new MTax(ctx, C_Tax_ID, null); TaxAmt = tax.calculateTax(LineNetAmt, isTaxIncluded(WindowNo), StdPrecision); mTab.setValue("TaxAmt", TaxAmt); } } // Add it up mTab.setValue("LineTotalAmt", LineNetAmt.add(TaxAmt)); } return ""; } // amt