#
# first of all select all records matching the current records
# articleNr from our price table
$where  = "WHERE articleNr='".xChop($values{articlenr})."'";
$cmd    = "SELECT instock, vendor, price FROM prices $where";
$xret   = $dbconn->prepare($cmd);
$xret->execute || $log->debug("$cmd -- could not get items in stock. SQL Error:$DBI::errstr");

$winner = 9999999;

#
# scan them
while(@array = $xret->fetchrow_array) {
        #
        # and only look at those which refer to articles that are in stock
        if(@array[0] eq "1") {

                #
                # use XChop to remove any trailing blanks and keep
                # the vendor in mind
                $vendor = xChop(@array[1]);

                #
                # then get the shippingCost that is usually billed
                # by this vendor
                $cmd     = "SELECT shippingCost FROM vendors ";
                $cmd    .="WHERE name='$vendor'";
                $lret    = $dbconn->prepare($cmd);
                $lret->execute || $log->debug("could not get shipping cost. SQL Error:$DBI::errstr");

                #
                # and set our $sh variable accordingly
                if(@ldat = $lret->fetchrow_array) {
                        $sh     = @ldat[0];
                } else {
                        $sh     = 0.0;
                }

                #
                # now calculate the total price of the article if purchased
                # from this vendor
                $price = @array[2] + $sh;

                #
                # and if this was the cheapest go ahead and store
                # the reference
                if($price > 0 && $price < $winner) {
                        $winner = $price;
                        $winnm  = $vendor;
                }
        }
}

#
# finally return our result...
unless ($winnm) {
        "0.00 sorry, no price";
} else {
        sprintf("%.2f $winnm", $winner);
}
