/// <summary>
            /// Find and total all multiline discount trade agreements that match the given relations and quantity.
            /// </summary>
            /// <param name="tradeAgreements">Trade agreement collection to calculate on.</param>
            /// <param name="itemCode">The item code to search by (item group or all).</param>
            /// <param name="retailTransaction">The transaction context with Id and customer Id.</param>
            /// <param name="priceGroup">Multiline price group.</param>
            /// <param name="salesQuantity">Aggregated quantity for multiline price group.</param>
            /// <param name="percent1">Percentage one.</param>
            /// <param name="percent2">Percentage two.</param>
            /// <param name="discountAmount">Discount amount.</param>
            private void GetMultiLineDiscountLine(
                List <TradeAgreement> tradeAgreements,
                PriceDiscountItemCode itemCode,
                SalesTransaction retailTransaction,
                string priceGroup,
                decimal salesQuantity,
                out decimal percent1,
                out decimal percent2,
                out decimal discountAmount)
            {
                PriceDiscountType relation  = PriceDiscountType.MultilineDiscountSales; // Sales multiline discount - 6
                ProductVariant    dimension = new ProductVariant();

                percent1       = decimal.Zero;
                percent2       = decimal.Zero;
                discountAmount = decimal.Zero;

                bool searchAgain = true;
                var  codes       = new PriceDiscountAccountCode[] { PriceDiscountAccountCode.Customer, PriceDiscountAccountCode.CustomerGroup, PriceDiscountAccountCode.AllCustomers };

                foreach (var accountCode in codes)
                {
                    // skip to next configuration if this one isn't enabled
                    if (!this.discountParameters.Activation(relation, accountCode, itemCode))
                    {
                        continue;
                    }

                    // get item relation based on item code
                    string itemRelation = (itemCode == PriceDiscountItemCode.ItemGroup) ? priceGroup : string.Empty;
                    itemRelation = itemRelation ?? string.Empty;

                    // get customer relation based on account code
                    string accountRelation = string.Empty;
                    if (accountCode == PriceDiscountAccountCode.Customer)
                    {
                        accountRelation = retailTransaction.CustomerId;
                    }
                    else if (accountCode == PriceDiscountAccountCode.CustomerGroup)
                    {
                        accountRelation = this.priceContext.CustomerMultipleLinePriceGroup;
                    }

                    accountRelation = accountRelation ?? string.Empty;

                    // if both relations are valid for the given item and account codes, look for trade agreements matching these relations
                    if (DiscountParameters.ValidRelation(accountCode, accountRelation) &&
                        DiscountParameters.ValidRelation(itemCode, itemRelation))
                    {
                        // get any active multiline discount trade agreement matching relations and quantity
                        var priceDiscTable = Discount.GetPriceDiscData(tradeAgreements, relation, itemRelation, accountRelation, itemCode, accountCode, salesQuantity, this.priceContext, dimension, false);

                        // compute running sum of discount values found
                        foreach (TradeAgreement row in priceDiscTable)
                        {
                            percent1       += row.PercentOne;
                            percent2       += row.PercentTwo;
                            discountAmount += row.Amount;

                            // stop search when we find a trade agreement set to not find next trade agreement
                            if (!row.ShouldSearchAgain)
                            {
                                searchAgain = false;
                            }
                        }
                    }

                    // stop search if we found a discount without "find next" marked
                    if (!searchAgain)
                    {
                        break;
                    }
                }
            }
Exemple #2
0
            private void GetLineDiscountLines(
                List <TradeAgreement> tradeAgreements,
                SalesLine saleItem,
                ref decimal absQty,
                ref decimal discountAmount,
                ref decimal percent1,
                ref decimal percent2,
                ref decimal minQty)
            {
                int idx = 0;

                while (idx < 9)
                {
                    PriceDiscountItemCode    itemCode    = (PriceDiscountItemCode)(idx % 3); // Mod divsion
                    PriceDiscountAccountCode accountCode = (PriceDiscountAccountCode)(idx / 3);

                    string accountRelation = string.Empty;
                    if (accountCode == PriceDiscountAccountCode.Customer)
                    {
                        accountRelation = this.priceContext.CustomerAccount;
                    }
                    else if (accountCode == PriceDiscountAccountCode.CustomerGroup)
                    {
                        accountRelation = this.priceContext.CustomerLinePriceGroup;
                    }

                    accountRelation = accountRelation ?? string.Empty;

                    string itemRelation;
                    if (itemCode == PriceDiscountItemCode.Item)
                    {
                        itemRelation = saleItem.ItemId;
                    }
                    else
                    {
                        Item item = PriceContextHelper.GetItem(this.priceContext, saleItem.ItemId);
                        itemRelation = item != null ? item.LineDiscountGroupId : string.Empty;
                    }

                    itemRelation = itemRelation ?? string.Empty;

                    PriceDiscountType relation = PriceDiscountType.LineDiscountSales; // Sales line discount - 5

                    if (this.discountParameters.Activation(relation, accountCode, itemCode))
                    {
                        if (DiscountParameters.ValidRelation(accountCode, accountRelation) &&
                            DiscountParameters.ValidRelation(itemCode, itemRelation))
                        {
                            bool dimensionDiscountFound = false;

                            if (saleItem.Variant != null && !string.IsNullOrEmpty(saleItem.Variant.VariantId))
                            {
                                var dimensionPriceDiscTable = Discount.GetPriceDiscData(tradeAgreements, relation, itemRelation, accountRelation, itemCode, accountCode, absQty, this.priceContext, saleItem.Variant, true);

                                foreach (TradeAgreement row in dimensionPriceDiscTable)
                                {
                                    bool unitsAreUndefinedOrEqual =
                                        string.IsNullOrEmpty(row.UnitOfMeasureSymbol) ||
                                        string.Equals(row.UnitOfMeasureSymbol, saleItem.SalesOrderUnitOfMeasure, StringComparison.OrdinalIgnoreCase);

                                    if (unitsAreUndefinedOrEqual)
                                    {
                                        percent1       += row.PercentOne;
                                        percent2       += row.PercentTwo;
                                        discountAmount += row.Amount;
                                        minQty         += row.QuantityAmountFrom;
                                    }

                                    if (percent1 > 0M || percent2 > 0M || discountAmount > 0M)
                                    {
                                        dimensionDiscountFound = true;
                                    }

                                    if (!row.ShouldSearchAgain)
                                    {
                                        idx = 9;
                                    }
                                }
                            }

                            if (!dimensionDiscountFound)
                            {
                                var priceDiscTable = Discount.GetPriceDiscData(tradeAgreements, relation, itemRelation, accountRelation, itemCode, accountCode, absQty, this.priceContext, saleItem.Variant, false);

                                foreach (TradeAgreement row in priceDiscTable)
                                {
                                    // Apply default if the unit of measure is not set from the cart.
                                    string unitOfMeasure = Discount.GetUnitOfMeasure(saleItem);

                                    bool unitsAreUndefinedOrEqual =
                                        string.IsNullOrEmpty(row.UnitOfMeasureSymbol) ||
                                        string.Equals(row.UnitOfMeasureSymbol, unitOfMeasure, StringComparison.OrdinalIgnoreCase);

                                    if (unitsAreUndefinedOrEqual)
                                    {
                                        percent1       += row.PercentOne;
                                        percent2       += row.PercentTwo;
                                        discountAmount += row.Amount;
                                        minQty         += row.QuantityAmountFrom;
                                    }

                                    if (!row.ShouldSearchAgain)
                                    {
                                        idx = 9;
                                    }
                                }
                            }
                        }
                    }

                    idx++;
                }
            }