1083 lines
44 KiB
PHP
1083 lines
44 KiB
PHP
<?php
|
|
require_once("include/globals.php");
|
|
|
|
if (SessionActive() != 1 || ! UserLoggedIn()) {
|
|
header("Location: /");
|
|
exit();
|
|
}
|
|
|
|
|
|
//
|
|
// Process all actions to list and edit records
|
|
//
|
|
$action = "user-account";
|
|
$CurrentTable = "user_account";
|
|
$FormFile = "user_account.form.php";
|
|
$TitleText = getLanguageText('TEXT-ACCOUNT-TRANSACTIONS');
|
|
$OrderBy = "userid,transaction_datetime,record_num";
|
|
|
|
// Start the template and output buffering
|
|
startTemplate();
|
|
$templatePageTitle = "$TitleText";
|
|
|
|
|
|
if ($_SESSION['UserInfo']['type'] == 'Supplier') {
|
|
$error_message = getLanguageText('ERRORMSG-ACCESS-DENIED');
|
|
include "form.messages.php";
|
|
endTemplate();
|
|
exit();
|
|
}
|
|
|
|
?>
|
|
<style>
|
|
@media screen { #printSection { display: none; } }
|
|
@media print {
|
|
body * { display: none; }
|
|
#receipt-content { display: block; }
|
|
}
|
|
</style>
|
|
<?php
|
|
|
|
|
|
$moduleName = "user_account.php";
|
|
// if ($_SESSION['UserInfo']['type'] == 'SuperUser') startLog($moduleName);
|
|
writeLog("_POST: ".print_r($_POST, true));
|
|
writeLog("_GET: ".print_r($_GET, true));
|
|
|
|
|
|
// Get parameters
|
|
if (array_key_exists('naction',$_GET)) $naction = $_GET['naction'];
|
|
if (array_key_exists('naction',$_POST)) $naction = $_POST['naction'];
|
|
if (isset($new_naction)) $naction = $new_naction;
|
|
$onaction = $naction;
|
|
|
|
unset($user_num);
|
|
if (array_key_exists('user_num', $_GET) && intval($_GET['user_num']) > 0) $user_num = intval($_GET['user_num']);
|
|
if (array_key_exists('user_num', $_POST) && intval($_POST['user_num']) > 0) $user_num = intval($_POST['user_num']);
|
|
|
|
unset($student_num);
|
|
if (array_key_exists('student_num', $_GET) && intval($_GET['student_num']) > 0) $student_num = intval($_GET['student_num']);
|
|
if (array_key_exists('student_num', $_POST) && intval($_POST['student_num']) > 0) $student_num = intval($_POST['student_num']);
|
|
|
|
if (isset($student_num) && $student_num > 0 && !isset($user_num)) {
|
|
$user_num = db_getVal("SELECT user_num FROM people WHERE school_num = $base_school_num AND record_num = $student_num");
|
|
}
|
|
|
|
if (!isset($user_num) && in_array($_SESSION['UserInfo']['type'],array('Application Administrator','Administration Team','Schedule Team','Financial','Financial Team'))) $user_num = intval($_SESSION['UserInfo']['record_num']);
|
|
|
|
unset($selected_user_num);
|
|
if (isset($user_num)) $selected_user_num = $user_num;
|
|
|
|
$mode = "";
|
|
if (array_key_exists('mode',$_GET)) $mode = $_GET['mode'];
|
|
if (array_key_exists('mode',$_POST)) $mode = $_POST['mode'];
|
|
$_SESSION['mode'] = $mode;
|
|
|
|
$class_num = 0;
|
|
if (array_key_exists('class_num',$_GET)) $class_num = intval($_GET['class_num']);
|
|
if (array_key_exists('class_num',$_POST)) $class_num = intval($_POST['class_num']);
|
|
|
|
$view = "";
|
|
if (array_key_exists('view',$_GET)) $view = $_GET['view'];
|
|
|
|
|
|
if (array_key_exists('order_num',$_GET)) {
|
|
$order_num = intval($_GET['order_num']);
|
|
$results = db_query("SELECT * FROM hotlunch_order_header WHERE school_num = $school_num AND record_num = $order_num");
|
|
$row = db_fetch_array($results);
|
|
$pnum = $row['ordered_by_num'];
|
|
$ptype = $row['ordered_by_type'];
|
|
|
|
if ($ptype == '' || intval($pnum) == 0) {
|
|
$user_num = 0;
|
|
$selected_user_num = 0;
|
|
header("Location: /?error_message=".urlencode(getLanguageText('ERRORMSG-UNABLE-TO-COMPLETE-ACTION')));
|
|
exit();
|
|
}
|
|
else {
|
|
$user_num = db_getVal("SELECT user_num FROM people WHERE school_num = $base_school_num AND people_type = '$ptype' AND record_num = $pnum");
|
|
$selected_user_num = $user_num;
|
|
}
|
|
}
|
|
|
|
|
|
$override = false;
|
|
if ($_SESSION['UserInfo']['type'] == 'Staff' && array_key_exists('naction', $_GET) && $_GET['naction'] == 'post' ) {
|
|
$naction = 'add';
|
|
$override = true;
|
|
}
|
|
|
|
if ($_SESSION['UserInfo']['type'] == 'Staff' && array_key_exists('naction', $_POST) && $_POST['naction'] == 'insert' && $_POST['prev_naction'] == 'post') {
|
|
$naction = 'insert';
|
|
$override = true;
|
|
}
|
|
|
|
if (!$override) {
|
|
if (in_array($_SESSION['UserInfo']['type'],array('Parent','Staff','Guest'))) {
|
|
$user_num = intval($_SESSION['UserInfo']['record_num']);
|
|
$naction = 'list';
|
|
}
|
|
}
|
|
|
|
|
|
if (array_key_exists('mode', $_GET) && $_GET['mode'] == 'parent') {
|
|
$user_num = intval($_SESSION['UserInfo']['record_num']);
|
|
$naction = 'list';
|
|
}
|
|
|
|
|
|
if (!isset($user_num) || intval($user_num) == 0) {
|
|
$error_message = getLanguageText('ERRORMSG-USER-NOT-DEFINED');
|
|
include "form.messages.php";
|
|
endTemplate();
|
|
exit();
|
|
}
|
|
|
|
|
|
// Validate the user_num
|
|
$results = db_query("select * from users where school_num = $base_school_num and record_num = $user_num");
|
|
if (db_num_rows($results) == 0) {
|
|
$error_message = getLanguageText('ERRORMSG-USER-INVALID');
|
|
include "form.messages.php";
|
|
endTemplate();
|
|
exit();
|
|
}
|
|
|
|
$user_record = db_fetch_array($results);
|
|
$userid = $user_record['userid'];
|
|
$user_name = $user_record['first_name'] . ' ' . $user_record['last_name'];
|
|
|
|
|
|
//
|
|
// Verify all user orders
|
|
//
|
|
$orders = db_query("SELECT * FROM user_account WHERE school_num = $school_num AND user_num = $user_num AND transaction_type = 'Order' ORDER BY order_num");
|
|
if (db_num_rows($orders) > 0) {
|
|
$prevOrder = 0;
|
|
while ($row = db_fetch_assoc($orders)) {
|
|
$order_num = $row['order_num'];
|
|
if ($order_num == $prevOrder) {
|
|
$rnum = $row['record_num'];
|
|
db_query("DELETE FROM user_account WHERE school_num = $school_num AND user_num = $user_num AND transaction_type = 'Order' AND order_num = $order_num AND record_num = $rnum");
|
|
}
|
|
else {
|
|
$r = db_query("SELECT * FROM hotlunch_order_header WHERE school_num = $school_num AND record_num = $order_num");
|
|
if (db_num_rows($r) == 0) {
|
|
db_query("DELETE FROM user_account WHERE school_num = $school_num AND user_num = $user_num AND transaction_type = 'Order' AND order_num = $order_num");
|
|
db_query("DELETE FROM hotlunch_order_items WHERE school_num = $school_num AND order_num = $order_num");
|
|
}
|
|
}
|
|
$prevOrder = $order_num;
|
|
}
|
|
}
|
|
|
|
|
|
$school_num = $school_record['record_num'];
|
|
|
|
|
|
// Include the menu for Admin users
|
|
// if (in_array($_SESSION['UserInfo']['type'],array('SuperUser','System Administrator','Application Administrator','Administration Team','Schedule Team','Financial','Financial Team'))) {
|
|
// include "users.options.php";
|
|
// }
|
|
|
|
|
|
|
|
include "form.messages.php";
|
|
|
|
switch (strtolower($naction)) {
|
|
case "add":
|
|
case "add-payment":
|
|
$PageTitle = getLanguageText('TEXT-ADD-NEW');
|
|
$ButtonText = $PageTitle;
|
|
$next_naction = "insert";
|
|
|
|
include $FormFile;
|
|
$selected_user_num = $user_num;
|
|
|
|
// Check account balance
|
|
$balance = db_getVal("select sum(transaction_amount) as balance from user_account where school_num = $school_num and user_num = $user_num AND (deleted != 'Y' OR deleted IS NULL)");
|
|
if ($balance < 0.01) updateOrders($user_num,'Paid');
|
|
|
|
include "user_account.list.php";
|
|
break;
|
|
|
|
case "insert":
|
|
extract($_POST);
|
|
writeLog("_POST: ".print_r($_POST, true));
|
|
unset($error_message);
|
|
|
|
// Validate the form data
|
|
if (strlen($user_num) == 0) {
|
|
$error_message = getLanguageText('MSG-USER-NUM-REQUIRED');
|
|
}
|
|
|
|
if (strlen($transaction_type) == 0) {
|
|
$error_message = getLanguageText('MSG-TRX-TYPE-REQUIRED');
|
|
}
|
|
|
|
$transaction_amount = str_replace(",","",str_replace('$','',$transaction_amount));
|
|
$transaction_amount = floatval($transaction_amount);
|
|
if (!isset($error_message) && $transaction_type != 'TRX Note' && $transaction_amount == 0) {
|
|
$error_message = getLanguageText('MSG-TRX-AMOUNT-ZERO');
|
|
}
|
|
|
|
if (!isset($error_message) && $transaction_type == 'Payment' && strlen($payment_method) == 0) {
|
|
$error_message = getLanguageText('MSG-PAYMENT-METHOD-REQUIRED');
|
|
}
|
|
|
|
// If there is an error then display the form again
|
|
if (isset($error_message) && strlen($error_message) > 0) {
|
|
$new_naction = 'add';
|
|
return;
|
|
}
|
|
|
|
$transaction_status = 'Open';
|
|
|
|
// Lookup TRX Type code
|
|
$trx_type = $transaction_type;
|
|
$query = "SELECT * FROM user_account_trx_codes WHERE school_num = $school_num AND transaction_type_code = '$transaction_type'";
|
|
writeLog($query);
|
|
$r = db_query($query);
|
|
writeLog("Num Records: ".db_num_rows($r));
|
|
if (db_num_rows($r)) {
|
|
$row = db_fetch_assoc($r);
|
|
$trx_type = $row['trx_category'];
|
|
}
|
|
|
|
|
|
// Update user balance
|
|
$transaction_amount = floatval($transaction_amount);
|
|
$transaction_amount = abs($transaction_amount);
|
|
writeLog("transaction_amount: $transaction_amount");
|
|
switch ($trx_type) {
|
|
case 'Credit':
|
|
case 'Credit Adjustment':
|
|
case 'Multiple Order Discount':
|
|
case 'PAC Gift':
|
|
case 'Council Gift':
|
|
case 'Transaction Credit':
|
|
$payment_method = $transaction_type;
|
|
$transaction_amount = -1 * abs($transaction_amount);
|
|
$transaction_status = 'Closed';
|
|
break;
|
|
|
|
case 'Payment':
|
|
$transaction_amount = -1 * abs($transaction_amount);
|
|
$transaction_status = 'Closed';
|
|
break;
|
|
|
|
case 'TRX Note':
|
|
$transaction_amount = 0;
|
|
$transaction_status = 'Closed';
|
|
break;
|
|
}
|
|
|
|
// Check data
|
|
if ($transaction_type != 'Payment') {
|
|
$payment_method = '';
|
|
}
|
|
|
|
// Insert transaction entry
|
|
$order_num = 0;
|
|
unset($people_num);
|
|
$entered_by_user_num = intval($_SESSION['UserInfo']['record_num']);
|
|
$deleted = 'N';
|
|
$deleted_datetime = NULL;
|
|
$delete_reason = NULL;
|
|
$entered_time = time();
|
|
$transaction_datetime = date("Y-m-d H:i:s",time());
|
|
if ($posting_date != $curr_date) {
|
|
$transaction_datetime = "$posting_date 23:59:59";
|
|
}
|
|
|
|
$schedule_num = intval($schedule_num);
|
|
|
|
// Insert the new transaction
|
|
$user_account = new Table("user_account");
|
|
$query = $user_account->BuildInsert();
|
|
writeLog($query);
|
|
db_query($query);
|
|
$trx_num = db_insert_id();
|
|
|
|
if ($trx_type == 'Payment') {
|
|
adjustUserAccountDetails($user_num,$school_num);
|
|
include "send-payment-notification.php";
|
|
}
|
|
|
|
// Get set of people associated with this user account
|
|
$results = db_query("select record_num from people where school_num = $base_school_num and user_num = $user_num");
|
|
|
|
// If there are people linked to this account then update any orders associated with the people
|
|
$people = "";
|
|
if (db_num_rows($results)) {
|
|
while ($row = db_fetch_array($results)) {
|
|
$people .= $row['record_num'] . ",";
|
|
}
|
|
$people = preg_replace("/,$/","",$people);
|
|
}
|
|
|
|
|
|
// Update the order headers
|
|
$order_payments = "";
|
|
$transaction_payments = "";
|
|
|
|
// Get posted orders and transactions
|
|
$selected_orders = array();
|
|
$selected_transactions = array();
|
|
if (array_key_exists('selected_orders',$_POST) && is_array($_POST['selected_orders']) && count($_POST['selected_orders'])) $selected_orders = $_POST['selected_orders'];
|
|
if (array_key_exists('selected_transactions',$_POST) && is_array($_POST['selected_transactions']) && count($_POST['selected_transactions'])) $selected_transactions = $_POST['selected_transactions'];
|
|
writeLog("transaction_amount: $transaction_amount\n");
|
|
writeLog("selected_orders: " . count($selected_orders) . "\n");
|
|
writeLog("selected_transactions: " . count($selected_transactions) . "\n");
|
|
|
|
// Get account balance
|
|
$acct_balance = db_getVal("select sum(transaction_amount) as balance from user_account where school_num = $school_num and user_num = $user_num AND (deleted != 'Y' OR deleted IS NULL)");
|
|
$bal = sprintf("%0.2f",$acct_balance);
|
|
db_query("UPDATE users SET balance_owing = $bal, account_balance = $bal WHERE school_num = $school_num AND record_num = $user_num");
|
|
writeLog("acct_balance: $acct_balance\n");
|
|
|
|
// Update user record if balance is zero
|
|
if ($acct_balance <= 0.01) {
|
|
db_query("UPDATE users SET can_order = 'Yes' WHERE school_num = $school_num AND record_num = $user_num");
|
|
}
|
|
|
|
// Update user orders
|
|
if (count($selected_orders) == 0 && count($selected_transactions) == 0 && $acct_balance <= 0.01 && $people) {
|
|
|
|
if ($school_record['select_transactions_on_post'] == 'Y') {
|
|
$today = date("Y-m-d",time());
|
|
$transaction_payments = "";
|
|
$total_posted = 0;
|
|
$allocated_total = 0;
|
|
|
|
// Get set of transactions to be updated
|
|
$query = "SELECT * FROM user_account " .
|
|
"WHERE school_num = $school_num " .
|
|
"AND user_num = $user_num " .
|
|
"AND transaction_status = 'Open' " .
|
|
"AND (deleted != 'Y' OR deleted IS NULL) " .
|
|
"ORDER BY transaction_amount ASC";
|
|
$results = db_query($query);
|
|
|
|
$order_numbers = array();
|
|
while ($row = db_fetch_assoc($results)) {
|
|
$trxnum = $row['record_num'];
|
|
$trx_type = $row['transaction_type'];
|
|
$onum = $row['order_num'];
|
|
$snum = $row['schedule_num'];
|
|
$trxtotal = $row['transaction_amount'];
|
|
$trxpaid = $row['amount_paid'];
|
|
$owing = $trxtotal - $trxpaid;
|
|
|
|
$amt = $owing;
|
|
if ($allocated_total + $owing > abs($transaction_amount)) {
|
|
$amt = abs($transaction_amount) - $allocated_total;
|
|
}
|
|
$payment_amt = $amt;
|
|
|
|
$total_posted += $payment_amt;
|
|
|
|
if (strlen($transaction_payments) > 0) $transaction_payments .= "|";
|
|
$transaction_payments .= "{" . $trxnum . ":" . $payment_amt . "}";
|
|
$allocated_total += $payment_amt;
|
|
|
|
// Save transaction details
|
|
$now = time();
|
|
$tamt = abs($payment_amt) * -1;
|
|
$snum = sprintf("%0d",intval($snum));
|
|
$query = "INSERT INTO user_account_detail (school_num,user_num,transaction_num,posted_to_transaction,posted_amount,posted_schedule,entered_time,deleted) " .
|
|
"VALUES ($school_num, $user_num, $trx_num, $trxnum, $tamt, $snum, $now, 'N')";
|
|
db_query($query);
|
|
|
|
if ($trx_type == 'Order' && $onum > 0) {
|
|
if (strlen($order_payments) > 0) $order_payments .= "|";
|
|
$order_payments .= "{" . $onum . ":" . $payment_amt . "}";
|
|
$order_numbers[] = $onum;
|
|
|
|
$query = "UPDATE hotlunch_order_header " .
|
|
"SET amount_paid = amount_paid + $payment_amt, " .
|
|
"paid_date = '$today', " .
|
|
"payment_method = '$payment_method' " .
|
|
"WHERE record_num = $onum";
|
|
db_query($query);
|
|
|
|
$query = "UPDATE hotlunch_order_header " .
|
|
"SET status = 'Paid' " .
|
|
"WHERE record_num = $onum " .
|
|
"AND amount_paid = order_total";
|
|
db_query($query);
|
|
}
|
|
|
|
// Update user account transactions
|
|
db_query("UPDATE user_account " .
|
|
"SET amount_paid = amount_paid + $payment_amt " .
|
|
"WHERE school_num = $school_num " .
|
|
"AND user_num = $user_num " .
|
|
"AND record_num = $trxnum");
|
|
|
|
db_query("UPDATE user_account " .
|
|
"SET transaction_status = 'Closed' " .
|
|
"WHERE school_num = $school_num " .
|
|
"AND user_num = $user_num " .
|
|
"AND amount_paid = transaction_amount " .
|
|
"AND record_num = $trxnum");
|
|
}
|
|
|
|
$order_payments = db_escape_string($order_payments);
|
|
$transaction_payments = db_escape_string($transaction_payments);
|
|
$query = "UPDATE user_account SET order_payments = '$order_payments', transaction_payments = '$transaction_payments' WHERE record_num = $trx_num";
|
|
db_query($query);
|
|
|
|
if ($total_posted < abs($transaction_amount)) {
|
|
// Save transaction balance
|
|
$amt = abs($transaction_amount) - $total_posted;
|
|
$tamt = abs($amt) * -1;
|
|
$now = time();
|
|
$query = "INSERT INTO user_account_detail (school_num,user_num,transaction_num,posted_to_transaction,posted_amount,posted_schedule,entered_time,deleted) " .
|
|
"VALUES ($school_num, $user_num, $trx_num, 0, $tamt, 0, $now, 'N')";
|
|
writeLog("query: $query\n");
|
|
db_query($query);
|
|
}
|
|
}
|
|
else {
|
|
|
|
// Get set of orders to be updated
|
|
$query = "SELECT * FROM hotlunch_order_header " .
|
|
"WHERE school_num = $school_num " .
|
|
"AND status NOT IN ('Paid','Complimentary','Concession','Deleted') " .
|
|
"AND ordered_by_num IN ($people) " .
|
|
"AND order_total > 0";
|
|
$results = db_query($query);
|
|
|
|
if (db_num_rows($results) > 0) {
|
|
|
|
while ($row = db_fetch_array($results)) {
|
|
$onum = $row['record_num'];
|
|
$trxtotal = $row['order_total'];
|
|
$trxpaid = $row['amount_paid'];
|
|
$amt = number_format($trxtotal - $trxpaid,2);
|
|
$payment_amt = abs($amt);
|
|
if (strlen($order_payments) > 0) $order_payments .= "|";
|
|
$order_payments .= "{" . $onum . ":" . $payment_amt . "}";
|
|
}
|
|
|
|
$order_payments = db_escape_string($order_payments);
|
|
db_query("UPDATE user_account SET order_payments = '$order_payments' WHERE record_num = $trx_num");
|
|
|
|
// Update orders for this account
|
|
$today = date("Y-m-d",time());
|
|
$query = "UPDATE hotlunch_order_header " .
|
|
"SET amount_paid = amount_paid + $payment_amt, " .
|
|
"paid_date = '$today', " .
|
|
"payment_method = '$payment_method' " .
|
|
"WHERE school_num = $school_num " .
|
|
"AND status NOT IN ('Paid','Complimentary','Concession','Deleted') " .
|
|
"AND ordered_by_num IN ($people)";
|
|
db_query($query);
|
|
}
|
|
}
|
|
|
|
db_query("UPDATE user_account SET transaction_status = 'Closed' WHERE school_num = $school_num AND user_num = $user_num");
|
|
}
|
|
else {
|
|
$ordersPosted = false;
|
|
|
|
// If transactions have been selected to post against
|
|
if ($ordersPosted == false && isset($selected_transactions) && count($selected_transactions)) {
|
|
writeLog("post to selected transactions\n");
|
|
$transactions = implode(',',$selected_transactions);
|
|
$today = date("Y-m-d",time());
|
|
$transaction_payments = "";
|
|
$total_posted = 0;
|
|
$allocated_total = 0;
|
|
writeLog("transactions: $transactions\n");
|
|
|
|
// Get set of transactions to be updated
|
|
$query = "SELECT * FROM user_account " .
|
|
"WHERE school_num = $school_num " .
|
|
"AND record_num IN ($transactions)";
|
|
writeLog("query: $query\n");
|
|
$results = db_query($query);
|
|
|
|
$order_numbers = array();
|
|
while ($row = db_fetch_assoc($results)) {
|
|
$trxnum = $row['record_num'];
|
|
$onum = $row['order_num'];
|
|
$snum = $row['schedule_num'];
|
|
$trxtotal = $row['transaction_amount'];
|
|
$trxpaid = $row['amount_paid'];
|
|
$owing = $trxtotal - $trxpaid;
|
|
|
|
writeLog("trxtotal: $trxtotal\n");
|
|
writeLog("trxpaid: $trxpaid\n");
|
|
|
|
$amt = $owing;
|
|
writeLog("amt: $amt\n");
|
|
writeLog("allocated_total: $allocated_total\n");
|
|
writeLog("owing: $owing\n");
|
|
writeLog("transaction_amount: $transaction_amount\n");
|
|
if ($allocated_total + $owing > abs($transaction_amount)) {
|
|
$amt = abs($transaction_amount) - $allocated_total;
|
|
}
|
|
writeLog("amt: $amt\n");
|
|
$payment_amt = abs($amt);
|
|
writeLog("payment_amt: $payment_amt\n");
|
|
|
|
$total_posted += $payment_amt;
|
|
|
|
if (strlen($transaction_payments) > 0) $transaction_payments .= "|";
|
|
$transaction_payments .= "{" . $trxnum . ":" . $payment_amt . "}";
|
|
$allocated_total += $payment_amt;
|
|
|
|
// Save transaction details
|
|
$now = time();
|
|
if (abs($payment_amt) > 10000) $payment_amt = 5000;
|
|
$tamt = abs($payment_amt) * -1;
|
|
$snum = sprintf("%0d",intval($snum));
|
|
$query = "INSERT INTO user_account_detail (school_num,user_num,transaction_num,posted_to_transaction,posted_amount,posted_schedule,entered_time,deleted) " .
|
|
"VALUES ($school_num, $user_num, $trx_num, $trxnum, $tamt, $snum, $now, 'N')";
|
|
writeLog("query: $query\n");
|
|
db_query($query);
|
|
|
|
if ($onum > 0) {
|
|
if (strlen($order_payments) > 0) $order_payments .= "|";
|
|
$order_payments .= "{" . $onum . ":" . $payment_amt . "}";
|
|
$order_numbers[] = $onum;
|
|
|
|
$query = "UPDATE hotlunch_order_header " .
|
|
"SET amount_paid = amount_paid + $payment_amt, " .
|
|
"paid_date = '$today', " .
|
|
"payment_method = '$payment_method' " .
|
|
"WHERE record_num = $onum";
|
|
writeLog("query: $query\n");
|
|
db_query($query);
|
|
|
|
$query = "UPDATE hotlunch_order_header " .
|
|
"SET status = 'Paid' " .
|
|
"WHERE record_num = $onum " .
|
|
"AND amount_paid = order_total";
|
|
writeLog("query: $query\n");
|
|
db_query($query);
|
|
}
|
|
|
|
// Update user account transactions
|
|
$query = "UPDATE user_account " .
|
|
"SET amount_paid = amount_paid + $payment_amt " .
|
|
"WHERE school_num = $school_num " .
|
|
"AND user_num = $user_num " .
|
|
"AND record_num = $trxnum";
|
|
writeLog("query: $query\n");
|
|
db_query($query);
|
|
|
|
// Close transactions that are paid in full
|
|
$query = "UPDATE user_account " .
|
|
"SET transaction_status = 'Closed' " .
|
|
"WHERE school_num = $school_num " .
|
|
"AND user_num = $user_num " .
|
|
"AND amount_paid = transaction_amount " .
|
|
"AND record_num = $trxnum";
|
|
writeLog("query: $query\n");
|
|
db_query($query);
|
|
}
|
|
|
|
$order_payments = db_escape_string($order_payments);
|
|
$transaction_payments = db_escape_string($transaction_payments);
|
|
$query = "UPDATE user_account SET order_payments = '$order_payments', transaction_payments = '$transaction_payments' WHERE record_num = $trx_num";
|
|
writeLog("query: $query\n");
|
|
db_query($query);
|
|
|
|
$transaction_amount = floatval($transaction_amount);
|
|
writeLog("total_posted: $total_posted\n");
|
|
writeLog("transaction_amount: $transaction_amount\n");
|
|
|
|
if ($total_posted < abs($transaction_amount)) {
|
|
// Save transaction balance
|
|
$amt = abs($transaction_amount) - $total_posted;
|
|
$tamt = abs($amt) * -1;
|
|
$now = time();
|
|
$query = "INSERT INTO user_account_detail (school_num,user_num,transaction_num,posted_to_transaction,posted_amount,posted_schedule,entered_time,deleted) " .
|
|
"VALUES ($school_num, $user_num, $trx_num, 0, $amt, 0, $now, 'N')";
|
|
writeLog("query: $query\n");
|
|
db_query($query);
|
|
}
|
|
|
|
$ordersPosted = true;
|
|
}
|
|
|
|
// If orders have been selected to post against
|
|
if ($ordersPosted == false && isset($selected_orders) && count($selected_orders)) {
|
|
writeLog("post to selected orders\n");
|
|
$sorders = implode(',',$selected_orders);
|
|
$today = date("Y-m-d",time());
|
|
|
|
$allocated_total = 0;
|
|
writeLog("orders: $sorders\n");
|
|
|
|
// Get set of orders to be updated
|
|
$query = "SELECT * FROM hotlunch_order_header " .
|
|
"WHERE school_num = $school_num " .
|
|
"AND record_num IN ($sorders)";
|
|
$results = db_query($query);
|
|
|
|
if (db_num_rows($results) > 0) {
|
|
|
|
$order_numbers = array();
|
|
while ($row = db_fetch_assoc($results)) {
|
|
$onum = $row['record_num'];
|
|
$trxtotal = $row['order_total'];
|
|
$trxpaid = $row['amount_paid'];
|
|
$amt = number_format($trxtotal - $trxpaid,2);
|
|
|
|
$transaction_amount = floatval($transaction_amount);
|
|
if ($allocated_total + $owing > abs($transaction_amount)) {
|
|
$amt = abs($transaction_amount) - $allocated_total;
|
|
}
|
|
writeLog("amt: $amt\n");
|
|
$payment_amt = abs($amt);
|
|
writeLog("payment_amt: $payment_amt\n");
|
|
|
|
$allocated_total += $payment_amt;
|
|
|
|
if (strlen($order_payments) > 0) $order_payments .= "|";
|
|
$order_payments .= "{" . $onum . ":" . $amt . "}";
|
|
$order_numbers[] = $onum;
|
|
|
|
$query = "UPDATE hotlunch_order_header " .
|
|
"SET amount_paid = amount_paid + $payment_amt, " .
|
|
"paid_date = '$today', " .
|
|
"payment_method = '$payment_method' " .
|
|
"WHERE record_num = $onum";
|
|
db_query($query);
|
|
|
|
$query = "UPDATE hotlunch_order_header " .
|
|
"SET status = 'Paid' " .
|
|
"WHERE record_num = $onum " .
|
|
"AND amount_paid = order_total";
|
|
db_query($query);
|
|
|
|
$query = "UPDATE user_account " .
|
|
"SET transaction_status = 'Closed' " .
|
|
"WHERE school_num = $school_num " .
|
|
"AND user_num = $user_num " .
|
|
"AND amount_paid = transaction_amount " .
|
|
"AND record_num = $onum";
|
|
db_query($query);
|
|
}
|
|
|
|
$order_payments = db_escape_string($order_payments);
|
|
$query = "UPDATE user_account SET order_payments = '$order_payments' WHERE record_num = $trx_num";
|
|
db_query($query);
|
|
}
|
|
|
|
$ordersPosted = true;
|
|
}
|
|
|
|
// If a schedule is being posted to then get the orders for that schedule
|
|
if ($ordersPosted == false && strlen($people) && isset($schedule_num) && $schedule_num > 0) {
|
|
writeLog("post to selected schedule\n");
|
|
$schedule_balance = db_getVal("select sum(order_total) from hotlunch_order_header " .
|
|
"where school_num = $school_num " .
|
|
"AND schedule_num = $schedule_num " .
|
|
"AND ordered_by_num in ($people) " .
|
|
"AND status NOT IN ('Paid','Complimentary','Concession','Deleted')");
|
|
|
|
// If the payment is posted to a schedule and the amount exceeds the balance owing of schedule orders
|
|
$transaction_amount = floatval($transaction_amount);
|
|
$schedule_balance = floatval($schedule_balance);
|
|
if (abs($transaction_amount) >= abs($schedule_balance)) {
|
|
|
|
// Get set of orders to be updated
|
|
$query = "SELECT * FROM hotlunch_order_header " .
|
|
"WHERE school_num = $school_num " .
|
|
"AND schedule_num = $schedule_num " .
|
|
"AND status NOT IN ('Paid','Complimentary','Concession','Inactive','Deleted') " .
|
|
"AND ordered_by_num IN ($people) " .
|
|
"AND order_total > 0";
|
|
$results = db_query($query);
|
|
|
|
if (db_num_rows($results) > 0) {
|
|
|
|
$order_numbers = array();
|
|
while ($row = db_fetch_array($results)) {
|
|
$onum = $row['record_num'];
|
|
$trxtotal = $row['order_total'];
|
|
$trxpaid = $row['amount_paid'];
|
|
$amt = number_format($trxtotal - $trxpaid,2);
|
|
if (strlen($order_payments) > 0) $order_payments .= "|";
|
|
$order_payments .= "{" . $onum . ":" . $amt . "}";
|
|
$order_numbers[] = $onum;
|
|
|
|
db_query("UPDATE user_account SET transaction_status = 'Closed' WHERE school_num = $school_num AND user_num = $user_num AND order_num = $onum");
|
|
}
|
|
|
|
$order_payments = db_escape_string($order_payments);
|
|
db_query("UPDATE user_account SET order_payments = '$order_payments' WHERE record_num = $trx_num");
|
|
|
|
// Update orders for this account
|
|
$today = date("Y-m-d",time());
|
|
$query = "UPDATE hotlunch_order_header " .
|
|
"SET status = 'Paid', " .
|
|
"amount_paid = order_total, " .
|
|
"paid_date = '$today', " .
|
|
"payment_method = '$payment_method' " .
|
|
"WHERE school_num = $school_num " .
|
|
"AND schedule_num = $schedule_num " .
|
|
"AND status NOT IN ('Paid','Complimentary','Concession') " .
|
|
"AND ordered_by_num IN ($people) " .
|
|
"AND order_total > 0";
|
|
db_query($query);
|
|
}
|
|
}
|
|
|
|
$ordersPosted = true;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
$form_message = getLanguageText('RECORD-INSERTED-MSG');
|
|
|
|
if ($_SESSION['UserInfo']['type'] == 'Staff' && $prev_naction == 'post') {
|
|
header("Location: /?action=class-balances&class_num=$class_num&trx_num=$trx_num&form_message=" . urlencode($form_message));
|
|
}
|
|
else {
|
|
header("Location: /?action=user-account&naction=list&user_num=$user_num&trx_num=$trx_num&form_message=" . urlencode($form_message));
|
|
}
|
|
exit();
|
|
|
|
case "update-trx":
|
|
$user_num = intval($_POST['posted_user_num']);
|
|
foreach ($_POST as $key => $val) {
|
|
if (preg_match("/^trx_schedule/",$key)) {
|
|
$trx_num = intval(preg_replace("/^trx_schedule_/","",$key));
|
|
|
|
$uset = "";
|
|
$ukey = "user_num_$trx_num";
|
|
if (array_key_exists($ukey, $_POST) && $_POST[$ukey] > 0) $uset = ", user_num = " . $_POST[$ukey];
|
|
$query = "UPDATE user_account SET schedule_num = $val$uset WHERE school_num = $school_num AND user_num = $user_num AND record_num = $trx_num";
|
|
db_query($query);
|
|
}
|
|
}
|
|
|
|
$form_message = getLanguageText('TEXT-TRANSACTIONS-UPDATED');
|
|
include "form.messages.php";
|
|
$view = 'details';
|
|
$naction = "list";
|
|
break;
|
|
|
|
case "delete":
|
|
include "user_account.delete.form.php";
|
|
$naction = "list";
|
|
break;
|
|
|
|
case "delete-confirmed":
|
|
extract($_GET);
|
|
$record_num = intval($record_num);
|
|
|
|
if ($record_num == 0) {
|
|
$error_message = getLanguageText('ERRORMSG-MISSING-DATA');
|
|
header("Location: /?error_messge=".urlencode($error_message));
|
|
exit();
|
|
}
|
|
|
|
if (strlen($delete_reason) < 3) {
|
|
$error_message = "Error: Reason for Deleting Transaction is Required";
|
|
$tn = $record_num;
|
|
$naction = 'delete';
|
|
header("Location: /?action=user-account&naction=$naction&user_num=$user_num&form_message=" . urlencode($form_message) . "&error_message=" . urlencode($error_message));
|
|
exit();
|
|
}
|
|
|
|
// Get the user transaction record
|
|
$results = db_query("SELECT * FROM user_account WHERE school_num = $school_num AND record_num = $record_num");
|
|
$row = db_fetch_assoc($results);
|
|
|
|
// If this was a Payment then update the order status
|
|
if ($row['transaction_amount'] < 0.00) {
|
|
$default_order_status = "Unpaid";
|
|
|
|
// Check for user_account_detail entries
|
|
$details = db_query("SELECT * FROM user_account_detail WHERE school_num = $school_num AND transaction_num = $record_num");
|
|
|
|
if (db_num_rows($details) > 0) {
|
|
while ($row = db_fetch_assoc($details)) {
|
|
$trxNum = $row['posted_to_transaction'];
|
|
$trxAmt = $row['posted_amount'];
|
|
|
|
$trxType = db_getVal("SELECT transaction_type FROM user_account WHERE school_num = $school_num AND record_num = $trxNum");
|
|
if ($trxType == 'Order') {
|
|
$orderNum = db_getVal("SELECT order_num FROM user_account WHERE school_num = $school_num AND record_num = $trxNum");
|
|
$query = "UPDATE hotlunch_order_header " .
|
|
"SET status = '$default_order_status', amount_paid = amount_paid - $trxAmt, paid_date = NULL " .
|
|
"WHERE school_num = $school_num " .
|
|
"AND record_num = $orderNum";
|
|
db_query($query);
|
|
|
|
$query = "UPDATE hotlunch_order_header " .
|
|
"SET payment_method = NULL " .
|
|
"WHERE school_num = $school_num " .
|
|
"AND amount_paid = 0 " .
|
|
"AND record_num = $orderNum";
|
|
db_query($query);
|
|
}
|
|
|
|
// Set posted to transaction as Open
|
|
$query = "UPDATE user_account SET transaction_status = 'Open' WHERE school_num = $school_num AND record_num = $trxNum";
|
|
db_query($query);
|
|
}
|
|
}
|
|
else {
|
|
$order_payments = $row['order_payments'];
|
|
$entries = explode("|",$order_payments);
|
|
if (count($entries)) {
|
|
for ($i = 0; $i < count($entries); $i++) {
|
|
$entry = str_replace('{','',str_replace('}','',$entries[$i]));
|
|
if (strlen($entry) && strpos($entry, ':') !== false) {
|
|
list($order_num,$amt) = explode(":",$entry);
|
|
if (isset($order_num) && strlen($order_num) > 0) {
|
|
$query = "UPDATE hotlunch_order_header " .
|
|
"SET status = '$default_order_status',amount_paid = 0,paid_date = NULL,payment_method = NULL " .
|
|
"WHERE school_num = $school_num " .
|
|
"AND record_num = $order_num";
|
|
db_query($query);
|
|
|
|
$query = "UPDATE user_account SET transaction_status = 'Open' WHERE school_num = $school_num AND order_num = $order_num";
|
|
db_query($query);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// Update the user transaction
|
|
$time = date("Y-m-d H:i:s",time());
|
|
$unum = intval($_SESSION['UserInfo']['record_num']);
|
|
$delete_reason = db_escape_string($delete_reason);
|
|
|
|
$query = "UPDATE user_account SET deleted = 'Y', deleted_datetime = '$time', deleted_by_user_num = $unum, delete_reason = '$delete_reason' " .
|
|
"WHERE school_num = $school_num AND record_num = $record_num";
|
|
db_query($query);
|
|
|
|
// if (isset($row['payment_reference']) && strlen($row['payment_reference'])) {
|
|
// $payment_reference = $row['payment_reference'];
|
|
// if (strlen($payment_reference)) {
|
|
// $query = "UPDATE user_account SET deleted = 'Y', deleted_datetime = '$time', deleted_by_user_num = $unum, delete_reason = '$delete_reason' " .
|
|
// "WHERE school_num = $school_num AND payment_reference = '$payment_reference' AND record_num != $record_num";
|
|
// db_query($query);
|
|
// }
|
|
// }
|
|
|
|
$form_message = getLanguageText('RECORD-DELETED-MSG');
|
|
|
|
$naction = "list";
|
|
header("Location: /?action=user-account&naction=list&user_num=$user_num&form_message=" . urlencode($form_message));
|
|
exit();
|
|
|
|
case "set-paid":
|
|
$user_num = intval($_GET['user_num']);
|
|
$order_num = intval($_GET['order_num']);
|
|
$today = date('Y-m-d',time());
|
|
db_query("UPDATE hotlunch_order_header SET status = 'Paid', amount_paid = order_total, paid_date = '$today' WHERE school_num = $school_num AND record_num = $order_num");
|
|
$naction = "list";
|
|
break;
|
|
|
|
case "set-unpaid":
|
|
$user_num = intval($_GET['user_num']);
|
|
$order_num = intval($_GET['order_num']);
|
|
db_query("UPDATE hotlunch_order_header SET status = 'Unpaid' WHERE school_num = $school_num AND record_num = $order_num");
|
|
$naction = "list";
|
|
break;
|
|
|
|
default:
|
|
$naction = "list";
|
|
break;
|
|
}
|
|
|
|
|
|
//
|
|
// List all records if naction is "list"
|
|
//
|
|
if ($naction == "list") {
|
|
// Get account balance
|
|
if (in_array($_SESSION['UserInfo']['type'],array('SuperUser','System Administrator','Application Administrator','Schedule Team','Financial','Financial Team'))) {
|
|
$balance = floatval(db_getVal("select sum(transaction_amount) as balance from user_account where school_num = $school_num and user_num = $user_num AND (deleted != 'Y' OR deleted IS NULL)"));
|
|
if ($balance < 0.01) updateOrders($user_num,'Paid');
|
|
}
|
|
|
|
if (array_key_exists('trx_num',$_GET) && intval($_GET['trx_num']) > 0) {
|
|
$trxnum = intval($_GET['trx_num']);
|
|
echo("<br><a href='#' class='receipt-print btn btn--primary btn--xsmall' trx-num='$trxnum' unum='$user_num'><span>" . getLanguageText('TEXT-RECEIPT') . "</span></a><br><br>");
|
|
}
|
|
|
|
include "user_account.list.php";
|
|
|
|
if (in_array($_SESSION['UserInfo']['type'],array('SuperUser','System Administrator','Application Administrator','Schedule Team','Financial','Financial Team'))) {
|
|
?>
|
|
<p><a href='?action=user-account&user_num=<?php echo($user_num) ?>&naction=add' class='btn btn--primary>'> <?php displayLanguageText('TEXT-ENTER-TRANSACTION'); ?> </a></p>
|
|
<?php
|
|
}
|
|
else {
|
|
if (isset($balance) && $balance > 0) include "payment_form.php";
|
|
}
|
|
}
|
|
|
|
include "get.adobe.php";
|
|
|
|
?>
|
|
|
|
|
|
<script>
|
|
$(document).ready(function() {
|
|
|
|
<?php include "include/ajax.Setup.php" ?>
|
|
|
|
$("#view-matching-users").hide();
|
|
|
|
$("#trx-dialog").dialog({
|
|
autoOpen: false,
|
|
minWidth: 980,
|
|
minHeight: 600,
|
|
modal: true,
|
|
position: { my: 'center', at: 'center' }
|
|
});
|
|
|
|
$("#receipt-dialog").dialog({
|
|
autoOpen: false,
|
|
minWidth: 750,
|
|
modal: true,
|
|
position: { my: 'center top', at: 'center top', of: window }
|
|
});
|
|
|
|
|
|
$("#user-search-field").autocomplete({
|
|
source: "ajax.UserSearch.php",
|
|
minLength: 3,
|
|
width: 750,
|
|
selectFirst: false,
|
|
select: function( event, results ) {
|
|
var user_num = results.item.user_num;
|
|
$("#user-search-field").val('');
|
|
loadUserAcct(user_num);
|
|
}
|
|
});
|
|
|
|
function loadUserAcct(user_num) {
|
|
var data = "table_name=user_account"+
|
|
"&sequence_flag=N"+
|
|
"&action=user-account-transactions"+
|
|
"&table_id="+
|
|
"&user_num="+user_num;
|
|
|
|
// Get user account transactions
|
|
$.getJSON( "ajax.DataTable.Fetch.php",data,
|
|
function(results) {
|
|
$("#user-account-div").html(results.content);
|
|
$("#trx-dialog").dialog("open");
|
|
if (results.count > 40) window.location = '#user-account-div';
|
|
$("#user-search-field").val('');
|
|
}
|
|
);
|
|
}
|
|
|
|
$(document).on('click', '.trx-close-btn', function(e) {
|
|
e.preventDefault();
|
|
$("#trx-dialog").dialog("close");
|
|
});
|
|
|
|
$(document).on('click', '#edit-user-account-transactions', function(e) {
|
|
e.preventDefault();
|
|
|
|
var unum = $(this).attr('unum');
|
|
window.location = "?action=user-account&user_num="+unum;
|
|
});
|
|
|
|
|
|
$(document).on("click", ".receipt-print", function(e) {
|
|
e.preventDefault();
|
|
var trxnum = $(this).attr('trx-num');
|
|
|
|
$("#receipt-dialog").dialog("open");
|
|
loadReceipt(trxnum);
|
|
});
|
|
|
|
function loadReceipt(trxnum) {
|
|
var data = "table_name=user_account"+
|
|
"&sequence_flag=N"+
|
|
"&action=trx-receipt"+
|
|
"&table_id="+
|
|
"&trxnum="+trxnum;
|
|
|
|
// Get receipt text
|
|
$.getJSON( "ajax.DataTable.Fetch.php",data,
|
|
function(results) {
|
|
$("#receipt-content").html(results.content);
|
|
<?php if (!isset($action) || $action != 'receipt-print') { ?>
|
|
// $("#receipt-dialog").dialog("open");
|
|
$("#receipt-content").printElement();
|
|
<?php } ?>
|
|
<?php if (isset($action) && $action == 'receipt-print') { ?>
|
|
window.print();
|
|
<?php } ?>
|
|
}
|
|
);
|
|
}
|
|
|
|
$("#close-receipt-btn").click(function(e) {
|
|
e.preventDefault();
|
|
$("#receipt-dialog").dialog("close");
|
|
<?php if (isset($action) && $action == 'receipt') { ?>
|
|
location.assign("/");
|
|
<?php } ?>
|
|
});
|
|
|
|
|
|
function PrintElem(elem) {
|
|
var content = $(elem).html();
|
|
Popup(content);
|
|
}
|
|
|
|
function Popup(data) {
|
|
var mywindow = window.open('', 'my div', 'height=400,width=600');
|
|
mywindow.document.write('<html><head><title></title>');
|
|
mywindow.document.write('<link rel="stylesheet" type="text/css" href="css/master.css" media="all">');
|
|
mywindow.document.write('<link rel="stylesheet" type="text/css" href="css/layout.wide.css" media="all">');
|
|
mywindow.document.write('<link rel="stylesheet" type="text/css" href="css/top-bar.css" media="all">');
|
|
mywindow.document.write('<link rel="stylesheet" type="text/css" href="css/primary-nav.css" media="all">');
|
|
mywindow.document.write('<link rel="stylesheet" type="text/css" href="css/secondary-nav.css" media="all">');
|
|
mywindow.document.write('<link rel="stylesheet" type="text/css" href="css/black-green.css" media="all">');
|
|
mywindow.document.write('<link rel="stylesheet" type="text/css" href="css/Calendar.black.css" media="all">');
|
|
mywindow.document.write(data);
|
|
mywindow.document.write('</body></html>');
|
|
mywindow.document.close();
|
|
mywindow.print();
|
|
}
|
|
|
|
$("#print-receipt-btn").click(function(e) {
|
|
e.preventDefault();
|
|
PrintElem('#receipt-content');
|
|
});
|
|
|
|
|
|
});
|
|
|
|
</script>
|
|
|
|
<div id='trx-dialog' title="<?php displayLanguageText('TEXT-USER-ACCOUNT'); ?>">
|
|
<div id='user-account-div'></div>
|
|
<br>
|
|
<p><button class='trx-close-btn btn btn--outline btn--xsmall'> <?php displayLanguageText('TEXT-CLOSE-WINDOW'); ?> </button></p>
|
|
</div>
|
|
|
|
<div id='receipt-dialog' title="<?php displayLanguageText('TEXT-TRANSACTION-RECEIPT'); ?>">
|
|
<div class='dialog-buttons noPrint'>
|
|
<a href='#' id='print-receipt-btn'><span class='btn btn--primary btn--xsmall'><?php displayLanguageText('TEXT-PRINT'); ?></span></a>
|
|
<a href='#' id='close-receipt-btn'><span class='btn btn--primary btn--xsmall'><?php displayLanguageText('TEXT-CLOSE'); ?></span></a>
|
|
</div>
|
|
<div id='receipt-content'></div>
|
|
</div>
|
|
|
|
<?php
|
|
endTemplate();
|
|
?>
|