<?php $_REQUEST['src']="ext2";require_once('../../functions/global.php');
$record=file_get_contents('renewals_due.txt');
$record=explode("|",$record);
if(strtotime($record[0]) < strtotime('today')){

function countdown($renewal){
    $renewal_date = date('d-m-Y',$renewal); 
    $current_date = date("d-m-Y");
    $datetime1 = new DateTime($current_date );    
    $datetime2 = new DateTime($renewal_date);    
    $difference = $datetime1->diff($datetime2);    
    $days=$difference->format("%r%a");
    
    if(($days<30 && $days>0) && (date('m',$renewal)==date("m")) ){return '<div class="alert alert-warning text-center" style="padding:8px">Renewal due in '.$days.' days ('.$renewal_date.')</div>';}
    if(date('m-Y',$renewal)==date("m-Y")){return '<div class="alert alert-danger text-center" style="padding:8px">Renewal due in '.$days.' days ('.$renewal_date.')</div>';}
    else{return 'NULL';}
    }
    
    $stmt = $GLOBALS['con']->prepare("SELECT * FROM `clientdetails` ORDER BY abs(renewal_date) ASC")or die(mysqli_error("GET_INV_NO: ".$GLOBALS['con']));
    $stmt->execute();$iv=$stmt->get_result();$stmt->close();
    while($list=mysqli_fetch_array($iv)){
    if($list['renewal_date']!=="" && $list['renewal_status']!="LAPSED"){
    $status=countdown($list['renewal_date']);
    
    if($status=="NULL"){}else{
    $output.='<tr id="R'.$list['clientnumber'].'">
    <td>
    <div class="row">
    <div class="col-sm-4 col-md-4"><p class="lead mt-2">'.$list['clientname'].'</p></div>
    <div class="col-sm-7 col-md-7">'.$status.'</div>
    <div class="col-sm-1 col-md-1"><a href="javascript:void(0)" onClick="setRenew(\''.$list['clientnumber'].'\',\''.$list['clientname'].'\')" class="btn btn-secondary" data-mdb-tooltip-init data-mdb-placement="bottom" data-mdb-trigger="hover"  title="Confirm Contract Renewal" title="Confirm Contract Renewal"><i class="fad fa-hands-helping"></i></a></div>
    </div>
    </td>
    </tr>';
    }
    }
    }
    $renewalFile = fopen("renewals_due.txt", "w");
    fwrite($renewalFile, date('d-m-Y')."|".$output);
    fclose($renewalFile);

    }
    echo "done";
?>
