Public Class frmFiskalSchreiben

    '  Dim KUNDE As cKunde = Nothing
    Dim ADRESSE As VERAG_PROG_ALLGEMEIN.cAdressen = Nothing
    Dim KUNDE As VERAG_PROG_ALLGEMEIN.cKunde = Nothing
    Dim SPED As VERAG_PROG_ALLGEMEIN.cSpeditionsbuch = Nothing
    Dim FISKAL As cFiskal_Daten = Nothing
    'Dim SPED_LIST As cSpeditionsbuchList = Nothing

    '  Dim SENDUNG As VERAG_PROG_ALLGEMEIN.cSendungen

    Sub New(kdnr As Integer, SPED As VERAG_PROG_ALLGEMEIN.cSpeditionsbuch)
        InitializeComponent()

        Me.ADRESSE = New VERAG_PROG_ALLGEMEIN.cAdressen(kdnr)
        Me.KUNDE = New VERAG_PROG_ALLGEMEIN.cKunde(kdnr)
        Me.FISKAL = New cFiskal_Daten(kdnr)
        Me.SPED = SPED

    End Sub

    Private Sub frmFiskalSchreiben_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
        Try
            VERAG_PROG_ALLGEMEIN.DATENVERVER_OPTIONS.clearTMPPath()
        Catch ex As Exception
        End Try
    End Sub


    Private Sub frmFiskalSchreiben_Load(sender As Object, e As EventArgs) Handles Me.Load
        If Not ADRESSE.hasEntry Then
            MsgBox("Keine Kunden-Nummer gefunden!")
            Me.Close()
        End If

        cbxCMR.Checked = FISKAL.fisk_schreibenCMR
        cbxFA.Checked = FISKAL.fisk_schreibenFA
        cbxSB.Checked = FISKAL.fisk_schreibenSB

        txtSB.Text = FISKAL.fisk_schreibenSB_Adr
        txtFA.Text = FISKAL.fisk_schreibenFA_Adr

        txtKunde.Text = ADRESSE.Ordnungsbegriff
        txtAbf.Text = String.Format("{0} - {1} - {2}", SPED.FilialenNr, SPED.AbfertigungsNr, SPED.UnterNr)

      
      
        '
        ' If SENDUNG.FilialenNr > 0 And SENDUNG.AbfertigungsNr > 0 Then
        'SPED_LIST = New cSpeditionsbuchList(SENDUNG.FilialenNr, SENDUNG.AbfertigungsNr)
        ' End If


    End Sub


    Sub loadDatatToSFiskal()
        FISKAL.fisk_schreibenCMR = cbxCMR.Checked
        FISKAL.fisk_schreibenFA = cbxFA.Checked
        FISKAL.fisk_schreibenSB = cbxSB.Checked

        FISKAL.fisk_schreibenSB_Adr = txtSB.Text
        FISKAL.fisk_schreibenFA_Adr = txtFA.Text
    End Sub
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        loadDatatToSFiskal()
        If FISKAL.SAVE() Then
            lblFiskGespeichert.Visible = True
            tmrFiskal.Enabled = True
        End If
    End Sub


    ' Dim ds As New VERAG_PROG_ALLGEMEIN.cDATENSERVER
    Sub Finanzamt(showPrint)

        Me.Cursor = Cursors.WaitCursor
        Dim odoc As Microsoft.Office.Interop.Word.Document = Nothing
        Try
            If ADRESSE Is Nothing Then Exit Sub
            If KUNDE Is Nothing Then Exit Sub
            If FISKAL Is Nothing Then Exit Sub
            If SPED Is Nothing Then Exit Sub

            Dim path As String = ""
            Select Case SPED.Abfertigungsart
                Case 26 'AT FISK
                    Select Case ADRESSE.LandKz
                        Case "A", "D", "CH"
                            Dim DS As New VERAG_PROG_ALLGEMEIN.cDATENSERVER("DOKUMENTE", "VORLAGEN", "FISKALSCHREIBEN", "", "", "Fiskal_Schreiben_Finanzamt_VERAG_CS_DE.docx")
                            path = DS.OPEN_SINGLE(False)
                        Case "F"
                            Dim DS As New VERAG_PROG_ALLGEMEIN.cDATENSERVER("DOKUMENTE", "VORLAGEN", "FISKALSCHREIBEN", "", "", "Fiskal_Schreiben_Finanzamt_VERAG_CS_FR.docx")
                            path = DS.OPEN_SINGLE(False)
                          Case Else
                            Dim DS As New VERAG_PROG_ALLGEMEIN.cDATENSERVER("DOKUMENTE", "VORLAGEN", "FISKALSCHREIBEN", "", "", "Fiskal_Schreiben_Finanzamt_VERAG_CS_EN.docx")
                            path = DS.OPEN_SINGLE(False)
                    End Select
                Case 28 'DE FISK
                    Select Case ADRESSE.LandKz
                        Case "A", "D", "CH"
                            Dim DS As New VERAG_PROG_ALLGEMEIN.cDATENSERVER("DOKUMENTE", "VORLAGEN", "FISKALSCHREIBEN", "", "", "Fiskal_Schreiben_Finanzamt_VERAG_DE_GMBH_DE.docx")
                            path = DS.OPEN_SINGLE(False)
                        Case "F"
                            Dim DS As New VERAG_PROG_ALLGEMEIN.cDATENSERVER("DOKUMENTE", "VORLAGEN", "FISKALSCHREIBEN", "", "", "Fiskal_Schreiben_Finanzamt_VERAG_DE_GMBH_FR.docx")
                            path = DS.OPEN_SINGLE(False)
                        Case "N", "NL"
                            Dim DS As New VERAG_PROG_ALLGEMEIN.cDATENSERVER("DOKUMENTE", "VORLAGEN", "FISKALSCHREIBEN", "", "", "Fiskal_Schreiben_Finanzamt_VERAG_DE_GMBH_NL.docx")
                            path = DS.OPEN_SINGLE(False)
                        Case Else
                            Dim DS As New VERAG_PROG_ALLGEMEIN.cDATENSERVER("DOKUMENTE", "VORLAGEN", "FISKALSCHREIBEN", "", "", "Fiskal_Schreiben_Finanzamt_VERAG_DE_GMBH_EN.docx")
                            path = DS.OPEN_SINGLE(False)
                    End Select
                Case Else
                    MsgBox("KEINE FISKAL - VZ!")
                    Me.Close()
            End Select
            

            Dim oWord As New Microsoft.Office.Interop.Word.Application

            oWord.Visible = False 'Word sehen zum Test?
            Dim m As Object = System.Reflection.Missing.Value
            odoc = oWord.Documents.Open(path, m, m, m, m, m, m, m, m, m, m, m)



            Dim split() As String = If(FISKAL.fisk_schreibenFA_Adr, "").ToString.Split(New String() {Environment.NewLine}, StringSplitOptions.None)
            odoc.FormFields("Adr1").Range.Text = If(split.Count > 0, split(0), "")
            odoc.FormFields("Adr2").Range.Text = If(split.Count > 1, split(1), "")
            odoc.FormFields("Adr3").Range.Text = If(split.Count > 2, split(2), "")
            odoc.FormFields("Adr4").Range.Text = If(split.Count > 3, split(3), "")
            odoc.FormFields("Adr5").Range.Text = If(split.Count > 4, split(4), "")
            odoc.FormFields("Eori").Range.Text = If(KUNDE.EORITIN, "")
            odoc.FormFields("Uid").Range.Text = If(ADRESSE.UstIdKz, "") & If(ADRESSE.UstIdNr, "")
            'MsgBox((If(ADRESSE.Name_1, "") & " " & If(ADRESSE.Name_2, "")).Trim)
            odoc.FormFields("Firma1").Range.Text = (If(ADRESSE.Name_1, "") & " " & If(ADRESSE.Name_2, "")).Trim
            odoc.FormFields("Firma2").Range.Text = If(ADRESSE.Straße, " ")
            odoc.FormFields("Firma3").Range.Text = (If(ADRESSE.LandKz IsNot Nothing, ADRESSE.LandKz & " ", "") & If(ADRESSE.PLZ IsNot Nothing, ADRESSE.PLZ & " ", "") & If(ADRESSE.Ort, "")).trim
            odoc.FormFields("Firma4").Range.Text = ""
            Select Case ADRESSE.LandKz
                Case "A", "D", "CH"
                    odoc.FormFields("Anlage1").Range.Text = "Zollbeleg (Kopie)"
                    odoc.FormFields("Anlage2").Range.Text = "Rechnung"
                Case "F"
                    odoc.FormFields("Anlage1").Range.Text = "Documents de douanes (copie)"
                    odoc.FormFields("Anlage2").Range.Text = "Facture"
                Case Else
                    odoc.FormFields("Anlage1").Range.Text = "customs documents(copy)"
                    odoc.FormFields("Anlage2").Range.Text = "account"

            End Select
            odoc.FormFields("Anlage3").Range.Text = ""
            odoc.FormFields("Anlage4").Range.Text = ""
            odoc.FormFields("Anlage5").Range.Text = ""



            ' odoc.FormFields("SB").Range.Text = VERAG_PROG_ALLGEMEIN.cAllgemein.USRNAME 'VERAG_PROG_ALLGEMEIN.cglobal.AktiverMitarbeiter.Mitarbeiter
            odoc.Save()
            odoc.Activate()

            Select Case showPrint
                Case "print"
                    oWord.DisplayAlerts = False
                    oWord.PrintOut()
                    oWord.Documents.Close()
                    'Quit word application
                    oWord.Quit()

                    'Release
                    oWord = Nothing
                Case Else
                    oWord.Visible = True
            End Select



        Catch ex As Exception
            MessageBox.Show(ex.ToString)
        End Try
        Me.Cursor = Cursors.Default

    End Sub

    Sub Steuerberater(showPrint)

        Me.Cursor = Cursors.WaitCursor
        Dim odoc As Microsoft.Office.Interop.Word.Document = Nothing
        Try
            If ADRESSE Is Nothing Then Exit Sub
            If KUNDE Is Nothing Then Exit Sub
            If FISKAL Is Nothing Then Exit Sub
            If SPED Is Nothing Then Exit Sub

            Dim path As String = ""

            Select Case SPED.Abfertigungsart
                Case 26 'AT FISK
                    Select Case ADRESSE.LandKz
                        Case "A", "D", "CH"
                            Dim DS As New VERAG_PROG_ALLGEMEIN.cDATENSERVER("DOKUMENTE", "VORLAGEN", "FISKALSCHREIBEN", "", "", "Fiskal_Schreiben_Steuerberater_VERAG_CS_DE.docx")
                            path = DS.OPEN_SINGLE(False)
                        Case "F"
                            Dim DS As New VERAG_PROG_ALLGEMEIN.cDATENSERVER("DOKUMENTE", "VORLAGEN", "FISKALSCHREIBEN", "", "", "Fiskal_Schreiben_Steuerberater_VERAG_CS_FR.docx")
                            path = DS.OPEN_SINGLE(False)
                        Case "N", "NL"
                            Dim DS As New VERAG_PROG_ALLGEMEIN.cDATENSERVER("DOKUMENTE", "VORLAGEN", "FISKALSCHREIBEN", "", "", "Fiskal_Schreiben_Steuerberater_VERAG_CS_NL.docx")
                            path = DS.OPEN_SINGLE(False)
                        Case Else
                            Dim DS As New VERAG_PROG_ALLGEMEIN.cDATENSERVER("DOKUMENTE", "VORLAGEN", "FISKALSCHREIBEN", "", "", "Fiskal_Schreiben_Steuerberater_VERAG_CS_EN.docx")
                            path = DS.OPEN_SINGLE(False)
                    End Select
                Case 28 'DE FISK
                    Select Case ADRESSE.LandKz
                        Case "A", "D", "CH"
                            Dim DS As New VERAG_PROG_ALLGEMEIN.cDATENSERVER("DOKUMENTE", "VORLAGEN", "FISKALSCHREIBEN", "", "", "Fiskal_Schreiben_Steuerberater_VERAG_DE_GMBH_DE.docx")
                            path = DS.OPEN_SINGLE(False)
                        Case "F"
                            Dim DS As New VERAG_PROG_ALLGEMEIN.cDATENSERVER("DOKUMENTE", "VORLAGEN", "FISKALSCHREIBEN", "", "", "Fiskal_Schreiben_Steuerberater_VERAG_DE_GMBH_FR.docx")
                            path = DS.OPEN_SINGLE(False)
                        Case "N", "NL"
                            Dim DS As New VERAG_PROG_ALLGEMEIN.cDATENSERVER("DOKUMENTE", "VORLAGEN", "FISKALSCHREIBEN", "", "", "Fiskal_Schreiben_Steuerberater_VERAG_DE_GMBH_NL.docx")
                            path = DS.OPEN_SINGLE(False)
                        Case Else
                            Dim DS As New VERAG_PROG_ALLGEMEIN.cDATENSERVER("DOKUMENTE", "VORLAGEN", "FISKALSCHREIBEN", "", "", "Fiskal_Schreiben_Steuerberater_VERAG_DE_GMBH_EN.docx")
                            path = DS.OPEN_SINGLE(False)
                    End Select
                Case Else
                    MsgBox("KEINE FISKAL - VZ!")
                    Me.Close()
            End Select

            If path = "" Then Exit Sub : Me.Cursor = Cursors.Default

            Dim oWord As New Microsoft.Office.Interop.Word.Application

            oWord.Visible = False 'Word sehen zum Test?
            Dim m As Object = System.Reflection.Missing.Value
            odoc = oWord.Documents.Open(path, m, m, m, m, m, m, m, m, m, m, m)


            Dim split() As String = If(FISKAL.fisk_schreibenSB_Adr, "").ToString.Split(New String() {Environment.NewLine}, StringSplitOptions.None)
            odoc.FormFields("Adr1").Range.Text = If(split.Count > 0, split(0), "")
            odoc.FormFields("Adr2").Range.Text = If(split.Count > 1, split(1), "")
            odoc.FormFields("Adr3").Range.Text = If(split.Count > 2, split(2), "")
            odoc.FormFields("Adr4").Range.Text = If(split.Count > 3, split(3), "")
            odoc.FormFields("Adr5").Range.Text = If(split.Count > 4, split(4), "")
            odoc.FormFields("Eori").Range.Text = If(KUNDE.EORITIN, "")
            odoc.FormFields("Uid").Range.Text = If(ADRESSE.UstIdKz, "") & If(ADRESSE.UstIdNr, "")
            'MsgBox((If(ADRESSE.Name_1, "") & " " & If(ADRESSE.Name_2, "")).Trim)
            odoc.FormFields("Firma1").Range.Text = (If(ADRESSE.Name_1, "") & " " & If(ADRESSE.Name_2, "")).Trim
            odoc.FormFields("Firma2").Range.Text = If(ADRESSE.Straße, " ")
            odoc.FormFields("Firma3").Range.Text = (If(ADRESSE.LandKz IsNot Nothing, ADRESSE.LandKz & " ", "") & If(ADRESSE.PLZ IsNot Nothing, ADRESSE.PLZ & " ", "") & If(ADRESSE.Ort, "")).trim
            odoc.FormFields("Firma4").Range.Text = ""
            Select Case ADRESSE.LandKz
                Case "A", "D", "CH"
                    odoc.FormFields("Anlage1").Range.Text = "Zollbeleg (Kopie)"
                    odoc.FormFields("Anlage2").Range.Text = "Rechnung"
                Case "F"
                    odoc.FormFields("Anlage1").Range.Text = "Documents de douanes (copie)"
                    odoc.FormFields("Anlage2").Range.Text = "Facture"
                Case Else
                    odoc.FormFields("Anlage1").Range.Text = "customs documents(copy)"
                    odoc.FormFields("Anlage2").Range.Text = "account"

            End Select
            odoc.FormFields("Anlage3").Range.Text = ""
            odoc.FormFields("Anlage4").Range.Text = ""
            odoc.FormFields("Anlage5").Range.Text = ""
            ' odoc.FormFields("SB").Range.Text = VERAG_PROG_ALLGEMEIN.cAllgemein.USRNAME 'VERAG_PROG_ALLGEMEIN.cglobal.AktiverMitarbeiter.Mitarbeiter
            odoc.Save()
            odoc.Activate()

            Select Case showPrint
                Case "print"
                    oWord.DisplayAlerts = False
                    oWord.PrintOut()
                    oWord.Documents.Close()
                    'Quit word application
                    oWord.Quit()

                    'Release
                    oWord = Nothing
                Case Else
                    oWord.Visible = True
            End Select



        Catch ex As Exception
            MessageBox.Show(ex.ToString)
        End Try
        Me.Cursor = Cursors.Default

    End Sub



    Sub CMR(showPrint)

        Me.Cursor = Cursors.WaitCursor
        Dim odoc As Microsoft.Office.Interop.Word.Document = Nothing
        Try
            If ADRESSE Is Nothing Then Exit Sub
            If KUNDE Is Nothing Then Exit Sub
            If FISKAL Is Nothing Then Exit Sub
            If SPED Is Nothing Then Exit Sub

            Dim path As String = ""

            Select Case SPED.Abfertigungsart
                Case 26 'AT FISK
                    Select Case ADRESSE.LandKz
                        Case "A", "D", "CH"
                            Dim DS As New VERAG_PROG_ALLGEMEIN.cDATENSERVER("DOKUMENTE", "VORLAGEN", "FISKALSCHREIBEN", "", "", "Fiskal_Schreiben_CMR_VERAG_CS_DE.docx")
                            path = DS.OPEN_SINGLE(False)
                        Case "F"
                            Dim DS As New VERAG_PROG_ALLGEMEIN.cDATENSERVER("DOKUMENTE", "VORLAGEN", "FISKALSCHREIBEN", "", "", "Fiskal_Schreiben_CMR_VERAG_CS_FR.docx")
                            path = DS.OPEN_SINGLE(False)
                        Case "CZ"
                            Dim DS As New VERAG_PROG_ALLGEMEIN.cDATENSERVER("DOKUMENTE", "VORLAGEN", "FISKALSCHREIBEN", "", "", "Fiskal_Schreiben_CMR_VERAG_CS_CZ.docx")
                            path = DS.OPEN_SINGLE(False)
                        Case "H"
                            Dim DS As New VERAG_PROG_ALLGEMEIN.cDATENSERVER("DOKUMENTE", "VORLAGEN", "FISKALSCHREIBEN", "", "", "Fiskal_Schreiben_CMR_VERAG_CS_HU.docx")
                            path = DS.OPEN_SINGLE(False)
                        Case Else
                            Dim DS As New VERAG_PROG_ALLGEMEIN.cDATENSERVER("DOKUMENTE", "VORLAGEN", "FISKALSCHREIBEN", "", "", "Fiskal_Schreiben_CMR_VERAG_CS_EN.docx")
                            path = DS.OPEN_SINGLE(False)
                    End Select
                Case 28 'DE FISK
                    Select Case ADRESSE.LandKz
                        Case "A", "D", "CH"
                            Dim DS As New VERAG_PROG_ALLGEMEIN.cDATENSERVER("DOKUMENTE", "VORLAGEN", "FISKALSCHREIBEN", "", "", "Fiskal_Schreiben_CMR_VERAG_DE_GMBH_DE.docx")
                            path = DS.OPEN_SINGLE(False)
                        Case "F"
                            Dim DS As New VERAG_PROG_ALLGEMEIN.cDATENSERVER("DOKUMENTE", "VORLAGEN", "FISKALSCHREIBEN", "", "", "Fiskal_Schreiben_CMR_VERAG_DE_GMBH_FR.docx")
                            path = DS.OPEN_SINGLE(False)
                        Case "E"
                            Dim DS As New VERAG_PROG_ALLGEMEIN.cDATENSERVER("DOKUMENTE", "VORLAGEN", "FISKALSCHREIBEN", "", "", "Fiskal_Schreiben_CMR_VERAG_DE_GMBH_ES.docx")
                            path = DS.OPEN_SINGLE(False)
                        Case "NL", "N"
                            Dim DS As New VERAG_PROG_ALLGEMEIN.cDATENSERVER("DOKUMENTE", "VORLAGEN", "FISKALSCHREIBEN", "", "", "Fiskal_Schreiben_CMR_VERAG_DE_GMBH_NL.docx")
                            path = DS.OPEN_SINGLE(False)
                        Case Else
                            Dim DS As New VERAG_PROG_ALLGEMEIN.cDATENSERVER("DOKUMENTE", "VORLAGEN", "FISKALSCHREIBEN", "", "", "Fiskal_Schreiben_CMR_VERAG_DE_GMBH_EN.docx")
                            path = DS.OPEN_SINGLE(False)
                    End Select
                Case Else
                    MsgBox("KEINE FISKAL - VZ!")
                    Me.Close()
            End Select

            If path = "" Then Exit Sub : Me.Cursor = Cursors.Default

            Dim oWord As New Microsoft.Office.Interop.Word.Application

            oWord.Visible = False 'Word sehen zum Test?
            Dim m As Object = System.Reflection.Missing.Value
            odoc = oWord.Documents.Open(path, m, m, m, m, m, m, m, m, m, m, m)



            Dim split() As String = If(FISKAL.fisk_schreibenSB_Adr, "").ToString.Split(vbNewLine)
            odoc.FormFields("Adr1").Range.Text = (If(ADRESSE.Name_1, "") & " " & If(ADRESSE.Name_2, "")).Trim
            odoc.FormFields("Adr2").Range.Text = If(ADRESSE.Straße, " ")
            odoc.FormFields("Adr3").Range.Text = ""
            odoc.FormFields("Adr4").Range.Text = (If(ADRESSE.LandKz IsNot Nothing, ADRESSE.LandKz & " ", "") & If(ADRESSE.PLZ IsNot Nothing, ADRESSE.PLZ & " ", "") & If(ADRESSE.Ort, "")).trim
            odoc.FormFields("Sachbearbeiter").Range.Text = VERAG_PROG_ALLGEMEIN.cAllgemein.USRNAME

            If SPED IsNot Nothing Then
                odoc.FormFields("PosNr").Range.Text = "Pos.Nr: " & SPED.FilialenNr & " " & SPED.AbfertigungsNr & " " & SPED.UnterNr
            End If

            If SPED IsNot Nothing Then
                odoc.FormFields("VzDat").Range.Text = If(SPED.Abfertigungsdatum, "")
                odoc.FormFields("ZollNr").Range.Text = If(SPED.BelegNr, "")
            End If

            ' odoc.FormFields("SB").Range.Text = VERAG_PROG_ALLGEMEIN.cAllgemein.USRNAME 'VERAG_PROG_ALLGEMEIN.cglobal.AktiverMitarbeiter.Mitarbeiter
            odoc.Save()
            odoc.Activate()

            Select Case showPrint
                Case "print"
                    oWord.DisplayAlerts = False
                    oWord.PrintOut()
                    Try
                        oWord.Documents.Close()
                        'Quit word application
                        oWord.Quit()

                        'Release
                        oWord = Nothing
                    Catch ex As Exception
                        'NIX, kann sein, dass noch offen..
                    End Try

                Case Else
                    oWord.Visible = True
            End Select

        Catch ex As Exception
            MessageBox.Show(ex.ToString)
        End Try
        Me.Cursor = Cursors.Default

    End Sub




    Private Sub Button16_Click(sender As Object, e As EventArgs) Handles Button16.Click
        loadDatatToSFiskal()
        CMR("show")
    End Sub

    Private Sub Button15_Click(sender As Object, e As EventArgs) Handles Button15.Click
        loadDatatToSFiskal()
        Finanzamt("show")
    End Sub

    Private Sub Button14_Click(sender As Object, e As EventArgs) Handles Button14.Click
        loadDatatToSFiskal()
        Steuerberater("show")
    End Sub

    Private Sub tmrFiskal_Tick(sender As Object, e As EventArgs) Handles tmrFiskal.Tick
        lblFiskGespeichert.Visible = False
        tmrFiskal.Enabled = False
    End Sub

    Private Sub Button13_Click(sender As Object, e As EventArgs) Handles Button13.Click
        loadDatatToSFiskal()
        If cbxCMR.Checked Then CMR("print")
        If cbxSB.Checked Then Steuerberater("print")
        If cbxFA.Checked Then Finanzamt("print")
    End Sub
End Class