public DefectionReportCollection(ProcessReportVm parent) { Parent = parent; AddCommand = new Commands.Command(o => { var model = new Model.DefectionReport { ProcessReport = parent.Model, LostCount = 0, LostTime = 0, ProductDefection = null, ModifiedBy = LoginInfo.Id, }; foreach (var po in Parent.Model.Process.ProcessOperators) { model.OperatorDefectionReports.Add(new Model.OperatorDefectionReport { Operator = po.Operator, DefectionReport = model, Code = po.Operator.Code, ModifiedBy = LoginInfo.Id, }); } parent.Model.DefectionReports.Add(model); var vm = new DefectionReportVm(this, model); List.Add(vm); }); }
public DefectionReportVm(DefectionReportCollection parent, Model.DefectionReport model) { Model = model; Index = parent.Parent.DefectionReports.List.Count + 1; Parent = parent; IsG2 = model.IsG2; ProductDefection = FilterBoxVm.CreateForProductDefections( model.ProductDefection == null ? -1 : model.ProductDefection.Id, model.ProcessReport.Process.StateStationActivity.StateStation.State.FPC.Product.Id); var pdrepo = new Dal.Repository<Model.ProductDefection>(Parent.Parent.UOW); ProductDefection.FilterableItemSelected += (s, old, v) => Model.ProductDefection = pdrepo.FirstOrDefault(x => x.Id == v.Id); if (ProductDefection.SelectedItem == null) ProductDefection.SelectedItem = ProductDefection.FilteredList.FirstOrDefault(); //create and load OperatorDefectionReports GuiltyOperators = FilterBoxVmCollection.CreateForGuiltyOperators(model.OperatorDefectionReports, Parent.Parent.UOW); var odrRepo = new Dal.Repository<Model.OperatorDefectionReport>(Parent.Parent.UOW); GuiltyOperators.OperatorSelected += (vm, oldOp, newOp) => { if (newOp.Model == null) return; if (vm.Model == null) { //create and add new ODR var odr = new Model.OperatorDefectionReport { DefectionReport = model, Operator = newOp.Model, ModifiedBy = LoginInfo.Id, }; odrRepo.Add(odr); vm.Model = odr; } else { //update existing ODR (vm.Model as Model.OperatorDefectionReport).Operator = newOp.Model; } }; GuiltyOperators.OperatorRemoved += vm => { if (vm.Model != null) { model.OperatorDefectionReports.Remove(vm.Model as Model.OperatorDefectionReport); odrRepo.Delete(vm.Model as Model.OperatorDefectionReport); } }; AffectsTaskReport = model.AffectsTaskReport; LostSeconds = model.LostTime; LostCount = model.LostCount; Description = model.Description; DeleteCommand = new Commands.Command(o => { //correct sums Parent.SumOfLostCount -= LostCount; Parent.SumOfLostTime-= LostSeconds; Parent.SumOfTimeEquivalent -= TimeEquivalent; Parent.SumOfCountEquivalent -= QuantityEquivalent; //delete Parent.List.Remove(this); Model.ProcessReport.DefectionReports.Remove(Model); new DataServices.ProcessReportDataService(Parent.Parent.UOW).Delete(Model); //reset indices for (int i = 0; i < Parent.List.Count; i++) { Parent.List[i].Index = i + 1; } }); }