Imports IntraSell_DLL
Imports MySql.Data.MySqlClient

Public Module ModuleBarcode

    Public Const BARCODE_LENGTH = 16

    'creates new barcode
    'if already one product with LE exists then the next ITEM is selected
    Public Function GenerateNewBarcode(ArtNr As String, LieferantNR As String) As String
        Dim ANR, lNr, itemID
        Dim newBarcode

        ANR = Right("0000000" & ArtNr, 7)
        lNr = Right("0000" & LieferantNR, 4)
        itemID = "00001"

        Dim sql As String
        sql = "select Barcode from [grArtikel-ItemTrackingHistory] where vorgangstyp='LE' and  Barcode like @Barcode order by Barcode desc"   'lager eingang
        Dim cmd As New MySqlCommand(sql)
        cmd.Parameters.AddWithValue("@Barcode", ANR & lNr & "%")
        Dim barcodeOld = FirstRow(cmd)

        If IsNumeric(barcodeOld) Then
            Dim nbc = barcodeOld + 1
            newBarcode = Right("0000000" & nbc, 16)
        Else
            newBarcode = ANR & lNr & itemID
        End If

        GenerateNewBarcode = newBarcode
    End Function

    Public Function GetArtNrFromBC(BC As String) As String
        If BC = "" Then Return ""
        Return Left(BC, 7)
    End Function

    Public Function GetLieferantNrFromBC(BC As String) As String
        Return Mid(BC, 8, 4)
    End Function

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <param name="BC"></param>
    ''' <param name="Silent"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function IsBarcodeOK(BC As String, Silent As Boolean) As Boolean
        IsBarcodeOK = True
        If Len(BC) <> BARCODE_LENGTH Then
            IsBarcodeOK = False
            Dim msg = String.Format(Messages.ModuleBarcode_Barcode_Stellen, BARCODE_LENGTH)
            If Not Silent Then MsgBox(msg, vbCritical)
            'show in satus text
            Debug.Print(msg)
        End If
    End Function

    ''' <summary>
    ''' Barcode Historie anlegen
    ''' </summary>
    ''' <param name="BC">Barcode oder SerienNr</param>
    ''' <param name="Vorgangtyp"></param>
    ''' <param name="VorgangNummer"></param>
    ''' <param name="Status"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Function MakeBCHistory(BC As String, Vorgangtyp As String, VorgangNummer As String, Status As String) As Boolean
        MakeBCHistory = False
        If BC & "" = "" Then Exit Function
        'Prüfung ob das eine SerienNr ist (nicht interne Barcode) 
        Dim Barcode As String = Nothing
        Try
            Barcode = GetBarcodeNachSeriennummer(BC)
        Catch ex As Exception

        End Try

        If Barcode Is Nothing Or Barcode = NOT_AVAILABLE Then Barcode = BC
        Dim sqlBC As String = "Insert into [grArtikel-ItemTrackingHistory] (SerienNrOriginal, Barcode, Vorgangstyp, VorgangsNummer, Status, [Timestamp]) " &
                " Values (@SerienNrOriginal, @Barcode, @Vorgangtyp, @VorgangNummer, @Status, @Datum)"
        Dim cmd = New MySqlCommand(sqlBC)
        cmd.Parameters.AddWithValue("SerienNrOriginal", BC)
        cmd.Parameters.AddWithValue("Barcode", Barcode)
        cmd.Parameters.AddWithValue("Vorgangtyp", Vorgangtyp)
        cmd.Parameters.AddWithValue("VorgangNummer", VorgangNummer)
        cmd.Parameters.AddWithValue("Status", Status)
        cmd.Parameters.AddWithValue("Datum", Now)
        RunSQL(cmd)
        MakeBCHistory = True
    End Function

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <param name="Vorgangstyp"></param>
    ''' <param name="Vorgangsnummer"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function CheckIfVorgangProperScanned(Vorgangstyp As String, Vorgangsnummer As String) As String
        Dim sql As String
        Dim rs As DataTable, rsAlle As DataTable

        sql = "select count(Id) as c from `buchVorgang-Artikel` where not Bezeichnung like '*CALCULATE*' and (ArtikelIdentifikation is null or ArtikelIdentifikation="""") and rechnr = " & Vorgangsnummer
        rs = OpenRecordsetInMemory(sql)
        sql = "select count(Id) as c from `buchVorgang-Artikel` where  not Bezeichnung like '*CALCULATE*' and  rechnr = " & Vorgangsnummer
        rsAlle = OpenRecordsetInMemory(sql)

        If rs.Rows.Count > 0 Then
            If rs.Rows(0).Item("c") = 0 Then
                Return "alle Pos. OK"
            Else
                Return rs.Rows(0).Item("c") & " von " & rsAlle.Rows(0).Item("c") & " Pos.offen"
            End If
        Else
            Return "keine Pos."
        End If

    End Function

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <param name="txtSeriennummer"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Function GetBarcodeNachSeriennummer(txtSeriennummer As String) As String
        Dim cmd As New MySqlCommand("select Barcode from `grArtikel-ItemTrackingHistory` where SerienNrOriginal = @SerienNrOriginal order by Id desc")
        cmd.Parameters.AddWithValue("SerienNrOriginal", txtSeriennummer)
        Return FirstRow(cmd)
    End Function

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <param name="txtSeriennummer"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Function GetArtNrNachSeriennummer(txtSeriennummer As String)
        Dim cmd As New MySqlCommand("select artnr from grArtikel where barcode=@barcode and ProduktAktiv<>0")
        cmd.Parameters.AddWithValue("barcode", txtSeriennummer)
        Dim ArtNr = FirstRow(cmd)
        If IsNumeric(ArtNr) Then
            Return ArtNr.ToString
        Else
            Return GetArtNrFromBC(GetBarcodeNachSeriennummer(txtSeriennummer))
        End If
        'TODO Erkennung nach buchSeriennummer implementieren 
    End Function

End Module
