<? include('includes/main.php'); ?>
<? include('includes/glfunctions.php'); ?>
<script language="JavaScript">
function fillform() { //prefills budget form
<? for ($i=$beginmonth; $i<=$beginmonth+12; $i++) echo ' if (eval(document.mainform.month'.intval($i).'.value)==0) { document.mainform.month'.intval($i).'.value = document.mainform.month'.intval($beginmonth).'.value }'."\n"; ?>
}
</script>
<?
echo texttitle('Budget Add/Update');
if (!$beginyear) {
$timestamp = time();
$date_time_array = getdate($timestamp);
$hours = $date_time_array["hours"];
$minutes = $date_time_array["minutes"];
$seconds = $date_time_array["seconds"];
$month = $date_time_array["mon"];
$day = $date_time_array["mday"];
$year = $date_time_array["year"];
$timestamp = mktime($hour, $minute, $second, $month, 1, $year);
$bgdateyear=date("Y", $timestamp);
$bgdatemonth=date("m", $timestamp);
$timestamp = mktime($hour, $minute, $second, $month, 1, $year+1);
$datenextyear=date("Y", $timestamp);
echo '<form action="glbudadd.php" method="post" name="mainform"><table>';
echo '<tr><td align="right">Update Begin Period (Year):</td><td><input type="text" name="beginyear" onchange="validateint(this)" value='.$bgdateyear.' size="14" maxlength="4"></td></tr><tr><td align="right">Month</td><td><input type="text" name="beginmonth" value='.$bgdatemonth.' size="14" maxlength="4"></td><input type="hidden" name="datenextyear" value='.$datenextyear.'></tr>';
echo '<tr><td>General Ledger Account to Budget:</td><td><select name="glaccountid">';
$recordSet = &$conn->Execute('select id, name,description from glaccount where accounttypeid>49 and (companyid=0 or companyid='.$active_company.') order by name');
while (!$recordSet->EOF) {
echo '<option value="'.$recordSet->fields[0].'">'.$recordSet->fields[1].' - '.$recordSet->fields[2]."\n";
$recordSet->MoveNext();
};
echo '</select></td></tr></table><input type="submit" value="Continue"></form>';
} elseif (!$binsert) {
//now display account name & number at top
$recordSet=&$conn->Execute('select id, name,description from glaccount where (companyid=0 or companyid='.sqlprep($active_company).') and id='.$glaccountid);
if (!$recordSet->EOF) {
//read account name and description for display
$glaccountname=$recordSet->fields[1];
$glaccountdesc=$recordSet->fields[2];
};
echo '<form action="glbudadd.php" method="post" name="mainform"><table border=1><input type="hidden" name="binsert" value="1"><input type="hidden" name="datenextyear" value="'.$datenextyear.'">';
echo '<input type="hidden" name="beginyear" value="'.$beginyear.'">';
echo '<tr><th colspan="2">General Ledger Account: '.$glaccountname." - ".$glaccountdesc.'</td><tr>';
echo '<input type="hidden" name="glaccountid" value="'.$glaccountid.'">';
//read budget year one (this year)
$recordSet = &$conn->Execute('select id,glaccountid,companyid,budgetyear,jan,feb,mar,apr,may,jun,jul,aug,sep,oct,nov,decm,lastchangedate from glbudgets where budgetyear='.$beginyear.' and glaccountid='.$glaccountid.' and companyid='.$active_company);
if (!$recordSet->EOF) {
//read data now for existing budget info
for ($curmonth=1;$curmonth<=12;$curmonth++) {
//read monthly data
${"month".$curmonth}=$recordSet->fields[$curmonth+3];
${"year".$curmonth}=$beginyear;
};
} else {
//did not find record, so create a new one
checkpermissions('gl');
if ($conn->Execute('insert into glbudgets (glaccountid,companyid,budgetyear) VALUES ('.$glaccountid.",".$active_company.",".$beginyear.')') === false) die(texterror("Error inserting budget info for '.$beginyear"));
$recordSet = &$conn->Execute('select id,glaccountid,companyid,budgetyear,jan,feb,mar,apr,may,jun,jul,aug,sep,oct,nov,decm,lastchangedate from glbudgets where budgetyear='.$beginyear.' and glaccountid='.$glaccountid.' and companyid='.$active_company);
if (!$recordSet->EOF) {
//read data now for existing budget info
for ($curmonth=1;$curmonth<=12;$curmonth++) {
//read monthly data
${"month".$curmonth}=$recordSet->fields[$curmonth+3];
${"year".$curmonth}=$beginyear;
};
};
};
//read budget from year two (next year)
$recordSet = &$conn->Execute('select id,glaccountid,companyid,budgetyear,jan,feb,mar,apr,may,jun,jul,aug,sep,oct,nov,decm,lastchangedate from glbudgets where budgetyear='.$datenextyear.' and glaccountid='.$glaccountid.' and companyid='.$active_company);
if (!$recordSet->EOF) {
//read data now for existing budget info from next year
for ($curmonth=13;$curmonth<=24;$curmonth++) {
//read monthly data
${"month".$curmonth}=$recordSet->fields[$curmonth+3-12];
${"year".$curmonth}=$datenextyear;
};
$lastchangedate=$recordSet->fields[16];
} else {
//did not find record, so create a new one
checkpermissions('gl');
if ($conn->Execute('insert into glbudgets (glaccountid,companyid,budgetyear) VALUES ('.$glaccountid.",".$active_company.",".$datenextyear.')') === false) die(texterror("Error inserting budget info for ".$datenextyear));
$recordSet = &$conn->Execute('select id,glaccountid,companyid,budgetyear,jan,feb,mar,apr,may,jun,jul,aug,sep,oct,nov,decm,lastchangedate from glbudgets where budgetyear='.$beginyear.' and glaccountid='.$glaccountid.' and companyid='.$active_company);
if (!$recordSet->EOF) {
//read data now for existing budget info
for ($curmonth=13;$curmonth<25;$curmonth++) { //read monthly data
${"month".$curmonth}=$recordSet->fields[$curmonth+3-12];
${"year".$curmonth}=$beginyear;
};
};
$lastchangedate=$recordSet->fields[16];
};
echo '<tr><th>MONTH - YEAR</th><th>BUDGET AMOUNT</th></tr>';
for ($curmonth=1;$curmonth<=24;$curmonth++) {
if ($curmonth>=$beginmonth&&$curmonth<$beginmonth+12) {
if ($curmonth==$beginmonth) {
echo '<tr><td align="'.TABLE_LEFT_SIDE_ALIGN.'">'.month2monthlong(num2month($curmonth)).' - '.${"year".$curmonth}.'</td><td><input onblur="fillform()" type="text" name="month'.$curmonth.'" onchange="validatenum(this)" value="'.${"month".$curmonth}.'"></td></tr>';
} else {
echo '<tr><td align="'.TABLE_LEFT_SIDE_ALIGN.'">'.month2monthlong(num2month($curmonth)).' - '.${"year".$curmonth}.'</td><td><input type="text" name="month'.$curmonth.'" onchange="validatenum(this)" value='.${"month".$curmonth}.'></td></tr>';
};
} else {
echo '<input type="hidden" name="month'.$curmonth.'" value="'.${"month".$curmonth}.'">';
echo '<input type="hidden" name="lastchangedate" value="'.$lastchangedate.'">';
};
};
echo '</table><input type="submit" value="Update"></form>';
} else { //update files now for both years
$recordSet=&$conn->Execute('select count(*) from glbudgets where budgetyear='.sqlprep($datenextyear).' and glaccountid='.sqlprep($glaccountid).' and companyid='.sqlprep($active_company).' and lastchangedate='.sqlprep($lastchangedate));
if (!$recordSet->EOF) {
if ($recordSet->fields[0]==0) {
$recordSet=&$conn->Execute('select lastchangedate, lastchangeuserid from glbudgets where budgetyear='.sqlprep($datenextyear).' and glaccountid='.sqlprep($glaccountid).' and companyid='.sqlprep($active_company));
if (!$recordSet->EOF) {
$lastchangeuserid=$recordSet->fields[1];
$lastchangedate=$recordSet->fields[0];
echo texterror("COULD NOT UPDATE because ".getname($lastchangeuserid)." made changes ".substr($lastchangedate,1,2)."/".substr($lastchangedate,3,2)."/".substr($lastchangedate,5,4)." to this same GL Budget Account while you were working on your changes");
};
} else {
checkpermissions('gl');
$query='update glbudgets set ';
for ($curmonth=1;$curmonth<=12;$curmonth++) {
if (${"month".$curmonth}==0) ${"month".$curmonth}=0;
$query.=" ".num2month($curmonth, 1)."=".sqlprep(${"month".$curmonth});
if ($curmonth<12) $query.=",";
};
$query.=", lastchangeuserid=".sqlprep($userid).' where budgetyear='.sqlprep($beginyear).' and glaccountid='.sqlprep($glaccountid).' and companyid='.sqlprep($active_company);
if ($conn->Execute($query) === false) {
echo texterror("Error updating budget for year ".$beginyear);
} else {
echo textsuccess("Budget for year ".$beginyear." updated successfully.");
$query='update glbudgets set';
for ($curmonth=13;$curmonth<=24;$curmonth++) {
if (${"month".$curmonth}==0) ${"month".$curmonth}=0;
$query.=" ".num2month($curmonth, 1).'='.sqlprep(${"month".$curmonth});
if ($curmonth<24) $query.=",";
};
$query.=", lastchangeuserid=".sqlprep($userid).' where budgetyear='.sqlprep($datenextyear).' and glaccountid='.sqlprep($glaccountid).' and companyid='.sqlprep($active_company);
if ($conn->Execute($query) === false) {
echo texterror("Error updating budget for year ".$datenextyear);
} else {
echo textsuccess("Budget for year ".$datenextyear." updated successfully.");
};
};
};
};
};
?>
<? include('includes/footer.php'); ?>