コード例 #1
0
        public OrderViewModel()
        {
            Title = "Bán hàng";
            // commands
            SelectedCategory = ListCategory[0];
            CurrentOrder     = new Order(this);

            LoadDrinkByCategory = new RelayCommand <Category>((category) => { return(category != SelectedCategory); }, (category) =>
            {
                SelectedCategory = category;
            });

            AddDrink = new RelayCommand <Drink>((drink) => { return(true); }, (drink) =>
            {
                OrderItem ItemOfCurrentDrink = DrinkService.FindDrink(CurrentOrder, drink);

                // only increase amount if item already exist and not having topping or option yet
                // TODO: refactor
                if (ItemOfCurrentDrink != null && !ItemOfCurrentDrink.HasToppings() /* && !ItemOfCurrentDrink.HasOptions()*/)
                {
                    OrderService.SetItemAmount(CurrentOrder, ItemOfCurrentDrink, ItemOfCurrentDrink.Number + 1);
                    return;
                }
                OrderService.AddItem(CurrentOrder, new OrderItem(drink));
            });

            ToggleToppingForDrink = new RelayCommand <Topping>((drink) => { return(true); }, (drink) =>
            {
                if (drink.Checked)
                {
                    OrderService.AddTopping(CurrentOrder, drink, SelectedOrderItem);
                }
                else
                {
                    OrderService.RemoveTopping(CurrentOrder, drink, SelectedOrderItem);
                }
            });
            ToggleOptionForDrink = new RelayCommand <Option>((drink) => { return(true); }, (drink) =>
            {
                //MessageBox.Show(drink.Item.ID);
                //OrderService.SelectOption(CurrentOrder, drink, SelectedOrderItem);
            });

            IncreaseAmount = new RelayCommand <OrderItem>((drink) => { return(true); }, (OrderItem) =>
            {
                OrderService.SetItemAmount(CurrentOrder, OrderItem, OrderItem.Number + 1);
            });

            DecreaseAmount = new RelayCommand <OrderItem>((drink) => { return(drink.Number > 1); }, (OrderItem) =>
            {
                OrderService.SetItemAmount(CurrentOrder, OrderItem, OrderItem.Number - 1);
            });

            RemoveDrink = new RelayCommand <OrderItem>((drink) => { return(true); }, (orderitem) =>
            {
                OrderService.RemoveItem(CurrentOrder, orderitem);
            });

            ClearOrder = new RelayCommand <OrderItem>((drink) => { return(!OrderService.IsEmpty(CurrentOrder)); }, (orderitem) =>
            {
                OrderService.RemoveAllItems(CurrentOrder);
            });

            CheckoutOrder = new RelayCommand <OrderItem>((drink) => { return(!OrderService.IsEmpty(CurrentOrder)); }, (orderitem) =>
            {
                OrderService.AddOrder(CurrentOrder);
                CurrentOrder.OnPropertyChanged(null);
                CurrentOrder = new Order();
            });

            ShowAddCouponDialog = new RelayCommand <object>((p) => { return(!OrderService.IsEmpty(CurrentOrder)); }, (p) =>
            {
                IsAddCouponDialogOpen = true;
            });

            HideAddCouponDialog = new RelayCommand <object>((p) => { return(true); }, (p) =>
            {
                OrderService.RemoveCoupon(CurrentOrder);
                IsAddCouponDialogOpen = false;
            });

            AddCoupon = new RelayCommand <object>((p) => { return(!OrderService.HasCoupon(CurrentOrder)); }, (p) =>
            {
                if (!OrderService.AddCoupon(CurrentOrder, CouponCode))
                {
                    MessageBox.Show("Mã không hợp lệ");
                    return;
                }
                IsAddCouponDialogOpen = false;
            });

            ToggleOptionView = new RelayCommand <OrderItem>((p) => { return(true); }, (p) =>
            {
                //if (p == SelectedOrderItem)
                //{
                //    MessageBox.Show("Toggle" + SelectedOrderItem.Item.Name + " - " + p.Item.Name);
                //    OptionSideBar = (OptionSideBar == 0) ? OptionSideBarWidth : 0;
                //}
            });
        }