Imports IntraSell_DLL
Imports IntraSell_Net_Base

Public Class TouchKundeAnlegen

    Public Event IDNRChanged(IDNR As Integer)
    Public Event Order(IDNR As Integer, Notiz As String, Liefermethode As String, Typ As String)

    ReadOnly Property PLZ As String
        Get
            Return AdressenDetailControlQuick1.PLZ
        End Get
    End Property

    ReadOnly Property Ort As String
        Get
            Return AdressenDetailControlQuick1.Ort
        End Get
    End Property

    ReadOnly Property Adresse As String
        Get
            Return AdressenDetailControlQuick1.Adresse
        End Get
    End Property

    Private Sub TouchKundeAnlegen_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Try
            FillComboBox(PreislisteComboBox, SQL_PREISLISTEN, "PreislisteName")
            ButtonRechnung.Visible = HasRecht(MitarbeiterID, Rechte.Handelskassa, False) And VarValue_Default(Variablen.HANDELSKASSA_AKTIVIERT, False)
        Catch ex As Exception
            HandleAppError(ex, Silent:=True)
        End Try
    End Sub

    Public Sub ShowIDNR(KundNr As Integer, notiz As String)
        Try
            ModuleGlobals.KundenIDNR = KundNr
            DataSetKunden._ofadressen_settings.Clear()
            DataSetKunden.ofadressen.Clear()
            OfAdressenTableAdapter.FillByIDNR(DataSetKunden.ofadressen, KundNr)
            Ofadressen_settingsTableAdapter.Fill(DataSetKunden._ofadressen_settings, KundNr)

            If Ofadressen_settingsBindingSource.Current Is Nothing Then
                KundeSettingsAnlegen(KundNr)
            End If

            Me.Notiz.Text = notiz
            ButtonUebernehmen.Enabled = KundNr > 0
            RaiseEvent IDNRChanged(KundNr)
        Catch ex As Exception
            HandleAppError(ex)
        End Try
    End Sub

    Private Sub AdressenControl1_IDNRChanged(KundNr As Integer) Handles AdressenControl1.IDNRChanged
        Try
            ShowIDNR(KundNr, "")
        Catch ex As Exception
            HandleAppError(ex)
        End Try
    End Sub

    Private Sub ButtonUebernehmen_Click(sender As Object, e As EventArgs) Handles ButtonUebernehmen.Click
        RaiseEventErfasst(VorgangTypen.AU)
    End Sub

    Public Sub ButtonNeu_Click(sender As Object, e As EventArgs) Handles ButtonNeu.Click
        Try
            Dim moeglicheTelNr = AdressenControl1.Text
            If IsTelefonNumber(moeglicheTelNr) Then
                TouchKundeAnlegen(moeglicheTelNr)
            Else
                TouchKundeAnlegen(Nothing)
            End If
        Catch ex As Exception
        End Try
    End Sub

    Private Sub AdressenDetailControlQuick1_Enter(sender As Object, e As EventArgs) Handles AdressenDetailControlQuick1.Enter
        Dim cur As dsAdressen.ofadressenRow = Nothing
        If ofAdressenBindingSource.Current Is Nothing Then
            cur = DataSetKunden.ofadressen.NewofadressenRow
            cur.IDNR = NextId("ofAdressen", "Idnr")
            DataSetKunden.ofadressen.AddofadressenRow(cur)
            ofAdressenBindingSource.MoveLast()
        Else
            cur = ofAdressenBindingSource.Current.row
            If cur.IDNR <= 1 Then
                ButtonNeu_Click(Nothing, Nothing)
            End If
        End If

    End Sub

    Function GetLieferMethode() As String
        Dim LieferMethode = Transportmethoden.Zustellung
        If RadioAbholung.Checked Then LieferMethode = Transportmethoden.Abholung
        Return LieferMethode
    End Function

    Private Sub RaiseEventErfasst(Vorgangtyp As String)
        Try
            RaiseEvent Order(AdressenDetailControlQuick1.IDNR, Notiz.Text, GetLieferMethode(), Vorgangtyp)
            AdressenDetailControlQuick1.Enabled = False
        Catch ex As Exception
            HandleAppError(ex)
        End Try
    End Sub

    Sub SucheNachNummer(telnummer As String)
        Try
            AdressenDetailControlQuick1.Tel = telnummer
            telnummer = Replace(telnummer, "+43", "0")
            Dim kundNrListe = KundeSuchen(telnummer)
            If kundNrListe IsNot Nothing Then
                If (kundNrListe.Count = 1) Then
                    ShowIDNR(kundNrListe(0), "")
                    Return
                ElseIf IsNumeric(kundNrListe.Count > 1) Then 'Mehrere Kunden Erkannt 
                    Dim telnummerSuche = CleanUpTelNrFuerSuche(telnummer)
                    Me.AdressenControl1.ZeigeAddressenSelectorFuerNummer(telnummerSuche)
                    Return
                End If
            End If
            TouchKundeAnlegen(telnummer)
        Catch ex As Exception
            HandleAppError(ex)
        End Try
    End Sub

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub ButtonAendern_Click(sender As Object, e As EventArgs) Handles ButtonAendern.Click
        AdressenDetailControlQuick1.Enabled = True
    End Sub

    Friend Sub ButtonSpeichern_Click(sender As Object, e As EventArgs) Handles ButtonSpeichernUndBestellen.Click
        SpeichernUndWeiter(True, VorgangTypen.AU)
    End Sub

    Private Sub ButtonSpeichernOhneAdressdaten_Click(sender As Object, e As EventArgs) Handles ButtonSpeichernOhneAdressdaten.Click
        SpeichernUndWeiter(False, VorgangTypen.AU)
    End Sub

    Private Sub ButtonRechnung_Click(sender As Object, e As EventArgs) Handles ButtonRechnung.Click
        SpeichernUndWeiter(True, VorgangTypen.AR)
    End Sub

    Private Sub SpeichernUndWeiter(stopWennFehler As Boolean, Vorgangtyp As String)
        Try
            LabelValidierung.Text = ""
            Dim cur As dsAdressen.ofadressenRow = Nothing
            If ofAdressenBindingSource.Current Is Nothing Then
                LabelValidierung.Text = Messages.TelefonischeBestellung_Kunde_Waehlen
                LabelValidierung.ForeColor = Color.Red
                Exit Sub
                'cur = Me.ofAdressenBindingSource.AddNew.row
                'sDataSetKunden.ofadressen.AddofadressenRow(cur)
            Else
                cur = ofAdressenBindingSource.Current.row
                cur.Adresse = AdressenDetailControlQuick1.Adresse
            End If

            Try
                If IsDBNull(cur.IDNR) Then
                    cur.IDNR = NextId("ofAdressen", "Idnr")
                End If
            Catch ex As Exception
                cur.IDNR = NextId("ofAdressen", "Idnr")
            End Try

            ofAdressenBindingSource.EndEdit()
            Ofadressen_settingsBindingSource.EndEdit()

            'Validierung 
            LabelValidierung.Text = ""
            If Not cur.IDNR > 0 Then
                LabelValidierung.Text = String.Format(Messages.Generell_nicht_gesetzt, "IDNR")
            End If

            If Not cur.Name.Length >= 3 Then
                LabelValidierung.Text = String.Format(Messages.Generell_nicht_gesetzt, "Name")
                AdressenDetailControlQuick1.NameTextBox.BackColor = Color.LightCoral
            Else
                AdressenDetailControlQuick1.NameTextBox.BackColor = Color.LightGreen
            End If

            If Not cur.Ort.Length >= 3 Then
                AdressenDetailControlQuick1.OrtComboBox.Focus()
                AdressenDetailControlQuick1.OrtComboBox.BackColor = Color.LightCoral
                LabelValidierung.Text = String.Format(Messages.Generell_nicht_gesetzt, "Ort")
            Else
                AdressenDetailControlQuick1.OrtComboBox.BackColor = Color.LightGreen
            End If

            If Not cur.Adresse.Length >= 3 Then
                LabelValidierung.Text = String.Format(Messages.Generell_nicht_gesetzt, "Strasse/Nr.")
                AdressenDetailControlQuick1.AdresseTextBox.BackColor = Color.LightCoral
            Else
                AdressenDetailControlQuick1.AdresseTextBox.BackColor = Color.LightGreen
            End If

            If Not (cur.Tel.Length >= 3 Or cur.Mobil.Length >= 3 Or cur.Email.Length >= 3) Then
                LabelValidierung.Text = Messages.TelefonischeBestellung_Kunde_TelMobEmail_fehlt
                AdressenDetailControlQuick1.TelTextBox.BackColor = Color.LightCoral
            Else
                AdressenDetailControlQuick1.TelTextBox.BackColor = Color.LightGreen
            End If

            If LabelValidierung.Text.Length > 0 Then
                'If ButtonSpeichern.ForeColor <> Color.Red Then 'Die Eingabe ohne korrekte Daten erlauben
                LabelValidierung.ForeColor = Color.Red
                ButtonSpeichernOhneAdressdaten.Visible = True
                ButtonSpeichernOhneAdressdaten.ForeColor = Color.Red
                If stopWennFehler Then
                    Exit Sub
                End If
                'End If
            Else
                LabelValidierung.Text = "OK"
                LabelValidierung.ForeColor = Color.Green
            End If

            'Settings 
            If Ofadressen_settingsBindingSource.Current IsNot Nothing Then
                Dim curSettings As dsAdressen._ofadressen_settingsRow = Ofadressen_settingsBindingSource.Current.row
                If curSettings.IsPreislisteNull Then
                    curSettings.Preisliste = GetLieferMethode()
                End If
            End If
            OfAdressenTableAdapter.Update(DataSetKunden)
            Ofadressen_settingsTableAdapter.Update(DataSetKunden)

            If Vorgangtyp IsNot Nothing Then
                RaiseEventErfasst(Vorgangtyp)
            End If

        Catch ex As Exception
            HandleAppError(ex)
        End Try
    End Sub

    Private Sub TouchKundeAnlegen(tel As Object)
        Dim cur As dsAdressen.ofadressenRow = Nothing
        cur = DataSetKunden.ofadressen.NewofadressenRow
        cur.IDNR = NextId("ofAdressen", "Idnr")
        cur.Tel = tel
        DataSetKunden.ofadressen.AddofadressenRow(cur)
        ofAdressenBindingSource.MoveLast()

        KundeSettingsAnlegen(cur.IDNR)

        AdressenDetailControlQuick1.Enabled = True
        AdressenDetailControlQuick1.Focus()
    End Sub

    Sub KundeSettingsAnlegen(IDNR As Integer)
        Dim curSettings As dsAdressen._ofadressen_settingsRow = Nothing
        curSettings = DataSetKunden._ofadressen_settings.New_ofadressen_settingsRow
        curSettings.IDNR = IDNR
        DataSetKunden._ofadressen_settings.Add_ofadressen_settingsRow(curSettings)
        Ofadressen_settingsBindingSource.MoveLast()
    End Sub

    Private Sub ButtonSave_Click(sender As Object, e As EventArgs) Handles ButtonSave.Click
        Save()
    End Sub

    Sub Save()
        Try
            SpeichernUndWeiter(True, Vorgangtyp:=Nothing)
        Catch ex As Exception
            HandleAppError(ex)
        End Try
    End Sub

    Private Sub RadioAbholung_Click(sender As Object, e As EventArgs) Handles RadioAbholung.Click
        Try
            If RadioAbholung.Checked Then
                PreislisteComboBox.SelectedValue = Transportmethoden.Abholung
            End If
        Catch ex As Exception
            HandleAppError(ex)
        End Try
    End Sub

    Private Sub RadioZustellung_Click(sender As Object, e As EventArgs) Handles RadioZustellung.Click
        Try
            If RadioZustellung.Checked Then
                PreislisteComboBox.SelectedValue = Transportmethoden.Zustellung
            End If
        Catch ex As Exception
            HandleAppError(ex)
        End Try
    End Sub


End Class
