コード例 #1
0
        public OrderLinesViewModel()
        {
            DisplayName = "Сводный заказ";

            InitFields();
            Lines             = new NotifyValue <ObservableCollection <OrderLine> >(new ObservableCollection <OrderLine>());
            SentLines         = new NotifyValue <List <SentOrderLine> >(new List <SentOrderLine>());
            IsCurrentSelected = new NotifyValue <bool>(true);
            Begin             = new NotifyValue <DateTime>(DateTime.Today.AddMonths(-3).FirstDayOfMonth());
            End             = new NotifyValue <DateTime>(DateTime.Today);
            AddressSelector = new AddressSelector(this);

            FilterItems = new List <Selectable <Tuple <string, string> > >();
            FilterItems.Add(new Selectable <Tuple <string, string> >(Tuple.Create("InFrozenOrders", "Позиции присутствуют в замороженных заказах")));
            FilterItems.Add(new Selectable <Tuple <string, string> >(Tuple.Create("IsMinCost", "Позиции по мин.ценам")));
            FilterItems.Add(new Selectable <Tuple <string, string> >(Tuple.Create("IsNotMinCost", "Позиции не по мин.ценам")));
            FilterItems.Add(new Selectable <Tuple <string, string> >(Tuple.Create("OnlyWarning", "Только позиции с корректировкой")));

            CanDelete    = CurrentLine.CombineLatest(IsCurrentSelected, (l, s) => l != null && s).ToValue();
            BeginEnabled = IsSentSelected.ToValue();
            EndEnabled   = IsSentSelected.ToValue();

            IsCurrentSelected.Subscribe(_ => NotifyOfPropertyChange(nameof(CanPrint)));
            IsCurrentSelected.Subscribe(_ => NotifyOfPropertyChange(nameof(CanExport)));

            Sum = new NotifyValue <decimal>(() => {
                if (IsCurrentSelected)
                {
                    return(Lines.Value.Sum(l => l.MixedSum));
                }
                return(SentLines.Value.Sum(l => l.MixedSum));
            }, SentLines, Lines, IsCurrentSelected);

            OrderWarning = new InlineEditWarning(UiScheduler, Manager);
            QuickSearch  = new QuickSearch <OrderLine>(UiScheduler,
                                                       s => Lines.Value.FirstOrDefault(l => l.ProductSynonym.IndexOf(s, StringComparison.CurrentCultureIgnoreCase) >= 0),
                                                       CurrentLine);
            QuickSearch2 = new QuickSearch <SentOrderLine>(UiScheduler,
                                                           s => SentLines.Value.FirstOrDefault(l => l.ProductSynonym.IndexOf(s, StringComparison.CurrentCultureIgnoreCase) >= 0),
                                                           SelectedSentLine);
            Editor = new Editor(OrderWarning, Manager, CurrentLine, Lines.Cast <IList>().ToValue());

            var currentLinesChanged = this.ObservableForProperty(m => m.CurrentLine.Value.Count)
                                      .Throttle(Consts.RefreshOrderStatTimeout, UiScheduler)
                                      .Select(e => new Stat(Address));

            OnCloseDisposable.Add(Bus.RegisterMessageSource(currentLinesChanged));
            OnCloseDisposable.Add(currentLinesChanged.Subscribe(_ => Sum.Recalculate()));

            Settings.Subscribe(_ => CalculateOrderLine());

            if (Session != null)
            {
                Prices = Session.Query <Price>().OrderBy(p => p.Name).ToList()
                         .Select(p => new Selectable <Price>(p))
                         .ToList();
            }
            else
            {
                Prices = new List <Selectable <Price> >();
            }

            MatchedWaybills = new MatchedWaybills(this, SelectedSentLine, IsSentSelected);
            IsCurrentSelected
            .Select(v => v ? "Lines" : "SentLines")
            .Subscribe(ExcelExporter.ActiveProperty);

            Observable.Merge(IsCurrentSelected.Select(x => (object)x), Lines, SentLines, currentLinesChanged)
            .Select(_ => {
                if (IsCurrentSelected)
                {
                    return(Lines.Value?.Count ?? 0);
                }
                return(SentLines.Value?.Count ?? 0);
            })
            .Subscribe(LinesCount);
            IsLoading = new NotifyValue <bool>();
            IsCurrentSelected.Where(v => v)
            .Select(_ => false)
            .Subscribe(IsLoading);

            SessionValue(Begin, GetType().Name + ".Begin");
            SessionValue(End, GetType().Name + ".End");
            SessionValue(IsSentSelected, GetType().Name + ".IsSentSelected");
            Persist(IsExpanded, "IsExpanded");

            PrintMenuItems = new ObservableCollection <MenuItem>();
            IsView         = true;
        }