324 lines
12 KiB
VB.net

Imports DevExpress.Utils.Extensions
Imports DevExpress.XtraSplashScreen
Imports DevExpress.XtraCharts
Imports DigitalData.GUIs.Common
Imports DigitalData.Modules.Base
Imports DigitalData.Modules.Config
Imports DigitalData.Modules.Database
Imports DigitalData.Modules.Logging
Imports EnvelopeGenerator.Common
Imports EnvelopeGenerator.Common.My
Imports DevExpress.Charts.Native
Imports DevExpress.XtraGrid.Views.Grid
Imports DevExpress.XtraGrid
Public Class frmMain
Private LogConfig As LogConfig
Private Logger As Logger
Private Database As MSSQLServer
Private ConfigManager As ConfigManager(Of Config)
Private TempFiles As TempFiles
Private FormHelper As FormHelper
Private GridBuilder As GridBuilder
Private RefreshHelper As RefreshHelper
Private State As State
Private Controller As EnvelopeListController
Private Sub frmMain_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim oLogPath = IO.Path.Combine(Application.LocalUserAppDataPath, "Log")
LogConfig = New LogConfig(LogConfig.PathType.CustomPath, oLogPath, CompanyName:="Digital Data", ProductName:="Envelope Generator")
Logger = LogConfig.GetLogger()
FormHelper = New FormHelper(LogConfig, Me)
TempFiles = New TempFiles(LogConfig)
TempFiles.Create()
RefreshHelper = New RefreshHelper(ViewEnvelopes, "Id")
Try
ConfigManager = New ConfigManager(Of Config)(LogConfig, Application.UserAppDataPath)
If ConfigManager.Config.ConnectionString = String.Empty Then
Dim oSQLConfig As New frmSQLConfig(LogConfig)
If oSQLConfig.ShowDialog() = DialogResult.OK Then
ConfigManager.Config.ConnectionString = oSQLConfig.ConnectionString
ConfigManager.Save()
Application.Restart()
Else
FormHelper.ShowErrorMessage(New ApplicationException("No Database configured. Application will close!"), "Form Load")
Application.Exit()
End If
End If
Dim oConnectionString = MSSQLServer.DecryptConnectionString(ConfigManager.Config.ConnectionString)
Database = New MSSQLServer(LogConfig, oConnectionString)
State = New State With {
.UserId = 0,
.Config = ConfigManager.Config,
.DbConfig = New DbConfig(),
.LogConfig = LogConfig,
.Database = Database
}
If Database.DBInitialized = True Then
Dim ConfigModel = New ConfigModel(State)
State.DbConfig = ConfigModel.LoadConfiguration()
State.UserId = ConfigModel.GetUserId()
End If
If Not String.IsNullOrEmpty(State.DbConfig.ExternalProgramName) Then
Me.Text = State.DbConfig.ExternalProgramName
End If
Controller = New EnvelopeListController(State)
LoadEnvelopeData()
Catch ex As Exception
Logger.Error(ex)
End Try
End Sub
Private Sub LoadEnvelopeData()
RefreshHelper.SaveViewInfo()
LoadEnvelopes()
LoadCompletedEnvelopes()
RefreshHelper.LoadViewInfo()
'LoadCharts()
txtRefreshLabel.Caption = String.Format(txtRefreshLabel.Tag, Now)
End Sub
Private Sub LoadEnvelopes()
GridBuilder = New GridBuilder(ViewEnvelopes)
GridBuilder.SetDefaults(ViewEnvelopes)
GridBuilder.SetReadOnlyOptions(ViewEnvelopes)
GridEnvelopes.DataSource = Controller.ListEnvelopes()
End Sub
Private Sub LoadCompletedEnvelopes()
GridBuilder = New GridBuilder(ViewCompleted)
GridBuilder.SetDefaults(ViewCompleted)
GridBuilder.SetReadOnlyOptions(ViewCompleted)
GridCompleted.DataSource = Controller.ListCompleted()
End Sub
Private Sub LoadCharts()
Dim oChartControl As ChartControl = Controller.GetPieChart()
Me.SplitContainerControl1.Panel2.AddControl(oChartControl)
End Sub
Private Sub btnCreateEnvelope_ItemClick(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles btnCreateEnvelope.ItemClick
Dim oHandle = SplashScreenManager.ShowOverlayForm(Me)
Try
Dim oForm As New frmEnvelopeEditor() With {.State = State}
oForm.ShowDialog()
LoadEnvelopeData()
Catch ex As Exception
Logger.Error(ex)
Finally
SplashScreenManager.CloseOverlayForm(oHandle)
End Try
End Sub
Private Sub btnEditEnvelope_ItemClick(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles btnEditEnvelope.ItemClick
Dim oSelectedRows = ViewEnvelopes.GetSelectedRows()
If oSelectedRows.Count > 0 Then
LoadEnvelope(oSelectedRows.First)
End If
End Sub
Private Sub LoadEnvelope(pRowHandle As Integer)
Dim oHandle = SplashScreenManager.ShowOverlayForm(Me)
Try
Dim oEnvelope As Envelope = DirectCast(ViewEnvelopes.GetRow(pRowHandle), Envelope)
If oEnvelope.IsAlreadySent Then
Exit Sub
End If
Dim oForm As New frmEnvelopeEditor() With {.State = State, .Envelope = oEnvelope}
oForm.ShowDialog()
LoadEnvelopeData()
Catch ex As Exception
Logger.Error(ex)
Finally
SplashScreenManager.CloseOverlayForm(oHandle)
End Try
End Sub
Private Sub DeleteEnvelope(pRowHandle As Integer)
Dim oEnvelope As Envelope = ViewEnvelopes.GetRow(pRowHandle)
If MsgBox(Resources.Envelope.Do_you_really_want_to_delete_this_envelope, MsgBoxStyle.Question Or MsgBoxStyle.YesNo, Text) = MsgBoxResult.No Then
Exit Sub
End If
If Controller.DeleteEnvelope(oEnvelope) Then
LoadEnvelopeData()
Else
MsgBox(Resources.Envelope.The_envelope_could_not_be_deleted, MsgBoxStyle.Critical, Text)
End If
End Sub
Private Sub ViewEnvelopes_DoubleClick(sender As Object, e As EventArgs) Handles ViewEnvelopes.DoubleClick
Dim oSelectedRows = ViewEnvelopes.GetSelectedRows()
If oSelectedRows.Count > 0 Then
LoadEnvelope(oSelectedRows.First)
End If
End Sub
Private Sub btnDeleteEnvelope_ItemClick_1(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles btnDeleteEnvelope.ItemClick
Dim oSelectedRows = ViewEnvelopes.GetSelectedRows()
If oSelectedRows.Count > 0 Then
DeleteEnvelope(oSelectedRows.First)
End If
End Sub
Private Sub frmMain_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
' Cleanup Methods
TempFiles.CleanUp()
End Sub
Private Sub XtraTabControl1_SelectedPageChanged(sender As Object, e As DevExpress.XtraTab.TabPageChangedEventArgs) Handles XtraTabControl1.SelectedPageChanged
Select Case XtraTabControl1.SelectedTabPageIndex
Case 1
btnEditEnvelope.Enabled = False
btnDeleteEnvelope.Enabled = False
Case 0
btnEditEnvelope.Enabled = True
btnDeleteEnvelope.Enabled = True
End Select
End Sub
Private Sub BarButtonItem1_ItemClick(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItem1.ItemClick
LoadEnvelopeData()
End Sub
Private Sub ViewEnvelopes_FocusedRowChanged(sender As Object, e As DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs) Handles ViewEnvelopes.FocusedRowChanged
If ViewEnvelopes.FocusedRowHandle < 0 Then
Exit Sub
End If
Dim oEnvelope As Envelope = ViewEnvelopes.GetRow(ViewEnvelopes.FocusedRowHandle)
If oEnvelope.IsAlreadySent Then
btnEditEnvelope.Enabled = False
Else
btnEditEnvelope.Enabled = True
End If
End Sub
Private Sub ViewEnvelopes_CustomDrawCell(sender As Object, e As DevExpress.XtraGrid.Views.Base.RowCellCustomDrawEventArgs) Handles ViewEnvelopes.CustomDrawCell
If e.RowHandle < 0 Then
Exit Sub
End If
Dim oEnvelope As Envelope = ViewEnvelopes.GetRow(e.RowHandle)
If oEnvelope.Status = Common.Constants.EnvelopeStatus.EnvelopePartlySigned Then
e.Appearance.BackColor = ColorTranslator.FromHtml(Common.Constants.GREEN_300)
End If
If oEnvelope.Status = Common.Constants.EnvelopeStatus.EnvelopeQueued Or oEnvelope.Status = Common.Constants.EnvelopeStatus.EnvelopeSent Then
e.Appearance.BackColor = ColorTranslator.FromHtml(Common.Constants.ORANGE_300)
End If
End Sub
Private Sub ViewReceivers_CustomDrawCell(sender As Object, e As DevExpress.XtraGrid.Views.Base.RowCellCustomDrawEventArgs) Handles ViewReceivers.CustomDrawCell
If e.RowHandle < 0 Then
Exit Sub
End If
Dim oView As GridView = DirectCast(sender, GridView)
Dim oReceiver As EnvelopeReceiver = oView.GetRow(e.RowHandle)
If (oReceiver Is Nothing) Then
Exit Sub
End If
If oReceiver.Status = Common.Constants.ReceiverStatus.Signed Then
e.Appearance.BackColor = ColorTranslator.FromHtml(Common.Constants.GREEN_300)
Else
e.Appearance.BackColor = ColorTranslator.FromHtml(Common.Constants.RED_300)
End If
End Sub
Private Sub ViewCompleted_CustomDrawCell(sender As Object, e As Views.Base.RowCellCustomDrawEventArgs) Handles ViewCompleted.CustomDrawCell
If e.RowHandle < 0 Then
Exit Sub
End If
Dim oEnvelope As Envelope = ViewCompleted.GetRow(e.RowHandle)
If oEnvelope.Status = Common.Constants.EnvelopeStatus.EnvelopeCompletelySigned Then
e.Appearance.BackColor = ColorTranslator.FromHtml(Common.Constants.GREEN_300)
End If
If oEnvelope.Status = Common.Constants.EnvelopeStatus.EnvelopeDeleted Then
e.Appearance.BackColor = ColorTranslator.FromHtml(Common.Constants.RED_300)
End If
End Sub
Private Sub ViewReceiversCompleted_CustomDrawCell(sender As Object, e As Views.Base.RowCellCustomDrawEventArgs) Handles ViewReceiversCompleted.CustomDrawCell
If e.RowHandle < 0 Then
Exit Sub
End If
Dim oView As GridView = DirectCast(sender, GridView)
Dim oReceiver As EnvelopeReceiver = oView.GetRow(e.RowHandle)
If (oReceiver Is Nothing) Then
Exit Sub
End If
If oReceiver.Status = Common.Constants.ReceiverStatus.Signed Then
e.Appearance.BackColor = ColorTranslator.FromHtml(Common.Constants.GREEN_300)
Else
e.Appearance.BackColor = ColorTranslator.FromHtml(Common.Constants.RED_300)
End If
End Sub
Private Sub RefreshTimer_Tick(sender As Object, e As EventArgs) Handles RefreshTimer.Tick
If Application.OpenForms.OfType(Of frmEnvelopeEditor).Any = False Then
LoadEnvelopeData()
End If
End Sub
Private Sub btnContactReceiver_ItemClick(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles btnContactReceiver.ItemClick
Dim oView As GridView = GridEnvelopes.FocusedView
If oView.Name = ViewReceivers.Name Then
Dim oReceiver As EnvelopeReceiver = oView.GetRow(oView.FocusedRowHandle)
Process.Start($"mailto:{oReceiver.Email}")
Else
MsgBox("Bitte wählen Sie einen Empfänger aus dem Reiter Empfänger aus.", MsgBoxStyle.Information, Text)
End If
End Sub
Private Sub btnShowDocument_ItemClick(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles btnShowDocument.ItemClick
If ViewEnvelopes.FocusedRowHandle < 0 Then
Exit Sub
End If
Dim oEnvelope As Envelope = ViewEnvelopes.GetRow(ViewEnvelopes.FocusedRowHandle)
Dim oDocument = oEnvelope.Documents.FirstOrDefault()
If oDocument Is Nothing Then
MsgBox("Der Umschlag enthält keine Dokumente.", MsgBoxStyle.Exclamation, Text)
Exit Sub
End If
Try
Process.Start(oDocument.Filepath)
Catch ex As Exception
MsgBox("Dokument konnte nicht geöffnet werden!", MsgBoxStyle.Critical, Text)
Logger.Error(ex)
End Try
End Sub
End Class