213 lines
7.0 KiB
C#
213 lines
7.0 KiB
C#
using System;
|
|
using System.Collections.ObjectModel;
|
|
using System.Linq;
|
|
using System.Windows.Input;
|
|
|
|
namespace XLAB2
|
|
{
|
|
internal sealed class CreateDocumentWindowViewModel : ObservableObject
|
|
{
|
|
private DateTime? _acceptedOn;
|
|
private string _documentSequenceNumber;
|
|
private GroupOption _selectedAccountingBook;
|
|
private GroupOption _selectedDocumentType;
|
|
private string _validationMessage;
|
|
|
|
public CreateDocumentWindowViewModel(DocumentEditorResult seed, DocumentNumberDirectory directory)
|
|
{
|
|
if (directory == null)
|
|
{
|
|
throw new ArgumentNullException("directory");
|
|
}
|
|
|
|
AccountingBooks = new ObservableCollection<GroupOption>(directory.AccountingBooks ?? Array.Empty<GroupOption>());
|
|
DocumentTypes = new ObservableCollection<GroupOption>(directory.DocumentTypes ?? Array.Empty<GroupOption>());
|
|
|
|
_acceptedOn = seed != null ? seed.AcceptedOn : DateTime.Today;
|
|
_documentSequenceNumber = seed == null ? string.Empty : seed.DocumentSequenceNumber ?? string.Empty;
|
|
_selectedAccountingBook = ResolveSelectedOption(AccountingBooks, seed == null ? null : seed.AccountingBookKey);
|
|
_selectedDocumentType = ResolveSelectedOption(DocumentTypes, seed == null ? null : seed.DocumentTypeKey);
|
|
|
|
ConfirmCommand = new RelayCommand(Confirm);
|
|
CancelCommand = new RelayCommand(Cancel);
|
|
}
|
|
|
|
public event EventHandler<bool?> CloseRequested;
|
|
|
|
public DateTime? AcceptedOn
|
|
{
|
|
get { return _acceptedOn; }
|
|
set
|
|
{
|
|
if (SetProperty(ref _acceptedOn, value))
|
|
{
|
|
ClearValidationMessage();
|
|
}
|
|
}
|
|
}
|
|
|
|
public ObservableCollection<GroupOption> AccountingBooks { get; private set; }
|
|
|
|
public ICommand CancelCommand { get; private set; }
|
|
|
|
public ICommand ConfirmCommand { get; private set; }
|
|
|
|
public string DocumentNumber
|
|
{
|
|
get
|
|
{
|
|
return DocumentNumberFormatter.Build(
|
|
SelectedDocumentType == null ? null : SelectedDocumentType.Title,
|
|
SelectedAccountingBook == null ? null : SelectedAccountingBook.Title,
|
|
DocumentSequenceNumber);
|
|
}
|
|
}
|
|
|
|
public string DocumentSequenceNumber
|
|
{
|
|
get { return _documentSequenceNumber; }
|
|
set
|
|
{
|
|
if (SetProperty(ref _documentSequenceNumber, value))
|
|
{
|
|
ClearValidationMessage();
|
|
OnPropertyChanged("DocumentNumber");
|
|
}
|
|
}
|
|
}
|
|
|
|
public ObservableCollection<GroupOption> DocumentTypes { get; private set; }
|
|
|
|
public GroupOption SelectedAccountingBook
|
|
{
|
|
get { return _selectedAccountingBook; }
|
|
set
|
|
{
|
|
if (SetProperty(ref _selectedAccountingBook, value))
|
|
{
|
|
ClearValidationMessage();
|
|
OnPropertyChanged("DocumentNumber");
|
|
}
|
|
}
|
|
}
|
|
|
|
public GroupOption SelectedDocumentType
|
|
{
|
|
get { return _selectedDocumentType; }
|
|
set
|
|
{
|
|
if (SetProperty(ref _selectedDocumentType, value))
|
|
{
|
|
ClearValidationMessage();
|
|
OnPropertyChanged("DocumentNumber");
|
|
}
|
|
}
|
|
}
|
|
|
|
public string ValidationMessage
|
|
{
|
|
get { return _validationMessage; }
|
|
private set { SetProperty(ref _validationMessage, value); }
|
|
}
|
|
|
|
public DocumentEditorResult ToResult()
|
|
{
|
|
return new DocumentEditorResult
|
|
{
|
|
AccountingBookKey = SelectedAccountingBook == null ? string.Empty : SelectedAccountingBook.Key,
|
|
AccountingBookTitle = SelectedAccountingBook == null ? string.Empty : SelectedAccountingBook.Title,
|
|
DocumentNumber = DocumentNumber,
|
|
DocumentSequenceNumber = DocumentSequenceNumber == null ? string.Empty : DocumentSequenceNumber.Trim(),
|
|
DocumentTypeKey = SelectedDocumentType == null ? string.Empty : SelectedDocumentType.Key,
|
|
DocumentTypeTitle = SelectedDocumentType == null ? string.Empty : SelectedDocumentType.Title,
|
|
AcceptedOn = AcceptedOn.HasValue ? AcceptedOn.Value : DateTime.Today,
|
|
IssuedOn = null
|
|
};
|
|
}
|
|
|
|
private void Cancel(object parameter)
|
|
{
|
|
RaiseCloseRequested(false);
|
|
}
|
|
|
|
private void ClearValidationMessage()
|
|
{
|
|
if (!string.IsNullOrEmpty(ValidationMessage))
|
|
{
|
|
ValidationMessage = string.Empty;
|
|
}
|
|
}
|
|
|
|
private void Confirm(object parameter)
|
|
{
|
|
if (SelectedDocumentType == null)
|
|
{
|
|
ValidationMessage = "Выберите тип документа.";
|
|
return;
|
|
}
|
|
|
|
if (SelectedAccountingBook == null)
|
|
{
|
|
ValidationMessage = "Выберите книгу учета.";
|
|
return;
|
|
}
|
|
|
|
if (string.IsNullOrWhiteSpace(DocumentSequenceNumber))
|
|
{
|
|
ValidationMessage = "Введите номер документа.";
|
|
return;
|
|
}
|
|
|
|
if (!AcceptedOn.HasValue)
|
|
{
|
|
ValidationMessage = "Укажите дату приемки.";
|
|
return;
|
|
}
|
|
|
|
if (DocumentNumber.Length > DocumentNumberFormatter.MaxLength)
|
|
{
|
|
ValidationMessage = string.Format(
|
|
"Итоговый номер документа не должен превышать {0} символов.",
|
|
DocumentNumberFormatter.MaxLength);
|
|
return;
|
|
}
|
|
|
|
ValidationMessage = string.Empty;
|
|
RaiseCloseRequested(true);
|
|
}
|
|
|
|
private static GroupOption ResolveSelectedOption(ObservableCollection<GroupOption> items, string preferredKey)
|
|
{
|
|
if (items == null || items.Count == 0)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
if (!string.IsNullOrWhiteSpace(preferredKey))
|
|
{
|
|
var matchingItem = items.FirstOrDefault(delegate(GroupOption item)
|
|
{
|
|
return item != null
|
|
&& string.Equals(item.Key, preferredKey.Trim(), StringComparison.OrdinalIgnoreCase);
|
|
});
|
|
|
|
if (matchingItem != null)
|
|
{
|
|
return matchingItem;
|
|
}
|
|
}
|
|
|
|
return items[0];
|
|
}
|
|
|
|
private void RaiseCloseRequested(bool? dialogResult)
|
|
{
|
|
var handler = CloseRequested;
|
|
if (handler != null)
|
|
{
|
|
handler(this, dialogResult);
|
|
}
|
|
}
|
|
}
|
|
}
|