public WaybillDetails(uint id)
        {
            DisplayName = "Детализация накладной";
            this.id     = id;
            InitFields();
            CurrentWaybillLine = CurrentLine.OfType <WaybillLine>().ToValue();

            Settings.Subscribe(_ => Calculate());
            CurrentTax.Subscribe(v => {
                if (Lines.Value == null)
                {
                    return;
                }
                if (v == null || v.Value == -1)
                {
                    Lines.Value.Filter = null;
                }
                else
                {
                    Lines.Value.Filter = o => ((WaybillLine)o).Nds == v.Value;
                }
            });
            Lines.Select(v => v == null
                                ? Observable.Empty <EventPattern <NotifyCollectionChangedEventArgs> >()
                                : v.ToCollectionChanged())
            .Switch()
            .Subscribe(e => {
                if (e.EventArgs.Action == NotifyCollectionChangedAction.Remove)
                {
                    e.EventArgs.OldItems.OfType <WaybillLine>().Each(l => Waybill.RemoveLine(l));
                }
                else if (e.EventArgs.Action == NotifyCollectionChangedAction.Add)
                {
                    e.EventArgs.NewItems.OfType <WaybillLine>().Each(l => Waybill.AddLine(l));
                }
            });
            OrderLines = CurrentWaybillLine
                         .Throttle(Consts.ScrollLoadTimeout, UiScheduler)
                         .Select(v => {
                if (v == null)
                {
                    return(new List <SentOrderLine>());
                }
                var lineId       = v.Id;
                var orderLineIds = Session.Query <WaybillOrder>().Where(l => l.DocumentLineId == lineId)
                                   .Select(l => (uint?)l.OrderLineId)
                                   .ToArray();
                var line = Session.Query <SentOrderLine>().FirstOrDefault(l => orderLineIds.Contains(l.ServerId));
                if (line != null)
                {
                    CurrentOrderLine.Value = line;
                    line.Order.Lines.Each(l => l.Configure(User));
                    return(line.Order.Lines.OrderBy(l => l.ProductSynonym).ToList());
                }
                return(new List <SentOrderLine>());
            })
                         .ToValue(CloseCancellation);
            EmptyLabelVisibility = OrderLines
                                   .Select(v => v == null || v.Count == 0 ? Visibility.Visible : Visibility.Collapsed)
                                   .ToValue();
            OrderDetailsVisibility = EmptyLabelVisibility
                                     .Select(v => v == Visibility.Collapsed ? Visibility.Visible :  Visibility.Collapsed)
                                     .ToValue();
        }
        protected override void OnInitialize()
        {
            base.OnInitialize();

            OnCloseDisposable.Add(Shell.SpecialMarkupProducts.Subscribe(_ => Calculate()));

            if (Session == null)
            {
                return;
            }

            Waybill = Session.Get <Waybill>(id);
            if (Waybill == null)
            {
                return;
            }
            Waybill.Settings = Settings;
            Waybill.ObservableForProperty(x => x.Status, skipInitial: false)
            .Select(x => x.Value == DocStatus.NotPosted).Subscribe(CanStock);
            Waybill.ObservableForProperty(x => x.Status, skipInitial: false)
            .Select(x => x.Value == DocStatus.NotPosted && Waybill.SupplierSum == null).Subscribe(CanEditSum);

            Waybill.ObservableForProperty(m => (object)m.Status, skipInitial: false)
            .Merge(Waybill.ObservableForProperty(m => (object)m.IsCreatedByUser))
            .Select(_ => !User.IsStockEnabled && Waybill.Status == DocStatus.NotPosted && !Waybill.IsCreatedByUser)
            .Subscribe(CanToEditable);

            Waybill.ObservableForProperty(m => (object)m.Status, skipInitial: false)
            .Merge(Waybill.ObservableForProperty(m => (object)m.IsCreatedByUser))
            .Select(_ => Waybill.Status == DocStatus.NotPosted && Waybill.IsCreatedByUser)
            .Subscribe(CanAddFromCatalog);

            if (Waybill.IsNew)
            {
                Waybill.IsNew = false;
                if (Shell.NewDocsCount.Value > 0)
                {
                    Shell.NewDocsCount.Value--;
                }
            }

            Calculate();

            var stockids = Waybill.Lines.Where(x => x.StockId != null).Select(x => x.StockId).ToArray();

            RxQuery(s => s.Query <Stock>().Where(x => stockids.Contains(x.ServerId)).ToDictionary(x => x.ServerId))
            .ObserveOn(UiScheduler)
            .Subscribe(x => {
                Waybill.Lines.Each(y => {
                    y.Stock = x.GetValueOrDefault(y.StockId);
                });
            });

            Lines.Value = new ListCollectionView(Waybill.Lines.OrderBy(l => l.Product).ToList());
            Taxes       = new List <ValueDescription <int?> > {
                new ValueDescription <int?>("Все", -1),
            };
            Taxes.AddRange(Lines.Value.Cast <WaybillLine>().Select(l => l.Nds).Distinct()
                           .OrderBy(t => t)
                           .Select(t => new ValueDescription <int?>(((object)t ?? "Нет значения").ToString(), t)));
            CurrentTax.Value = Taxes.FirstOrDefault();
            Reject           = CurrentWaybillLine
                               .Throttle(Consts.ScrollLoadTimeout, Scheduler)
                               .Select(v => RxQuery(s => {
                if (v?.RejectId == null)
                {
                    return(null);
                }
                return(s.Get <Reject>(v.RejectId.Value));
            }))
                               .Switch()
                               .ToValue(CloseCancellation);
            IsRejectVisible = Reject.Select(r => r != null).ToValue();
            if (Waybill.IsCreatedByUser)
            {
                EmptyLabelVisibility   = EmptyLabelVisibility.Select(s => Visibility.Collapsed).ToValue();
                OrderDetailsVisibility = OrderDetailsVisibility.Select(s => Visibility.Collapsed).ToValue();
            }

            RxQuery(s => PriceTag.LoadOrDefault(s.Connection, TagType.PriceTag, Waybill.Address))
            .Subscribe(x => priceTag = x);
            RxQuery(s => PriceTag.LoadOrDefault(s.Connection, TagType.RackingMap, Waybill.Address))
            .Subscribe(x => rackingMap = x);
        }