<?php $_REQUEST['src']="ext2";require_once('../../functions/global.php');
$c=1;
$date=strtotime('now');
$dateremind=strtotime('now +1 week');

function checkOverdue(){
$stmt = $GLOBALS['con']->prepare("SELECT `clientnumber` FROM `clientdetails` WHERE `statuslive`='1' AND `contractMode`='1' AND `renewal_date`<='".$date."' ORDER BY `clientname` DESC")or die(mysqli_error("LIST: ".$GLOBALS['con']));
$stmt->execute();$cc=$stmt->get_result();$stmt->close();
while($cl=mysqli_fetch_array($cc)){

$stmt = $GLOBALS['con']->prepare("UPDATE `clientdetails` SET `renewal_status`='OVERDUE' WHERE `clientnumber`='".$cl['clientnumber']."'")or die(mysqli_error("LIST: ".$GLOBALS['con']));
$stmt->execute();$stmt->close();
$c++;
}

echo $c;

}

function cleaner($i){
$i=str_replace("'","&#39;",$i);
return $i;
}

function sendNow($r,$s,$m){
$mailExt="auto";
$_REQUEST['recipient_1']=$r;
$_REQUEST['subject']=$s;
$_REQUEST['message']=$m;
require_once('../sendmail.php');
}

function genReminder(){
$date=strtotime('now');
$stmt = $GLOBALS['con']->prepare("SELECT * FROM `clientdetails` WHERE (`statuslive`='1' AND `contractMode`='1') AND `renewal_date` BETWEEN '$now' AND '$dateremind' ")or die(mysqli_error("LIST: ".$GLOBALS['con']));
$stmt->execute();$cc=$stmt->get_result();$stmt->close();
$num=mysqli_num_rows($cc);
while($cl=mysqli_fetch_array($cc)){
sendNow($cl['email'],'Contract Renewal','Your contract <b>'.$cl['contract_number'].'</b> is due for renewal in one week.');
}

}



function genInvoice(){
$date=strtotime('now');
$stmt = $GLOBALS['con']->prepare("SELECT * FROM `clientdetails` WHERE `statuslive`='1' AND `renewal_date`<='".$date."' ")or die(mysqli_error("LIST: ".$GLOBALS['con']));
$stmt->execute();$cc=$stmt->get_result();$stmt->close();
$num=mysqli_num_rows($cc);
while($cl=mysqli_fetch_array($cc)){

$_REQUEST['address']=serialize($address);

if($cl['commission']!="" && $cl['commission']!="0.00"){
$_REQUEST['sumTotal']=$cl['rate_per_hour']-$cl['commission'];
$address = array(
'client_name' => cleaner($cl['clientnumber']),
'billing_name'  => cleaner($cl['billing_name']),
'billing1'  => cleaner($cl['billing1']),
'billing2'  => cleaner($cl['billing2']),
'billing3'  => cleaner($cl['billing3']),
'billing_town'  => cleaner($cl['billing_town']),
'billing_county'  => cleaner($cl['billing_county']),
'billing_postcode'  => cleaner($cl['billing_postcode']),
'email'  => $cl['email'],
'contract_number'  => cleaner($cl['contract_number']),
'agent_number'  => cleaner($cl['agent_number']),
'agent_name'  => getAgentName($cl['agent_number']),
'commission'  => number_format($cl['commission'],2),
);

}
else{
$_REQUEST['sumTotal']=$cl['rate_per_hour'];
$address = array(
'client_name' => cleaner($cl['clientnumber']),
'billing_name'  => cleaner($cl['billing_name']),
'billing1'  => cleaner($cl['billing1']),
'billing2'  => cleaner($cl['billing2']),
'billing3'  => cleaner($cl['billing3']),
'billing_town'  => cleaner($cl['billing_town']),
'billing_county'  => cleaner($cl['billing_county']),
'billing_postcode'  => cleaner($cl['billing_postcode']),
'email'  => $cl['email'],
'contract_number'  => cleaner($cl['contract_number']),
'agent_number'  => cleaner($cl['agent_number']),
'agent_name'  => getAgentName($cl['agent_number']),
);

}

$_REQUEST['address']=serialize($address);

$_REQUEST['vatTotal']=$_REQUEST['sumTotal']*0.2;

$_REQUEST['filter']="Contract Renewal";

$date=strtotime('today');
$ndate=date('d-m-Y');
$newDate = new DateTime($ndate);
$newDate->add(new DateInterval('P30D'));
$setDate=$newDate->format('d-m-Y');
$dueDate=strtotime($setDate);

$stmt = $GLOBALS['con']->prepare("INSERT INTO `invoices` VALUES(NULL,'".$cl['clientnumber']."','".$cl['agent_number']."','".$_REQUEST['address']."','".$_REQUEST['items']."','".$_REQUEST['sumTotal']."|".$_REQUEST['vatTotal']."','0.2','".$date."','".$dueDate."','','','1','','','".$cl['commission']."','0','".$_REQUEST['filter']."')")or die("1".mysqli_error($GLOBALS['con']));
$stmt->execute();$iv=$stmt->get_result();$stmt->close();


}
echo $num;
}
echo date('d-m-Y',$dateremind);
//genInvoice();
genReminder();
