public UpdateKpiTargetResponse UpdateKpiTarget(UpdateKpiTargetRequest request) { PeriodeType periodeType = (PeriodeType)Enum.Parse(typeof(PeriodeType), request.PeriodeType); var response = new UpdateKpiTargetResponse(); response.PeriodeType = periodeType; try { foreach (var pillar in request.Pillars) { foreach (var kpi in pillar.Kpis) { foreach (var kpiTarget in kpi.KpiTargets) { if (kpiTarget.Id == 0) { var kpiTargetNew = new KpiTarget(); kpiTargetNew.Value = kpiTarget.Value; kpiTargetNew.Kpi = DataContext.Kpis.Single(x => x.Id == kpi.Id); kpiTargetNew.PeriodeType = periodeType; kpiTargetNew.Periode = kpiTarget.Periode; kpiTargetNew.IsActive = true; kpiTargetNew.Remark = kpiTarget.Remark; kpiTargetNew.CreatedDate = DateTime.Now; kpiTargetNew.UpdatedDate = DateTime.Now; DataContext.KpiTargets.Add(kpiTargetNew); } else { var kpiTargetNew = new KpiTarget(); kpiTargetNew.Id = kpiTarget.Id; kpiTargetNew.Value = kpiTarget.Value; kpiTargetNew.Kpi = DataContext.Kpis.Single(x => x.Id == kpi.Id); kpiTargetNew.PeriodeType = periodeType; kpiTargetNew.Periode = kpiTarget.Periode; kpiTargetNew.IsActive = true; kpiTargetNew.Remark = kpiTarget.Remark; kpiTargetNew.UpdatedDate = DateTime.Now; DataContext.KpiTargets.Attach(kpiTargetNew); DataContext.Entry(kpiTargetNew).State = EntityState.Modified; } } } } response.IsSuccess = true; response.Message = "KPI Target has been updated successfully"; DataContext.SaveChanges(); } catch (InvalidOperationException invalidOperationException) { response.Message = invalidOperationException.Message; } return(response); }
public BaseResponse KpiTargetInput(KpiTargetInputRequest request) { try { //yearly var year = request.Start.Year; var yearlyTarget = DataContext.KpiTargets.FirstOrDefault(x => x.Kpi.Id == request.KpiId && x.PeriodeType == PeriodeType.Yearly && x.Periode.Year == year); var kpi = new Kpi { Id = request.KpiId }; DataContext.Kpis.Attach(kpi); if (yearlyTarget != null) { yearlyTarget.Value = request.Value; } else { var newYearlyTarget = new KpiTarget { Value = request.Value, PeriodeType = PeriodeType.Yearly, Periode = new DateTime(request.Start.Year, 1, 1), Kpi = kpi }; DataContext.KpiTargets.Add(newYearlyTarget); } //monthly //var monthlyTargets = DataContext.KpiTargets.Where(x => x.Kpi.Id == request.KpiId // && x.PeriodeType == PeriodeType.Monthly // && x.Periode >= request.Start && x.Periode <= request.End).ToList(); //for (var i = request.Start.Month; i <= request.End.Month; i++) //{ // var monthlyTarget = monthlyTargets.FirstOrDefault(x => x.Periode.Month == i); // if (monthlyTarget != null) // { // monthlyTarget.Value = request.Value; // } // else // { // var newMonthlyTarget = new KpiTarget // { // Periode = new DateTime(year, i, 1), // PeriodeType = PeriodeType.Monthly, // Value = request.Value, // Kpi = kpi // }; // DataContext.KpiTargets.Add(newMonthlyTarget); // } //} DataContext.SaveChanges(); return(new BaseResponse { IsSuccess = true, Message = "Kpi Valu has been saved successfully" }); } catch { return(new BaseResponse { IsSuccess = false, Message = "An error occured, please contact adminstrator for further information" }); } }