Imports IntraSell_DLL
Imports IntraSell_Net_Base

Module FormManager

    Sub SucheNachNummer(Typ As String, Vorgangname As String)
        Try
            Dim varName = "SUCHE_NACH_NUMMER_" & Typ & "_" & MitarbeiterID
            Dim letzteNummer = VarValue_Default(varName, "")
            Dim Nummer = InputBox(Vorgangname & " Nummer:", "Suche nach Vorgangsnummer", letzteNummer)
            If Nummer = "" Then
                Exit Sub
            End If
            SetVarValue(varName, Nummer)
            OeffneVorgangByNummer(Typ, Nummer)
        Catch ex As Exception
            HandleAppError(ex)
        End Try
    End Sub

#Region "Vorgangliste"


    Sub OeffneVorgangListe()
        Try
            Main.Cursor = Cursors.WaitCursor
            VorgangListe.MdiParent = Main
            VorgangListe.Show()
            VorgangListe.LadeFormDaten()
            VorgangListe.BringToFront()
        Catch ex As Exception
            HandleAppError(ex)
        Finally
            Main.Cursor = Cursors.Default
        End Try
    End Sub

    Sub OeffneVorgangListe(IDNR As Integer, Optional Typ As String = Nothing, Optional abgeschlossen As Boolean = True)
        Try
            Main.Cursor = Cursors.WaitCursor
            Dim vorgangliste As New VorgangListe With {
                .SaveFilter = False,
                .MdiParent = Main
            }
            vorgangliste.Show()

            vorgangliste.LadeFormDaten()
            vorgangliste.AdressenControl1.IDNR = IDNR
            vorgangliste.DatumVon.Value = Now.AddDays(-365)
            If Typ IsNot Nothing Then
                vorgangliste.TypComboBox.SelectedValue = Typ
            End If

            vorgangliste.AbgeschlossenCheckBox.Checked = abgeschlossen


            vorgangliste.FilterButton.PerformClick()

        Catch ex As Exception
        Finally
            Main.Cursor = Cursors.Default

        End Try
    End Sub

#End Region

#Region "Vorgang"



    ''' <summary>
    ''' 
    ''' </summary>
    ''' <param name="VorgangTyp"></param>
    ''' <remarks></remarks>
    Public Sub BeginNewVorgang(VorgangTyp As String)
        Try
            Dim Vorgang = New Vorgang With {
                .MdiParent = Main
            }
            Vorgang.Show()
            Vorgang.BeginNew(VorgangTyp:=VorgangTyp)
        Catch ex As Exception
            HandleAppError(ex)
        End Try
    End Sub

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <param name="KundNr"></param>
    ''' <param name="VorgangTyp"></param>
    ''' <remarks></remarks>
    Public Function BeginNewVorgangFuerKunden(KundNr As Integer, VorgangTyp As String, Optional SetMDIParent As Boolean = True) As Vorgang
        Try
            Dim v As New Vorgang
            If SetMDIParent Then
                v.MdiParent = Main
            End If

            v.Show()
            v.BeginNewVorgangFuerKunden(addNew:=True, kundNr:=KundNr, vorgangTyp:=VorgangTyp, datum:=Date.Now)
            Return v
        Catch ex As Exception
            HandleAppError(ex)
        End Try
        Return Nothing
    End Function

    Function BeginNewVorgangFuerKundenOhneTyp(KundNr As Integer) As Vorgang
        Try
            Dim v As New Vorgang With {
                .MdiParent = Main
            }
            v.Show()
            v.BeginNew()
            Return v
        Catch ex As Exception
            HandleAppError(ex)
        End Try
        Return Nothing
    End Function

    ''' <summary>
    ''' Maske zum blättern öffnen
    ''' </summary>
    ''' <param name="IDNR"></param>
    ''' <param name="Typ"></param>
    ''' <remarks></remarks>
    Sub OeffneVorgangMaske(IDNR As Integer, Optional Typ As String = Nothing)
        OpenVorgangByIDNR(KundNr:=IDNR, VorgangTyp:=Typ)
    End Sub

#End Region


    Sub OffeneBestellungenListe()
        Try
            Main.Cursor = Cursors.WaitCursor
            TouchBestellung.MdiParent = Main
            TouchBestellung.Show()
            TouchBestellung.WindowState = FormWindowState.Maximized
            TouchBestellung.ZeigeListe(True)
        Catch ex As Exception
            HandleAppError(ex)
        Finally
            Main.Cursor = Cursors.Default
        End Try
    End Sub

#Region "Artikel"

    Function OeffneArtikelListe() As Artikelliste
        Try
            Dim f As New Artikelliste With {
                .MdiParent = Main
            }
            f.Show()
            Return f
        Catch ex As Exception
            HandleAppError(ex)
        End Try
        Return Nothing
    End Function

    Function OeffneArtikelMaskeNoFilter() As Artikel
        Try
            Dim f As New Artikel With {
                .MdiParent = Main
            }
            f.Show()
            Return f
        Catch ex As Exception
            HandleAppError(ex)
        End Try
        Return Nothing
    End Function

    Function OeffneArtikelMaskeQuickNoFilter() As ArtikelQuick2
        Try
            Dim f As New ArtikelQuick2 With {
                .MdiParent = Main
            }
            f.Show()
            Return f
        Catch ex As Exception
            HandleAppError(ex)
        End Try
        Return Nothing
    End Function

    Function OffneArtikelMaske(ArtNr As String) As Artikel
        Return OffneArtikelMaskeByFilter("ArtNr=" & ArtNr)
    End Function

    Function OffneArtikelMaskeByFilter(Filter As String, Optional SortBy As String = "EAN") As Artikel
        Try
            Main.Cursor = Cursors.WaitCursor
            Artikel.MdiParent = Main
            Artikel.Show()
            Artikel.FilterBy(Filter)
            Artikel.Sort(SortBy)
            Return Artikel
        Catch ex As Exception
            HandleAppError(ex)
        Finally
            Main.Cursor = Cursors.Default
        End Try
        Return Nothing
    End Function


    Sub OffneArtikelMaskeQuick(ArtNr As String)
        Try
            Main.Cursor = Cursors.WaitCursor
            'ArtikelQuick2.MdiParent = Main
            ArtikelQuick2.Show()
            ArtikelQuick2.FilterBy("ArtNr=" & ArtNr)
            ArtikelQuick2.BringToFront()
        Catch ex As Exception
            HandleAppError(ex)
        Finally
            Main.Cursor = Cursors.Default
        End Try
    End Sub

    Sub BeginNewArtikel()
        Try
            Main.Cursor = Cursors.WaitCursor
            'ArtikelQuick2.MdiParent = Main
            ArtikelQuick2.Show()
            ArtikelQuick2.BeginNew()
        Catch ex As Exception
            HandleAppError(ex)
        Finally
            Main.Cursor = Cursors.Default
        End Try
    End Sub

#End Region



    Sub OeffneKundenMaske(beginNew As Boolean)
        Try
            Main.Cursor = Cursors.WaitCursor
            Dim f As New Kunden With {
                .MdiParent = Main
            }
            If beginNew Then
                f.BeginNewFlag = True
                f.Show()
            Else
                f.Show()
            End If
        Catch ex As Exception
            HandleAppError(ex)
        Finally
            Main.Cursor = Cursors.Default
        End Try
    End Sub



    ''' <summary>
    ''' 
    ''' </summary>
    ''' <param name="IDNR"></param>
    ''' <param name="NeuesFenster"></param>
    ''' <remarks></remarks>
    Sub OeffneKundenMaske(IDNR As String, NeuesFenster As Boolean)
        Try
            Main.Cursor = Cursors.WaitCursor
            Dim k As Kunden
            If NeuesFenster Then
                k = New Kunden
            Else
                k = Kunden
            End If
            k.MdiParent = Main

            k.FilterBy("IDNR=" & IDNR)
            k.LadeKunde(IDNR)
            k.Show()
            k.BringToFront()
        Catch ex As Exception
            HandleAppError(ex)
        Finally
            Main.Cursor = Cursors.Default
        End Try
    End Sub

    Sub OeffneKundenPreise(Optional IDNR As Integer = -1, Optional Preisliste As String = Nothing)
        Try
            Main.Cursor = Cursors.WaitCursor
            KundenPreisregeln.MdiParent = Main
            KundenPreisregeln.Show()
            KundenPreisregeln.BringToFront()

            If IDNR >= 0 Then
                KundenPreisregeln.AdressenControl1.IDNR = IDNR
                KundenPreisregeln.FilterButton.PerformClick()
            End If
            If Preisliste IsNot Nothing Then
                KundenPreisregeln.PreislisteComboBox.SelectedValue = Preisliste
                KundenPreisregeln.AdressenControl1.IDNR = -1
                KundenPreisregeln.FilterButton.PerformClick()
            End If

        Catch ex As Exception
            HandleAppError(ex)
        Finally
            Main.Cursor = Cursors.Default
        End Try
    End Sub

    Sub OeffneKundenMengenPreise(Optional IDNR As Integer = -1)
        Try
            Main.Cursor = Cursors.WaitCursor
            KundenPreiseForm.MdiParent = Main
            KundenPreiseForm.Show()
            KundenPreiseForm.BringToFront()

            If IDNR >= 0 Then
                KundenPreiseForm.Lade(IDNR)
            End If

        Catch ex As Exception
            HandleAppError(ex)
        Finally
            Main.Cursor = Cursors.Default
        End Try
    End Sub

    Friend Sub OeffneTelefonischeBestellung(Optional idnr As Integer = -1)
        Try
            Main.Cursor = Cursors.WaitCursor
            TelefonischeBestellung.MdiParent = Main
            TelefonischeBestellung.Show()
            TelefonischeBestellung.Focus()
            TelefonischeBestellung.BringToFront()
            If idnr > 0 Then
                TelefonischeBestellung.TouchKundeAnlegen1.ShowIDNR(idnr, "")
                TelefonischeBestellung.LadeKunde(idnr)
            End If
            OeffneFormKundenErkennen()
        Catch ex As Exception
            HandleAppError(ex)
        Finally
            Main.Cursor = Cursors.Default
        End Try
    End Sub

    Private Sub OeffneFormKundenErkennen()
        Try

            Dim letztesMalGezeigt As Date = VarValue_Default("OeffneFormKundenErkennen_letzte_Anzeige", Today.AddDays(-7))
            If letztesMalGezeigt.AddDays(3) < Today Then
                FormKundenErkennen.Show()
                SetVarValue("OeffneFormKundenErkennen_letzte_Anzeige", Today)
            End If

        Catch ex As Exception
            HandleAppError(ex, Silent:=True)
        End Try
    End Sub

    Sub OeffnePreisKalkulator()
        Try
            Main.Cursor = Cursors.WaitCursor
            PreisKalkulator.MdiParent = Main
            PreisKalkulator.Show()
            PreisKalkulator.BringToFront()

        Catch ex As Exception
            HandleAppError(ex)
        Finally
            Main.Cursor = Cursors.Default
        End Try
    End Sub


    Sub OeffneAnrufliste(IDNR As Integer)
        Try
            Main.Cursor = Cursors.WaitCursor
            Dim a As New Anrufliste With {
                .MdiParent = Main
            }
            a.Show()
            If IDNR > 0 Then
                a.Anrufprotokoll(IDNR)
            End If
        Catch ex As Exception
            HandleAppError(ex)
        Finally
            Main.Cursor = Cursors.Default
        End Try
    End Sub

    Function OeffneAnruf(AnrufNr As Integer) As Anruf
        Try
            Anruf.MdiParent = Main
            Anruf.Show()
            Anruf.FilterByAnrufNr(AnrufNr)
            Return Anruf
        Catch ex As Exception
            HandleAppError(ex)
        End Try
        Return Nothing
    End Function

    Function OeffneAnrufNeu(KundNr As Integer) As Anruf
        Try
            Dim a As New Anruf With {
                .MdiParent = Main
            }
            a.Show()
            a.StarteAnruf(KundNr)
            Return a
        Catch ex As Exception
            HandleAppError(ex)
        End Try
        Return Nothing
    End Function

    Sub OeffneKundenlisteMaske()
        Try
            Main.Cursor = Cursors.WaitCursor
            Dim f As New Kundenliste With {
                .MdiParent = Main
            }
            f.Show()
        Catch ex As Exception
            HandleAppError(ex)
        Finally
            Main.Cursor = Cursors.Default
        End Try
    End Sub

#Region "Vorgang"

    Public Function OpenVorgangByIDNR(KundNr As Integer, Optional VorgangTyp As String = Nothing, Optional neuesFenster As Boolean = True) As Vorgang
        Try
            Application.UseWaitCursor = True


            Dim frm As Vorgang
            If neuesFenster Then
                frm = New Vorgang
            Else
                frm = Vorgang
            End If

            frm.MdiParent = Main
            frm.KundNr = KundNr ' FirstRow("select Kundnr from buchVorgang where IDNR=" & KundNr & " order b Datum desc")
            frm.Show()
            frm.LadeVorgaengeKunde(KundNr:=KundNr, VorgangTyp:=VorgangTyp)
            frm.Visible = True

            frm.BringToFront()
            Return frm
        Catch ex As Exception
            HandleAppError(ex)
        Finally
            Application.UseWaitCursor = False
        End Try
        Return Nothing
    End Function

    ''' <summary>
    ''' öffnet die Maske Vorgang (für alle möglich vorgänge) und positioniert die maske an dem bestimmten vorgang
    ''' </summary>
    ''' <param name="VorgangTyp"></param>
    ''' <param name="VorgangNummer"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function OeffneVorgangByNummer(VorgangTyp As String, VorgangNummer As Integer, Optional NeuesFenster As Boolean = True, Optional SetMDIParent As Boolean = True) As Vorgang
        Try
            Application.UseWaitCursor = True
            If VorgangNummer < 1 Then
                Throw New Exception("Ungültige Vorgangsnummer!")
            End If

            If VorgangTyp = "" Then
                Throw New Exception("Ungültiger Vorgangstyp!")
            End If

            Dim exists = FirstRow("select nummer from  buchVorgang where Nummer=" & VorgangNummer & " and Typ='" & VorgangTyp & "'")
            If Not exists = VorgangNummer Then
                Throw New Exception("Vorgang " & VorgangTyp & "-" & VorgangNummer & " wurde nicht gefunden!")
            End If

            Dim frm As Vorgang
            If NeuesFenster Then
                frm = New Vorgang
            Else
                frm = Vorgang
            End If

            If SetMDIParent Then
                frm.MdiParent = Main
            End If

            frm.KundNr = FirstRow("select Kundnr from buchVorgang where Nummer=" & VorgangNummer & " and Typ='" & VorgangTyp & "'")
            frm.Show()
            frm.LadeVorgangNummer(Typ:=VorgangTyp, Nummer:=VorgangNummer, raiseEventVorgangGeladen:=True)
            frm.Visible = True
            frm.BringToFront()
            OeffneVorgangByNummer = frm

        Catch ex As Exception
            HandleAppError(ex)
            OeffneVorgangByNummer = Nothing
        Finally
            Application.UseWaitCursor = False
        End Try

    End Function

    Public Function OeffneVorgangByNummerEasy(VorgangTyp As String, VorgangNummer As Integer, Optional NeuesFenster As Boolean = True, Optional SetMDIParent As Boolean = True) As VorgangEasy
        Try
            Application.UseWaitCursor = True
            If VorgangNummer < 1 Then
                Throw New Exception("Ungültige Vorgangsnummer!")
            End If

            If VorgangTyp = "" Then
                Throw New Exception("Ungültiger Vorgangstyp!")
            End If

            Dim exists = FirstRow("select nummer from  buchVorgang where Nummer=" & VorgangNummer & " and Typ='" & VorgangTyp & "'")
            If Not exists = VorgangNummer Then
                Throw New Exception("Vorgang " & VorgangTyp & "-" & VorgangNummer & " wurde nicht gefunden!")
            End If

            Dim frm As VorgangEasy
            If NeuesFenster Then
                frm = New VorgangEasy
            Else
                frm = VorgangEasy
            End If

            If SetMDIParent Then
                frm.MdiParent = Main
            End If

            frm.KundNr = FirstRow("select Kundnr from buchVorgang where Nummer=" & VorgangNummer & " and Typ='" & VorgangTyp & "'")
            frm.Show()
            frm.LadeVorgang(Typ:=VorgangTyp, Nummer:=VorgangNummer, raiseEventVorgangGeladen:=False)
            frm.Visible = True
            frm.BringToFront()
            Return frm

        Catch ex As Exception
            HandleAppError(ex)
            Return Nothing
        Finally
            Application.UseWaitCursor = False
        End Try

    End Function


    ''' <summary>
    ''' 
    ''' </summary>
    ''' <param name="VorgangTyp"></param>
    ''' <param name="VorgangNummer"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function OeffneBarVorgangByNummer(VorgangTyp As String, VorgangNummer As Integer) As VorgangBar
        Try

            If VorgangNummer < 1 Then
                Throw New Exception("Ungültige Vorgangsnummer!")
            End If

            If VorgangTyp = "" Then
                Throw New Exception("Ungültiger Vorgangstyp!")
            End If

            Dim frm As New VorgangBar With {
                .MdiParent = Main
            }

            Dim fltr As String = "Nummer=" & VorgangNummer & " and Typ='" & VorgangTyp & "'"
            frm.KundNr = FirstRow("select Kundnr from buchVorgang where " & fltr)
            frm.LadeVorgangNummer(VorgangTyp, VorgangNummer)
            frm.FilterBy(fltr)
            frm.Show()
            frm.Visible = True
            Return frm

        Catch ex As Exception
            HandleAppError(ex)
            Return Nothing
        End Try
    End Function

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <param name="VorgangTyp"></param>
    ''' <param name="VorgangNummer"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function OeffneEasyKassaByNummer(VorgangTyp As String, VorgangNummer As Integer) As AbstractVorgangBar
        Try

            If VorgangNummer < 1 Then
                Throw New Exception("Ungültige Vorgangsnummer!")
            End If

            If VorgangTyp = "" Then
                Throw New Exception("Ungültiger Vorgangstyp!")
            End If

            ' OpenForm "buchRechnung"
            Dim frm As AbstractVorgangBar = AbstractVorgangBar
            frm.MdiParent = Main

            Dim fltr As String = "Nummer=" & VorgangNummer & " and Typ='" & VorgangTyp & "'"
            frm.LadeVorgangNummer(VorgangTyp, VorgangNummer)
            frm.Show()
            frm.ShowTouch()
            frm.Visible = True
            frm.BringToFront()
            Return frm

        Catch ex As Exception
            HandleAppError(ex)
            Return Nothing
        End Try
    End Function

    Public Function ShowRestaurantKassa() As AbstractVorgangBar
        Try

            Dim frm As New AbstractVorgangBar
            frm.MdiParent = Main
            'VorgangBar.WindowState = FormWindowState.Maximized
            frm.LadenBase()

            CheckMonatsBeleg(frm)

            frm.Show()
            frm.ShowTouch()
            frm.Refresh()
            frm.Focus()

            Return frm
        Catch ex As Exception
            HandleAppError(ex)
        End Try
        Return Nothing
    End Function


    Public Sub ShowHandelskassa()
        Try
            VorgangBar.MdiParent = Main
            VorgangBar.Show()
            VorgangBar.WindowState = FormWindowState.Maximized
            VorgangBar.BringToFront()
            CheckMonatsBeleg(VorgangBar)
        Catch ex As Exception
            HandleAppError(ex)
        End Try
    End Sub

#End Region

    Sub OeffneVorgangLagerbewegung(typ As String, nummer As Integer)

        Try
            Main.Cursor = Cursors.WaitCursor
            Dim cmd = New IntraSellLagerbuchungenFürVorgangCommand With {.Typ = typ, .Nummer = nummer}
            ModuleGlobal.RequestCommandDirekt(cmd)
            ' LagerBewegung.MdiParent = Main
            ' LagerBewegung.Show()
            ' LagerBewegung.OeffneVorgangsbewegungen(typ, nummer)
        Catch ex As Exception
            HandleAppError(ex)
        Finally
            Main.Cursor = Cursors.Default
        End Try
    End Sub

    Sub OeffneZustellerPanel()
        Try
            If Not CBool(VarValue_Default(ZUSTELLER_ABRECHNUNG_EIGENES_FENSTER, "true")) Then
                ZustellerDashboardForm.MdiParent = Main
            End If

            ZustellerDashboardForm.Show()
            ZustellerDashboardForm.BringToFront()

            Dim fU = New FormUtility
            fU.MakeFormCustomizable(ZustellerDashboardForm)
        Catch ex As Exception
            HandleAppError(ex)
        End Try
    End Sub

    Sub OeffneKuechenDisplay()
        Try
            Dim kd As New KuechenDisplay
            kd.Show()
            kd.BringToFront()
            Dim fU = New FormUtility
            fU.MakeFormCustomizable(kd)
        Catch ex As Exception
            HandleAppError(ex)
        End Try
    End Sub

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <param name="TableName"></param>
    ''' <param name="Key"></param>
    ''' <param name="FieldName"></param>
    ''' <param name="textToTranslate"></param>
    ''' <remarks></remarks>
    Public Sub OpenTransForm(tableName As String, key As String, FieldName As String, textToTranslate As String)
        Dim frlm As New Translation With {
            .TableName = tableName,
            .Key = key,
            .FieldName = FieldName,
            .TextToTranslate = textToTranslate
        }
        frlm.Filter()
        frlm.Show()
    End Sub

    Friend Sub OpenNachrichtEinstellungen()
        Try
            NachrichtenEinstellungen.Show()
            NachrichtenEinstellungen.BringToFront()
        Catch ex As Exception
            HandleAppError(ex)
        End Try
    End Sub

    Friend Sub OffneOrderPreview(vorgangInBearbeitung As dsVorgaenge.buchVorgangListeRow)
        Try
            Main.Cursor = Cursors.WaitCursor
            Dim Vorgang As New VorgangInBearbeitung(vorgangInBearbeitung)
            OrderPreview.VorgangInfo(Vorgang)
        Catch ex As Exception
            HandleAppError(ex)
        Finally
            Main.Cursor = Cursors.Default
        End Try
    End Sub
End Module
