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

$_REQUEST['broker_name']=str_replace("'","&#39;",$_REQUEST['broker_name']);
$_REQUEST['pri_contact']=str_replace("'","&#39;",$_REQUEST['pri_contact']);
switch($_REQUEST['FUNCTION']){
default: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 `brokers` VALUES(NULL,?,?,?,?,?,?,?,?,?,'',?,?,?,?,?,?,?,?,'',?,0,'','1')")or die(mysqli_error($GLOBALS['con']));
$stmt->bind_param('ssssssssssssssssss',$_REQUEST['broker_name'],$_REQUEST['address1'],$_REQUEST['address2'],$_REQUEST['town'],$_REQUEST['county'],$_REQUEST['post'],$_REQUEST['tel'],$_REQUEST['fax'],$_REQUEST['email'],$_REQUEST['billing_name'], 

$_REQUEST['billing1'],
$_REQUEST['billing2'],
$_REQUEST['billing_town'],
$_REQUEST['billing_county'],
$_REQUEST['billing_postcode'],
$_REQUEST['billing_phone'],
$_REQUEST['billing_email'], $_REQUEST['pri_contact']);

$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 `brokers` SET `broker_name`=?, `address1`=?, `address2`=?, `town`=?, `county`=?, `post`=?, `tel`=?, `fax`=?, `email`=?, `billing_name`=?, `billing1`=?, `billing2`=?, `billing_town`=?, `billing_county`=?, `billing_postcode`=?, `billing_phone`=?, `billing_email`=?,`password`= AES_ENCRYPT('".$_REQUEST['password']."','".$_REQUEST['email']."'), `pri_contact`=?, `commission_rate`=?, `statuslive`=? WHERE `broker_number`=?")or die(mysqli_error($GLOBALS['con']));
$stmt->bind_param('sssssssssssssssssssii',$_REQUEST['broker_name'],$_REQUEST['address1'],$_REQUEST['address2'],$_REQUEST['town'],$_REQUEST['county'],$_REQUEST['post'],$_REQUEST['tel'],$_REQUEST['fax'],$_REQUEST['email'],$_REQUEST['billing_name'], 

$_REQUEST['billing1'],
$_REQUEST['billing2'],
$_REQUEST['billing_town'],
$_REQUEST['billing_county'],
$_REQUEST['billing_postcode'],
$_REQUEST['billing_phone'],
$_REQUEST['billing_email'], $_REQUEST['pri_contact'],$_REQUEST['commission_rate'],$_REQUEST['statuslive'],$_REQUEST['broker_number']);
$stmt->execute();
$e= $stmt->error;
echo "success";
exit();
break;

case "convert":
    $stmt = $GLOBALS['con']->prepare("SELECT * FROM `brokers` WHERE `broker_number`='".$_REQUEST['id']."'")or die(mysqli_error($GLOBALS['con']));
    $stmt->execute();$cc=$stmt->get_result();$stmt->close();
    $d=mysqli_fetch_array($cc);
    $d['broker_name']=str_replace("'","&#39;",$d['broker_name']);
    $d['pri_contact']=str_replace("'","&#39;",$d['pri_contact']);
    $stmt = mysqli_query($GLOBALS['con'],"INSERT INTO `agents` VALUES(
    NULL,
    '".$d['broker_name']."',
    '".$d['address1']."',
    '".$d['address2']."',
    '".$d['town']."',
    '".$d['county']."',
    '".$d['post']."',
    '".$d['tel']."',
    '".$d['fax']."',
    '".$d['email']."',
    '".$d['password']."',
    '".$d['billing_name']."',     
    '".$d['billing1']."',
    '".$d['billing2']."',
    '".$d['billing_town']."',
    '".$d['billing_county']."',
    '".$d['billing_postcode']."',
    '".$d['billing_phone']."',
    '".$d['billing_email']."',
    '".$d['access']."',
    '".$d['pri_contact']."',
    '".$d['commission_rate']."',
    '".$d['notes']."',
    '".$d['statuslive']."'
    )")or die($e.mysqli_error($GLOBALS['con']));
    
    $stmt = mysqli_query($GLOBALS['con'],"DELETE FROM `brokers` WHERE `broker_number`='".$_REQUEST['id']."'")or die(mysqli_error($GLOBALS['con']));
    break;

    case "archive":
    $stmt = mysqli_query($GLOBALS['con'],"UPDATE `brokers` SET `statuslive`=0 WHERE `broker_number`='".$_REQUEST['id']."'")or die(mysqli_error($GLOBALS['con']));
    break;

    case "reinstate":
        $stmt = mysqli_query($GLOBALS['con'],"UPDATE `brokers` SET `statuslive`=1 WHERE `broker_number`='".$_REQUEST['id']."'")or die(mysqli_error($GLOBALS['con']));
        break;
}?>