include "../common.inc.php";
if(array_key_exists('action', $_POST)){
switch($_POST['action']){
case 'rollover':
// error check the data that's getting posted
$year = $_POST['year'];
if(!is_numeric($year)){
error_("Not a valid year");
break;
}
if($year <= $config['FISCALYEAR']){
error_("The new fiscal year must be after the current one");
break;
}
// ok, the request checks out, let's go ahead and do the rollover
echo rolloverfiscalyear($year);
break;
default:
}
exit;
}
send_header("Fiscal Rollover");
//echo "not implemented yet";
draw_body();
send_footer();
exit;
function draw_body(){
global $config;
?>
echo "
";
echo "".i18n("You should consider making a database backup before rolling over, just in case!")."
\n";
echo "
";
echo "
";
echo "";
}
function rolloverfiscalyear($newYear){
global $config;
$oldYear = $config['FISCALYEAR'];
$yearDiff = $newYear - $oldYear;
// first we'll roll over fundraising_campaigns:
$fields = "`name`,`type`,`startdate`,`enddate`,`followupdate`,`active`,`target`,`fundraising_goal`,`filterparameters`";
$q = mysql_query("SELECT $fields FROM fundraising_campaigns WHERE fiscalyear = $oldYear");
while(mysql_error() == null && $r = mysql_fetch_assoc($q)){
foreach(array('startdate','enddate','followupdate') as $dateField){
$dateval = $r[$dateField];
$parts = explode('-', $dateval);
if($parts[0] != '0000')
$parts[0] += $yearDiff;
$r[$dateField] = implode('-', $parts);
}
$r['fiscalyear'] = $newYear;
$fields = array_keys($r);
$values = array_values($r);
foreach($values as $idx => $val){
$values[$idx] = mysql_real_escape_string($val);
}
$query = "INSERT INTO fundraising_campaigns (`" . implode("`,`", $fields) . "`) VALUES('" . implode("','", $values) . "')";
mysql_query($query);
}
// next we'll hit findraising_donor_levels
$fields = "`level`,`min`,`max`,`description`";
if(mysql_error() == null)
$q = mysql_query("SELECT $fields FROM fundraising_donor_levels WHERE fiscalyear = $oldYear");
while(mysql_error() == null && $r = mysql_fetch_assoc($q)){
$r['fiscalyear'] = $newYear;
$fields = array_keys($r);
$values = array_values($r);
foreach($values as $idx => $val){
$values[$idx] = mysql_real_escape_string($val);
}
$query = "INSERT INTO fundraising_donor_levels (`" . implode("`,`", $fields) . "`) VALUES('" . implode("','", $values) . "')";
mysql_query($query);
}
// and now we'll do findraising_goals
$fields = "`goal`,`name`,`description`,`system`,`budget`,`deadline`";
if(mysql_error() == null){
$q = mysql_query("SELECT $fields FROM fundraising_goals WHERE fiscalyear = $oldYear");
}
while(mysql_error() == null && $r = mysql_fetch_assoc($q)){
$dateval = $r['deadline'];
$parts = explode('-', $dateval);
if($parts[0] != '0000')
$parts[0] += $yearDiff;
$r['deadline'] = implode('-', $parts);
$r['fiscalyear'] = $newYear;
$fields = array_keys($r);
$values = array_values($r);
foreach($values as $idx => $val){
$values[$idx] = mysql_real_escape_string($val);
}
$query = "INSERT INTO fundraising_goals (`" . implode("`,`", $fields) . "`) VALUES('" . implode("','", $values) . "')";
mysql_query($query);
}
// finally, let's update the fiscal year itself:
if(mysql_error() == null){
mysql_query("UPDATE config SET val='$newYear' WHERE var='FISCALYEAR'");
}
if(mysql_error() == null){
$config['FISCALYEAR'] = $newYear;
echo happy(i18n("Fiscal year has been rolled over from %1 to %2", array($oldYear, $newYear)));
}else{
echo error(mysql_error());
}
}