private CatalogOfferViewModel(OfferComposedId initOfferId = null)
            : base(initOfferId)
        {
            InitFields();
            NeedToCalculateDiff = true;
            DisplayName         = "Сводный прайс-лист";
            Filters             = new[] { "Все", "Основные", "Неосновные" };

            CurrentFilter.Value = Filters[0];
            CurrentRegion.Value = Consts.AllRegionLabel;

            GroupByProduct.Value = Settings.Value.GroupByProduct;
            GroupByProduct.Subscribe(_ => Offers.Value = Sort(Offers.Value));

            RetailMarkup = new NotifyValue <decimal>(true,
                                                     () => MarkupConfig.Calculate(Settings.Value.Markups, CurrentOffer.Value, User, Address),
                                                     Settings);

            RetailCost = CurrentOffer.CombineLatest(RetailMarkup, Rounding,
                                                    (o, m, r) => Round(NullableHelper.Round(o?.ResultCost * (1 + m / 100), 2), r))
                         .ToValue();

            CurrentOffer.Subscribe(_ => RetailMarkup.Recalculate());

            Persist(HideJunk, "HideJunk");
            Persist(GroupByProduct, "GroupByProduct");
            SessionValue(CurrentRegion, "CurrentRegion");
            SessionValue(CurrentFilter, "CurrentFilter");

            PrintMenuItems = new ObservableCollection <MenuItem>();
            IsView         = true;
        }
Пример #2
0
        public BaseOfferViewModel(OfferComposedId initOfferId = null)
        {
            Readonly         = true;
            UpdateOnActivate = false;

            this.initOfferId = initOfferId;

            LastEditOffer   = new NotifyValue <Offer>();
            Offers          = new NotifyValue <IList <Offer> >(new List <Offer>());
            CurrentProducer = new NotifyValue <Producer>(EmptyProducer);
            Producers       = new NotifyValue <List <Producer> >(new List <Producer> {
                EmptyProducer
            });
            InitFields();

            CurrentCatalog.Select(x => x?.Name?.Description != null)
            .Subscribe(CanShowDescription);
            CurrentCatalog.Select(x => x?.Name?.Mnn != null)
            .Subscribe(CanShowCatalogWithMnnFilter);
            CurrentOffer.Select(x => x?.OrderLine?.Order)
            .Subscribe(CurrentOrder);

            CurrentOffer.Subscribe(_ => {
                if (ResetAutoComment)
                {
                    AutoCommentText = CurrentOffer.Value?.OrderLine?.Comment;
                }
            });
            CurrentOffer
            .Select(_ => (List <SentOrderLine>)Cache[HistoryOrdersCacheKey(Settings.Value, CurrentOffer.Value)])
            .Subscribe(HistoryOrders);

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

            SessionValue(CanSaveFilterProducer, "CanSaveFilterProducer");
            SessionValue(CurrentFilterProducer, "CurrentFilterProducer");
        }
        protected override void OnInitialize()
        {
            base.OnInitialize();

            Bus.Listen <string>("db")
            .Where(m => m == "Reload")
            .Subscribe(_ => CatalogOffers?.Clear(), CloseCancellation.Token);

            Offers.Select(v => {
                v = v ?? new List <Offer>();
                if (IsFilterByCatalogName)
                {
                    return(v.GroupBy(g => g.GroupName)
                           .Select(g => new object[] { new GroupHeader(g.Key) }.Concat(g))
                           .SelectMany(o => o)
                           .ToList());
                }
                else
                {
                    return(v.Cast <object>().ToList());
                }
            })
            .Subscribe(DisplayItems);
            CurrentDisplayItem.OfType <Offer>().Subscribe(CurrentOffer);
            CurrentOffer.Subscribe(CurrentDisplayItem);
            //.Skip(1) - пропускаем начальные значения
            CurrentRegion.Cast <Object>().Skip(1)
            .Merge(CurrentFilter.Skip(1))
            .Merge(CurrentProducer.Skip(1))
            .Merge(HideJunk.Select(v => (object)v).Skip(1))
            .Subscribe(_ => Filter());
            DbReloadToken.SelectMany(_ => Env.RxQuery(s => {
                if (IsFilterByCatalogName)
                {
                    var catalogs = s.Query <Catalog>()
                                   .Fetch(c => c.Name)
                                   .Where(c => c.Name == filterCatalogName).ToArray();
                    var ids = catalogs.Select(c => c.Id).ToArray();

                    var result = s.Query <Offer>()
                                 .Where(o => ids.Contains(o.CatalogId))
                                 .Fetch(o => o.Price)
                                 .ToList();
                    result.Each(o => o.GroupName = catalogs.Where(c => c.Id == o.CatalogId)
                                                   .Select(c => c.FullName)
                                                   .FirstOrDefault());
                    return(result);
                }
                else
                {
                    var catalogId = filterCatalog.Id;
                    return(s.Query <Offer>().Where(o => o.CatalogId == catalogId)
                           .Fetch(o => o.Price)
                           .ToList());
                }
            })).CatchSubscribe(x => {
                CatalogOffers = x;
                UpdateFilters();
                Filter(false);
                UpdateOffers(Offers.Value);
                if (x.Count == 0)
                {
                    Manager.Warning("Нет предложений");
                    TryClose();
                }
            }, CloseCancellation);

            UpdateMaxProducers();
        }