Esempio n. 1
0
        int search(InvPriceListLine searchrec)
        {
            var pos = Array.BinarySearch(CustomerPrices, searchrec, priceSort);

            if (pos < 0)
            {
                return(~pos);
            }
            else
            {
                return(pos);
            }
        }
Esempio n. 2
0
        public async Task GetCustomerPrice(ProjectJournalLineClient orderLine, bool IsNewItem)
        {
            if (!this.UseCustomerPrices)
            {
                return;
            }

            var t = LoadingTask;

            if (t != null && !t.IsCompleted)
            {
                await t;
            }

            if (this._PriceList == null || orderLine._Item == null)
            {
                return;
            }
            var itemRec = (InvItem)items.Get(orderLine._Item);

            if (itemRec == null)
            {
                return;
            }
            orderLine._Qty = Math.Round(orderLine._Qty, itemRec._Decimals);

            if (CustomerPrices == null)
            {
                await loadPriceList();
            }
            if (CustomerPrices == null)
            {
                return;
            }

            var len       = CustomerPrices.Length;
            var searchrec = new InvPriceListLine();

            searchrec._DCType = debtor.__DCType();
            searchrec._Item   = orderLine._Item;
            // Do not include Variant, since we also want to finde prices without variant
            //searchrec._Variant1 = orderLine._Variant1;
            //searchrec._Variant2 = orderLine._Variant2;
            searchrec._prioritet = int.MinValue;
            var pos = search(searchrec);

            var              now             = OrderTime;
            int              FoundPriority   = -1;
            bool             HasQtyPrices    = false;
            int              CurVariantMatch = -1;
            InvPriceListLine rec             = null;
            var              qty             = Math.Abs(orderLine._Qty) + 0.00000000001d;

            while (pos < len)
            {
                var r = CustomerPrices[pos++];
                var c = string.Compare(r._Item, orderLine._Item);
                if (c != 0)
                {
                    break;
                }
                if ((r._ValidFrom != DateTime.MinValue && now < r._ValidFrom) ||
                    (r._ValidTo != DateTime.MinValue && now > r._ValidTo))
                {
                    continue;
                }
                int VariantMatch = VariantCompare(orderLine._Variant1, r._Variant1);
                if (VariantMatch >= 0)
                {
                    int VariantMatch2 = VariantCompare(orderLine._Variant2, r._Variant2);
                    if (VariantMatch2 >= 0)
                    {
                        VariantMatch += VariantMatch2;
                        if (VariantMatch >= CurVariantMatch)
                        {
                            if (r._Qty != 0d)
                            {
                                HasQtyPrices = true;
                            }
                            if (r._Qty <= qty)
                            {
                                bool UseMatch = true;
                                if (r._FirstMatch)
                                {
                                    if (FoundPriority == -1)
                                    {
                                        FoundPriority = r._prioritet;
                                    }
                                    else if (FoundPriority != r._prioritet)
                                    {
                                        UseMatch = false;
                                    }
                                }
                                if (UseMatch)
                                {
                                    rec             = r;
                                    CurVariantMatch = VariantMatch;
                                }
                            }
                        }
                    }
                }
            }

            CurVariantMatch     = 0;
            searchrec._Item     = null;
            searchrec._Variant1 = null;
            searchrec._Variant2 = null;

            if (rec == null && itemRec._DiscountGroup != null)
            {
                var discountgroup = itemRec._DiscountGroup;
                searchrec._DiscountGroup = discountgroup;
                pos = search(searchrec);
                while (pos < len)
                {
                    var r = CustomerPrices[pos++];
                    int c = string.Compare(r._DiscountGroup, discountgroup);
                    if (c != 0)
                    {
                        break;
                    }
                    if ((r._ValidFrom != DateTime.MinValue && now < r._ValidFrom) ||
                        (r._ValidTo != DateTime.MinValue && now > r._ValidTo))
                    {
                        continue;
                    }
                    if (r._Qty != 0d)
                    {
                        HasQtyPrices = true;
                    }
                    if (r._Qty <= qty)
                    {
                        rec = r;
                    }
                }
            }

            if (rec == null && itemRec._Group != null)
            {
                var group = itemRec._Group;
                searchrec._ItemGroup     = group;
                searchrec._DiscountGroup = null;
                pos = search(searchrec);
                while (pos < len)
                {
                    var r = CustomerPrices[pos++];
                    int c = string.Compare(r._ItemGroup, group);
                    if (c != 0)
                    {
                        break;
                    }
                    if ((r._ValidFrom != DateTime.MinValue && now < r._ValidFrom) ||
                        (r._ValidTo != DateTime.MinValue && now > r._ValidTo))
                    {
                        continue;
                    }
                    if (r._Qty != 0d)
                    {
                        HasQtyPrices = true;
                    }
                    if (r._Qty <= qty)
                    {
                        rec = r;
                    }
                }
            }

            if (rec != null && (IsNewItem || HasQtyPrices || CurVariantMatch >= 0))
            {
                if (rec._Pct != 0d)
                {
                    orderLine.DiscountPct = rec._Pct;
                }
                if (rec._Discount != 0d && orderLine._SalesPrice >= rec._Discount)
                {
                    orderLine.SalesPrice = orderLine._SalesPrice - rec._Discount;
                }

                if (rec._Price != 0d)
                {
                    var price = rec._Price;
                    if (OrderCurrency != PriceListCurrency)
                    {
                        if (PriceListCurrency == CompCurrency)
                        {
                            price = (ExchangeRate == 0d) ? price : Math.Round(price * ExchangeRate, 2);
                        }
                        else
                        {
                            await LookRate(orderLine, price, PriceListCurrency, OrderCurrency);

                            return;
                        }
                    }
                    orderLine.SalesPrice = price;
                }

                if (!rec._FixedContributionRate)
                {
                    return;
                }
            }

            var task = _SetPriceFromItem(orderLine, (InvItem)items.Get(orderLine._Item), rec != null ? rec._ContributionRate : 0d, rec != null ? rec._SalesCharge : 0d);

            if (task != null)
            {
                await task;
            }
        }