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

//GET CLIENT DETAILS
$stmt = $GLOBALS['con']->prepare("SELECT `clientnumber`,`clientname` FROM `clientdetails` WHERE `clientnumber`='".$_REQUEST['clientnumber']."' ")or die(mysqli_error($GLOBALS['con']));
$stmt->execute();$cc=$stmt->get_result();$stmt->close();
$cd=mysqli_fetch_array($cc);

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

function getItem($itemnumber){
$stmt = $GLOBALS['con']->prepare("SELECT `desc` FROM `itemdetails` WHERE `itemnumber`=?")or die(mysqli_error($GLOBALS['con']));
$stmt->bind_param('i',$itemnumber);$stmt->execute();$rc=$stmt->get_result();$stmt->close();	
$cd=mysqli_fetch_array($rc);	
return $cd['desc'];	
}


function getItemsPlant($itemnumber){
$stmt = $GLOBALS['con']->prepare("SELECT `plantnumber` FROM `itemdetails` WHERE `itemnumber`=?")or die(mysqli_error($GLOBALS['con']));
$stmt->bind_param('i',$itemnumber);$stmt->execute();$rc=$stmt->get_result();$stmt->close();	
$cd=mysqli_fetch_array($rc);	
return $cd['plantnumber'];	
}

function getItemsSerial($itemnumber){
$stmt = $GLOBALS['con']->prepare("SELECT `serialnumber` FROM `itemdetails` WHERE `itemnumber`=?")or die(mysqli_error($GLOBALS['con']));
$stmt->bind_param('i',$itemnumber);$stmt->execute();$rc=$stmt->get_result();$stmt->close();	
$cd=mysqli_fetch_array($rc);	
return $cd['serialnumber'];	
}


define('WS_ADD','Created a new WS entry for '.$cd['clientname']);
define('WS_REMOVE','Deleted WS entry for '.$cd['clientname']);
define('WS_SET','Committed WS entry ('.$wsd['no'].') for '.$cd['clientname']);
define('WSI_UPDATE','Updated WS entry ('.$wsd['no'].') for '.$cd['clientname']);
define('WS_UPDATE','Updated WS entry ('.$wsd['no'].') for '.$cd['clientname']);
define('WSI_ADD','Added a new item ('. getItem($_REQUEST['itemnumber']).' - '. getItemsPlant($_REQUEST['itemnumber']).' - '. getItemsSerial($_REQUEST['itemnumber']).') to '.$wsd['no'].' at '.$cd['clientname']);
define('WSI_REMOVE','Removed item ('. getItem($_REQUEST['itemnumber']).' - '. getItemsPlant($_REQUEST['itemnumber']).' - '. getItemsSerial($_REQUEST['itemnumber']).') from '.$wsd['no'].' at '.$cd['clientname']);
define('REV_REMOVE','Removed revision entry from '.$wsd['no'].' for '.$cd['clientname']);
define('WS_DELIVER','Written Scheme '.$wsd['no'].' for '.$cd['clientname'].' was delivered successfully');
define('WS_REVIEW','No changes');




switch ($_REQUEST['mode']){
default:break;

case "delWS":
$stmt = $GLOBALS['con']->prepare("DELETE FROM `writtenscheme` WHERE `id`=?")or die(mysqli_error($GLOBALS['con']));
$stmt->bind_param('i',$_REQUEST['id']);
$stmt->execute();
$stmt->close();
$stmt = $GLOBALS['con']->prepare("DELETE FROM `wsitems` WHERE `wsnumber`=?")or die(mysqli_error($GLOBALS['con']));
$stmt->bind_param('i',$_REQUEST['id']);
$stmt->execute();
$stmt->close();
writeLog("WS",WS_REMOVE); 
echo "success";
break;

case "setWS":
//CHECK NUMBERING
$stmt = $GLOBALS['con']->prepare("SELECT `no` FROM `writtenscheme` WHERE `clientnumber`='".$_REQUEST['clientnumber']."'")or die(mysqli_error($GLOBALS['con']));
$stmt->execute();$s=$stmt->get_result();$stmt->close();
$number_array=array();
while($n=mysqli_fetch_array($s)){
$d=explode(":",$n['no']);
$number=str_replace('WS','',$d[0]);
if($number!="00"){$number_array[]=$number;}
}
if(empty($number_array)){$number_array[]=0;}
$current= max($number_array);
$new=$current+1;
$n = sprintf('%02d',$new);
$wsn="WS".$n;

// INSERT RECORD
if(!empty($_REQUEST['scheme_number_alt'])){
$stmt = $GLOBALS['con']->prepare("INSERT INTO `writtenscheme` VALUES('',?,?,'',?,'".$wsn."','','".strtotime('now')."','0','','','0',?,?,?,'')")or die(mysqli_error($GLOBALS['con']));
$stmt->bind_param('issisi',$_REQUEST['clientnumber'],$_REQUEST['wsDesc'],$_REQUEST['locnumber'],$_REQUEST['wstype'],$_REQUEST['scheme_number_alt'],strtotime($_REQUEST['renewal_date']));
$stmt->execute();
$stmt->close();
echo "success";
}
else{
 $stmt = $GLOBALS['con']->prepare("INSERT INTO `writtenscheme` VALUES('',?,?,'',?,'".$wsn."','','".strtotime('now')."','0','','','0',?,'',?)")or die(mysqli_error($GLOBALS['con']));
$stmt->bind_param('issii',$_REQUEST['clientnumber'],$_REQUEST['wsDesc'],$_REQUEST['locnumber'],$_REQUEST['wstype'],strtotime($_REQUEST['renewal_date']));
$stmt->execute();
$stmt->close();   
   echo "success"; 
}
writeLog("WS",WS_SET); 

break;	

case "saveAlt":
$stmt = $GLOBALS['con']->prepare("UPDATE `writtenscheme` SET `scheme_number_alt`='".$_REQUEST['scheme_number_alt']."' WHERE `id`=?")or die(mysqli_error($GLOBALS['con']));
$stmt->bind_param('i',$_REQUEST['id']);$stmt->execute();$stmt->close();
echo "success";
break;

case "saveSurv":
$stmt = $GLOBALS['con']->prepare("UPDATE `writtenscheme` SET `surveyor`='".$_REQUEST['surveyor']."' WHERE `id`=?")or die(mysqli_error($GLOBALS['con']));
$stmt->bind_param('i',$_REQUEST['id']);$stmt->execute();$stmt->close();
echo "success";
break;


case "addRevision":
$stmt = $GLOBALS['con']->prepare("UPDATE `writtenscheme` SET `rev`=rev+1,`del`='1' WHERE `id`=?")or die(mysqli_error($GLOBALS['con']));
$stmt->bind_param('i',$_REQUEST['wsnumber']);$stmt->execute();$stmt->close();

$desc=str_replace("'","&#39;",$_REQUEST['desc']);

$stmt = $GLOBALS['con']->prepare("INSERT INTO `wsrevisions` VALUES('',?,'".$desc."',".$_REQUEST['surveyor'].",'".strtotime($_REQUEST['date'])."')")or die(mysqli_error($GLOBALS['con']));
$stmt->bind_param('i',$_REQUEST['wsnumber']);$stmt->execute();$stmt->close();
define('WS_UPDATE','Updated WS entry');
writeLog("WS",WS_UPDATE); 


$stmt = $GLOBALS['con']->prepare("SELECT * FROM `wsrevisions` WHERE `wsnumber`=? ORDER BY abs(date) DESC")or die($error=mysqli_error($GLOBALS['con']));
$stmt->bind_param('i',$_REQUEST['wsnumber']);$stmt->execute();$res=$stmt->get_result();$stmt->close();
while($d=mysqli_fetch_array($res)){
$stmt = $GLOBALS['con']->prepare("SELECT `forename`,`surname` FROM `surveyor` WHERE `id`=?")or die($error=mysqli_error($GLOBALS['con']));
$stmt->bind_param('i',$d['surveyor']);$stmt->execute();$res2=$stmt->get_result();$stmt->close();
$e=mysqli_fetch_array($res2);
if($_SESSION['usertype']=="admins"){$del='<a href="javascript:void(0)" class="btn btn-danger btn-sm" onClick="remrevConfirm(\''.$d['id'].'\')" data-toggle="tooltip" data-placement="left" title="Remove this revision entry"><i class="fad fa-times"></i></a>';}
$ret.='<tr id="Rr'.$d['id'].'"><td>'.date("d-m-Y",$d['date']).' <small>'.date("H:m:i",$d['date']).'</small></td><td>'.$d['description'].'</td><td>'.$e['forename'].' '.$e['surname'].'</td><td>'.$del.'</td></tr>';	
}
echo "success|".$ret;
exit();
break;



case "addWSI":
if($wsd['rev']=="0"){}
else{
$stmt = $GLOBALS['con']->prepare("UPDATE `writtenscheme` SET `rev`=rev+1,`del`='0' WHERE `id`=?")or die(mysqli_error($GLOBALS['con']));
$stmt->bind_param('i',$_REQUEST['wsnumber']);$stmt->execute();$stmt->close();
$stmt = $GLOBALS['con']->prepare("INSERT INTO `wsrevisions` VALUES('',?,'".WSI_ADD."',?,'".strtotime('now')."')")or die(mysqli_error($GLOBALS['con']));
$stmt->bind_param('ii',$_REQUEST['wsnumber'],$wsd['surveyor']);$stmt->execute();$stmt->close();
$stmt = $GLOBALS['con']->prepare("UPDATE `writtenscheme` SET `del`='1' WHERE `id`=?")or die(mysqli_error($GLOBALS['con']));
$stmt->bind_param('i',$_REQUEST['wsnumber']);$stmt->execute();$stmt->close();
writeLog("WS",WSI_ADD);
}

$stmt = $GLOBALS['con']->prepare("INSERT INTO `wsitems` VALUES('',?,?,'".strtotime('now')."','1','','')")or die(mysqli_error($GLOBALS['con']));
$stmt->bind_param('ii',$_REQUEST['wsnumber'],$_REQUEST['itemnumber']);$stmt->execute();$stmt->close();

writeLog("WSI",WSI_ADD);
echo "success";
break;	

case "removeWSI":
if($wsd['rev']=="0"){}
else{
$stmt = $GLOBALS['con']->prepare("UPDATE `writtenscheme` SET `rev`=rev+1,`del`='0' WHERE `id`=?")or die(mysqli_error($GLOBALS['con']));
$stmt->bind_param('i',$_REQUEST['wsnumber']);$stmt->execute();$stmt->close();
$stmt = $GLOBALS['con']->prepare("INSERT INTO `wsrevisions` VALUES('',?,'".WSI_REMOVE."',?,'".strtotime('now')."')")or die(mysqli_error($GLOBALS['con']));
$stmt->bind_param('ii',$_REQUEST['wsnumber'],$wsd['surveyor']);$stmt->execute();$stmt->close();
$stmt = $GLOBALS['con']->prepare("UPDATE `writtenscheme` SET `del`='1' WHERE `id`=?")or die(mysqli_error($GLOBALS['con']));
$stmt->bind_param('i',$_REQUEST['wsnumber']);$stmt->execute();$stmt->close();
writeLog("WS",WSI_REMOVE); 	
}

$stmt = $GLOBALS['con']->prepare("DELETE FROM `wsitems` WHERE `wsnumber`=? AND `itemnumber`=?")or die(mysqli_error($GLOBALS['con']));
$stmt->bind_param('ii',$_REQUEST['wsnumber'],$_REQUEST['itemnumber']);
$stmt->execute();
$stmt->close();
writeLog("WSI",WSI_REMOVE); 

$stmt = $GLOBALS['con']->prepare("UPDATE `itemdetails` SET `scheme_number`='',`scheme_number_alt`='',`system_type`='' WHERE `itemnumber`=?")or die(mysqli_error($GLOBALS['con']));
$stmt->bind_param('i',$_REQUEST['itemnumber']);
$stmt->execute();$stmt->close();
echo "success";
break;	

case "setWSI":	
$stmt = $GLOBALS['con']->prepare("UPDATE `writtenscheme` SET `rev`='1' WHERE `id`=?")or die(mysqli_error($GLOBALS['con']));
$stmt->bind_param('i',$_REQUEST['wsnumber']);
$stmt->execute();$stmt->close();
genWSI($_REQUEST['wsnumber']);
echo "success";
break;

case "addWSIE":	
$stmt = $GLOBALS['con']->prepare("INSERT INTO `wsitems` VALUES('',?,'','".strtotime('now')."','0',?,?)")or die(mysqli_error($GLOBALS['con']));
$stmt->bind_param('iss',$_REQUEST['wsnumber'],$_REQUEST['desc'],$_REQUEST['swl']);$stmt->execute();$stmt->close();
writeLog("WS",WSIE_ADD); 
break;

case "removeWSIE":
$stmt = $GLOBALS['con']->prepare("DELETE FROM `wsitems` WHERE `id`=?")or die(mysqli_error($GLOBALS['con']));
$stmt->bind_param('i',$_REQUEST['id']);
$stmt->execute();
$stmt->close();
writeLog("WSI",WSIE_REMOVE); 
echo "success";
break;

case "updateWSI":
if($wsd['rev']=="0"){}
else{
if($wsd['description']==$_REQUEST['description']){}
else{
$stmt = $GLOBALS['con']->prepare("UPDATE `writtenscheme` SET `rev`=rev+1,`del`='1' WHERE `id`=?")or die(mysqli_error($GLOBALS['con']));
$stmt->bind_param('i',$_REQUEST['wsnumber']);$stmt->execute();$stmt->close();

//$stmt = $GLOBALS['con']->prepare("INSERT INTO `wsrevisions` VALUES('',?,'".WS_UPDATE."',?,'".strtotime('now')."')")or die(mysqli_error($e="CREATE WS ENTRY: ".$GLOBALS['con']));
//$stmt->bind_param('ii',$_REQUEST['wsnumber'],$_REQUEST['surveyor']);$stmt->execute();$stmt->close();

writeLog("WS",WS_UPDATE); 	
}	
}

$stmt = $GLOBALS['con']->prepare("UPDATE `writtenscheme` SET `surveyor`=?, `date`=?, `description`=?, `title`=?,`location`=?, `wCat`=?, `wSol`=?, `scheme_number_alt`=?, `renewal`=? WHERE `id`=?")or die(mysqli_error($GLOBALS['con']));
$stmt->bind_param('iissssssii',$_REQUEST['surveyor'],strtotime($_REQUEST['date']),$_REQUEST['description'],$_REQUEST['title'],$_REQUEST['locnumber'],$_REQUEST['wCat'],$_REQUEST['wSol'],$_REQUEST['scheme_number_alt'],strtotime($_REQUEST['renewal_date']),$_REQUEST['wsnumber']);
$stmt->execute();
$stmt->close();
writeLog("WS",WS_UPDATE); 
echo "success";
break;


case "remRev":
$stmt = $GLOBALS['con']->prepare("DELETE FROM `wsrevisions` WHERE `id`=?")or die(mysqli_error($GLOBALS['con']));
$stmt->bind_param('i',$_REQUEST['entry']);
$stmt->execute();
$stmt->close();
writeLog("WS",REV_REMOVE); 
echo "success";
break;	

case"setWSIDel":
$stmt = $GLOBALS['con']->prepare("UPDATE `writtenscheme` SET `del`='0' WHERE `id`=?")or die(mysqli_error($GLOBALS['con']));
$stmt->bind_param('i',$_REQUEST['wsnumber']);$stmt->execute();$stmt->close();
writeLog("WS",WS_DELIVER); 
echo "success";
break;

case"addReview":
$stmt = $GLOBALS['con']->prepare("UPDATE `writtenscheme` SET `rev`=rev+1,`del`='1' WHERE `id`=?")or die(mysqli_error($GLOBALS['con']));
$stmt->bind_param('i',$_REQUEST['wsnumber']);$stmt->execute();$stmt->close();

$stmt = $GLOBALS['con']->prepare("INSERT INTO `wsrevisions` VALUES('',?,'".WS_REVIEW."',?,'".strtotime('now')."')")or die(mysqli_error($GLOBALS['con']));
$stmt->bind_param('ii',$_REQUEST['wsnumber'],$_REQUEST['surveyor']);$stmt->execute();$stmt->close();

writeLog("WS",WS_REVIEW); 
echo "Rsuccess";	
break;
}


?>