DestructionActServerFunctions.cs 2.99 KB
using System;
using System.Collections.Generic;
using System.Linq;
using Sungero.Core;
using Sungero.CoreEntities;
using DirRX.Storage.DestructionAct;

namespace DirRX.Storage.Server
{
  partial class DestructionActFunctions
  {    
    // Получить список дел акта.
    [Public, Remote(IsPure=true)]
    public virtual IQueryable<DirRX.LongTermArchive.ICaseFile> GetCaseFiles()
    {
      return DirRX.LongTermArchive.CaseFiles.GetAll(x => x.LTADestructionActDirRX != null && x.LTADestructionActDirRX.Equals(_obj));
    }
    
    /// <summary>
    /// Исключить все дела из акта.
    /// </summary>
    /// <returns>Сообщение об ошибке, либо пустая строка.</returns>
    [Public, Remote]
    public string RemoveAllCaseFiles()
    {
      try
      {
        foreach (var cf in PublicFunctions.DestructionAct.Remote.GetCaseFiles(_obj))
        {
          if (!DirRX.LongTermArchive.PublicFunctions.CaseFile.UpdateDestructionAct(cf, null, null))
          {
            throw new ApplicationException(string.Format("Не удалось исключить дело ИД {0}.", cf.Id));
          }
        }
        Functions.DestructionAct.UpdatePeriod(_obj);
        _obj.Save();
        return string.Empty;
      }
      catch (Exception e)
      {
        Logger.Debug(e.ToString());
        return e.Message;
      }
    }

    /// <summary>
    /// Сформировать тело документа из отчета.
    /// </summary>
    /// <returns>Версия документа.</returns>
    [Public]
    public virtual void GenerateLastVersionFromReport()
    {
      var report = Storage.Reports.GetDestructionActMainReport();
      report.Entity = _obj;
      
      if (!_obj.HasVersions)
        _obj.Versions.AddNew();
      
      var version = _obj.LastVersion;
      
      using (var reportMS = new System.IO.MemoryStream())
      {
        var internalReport = (Sungero.Reporting.Shared.IInternalReport)report;
        internalReport.InternalExecute(reportMS);
        version.Body.Write(reportMS);
      }

      version.Author = Users.Current;
      version.AssociatedApplication = Sungero.Content.AssociatedApplications.GetByExtension("docx");
      _obj.Save();
    }
    
    /// <summary>
    /// Получить следующий по порядку номер акта.
    /// </summary>
    /// <returns>Номер акта.</returns>
    public virtual int GetNextDocumentNumber()
    {
      var result = 1;
      AccessRights.AllowRead(
        () =>
        {
          // Предполагается, что все ключевые свойства заполнены (необходимо проверить до вызова).
          var lastDocument = DestructionActs.GetAll(x => x.Id != _obj.Id && x.DocumentNumber.HasValue).OrderByDescending(x => x.Id).FirstOrDefault();
          if (lastDocument != null)
            result += lastDocument.DocumentNumber.Value;
        });
            
      return result;
    }
    
  }
}