Files
HotLunches/invoices.form.php
T
2026-10-19 15:22:52 -07:00

347 lines
12 KiB
PHP

<?php
require_once("include/globals.php");
if (SessionActive() != 1 || ! UserLoggedIn()) {
header("Location: /");
exit();
}
// Ensure that only valid user types have access to this module
$AllowedUserTypes = array('SuperUser');
if (checkPermission($AllowedUserTypes) != 1) {
echo("<br>\n");
$error_message = getLanguageText('ERRORMSG-ACCESS-DENIED');
include "form.messages.php";
return;
}
// Setup default values on insert
if ($naction == 'add') {
$initTableName = "invoice_header";
$tarray = initializeTableArray($initTableName);
extract($tarray, EXTR_OVERWRITE);
$invoice_date = date("Y-m-d",time());
$due_date = date("Y-m-d",time() + 60 * 60 * 24 * 30);
$monthdate = date("m-d",time());
$yr = date("Y",time());
$renewal_date = "$yr-08-01";
if ($monthdate > "02-15") {
$yr++;
$renewal_date = "$yr-08-01";
}
$charge_gst = 'Y';
$status = 'Open';
$amount_paid = 0.00;
}
if (!isset($selected_school) && isset($school_num)) $selected_school = $school_num;
if (isset($selected_school)) {
$results = db_query("SELECT * FROM schools WHERE record_num = $selected_school");
$school = db_fetch_array($results);
}
$gst_rate = 5;
$tax_code = "GST";
// if ($school['province'] == 'BC') {
// $gst_rate = 12;
// $tax_code = "HST";
// }
if ($school['country'] != 'Canada') {
$charge_gst = 'N';
$gst_rate = 0;
$tax_code = "";
}
else {
if ($school['charge_gst'] == 'N') {
$charge_gst = 'N';
$gst_rate = 0;
$tax_code = "";
}
}
$balance = $total - $amount_paid;
$numLines = 12;
// if ($selected_school == 258) $numLines = 25;
if (isset($record_num) && $record_num > 0) {
$query = "select count(*) from invoice_items " .
"where invoice_num = $record_num";
$num = db_getVal($query);
if ($num > $numLines - 5) $numLines = $num + 5;
}
// echo($numLines." - ".$num);
?>
<br>
<form method='post' action='/' name='invoices'>
<?php
$message_width = $default_form_width;
include "form.messages.php";
?>
<table width='<?php echo($default_form_width) ?>' class='content-table'>
<tr>
<td colspan='2' align='center' class='heading'><?php echo($PageTitle) ?></td>
</tr>
<tr>
<td colspan='2' align='center'>&nbsp;</td>
</tr>
<tr>
<td width='30%' align='right' valign='top'>School: </td>
<td width='70%' align='left' valign='top'>
<select name='school_num'>
<?php
$schools = db_query("select * from schools where status != 'Linked' order by school_name");
while ($row = db_fetch_array($schools)) {
$school_name = $row['school_name'];
$s_num = $row['record_num'];
$configuration = $row['system_configuration'];
$sstatus = $row['status'];
$selected = "";
if (isset($selected_school) && $selected_school == $s_num) $selected = " selected='selected'";
if (!isset($selected_school) && isset($record_num) && $school_num == $s_num) $selected = " selected='selected'";
echo(" <option value='$s_num'$selected>$school_name ($sstatus - $configuration: $s_num) </option>\n");
}
?>
</select>
</td>
</tr>
<tr>
<td width='30%' align='right' valign='top'>School Year: </td>
<td width='70%' align='left' valign='top'>
<select name='school_year'>
<?php
$curr = date("Y",time());
$monthdate = date("m-d",time());
if ($monthdate < "02-15") $curr--;
$year = 2008;
for ($i = 0; $i < 20; $i++) {
$y2 = $year + 1;
$selected = "";
if ($naction == 'add' && $year == $curr) $selected = " selected='selected'";
if ($naction == 'edit' && "$year/$y2" == $school_year) $selected = " selected='selected'";
echo(" <option value='$year/$y2'$selected>$year/$y2</option>\n");
$year++;
}
?>
</select>
</td>
</tr>
<tr>
<td align='right' valign='top'>Invoice Date: </td>
<td align='left' valign='top'><input type='text' name='invoice_date' value='<?php echo($invoice_date) ?>' size='15' class='date-field'></td>
</tr>
<tr>
<td align='right' valign='top'>Due Date: </td>
<td align='left' valign='top'><input type='text' name='due_date' value='<?php echo($due_date) ?>' size='15' class='date-field'></td>
</tr>
<tr>
<td align='right' valign='top'>Renewal Date: </td>
<td align='left' valign='top'><input type='text' name='renewal_date' value='<?php echo($renewal_date) ?>' size='15' class='date-field'></td>
</tr>
<tr>
<td align='right' valign='top'>Charge <?php echo($tax_code) ?>: </td>
<td align='left' valign='top'>
<?php BuildFileRadio("yn.txt","charge_gst",$charge_gst) ?> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<input type='text' name='gst_rate' id='gst_rate' value='<?php echo($gst_rate) ?>' size='1' style='text-align: right;'>%</td>
</tr>
<tr>
<td align='right' valign='top'>Invoice Status: </td>
<td align='left' valign='top'>
<select name='status'>
<?php BuildFileList("invoice_status.txt",$status) ?>
</select>
</td>
</tr>
<tr>
<td align='right' valign='top'>Include School List for Reseller: </td>
<td align='left' valign='top'>
<?php BuildFileRadio("yn.txt","include_school_names",$include_school_names) ?>
</tr>
<tr>
<td colspan='2' align='center'>
<br>
<table cellpadding='3' cellspacing='1' class='content-table'>
<tr>
<td align='left' class='heading'>Description</td>
<td align='right' class='heading'>Amount</td>
</tr>
<?php
$bgclass = 'data-even';
$subtotal = 0;
$gst = 0;
$total = 0;
for ($i = 1; $i <= $numLines; $i++) {
$description = "";
$amount = "0";
// Handle invoice creation
if ($naction == 'add') {
if ($i == 1) {
$description = "Hot Lunches On-Line Annual Hosting Fee";
$amount = 350;
if ($school['system_configuration'] != 'Full') $amount = 300;
}
if ($i == 2) {
if (date("m",time()) >= '04' && date("m",time()) <= '07') {
$description = "- Special Discount Offer (expires June 30, " . date("Y",time()) . ")";
$amount = -50;
}
}
}
else {
if (isset($record_num) && $record_num > 0) {
$query = "select * from invoice_items " .
"where invoice_num = $record_num " .
"and sequence = $i";
$results = db_query($query);
if (db_num_rows($results)) {
$row = db_fetch_array($results);
$description = $row['description'];
$amount = str_replace(",","",$row['amount']);
}
}
}
$subtotal += $amount;
$amount = number_format($amount,2);
?>
<tr class='<?php echo($bgclass) ?>'>
<td align='left'><input type='text' name='description<?php echo($i) ?>' size='80' value="<?php echo($description) ?>"></td>
<td align='right'><input type='text' name='amount<?php echo($i) ?>' id='amount<?php echo($i) ?>' size='15' value='<?php echo($amount) ?>' style='text-align: right; float: right;' onBlur='updateTotals();'></td>
</tr>
<?php
}
if ($charge_gst == 'Y') {
$gst = $subtotal * $gst_rate / 100;
}
$total = $subtotal + $gst;
$balance = $total - $amount_paid;
$subtotal = number_format($subtotal,2);
$gst = number_format($gst,2);
$total = number_format($total,2);
?>
<tr class='<?php echo($bgclass) ?>'>
<td align='right'>SubTotal :: </td>
<td align='right'><input type='text' name='subtotal' id='subtotal' size='15' value='<?php echo($subtotal) ?>' style='text-align: right; float: right;'></td>
</tr>
<tr class='<?php echo($bgclass) ?>'>
<td align='right'><?php echo($tax_code) ?> :: </td>
<td align='right'><input type='text' name='gst' id='gst' size='15' value='<?php echo($gst) ?>' style='text-align: right; float: right;'></td>
</tr>
<tr class='<?php echo($bgclass) ?>'>
<td align='right'>Total :: </td>
<td align='right'><input type='text' name='total' id='total' size='15' value='<?php echo($total) ?>' style='text-align: right; float: right;'></td>
</tr>
<tr class='<?php echo($bgclass) ?>'>
<td align='right'>Payments :: </td>
<td align='right'><input type='text' name='amount_paid' id='amount_paid' size='15' value='<?php echo($amount_paid) ?>' style='text-align: right; float: right;' onBlur='updateTotals();'></td>
</tr>
<tr class='<?php echo($bgclass) ?>'>
<td align='right'>Payment Date :: </td>
<td align='right'><input type='text' name='payment_date' id='payment_date' size='15' value='<?php echo($payment_date) ?>' class='date-field'></td>
</tr>
<tr class='<?php echo($bgclass) ?>'>
<td align='right'>Payment Method :: </td>
<td align='right'><input type='text' name='payment_method' id='payment_method' size='15' value='<?php echo($payment_method) ?>'></td>
</tr>
<tr class='<?php echo($bgclass) ?>'>
<td align='right'>Balance Outstanding :: </td>
<td align='right'><input type='text' name='balance' id='balance' size='15' value='<?php echo(number_format($balance,2)) ?>' style='text-align: right; float: right;'></td>
</tr>
</table>
</td>
</tr>
<tr>
<td colspan='2' align='center'>
<br>
<input type='submit' class='btn btn--primary' value='<?php echo($ButtonText) ?>'>
<input type='hidden' name='action' value='<?php echo($action) ?>'>
<input type='hidden' name='naction' value='<?php echo($next_naction) ?>'>
<input type='hidden' name='record_num' value='<?php echo($record_num) ?>'>
<input type='hidden' name='tax_code' value='<?php echo($tax_code) ?>'>
<?php if (isset($selected_school)) { ?>
<input type='hidden' name='selected_school' value='<?php echo($selected_school) ?>'>
<?php } ?>
<?php if (isset($show)) { ?>
<input type='hidden' name='show' value='<?php echo($show) ?>'>
<?php } ?>
<br>
<br>
</td>
</tr>
</table>
</form>
<script language="JavaScript">
// Function to update the totals
function updateTotals() {
var subtotal = 0;
for (var i = 1; i <= 5; i++) {
amt = document.getElementById('amount'+i).value;
amt = parseFloat(amt.replace(",",""));
subtotal += eval(amt);
document.getElementById('amount'+i).value = formatDecimal(amt);
}
var total = subtotal;
var gst = 0;
var gst_rate = document.forms[0].gst_rate.value;
document.forms[0].gst.value = formatDecimal(0);
for (var i = 0; i < document.forms[0].charge_gst.length; i++) {
if (document.forms[0].charge_gst[i].checked) gst_flag = document.forms[0].charge_gst[i].value;
}
if (gst_flag == 'Y') {
gst = Math.floor(eval(gst_rate) * subtotal) / 100;
document.forms[0].gst.value = formatDecimal(gst);
}
total = subtotal + gst;
var paid = document.forms[0].amount_paid.value;
var balance = total - paid;
document.forms[0].subtotal.value = formatDecimal(subtotal);
document.forms[0].total.value = formatDecimal(total);
document.forms[0].amount_paid.value = formatDecimal(paid);
document.forms[0].balance.value = formatDecimal(balance);
}
// Define function to format a value as decimal
function formatDecimal(num) {
num = num.toString().replace(/\$|\,/g,'');
if(isNaN(num))
num = "0";
sign = (num == (num = Math.abs(num)));
num = Math.floor(num*100+0.50000000001);
cents = num%100;
num = Math.floor(num/100).toString();
if(cents<10)
cents = "0" + cents;
for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
num = num.substring(0,num.length-(4*i+3))+','+
num.substring(num.length-(4*i+3));
return (((sign)?'':'-') + num + '.' + cents);
}
</script>