protected override String DoIt() { MInvoice obj = new MInvoice(GetCtx(), GetRecord_ID(), Get_Trx()); // get Precision for rounding MCurrency currency = new MCurrency(GetCtx(), obj.GetC_Currency_ID(), Get_Trx()); precision = currency.GetStdPrecision(); MInvoiceLine[] lines = obj.GetLines(); if (_IsCLearDiscount == "N") { if (_DiscountAmt == 0 && _DiscountPercent == 0) { return(Msg.GetMsg(GetCtx(), "PlsSelAtlstOneField")); } if (_DiscountAmt != 0 && _DiscountPercent != 0) { return(Msg.GetMsg(GetCtx(), "PlsSelOneField")); } // get amount on which we have to apply discount subTotal = obj.GetTotalLines(); // when we are giving discount in terms of amount, then we have to calculate discount in term of percentage discountPercentageOnTotalAmount = GetDiscountPercentageOnTotal(subTotal, _DiscountAmt, precision); for (int i = 0; i < lines.Length; i++) { MInvoiceLine ln = lines[i]; // this value represent discount on line net amount discountAmountOnTotal = GetDiscountAmountOnTotal(ln.GetLineNetAmt(), discountPercentageOnTotalAmount != 0 ? discountPercentageOnTotalAmount : _DiscountPercent); // this value represent discount on unit price of 1 qty discountAmountOnTotal = Decimal.Round(Decimal.Divide(discountAmountOnTotal, ln.GetQtyEntered()), precision); ln.SetAmountAfterApplyDiscount(Decimal.Add(ln.GetAmountAfterApplyDiscount(), discountAmountOnTotal)); ln.SetPriceActual(Decimal.Round(Decimal.Subtract(ln.GetPriceActual(), discountAmountOnTotal), precision)); ln.SetPriceEntered(Decimal.Round(Decimal.Subtract(ln.GetPriceEntered(), discountAmountOnTotal), precision)); // set tax amount as 0, so that on before save we calculate tax again on discounted price ln.SetTaxAmt(0); if (!ln.Save(Get_TrxName())) { Rollback(); ValueNamePair pp = VLogger.RetrieveError(); log.Info("ApplyDiscountInvoiceVendor : Not Saved. Error Value : " + pp.GetValue() + " , Error Name : " + pp.GetName()); throw new Exception(Msg.GetMsg(GetCtx(), "DiscNotApplied")); } } return(Msg.GetMsg(GetCtx(), "DiscAppliedSuccess")); } else { for (int i = 0; i < lines.Length; i++) { MInvoiceLine ln = lines[i]; ln.SetPriceEntered(Decimal.Add(ln.GetPriceEntered(), ln.GetAmountAfterApplyDiscount())); ln.SetPriceActual(Decimal.Add(ln.GetPriceActual(), ln.GetAmountAfterApplyDiscount())); ln.SetAmountAfterApplyDiscount(0); ln.SetTaxAmt(0); if (!ln.Save(Get_TrxName())) { Rollback(); ValueNamePair pp = VLogger.RetrieveError(); log.Info("ApplyDiscountInvoiceVendor : Not Saved. Error Value : " + pp.GetValue() + " , Error Name : " + pp.GetName()); throw new Exception(Msg.GetMsg(GetCtx(), "DiscNotCleared")); } } return(Msg.GetMsg(GetCtx(), "DiscClearedSuccessfully")); } }