private decimal CalculatePointsForTransaction(DataTable loyaltyPointsTable) { decimal totalCollectedPoints = 0; foreach (DataRow row in loyaltyPointsTable.Rows) { LoyaltyPointTypeBase type = (LoyaltyPointTypeBase)row["PRODUCTTENDERTYPE"]; Int64 groupMemberLine = (Int64)row["RETAILGROUPMEMBERLINE"]; decimal qtyAmountLimit = Convert.ToDecimal(row["QTYAMOUNTLIMIT"].ToString()); decimal points = Convert.ToDecimal(row["POINTS"]); CalculationTypeBase baseCalculationOn = (CalculationTypeBase)row["BASECALCULATIONON"]; // Loyalty scheme ending date on the AX server is in a date only format // E.g. an expiry date of 4/12/2010 means that the loyalty scheme expires by the end of 4/12/2010 // if (IsValidDate(Convert.ToDateTime(row["VALIDFROM"]), Convert.ToDateTime(row["VALIDTO"]))) { totalCollectedPoints += CalculatePointsForTransactionByScheme(groupMemberLine, qtyAmountLimit, points, baseCalculationOn, type); } } return(totalCollectedPoints); }
private static decimal CalculatePointsForTender(ref bool tenderRuleFound, string tenderTypeID, decimal amount, DataTable loyaltyPointsTable) { tenderRuleFound = false; foreach (DataRow row in loyaltyPointsTable.Rows) { LoyaltyPointTypeBase type = (LoyaltyPointTypeBase)row["PRODUCTTENDERTYPE"]; if (type == LoyaltyPointTypeBase.Tender) { string loyaltytenderTypeId = row["RETAILTENDERTYPEID"].ToString(); decimal qtyAmountLimit = Convert.ToDecimal(row["QTYAMOUNTLIMIT"].ToString()); decimal points = Convert.ToDecimal(row["POINTS"]); CalculationTypeBase baseCalculationOn = (CalculationTypeBase)row["BASECALCULATIONON"]; if (IsValidDate(Convert.ToDateTime(row["VALIDFROM"]), Convert.ToDateTime(row["VALIDTO"]))) { if (tenderTypeID == loyaltytenderTypeId) { tenderRuleFound = true; if ((qtyAmountLimit > 0) && (baseCalculationOn == CalculationTypeBase.Amounts)) { decimal companyCurrencyAmount = Loyalty.InternalApplication.Services.Currency.CurrencyToCurrency( ApplicationSettings.Terminal.StoreCurrency, ApplicationSettings.Terminal.CompanyCurrency, amount); // If the company currency amount sign is not held constant, we give back less points than we charge. int sign = Math.Sign(companyCurrencyAmount); return(Math.Floor(Math.Abs(companyCurrencyAmount) / qtyAmountLimit * points) * (decimal)sign); } } } } } return(0); }
private decimal CalculatePointsForTransactionByScheme(Int64 groupMemberLineRecId, decimal qtyAmountLimit, decimal points, CalculationTypeBase baseType, LoyaltyPointTypeBase type) { try { decimal totalQty = 0; decimal totalAmount = 0; Int64 variantId; Int64 productId; Int64 categoryId; DataRow groupMemberLine = GetRetailGroupLineMember(groupMemberLineRecId); if (groupMemberLine == null) { NetTracer.Warning("Loyalty:CalculatePointsForTranactionByScheme: groupMemberLine is null"); return(decimal.Zero); } categoryId = (Int64)groupMemberLine["Category"]; productId = (Int64)groupMemberLine["Product"]; variantId = (Int64)groupMemberLine["Variant"]; if (type != LoyaltyPointTypeBase.Tender) { foreach (SaleLineItem saleLineItem in this.transaction.SaleItems) { bool found = false; if (!saleLineItem.Voided) { ItemData itemData = new ItemData( ApplicationSettings.Database.LocalConnection, ApplicationSettings.Database.DATAAREAID, ApplicationSettings.Terminal.StorePrimaryId); // check for a variant being put on loyalty if (variantId != 0) { found = (variantId == saleLineItem.Dimension.DistinctProductVariantId); } // Check for a product or product master being put on loyalty else if (productId != 0) { found = (productId == saleLineItem.ProductId); } // Check for a category being put on loyalty else if (categoryId != 0) { found = itemData.ProductInCategory(saleLineItem.ProductId, saleLineItem.Dimension.DistinctProductVariantId, categoryId); } } if (found) { totalQty += saleLineItem.UnitQtyConversion.Convert(saleLineItem.Quantity); totalAmount += saleLineItem.NetAmount; } } } //when check limit, we use absolute value, as in return transaction, qty and amount could be nagative. if (qtyAmountLimit > 0) { if (baseType == CalculationTypeBase.Amounts) { decimal companyCurrencyAmount = this.Application.Services.Currency.CurrencyToCurrency( ApplicationSettings.Terminal.StoreCurrency, ApplicationSettings.Terminal.CompanyCurrency, totalAmount); //Check QtyAmountLimit only for non-tender loyalty point type. if (Math.Abs(companyCurrencyAmount) >= qtyAmountLimit || type == LoyaltyPointTypeBase.Tender) { return(companyCurrencyAmount > 0 ? Math.Floor(companyCurrencyAmount / qtyAmountLimit * points) : Math.Ceiling(companyCurrencyAmount / qtyAmountLimit * points)); } } else { if (Math.Abs(totalQty) >= qtyAmountLimit) { return(totalQty > 0 ? Math.Floor(totalQty / qtyAmountLimit * points) : Math.Ceiling(totalQty / qtyAmountLimit * points)); } } } // default return(0); } catch (Exception ex) { NetTracer.Error(ex, "Loyalty::CalculatePointsForTransactionByScheme failed for groupMemberLineRecId {0} qtyAmountLimit {1} points {2}", groupMemberLineRecId, qtyAmountLimit, points); LSRetailPosis.ApplicationExceptionHandler.HandleException(this.ToString(), ex); throw; } }