VerificationAssignmentActions.cs 3.5 KB
using System;
using System.Collections.Generic;
using System.Linq;
using Sungero.Core;
using Sungero.CoreEntities;
using DirRX.CaseArchiving.VerificationAssignment;

namespace DirRX.CaseArchiving.Client
{
  partial class VerificationAssignmentActions
  {
    public virtual void Terminate(Sungero.Workflow.Client.ExecuteResultActionArgs e)
    {
      var document = _obj.DocumentGroup.CaseInventories.Single();
      var error = CaseArchiving.PublicFunctions.CaseInventory.Remote.RemoveAllCaseFiles(document);
      if (!string.IsNullOrEmpty(error))
        e.AddError(error);
    }

    public virtual bool CanTerminate(Sungero.Workflow.Client.CanExecuteResultActionArgs e)
    {
      return true;
    }

    public virtual void Forward(Sungero.Workflow.Client.ExecuteResultActionArgs e)
    {
      if (_obj.Addressee == null)
      {
        e.AddError(VerificationTasks.Resources.CantRedirectWithoutAddressee);
        e.Cancel();
      }
      
      // Изменить ответственного в описи и в делах.
      var document = _obj.DocumentGroup.CaseInventories.SingleOrDefault();
      var error = CaseArchiving.PublicFunctions.CaseInventory.Remote.ChangeCaseFilesResponsible(document, _obj.Addressee);
      if (!string.IsNullOrEmpty(error))
      {
        e.AddError(error);
        e.Cancel();
      }
    }

    public virtual bool CanForward(Sungero.Workflow.Client.CanExecuteResultActionArgs e)
    {
      return true;
    }

    public virtual void Complete(Sungero.Workflow.Client.ExecuteResultActionArgs e)
    {
      var document = _obj.DocumentGroup.CaseInventories.SingleOrDefault();
      
      // Проверить наличие документа.
      if (document == null || !document.HasVersions)
      {
        e.AddError(VerificationTasks.Resources.ErrorDocumentNotFound);
        return;
      }
      
      // Проверить тип текущего пользователя.
      var currentUser = Sungero.CoreEntities.Users.Current;
      if (currentUser == null || currentUser.IsSystem == true)
      {
        e.AddError(VerificationTasks.Resources.ErrorSystemUserIsNotAllowed);
        return;
      }
      
      // Подписать документ, если еще не подписан.
      if (!Signatures.Get(document.LastVersion).Where(s => s.Signatory.Equals(currentUser) && s.SignatureType == SignatureType.Endorsing).Any())
      {
        if (!Signatures.Endorse(document.LastVersion, VerificationTasks.Resources.EndorseNote))
        {
          e.AddError(VerificationTasks.Resources.ErrorUnableToSign);
          return;
        }
      }
      
       // Обновить жизненный цикл описи в зависимости от ее вида.
      var isElectronic = document.DocumentKind != null && PublicFunctions.Module.Remote.GetCaseInvenoryDocumentKinds().Contains(document.DocumentKind);
      document.VerificationState = CaseInventory.VerificationState.Completed;
      document.InternalApprovalState = isElectronic ? CaseInventory.InternalApprovalState.PendingSign : CaseInventory.InternalApprovalState.OnApproval;      
      
      if (document.State.IsChanged)
        document.Save(); 
      
      // Отправить на согласование по регламенту.
      PublicFunctions.Module.SendCaseInventoryForApproving(document, _obj.Performer);
    }

    public virtual bool CanComplete(Sungero.Workflow.Client.CanExecuteResultActionArgs e)
    {
      return true;
    }

  }

}