public ActionResult ScMeasureTarget(DataSourceRequest command, int?supplierId)
        {
            if (!_permissionService.Authorize(PermissionProvider.SupplierManagement))
            {
                return(AccessDeniedView());
            }

            if (!supplierId.HasValue)
            {
                supplierId = _supplierService.GetAllAsync().Result.FirstOrDefault().Id;
            }

            var scMeasures       = _scMeasureService.GetAllAsync(pageIndex: command.Page - 1, pageSize: command.PageSize).Result;
            var scMeasureTargets = _scMeasureTargetService.GetAllScMeasureTargetBySupplierId(supplierId.Value).Result;

            List <ScMeasureTargetModel> scMeasureTargetModels = new List <ScMeasureTargetModel>();

            foreach (var scMeasure in scMeasures)
            {
                ScMeasureTargetModel scMeasureTargetModel = new ScMeasureTargetModel();
                scMeasureTargetModel.ScMeasureName = scMeasure.Name;
                scMeasureTargetModel.ScMeasureId   = scMeasure.Id;

                ScMeasureTarget scMeasureTarget = scMeasureTargets.FirstOrDefault(x => x.ScMeasureId == scMeasure.Id);
                if (scMeasureTarget != null)
                {
                    scMeasureTargetModel.Id     = scMeasureTarget.Id;
                    scMeasureTargetModel.Target = scMeasureTarget.Target;
                }
                scMeasureTargetModels.Add(scMeasureTargetModel);
            }

            var gridModel = new DataSourceResult
            {
                Data  = scMeasureTargetModels,
                Total = scMeasures.TotalCount
            };

            // Return the result as JSON
            return(Json(gridModel));
        }
示例#2
0
        public List <ScoreCardObject> GetScoreCardData(int year, int supplierId, DateTime?fromDate, DateTime?toDate)
        {
            if (toDate.HasValue && toDate.Value > DateTime.Now.AddDays(1))
            {
                toDate = DateTime.Now.AddDays(1);
            }

            var scMeasures = _scMeasureRepository.Table.Where(m => m.IsDisplay).ToList();

            var result = scMeasures.Select(scm => new ScoreCardObject
            {
                MsqMeasure  = scm.Name,
                ScMeasureId = scm.Id,
                IsBold      = scm.IsBold
            }).ToList();
            var result2 = scMeasures.Select(scm => new ScoreCardObject
            {
                MsqMeasure  = scm.Name,
                ScMeasureId = scm.Id
            }).ToList();
            //Get supplier
            var supplier = _supplierService.GetByIdAsync(supplierId).Result;
            //Get all quality alert in year from 1st July to 30th Jun of supplier
            var quanlityAlerts = _qualityAlertService.SearchQualityAlertObjectAsync(startDate: new DateTime(year - 1, 7, 1), endDate: new DateTime(year, 7, 1), supplierIds: new List <int>()
            {
                supplierId
            }).Result;
            //Get all MQS measure
            var mqsMeasures = _scMeasureRepository.Table.Where(m => m.IsImported).OrderBy(m => m.DisplayOrder).ToList();
            //Get all sub-Column to add to Quality alert table in excel sheeet
            var subColumns = _subColumnService.GetAllAsync().Result;
            //get all scoreCardItems of supplier
            var scoreCardItems = _mqsMeasureRepository.Table.Where(x => x.SupplierId == supplierId).ToList();
            //Get all score card target of supplier
            var scMeasureTargets = _scMeasureTargetService.GetAllScMeasureTargetBySupplierId(supplier.Id).Result;
            var listTask         = new List <Task>();

            DateTime fStartDate = new DateTime(year - 1, 7, 1);
            DateTime fEndDate   = DateTime.Now.AddDays(1);

            do
            {
                DateTime tDate;
                if (fStartDate.Year == fEndDate.Year && fStartDate.Month == fEndDate.Month)
                {
                    tDate = fEndDate;
                }
                else
                {
                    tDate = fStartDate.AddMonths(1).AddDays(1 - fStartDate.Day);
                }

                listTask.Add(CreateScoreCardAsync(fStartDate, tDate, result, quanlityAlerts, scMeasures, mqsMeasures, scoreCardItems, scMeasureTargets, subColumns));
                fStartDate = tDate;
            } while (fStartDate < fEndDate);


            if (fromDate.HasValue && toDate.HasValue && fromDate.Value.Year == toDate.Value.Year && fromDate.Value.Month == toDate.Value.Month)
            {
                listTask.Add(CreateScoreCardAsync(fromDate.Value, toDate.Value, result2, quanlityAlerts, scMeasures, mqsMeasures, scoreCardItems, scMeasureTargets, subColumns));

                Task.WaitAll(listTask.ToArray());

                YtdCalculate(result, scMeasures);

                for (int j = 0; j < scMeasures.Count; j++)
                {
                    result[j].Data[(fromDate.Value.Month + 5) % 12] = result2[j].Data[(fromDate.Value.Month + 5) % 12];
                }
                return(result);
            }

            if (fromDate.HasValue && fromDate.Value != new DateTime(year - 1, 7, 1))
            {
                var tDate = fromDate.Value.AddMonths(1).AddDays(1 - fromDate.Value.Day);
                listTask.Add(CreateScoreCardAsync(fromDate.Value, tDate, result2, quanlityAlerts, scMeasures, mqsMeasures, scoreCardItems, scMeasureTargets, subColumns));
            }
            if (toDate.HasValue && toDate.Value != DateTime.Now.Date)
            {
                var sDate = toDate.Value.AddDays(1 - toDate.Value.Day);
                listTask.Add(CreateScoreCardAsync(sDate, toDate.Value, result2, quanlityAlerts, scMeasures, mqsMeasures, scoreCardItems, scMeasureTargets, subColumns));
            }
            //Calculate for every month from July to Jun

            Task.WaitAll(listTask.ToArray());

            YtdCalculate(result, scMeasures);

            if (fromDate.HasValue && fromDate.Value != new DateTime(year - 1, 7, 1))
            {
                for (int j = 0; j < scMeasures.Count; j++)
                {
                    result[j].Data[(fromDate.Value.Month + 5) % 12] = result2[j].Data[(fromDate.Value.Month + 5) % 12];
                }
            }
            if (toDate.HasValue && toDate.Value != DateTime.Now.Date)
            {
                for (int j = 0; j < scMeasures.Count; j++)
                {
                    result[j].Data[(toDate.Value.Month + 5) % 12] = result2[j].Data[(toDate.Value.Month + 5) % 12];
                }
            }
            return(result);
        }