<?php if(defined('TENANT_URL') && !empty(TENANT_URL)) {
    $_REQUEST['tenant'] = TENANT_URL;
}


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

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

case "archive":
    $stmt = $GLOBALS['con']->prepare("UPDATE `contractors` SET `statuslive`='0' WHERE `contractor_number`='".$_REQUEST['contractornumber']."'");
    $stmt->execute();
    echo "success";
    break;
    
    case "activate":
    $stmt = $GLOBALS['con']->prepare("UPDATE `contractors` SET `statuslive`='1' WHERE `contractor_number`='".$_REQUEST['contractornumber']."'");
    $stmt->execute();
    echo "success";
    break;

case "insert":
if($_REQUEST['statuslive']=="on"){$_REQUEST['statuslive']="1";}
if($_REQUEST['statuslive']=="off" || $_REQUEST['statuslive']==""){$_REQUEST['statuslive']="0";}
$stmt = $GLOBALS['con']->prepare("INSERT INTO `contractors` VALUES('',?,?,?,?,?,?,?,?,?,'','4',?,'',?,'')")or die(mysqli_error($GLOBALS['con']));
$stmt->bind_param('ssssssssssi',$_REQUEST['contractor_name'],$_REQUEST['address1'],$_REQUEST['address2'],$_REQUEST['town'],$_REQUEST['county'],$_REQUEST['post'],$_REQUEST['tel'],$_REQUEST['fax'],$_REQUEST['email'],$_REQUEST['pri_contact'],$_REQUEST['statuslive']);
$stmt->execute();
$e= $stmt->error;
if(empty($e)){echo  mysqli_insert_id($GLOBALS['con']);}
else{echo "error:".$e;}
$stmt->close();
exit();
break;

case "update":
if($_REQUEST['statuslive']=="on"){$_REQUEST['statuslive']="1";}
if($_REQUEST['statuslive']=="off" || $_REQUEST['statuslive']==""){$_REQUEST['statuslive']="0";}
$stmt = $GLOBALS['con']->prepare("UPDATE `contractors` SET `contractor_name`=?, `address1`=?, `address2`=?, `town`=?, `county`=?, `post`=?, `tel`=?, `fax`=?, `email`=?,`password`= AES_ENCRYPT('".$_REQUEST['password']."','".$_REQUEST['email']."'), `pri_contact`=?, `statuslive`=? WHERE `contractor_number`=?")or die(mysqli_error($GLOBALS['con']));
$stmt->bind_param('ssssssssssii',$_REQUEST['contractor_name'],$_REQUEST['address1'],$_REQUEST['address2'],$_REQUEST['town'],$_REQUEST['county'],$_REQUEST['post'],$_REQUEST['tel'],$_REQUEST['fax'],$_REQUEST['email'],$_REQUEST['pri_contact'],$_REQUEST['statuslive'],$_REQUEST['contractor_number']);
$stmt->execute();
$e= $stmt->error;
echo "success";
exit();
break;

case "add":
    $stmt = $GLOBALS['con']->prepare("UPDATE `clientdetails` SET `contractor`=?  WHERE `clientnumber`=?")or die(mysqli_error($GLOBALS['con']));
    $stmt->bind_param('ii',$_REQUEST['contractor_number'],$_REQUEST['clientnumber']);
    $stmt->execute();
    $e= $stmt->error;
    $stmt = $GLOBALS['con']->prepare("UPDATE `itemdetails` SET `contractor`=?  WHERE `clientnumber`=?")or die(mysqli_error($GLOBALS['con']));
    $stmt->bind_param('ii',$_REQUEST['contractor_number'],$_REQUEST['clientnumber']);
    $stmt->execute();
    $e= $stmt->error;

    echo "success";
    exit();
    break;

case "remove":
    $stmt = $GLOBALS['con']->prepare("UPDATE `clientdetails` SET `contractor`='0'  WHERE `clientnumber`=?")or die(mysqli_error($GLOBALS['con']));
    $stmt->bind_param('i',$_REQUEST['clientnumber']);
    $stmt->execute();
    $e= $stmt->error;
    $stmt = $GLOBALS['con']->prepare("UPDATE `itemdetails` SET `contractor`='0'  WHERE `clientnumber`=?")or die(mysqli_error($GLOBALS['con']));
    $stmt->bind_param('i',$_REQUEST['clientnumber']);
    $stmt->execute();
    $e= $stmt->error;
    echo "success";
    exit();
    break;



}?>