Imports System.Data.SqlClient
Imports Newtonsoft.Json

Public Class ScannerModule
    Private Const _connectionstring As String = "data source=195.228.156.88,1433;user id=sisuser;password=M1cr0s0ft!;initial catalog=pcs_whs;Persist Security Info=true;Application Name=PCS WCF Service;Pooling=False"
    Public Function GetInfo(I As String) As String
        '// JSON-ban van a kérdés
        Dim _Result As String = ""
        Dim _SimpleAnswer As New SimpleAnswer() With {.ResultStatus = 0}
        Dim _SimpleQuery As SimpleQuery = JsonConvert.DeserializeObject(Of SimpleQuery)(I)
        If _SimpleQuery.Query = "DX_GetCustomersFrom_dxSelectBox" Then
            _SimpleAnswer.ResultStatus = 1
            _SimpleAnswer.Result = Me.DX_GetCustomersFrom_dxSelectBox()
        End If
        If _SimpleQuery.Query = "DX_Login" Then
            If Me.DX_Login(_SimpleQuery.UserName, _SimpleQuery.Password, _SimpleQuery.Database) Then
                _SimpleAnswer.ResultStatus = 1
                _SimpleAnswer.Result = "OK"
            End If
        End If
        If _SimpleQuery.Query = "DX_GetStoragePDACollectionHeader_dxList" Then
            _SimpleAnswer.ResultStatus = 1
            _SimpleAnswer.Result = Me.DX_GetStoragePDACollectionHeader_dxList(_SimpleQuery.Database)
        End If
        If _SimpleQuery.Query = "DX_GetStorageOutHeader_dxList" Then
            _SimpleAnswer.ResultStatus = 1
            _SimpleAnswer.Result = Me.DX_GetStorageOutHeader_dxList(_SimpleQuery.Database)
        End If
        If _SimpleQuery.Query Like "DX_GetStoragePDACollectionRows_dxDataGrid*" Then
            Dim _sArray As String() = Split(_SimpleQuery.Query, ";")
            _SimpleAnswer.ResultStatus = 1
            _SimpleAnswer.Result = Me.DX_GetStoragePDACollectionRows_dxDataGrid(Guid.Parse(_sArray(1)), _SimpleQuery.Database)
        End If
        If _SimpleQuery.Query Like "DX_GetProductsInfo_dxDatagrid*" Then
            Dim _sArray As String() = Split(_SimpleQuery.Query, ";")
            _SimpleAnswer.ResultStatus = 1
            _SimpleAnswer.Result = Me.DX_GetProductsInfo_dxDatagrid(_sArray(1), _SimpleQuery.Database)
        End If
        If _SimpleQuery.Query Like "DX_GetDiscountInfo_dxDatagrid*" Then
            Dim _sArray As String() = Split(_SimpleQuery.Query, ";")
            _SimpleAnswer.ResultStatus = 1
            _SimpleAnswer.Result = Me.DX_GetDiscountInfo_dxDatagrid(_sArray(1), _SimpleQuery.Database)
        End If
        If _SimpleQuery.Query Like "DX_InsertStoragePDACollectionRows_dxDataGrid*" Then
            Dim _sArray As String() = Split(_SimpleQuery.Query, ";")
            _SimpleAnswer.ResultStatus = 1
            _SimpleAnswer.Result = Me.DX_InsertStoragePDACollectionRows_dxDataGrid(_sArray(1), Guid.Parse(_sArray(2)), Val(_sArray(3)), _SimpleQuery.Database)
        End If
        If _SimpleQuery.Query Like "DX_DeleteStoragePDACollectionRows_dxDataGrid*" Then
            Dim _sArray As String() = Split(_SimpleQuery.Query, ";")
            _SimpleAnswer.ResultStatus = 1
            _SimpleAnswer.Result = "OK"
            Me.DX_DeleteStoragePDACollectionRows_dxDataGrid(Guid.Parse(_sArray(1)), _SimpleQuery.Database)
        End If
        If _SimpleQuery.Query Like "DX_InsertDiscount_dxDataGrid*" Then
            Dim _sArray As String() = Split(_SimpleQuery.Query, ";")
            _SimpleAnswer.ResultStatus = 1
            _SimpleAnswer.Result = Me.DX_InsertDiscount_dxDataGrid(_sArray(1), Val(_sArray(2)), _sArray(3),
                                                                   _sArray(4), _SimpleQuery.Database)
        End If

        If _SimpleQuery.Query Like "DX_InsertStorageOutRows_dxDataGrid*" Then
            Dim _sArray As String() = Split(_SimpleQuery.Query, ";")
            _SimpleAnswer.ResultStatus = 1
            _SimpleAnswer.Result = Me.DX_InsertStorageOutRows_dxDataGrid(_sArray(1), Guid.Parse(_sArray(2)), Val(_sArray(3)), _SimpleQuery.Database)
        End If
        If _SimpleQuery.Query Like "DX_GetStorageOutRows_dxDataGrid*" Then
            Dim _sArray As String() = Split(_SimpleQuery.Query, ";")
            _SimpleAnswer.ResultStatus = 1
            _SimpleAnswer.Result = Me.DX_GetStorageOutRows_dxDataGrid(Guid.Parse(_sArray(1)), _SimpleQuery.Database)
        End If
        If _SimpleQuery.Query Like "DX_DeleteStorageOutRows_dxDataGrid*" Then
            Dim _sArray As String() = Split(_SimpleQuery.Query, ";")
            _SimpleAnswer.ResultStatus = 1
            _SimpleAnswer.Result = "OK"
            Me.DX_DeleteStorageOutRows_dxDataGrid(Guid.Parse(_sArray(1)), _SimpleQuery.Database)

        End If
        Return JsonConvert.SerializeObject(_SimpleAnswer)
    End Function
#Region "PRIVATE"
#Region "DX"
    Private Function DX_GetCustomersFrom_dxSelectBox() As String
        Dim _SQLConnection As New SqlConnection(_connectionstring)
        Dim _SQLCommand As New SqlCommand("DX_GetCustomersFrom_dxSelectBox") With {.Connection = _SQLConnection, .CommandType = CommandType.StoredProcedure}
        Dim _Result As String = ""

        _SQLCommand.Parameters.Add(New SqlParameter() With {.ParameterName = "@Result", .Size = 8000, .DbType = DbType.String, .Direction = ParameterDirection.Output, .Value = ""})
        _SQLConnection.Open()
        _SQLCommand.ExecuteNonQuery()

        _Result = _SQLCommand.Parameters("@Result").Value

        _SQLConnection.Close()
        Return _Result
    End Function
    Private Function DX_Login(_UserName As String, _Password As String, _SQLDatabase As String) As Boolean
        Return True
    End Function
    Private Function DX_GetStoragePDACollectionHeader_dxList(_SQLDatabase As String) As String
        Dim _SQLConnection As New SqlConnection(Replace(_connectionstring, "pcs_whs", _SQLDatabase))
        Dim _SQLCommand As New SqlCommand("DX_GetStoragePDACollectionHeader_dxList") With {.Connection = _SQLConnection, .CommandType = CommandType.StoredProcedure}
        Dim _Result As String = ""

        _SQLCommand.Parameters.Add(New SqlParameter() With {.ParameterName = "@Result", .Size = 8000,
                                                            .DbType = DbType.String, .Direction = ParameterDirection.Output,
                                                            .Value = ""})

        _SQLConnection.Open()
        _SQLCommand.ExecuteNonQuery()

        _Result = _SQLCommand.Parameters("@Result").Value

        _SQLConnection.Close()
        Return _Result
    End Function
    Private Function DX_GetStorageOutHeader_dxList(_SQLDatabase As String) As String
        Dim _SQLConnection As New SqlConnection(Replace(_connectionstring, "pcs_whs", _SQLDatabase))
        Dim _SQLCommand As New SqlCommand("DX_GetStorageOutHeader_dxList") With {.Connection = _SQLConnection, .CommandType = CommandType.StoredProcedure}
        Dim _Result As String = ""

        _SQLCommand.Parameters.Add(New SqlParameter() With {.ParameterName = "@Result", .Size = 8000,
                                                            .DbType = DbType.String, .Direction = ParameterDirection.Output,
                                                            .Value = ""})

        _SQLConnection.Open()
        _SQLCommand.ExecuteNonQuery()

        _Result = _SQLCommand.Parameters("@Result").Value

        _SQLConnection.Close()
        Return _Result
    End Function
    Private Function DX_GetStoragePDACollectionRows_dxDataGrid(_StoragePDACollectionHeader As Guid, _SQLDatabase As String) As String
        Dim _SQLConnection As New SqlConnection(Replace(_connectionstring, "pcs_whs", _SQLDatabase))
        Dim _SQLCommand As New SqlCommand("DX_GetStoragePDACollectionRows_dxDataGrid") With {.Connection = _SQLConnection, .CommandType = CommandType.StoredProcedure}
        Dim _Result As String = ""

        _SQLCommand.Parameters.Add(New SqlParameter() With {.ParameterName = "@StoragePDACollectionHeader", .Size = 8000, .DbType = DbType.Guid, .Direction = ParameterDirection.Input, .Value = _StoragePDACollectionHeader})
        _SQLCommand.Parameters.Add(New SqlParameter() With {.ParameterName = "@Result", .Size = 8000, .DbType = DbType.String, .Direction = ParameterDirection.Output, .Value = ""})

        _SQLConnection.Open()
        _SQLCommand.ExecuteNonQuery()

        _Result = _SQLCommand.Parameters("@Result").Value

        _SQLConnection.Close()
        Return _Result
    End Function
    Private Function DX_GetProductsInfo_dxDatagrid(_Barcode As String, _SQLDatabase As String) As String
        Dim _SQLConnection As New SqlConnection(Replace(_connectionstring, "pcs_whs", _SQLDatabase))
        Dim _SQLCommand As New SqlCommand("DX_GetProductsInfo_dxDatagrid") With {.Connection = _SQLConnection, .CommandType = CommandType.StoredProcedure}
        Dim _Result As String = ""

        _SQLCommand.Parameters.Add(New SqlParameter() With {.ParameterName = "@Barcode", .Size = 8000, .DbType = DbType.String, .Direction = ParameterDirection.Input, .Value = _Barcode})
        _SQLCommand.Parameters.Add(New SqlParameter() With {.ParameterName = "@Result", .Size = 8000, .DbType = DbType.String, .Direction = ParameterDirection.Output, .Value = ""})

        _SQLConnection.Open()
        _SQLCommand.ExecuteNonQuery()

        _Result = _SQLCommand.Parameters("@Result").Value

        _SQLConnection.Close()
        Return _Result
    End Function
    Private Function DX_InsertStoragePDACollectionRows_dxDataGrid(_Barcode As String,
                                                                  _StoragePDACollectionHeader As Guid,
                                                                  _QTT As Double,
                                                                  _SQLDatabase As String) As String

        Dim _SQLConnection As New SqlConnection(Replace(_connectionstring, "pcs_whs", _SQLDatabase))
        Dim _SQLCommand As New SqlCommand("DX_InsertStoragePDACollectionRows_dxDataGrid") With {.Connection = _SQLConnection, .CommandType = CommandType.StoredProcedure}
        Dim _Result As String = ""

        _SQLCommand.Parameters.Add(New SqlParameter() With {.ParameterName = "@QTT", .DbType = DbType.Double, .Direction = ParameterDirection.Input, .Value = _QTT})
        _SQLCommand.Parameters.Add(New SqlParameter() With {.ParameterName = "@Barcode", .Size = 8000, .DbType = DbType.String, .Direction = ParameterDirection.Input, .Value = _Barcode})
        _SQLCommand.Parameters.Add(New SqlParameter() With {.ParameterName = "@StoragePDACollectionHeader", .Size = 8000, .DbType = DbType.Guid, .Direction = ParameterDirection.Input, .Value = _StoragePDACollectionHeader})
        _SQLCommand.Parameters.Add(New SqlParameter() With {.ParameterName = "@Result", .Size = 8000, .DbType = DbType.String, .Direction = ParameterDirection.Output, .Value = ""})

        _SQLConnection.Open()
        _SQLCommand.ExecuteNonQuery()

        '_Result = _SQLCommand.Parameters("@Result").Value

        _SQLConnection.Close()
        Return DX_GetStoragePDACollectionRows_dxDataGrid(_StoragePDACollectionHeader, _SQLDatabase)
    End Function
    Private Sub DX_DeleteStoragePDACollectionRows_dxDataGrid(_StoragePDACollectionRows As Guid, _SQLDatabase As String)
        Dim _SQLConnection As New SqlConnection(Replace(_connectionstring, "pcs_whs", _SQLDatabase))
        Dim _SQLCommand As New SqlCommand("DX_DeleteStoragePDACollectionRows_dxDataGrid") With {.Connection = _SQLConnection, .CommandType = CommandType.StoredProcedure}

        _SQLCommand.Parameters.Add(New SqlParameter() With {.ParameterName = "@StoragePDACollectionRows", .Size = 8000, .DbType = DbType.Guid, .Direction = ParameterDirection.Input, .Value = _StoragePDACollectionRows})


        _SQLConnection.Open()
        _SQLCommand.ExecuteNonQuery()



        _SQLConnection.Close()

    End Sub
    Private Function DX_InsertDiscount_dxDataGrid(_Barcode As String, _PriceBruttoHUF As Double, _ActualDate As Date, _ExpirationDate As Date, _SQLDatabase As String) As String
        Dim _SQLConnection As New SqlConnection(Replace(_connectionstring, "pcs_whs", _SQLDatabase))
        Dim _SQLCommand As New SqlCommand("DX_InsertDiscount_dxDataGrid") With {.Connection = _SQLConnection, .CommandType = CommandType.StoredProcedure}

        _SQLCommand.Parameters.Add(New SqlParameter() With {.ParameterName = "@Barcode", .Size = 8000, .DbType = DbType.String, .Direction = ParameterDirection.Input, .Value = _Barcode})
        _SQLCommand.Parameters.Add(New SqlParameter() With {.ParameterName = "@PriceBruttoHUF", .DbType = DbType.Double, .Direction = ParameterDirection.Input, .Value = _PriceBruttoHUF})
        _SQLCommand.Parameters.Add(New SqlParameter() With {.ParameterName = "@ActualDate", .DbType = DbType.Date, .Direction = ParameterDirection.Input, .Value = _ActualDate})
        _SQLCommand.Parameters.Add(New SqlParameter() With {.ParameterName = "@Expirationdate", .DbType = DbType.Date, .Direction = ParameterDirection.Input, .Value = _ExpirationDate})


        _SQLConnection.Open()
        _SQLCommand.ExecuteNonQuery()

        _SQLConnection.Close()

        Return DX_GetDiscountInfo_dxDatagrid(_Barcode, _SQLDatabase)
    End Function
    Private Function DX_GetDiscountInfo_dxDatagrid(_Barcode As String, _SQLDatabase As String) As String
        Dim _SQLConnection As New SqlConnection(Replace(_connectionstring, "pcs_whs", _SQLDatabase))
        Dim _SQLCommand As New SqlCommand("DX_GetDiscountInfo_dxDatagrid") With {.Connection = _SQLConnection, .CommandType = CommandType.StoredProcedure}
        Dim _Result As String = ""

        _SQLCommand.Parameters.Add(New SqlParameter() With {.ParameterName = "@Barcode", .Size = 8000, .DbType = DbType.String, .Direction = ParameterDirection.Input, .Value = _Barcode})
        _SQLCommand.Parameters.Add(New SqlParameter() With {.ParameterName = "@Result", .Size = 8000, .DbType = DbType.String, .Direction = ParameterDirection.Output, .Value = ""})

        _SQLConnection.Open()
        _SQLCommand.ExecuteNonQuery()

        _Result = _SQLCommand.Parameters("@Result").Value

        _SQLConnection.Close()
        Return _Result
    End Function
    Private Function DX_InsertStorageOutRows_dxDataGrid(_Barcode As String, _StorageOutHeader As Guid, _QTT As Double, _SQLDatabase As String) As String
        Dim _SQLConnection As New SqlConnection(Replace(_connectionstring, "pcs_whs", _SQLDatabase))
        Dim _SQLCommand As New SqlCommand("DX_InsertStorageOutRows_dxDataGrid") With {.Connection = _SQLConnection, .CommandType = CommandType.StoredProcedure}

        _SQLCommand.Parameters.Add(New SqlParameter() With {.ParameterName = "@QTT", .DbType = DbType.Double, .Direction = ParameterDirection.Input, .Value = _QTT})
        _SQLCommand.Parameters.Add(New SqlParameter() With {.ParameterName = "@Barcode", .Size = 8000, .DbType = DbType.String, .Direction = ParameterDirection.Input, .Value = _Barcode})
        _SQLCommand.Parameters.Add(New SqlParameter() With {.ParameterName = "@StorageOutHeader", .Size = 8000, .DbType = DbType.Guid, .Direction = ParameterDirection.Input, .Value = _StorageOutHeader})
        _SQLCommand.Parameters.Add(New SqlParameter() With {.ParameterName = "@Result", .Size = 8000, .DbType = DbType.String, .Direction = ParameterDirection.Output, .Value = ""})

        _SQLConnection.Open()
        _SQLCommand.ExecuteNonQuery()

        _SQLConnection.Close()
        Return DX_GetStorageOutRows_dxDataGrid(_StorageOutHeader, _SQLDatabase)
    End Function
    Private Function DX_GetStorageOutRows_dxDataGrid(_StorageOutHeader As Guid, _SQLDatabase As String) As String
        Dim _SQLConnection As New SqlConnection(Replace(_connectionstring, "pcs_whs", _SQLDatabase))
        Dim _SQLCommand As New SqlCommand("DX_GetStorageOutRows_dxDataGrid") With {.Connection = _SQLConnection, .CommandType = CommandType.StoredProcedure}
        Dim _Result As String = ""

        _SQLCommand.Parameters.Add(New SqlParameter() With {.ParameterName = "@StorageOutHeader", .Size = 8000, .DbType = DbType.Guid, .Direction = ParameterDirection.Input, .Value = _StorageOutHeader})
        _SQLCommand.Parameters.Add(New SqlParameter() With {.ParameterName = "@Result", .Size = 8000, .DbType = DbType.String, .Direction = ParameterDirection.Output, .Value = ""})

        _SQLConnection.Open()
        _SQLCommand.ExecuteNonQuery()

        _Result = _SQLCommand.Parameters("@Result").Value

        _SQLConnection.Close()
        Return _Result
    End Function
    Private Sub DX_DeleteStorageOutRows_dxDataGrid(_StorageOutRows As Guid, _SQLDatabase As String)
        Dim _SQLConnection As New SqlConnection(Replace(_connectionstring, "pcs_whs", _SQLDatabase))
        Dim _SQLCommand As New SqlCommand("DX_DeleteStorageOutRows_dxDataGrid") With {.Connection = _SQLConnection, .CommandType = CommandType.StoredProcedure}

        _SQLCommand.Parameters.Add(New SqlParameter() With {.ParameterName = "@StorageOutRows", .Size = 8000, .DbType = DbType.Guid, .Direction = ParameterDirection.Input, .Value = _StorageOutRows})

        _SQLConnection.Open()
        _SQLCommand.ExecuteNonQuery()

        _SQLConnection.Close()
    End Sub
#End Region
#Region "OTHER"

#End Region
#End Region
End Class
