Imports IntraSell_DLL
Imports IntraSell_Net_Base

Public Class ArtikelQuick2
    Inherits AbstractForm
    Implements InterfacePrintable, IArtikelEditor

    Dim ARTIKEL_BILDER_VERZEICHNIS As String

    Private Sub Form_Verlassen() Handles MyBase.Verlassen
        Me.Close()
    End Sub

    'Die Datensätze filtern
    Public Sub FilterBy(Expression As String)
        Try
            GrartikelBindingSource.Filter = Expression
        Catch ex As Exception
            HandleAppError(ex)
        End Try
    End Sub


    ''' <summary>
    ''' Save Artikel
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Friend Sub GrartikelBindingNavigatorSaveItem_Click(sender As Object, e As System.EventArgs) Handles MyBase.Save
        Try
            ArtikelSave.Save(Me)
            Dim artnr = GrartikelBindingSource.Current.Item("ArtNr")
            If Not IsDBNull(artnr) Then
                ArtikelBild(artnr, PictureBoxProduktBild)
            End If
            ButtonSave.Enabled = False
        Catch ex As Exception
            HandleAppError(ex)
        End Try
    End Sub

    Private Sub Artikel_Load(sender As Object, e As System.EventArgs) Handles MyBase.Load
        Try
            ds = DsArtikel
            Dim ph = New ArtikelNettoBruttoHandler
            ph.HaendlerInitialisieren(PreisATSTextBox, PreisATS_BruttoTextBox, MWSTTextBox)

            ARTIKEL_BILDER_VERZEICHNIS = VarValue_Default(Variablen.ARTIKEL_BILDER_VERZEICHNIS, ClassSettings.GetDataPath & "produktbilder")

            Try
                Grartikel_kategorienTableAdapter.Fill(DsArtikel._grartikel_kategorien)
                GrartikelTableAdapter.Fill(DsArtikel.grartikel)
            Catch ex As Exception
                IgnoreAppError(ex)
            End Try

            ParentBindingNavigator.BindingSource = GrartikelBindingSource

            If BeginNewFlag Then
                BeginNew()
            End If

            AddHandler DsArtikel.grartikel.grartikelRowChanged, AddressOf AktivateSaveButton
            AddHandler DsArtikel.grartikel.ColumnChanged, AddressOf AktivateSaveButton2
        Catch ex As Exception
            HandleAppError(ex)
        End Try
    End Sub

    Public Sub Print(sender As Object) Implements InterfacePrintable.Print
        EtikettenToolStripMenuItem_Click(Nothing, Nothing)
    End Sub

#Region "New"

    Private Sub BindingNavigatorAddNewItem_Click(sender As Object, e As System.EventArgs) Handles MyBase.AddNew
        Try
            AddingNewFlag = True
        Catch ex As Exception
            HandleAppError(ex)
        End Try
    End Sub

    Private Sub BindingSource_CurrentChanged(sender As Object, _
                              e As System.EventArgs) _
                              Handles GrartikelBindingSource.CurrentChanged
        Try
            If AddingNewFlag = True Then
                AddingNewFlag = False
                ArtNrTextBox.Text = NextId("grArtikel", "ArtNr", , True)
                MWSTTextBox.Text = VarValue_Default(ARTIKEL_ANLAGE_MWST, "20")
                ProduktAktivCheckBox.Checked = True
                LagerArtikelCheckBox.Checked = VarValue_Default("Lager_autostart", False)
            Else
                If GrartikelBindingSource.Current IsNot Nothing Then
                    Dim artnr = GrartikelBindingSource.Current.Item("ArtNr")
                    If Not IsDBNull(artnr) Then
                        ArtikelBild(artnr, PictureBoxProduktBild)
                        BackColor = ArtikelFarbe(artnr)
                    End If
                End If
            End If
        Catch ex As Exception
            HandleAppError(ex)
        End Try
    End Sub

    Public Sub BeginNew(Optional artkatnr As Integer = -1)
        Try
            BindingNavigatorAddNewItem_Click(Nothing, Nothing)
            GrartikelBindingSource.AddNew()
            If artkatnr > -1 Then
                ArtikelKategorieControl1.ArtKatNr = artkatnr
            End If
            PictureBoxProduktBild.BackgroundImage = Nothing
        Catch ex As Exception
            HandleAppError(ex)
        End Try
    End Sub
#End Region


    Private Sub GrartikelBindingNavigatorDeleteItem_Click(sender As Object, e As System.EventArgs) Handles MyBase.Delete
        Try
            Dim artikel As dsArtikel.grartikelRow = GrartikelBindingSource.Current.Row
            If DeleteArtikel(artikel.ArtNr) Then
                Close()
            End If
        Catch ex As Exception
            HandleAppError(ex)
        End Try
    End Sub

    ''' <summary>
    ''' EAN Automatisch setzen
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Friend Sub BezeichnungTextBox_TextChanged(sender As Object, e As EventArgs) Handles BezeichnungTextBox.Validated
        Try
            If EANTextBox.Text = "" Then
                EANTextBox.Text = ModuleArtikel.MD5StringHash(BezeichnungTextBox.Text).Substring(0, 6)
            End If
        Catch ex As Exception
        End Try
    End Sub

#Region "Bilder"
    Private Sub ButtonBildLaden_Click(sender As Object, e As EventArgs) Handles ButtonBildLaden.Click
        BildLadenDialog(GrartikelBindingSource.Current.Row, PictureBoxProduktBild)
    End Sub


    Private Sub Form1_Load(sender As Object, e As System.EventArgs) Handles MyBase.Load
        AllowDrop = True
    End Sub

    Private Sub Form1_DragDrop(sender As Object, e As System.Windows.Forms.DragEventArgs) Handles Me.DragDrop
        Dim files() As String = e.Data.GetData(DataFormats.FileDrop)
        For Each path In files
            Dim artikel As dsArtikel.grartikelRow = GrartikelBindingSource.Current.Row
            artikel.Picture = BildLaden(artikel.ArtNr, path, PictureBoxProduktBild)
        Next
    End Sub

    Private Sub Form1_DragEnter(sender As Object, e As System.Windows.Forms.DragEventArgs) Handles Me.DragEnter
        If e.Data.GetDataPresent(DataFormats.FileDrop) Then
            e.Effect = DragDropEffects.Copy
        End If
    End Sub
#End Region

    Private Sub GrartikelBindingSource_DataError(sender As Object, e As BindingManagerDataErrorEventArgs) Handles GrartikelBindingSource.DataError

    End Sub


    Private Sub ArtikelControlSuche_ArtNrChanged(ArtNr As Integer) Handles ArtikelControlSuche.ArtNrChanged
        FilterBy("ArtNr=" & ArtNr)
    End Sub


    Private Sub EtikettenToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles EtikettenToolStripMenuItem.Click
        Try
            ArtikelEtikette.ArtNr = GrartikelBindingSource.Current.row.artnr
            ArtikelEtikette.Show()
            ArtikelEtikette.Focus()
        Catch ex As Exception
            HandleAppError(ex)
        End Try
    End Sub

    Private Sub ArtikelKategorieControl1_ArtKatNrAdded(newArtKatNR As Integer) Handles ArtikelKategorieControl1.ArtKatNrAdded
        Try
            DsArtikel.EnforceConstraints = False
            Grartikel_kategorienTableAdapter.Fill(DsArtikel._grartikel_kategorien)

            Dim artikel As dsArtikel.grartikelRow = GrartikelBindingSource.Current.Row
            artikel.ArtKatNr = newArtKatNR

            Try
                DsArtikel.EnforceConstraints = True
            Catch ex As Exception
            End Try

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

    Private Sub ButtonModifikationen_Click(sender As Object, e As EventArgs) Handles ButtonModifikationen.Click
        Dim opt = New ArtikelOptionen With {
            .Modifikationen = ModifikationenTextBox.Text
        }
        opt.ShowDialog()
        ModifikationenTextBox.Text = opt.Modifikationen
    End Sub

    Private Sub ArtikelKategorieControl1_ArtKatNrChanged2(ArtKatNr As Integer) Handles ArtikelKategorieControl1.ArtKatNrChanged2
        Dim mwst = FirstRow("select mwst from grartikel a  where artkatnr = " & ArtKatNr)
        If IsNumeric(mwst) Then
            MWSTTextBox.Text = mwst
            MWSTTextBox.Enabled = False
        Else
            MWSTTextBox.Enabled = True
        End If
    End Sub

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

    Private Sub AktivateSaveButton(sender As Object, e As IntraSell_Net_Base.dsArtikel.grartikelRowChangeEvent)
        ButtonSave.Enabled = True
    End Sub

    Private Sub AktivateSaveButton2(sender As Object, e As DataColumnChangeEventArgs)
        ButtonSave.Enabled = True
    End Sub

#Region "IArtikelEditor"
    Function ErrorProvider() As ErrorProvider Implements IArtikelEditor.ErrorProvider
        Return ErrorProvider1
    End Function

    Function BindingSource() As System.Windows.Forms.BindingSource Implements IArtikelEditor.BindingSource
        Return GrartikelBindingSource
    End Function

    Function GetBezeichnungTextBox() As Control Implements IArtikelEditor.GetBezeichnungTextBox
        Return BezeichnungTextBox
    End Function

    Function GetPreisATSTextBox() As Control Implements IArtikelEditor.GetPreisATSTextBox
        Return PreisATSTextBox
    End Function

    Function GetPreisATS_BruttoTextBox() As Control Implements IArtikelEditor.GetPreisATS_BruttoTextBox
        Return PreisATS_BruttoTextBox
    End Function

    Function GetArtikelKategorieControl() As Control Implements IArtikelEditor.GetArtikelKategorieControl
        Return ArtikelKategorieControl1
    End Function

    Function GetMWSTTextBox() As Control Implements IArtikelEditor.GetMWSTTextBox
        Return MWSTTextBox
    End Function

    Function GetEANTextBox() As Control Implements IArtikelEditor.GetEANTextBox
        Return EANTextBox
    End Function

    Function GetGrartikelTableAdapter() As IntraSell_Net_Base.dsArtikelTableAdapters.grartikelTableAdapter Implements IArtikelEditor.GetGrartikelTableAdapter
        Return GrartikelTableAdapter
    End Function

    Function GetDsArtikel() As IntraSell_Net_Base.dsArtikel Implements IArtikelEditor.GetDsArtikel
        Return DsArtikel
    End Function

    Function ValidateFrom() As Boolean Implements IArtikelEditor.ValidateFrom
        Return Validate()
    End Function

    Private Sub ButtonDelete_Click(sender As Object, e As EventArgs) Handles ButtonDelete.Click
        Try
            Dim artikel As dsArtikel.grartikelRow = GrartikelBindingSource.Current.Row
            If ModuleArtikel.DeleteArtikel(artikel.ArtNr) Then
                Me.Close()
            End If
        Catch ex As Exception
            HandleAppError(ex)
        End Try
    End Sub
#End Region
End Class