MultiTool/MultiTool.Form/frmRowEditor.vb
2021-11-11 10:55:50 +01:00

252 lines
10 KiB
VB.net

Imports DevExpress.XtraGrid.Columns
Imports DevExpress.XtraGrid.Views.Grid
Imports DevExpress.XtraVerticalGrid.Rows
Imports MultiTool.Shared.Documents
Imports MultiTool.Shared.Documents.DocumentRow
Imports MultiTool.Shared
Imports DevExpress.XtraEditors.Repository
Imports MultiTool.Shared.Winline
Imports DigitalData.Modules.Language
Imports DevExpress.XtraEditors.Controls
Imports System.Globalization
Imports DevExpress.XtraEditors
Imports DigitalData.Modules.Logging
Public Class frmRowEditor
Private ReadOnly LogConfig As LogConfig
Private ReadOnly Logger As Logger
Private ReadOnly FormHelper As FormHelper
Private ReadOnly _Columns As List(Of String)
Private ReadOnly _DataTable As New DataTable
Private ReadOnly _Accounts As List(Of Account)
Private ReadOnly _Articles As List(Of Article)
Private ReadOnly _DocumentKinds As List(Of DocumentKind)
Private ReadOnly _DocumentRow As DocumentRow
Private ReadOnly MultilineEditor As New RepositoryItemMemoEdit
Private ReadOnly AccountPicker As New RepositoryItemSearchLookUpEdit
Private ReadOnly ArticlePicker As New RepositoryItemSearchLookUpEdit
Private ReadOnly DocumentKindPicker As New RepositoryItemSearchLookUpEdit
Private ReadOnly ReadOnlyEditor As New RepositoryItemTextEdit
'Private ReadOnly MaskDateEditor As New RepositoryItemTextEdit
'Private ReadOnly DatePicker As New RepositoryItemDateEdit
Private Const COL_KEY = "KEY"
Private Const COL_VALUE_ORIGINAL = "VALUE_ORIGINAL"
Private Const COL_VALUE_EXTERNAL = "VALUE_EXTERNAL"
Private Const COL_VALUE_FINAL = "VALUE_FINAL"
Public ReadOnly Property DocumentRow As DocumentRow
Get
Return _DocumentRow
End Get
End Property
Public Sub New(pLogConfig As LogConfig, pColumns As List(Of String), pDocumentRow As DocumentRow, pMandator As Mandator, pWinline As Data)
' Dieser Aufruf ist für den Designer erforderlich.
InitializeComponent()
LogConfig = pLogConfig
Logger = pLogConfig.GetLogger()
FormHelper = New FormHelper(pLogConfig)
_Columns = pColumns
_Accounts = pWinline.Accounts.Where(Function(a) a.Mandator.Id = pMandator.Id).ToList()
_Articles = pWinline.Articles.Where(Function(a) a.Mandator.Id = pMandator.Id).ToList()
_DocumentKinds = pWinline.DocumentKinds.Where(Function(k) k.Mandator.Id = pMandator.Id).ToList()
_DocumentRow = pDocumentRow
AccountPicker.DataSource = _Accounts
AccountPicker.DisplayMember = "Name"
AccountPicker.ValueMember = "Id"
ArticlePicker.DataSource = _Articles
ArticlePicker.DisplayMember = "Description"
ArticlePicker.ValueMember = "Id"
DocumentKindPicker.DataSource = _DocumentKinds
DocumentKindPicker.ValueMember = "Id"
DocumentKindPicker.DisplayMember = "Name"
ReadOnlyEditor.ReadOnly = True
'DatePicker.CalendarTimeEditing = DevExpress.Utils.DefaultBoolean.False
'DatePicker.EditMask = "yyyy-MM-dd"
'DatePicker.Mask.MaskType = DevExpress.XtraEditors.Mask.MaskType.DateTimeAdvancingCaret
'DatePicker.DisplayFormat.FormatString = "yyyy-MM-dd"
' DatePicker.DisplayFormat.FormatType = DevExpress.Utils.FormatType.DateTime
'DatePicker.DisplayFormat.FormatString = "d"
'DatePicker.EditFormat.FormatString = "yyyy-MM-dd"
'DatePicker.EditFormat.FormatType = DevExpress.Utils.FormatType.DateTime
'DatePicker.EditFormat.FormatString = "d"
'DatePicker.Mask.UseMaskAsDisplayFormat = True
'DatePicker.Mask.MaskType = DevExpress.XtraEditors.Mask.MaskType.DateTime
' DatePicker.Mask.EditMask = "yyyy-MM-dd"
'AddHandler DatePicker.ParseEditValue, AddressOf DatePicker_ParseEditValue
'MaskDateEditor.Mask.EditMask = "yyyy-MM-dd"
'MaskDateEditor.Mask.MaskType = DevExpress.XtraEditors.Mask.MaskType.Simple
End Sub
Private Sub DatePicker_ParseEditValue(sender As Object, e As ConvertEditValueEventArgs)
If TypeOf sender Is RepositoryItemDateEdit Then
Debug.WriteLine($"RepoItem # {e.Value} # {e.Value.GetType.Name}")
ElseIf TypeOf sender Is DateEdit Then
Debug.WriteLine($"DateEdit # {e.Value} # {e.Value.GetType.Name}")
End If
End Sub
Private Sub frmRowEditor_Load(sender As Object, e As EventArgs) Handles Me.Load
Dim oDict = New Dictionary(Of String, FieldValue)
For Each oColumn As String In _Columns
Dim oField = _DocumentRow.Fields.
Where(Function(f) f.Key = oColumn).
SingleOrDefault()
If oField.Value Is Nothing Then
oDict.Add(oColumn, New FieldValue())
Else
oDict.Add(oColumn, oField.Value)
End If
Next
_DataTable.Columns.Clear()
_DataTable.Columns.Add(COL_KEY)
_DataTable.Columns.Add(COL_VALUE_ORIGINAL)
_DataTable.Columns.Add(COL_VALUE_EXTERNAL)
_DataTable.Columns.Add(COL_VALUE_FINAL)
For Each oKV In oDict
_DataTable.Rows.Add(oKV.Key, oKV.Value.Original, oKV.Value.External, oKV.Value.Final)
Next
GridControl1.DataSource = _DataTable
End Sub
Private Sub btnSave_ItemClick(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles btnSave.ItemClick
Try
GridView1.CloseEditor()
For Each oRow As DataRow In _DataTable.Rows
Dim oField = _DocumentRow.Fields.
Where(Function(f) f.Key = oRow.Item(COL_KEY)).
SingleOrDefault()
If oField.Key Is Nothing Then
oField = New KeyValuePair(Of String, FieldValue)(oRow.Item(COL_KEY), New FieldValue())
End If
Dim oFieldValue As FieldValue = oField.Value
Dim oGridValue As String = Utils.NotNull(oRow.Item(COL_VALUE_FINAL), String.Empty)
If oField.Key = "Datum_Auftrag-Bestellung" And oGridValue.Length > 0 Then
If TryParseDate(oGridValue) Is Nothing Then
MsgBox(String.Format(My.Resources.frmRowEditorExtra.Datumswert_für___0___enthält_einen_ungüligen_Wert, oField.Key), MsgBoxStyle.Exclamation, Text)
Exit Sub
End If
End If
If Not oFieldValue.Final.Equals(oGridValue) Then
Dim oValue = Utils.NotNull(oRow.Item(COL_VALUE_FINAL), String.Empty).ToString
oFieldValue.Final = oValue.Trim()
If _DocumentRow.Fields.ContainsKey(oField.Key) Then
_DocumentRow.Fields.Item(oField.Key) = oFieldValue
Else
_DocumentRow.Fields.Add(oField.Key, oFieldValue)
End If
End If
Next
DialogResult = DialogResult.OK
Close()
Catch ex As Exception
MsgBox("")
Logger.Error(ex)
End Try
End Sub
Private Function TryParseDate(pValue As String)
Try
Return Date.ParseExact(pValue, "yyyy-MM-dd", CultureInfo.InvariantCulture)
Catch ex As Exception
Return Nothing
End Try
End Function
Private Sub btnApplyFromWinline_ItemClick(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles btnApplyFromWinline.ItemClick
CopyValueToFinalColumn(COL_VALUE_EXTERNAL)
End Sub
Private Sub btnApplyFromOriginal_ItemClick(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles btnApplyFromOriginal.ItemClick
CopyValueToFinalColumn(COL_VALUE_ORIGINAL)
End Sub
Private Sub CopyValueToFinalColumn(pSourceColumnName As String)
Dim oSelectedRow As DataRow = GridView1.GetDataRow(GridView1.FocusedRowHandle)
If oSelectedRow Is Nothing Then
Exit Sub
End If
oSelectedRow.Item(COL_VALUE_FINAL) = oSelectedRow.Item(pSourceColumnName)
oSelectedRow.AcceptChanges()
GridView1.CloseEditor()
End Sub
Private Sub GridView1_CustomRowCellEdit(sender As Object, e As CustomRowCellEditEventArgs) Handles GridView1.CustomRowCellEdit
Dim oDataRow As DataRow = GridView1.GetDataRow(e.RowHandle)
If e.Column.FieldName = COL_VALUE_ORIGINAL Or e.Column.FieldName = COL_VALUE_FINAL Then
If e.CellValue.ToString.Length > 100 Then
e.RepositoryItem = MultilineEditor
End If
End If
If e.Column.FieldName = COL_VALUE_FINAL Then
Dim oReadOnlyFields As New List(Of String) From {
"BELEGKEY"
}
If oReadOnlyFields.Contains(oDataRow.Item(COL_KEY)) Then
e.RepositoryItem = ReadOnlyEditor
ElseIf oDataRow.Item(COL_KEY) = "Datum_Auftrag-Bestellung" Then
'e.RepositoryItem = DatePicker
'e.RepositoryItem = MaskDateEditor
ElseIf e.CellValue.ToString.Length > 30 Then
e.RepositoryItem = MultilineEditor
ElseIf oDataRow.Item(COL_KEY) = "Fakt_Kontonummer" Or oDataRow.Item(COL_KEY) = "Lief_Kontonummer" Then
e.RepositoryItem = AccountPicker
ElseIf oDataRow.Item(COL_KEY) = "Artikelnummer" Then
e.RepositoryItem = ArticlePicker
ElseIf oDataRow.Item(COL_KEY) = "Belegart" Then
e.RepositoryItem = DocumentKindPicker
End If
End If
End Sub
Private Sub GridView1_CustomDrawCell(sender As Object, e As DevExpress.XtraGrid.Views.Base.RowCellCustomDrawEventArgs) Handles GridView1.CustomDrawCell
Dim oDataRow As DataRow = GridView1.GetDataRow(e.RowHandle)
If e.Column.FieldName = COL_VALUE_FINAL Then
Dim oReadOnlyFields As New List(Of String) From {
"BELEGKEY"
}
If oReadOnlyFields.Contains(oDataRow.Item(COL_KEY)) Then
e.Appearance.BackColor = Color.LightGray
End If
End If
End Sub
End Class