예제 #1
0
        public OrderEditorViewModel()
        {
            OrderItems = new ReactiveList <OrderItem>();

            Save = ReactiveCommand.Create <int>(() =>
            {
                var result = SaveImpl();
                return(result);
            }, this.WhenAnyValue(p => p.EditingItem.IsClosed).Select(p => p == false));
            Save.ThrownExceptions.Subscribe(async ex => await Interactions.ShowError(ex.Message));

            CarSelectorViewModel = new CarSelectorViewModel();
            CarSelectorViewModel.Select.Subscribe(car =>
            {
                SelectedCar = DbService.GetDataContext().GetTable <Car>()
                              .LoadWith(p => p.CarModel)
                              .FirstOrDefault(p => p.ID == car.ID);
            });
            SelectCar = ReactiveCommand.Create(() =>
            {
                CarSelectorViewModel.Init(SelectedCar?.ID ?? -1);
                return(CarSelectorViewModel);
            });
            ClearCar = ReactiveCommand.Create(() =>
            {
                SelectedCar = null;
            });
            this.WhenAnyValue(p => p.SelectedCar)
            .Subscribe(car =>
            {
                if (car != null)
                {
                    SelectedClient = DbService.GetDataContext().GetTable <Client>().FirstOrDefault(p => p.ID == car.ClientID);
                    CarCaption     = $"{car.FedCode}, {car.CarModel?.Caption}";
                }
                else
                {
                    SelectedClient = null;
                    CarCaption     = String.Empty;
                }
            });

            ClientSelectorViewModel = new ClientSelectorViewModel();
            ClientSelectorViewModel.Select.Subscribe(client =>
            {
                SelectedClient = DbService.GetDataContext().GetTable <Client>().FirstOrDefault(p => p.ID == client.ID);
            });
            SelectClient = ReactiveCommand.Create(() =>
            {
                ClientSelectorViewModel.Init(SelectedClient?.ID ?? -1);
                return(ClientSelectorViewModel);
            });
            ClearClient = ReactiveCommand.Create(() =>
            {
                SelectedClient = null;
            });
            this.WhenAnyValue(p => p.SelectedClient)
            .Subscribe(client =>
            {
                if (client != null)
                {
                    SelectedDiscountlist = Discountlists.FirstOrDefault(p => p.ID == client.DiscountlistID);
                    ClientCaption        = $"{client.Caption}, {client.Card}";
                }
                else
                {
                    SelectedDiscountlist = null;
                    ClientCaption        = String.Empty;
                }
            });

            this.WhenAnyValue(p => p.SelectedPricelist)
            .Subscribe(pricelist =>
            {
                if (pricelist == null)
                {
                    PricelistItems = new List <PricelistItem>();
                }
                else
                {
                    PricelistItems = DbService.GetDataContext().GetTable <PricelistItem>()
                                     .LoadWith(p => p.Service)
                                     .Where(p => p.PricelistID == pricelist.ID)
                                     .Where(p => p.Service.EntityState == EntityStateEnum.Active)
                                     .OrderBy(p => p.Service.Caption)
                                     .ToList();
                }
            });

            AddOrderItem = ReactiveCommand.Create(() =>
            {
                if (SelectedPricelistItem == null)
                {
                    return;
                }
                var orderItem = OrderItems.FirstOrDefault(
                    p => p.OrderID == EditingItem.ID &&
                    p.ServiceID == SelectedPricelistItem.ServiceID &&
                    p.Price == SelectedPricelistItem.Price);
                if (orderItem == null)
                {
                    orderItem = new OrderItem()
                    {
                        OrderID   = EditingItem.ID,
                        ServiceID = SelectedPricelistItem.ServiceID,
                        Service   = DbService.GetDataContext().GetTable <Service>().FirstOrDefault(p => p.ID == SelectedPricelistItem.ServiceID),
                        Price     = SelectedPricelistItem.Price.Value,
                        Quantity  = 1,
                        LastPrice = 0
                    };

                    if (SelectedDiscountlist != null)
                    {
                        var discountlistItem = DbService.GetDataContext().GetTable <DiscountlistItem>()
                                               .Where(p => p.DiscountlistID == SelectedDiscountlist.ID)
                                               .FirstOrDefault(p => p.ServiceID == orderItem.ServiceID);
                        if (discountlistItem != null)
                        {
                            orderItem.Discount = discountlistItem.Discount.Value;
                        }
                    }
                    orderItem.CalcLastPrice();
                    OrderItems.Add(orderItem);
                }
                else
                {
                    orderItem.Quantity++;
                }

                SelectedOrderItem = orderItem;
                calcCost();
            });
            AddOrderItem.ThrownExceptions.Subscribe(async ex => await Interactions.ShowError(ex.Message));

            RemoveOrderItem = ReactiveCommand.Create(() =>
            {
                if (SelectedOrderItem == null)
                {
                    return;
                }
                if (SelectedOrderItem.Quantity > 1)
                {
                    SelectedOrderItem.Quantity--;
                }
                else
                {
                    OrderItems.Remove(SelectedOrderItem);
                }
                calcCost();
            });
            RemoveOrderItem.ThrownExceptions.Subscribe(async ex => await Interactions.ShowError(ex.Message));

            DeleteOrderItem = ReactiveCommand.Create(() =>
            {
                if (SelectedOrderItem == null)
                {
                    return;
                }
                OrderItems.Remove(SelectedOrderItem);
                calcCost();
            });
            DeleteOrderItem.ThrownExceptions.Subscribe(async ex => await Interactions.ShowError(ex.Message));

            ApplyDiscount = ReactiveCommand.Create(() =>
            {
                if (SelectedDiscountlist == null)
                {
                    OrderItems.ForEach(orderItem =>
                    {
                        orderItem.Discount = 0;
                        orderItem.CalcLastPrice();
                    });
                }
                else
                {
                    var discountlistItems = DbService.GetDataContext().GetTable <DiscountlistItem>()
                                            .Where(p => p.DiscountlistID == SelectedDiscountlist.ID)
                                            .ToList();
                    OrderItems.ForEach(orderItem =>
                    {
                        var discountlistItem = discountlistItems
                                               .FirstOrDefault(q => q.ServiceID == orderItem.ServiceID);
                        if (discountlistItem != null)
                        {
                            orderItem.Discount = discountlistItem.Discount.Value;
                        }
                        else
                        {
                            orderItem.Discount = 0;
                        }
                        orderItem.CalcLastPrice();
                    });
                }
                calcCost();
            });
            ApplyDiscount.ThrownExceptions.Subscribe(async ex => await Interactions.ShowError(ex.Message));
        }