/// <summary> /// Creates a ViewModel for the given ProcessReport with given row and column /// </summary> /// <param name="model">if null, it automatically assign unreported process space</param> public ProcessReportVm(Model.ProcessReport model, Dal.SoheilEdmContext uow) { Model = model; Message = new EmbeddedException(); try { ActivityName = model.Process.StateStationActivity.Activity.Name; ProductName = model.Process.StateStationActivity.StateStation.State.FPC.Product.Name; ProductColor = model.Process.StateStationActivity.StateStation.State.FPC.Product.Color; } catch { } //uow UOW = uow; _processReportDataService = new DataServices.ProcessReportDataService(UOW); _taskReportDataService = new DataServices.TaskReportDataService(UOW); //properties //Model.ProducedG1 = Model.OperatorProcessReports.Sum(x => x.OperatorProducedG1);//??? can be different than sum ProducedG1 = Model.ProducedG1; Timing = new TimingSet(this); Timing.Saved += () => Save(); Timing.DurationChanged += v => Model.DurationSeconds = v; Timing.StartChanged += v => Model.StartDateTime = v; Timing.EndChanged += v => Model.EndDateTime = v; Timing.TargetPointChanged += tp => { TargetPointForOperator = Model.OperatorProcessReports.Any() ? string.Format("{0:F2}", (float)tp / Model.OperatorProcessReports.Count) : "---"; Model.ProcessReportTargetPoint = tp; updateEmptyCount(tp: tp); }; TargetPointForOperator = Model.OperatorProcessReports.Any() ? string.Format("{0:F2}", (float)Model.ProcessReportTargetPoint / Model.OperatorProcessReports.Count) : "---"; //reports OperatorReports = new OperatorReportCollection(this); DefectionReports = new DefectionReportCollection(this); DefectionCount = (int)Model.DefectionReports.Sum(x => x.CountEquivalence); StoppageReports = new StoppageReportCollection(this); StoppageCount = (int)Model.StoppageReports.Sum(x => x.CountEquivalence); IsUserDrag = false; _isInInitializingPhase = false; initializeCommands(); }
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; } }); }