// Checked for PHP5.6 / 8.x compatibility
<?php
$companyshow = array();
$currencysymbol = array();
$pricelistcodelist = array();
$currencycodelist = array();
$bankcursymbol = array();
$bankcur = array();
$lastreviewedpriceon = array();
$lastreviewedpriceby = array();
$excludeme = array();

include("../../operainfo/getq3.php");

$x = getsecurity();

$hostcompanyid = safetext2(isset($_GET['hostcompanyid']) ? $_GET['hostcompanyid'] : '');
$revdate       = safetext2(isset($_GET['revdate']) ? $_GET['revdate'] : '');
$companyid     = safetext2(isset($_GET['companyid']) ? $_GET['companyid'] : '');

if ($companyid != '') {
    $sql = "SELECT * FROM viamed_customer_stock_references WHERE companyid = ? AND status > ?";
    $stmt = pdoquery($sql, array($companyid, '0'));
    while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
        $usestk = $row['stock_ref'];
        $companyshow[$usestk] = 1;
    }
}
?>

<script type="text/javascript">
// Checked for PHP5.6 / 8.x compatibility
function pricereviewed(pricelist , stockref , divid) {
    document.getElementById(divid).innerHTML = "done";
    var link = "/intranet/databases/stock_extra/updatepricereview.php<?php print "$x&hostcompanyid=$hostcompanyid";?>&stkref=" + stockref + "&pricelist=" + pricelist;

    var xmlhttp;
    if (window.XMLHttpRequest) {
        xmlhttp = new XMLHttpRequest();
    } else {
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }

    xmlhttp.open("GET", link, true);
    xmlhttp.send();

    xmlhttp.onreadystatechange = function() {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            document.getElementById(divid).innerHTML = xmlhttp.responseText;
        }
    }
}
</script>

<?php
$mktimelimit = mktime(1, 1, 1, substr($revdate, 5, 2), substr($revdate, 8, 2), substr($revdate, 0, 4));

if ($hostcompanyid < 1) {
    echo "Missing Host";
    return;
}

// Checked for PHP5.6 / 8.x compatibility

$resp = "";
$activepercent = safetext2(isset($_GET['percent']) ? $_GET['percent'] : '') + 0.0;
if ($activepercent < 1) {
    $activepercent = 10;
}

print "Comparing Buying Price (Supplier stock references price) <b>PLUS</b> $activepercent% to Single 1 Off Price in Pricing Table<br>";

// Load company currency info
$sql = "SELECT * FROM global_companys_currencys WHERE hostcompanyid = ?";
$stmt = pdoquery($sql, array($hostcompanyid));
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
    $currencyid = $row['id'];
    $currencysymbol[$currencyid] = $row['currency'];
}

// NOTE: Unused result loop below can be removed
$sql = "SELECT * FROM viamedquote_purchase_orders";
$stmt = pdoquery($sql);
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
    // Possibly legacy loop - no action
}

// Bank Account Pricing Rules
$sql = "SELECT * FROM global_companys_bankaccounts WHERE companyid = ?";
$stmt = pdoquery($sql, array($hostcompanyid));
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
    $pricelistcode = $row['pricelist'];
    $currencycode = $row['currency_id'];
    $pricelistcodelist[$pricelistcode] = $currencycode;
    $currencycodelist[$currencycode] = $pricelistcode;
    $bankcursymbol[$currencycode] = $row['bankcurrsymbol'];
    $bankcursymbol[$pricelistcode] = $row['bankcurrsymbol'];
    $bankcur[$pricelistcode] = $row['currency'];
}

// Price Review Metadata
$sql = "SELECT * FROM specialprices_reviews WHERE hostcompanyid = ? AND userawareofchangefrom = ?";
$stmt = pdoquery($sql, array($hostcompanyid, '0'));
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
    $pllink = $row['pricelist'] . "Z" . $row['stockref'];
    $lastreviewedpriceon[$pllink] = $row['reviewedon'];
    $lastreviewedpriceby[$pllink] = $row['reviewedby'];
}

// Currency Rates
$sql = "SELECT * FROM balances WHERE usd_xrate > ? AND euro_xrate > ? ORDER BY id DESC LIMIT 1";
$stmt = pdoquery($sql, array('0', '0'));
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
    $eurorate = $row['euro_xrate'];
    $usdrate = $row['usd_xrate'];
}

// Superseded Stock
$sql = "SELECT * FROM stocklist_tarriffs WHERE SUPERSEDED <> ?";
$stmt = pdoquery($sql, array(''));
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
    $excludeme[$row['stock_ref']] = $row['SUPERSEDED'];
}

print "<h1>USD $usdrate  EURO $eurorate</h1>";

print "<table>
<tr>
<td>PriceList</td><td>Supplier Currency Code</td><td>Stock Ref</td>
<td>Supplier Price</td><td>PriceList Price</td><td>COMPARE BUY</td>
<td>Add $activepercent %</td><td>COMPARE SALES</td>
<td>Last Reviewed</td><td></td><td>Superceed</td>
</tr>";

// Main Comparison Logic
$sql = "SELECT * FROM viamed_customer_stock_references, specialprices 
        WHERE viamed_customer_stock_references.hostcompanyid = ? 
        AND specialprices.hostcompanyid = ? 
        AND viamed_customer_stock_references.currenyid > '0' 
        AND viamed_customer_stock_references.rawprice > '0' 
        AND viamed_customer_stock_references.stock_ref = specialprices.Stock_ref 
        ORDER BY viamed_customer_stock_references.stock_ref, 
                 specialprices.Pricelist, 
                 viamed_customer_stock_references.currenyid";

$stmt = pdoquery($sql, array($hostcompanyid, $hostcompanyid));
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
    $currencycode = $row['currenyid'];
    $pricelistcode = $row['Pricelist'];
    $stockref = $row['Stock_ref'];
    $pricelistlink = $pricelistcode . "Z" . $stockref;

    $pricedispl = $row['Price'] / 100;

    // Currency conversions
$comparesaleprice = 0;

if (isset($bankcur[$pricelistcode])) {
    if ($bankcur[$pricelistcode] == 'GBP') {
        $comparesaleprice = $pricedispl;
    } elseif ($bankcur[$pricelistcode] == 'EUR') {
        $comparesaleprice = $pricedispl / $eurorate;
    } elseif ($bankcur[$pricelistcode] == 'USD') {
        $comparesaleprice = $pricedispl / $usdrate;
    }
}
$comparebuyprice = 0;

if (isset($currencysymbol[$currencycode])) {
    if ($currencysymbol[$currencycode] == 'GBP') {
        $comparebuyprice = $row['rawprice'];
    } elseif ($currencysymbol[$currencycode] == 'EURO') {
        $comparebuyprice = $row['rawprice'] / $eurorate;
    } elseif ($currencysymbol[$currencycode] == 'USD') {
        $comparebuyprice = $row['rawprice'] / $usdrate;
    }
}


    $fifteen = (($comparebuyprice / 100) * $activepercent) + $comparebuyprice;
    $companyshow[$stockref]  = isset ( $companyshow[$stockref]  ) ? $companyshow[$stockref]  : 0 ;
    if ($companyid == '' || $companyshow[$stockref] == 1) {
        if ($comparesaleprice <= $fifteen && $comparesaleprice != 0 && $pricedispl != -1) {

            $rowcolor = ($comparesaleprice < $comparebuyprice) ? $pastelred : $pastelyellow;
            print "<tr bgcolor=\"$rowcolor\" onMouseOver=\"this.bgColor='#d4deea';\" onMouseOut=\"this.bgColor='$rowcolor';\">";

            $p1 = number_format($comparebuyprice, 2, '.', ',');
            $p2 = number_format($fifteen, 2, '.', ',');
            $p3 = number_format($comparesaleprice, 2, '.', ',');

            $divid = md5($pricelistlink);
            $datereviewed = isset($lastreviewedpriceon[$pricelistlink]) && $lastreviewedpriceon[$pricelistlink] > 0
                            ? date("d M Y", $lastreviewedpriceon[$pricelistlink])
                            : "Pre System";
            $excludeme[$stockref] = isset ( $excludeme[$stockref] ) ? $excludeme[$stockref] : '' ;
            print "<td>$pricelistcode {$bankcur[$pricelistcode]}</td>
<td>.{$currencysymbol[$currencycode]} $currencycode .</td>
<td>".linkstkref($x, $stockref, $float, $size)." $stockref</td>
<td>{$bankcursymbol[$currencycode]} {$row['rawprice']}</td>
<td>{$bankcursymbol[$pricelistcode]} $pricedispl</td>
<td>£ $p1</td>
<td>£ $p2</td>
<td>£ $p3</td>
<td><div id='$divid'>$datereviewed</div></td>
<td><input type='button' onclick=\"pricereviewed('$pricelistcode', '$stockref', '$divid')\" value='Reviewed'></td>
<td>{$excludeme[$stockref]}</td>";
            print "</tr>";
        }
    }
}
print "</table>";

echo $resp;
?>
