Exemplo n.º 1
0
        public async Task GetCustomerPrice(DCOrderLineClient 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.Discount = rec._Discount;
                }
                if (rec._Unit != 0)
                {
                    orderLine._Unit = rec._Unit;
                    orderLine.NotifyPropertyChanged("Unit");
                }

                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.Price = 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;
            }
        }
Exemplo n.º 2
0
        Task _SetPriceFromItem(DCOrderLineClient orderLine, InvItem item, double LineMarginRatio, double LineSalesCharge)
        {
            Task t             = null;
            byte priceCurrency = item._Currency1;
            var  price         = item._SalesPrice1;
            var  costprice     = item._CostPrice;
            var  unit          = item._SalesUnit;

            var MarginRatio = this.MarginRatio;

            if (LineMarginRatio > 0d)
            {
                MarginRatio = LineMarginRatio;
            }

            if (orderLine.__DCType() == 2)
            {
                unit = item._PurchaseUnit;
                if (item._PurchasePrice != 0d)
                {
                    price         = item._PurchasePrice;
                    priceCurrency = item._PurchaseCurrency;
                }
                else
                {
                    price         = costprice;
                    priceCurrency = 0;
                }
            }
            else if (MarginRatio > 0d && MarginRatio < 100d)
            {
                priceCurrency = 0;
                var margin = costprice * MarginRatio / (100d - MarginRatio);
                price = Math.Round(costprice + margin, RoundMargin ? 0 : 2) + SalesCharge + LineSalesCharge;
            }
            else
            {
                var pg = this.PriceGrp;
                if (pg == 0)
                {
                    if (OrderCurrency == item._Currency2)
                    {
                        pg = 2;
                    }
                    else if (OrderCurrency == item._Currency3)
                    {
                        pg = 3;
                    }
                }
                if (pg == 2)
                {
                    priceCurrency = item._Currency2;
                    price         = item._SalesPrice2;
                }
                else if (pg == 3)
                {
                    priceCurrency = item._Currency3;
                    price         = item._SalesPrice3;
                }
            }

            if (OrderCurrency != priceCurrency)
            {
                if (priceCurrency == 0)
                {
                    priceCurrency = CompCurrency;
                }

                if (OrderCurrency != priceCurrency)
                {
                    if (priceCurrency == CompCurrency)
                    {
                        price = (ExchangeRate == 0d) ? price : Math.Round(price * ExchangeRate, 2);
                    }
                    else
                    {
                        t     = LookRate(orderLine, price, priceCurrency, OrderCurrency);
                        price = 0;
                    }
                }
            }
            if (orderLine.__DCType() != 2)
            {
                orderLine.SetCostFromItem(item);
            }
            if (price != 0)
            {
                orderLine.Price = price;
            }
            if (unit != 0)
            {
                orderLine._Unit = unit;
                orderLine.NotifyPropertyChanged("Unit");
            }
            return(t);
        }