Exemplo n.º 1
0
        public UpdateKpiAchievementItemResponse UpdateKpiAchievementItem(UpdateKpiAchievementItemRequest request)
        {
            var response = new UpdateKpiAchievementItemResponse();
            try
            {
                var kpiAchievement = request.MapTo<KpiAchievement>();

                if (request.Id != 0)
                {
                    var attachedEntity = DataContext.KpiAchievements.Find(request.Id);
                    if (attachedEntity != null && DataContext.Entry(attachedEntity).State != EntityState.Detached)
                    {
                        DataContext.Entry(attachedEntity).State = EntityState.Detached;
                    }
                    DataContext.KpiAchievements.Attach(kpiAchievement);
                    DataContext.Entry(kpiAchievement).State = EntityState.Modified;
                    DataContext.SaveChanges();
                }
                else
                {
                    kpiAchievement.Kpi = DataContext.Kpis.FirstOrDefault(x => x.Id == request.KpiId);
                    DataContext.KpiAchievements.Add(kpiAchievement);
                    DataContext.SaveChanges();
                }
                response.Id = kpiAchievement.Id;
                response.IsSuccess = true;
                response.Message = "KPI Achievement item has been updated successfully";

            }
            catch (InvalidOperationException invalidOperationException)
            {
                response.Message = invalidOperationException.Message;
            }
            catch (ArgumentNullException argumentNullException)
            {
                response.Message = argumentNullException.Message;
            }

            return response;
        }
Exemplo n.º 2
0
 private BaseResponse UpdateKpiAchievement(IEnumerable<ConfigurationViewModel.Item> data)
 {
     var response = new BaseResponse { IsSuccess = false, Message = "Data Not Valid" };
     if (data != null)
     {
         var batch = new BatchUpdateKpiAchievementRequest();
         foreach (var datum in data)
         {
             var prepare = new UpdateKpiAchievementItemRequest() { Id = datum.Id, KpiId = datum.KpiId, Periode = datum.Periode, Value = datum.Value, PeriodeType = datum.PeriodeType, Remark = datum.Remark };// data.MapTo<UpdateKpiAchievementItemRequest>();
             batch.BatchUpdateKpiAchievementItemRequest.Add(prepare);
         }
         response = _kpiAchievementService.BatchUpdateKpiAchievements(batch);
     }
     return response;
 }
Exemplo n.º 3
0
        public void Process(SaveKpiTransformationScheduleResponse kpiTransformationSchedule)
        {
            var kpiPattern = @"k(\d+)";
            JobManager.AddJob(() =>
            {
                var complete = true;
                using (var dataContext = new DataContext())
                {
                    var kpiAchievementService = new KpiAchievementService(dataContext);
                    var logService = new KpiTransformationLogService(dataContext);
                    var kpiTransformationScheduleService = new KpiTransformationScheduleService(dataContext);
                    var kpiService = new KpiService(dataContext);
                    for (var date = kpiTransformationSchedule.Start; date <= kpiTransformationSchedule.End; date = Increment(kpiTransformationSchedule, date))
                    {
                        foreach (var kpi in kpiTransformationSchedule.SelectedKpis)
                        {
                            try
                            {
                                var kpiTransformed = kpi.CustomFormula;
                                var ytdTransformed = kpi.CustomFormula;
                                var mtdTransformed = kpi.CustomFormula;
                                var itdTransformed = kpi.CustomFormula;
                                var existingKpiActual = kpiAchievementService.GetKpiAchievement(kpi.Id, date, kpiTransformationSchedule.PeriodeType);
                                if (kpi.MethodId == 1)
                                {
                                    if (string.IsNullOrEmpty(kpi.CustomFormula)) {
                                        //log here for dependency error
                                        var logRequest = new SaveKpiTransformationLogRequest
                                        {
                                            KpiId = kpi.Id,
                                            KpiTransformationScheduleId = kpiTransformationSchedule.Id,
                                            Periode = date,
                                            Status = KpiTransformationStatus.Error,
                                            Notes = "Method input is formula but the formula is not defined"
                                        };
                                        logService.Save(logRequest);
                                        continue;
                                    }
                                    Regex r = new Regex(kpiPattern, RegexOptions.IgnoreCase);
                                    Match m = r.Match(kpi.CustomFormula);
                                    var meetRequirements = true;
                                    while (m.Success)
                                    {
                                        Group g = m.Groups[1];
                                        var relatedKpiId = int.Parse(g.Value);
                                        var relatedKpiActual = kpiAchievementService.GetKpiAchievement(relatedKpiId, date, kpiTransformationSchedule.PeriodeType);
                                        if (relatedKpiActual.IsSuccess && relatedKpiActual.Value.HasValue)
                                        {
                                            kpiTransformed = Regex.Replace(kpiTransformed, "k" + g.Value, relatedKpiActual.Value.ToString(), RegexOptions.IgnoreCase);
                                            if (kpi.YtdFormula == YtdFormula.Custom)
                                            {
                                                if (relatedKpiActual.Mtd.HasValue && relatedKpiActual.Ytd.HasValue && relatedKpiActual.Itd.HasValue)
                                                {
                                                    switch (kpiTransformationSchedule.PeriodeType)
                                                    {
                                                        case PeriodeType.Daily:
                                                            mtdTransformed = Regex.Replace(mtdTransformed, "k" + g.Value, relatedKpiActual.Mtd.ToString(), RegexOptions.IgnoreCase);
                                                            ytdTransformed = Regex.Replace(ytdTransformed, "k" + g.Value, relatedKpiActual.Ytd.ToString(), RegexOptions.IgnoreCase);
                                                            itdTransformed = Regex.Replace(itdTransformed, "k" + g.Value, relatedKpiActual.Itd.ToString(), RegexOptions.IgnoreCase);
                                                            break;
                                                        case PeriodeType.Monthly:
                                                            ytdTransformed = Regex.Replace(ytdTransformed, "k" + g.Value, relatedKpiActual.Ytd.ToString(), RegexOptions.IgnoreCase);
                                                            itdTransformed = Regex.Replace(itdTransformed, "k" + g.Value, relatedKpiActual.Itd.ToString(), RegexOptions.IgnoreCase);
                                                            break;
                                                        case PeriodeType.Yearly:
                                                            itdTransformed = Regex.Replace(itdTransformed, "k" + g.Value, relatedKpiActual.Itd.ToString(), RegexOptions.IgnoreCase);
                                                            break;
                                                        default:
                                                            break;
                                                    }
                                                }
                                                else
                                                {
                                                    //log here for dependency error
                                                    var logRequest = new SaveKpiTransformationLogRequest
                                                    {
                                                        KpiId = kpi.Id,
                                                        KpiTransformationScheduleId = kpiTransformationSchedule.Id,
                                                        Periode = date,
                                                        Status = KpiTransformationStatus.Error,
                                                        Notes = "The aggregation for <strong>" + relatedKpiActual.Kpi.Name + " (" + relatedKpiActual.Kpi.Measurement + ")</strong> has not been proceed"
                                                    };
                                                    logService.Save(logRequest);
                                                    meetRequirements = false;
                                                    complete = false;
                                                }

                                            }
                                        }
                                        else
                                        {
                                            var relatedKpi = kpiService.GetBy(new Services.Requests.Kpi.GetKpiRequest { Id = relatedKpiId });
                                            var logRequest = new SaveKpiTransformationLogRequest
                                            {
                                                KpiId = kpi.Id,
                                                KpiTransformationScheduleId = kpiTransformationSchedule.Id,
                                                Periode = date,
                                                Status = KpiTransformationStatus.Error,
                                                Notes = "Kpi <strong>" + relatedKpi.Name + " (" + relatedKpi.Measurement.Name + ")</strong> has no value for this periode of time"
                                            };
                                            logService.Save(logRequest);
                                            meetRequirements = false;
                                            complete = false;
                                        }
                                        m = m.NextMatch();
                                    }
                                    if (kpi.YtdFormula == YtdFormula.Custom )
                                    {
                                        if (meetRequirements) {
                                            var kpiActualRequest = new UpdateKpiAchievementItemRequest
                                            {
                                                Id = existingKpiActual.IsSuccess ? existingKpiActual.Id : 0,
                                                KpiId = kpi.Id,
                                                Periode = date,
                                                PeriodeType = kpiTransformationSchedule.PeriodeType,
                                                Value = new Expression(kpiTransformed).Evaluate().ToString(),
                                                UserId = kpiTransformationSchedule.UserId
                                            };
                                            if (mtdTransformed != kpi.CustomFormula)
                                            {
                                                kpiActualRequest.Mtd = (double?)new Expression(mtdTransformed).Evaluate();
                                            }
                                            if (ytdTransformed != kpi.CustomFormula)
                                            {
                                                kpiActualRequest.Ytd = (double?)new Expression(ytdTransformed).Evaluate();
                                            }
                                            if (itdTransformed != kpi.CustomFormula)
                                            {
                                                kpiActualRequest.Itd = (double?)new Expression(itdTransformed).Evaluate();
                                            }
                                            kpiActualRequest.UpdateDeviation = true;
                                            var resp = kpiAchievementService.UpdateKpiAchievementItem(kpiActualRequest);
                                            if (resp.IsSuccess)
                                            {
                                                switch (kpiTransformationSchedule.PeriodeType)
                                                {
                                                    case PeriodeType.Daily:
                                                        kpiAchievementService.UpdateKpiAchievementItem(kpi.Id, PeriodeType.Monthly, new DateTime(date.Year, date.Month, 1), kpiActualRequest.Mtd, kpiTransformationSchedule.UserId);
                                                        kpiAchievementService.UpdateKpiAchievementItem(kpi.Id, PeriodeType.Yearly, new DateTime(date.Year, 1, 1), kpiActualRequest.Ytd, kpiTransformationSchedule.UserId);
                                                        break;
                                                    case PeriodeType.Monthly:
                                                        kpiAchievementService.UpdateKpiAchievementItem(kpi.Id, PeriodeType.Yearly, new DateTime(date.Year, 1, 1), kpiActualRequest.Ytd, kpiTransformationSchedule.UserId);
                                                        break;
                                                    default:
                                                        break;
                                                }

                                                var logRequest = new SaveKpiTransformationLogRequest
                                                {
                                                    KpiId = kpi.Id,
                                                    KpiTransformationScheduleId = kpiTransformationSchedule.Id,
                                                    Periode = date,
                                                    Status = KpiTransformationStatus.Complete,
                                                };
                                                logService.Save(logRequest);
                                            }
                                            else
                                            {
                                                var logRequest = new SaveKpiTransformationLogRequest
                                                {
                                                    KpiId = kpi.Id,
                                                    KpiTransformationScheduleId = kpiTransformationSchedule.Id,
                                                    Periode = date,
                                                    Status = KpiTransformationStatus.Error,
                                                    Notes = resp.Message
                                                };
                                                logService.Save(logRequest);
                                                complete = false;
                                            }

                                        }

                                    }
                                    else
                                    {
                                        if (meetRequirements)
                                        {
                                            var request = new UpdateKpiAchievementItemRequest
                                            {
                                                Periode = date,
                                                PeriodeType = kpiTransformationSchedule.PeriodeType,
                                                Id = existingKpiActual.IsSuccess ? existingKpiActual.Id : 0,
                                                KpiId = kpi.Id,
                                                UserId = kpiTransformationSchedule.UserId,
                                                Value = new Expression(kpiTransformed).Evaluate().ToString()
                                            };
                                            var resp = kpiAchievementService.UpdateOriginalData(request);
                                            if (resp.IsSuccess)
                                            {
                                                var logRequest = new SaveKpiTransformationLogRequest
                                                {
                                                    KpiId = kpi.Id,
                                                    KpiTransformationScheduleId = kpiTransformationSchedule.Id,
                                                    Periode = date,
                                                    Status = KpiTransformationStatus.Complete,
                                                };
                                                logService.Save(logRequest);
                                            }
                                            else
                                            {
                                                var logRequest = new SaveKpiTransformationLogRequest
                                                {
                                                    KpiId = kpi.Id,
                                                    KpiTransformationScheduleId = kpiTransformationSchedule.Id,
                                                    Periode = date,
                                                    Status = KpiTransformationStatus.Error,
                                                    Notes = resp.Message
                                                };
                                                logService.Save(logRequest);
                                                complete = false;
                                            }
                                        }
                                    }

                                }
                                else
                                {
                                    var request = new UpdateKpiAchievementItemRequest
                                    {
                                        Periode = date,
                                        PeriodeType = kpiTransformationSchedule.PeriodeType,
                                        Id = existingKpiActual.IsSuccess ? existingKpiActual.Id : 0,
                                        KpiId = kpi.Id,
                                        UserId = kpiTransformationSchedule.UserId,
                                        Value = existingKpiActual.Value.ToString(),
                                        Remark = existingKpiActual.Remark
                                    };
                                    var resp = kpiAchievementService.UpdateOriginalData(request);
                                    if (resp.IsSuccess)
                                    {
                                        var logRequest = new SaveKpiTransformationLogRequest
                                        {
                                            KpiId = kpi.Id,
                                            KpiTransformationScheduleId = kpiTransformationSchedule.Id,
                                            Periode = date,
                                            Status = KpiTransformationStatus.Complete,
                                        };
                                        logService.Save(logRequest);
                                    }
                                    else
                                    {
                                        var logRequest = new SaveKpiTransformationLogRequest
                                        {
                                            KpiId = kpi.Id,
                                            KpiTransformationScheduleId = kpiTransformationSchedule.Id,
                                            Periode = date,
                                            Status = KpiTransformationStatus.Error,
                                            Notes = resp.Message
                                        };
                                        logService.Save(logRequest);
                                        complete = false;
                                    }
                                }
                            }
                            catch (Exception e)
                            {
                                var logRequest = new SaveKpiTransformationLogRequest
                                {
                                    KpiId = kpi.Id,
                                    KpiTransformationScheduleId = kpiTransformationSchedule.Id,
                                    Periode = date,
                                    Status = KpiTransformationStatus.Error,
                                    Notes = "Excception Message :" + e.Message + "<br/>Inner Exception Message : " + (e.InnerException != null ? e.InnerException.Message : "")
                                };
                                logService.Save(logRequest);
                                complete = false;
                            }

                        }
                        if (complete)
                        {
                            kpiTransformationScheduleService.UpdateStatus(kpiTransformationSchedule.Id, KpiTransformationStatus.Complete);
                        }
                        else
                        {
                            kpiTransformationScheduleService.UpdateStatus(kpiTransformationSchedule.Id, KpiTransformationStatus.Error);
                        }
                    }
                }

            }, (s) => s.ToRunNow());
        }
Exemplo n.º 4
0
 private BaseResponse _UpdateKpiAchievement(List<ConfigurationViewModel.Item> datas, string periodeType, int year, int month)
 {
     var response = new BaseResponse();
     if (datas != null)
     {
         var batch = new BatchUpdateKpiAchievementRequest();
         foreach (var data in datas)
         {
             var prepare = new UpdateKpiAchievementItemRequest() { Id = data.Id, KpiId = data.KpiId, Periode = data.Periode, Value = data.Value, PeriodeType = data.PeriodeType, Remark = data.Remark };// data.MapTo<UpdateKpiAchievementItemRequest>();
             batch.BatchUpdateKpiAchievementItemRequest.Add(prepare);
         }
         response = _kpiAchievementService.BatchUpdateKpiAchievements(batch);
     }
     return response;
 }
Exemplo n.º 5
0
        public ActionResult UpdateKpi(UpdateKpiOriginalViewModel viewModel)
        {
            if(viewModel.Id == 0 && viewModel.Value == null)
            {
                return null;
            }
            var sPeriodeType = viewModel.Type.Split('-')[0];
            var periodeType = (PeriodeType)Enum.Parse(typeof(PeriodeType), sPeriodeType, true);
            var theDate = DateTime.ParseExact(viewModel.Date, "MM/dd/yyyy", CultureInfo.InvariantCulture);
            switch (periodeType)
            {
                case PeriodeType.Monthly:
                    theDate = new DateTime(theDate.Year, theDate.Month, 1);
                    break;
                case PeriodeType.Yearly:
                    theDate = new DateTime(theDate.Year, 1, 1);
                    break;
            }
            switch (viewModel.Type)
            {
                case "daily-actual":
                case "monthly-actual":
                case "yearly-actual":
                    {
                        var request = new UpdateKpiAchievementItemRequest
                        {
                            Periode = theDate,
                            PeriodeType = periodeType,
                            Id = viewModel.Id,
                            KpiId = viewModel.KpiId,
                            UserId = UserProfile().UserId,
                            Value = viewModel.ValueType == "value" ? viewModel.Value : null,
                            Remark = viewModel.ValueType == "remark" ? viewModel.Value : null
                        };
                        var resp = _kpiAchievementService.UpdateOriginalData(request);
                        return Json(resp);
                    }
                // case gila-gilaan
                case "monthly-actual-prev":
                    {
                        var request = new UpdateKpiAchievementItemRequest
                        {
                            Periode = theDate.AddMonths(-1),
                            PeriodeType = periodeType,
                            Id = viewModel.Id,
                            KpiId = viewModel.KpiId,
                            UserId = UserProfile().UserId,
                            Value = viewModel.ValueType == "value" ? viewModel.Value : null,
                            Remark = viewModel.ValueType == "remark" ? viewModel.Value : null
                        };
                        var resp = _kpiAchievementService.UpdateOriginalData(request);
                        return Json(resp);
                    }
                case "monthly-actual-jcc":
                    {
                        var request = new UpdateKpiAchievementItemRequest
                        {
                            Periode = theDate,
                            PeriodeType = periodeType,
                            Id = viewModel.Id,
                            KpiId = viewModel.KpiId,
                            UserId = UserProfile().UserId,
                            Value = viewModel.ValueType == "value" ? viewModel.Value : null,
                            Remark = viewModel.ValueType == "remark" ? viewModel.Value : null
                        };
                        var resp = _kpiAchievementService.UpdateCustomJccFormula(request);
                        return Json(resp);
                    }
                case "monthly-actual-bunker":
                    {
                        var request = new UpdateKpiAchievementItemRequest
                        {
                            Periode = theDate,
                            PeriodeType = periodeType,
                            Id = viewModel.Id,
                            KpiId = viewModel.KpiId,
                            UserId = UserProfile().UserId,
                            Value = viewModel.ValueType == "value" ? viewModel.Value : null,
                            Remark = viewModel.ValueType == "remark" ? viewModel.Value : null
                        };
                        var resp = _kpiAchievementService.UpdateCustomBunkerPriceFormula(request);
                        return Json(resp);
                    }
                case "daily-actual-dafwc":
                    {
                        string value = viewModel.ValueType == "value" ? viewModel.Value : null;
                        if(viewModel.Value != null && viewModel.ValueType == "remark")
                        {
                            DateTime lastDAFWC;
                            DateTime.TryParse(viewModel.Value, out lastDAFWC);

                            if (lastDAFWC != null)
                            {
                                value =  (theDate - lastDAFWC).TotalDays.ToString();
                            }

                        }
                        var request = new UpdateKpiAchievementItemRequest
                        {
                            Periode = theDate,
                            PeriodeType = periodeType,
                            Id = viewModel.Id,
                            KpiId = viewModel.KpiId,
                            UserId = UserProfile().UserId,
                            Value = value,
                            Remark = viewModel.ValueType == "remark" ? viewModel.Value : null
                        };
                        var resp = _kpiAchievementService.UpdateOriginalData(request);
                        return Json(resp);
                    }
                default:
                    {
                        var request = new SaveKpiTargetRequest
                        {
                            Periode = theDate,
                            PeriodeType = periodeType,
                            Id = viewModel.Id,
                            KpiId = viewModel.KpiId,
                            UserId = UserProfile().UserId,
                            Value = viewModel.ValueType == "value" ? viewModel.Value : null,
                            Remark = viewModel.ValueType == "remark" ? viewModel.Value : null
                        };
                        var resp = _kpiTargetService.UpdateOriginalData(request);
                        return Json(resp);
                    }
            }
        }
Exemplo n.º 6
0
        public UpdateKpiAchievementItemResponse UpdateKpiAchievementItem(UpdateKpiAchievementItemRequest request)
        {
            var response = new UpdateKpiAchievementItemResponse();
            try
            {
                var user = DataContext.Users.First(x => x.Id == request.UserId);
                var kpiAchievement = request.MapTo<KpiAchievement>();
                if (request.Id != 0)
                {
                    kpiAchievement = DataContext.KpiAchievements
                        .Include(x => x.Kpi)
                        .Include(x => x.UpdatedBy)
                        .Single(x => x.Id == request.Id);
                    request.MapPropertiesToInstance<KpiAchievement>(kpiAchievement);
                }
                else
                {
                    kpiAchievement.CreatedBy = user;
                    DataContext.KpiAchievements.Add(kpiAchievement);
                }
                kpiAchievement.UpdatedBy = user;
                kpiAchievement.Kpi = DataContext.Kpis.Single(x => x.Id == request.KpiId);
                DataContext.SaveChanges();
                response.Id = kpiAchievement.Id;
                response.IsSuccess = true;
                response.Message = "KPI Achievement item has been updated successfully";

            }
            catch (InvalidOperationException invalidOperationException)
            {
                response.Message = invalidOperationException.Message;
            }
            catch (ArgumentNullException argumentNullException)
            {
                response.Message = argumentNullException.Message;
            }

            return response;
        }