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));
        }
        public ActionResult UpdateScMeasureTarget(DataSourceRequest command, int supplierId, ScMeasureTargetModel scMeasureTargetModel)
        {
            if (!_permissionService.Authorize(PermissionProvider.SupplierManagement))
            {
                return(AccessDeniedView());
            }

            if (scMeasureTargetModel.Id > 0)
            {
                ScMeasureTarget scMeasureTarget = _scMeasureTargetService.GetByIdAsync(scMeasureTargetModel.Id).Result;
                if (scMeasureTarget == null)
                {
                    return(Content("ScMeasureTarget not existed."));
                }

                if (scMeasureTarget.SupplierId != supplierId)
                {
                    return(Content("Supplier Identity not match with Supplier identity of ScMeasureTarget"));
                }

                if (scMeasureTarget.ScMeasureId != scMeasureTargetModel.ScMeasureId)
                {
                    return(Content("Measure Identity not match with Measure identity of ScMeasureTarget"));
                }

                scMeasureTarget.Target = scMeasureTargetModel.Target;

                try
                {
                    _scMeasureTargetService.UpdateAsync(scMeasureTarget).Wait();
                }
                catch (Exception ex)
                {
                    return(Content(ex.Message));
                }
            }

            else
            {
                Supplier supplier = _supplierService.GetByIdAsync(supplierId).Result;
                if (supplier == null)
                {
                    return(Content("Supplier not existed."));
                }

                ScMeasure scMeasure = _scMeasureService.GetByIdAsync(scMeasureTargetModel.ScMeasureId).Result;
                if (scMeasure == null)
                {
                    return(Content("ScMeasure not exiested."));
                }

                ScMeasureTarget existedScMeasureTarget = _scMeasureTargetService.GetScMeasureTargetBySupplierIdAndScMeasureId(supplierId, scMeasureTargetModel.ScMeasureId).Result;
                if (existedScMeasureTarget != null)
                {
                    return(Content("This ScMeasureTarget has existed."));
                }

                ScMeasureTarget scMeasureTarget = new ScMeasureTarget();
                scMeasureTarget.ScMeasureId = scMeasureTargetModel.ScMeasureId;
                scMeasureTarget.SupplierId  = supplierId;
                scMeasureTarget.Target      = scMeasureTargetModel.Target;

                try
                {
                    _scMeasureTargetService.InsertAsync(scMeasureTarget).Wait();
                }
                catch (Exception ex)
                {
                    return(Content(ex.Message));
                }
            }

            return(Json(new { status = "success" }));
        }