Files
HotLunches/check-user-balance.php
2026-10-19 15:22:52 -07:00

73 lines
2.5 KiB
PHP

<?php
require_once("include/globals.php");
// if ($school_num == 19677) startLog("Check User Balance");
writeLog($_SESSION['UserInfo']['type']);
//
// If required check for a saved user balance
//
if (($_SESSION['UserInfo']['type'] == 'Parent' || $_SESSION['UserInfo']['type'] == 'Staff') && $school_record['previous_balances_post'] == 'Post Automatically') {
// Check if there is a matching email
$userid = $_SESSION['UserInfo']['userid'];
$email = $_SESSION['UserInfo']['email'];
$user_num = $_SESSION['UserInfo']['record_num'];
$where = "original_userid = '".db_escape_string($userid)."' ";
if (strlen($email)) {
$email = db_escape_string($email);
$where = "email = '$email' OR $where";
}
$year = date("Y",time());
$cutoff = date("Y-m-d",time() - 60 * 60 * 24 * 450);
$query = "SELECT * FROM user_balance_save " .
"WHERE school_num = $school_num " .
"AND posted = 'N' " .
"AND saved_date > '$cutoff' " .
"AND ($where)";
// logQuery($school_num,"user_balance_save",$query,"check-user-balance");
writeLog($query);
$results = db_query($query);
// If there is a saved balance
if (db_num_rows($results) > 0) {
$ua = new Table("user_account");
while ($row = db_fetch_assoc($results)) {
$balance = $row['balance'];
$transaction_type = 'Charge Adjustment';
if ($balance < 0) $transaction_type = 'Credit Adjustment';
$note = $school_record['previous_balances_post_message'];
$transaction_amount = $balance;
$entered_time = time();
$transaction_datetime = date("Y-m-d H:i:s",$entered_time);
$deleted = 'N';
// Create a new transaction
$query = $ua->BuildInsert();
writeLog($query);
db_query($query);
$trx_num = db_insert_id();
// Update the saved balance record
$rnum = $row['record_num'];
$query = "UPDATE user_balance_save SET posted = 'Y', posted_time = $entered_time, user_num = $user_num, trx_num = $trx_num " .
"WHERE record_num = $rnum";
logQuery($school_num,"user_balance_save",$query,"check-user-balance");
writeLog($query);
db_query($query);
if ($school_record['balance_post_limit'] > 0.00 && $balance > $school_record['balance_post_limit']) {
$query = "UPDATE users SET can_order = 'Balance' WHERE school_num = $school_num AND record_num = $user_num";
db_query($query);
}
}
}
}
?>