Esempio n. 1
0
        public OrderViewModel(IUnityContainer container) : base(container)
        {
            //сохранить заказ
            SaveOrderCommand = new DelegateLogCommand(
                () =>
            {
                if (UnitOfWork.SaveChanges().OperationCompletedSuccessfully)
                {
                    Item.AcceptChanges();
                    _unitsWrappers.AcceptChanges();

                    var changed = _unitsWrappers.ModifiedItems.ToList();
                    if (changed.Any())
                    {
                        Container.Resolve <IEventAggregator>().GetEvent <AfterSaveOrderItemsEvent>().Publish(changed.Select(salesUnitOrderItem => salesUnitOrderItem.Model));
                    }
                }

                SaveOrderCommand.RaiseCanExecuteChanged();
            },
                () =>
            {
                //юниты валидны
                var unitsIsValid = _unitsWrappers != null && _unitsWrappers.IsValid;
                if (!unitsIsValid)
                {
                    return(false);
                }

                //заказ валиден
                var orderIsValid = Item != null && Item.IsValid && GroupsInOrder.Any();
                if (!orderIsValid)
                {
                    return(false);
                }

                //если нет плановых дат производства
                if (GroupsInOrder.SelectMany(x => x.Units).Any(x => x.EndProductionPlanDate == null))
                {
                    return(false);
                }

                //если нет позиций заказа
                if (GroupsInOrder.SelectMany(x => x.Units).Any(x => string.IsNullOrEmpty(x.OrderPosition)))
                {
                    return(false);
                }

                //что-то изменилось
                return(_unitsWrappers.IsChanged || Item.IsChanged);
            });

            RemoveOrderCommand = new DelegateLogCommand(
                () =>
            {
                var dr = Container.Resolve <IMessageService>().ShowYesNoMessageDialog("Удаление", "Вы уверены, что хотите удалить этот заказ?");
                if (dr != MessageDialogResult.Yes)
                {
                    return;
                }

                var order = Item.Model;

                _unitsWrappers.ForEach(x => x.RejectChanges());

                foreach (var groupInOrder in GroupsInOrder)
                {
                    groupInOrder.EndProductionPlanDate       = null;
                    groupInOrder.SignalToStartProductionDone = null;
                    groupInOrder.Order = null;
                    groupInOrder.Units.ForEach(x => x.OrderPosition = string.Empty);
                }

                _unitsWrappers.ForEach(salesUnitOrderItem => salesUnitOrderItem.AcceptChanges());

                if (UnitOfWork.RemoveEntity(order).OperationCompletedSuccessfully)
                {
                    Container.Resolve <IEventAggregator>().GetEvent <AfterRemoveOrderEvent>().Publish(order);
                    GoBackCommand.Execute(null);
                }
            });

            AddGroupCommand = new DelegateLogCommand(AddGroupCommand_Execute, () => GroupsPotential.SelectedItem != null);

            RemoveGroupCommand = new DelegateLogCommand(RemoveGroupCommand_Execute, () => GroupsInOrder.SelectedItem != null);

            ShowProductStructureCommand = new DelegateLogCommand(
                () =>
            {
                var salesUnit = GroupsPotential.SelectedUnit?.Model ??
                                GroupsPotential.SelectedGroup.Unit;
                var productStructureViewModel = new ProductStructureViewModel(salesUnit);
                Container.Resolve <IDialogService>().Show(productStructureViewModel, "Структура продукта");
            },
                () => GroupsPotential.SelectedItem != null);
        }