public CanResponse GetCanEdit(Guid workResourceElementId, Guid projectResourceElementId, decimal work)
        {
            var response              = new CanResponse();
            var entitySchemaManager   = UserConnection.EntitySchemaManager;
            var esqWork               = new EntitySchemaQuery(entitySchemaManager, "WorkResourceElement");
            var projectIdColumn       = esqWork.AddColumn("Project.Id");
            var oldResourceIdColumn   = esqWork.AddColumn("ProjectResourceElement.Id");
            var oldContactIdColumn    = esqWork.AddColumn("ProjectResourceElement.Contact.Id");
            var oldPlanningWorkColumn = esqWork.AddColumn("PlanningWork");
            var workEntity            = esqWork.GetEntity(UserConnection, workResourceElementId);

            if (workEntity == null)
            {
                response.Success = false;
                response.Code    = 0;              // WorkResourceElement Not Exist
                return(response);
            }
            var projectId     = workEntity.GetTypedColumnValue <Guid>(projectIdColumn.Name);
            var oldContactId  = workEntity.GetTypedColumnValue <Guid>(oldContactIdColumn.Name);
            var oldResourceId = workEntity.GetTypedColumnValue <Guid>(oldResourceIdColumn.Name);
            var oldWork       = workEntity.GetTypedColumnValue <decimal>(oldPlanningWorkColumn.Name);

            if (projectResourceElementId == oldResourceId && work == oldWork)
            {
                return(response);
            }
            if (projectResourceElementId == oldResourceId)
            {
                var     childsWorkList = GetChildWorkByProjectId(projectId, projectResourceElementId);
                decimal childsWork     = 0m;
                if (childsWorkList.Count > 0)
                {
                    foreach (var item in childsWorkList)
                    {
                        childsWork += item.Value.PlanningWork;
                    }
                }
                if (childsWork > work)
                {
                    response.Success = false;
                    response.Code    = 2110;                  // work in WorkResourceElement too small
                    response.Plan    = childsWork;
                    return(response);
                }
            }
            else
            {
                response = GetCanDelete(projectId, oldResourceId, oldContactId);
                if (!response.Success)
                {
                    return(response);
                }
                response = GetCanCreate(workResourceElementId, projectResourceElementId, projectId);
                if (!response.Success)
                {
                    return(response);
                }
            }
            return(response);
        }
        public CanResponse GetCanCreate(Guid workResourceElementId, Guid projectResourceElementId, Guid projectId)
        {
            var response            = new CanResponse();
            var entitySchemaManager = UserConnection.EntitySchemaManager;
            var esqWork             = new EntitySchemaQuery(entitySchemaManager, "WorkResourceElement");
            var idColumn            = esqWork.AddColumn("Id");
            var projectFilter       = esqWork.CreateFilterWithParameters(FilterComparisonType.Equal,
                                                                         "Project", projectId);

            esqWork.Filters.Add(projectFilter);
            var resourceFilter = esqWork.CreateFilterWithParameters(FilterComparisonType.Equal,
                                                                    "ProjectResourceElement", projectResourceElementId);

            esqWork.Filters.Add(resourceFilter);
            var selfFilter = esqWork.CreateFilterWithParameters(FilterComparisonType.NotEqual,
                                                                "Id", workResourceElementId);

            esqWork.Filters.Add(selfFilter);
            var entities = esqWork.GetEntityCollection(UserConnection);

            if (entities.Count > 0)
            {
                response.Success = false;
                response.Code    = 1010;              // duplicate ProjectResourceElement in project
                return(response);
            }
            return(response);
        }
        private CanResponse GetCanDelete(Guid projectId, Guid resourceId, Guid contactId)
        {
            var response            = new CanResponse();
            var entitySchemaManager = UserConnection.EntitySchemaManager;
            var childsOldWorkList   = GetChildWorkByProjectId(projectId, resourceId, true);

            if (childsOldWorkList.Count > 0)
            {
                response.Success = false;
                response.Code    = 3010;              // WorkResourceElement has childs
                return(response);
            }
            if (contactId != null && contactId != Guid.Empty)
            {
                if (GetActivityByProjectAndContact(projectId, contactId))
                {
                    response.Success = false;
                    response.Code    = 3020;                  // WorkResourceElement has activities
                    return(response);
                }
            }
            return(response);
        }
        public CanResponse GetCanDelete(Guid workResourceElementId)
        {
            var response            = new CanResponse();
            var entitySchemaManager = UserConnection.EntitySchemaManager;
            var esqWork             = new EntitySchemaQuery(entitySchemaManager, "WorkResourceElement");
            var projectIdColumn     = esqWork.AddColumn("Project.Id");
            var oldResourceIdColumn = esqWork.AddColumn("ProjectResourceElement.Id");
            var oldContactIdColumn  = esqWork.AddColumn("ProjectResourceElement.Contact.Id");
            var workEntity          = esqWork.GetEntity(UserConnection, workResourceElementId);

            if (workEntity == null)
            {
                response.Success = false;
                response.Code    = 0;              // WorkResourceElement Not Exist
                return(response);
            }
            var projectId  = workEntity.GetTypedColumnValue <Guid>(projectIdColumn.Name);
            var contactId  = workEntity.GetTypedColumnValue <Guid>(oldContactIdColumn.Name);
            var resourceId = workEntity.GetTypedColumnValue <Guid>(oldResourceIdColumn.Name);

            response = GetCanDelete(projectId, resourceId, contactId);
            return(response);
        }