public OrdersViewModel() { var fab = SQL.GetFabricator(); var mod = SQL.GetModel(); var cli = SQL.GetClientses(); var states = SQL.GetState(); OrdersEntries = new CollectionView(SQL.GetOrders().Select(o => { var client = cli.FirstOrDefault(c => c.IdClient == o.IdClient); return(new Order() { Id = o.IdOrder, State = states.First(f => f.Id == o.IdState).State, Number = o.Number ?? "", NameClient = client != null? client.Name: "", PhoneClient = client != null ? client.Phone : "", Model = String.Format("{0} {1}", fab.First(f => f.IdFabricator == o.IdFabricator).Fabricator, mod.First(m => m.IdModel == o.IdModel).Model), DateReceipt = o.DateReceipt.Date, AvailabilityDate = o.AvailabilityDate.Date, NameExecutor = SQL.GetExecutors(o.IdExecutor), EditOrder = new SimpleCommand((_) => ShowWin.ShowWork(o)) }); })); EditOrder = new SimpleCommand(() => { if (OrdersEntries.CurrentItem != null) { var o = SQL.GetOrders().FirstOrDefault(f => f.IdOrder == (OrdersEntries.CurrentItem as Order).Id); if (o.IdState == 1) { o.IdState = 2; SQL.Update(o); } ShowWin.ShowWork(o); } }); NewOrder = new SimpleCommand(() => ShowWin.ShowNewOrder(null)); LoadFilterEntries(); }
public NewOrderViewModel(Orders orders = null) { _orders = orders; AddOrder = new SimpleCommand(Create); EditOrder = new SimpleCommand(() => ShowWin.ShowNewOrder(_orders)); Back = new SimpleCommand(() => { if (orders == null) { ShowWin.ShowOrders(); } else { ShowWin.ShowWork(orders); } }); SetState = new SimpleCommand((state) => { _orders.IdState = Int32.Parse(state.ToString()); SQL.Update(_orders); State = _orders == null ? "" : SQL.GetState().First(w => w.Id == _orders.IdState).State; }); OpenOrders = new SimpleCommand((_) => ShowWin.ShowOrders()); if (orders != null) { Number = orders.Number; Malfunction = orders.Malfunction; Appearance = orders.Appearance; Equipment = orders.Equipment; NotesOnAdmission = orders.NotesOnAdmission; SerialNumber = orders.SerialNumber; Fabricator = SQL.GetFabricator(orders.IdFabricator); Model = SQL.GetModel(orders.IdModel); AvailabilityDate = orders.AvailabilityDate; EstimatedPrice = orders.EstimatedPrice; Prepayment = orders.Prepayment; TypesDevices = SQL.GetTypesDevices(orders.IdTypeDevice); Name = SQL.GetClientses(orders.IdClient).Name; Phone = SQL.GetClientses(orders.IdClient).Phone; Email = SQL.GetClientses(orders.IdClient).Email; Address = SQL.GetClientses(orders.IdClient).Address; DateReceipt = orders.DateReceipt; } else { var firstOrDefault = SQL.GetOrders().OrderByDescending(o => o.Number).FirstOrDefault(); if (firstOrDefault != null) { var n = firstOrDefault.Number; int res; if (Int32.TryParse(n, out res)) { Number = (res + 1).ToString(); } } } ClientNameEntries = new CollectionView(SQL.GetClientses().Select(c => c.Name + " " + c.Phone)); var e = new List <string> { "" }; e.AddRange(SQL.GetExecutors().Select(s => s.Name)); ExecutorsEntries = new CollectionView(e); State = _orders == null ? "" : SQL.GetState().First(w => w.Id == _orders.IdState).State; DeleteOrder = new SimpleCommand(() => { var res = MessageBox.Show("Вы действительно хотите удалить заказ", "Удалить заказ", MessageBoxButton.OKCancel, MessageBoxImage.Warning); if (res == MessageBoxResult.OK) { SQL.GetHistory().Where(w => w.IdOrder == _orders.IdOrder).ToList().ForEach(f => SQL.Delete(f)); SQL.GetWork().Where(w => w.IdOrder == _orders.IdOrder).ToList().ForEach(f => SQL.Delete(f)); SQL.Delete(_orders); ShowWin.ShowOrders(); } }); }