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

require('../asset/asset_functions.php');


//LOAD WS DATA

$stmt = $GLOBALS['con']->prepare("SELECT * FROM `writtenscheme` WHERE `id`=? ")or die(mysqli_error($GLOBALS['con']));
$stmt->bind_param('i',$_REQUEST['ws']);$stmt->execute();$s=$stmt->get_result();$stmt->close();
$n=mysqli_fetch_array($s);

// CHECK REV STATUS & SET SAVEMODE
if($n['rev']=="0"){$active="no"; $savemode="standalone";}
else{$savemode="revise";}
if($n['del']=="0"){$alertDelivery='none';}
else{$alertDelivery='block';}
//GET CLIENT DETAILS
$stmt = $GLOBALS['con']->prepare("SELECT `clientnumber`,`clientname`,`contract_number`,`email` FROM `clientdetails` WHERE `clientnumber`='".$n['clientnumber']."' ")or die(mysqli_error($GLOBALS['con']));
$stmt->execute();$cc=$stmt->get_result();$stmt->close();
$cd=mysqli_fetch_array($cc);

//LOAD WS CURRENT LIST
$ilist=array();
$itable=array();
$stmt = $GLOBALS['con']->prepare("SELECT * FROM `wsitems` WHERE `wsnumber`=? AND `type`='1'")or die(mysqli_error($GLOBALS['con']));
$stmt->bind_param('i',$n['id']);$stmt->execute();$cic=$stmt->get_result();$stmt->close();
while($cicl=mysqli_fetch_array($cic)){$ilist[]=$cicl['itemnumber'];$itable[]=$cicl;}

foreach ($ilist as $key => $value) {
$istmt = $GLOBALS['con']->prepare("SELECT `scheme_number_alt` FROM `itemdetails` WHERE `itemnumber`='".$value."'")or die(mysqli_error($GLOBALS['con']));
$istmt->execute();$iic=$istmt->get_result();$istmt->close();
$iicd=mysqli_fetch_array($iic);
if(!empty($iicd['scheme_number_alt'])){$wsalt=$iicd['scheme_number_alt'];}
}

//LOAD LWS CURRENT LIST
$lilist=array();
$stmt = $GLOBALS['con']->prepare("SELECT * FROM `lwsitems` WHERE `lwsnumber`=? AND `type`='1'")or die(mysqli_error($GLOBALS['con']));
$stmt->bind_param('i',$n['id']);$stmt->execute();$cic=$stmt->get_result();$stmt->close();
while($cicl=mysqli_fetch_array($cic)){$lilist[]=$cicl['itemnumber'];}

//LOAD CLIENT ITEMS
$stmt = $GLOBALS['con']->prepare("SELECT `clientnumber`,`itemnumber`,`desc`,`serialnumber`,`plantnumber`,`locnumber` FROM `itemdetails` WHERE `clientnumber`=? AND `statuslive`='1' AND `system_type`!=''")or die(mysqli_error($GLOBALS['con']));
$stmt->bind_param('i',$n['clientnumber']);$stmt->execute();$ic=$stmt->get_result();$stmt->close();


if($n['wstype']==1){require('../ws/ws_existing.php');}
else{require('../ws/ws_enrep.php');}
?>