<?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 `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);

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'];	
}


define('LWS_ADD','Created a new WS entry for '.$cd['clientname']);
define('LWS_REMOVE','Deleted WS entry for '.$cd['clientname']);
define('LWS_SET','Committed WS entry ('.$wsd['no'].') for '.$cd['clientname']);
define('LWSI_UPDATE','Updated WS entry ('.$wsd['no'].') for '.$cd['clientname']);
define('LWSI_ADD','Added a new item ('. getItem($_REQUEST['itemnumber']).') to '.$wsd['no'].' at '.$cd['clientname']);
define('LWSI_REMOVE','Removed item ('. getItem($_REQUEST['itemnumber']).') from '.$wsd['no'].' at '.$cd['clientname']);
define('LREV_REMOVE','Removed revision entry from '.$wsd['no'].' for '.$cd['clientname']);
define('LWS_DELIVER','Written Scheme '.$wsd['no'].' for '.$cd['clientname'].' was delivered successfully');

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

case "delLWS":
$stmt = $GLOBALS['con']->prepare("DELETE FROM `lwrittenscheme` WHERE `id`=?")or die(mysqli_error($GLOBALS['con']));
$stmt->bind_param('i',$_REQUEST['id']);
$stmt->execute();
$stmt->close();
$stmt = $GLOBALS['con']->prepare("DELETE FROM `lwsitems` WHERE `lwsnumber`=?")or die(mysqli_error($GLOBALS['con']));
$stmt->bind_param('i',$_REQUEST['id']);
$stmt->execute();
$stmt->close();
writeLog("LWS",LWS_REMOVE); 
echo $e."success";
break;

case "setLWS":

//CHECK NUMBERING
$stmt = $GLOBALS['con']->prepare("SELECT `id` FROM `lwrittenscheme` WHERE `clientnumber`='".$_REQUEST['clientnumber']."' ")or die(mysqli_error($GLOBALS['con']));
$stmt->execute();$s=$stmt->get_result();$stmt->close();
$n=mysqli_num_rows($s);
$n=$n+1;
$n = sprintf('%02d',$n);
$wsn="LWS".$n;

// INSERT RECORD
$stmt = $GLOBALS['con']->prepare("INSERT INTO `lwrittenscheme` VALUES('',?,?,'','".$wsn."','','".strtotime('now')."','0','','','0')")or die(mysqli_error($GLOBALS['con']));
$stmt->bind_param('is',$_REQUEST['clientnumber'],$_REQUEST['lwsDesc']);
$stmt->execute();
$stmt->close();
writeLog("LWS",LWS_SET); 
echo $e."success";
break;	

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

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

writeLog("LWSI",LWSI_ADD); 
echo $e."success";
break;	

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

writeLog("LWS",LWSI_REMOVE); 	
}

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

writeLog("LWSI",LWSI_REMOVE); 
echo $e."success";
break;	

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

case "addLWSIE":	
$stmt = $GLOBALS['con']->prepare("INSERT INTO `lwsitems` VALUES('',?,'','".strtotime('now')."','0',?,?)")or die(mysqli_error($GLOBALS['con']));
$stmt->bind_param('iss',$_REQUEST['lwsnumber'],$_REQUEST['desc'],$_REQUEST['swl']);$stmt->execute();$stmt->close();
writeLog("LWS",WSIE_ADD);
    
echo $e."success";
break;

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

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

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

writeLog("LWS",WS_UPDATE); 	
}	
}

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


case "remLRev":
$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("LWS",LREV_REMOVE); 
echo $e."success";
break;	

case"setLWSIDel":
$stmt = $GLOBALS['con']->prepare("UPDATE `lwrittenscheme` SET `del`='0' WHERE `id`=?")or die(mysqli_error($GLOBALS['con']));
$stmt->bind_param('i',$_REQUEST['lwsnumber']);$stmt->execute();$stmt->close();
writeLog("LWS",LWS_DELIVER); 
echo $e."success";
break;
}
?>