function New-RKSimpleQuery {
    param( [string]$Key, [string]$Value )

    $sql = . T:\m.nielsen\Scripts\RK\queries.ps1

    $con = New-Object System.Data.SqlClient.SqlConnection
    $con.ConnectionString = 'Server=REJK-P-SQL003\SQL01;Database=AfcCookedIFSv2;Trusted_Connection=True;'
    $con.Open()
}





function Get-RKObjOrder {
    param( [string]$OrderID )

    Write-Verbose -Verbose -Message "Attempting Get-RKObjOrder on OrderID $OrderID"

    Set-ExecutionPolicy -ExecutionPolicy Bypass -Scope Process
    $sql = . T:\m.nielsen\Scripts\RK\queries.ps1
    
    $con = New-Object System.Data.SqlClient.SqlConnection
    $con.ConnectionString = 'Server=REJK-P-SQL003\SQL01;Database=AfcCookedIFSv2;Trusted_Connection=True;'
    $con.Open()

    try {
        $cmd = $con.CreateCommand()

        $cmd.CommandText = $sql['RKObjOrder'].Query
        $null = $cmd.Parameters.Add('@OrderID', $orderid)

        $reader = $cmd.ExecuteReader()

        while($reader.Read()) {

            $obj = [pscustomobject]@{ }

            $columns = $sql['RKObjOrder'].Columns

            foreach($column in $columns) {
                $obj = $obj | Add-Member -MemberType NoteProperty -Name $column -Value $reader[$column] -PassThru
            }

            Write-Output $obj
        }
    }
    catch {
        Write-Warning $_
    }
    finally {
        $reader.Close()
        $cmd.Dispose()
        $con.Close()
    }
}



function Get-RKObjOrderItem {
    param( [string]$OrderID )

    Write-Verbose -Verbose -Message "Attempting Get-RKObjOrderItem on OrderID $OrderID"

    Set-ExecutionPolicy -ExecutionPolicy Bypass -Scope Process
    $sql = . T:\m.nielsen\Scripts\RK\queries.ps1
    
    $con = New-Object System.Data.SqlClient.SqlConnection
    $con.ConnectionString = 'Server=REJK-P-SQL003\SQL01;Database=AfcCookedIFSv2;Trusted_Connection=True;'
    $con.Open()

    try {
        $cmd = $con.CreateCommand()

        $cmd.CommandText = $sql['RKObjOrderItem'].Query
        $null = $cmd.Parameters.Add('@OrderID', $orderid)

        $reader = $cmd.ExecuteReader()

        while($reader.Read()) {

            $obj = [pscustomobject]@{ }

            $columns = $sql['RKObjOrderItem'].Columns

            foreach($column in $columns) {
                $obj = $obj | Add-Member -MemberType NoteProperty -Name $column -Value $reader[$column] -PassThru
            }

            Write-Output $obj
        }
    }
    catch {
        Write-Warning $_
    }
    finally {
        $reader.Close()
        $cmd.Dispose()
        $con.Close()
    }
}




function Get-RKCustomerOrders {
    param( [string]$CustomerRefNumber )

    Write-Verbose -Verbose -Message "Attempting Get-RKCustomerOrders on RefNumber $CustomerRefNumber"

    Set-ExecutionPolicy -ExecutionPolicy Bypass -Scope Process
    $sql = . T:\m.nielsen\Scripts\RK\queries.ps1
    
    $con = New-Object System.Data.SqlClient.SqlConnection
    $con.ConnectionString = 'Server=REJK-P-SQL003\SQL01;Database=AfcCookedIFSv2;Trusted_Connection=True;'
    $con.Open()

    try {
        $cmd = $con.CreateCommand()

        $cmd.CommandText = $sql['RKCustomerOrders'].Query
        $null = $cmd.Parameters.Add('@RefNumber', $CustomerRefNumber)

        $reader = $cmd.ExecuteReader()

        while($reader.Read()) {

            $obj = [pscustomobject]@{ }

            $columns = $sql['RKCustomerOrders'].Columns

            foreach($column in $columns) {
                $obj = $obj | Add-Member -MemberType NoteProperty -Name $column -Value $reader[$column] -PassThru
            }

            Write-Output $obj
        }
    }
    catch {
        Write-Warning $_
    }
    finally {
        $reader.Close()
        $cmd.Dispose()
        $con.Close()
    }
}

Export-ModuleMember -Function *