<?php
// Checked for PHP5.6 / 8.x compatibility

$stksearch = " ( 1=2 ";
$serialsearch = " ( 1=2 ";

$sql = "SELECT * FROM supplier_items WHERE SuppAccount = ? GROUP BY OperaReference ORDER BY OperaReference";
$stmt = pdoquery($sql, array($opera));
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);

foreach ($rows as $row) {
    $stkref = $row['OperaReference'];
    $stocklist[$stkref] = $stkref;
    $stksearch .= " OR stock_ref='" . $stkref . "'";
    $serialsearch .= " OR Stock_ref='" . $stkref . "'";
}

$stksearch .= " ) and stock_ref<>''";
$serialsearch .= " ) ";

$sql2 = "SELECT * FROM viamed_customer_repairs_clean_faults";
$stmt2 = pdoquery($sql2);
$rows2 = $stmt2->fetchAll(PDO::FETCH_ASSOC);

foreach ($rows2 as $row) {
    $stkref = $row['stock_ref'];
    $actualfault = $row['actual_fault'];
    $link = md5($stkref . $actualfault);
    $isfault[$link] = $row['true_fault'];
}

// Find last header under the date!
$hedsearch = "( 1=2 ";
$sql3 = "SELECT * FROM viamed_customer_repairs_header WHERE bookedin > '" . $lastreviewstartdate . "'";
$stmt3 = pdoquery($sql3);
$rows3 = $stmt3->fetchAll(PDO::FETCH_ASSOC);

foreach ($rows3 as $row) {
    $hed = $row['id'];
    $hedsearch .= " OR repair_header_id='" . $row['id'] . "'";
}
$hedsearch .= " ) ";

$sql4 = "SELECT * FROM viamed_customer_repairs_details WHERE " . $stksearch . " AND " . $hedsearch;
$stmt4 = pdoquery($sql4);
$rows4 = $stmt4->fetchAll(PDO::FETCH_ASSOC);

foreach ($rows4 as $row) {
    if ($row['fault_code'] == '') {
        $row['fault_code'] = 'Needs Evaluating';
    }

    if ($row['status_date'] > 0) {
        $useyear = date("Y", (int)$row['status_date']);
    } else {
        $tttid = $row['repair_header_id'];
        $useyear = date("Y", (int)(isset($backupdate[$tttid]) ? $backupdate[$tttid] : 0));
    }
    if ($useyear + 0 < $earyyear && (int)$row['status_date'] > 0) {
        $earyyear = $useyear + 0;
    }

    $usecode = $row['fault_code'];
    $stkref = $row['stock_ref'];
    $uselink = $useyear . "zz" . $stkref . "zz" . $usecode;
    $uselink = str_replace(" ", "", $uselink);
    $stkyearlink = md5($useyear . "axa" . $stkref);

    $link = md5($stkref . $row['fault_code']);
    if (isset($isfault[$link]) && $isfault[$link] == 'Fault') {
        $countfaults[$uselink] = isset($countfaults[$uselink]) ? $countfaults[$uselink] + 1 : 1;
        $countfaults[$stkyearlink] = isset($countfaults[$stkyearlink]) ? $countfaults[$stkyearlink] + 1 : 1;
    }
}

print "<table class=n3 border =1><tr><td>Stock Ref</td><td>Fault</td><td>Is Fault?</td>";
$startyear = $earyyear;
$endyear = date("Y") + 1;
while ($startyear < $endyear) {
    print "<td> $startyear &nbsp</td>";
    $startyear = $startyear + 1;
}
print "</tr>";

$sql5 = "SELECT * FROM viamed_customer_repairs_details WHERE " . $stksearch . " AND " . $hedsearch . " GROUP BY stock_ref, fault_code ORDER BY stock_ref, fault_code";
$stmt5 = pdoquery($sql5);
$rows5 = $stmt5->fetchAll(PDO::FETCH_ASSOC);

foreach ($rows5 as $row) {
    if ($row['fault_code'] == '') {
        $row['fault_code'] = 'Needs Evaluating';
    }
    $link = md5($row['stock_ref'] . $row['fault_code']);
    $isfaultValue = isset($isfault[$link]) ? $isfault[$link] : '';
    print "<tr><td>" . linkstkref($x, $row['stock_ref'], '', 12, '') . $row['stock_ref'] . "</td><td>" . $row['fault_code'] . "</td><td>" . $isfaultValue . "</td>";
    $startyear = $earyyear;
    $endyear = date("Y") + 1;

    while ($startyear < $endyear) {
        $useyear = $startyear;
        $usecode = $row['fault_code'];
        $stkref = $row['stock_ref'];

        $uselink = $useyear . "zz" . $stkref . "zz" . $usecode;
        $uselink = str_replace(" ", "", $uselink);
        $countFaultsValue = isset($countfaults[$uselink]) ? $countfaults[$uselink] : 0;
        print "<td>" . $countFaultsValue . "</td>";
        $rowtoal[$stkref] = isset($rowtoal[$stkref]) ? $rowtoal[$stkref] + $countFaultsValue : $countFaultsValue;
        $startyear = $startyear + 1;
    }
}

print "</table>";

print "Summary<br><table border=1 class=n3><td>Reference</td><td>Total In System</td><td>QA Fails</td><td>Customer Fails</td><td>Total Fails</td><td>%</td></tr>";

if (isset($rowtoal) && $rowtoal != null) {
    foreach ($rowtoal as $stkref => $value) {
        $totalFailValue = isset($totalfail[$stkref]) ? $totalfail[$stkref] : 0;
        $rt = $rowtoal[$stkref] + $totalFailValue + 0;
        $totValue = isset($tot[$stkref]) ? $tot[$stkref] : 0;
        if ($rt > 0 && $totValue > 0) {
            $p = ($rt / $totValue) * 100;
            $pc = number_format($p, 2, '.', ' ');
        } else {
            $pc = 0;
        }
        print "<tr><td>" . linkstkref($x, $stkref, '', 12, '') . $stkref . "</td><td>" . $totValue . "</td><td>" . $totalFailValue . "</td><td>" . $rowtoal[$stkref] . "</td><td>" . $rt . "</td><td>" . $pc . "%</td></tr>";
    }
}
print "</table>";
?>
