Files
HotLunches/hotlunch.order_recovery.php
2026-10-19 15:22:52 -07:00

254 lines
9.8 KiB
PHP

<?php
require_once("include/globals.php");
if (SessionActive() != 1 || ! UserLoggedIn()) {
header("Location: /");
exit();
}
// Start the template and output buffering
startTemplate();
$headerArgs = "";
// Ensure that only valid user types have access to this module
$AllowedUserTypes = array('SuperUser','System Administrator','Application Administrator','Application Volunteer','Schedule Volunteer','Administration Team');
if (checkPermission($AllowedUserTypes) != 1) {
$error_message = getLanguageText('ERRORMSG-ACCESS-DENIED');
$action = '';
$naction = '';
header("Location: /?action=$action&naction=$naction$headerArgs&form_message=" . urlencode($form_message) . "&error_message=" . urlencode($error_message));
exit();
}
//
// Process all actions to list and edit records
//
$naction = "";
if (array_key_exists('schedule_num',$_GET)) $schedule_num = intval($_GET['schedule_num']);
if (array_key_exists('schedule_num',$_POST)) $schedule_num = intval($_POST['schedule_num']);
if (array_key_exists('naction',$_GET)) $naction = $_GET['naction'];
if (array_key_exists('naction',$_POST)) $naction = $_POST['naction'];
$headerArgs = "&schedule_num=$schedule_num&naction=$naction";
if (!isset($schedule_num)) {
$error_message = getLanguageText('ERRORMSG-MISSING-DATA');
$action = 'schedule-reports';
$naction = '';
header("Location: /?action=$action&naction=$naction$headerArgs&form_message=" . urlencode($form_message) . "&error_message=" . urlencode($error_message));
exit();
}
// Check if this is a valid schedule
$query = "select * from hotlunch_schedules " .
"where school_num = $school_num " .
"and record_num = $schedule_num";
$results = db_query($query);
if (db_num_rows($results) == 0) {
$error_message = getLanguageText('ERRORMSG-INVALID-SCHEDULE-NUM');
$action = 'schedule-reports';
$naction = '';
header("Location: /?action=$action&naction=$naction$headerArgs&form_message=" . urlencode($form_message) . "&error_message=" . urlencode($error_message));
exit();
}
$hotlunch_schedule = db_fetch_array($results);
$schedule_name = $hotlunch_schedule['schedule_name'];
// If this is a ScheduleTeam user check if they have permission to access this schedule
if (in_array($_SESSION['UserInfo']['type'],array('Schedule Team','Schedule Volunteer'))) {
$userNum = intval($_SESSION['UserInfo']['record_num']);
$access = db_getVal("SELECT count(*) FROM hotlunch_schedules_people WHERE school_num = $school_num AND schedule_num = $schedule_num AND user_num = $userNum");
if (!$access || $access == 0) {
$error_message = getLanguageText('ERRORMSG-NO-SCHEDULE-ACCESS');
$action = 'schedule-reports';
$naction = '';
header("Location: /?action=$action&naction=$naction$headerArgs&form_message=" . urlencode($form_message) . "&error_message=" . urlencode($error_message));
exit();
}
}
$templatePageTitle = getLanguageText('TEXT-ORDER-RECOVERY') . " - $schedule_name";
include "hotlunch_reports.menu.php";
echo("<br>\n");
$school_num = $school_record['record_num'];
include "form.messages.php";
// Handle the recovery of any posted menu dates
if ($naction == 'recover') {
$selected_menus = array();
if (array_key_exists('selected_menus', $_POST)) $selected_menus = $_POST['selected_menus'];
if (count($selected_menus) > 0) {
?>
<br>
<table width='500' class='content-table'>
<tr class='heading'>
<td align='left'><b><?php displayLanguageText('TEXT-MENU'); ?></b></td>
<td align='center'><b><?php displayLanguageText('TEXT-ITEMS'); ?></b></td>
<td align='center'><b><?php displayLanguageText('TEXT-ORDERS'); ?></b></td>
</tr>
<?php
for ($i = 0; $i < count($selected_menus); $i++) {
$menu_num = $selected_menus[$i];
$results = db_query("SELECT * FROM hotlunch_menu_dates WHERE school_num = $school_num AND schedule_num = $schedule_num AND record_num = $menu_num");
$row = db_fetch_array($results);
$mtitle = trim($row['menu_title']);
$log_txt = $mtitle;
$m_date = $row['menu_date'];
$record_num = $row['record_num'];
if ($hotlunch_schedule['display_menu_date'] == 'Y') $mtitle .= " - " . dateString('default',strtotime($m_date));
// Format the text to cleanup the user account entries
$log_txt .= "%" . dateString('default',strtotime($m_date));
$class = 'data-even';
if ($i % 2) $class = 'data-odd';
// Determine how many items are to be updated
$items = db_getVal("SELECT count(*) FROM hotlunch_order_items WHERE school_num = $school_num AND schedule_num = $schedule_num AND menu_num = $menu_num AND active = 'N'");
$orders = db_getVal("SELECT count(DISTINCT order_num) FROM hotlunch_order_items WHERE school_num = $school_num AND schedule_num = $schedule_num AND menu_num = $menu_num AND active = 'N'");
// Update menu items
db_query("UPDATE hotlunch_order_items SET active = 'Y' WHERE school_num = $school_num AND schedule_num = $schedule_num AND menu_num = $menu_num AND active = 'N'");
// Update user account
$ltext = db_escape_string($log_txt);
db_query("DELETE FROM user_account WHERE school_num = $school_num AND schedule_num = $schedule_num AND transaction_type = 'TRX Note' AND note LIKE '%$ltext%'");
?>
<tr class='<?php echo($class) ?>'>
<td width='50%' align='left'><?php echo($mtitle) ?></td>
<td width='25%' align='center'><?php echo($items) ?></td>
<td width='25%' align='center'><?php echo($orders) ?></td>
</tr>
<?php
}
?>
</table>
<?php
// Get all the orders for the schedule and update the order totals
$orders = db_query("SELECT DISTINCT order_num FROM hotlunch_order_items WHERE school_num = $school_num AND schedule_num = $schedule_num");
while ($row = db_fetch_array($orders)) {
$order_num = $row['order_num'];
// Get new order total
$query = "select sum(extended_price) " .
"from hotlunch_order_items " .
"where school_num = $school_num " .
"and order_num = $order_num " .
"and schedule_num = $schedule_num " .
"and active = 'Y'";
$order_subtotal = db_getVal($query);
// Get current order header
$results = db_query("SELECT * FROM hotlunch_order_header WHERE record_num = $order_num");
$row = db_fetch_array($results);
$pnum = $row['ordered_by_num'];
$disc = $row['discount_amount'];
$total = $row['order_total'];
$paid = $row['amount_paid'];
$status = $row['status'];
$amount_paid = $paid;
if (!isset($order_total)) $order_total = 0;
if ($status == 'Paid' || $total == $paid || $paid > $order_total) $amount_paid = $order_total;
// Calculate new order total
$order_total = $order_subtotal;
if ($disc > 0 && $order_subtotal > $disc) $order_total = $order_subtotal - $disc;
$order_subtotal = floatval($order_subtotal);
$order_total = floatval($order_total);
$amount_paid = floatval($amount_paid);
$today = date("Y-m-d",time());
// Update order header
db_query("UPDATE hotlunch_order_header " .
"SET order_total = $order_total, " .
"order_subtotal = $order_subtotal, " .
"amount_paid = $amount_paid, " .
"updated_date = '$today' " .
"WHERE record_num = $order_num");
// Update user account
if ($pnum > 0) {
$query = "update user_account " .
"set transaction_amount = $order_total " .
"where school_num = $school_num " .
"and schedule_num = $schedule_num " .
"and order_num = $order_num " .
"and people_num = $pnum " .
"and transaction_type = 'Order'";
db_query($query);
}
}
}
}
// Get all the menu dates for the selection list
$query = "select * from hotlunch_menu_dates " .
"where school_num = $school_num " .
"and schedule_num = $schedule_num " .
"order by sequence,menu_date";
$results = db_query($query);
?>
<br>
<form method='post' action='/'>
<table width='500' class='content-table'>
<tr class='heading'>
<td align='center'><b><?php echo("$schedule_name"); ?></b></td>
<td></td>
</tr>
<?php
$count = 0;
while ($row = db_fetch_array($results)) {
$mtitle = $row['menu_title'];
$m_date = $row['menu_date'];
$record_num = $row['record_num'];
if ($hotlunch_schedule['display_menu_date'] == 'Y') $mtitle .= " - " . dateString('default',strtotime($m_date));
$class = 'data-even';
if ($count % 2) $class = 'data-odd';
$count++;
?>
<tr class='<?php echo($class) ?>'>
<td align='left'><?php echo($mtitle) ?> :: </td>
<td align='center'><input type='checkbox' name='selected_menus[]' value='<?php echo($record_num) ?>'></td>
</tr>
<?php
}
?>
</table>
<br>
<input type='hidden' name='action' value='order-recovery'>
<input type='hidden' name='naction' value='recover'>
<input type='hidden' name='schedule_num' value='<?php echo($schedule_num) ?>'>
<input type='submit' name='button' class='btn btn--primary' value="<?php displayLanguageText('TEXT-RECOVER-ORDERS'); ?>">
</form>
<br>
<?php
include "hotlunch_orders.print_buttons.php";
endTemplate();
?>