<?php if(file_exists('../../'.$_REQUEST['tenant'].'/config.php')){require_once('../../'.$_REQUEST['tenant'].'/config.php'); }
if(file_exists('/config.php')){require_once('/config.php'); }

$items=json_decode($_REQUEST['items']);


foreach($items as $item){
//$item='21919';
$sql1=mysqli_query($GLOBALS['con'],"SELECT `repnumber` FROM `reportitems` WHERE `itemnumber`='".$item."' AND `clientnumber`='".$_REQUEST['clientnumber']."' ORDER BY abs(repitem) DESC LIMIT 1");
$sd=mysqli_fetch_array($sql1);

$stmt = $GLOBALS['con']->prepare("SELECT `olddate`,`result` FROM `reportitems` WHERE `repnumber`='".$sd['repnumber']."' AND  `itemnumber`='".$item."'")or die(mysqli_error($e=$GLOBALS['con']));
$stmt->execute();$r=$stmt->get_result();$stmt->close();
$repOld=mysqli_fetch_array($r);

$stmt = $GLOBALS['con']->prepare("DELETE  FROM `reportitems` WHERE `repnumber`='".$sd['repnumber']."' AND  `itemnumber`='".$item."'")or die(mysqli_error($e=$GLOBALS['con']));
$stmt->execute();$stmt->close();
unlink('../..'.$_REQUEST['tenant'].'/output/reports/'.$_REQUEST['clientnumber'].'/'.$sd['repnumber'].'_'.$item.'.pdf');

$stmt = $GLOBALS['con']->prepare("SELECT `olddate`,`result` FROM `reportitems` WHERE  `itemnumber`='".$item."' LIMIT 1")or die(mysqli_error($e=$GLOBALS['con']));
$stmt->execute();$r=$stmt->get_result();$stmt->close();
$repCheck=mysqli_fetch_array($r);

if(empty($repCheck['result'])){$repOld['result']=null;}
$stmt = $GLOBALS['con']->prepare("UPDATE `itemdetails` SET `duedate`='".$repOld['olddate']."', `lastReport`='".$repOld['result']."' WHERE `itemnumber`='".$item."'")or die(mysqli_error($e=$GLOBALS['con']));
$stmt->execute();$stmt->close();

}


$stmt=mysqli_query($GLOBALS['con'],"DELETE FROM `rapid_history` WHERE `id`='".$_REQUEST['entry']."'")or die(mysqli_error($GLOBALS['con']));
echo "done";