edit
This commit is contained in:
@@ -1,4 +1,6 @@
|
||||
using System;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Linq;
|
||||
using System.Windows.Input;
|
||||
|
||||
namespace XLAB2
|
||||
@@ -6,13 +8,25 @@ namespace XLAB2
|
||||
internal sealed class CreateDocumentWindowViewModel : ObservableObject
|
||||
{
|
||||
private DateTime? _acceptedOn;
|
||||
private string _documentNumber;
|
||||
private string _documentSequenceNumber;
|
||||
private GroupOption _selectedAccountingBook;
|
||||
private GroupOption _selectedDocumentType;
|
||||
private string _validationMessage;
|
||||
|
||||
public CreateDocumentWindowViewModel(DocumentEditorResult seed)
|
||||
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;
|
||||
_documentNumber = seed != null ? seed.DocumentNumber : string.Empty;
|
||||
_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);
|
||||
@@ -23,17 +37,71 @@ namespace XLAB2
|
||||
public DateTime? AcceptedOn
|
||||
{
|
||||
get { return _acceptedOn; }
|
||||
set { SetProperty(ref _acceptedOn, value); }
|
||||
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 _documentNumber; }
|
||||
set { SetProperty(ref _documentNumber, value); }
|
||||
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
|
||||
@@ -46,7 +114,12 @@ namespace XLAB2
|
||||
{
|
||||
return new DocumentEditorResult
|
||||
{
|
||||
DocumentNumber = DocumentNumber == null ? string.Empty : DocumentNumber.Trim(),
|
||||
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
|
||||
};
|
||||
@@ -57,11 +130,31 @@ namespace XLAB2
|
||||
RaiseCloseRequested(false);
|
||||
}
|
||||
|
||||
private void ClearValidationMessage()
|
||||
{
|
||||
if (!string.IsNullOrEmpty(ValidationMessage))
|
||||
{
|
||||
ValidationMessage = string.Empty;
|
||||
}
|
||||
}
|
||||
|
||||
private void Confirm(object parameter)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(DocumentNumber))
|
||||
if (SelectedDocumentType == null)
|
||||
{
|
||||
ValidationMessage = "Введите номер ПСВ.";
|
||||
ValidationMessage = "Выберите тип документа.";
|
||||
return;
|
||||
}
|
||||
|
||||
if (SelectedAccountingBook == null)
|
||||
{
|
||||
ValidationMessage = "Выберите книгу учета.";
|
||||
return;
|
||||
}
|
||||
|
||||
if (string.IsNullOrWhiteSpace(DocumentSequenceNumber))
|
||||
{
|
||||
ValidationMessage = "Введите номер документа.";
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -71,10 +164,42 @@ namespace XLAB2
|
||||
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;
|
||||
|
||||
Reference in New Issue
Block a user