public static IApprovalRule CreateDefaultInvoiceRule(string ruleName, List <Enumeration> stages) { var documentFlow = Docflow.ApprovalRuleBase.DocumentFlow.Incoming; var hasNotDefaultRule = ApprovalRuleBases.GetAll().Any(r => r.IsDefaultRule != true); var hasDefaultRule = ApprovalRuleBases.GetAll().Any(r => r.DocumentFlow == documentFlow); if (hasNotDefaultRule || hasDefaultRule) { return(null); } var rule = Docflow.ApprovalRules.Create(); rule.Status = Sungero.Docflow.ApprovalRuleBase.Status.Active; rule.Name = ruleName; rule.DocumentFlow = documentFlow; rule.IsDefaultRule = true; rule.DocumentKinds.AddNew().DocumentKind = DocumentKinds.GetAll().Where(k => k.DocumentType.DocumentTypeGuid == IncomingInvoice.ClassTypeGuid.ToString()).FirstOrDefault(); Docflow.PublicInitializationFunctions.Module.SetRuleStages(rule, stages); // Создать этап передачи счета в бухгалтерию. var stageName = Sungero.Contracts.Resources.GiveInvoiceApprovalStageName; var stageAlreadyExists = Docflow.ApprovalStages.GetAll().Where(s => s.Name == stageName); var giveInvoiceStage = stageAlreadyExists.Any() ? stageAlreadyExists.FirstOrDefault() : ApprovalStages.Create(); if (!stageAlreadyExists.Any()) { giveInvoiceStage.StageType = StageType.SimpleAgr; giveInvoiceStage.DeadlineInDays = 1; giveInvoiceStage.ApprovalRoles.AddNew().ApprovalRole = Docflow.PublicFunctions.ApprovalRoleBase.GetRole(Docflow.ApprovalRoleBase.Type.Initiator); giveInvoiceStage.Subject = Resources.GiveInAccountant; giveInvoiceStage.Name = stageName; giveInvoiceStage.Save(); } // Добавить этап передачи в правило. rule.Stages.AddNew().Stage = giveInvoiceStage; Docflow.PublicFunctions.ApprovalRuleBase.CreateAutoTransitions(rule); rule.Save(); return(rule); }
public static void CreateApprovalRule(string name, List <Enumeration> stages) { // Проверить наличие правил НЕ по умолчанию. var hasNotDefaultRule = ApprovalRuleBases.GetAll().Any(r => r.IsDefaultRule != true); // Проверить, есть ли похожее правило: по умолчанию, внутренний документопоток, // в правиле указаны виды документов, все виды документов проектного типа. var hasDefaultRule = ApprovalRuleBases.GetAll().Any(r => r.IsDefaultRule == true && r.DocumentFlow == Sungero.Docflow.ApprovalRuleBase.DocumentFlow.Inner && r.DocumentKinds.Any() && !r.DocumentKinds.Any(dk => !Equals(dk.DocumentKind.DocumentType.DocumentTypeGuid, ProjectDocument.ClassTypeGuid.ToString()))); if (hasNotDefaultRule || hasDefaultRule) { return; } var projectDocumentKinds = Docflow.DocumentKinds.GetAll().Where(pdk => pdk.DocumentType.DocumentTypeGuid == ProjectDocument.ClassTypeGuid.ToString()).ToList(); var rule = ApprovalRules.Create(); rule.Status = Sungero.Docflow.ApprovalRuleBase.Status.Active; rule.Name = name; rule.DocumentFlow = Sungero.Docflow.ApprovalRuleBase.DocumentFlow.Inner; rule.IsDefaultRule = true; // Задать виды документов в правиле. if (projectDocumentKinds != null && !rule.DocumentKinds.Any()) { foreach (var documentKind in projectDocumentKinds) { rule.DocumentKinds.AddNew().DocumentKind = documentKind; } } // Проверить, есть ли этап согласования с РП. var projectManagerRole = Docflow.PublicFunctions.ApprovalRoleBase.GetRole(Docflow.ApprovalRoleBase.Type.ProjectManager); var projectManagerApprovingStage = ApprovalStages.GetAll().FirstOrDefault(s => s.StageType == Docflow.ApprovalStage.StageType.Approvers && s.ApprovalRoles.Any(ar => Equals(ar.ApprovalRole, projectManagerRole)) && s.ApprovalRoles.Count() == 1); // Создать этап согласования с РП, если это необходимо. var stage = ApprovalStages.Create(); if (projectManagerApprovingStage == null) { stage.StageType = Docflow.ApprovalStage.StageType.Approvers; stage.Name = Sungero.Projects.Resources.ApprovingByProjectManager; stage.DeadlineInDays = 1; stage.ApprovalRoles.AddNew().ApprovalRole = projectManagerRole; stage.Save(); } else { stage = projectManagerApprovingStage; } // Добавить стандартные этапы. Docflow.PublicInitializationFunctions.Module.SetRuleStages(rule, stages); // Заменяем созданный по умолчанию этап согласования с обязательными согласующими созданным/найденным ранее этапом. // Функция SetRuleStages при генерации подставляет первый попавшийся этап с нужным типом. var replacedDefaultStage = rule.Stages.FirstOrDefault(s => Equals(s.StageType, Docflow.ApprovalStage.StageType.Approvers)); if (replacedDefaultStage != null) { rule.Stages.Single(s => Equals(s, replacedDefaultStage)).Stage = stage; } // Создать связи по умолчанию. Docflow.PublicFunctions.ApprovalRuleBase.CreateAutoTransitions(rule); rule.Save(); }
public IQueryable <IApprovalRuleBase> GetApprovalRules() { return(ApprovalRuleBases.GetAll(r => r.Stages.Any(s => Equals(s.Stage, _obj)))); }
public static bool HasRules(IApprovalStage stage) { return(ApprovalRuleBases.GetAll(r => r.Stages.Any(s => s.Stage == stage)).Any()); }
public override void BeforeExecute(Sungero.Reporting.Server.BeforeExecuteEventArgs e) { #region Параметры var reportSessionId = System.Guid.NewGuid().ToString(); ApprovalRulesConsolidatedReport.ReportSessionId = reportSessionId; var includeSubsidiary = ApprovalRulesConsolidatedReport.IncludeSubsidiary == true; var filterDepartment = ApprovalRulesConsolidatedReport.Department; var filterBusinessUnit = ApprovalRulesConsolidatedReport.BusinessUnit; var filterDepartmentsForBusinessUnits = ApprovalRulesConsolidatedReport.FilterDepartmentsForBusinessUnits == true; var filterDocumentFlow = ApprovalRulesConsolidatedReport.DocumentFlow; var filterDocumentKind = ApprovalRulesConsolidatedReport.DocumentKind; var filterCategory = ApprovalRulesConsolidatedReport.Category; #endregion #region Подготовка начальных данных var localizedValueCash = new Dictionary <Sungero.Core.Enumeration, string>(); localizedValueCash[ApprovalRuleBase.Status.Active] = ApprovalRuleBases.Info.Properties.Status.GetLocalizedValue(ApprovalRuleBase.Status.Active); localizedValueCash[ApprovalRuleBase.Status.Draft] = ApprovalRuleBases.Info.Properties.Status.GetLocalizedValue(ApprovalRuleBase.Status.Draft); var documentFlows = new List <Enumeration>() { ApprovalRuleBase.DocumentFlow.Incoming, ApprovalRuleBase.DocumentFlow.Outgoing, ApprovalRuleBase.DocumentFlow.Inner, ApprovalRuleBase.DocumentFlow.Contracts }; foreach (var flow in documentFlows) { localizedValueCash[flow] = ApprovalRuleBases.Info.Properties.DocumentFlow.GetLocalizedValue(flow); } var tableData = new List <Structures.ApprovalRulesConsolidatedReport.TableLine>(); // Сформировать список активных видов документов, которые можно утверждать. // Чтобы не было перезапросов к СП, весь список хранить в памяти. var approvalAction = Functions.Module.GetSendAction(OfficialDocuments.Info.Actions.SendForApproval); var rules = ApprovalRuleBases.GetAll().Where(r => r.Status == ApprovalRuleBase.Status.Active || r.Status == ApprovalRuleBase.Status.Draft).ToList(); #endregion #region Фильтрация // НОР. List <Company.IBusinessUnit> businessUnits; if (filterBusinessUnit != null) { businessUnits = new List <Company.IBusinessUnit>() { filterBusinessUnit }; if (includeSubsidiary) { businessUnits = this.GetSubBusinessUnits(businessUnits, Company.BusinessUnits.GetAll().Where(u => u.Status == Status.Active).ToList()); } } else { businessUnits = Company.BusinessUnits.GetAll().Where(u => u.Status == Status.Active).ToList(); // Для подразделений без НОР. if (filterDepartmentsForBusinessUnits) { businessUnits.Add(null); } } // Подразделения. List <Company.IDepartment> departments; if (filterDepartment != null) { departments = new List <Company.IDepartment>() { filterDepartment }; if (includeSubsidiary) { departments = this.GetSubDepartments(departments, Company.Departments.GetAll().Where(u => u.Status == Status.Active).ToList()); } } else { departments = Company.Departments.GetAll().Where(u => u.Status == Status.Active).ToList(); } // Виды документов. List <IDocumentKind> documentKinds; if (filterDocumentKind != null) { documentKinds = new List <IDocumentKind>() { filterDocumentKind }; } else { documentKinds = DocumentKinds.GetAll() .Where(k => k.Status == DocumentKind.Status.Active && k.AvailableActions.Any(a => a.Action == approvalAction)) .ToList(); } // Категории договоров. List <IDocumentGroupBase> categories; if (filterCategory != null) { categories = new List <IDocumentGroupBase>() { filterCategory }; } else { categories = DocumentGroupBases.GetAll().Where(d => d.Status == Status.Active).OrderBy(d => d.Name).ToList(); } // Документопоток. if (!string.IsNullOrEmpty(filterDocumentFlow)) { documentFlows = documentFlows.Where(x => localizedValueCash[x] == filterDocumentFlow).ToList(); } #endregion var tableLines = new List <Structures.ApprovalRulesConsolidatedReport.TableLine>(); foreach (var unit in businessUnits) { var subDepartments = departments.Where(d => (!filterDepartmentsForBusinessUnits && unit != null) || Equals(d.BusinessUnit, unit)).OrderBy(d => d.Name).ToList(); if (!subDepartments.Any() && unit != null && filterDepartment == null) { subDepartments.Add(null); } foreach (var department in subDepartments) { foreach (var flow in documentFlows) { foreach (var kind in documentKinds.Where(k => Equals(k.DocumentFlow, flow))) { var subRules = rules .Where(r => r.DocumentFlow == flow && (!r.DocumentKinds.Any() || r.DocumentKinds.Any(k => Equals(k.DocumentKind, kind))) && (!r.BusinessUnits.Any() || r.BusinessUnits.Any(u => Equals(u.BusinessUnit, unit))) && (!r.Departments.Any() || r.Departments.Any(d => Equals(d.Department, department)))); var documentParentType = this.GetDocumentBaseTypeDisplayName(kind); // Отдельно обрабатываем виды документов, для которых настроены категории. var documentKindCategories = categories.Where(c => c.DocumentKinds.Any(d => Equals(d.DocumentKind, kind))); if (documentKindCategories.Any()) { foreach (var category in documentKindCategories) { var categorySubRules = subRules.Where(sr => !sr.DocumentGroups.Any() || sr.DocumentGroups.Any(c => Equals(c.DocumentGroup, category))); tableLines = this.CreateTableDataLines(categorySubRules, reportSessionId, unit, department, category, flow, kind.Name, documentParentType, localizedValueCash); tableData.AddRange(tableLines); } continue; } // Если выбрана фильтрация по категории, то в отчет попадают только записи с заполненной категорией. if (filterCategory == null) { // Если не найдено правила для доп. соглашений, тогда указываем, что используется правило договора. var isSupAgreement = kind.DocumentType.DocumentTypeGuid == "265f2c57-6a8a-4a15-833b-ca00e8047fa5"; if (isSupAgreement) { tableLines = this.CreateTableDataLines(subRules, reportSessionId, unit, department, null, flow, kind.Name, documentParentType, localizedValueCash); foreach (var tableLine in tableLines) { if (string.IsNullOrEmpty(tableLine.ApprovalRule)) { tableLine.ApprovalRule = Reports.Resources.ApprovalRulesConsolidatedReport.ContractualDocumentRule; } } tableData.AddRange(tableLines); continue; } tableLines = this.CreateTableDataLines(subRules, reportSessionId, unit, department, null, flow, kind.Name, documentParentType, localizedValueCash); tableData.AddRange(tableLines); } } } } } Functions.Module.WriteStructuresToTable(Constants.ApprovalRulesConsolidatedReport.SourceTableName, tableData); }
public static bool HasRules(IConditionBase condition) { return(ApprovalRuleBases.GetAll(r => r.Conditions.Any(s => Equals(s.Condition, condition)) && r.Status == Docflow.ApprovalRuleBase.Status.Active).Any()); }