public static Guid InsertOrUpdate(Guid MetricID, DateTime Date, Guid OrgLocationID, bool IsFilesAttached, bool Approve, Guid?SelectedUoMID, string OldValue, string Value, bool?OldApproved, bool?Approved, Guid?UserId, string Notes, string CustomMetricAlias, string CustomMetricCode) { using (LinqMicajahDataContext dc = new LinqMicajahDataContext()) { Metric metric = (from m in dc.Metric where m.MetricID == MetricID && m.InstanceId == LinqMicajahDataContext.InstanceId && m.Status == true select m).FirstOrNull(); Guid _ActualValueID = Guid.Empty; if (metric != null) { MetricValue metricValue = (from mv in dc.MetricValue where mv.MetricID == MetricID && mv.InstanceId == LinqMicajahDataContext.InstanceId && mv.Status == true && mv.Date == Date && mv.FrequencyID == metric.FrequencyID && mv.OrgLocationID == OrgLocationID select mv).FirstOrNull(); MetricOrgLocationUoM MOUoM = (from muom in dc.MetricOrgLocationUoM where muom.MetricID == MetricID && muom.InstanceId == LinqMicajahDataContext.InstanceId && muom.OrgLocationID == OrgLocationID select muom).FirstOrNull(); if (MOUoM == null) { Bll.MetricOrgLocationUoM muom = new Bll.MetricOrgLocationUoM(); muom.MetricID = MetricID; muom.OrgLocationID = OrgLocationID; muom.InputUnitOfMeasureID = SelectedUoMID; dc.MetricOrgLocationUoM.InsertOnSubmit(muom); } else if (SelectedUoMID != MOUoM.InputUnitOfMeasureID) { MOUoM.InputUnitOfMeasureID = SelectedUoMID; // change org location uom } if (CustomMetricAlias != null && CustomMetricCode != null) { MetricOrgLocationName MOName = (from mname in dc.MetricOrgLocationName where mname.MetricID == MetricID && mname.InstanceId == LinqMicajahDataContext.InstanceId && mname.OrgLocationID == OrgLocationID select mname).FirstOrNull(); if (MOName == null) { Bll.MetricOrgLocationName moname = new Bll.MetricOrgLocationName(); moname.MetricID = MetricID; moname.OrgLocationID = OrgLocationID; moname.Alias = CustomMetricAlias; moname.Code = CustomMetricCode; dc.MetricOrgLocationName.InsertOnSubmit(moname); } else { // update org location specific metric names MOName.Alias = CustomMetricAlias; MOName.Code = CustomMetricCode; } } dc.SubmitChanges(); string ConvertedValue = Value; if (metric.UnitOfMeasureID != SelectedUoMID && metric.MetricDataTypeID == 1 && SelectedUoMID != null && metric.UnitOfMeasureID != null) { ConvertedValue = Mc_UnitsOfMeasure.ConvertValue(Value, (Guid)SelectedUoMID, (Guid)metric.UnitOfMeasureID); } if (metricValue == null) { Bll.MetricValue mv = new Bll.MetricValue(); mv.MetricValueID = Guid.NewGuid(); mv.MetricID = MetricID; mv.FrequencyID = metric.FrequencyID; mv.Date = Date; mv.OrgLocationID = OrgLocationID; mv.InputUnitOfMeasureID = SelectedUoMID; mv.UnitOfMeasureID = metric.UnitOfMeasureID; mv.MetricDataTypeID = metric.MetricDataTypeID; mv.Value = Value; mv.ConvertedValue = ConvertedValue; mv.InputUserId = UserId; mv.Approved = false; mv.ReviewUpdated = false; mv.ApproveUserId = null; mv.Notes = Notes; mv.FilesAttached = IsFilesAttached; mv.IsCalc = true; mv.InProcess = false; dc.MetricValue.InsertOnSubmit(mv); dc.SubmitChanges(); _ActualValueID = mv.MetricValueID; } else { metricValue.MetricDataTypeID = metric.MetricDataTypeID; metricValue.InputUnitOfMeasureID = SelectedUoMID; metricValue.UnitOfMeasureID = metric.UnitOfMeasureID; metricValue.Value = Value; metricValue.ConvertedValue = ConvertedValue; metricValue.Approved = Approved; metricValue.ReviewUpdated = (Approve) ? false : (OldApproved == null && Approved == null); if (Approve) { if (OldApproved != Approved) { metricValue.ApproveUserId = UserId; } if (OldValue != Value) { metricValue.InputUserId = UserId; } } else { metricValue.InputUserId = UserId; } metricValue.Notes = Notes; metricValue.FilesAttached = IsFilesAttached; metricValue.IsCalc = true; dc.SubmitChanges(); _ActualValueID = metricValue.MetricValueID; } } return(_ActualValueID); } }
/*private static void UpdateMetricOrgLocationUoM(Guid InstanceId, Guid MetricID, Guid OrgLocationID, Guid? InputUnitOfMeasureID) * { * using (LinqMicajahDataContext dc = new LinqMicajahDataContext()) * { * MetricOrgLocationUoM monum = * (from mon in dc.MetricOrgLocationUoM * where * mon.InstanceId == InstanceId && * mon.MetricID == MetricID && * mon.OrgLocationID == OrgLocationID * select mon).FirstOrNull(); * * if (monum != null) * monum.InputUnitOfMeasureID = InputUnitOfMeasureID; * else * { * Bll.MetricOrgLocationUoM muom = new Bll.MetricOrgLocationUoM(); * muom.InstanceId = InstanceId; * muom.MetricID = MetricID; * muom.OrgLocationID = OrgLocationID; * muom.InputUnitOfMeasureID = InputUnitOfMeasureID; * dc.MetricOrgLocationUoM.InsertOnSubmit(muom); * } * dc.SubmitChanges(); * } * }*/ public static void SaveCalcValues(List <Extend> _InputMetricValues, List <Extend> _CalcMetricValues) { using (LinqMicajahDataContext dc = new LinqMicajahDataContext()) { foreach (MetricValue.Extend mve in _CalcMetricValues) { if (mve.MetricValueID == Guid.Empty && String.IsNullOrEmpty(mve.Value)) { continue; // don't save uncalced values } mve.ConvertedValue = mve.Value; if (mve.InputUnitOfMeasureID != mve.UnitOfMeasureID && mve.InputUnitOfMeasureID != null && mve.UnitOfMeasureID != null) { Mc_UnitsOfMeasure.ConvertValue(mve.Value, (Guid)mve.InputUnitOfMeasureID, (Guid)mve.UnitOfMeasureID); } MetricValue metricValue = (from mv in dc.MetricValue where mv.MetricID == mve.MetricID && mv.InstanceId == mve.InstanceId && mv.Status == true && mv.Date == mve.Date && mv.FrequencyID == mve.FrequencyID && mv.OrgLocationID == mve.OrgLocationID select mv).FirstOrNull(); if (metricValue == null) { // insert Bll.MetricValue mv = new Bll.MetricValue(); mv.InstanceId = mve.InstanceId; mv.MetricID = mve.MetricID; mv.FrequencyID = mve.FrequencyID; mv.Date = mve.Date; mv.InputUnitOfMeasureID = mve.InputUnitOfMeasureID; mv.UnitOfMeasureID = mve.UnitOfMeasureID; mv.MetricDataTypeID = 1; mv.Value = mve.Value; mv.ConvertedValue = mve.ConvertedValue; mv.Notes = "Calculated"; mv.FilesAttached = false; mv.IsCalc = true; mv.InProcess = false; mv.OrgLocationID = mve.OrgLocationID; mv.Approved = false; mv.InputUserId = null; mv.MissedCalc = mve.MissedCalc; dc.MetricValue.InsertOnSubmit(mv); } else { // update metricValue.MetricDataTypeID = 1; metricValue.InputUnitOfMeasureID = mve.InputUnitOfMeasureID; metricValue.UnitOfMeasureID = mve.UnitOfMeasureID; metricValue.Value = mve.Value; metricValue.ConvertedValue = mve.ConvertedValue; metricValue.Notes = "Calculated"; metricValue.FilesAttached = false; metricValue.IsCalc = true; metricValue.OrgLocationID = mve.OrgLocationID; metricValue.MissedCalc = mve.MissedCalc; metricValue.InProcess = false; } dc.SubmitChanges(); } } }
public static void SaveBulkValues(List <Bll.MetricOrgLocationUoM> MetricOrgLocationUoMList, List <Bll.MetricValue.Extend> NewValues, List <Bll.MetricValue.Extend> OldValues, Micajah.Common.Security.UserContext CurrentUser, bool InputMode) { using (LinqMicajahDataContext dc = new LinqMicajahDataContext()) { foreach (MetricOrgLocationUoM uom in MetricOrgLocationUoMList) { MetricOrgLocationUoM MOUoM = (from muom in dc.MetricOrgLocationUoM where muom.MetricID == uom.MetricID && muom.InstanceId == LinqMicajahDataContext.InstanceId && muom.OrgLocationID == uom.OrgLocationID select muom).FirstOrNull(); if (MOUoM == null) { Bll.MetricOrgLocationUoM muom = new Bll.MetricOrgLocationUoM(); muom.MetricID = uom.MetricID; muom.OrgLocationID = uom.OrgLocationID; muom.InputUnitOfMeasureID = uom.InputUnitOfMeasureID; dc.MetricOrgLocationUoM.InsertOnSubmit(muom); } else if (uom.InputUnitOfMeasureID != MOUoM.InputUnitOfMeasureID) { MOUoM.InputUnitOfMeasureID = uom.InputUnitOfMeasureID; } } dc.SubmitChanges(); Metric metric = null; for (int i = 0; i < NewValues.Count; i++) { MetricValue.Extend NewValue = NewValues[i]; MetricValue.Extend OldValue = OldValues[i]; bool UpdateMetric = false; if (metric == null) { UpdateMetric = true; } else if (metric.MetricID != NewValue.MetricID) { UpdateMetric = true; } if (UpdateMetric) { metric = (from m in dc.Metric where m.MetricID == NewValue.MetricID && m.InstanceId == LinqMicajahDataContext.InstanceId && m.Status == true select m).FirstOrNull(); } if (metric != null) { MetricValue metricValue = (from mv in dc.MetricValue where mv.MetricID == NewValue.MetricID && mv.InstanceId == LinqMicajahDataContext.InstanceId && mv.Status == true && mv.Date == NewValue.Date && mv.FrequencyID == metric.FrequencyID && mv.OrgLocationID == NewValue.OrgLocationID select mv).FirstOrNull(); string ConvertedValue = NewValue.Value; if (metric.UnitOfMeasureID != NewValue.InputUnitOfMeasureID && metric.MetricDataTypeID == 1 && NewValue.InputUnitOfMeasureID != null && metric.UnitOfMeasureID != null) { ConvertedValue = Mc_UnitsOfMeasure.ConvertValue(NewValue.Value, (Guid)NewValue.InputUnitOfMeasureID, (Guid)metric.UnitOfMeasureID); } if (metricValue == null) { Bll.MetricValue mv = new Bll.MetricValue(); mv.MetricValueID = NewValue.MetricValueID = Guid.NewGuid(); mv.FrequencyID = metric.FrequencyID; mv.UnitOfMeasureID = metric.UnitOfMeasureID; mv.MetricDataTypeID = metric.MetricDataTypeID; mv.ConvertedValue = ConvertedValue; mv.InputUserId = CurrentUser.UserId; mv.Approved = false; mv.ReviewUpdated = false; mv.ApproveUserId = null; mv.Notes = null; mv.FilesAttached = false; mv.IsCalc = true; mv.InProcess = false; mv.MetricID = NewValue.MetricID; mv.OrgLocationID = NewValue.OrgLocationID; mv.Date = NewValue.Date; mv.InputUnitOfMeasureID = NewValue.InputUnitOfMeasureID; mv.Value = NewValue.Value; dc.MetricValue.InsertOnSubmit(mv); } else { metricValue.MetricDataTypeID = metric.MetricDataTypeID; metricValue.InputUnitOfMeasureID = NewValue.InputUnitOfMeasureID; metricValue.UnitOfMeasureID = metric.UnitOfMeasureID; metricValue.ConvertedValue = ConvertedValue; metricValue.Approved = NewValue.Approved; metricValue.ReviewUpdated = (!InputMode) ? false : (metricValue.Approved == null && NewValue.Approved == null); if (!InputMode) { if (metricValue.Approved != NewValue.Approved) { metricValue.ApproveUserId = CurrentUser.UserId; } if (metricValue.Value != NewValue.Value) { metricValue.InputUserId = CurrentUser.UserId; } } else { metricValue.InputUserId = CurrentUser.UserId; } metricValue.Value = NewValue.Value; metricValue.IsCalc = true; } } } dc.SubmitChanges(); for (int i = 0; i < NewValues.Count; i++) { MetricValue.Extend NewValue = NewValues[i]; MetricValue.Extend OldValue = OldValues[i]; Bll.Mc_User.Extend mue = Bll.Mc_User.GetValueInputUser(dc, OldValue.MetricValueID); // build mail to data collector if status or comment were changed if ((!InputMode) && (OldValue.Approved != NewValue.Approved)) { Bll.MetricValueChangeLog.LogChange(dc, OldValue.MetricValueID == Guid.Empty ? NewValue.MetricValueID : OldValue.MetricValueID, Bll.MetricValueChangeTypeEnum.StatusChanged, OldValue.MetricValueID == Guid.Empty ? "Pending" : (OldValue.Approved == null ? "Under Review" : ((bool)OldValue.Approved ? "Approved" : "Pending")), NewValue.Approved == null ? "Under Review" : ((bool)NewValue.Approved ? "Approved" : "Pending"), Utils.Mail.BuildLogMessageBody(OldValue, NewValue, String.Empty, CurrentUser, mue, Bll.MetricValueChangeTypeEnum.StatusChanged)); if (NewValue.Approved == null && mue != null) { Utils.Mail.Send(mue.Email, mue.FullName, "MetricTrac - Value Status is changed", Utils.Mail.BuildEmailBody(OldValue, NewValue, String.Empty, CurrentUser)); } } // record in change log if (OldValue.MetricValueID == Guid.Empty) { Bll.MetricValueChangeLog.LogChange(NewValue.MetricValueID, MetricTrac.Bll.MetricValueChangeTypeEnum.ValueEntered, String.Empty, NewValue.Value, Utils.Mail.BuildLogMessageBody(OldValue, NewValue, "Bulk Edit", CurrentUser, mue, MetricTrac.Bll.MetricValueChangeTypeEnum.ValueEntered)); } else if (OldValue.Value != NewValue.Value) { Bll.MetricValueChangeLog.LogChange(OldValue.MetricValueID, MetricTrac.Bll.MetricValueChangeTypeEnum.ValueChanged, OldValue.Value, NewValue.Value, Utils.Mail.BuildLogMessageBody(OldValue, NewValue, "Bulk Edit", CurrentUser, mue, MetricTrac.Bll.MetricValueChangeTypeEnum.ValueChanged)); } } dc.SubmitChanges(); } }