<?php ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
if(file_exists('../../../'.$_REQUEST['tenant'].'/config.php')){require_once('../../../'.$_REQUEST['tenant'].'/config.php');}
if(file_exists('/config.php')){require_once('/config.php');}

require_once('../../includes/master/form_functions.php');

function getWsItemReportDetails($repnumber,$type){
$stmt = $GLOBALS['con']->prepare("SELECT `reptype`,`insdate`,`repdate` FROM `reportitems` WHERE `repnumber`=?")or die(mysqli_error($GLOBALS['con']));
$stmt->bind_param('i',$repnumber);$stmt->execute();$rc=$stmt->get_result();$stmt->close();	
$cd=mysqli_fetch_array($rc);	
switch($type){
case"REPNO":return $cd['reptype'];break;
case"EXAM_DATE":return date('d-m-Y',$cd['insdate']);break;
case"REPORT_DATE":return date('d-m-Y',$cd['insdate']);break;
}
}

function getWsSurveyorDetails($surveyor,$type){
$stmt2 = $GLOBALS['con']->prepare("SELECT `surname`,`forename`,`qual` FROM `surveyor` WHERE `id`=?")or die(mysqli_error($GLOBALS['con']));
$stmt2->bind_param('i',$surveyor);$stmt2->execute();$sc=$stmt2->get_result();$stmt2->close();	
$sd=mysqli_fetch_array($sc);
switch($type){	
case"SURVEYOR_NAME":return $sd['forename']." ".$sd['surname'];break;
case"SURVEYOR_QUAL":return $sd['qual'];break;
case"SURVEYOR_SIGNITURE":return $surveyor;break;
}
}
//GET WS DETAILS

$wsno="LWS".$_REQUEST['lwsnumber'];
$stmt = $GLOBALS['con']->prepare("SELECT * FROM `lwrittenscheme` WHERE `id`=?")or die(mysqli_error($GLOBALS['con']));
$stmt->bind_param('i',$_REQUEST['lwsnumber']);$stmt->execute();$ws=$stmt->get_result();$stmt->close();
$wsd=mysqli_fetch_array($ws);

//GET CLIENT DETAILS
$stmt = $GLOBALS['con']->prepare("SELECT `clientnumber`,`clientname`,`address1`,`address2`,`town`,`county`,`postcode`,`contract_number` FROM `clientdetails` WHERE `clientnumber`='".$wsd['clientnumber']."' ")or die(mysqli_error("GET WS CLIENT: ".$GLOBALS['con']));
$stmt->execute();$cc=$stmt->get_result();$stmt->close();
$cd=mysqli_fetch_array($cc);

//CHECK DIR AND POPULATE
if(!file_exists('../../../'.TENANT_URL.'/output/lwsfiles/'.$_REQUEST['clientnumber'])){
mkdir('../../../'.TENANT_URL.'/output/lwsfiles/'.$_REQUEST['clientnumber']);	
}

//BUILD ITEM LIST
$c=1;

$string = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
$stmt = $GLOBALS['con']->prepare("SELECT * FROM `lwsitems` WHERE `lwsnumber`=? AND `type`='1'")or die(mysqli_error($GLOBALS['con']));
$stmt->bind_param('i',$_REQUEST['lwsnumber']);$stmt->execute();$wsi=$stmt->get_result();$stmt->close();
while($wsid=mysqli_fetch_array($wsi)){
$a=0;
$stmt = $GLOBALS['con']->prepare("SELECT `clientnumber`,`itemnumber`,`desc`,`serialnumber`,`plantnumber`,`locnumber`,`manf`,`dateofmanf`,`swl`,`valve`,`gauge`,`thorough`,`intermediate` FROM `itemdetails` WHERE `itemnumber`=?")or die(mysqli_error("ITEM RECALL: ".$GLOBALS['con']));
$stmt->bind_param('i',$wsid['itemnumber']);$stmt->execute();$icx=$stmt->get_result();$stmt->close();
$itd=mysqli_fetch_array($icx);

$wsList.='
<tr valign="top">
<td>'.$itd['desc'].' </td>
<td align="center">'.$itd['serialnumber'].'</td>
<td align="center">'.$itd['plantnumber'].'</td>
<td align="center">'.$wsd['wSol'].'</td>
</tr>
';

    if($itd['valve']!=""){
        $ar=array();
        $ar=unserialize($itd['valve']);
        if(is_array($ar)){
            foreach($ar as $ad){
                $char = $string[$a++];
                $wsList.='
<tr valign="top">
<td>00'.$c.$char.'</td>
<td>Safety Valve<br>'.$ad['serial'].'<br>'.$ad['make'].'</td>
<td>'.$ad['range'].'</td>
<td align="center">'.$itd['thorough'].'</td>
<td align="center">'.$itd['intermediate'].'</td>
<td>PSR/02</td>
</tr>
';
            }}
    }

    if($itd['gauge']!=""){
        $ar=array();
        $ar=unserialize($itd['gauge']);
        if(is_array($ar)){
            foreach($ar as $ad){
                $char = $string[$a++];
                $wsList.='
<tr valign="top">
<td>00'.$c.$char.'</td>
<td>Pressure Gauge<br>'.$ad['serial'].'<br>'.$ad['make'].'</td>
<td>'.$ad['range'].'</td>
<td align="center">'.$itd['thorough'].'</td>
<td align="center">'.$itd['intermediate'].'</td>
<td>PSR/02</td>
</tr>
';
            }}
        $char++;
    }

    $c++;
}

//BUILD EXCLUSION LIST
$stmt = $GLOBALS['con']->prepare("SELECT * FROM `lwsitems` WHERE `lwsnumber`=? AND `type`='0'")or die(mysqli_error($GLOBALS['con']));
$stmt->bind_param('i',$_REQUEST['lwsnumber']);$stmt->execute();$wsi=$stmt->get_result();$stmt->close();
while($wsid=mysqli_fetch_array($wsi)){
$wsexList.='
<tr valign="top">
<td>00'.$wsid['desc'].' </td>
<td>'.$wsid['serialnumber'].'</td>
<td align="center">'.$wsid['thorough'].'</td>
</tr>
';
$c++;
}

//BUILD REVISION LIST
$stmt = $GLOBALS['con']->prepare("SELECT * FROM `lwsrevisions` WHERE `lwsnumber`=? ORDER BY ABS(date) DESC")or die(mysqli_error($GLOBALS['con']));
$stmt->bind_param('i',$_REQUEST['lwsnumber']);$stmt->execute();$wsi=$stmt->get_result();$stmt->close();
while($wsid=mysqli_fetch_array($wsi)){
$revList.='
<tr valign="top">
<td>'.date('d-m-Y',$wsid['date']).' </td>
<td>'.$wsid['description'].'</td>
<td><img src="../../../'.TENANT_URL.'/images/signatures/'.$wsid['surveyor'].'.jpg" style="width:120px; margin: 0; padding: 0;" /></td>
</tr>
';

}

if(!is_dir('../../../'.TENANT_URL.'/output/lwsfiles/'.$_REQUEST['clientnumber'])){mkdir('../../../'.TENANT_URL.'/output/lwsfiles/'.$_REQUEST['clientnumber']);}
$filestore='../../../'.TENANT_URL.'/output/lwsfiles/'.$_REQUEST['clientnumber'].'/'.$_REQUEST['lwsnumber'].'.pdf';
include('../../includes/engines/lws.pdf.engine');	
?>