private String orderDiscount(MOrder order){
  String DISCOUNT="Discount";
  int C_Tax_ID=0;
  BigDecimal totalLines=Env.ZERO;
  MOrderLine discountLine=null;
  MOrderLine[] lines=order.getLines();
  for (int i=0; i < lines.length; i++) {
    MOrderLine oLine=lines[i];
    String description=oLine.getDescription();
    if (description != null && description.equals(DISCOUNT))     discountLine=oLine;
 else {
      totalLines=totalLines.add(oLine.getLineNetAmt());
      if (C_Tax_ID == 0)       C_Tax_ID=oLine.getC_Tax_ID();
 else       if (C_Tax_ID != oLine.getC_Tax_ID())       return "Order has more then one Tax, cannot add discount";
    }
  }
  if (discountLine == null) {
    discountLine=new MOrderLine(order);
    discountLine.setDescription(DISCOUNT);
    discountLine.setQty(Env.ONE);
    discountLine.setC_Tax_ID(C_Tax_ID);
    discountLine.setLine(9999);
  }
  BigDecimal discountPercent=new BigDecimal(3);
  BigDecimal discountAmt=totalLines.multiply(discountPercent);
  discountAmt=discountAmt.divide(Env.ONEHUNDRED,order.getPrecision(),BigDecimal.ROUND_HALF_UP);
  discountLine.setPrice(discountAmt.negate());
  if (!discountLine.save())   return "Could not save discount line";
  log.info(discountLine.toString());
  order.getLines(true,null);
  return null;
}

private String orderDiscount ( MOrder order ) { String DISCOUNT = "Discount" ; int C_Tax_ID = 0 ; BigDecimal totalLines = Env . ZERO ; MOrderLine discountLine = null ; MOrderLine [ ] lines = order . getLines ( ) ; for ( int i = 0 ; i < lines . length ; i ++ ) { MOrderLine oLine = lines [ i ] ; String description = oLine . getDescription ( ) ; if ( description != null && description . equals ( DISCOUNT ) ) discountLine = oLine ; else { totalLines = totalLines . add ( oLine . getLineNetAmt ( ) ) ; if ( C_Tax_ID == 0 ) C_Tax_ID = oLine . getC_Tax_ID ( ) ; else if ( C_Tax_ID != oLine . getC_Tax_ID ( ) ) return "Order has more then one Tax, cannot add discount" ; } } if ( discountLine == null ) { discountLine = new MOrderLine ( order ) ; discountLine . setDescription ( DISCOUNT ) ; discountLine . setQty ( Env . ONE ) ; discountLine . setC_Tax_ID ( C_Tax_ID ) ; discountLine . setLine ( 9999 ) ; } BigDecimal discountPercent = new BigDecimal ( 3 ) ; BigDecimal discountAmt = totalLines . multiply ( discountPercent ) ; discountAmt = discountAmt . divide ( Env . ONEHUNDRED , order . getPrecision ( ) , BigDecimal . ROUND_HALF_UP ) ; discountLine . setPrice ( discountAmt . negate ( ) ) ; if ( ! discountLine . save ( ) ) return "Could not save discount line" ; log . info ( discountLine . toString ( ) ) ; order . getLines ( true , null ) ; return null ; }
order discount . make sure that last line is discount and check correctness
