ConsolidatedInventoryClientFunctions.cs 3.14 KB
using System;
using System.Collections.Generic;
using System.Linq;
using Sungero.Core;
using Sungero.CoreEntities;
using DirRX.Storage.ConsolidatedInventory;

namespace DirRX.Storage.Client
{
  partial class ConsolidatedInventoryFunctions
  {
    /// <summary>
    /// Отобразить диалог для включения выбранных дел в сводную опись.
    /// </summary>
    /// <returns>Сообщение об ошибке, либо пустая строка.</returns>
    [Public]
    public virtual string AddCaseFilesDialog()
    {
      // Получить список дел для включения в сводную опись.
      var isPaper = _obj.Type != DirRX.Storage.ConsolidatedInventory.Type.ElectronicPerm && _obj.Type != DirRX.Storage.ConsolidatedInventory.Type.ElectronicTemp;
      var query = PublicFunctions.Module.Remote.GetCaseFilesToInventory().Where(x => x.LTAIsPaperDirRX == isPaper);
      if (query.Any())
      {
        // Отобразить диалог выбора дел. Включить выбранные дела в опись.
        var dialog = Dialogs.CreateInputDialog(Resources.AddCaseFilesToInventoryDialogTitle);
        var caseFileSelector = dialog.AddSelectMany(Resources.CaseFilesLabel, true, DirRX.LongTermArchive.CaseFiles.Null)
          .From(query.ToArray());
        
        if (query.Count() == 1)
          caseFileSelector.Value = query.Take(1).AsEnumerable();
        
        var year = dialog.AddInteger(Resources.YearLabel, true, Calendar.Today.Year);
        
        dialog.SetOnRefresh((arg) =>
                            {
                              if (year.Value.HasValue)
                              {
                                var error = Storage.PublicFunctions.Module.Remote.LaterApprovedAnnualSectionsExists(_obj, year.Value.Value);
                                if (error != string.Empty)
                                {
                                  arg.AddError(error);
                                  return;
                                }
                                
                                var annualSection = Storage.Functions.Module.Remote.GetAnnualSection(_obj, year.Value.Value);
                                if (annualSection != null)
                                  arg.AddWarning(ConsolidatedInventories.Resources.ReformAnnualSectionMessageFormat(annualSection.Name));
                              }
                            });
        
        if (dialog.Show() == DialogButtons.Ok)
        {
          if (caseFileSelector.Value.Count() + _obj.CasesNumber > Constants.Module.MaxCaseFilesNumber)
            return ConsolidatedInventories.Resources.WarningCaseFilesTooMuch;
          
          foreach (var caseFile in caseFileSelector.Value)
            if (!DirRX.LongTermArchive.PublicFunctions.CaseFile.UpdateConsInventory(caseFile, _obj, year.Value, string.Empty))
              return ConsolidatedInventories.Resources.WarningCanNotUpdateCaseFileFormat(caseFile.DisplayValue);
        }
      }
      else
        return ConsolidatedInventories.Resources.WarningCaseFilesNotFound;

      return string.Empty;
    }

  }
}