ServeMenus.php 8.33 KB
Newer Older
François Jacquet's avatar
François Jacquet committed
1 2
<?php

François Jacquet's avatar
François Jacquet committed
3 4
Widgets( 'fsa_status_active' );
Widgets( 'fsa_barcode' );
François Jacquet's avatar
François Jacquet committed
5

François Jacquet's avatar
François Jacquet committed
6
Search( 'student_id', $extra );
François Jacquet's avatar
François Jacquet committed
7

François Jacquet's avatar
François Jacquet committed
8
if ( $_REQUEST['modfunc'] === 'submit' )
François Jacquet's avatar
François Jacquet committed
9
{
10
	if ( ! empty( $_REQUEST['submit']['cancel'] ) )
François Jacquet's avatar
François Jacquet committed
11
	{
12 13 14 15
		if ( DeletePrompt( _( 'Sale' ), _( 'Cancel' ) ) )
		{
			unset( $_SESSION['FSA_sale'] );

François Jacquet's avatar
François Jacquet committed
16 17
			// Unset modfunc & redirect URL.
			RedirectURL( 'modfunc' );
18
		}
François Jacquet's avatar
François Jacquet committed
19
	}
François Jacquet's avatar
François Jacquet committed
20
	elseif ( $_REQUEST['submit']['save']
21
		&& ! empty( $_SESSION['FSA_sale'] ) )
François Jacquet's avatar
François Jacquet committed
22
	{
François Jacquet's avatar
François Jacquet committed
23 24 25 26
		$student = DBGet( "SELECT ACCOUNT_ID,DISCOUNT
			FROM FOOD_SERVICE_STUDENT_ACCOUNTS
			WHERE STUDENT_ID='" . UserStudentID() . "'" );

François Jacquet's avatar
François Jacquet committed
27 28
		$student = $student[1];

François Jacquet's avatar
François Jacquet committed
29 30 31
		$items_RET = DBGet( "SELECT DESCRIPTION,SHORT_NAME,PRICE,PRICE_REDUCED,PRICE_FREE
			FROM FOOD_SERVICE_ITEMS
			WHERE SCHOOL_ID='" . UserSchool() . "'", array(), array( 'SHORT_NAME' ) );
François Jacquet's avatar
François Jacquet committed
32 33

		// get next transaction id
34
		$id = DBSeqNextID( 'food_service_transactions_transaction_id_seq' );
François Jacquet's avatar
François Jacquet committed
35 36

		$item_id = 0;
François Jacquet's avatar
François Jacquet committed
37 38

		foreach ( (array) $_SESSION['FSA_sale'] as $item_sn )
François Jacquet's avatar
François Jacquet committed
39
		{
François Jacquet's avatar
François Jacquet committed
40
			// determine price based on discount
François Jacquet's avatar
François Jacquet committed
41
			$price = $items_RET[$item_sn][1]['PRICE'];
François Jacquet's avatar
François Jacquet committed
42
			$discount = $student['DISCOUNT'];
François Jacquet's avatar
François Jacquet committed
43 44 45 46 47 48 49

			if ( $student['DISCOUNT'] == 'Reduced' )
			{
				if ( $items_RET[$item_sn][1]['PRICE_REDUCED'] != '' )
				{
					$price = $items_RET[$item_sn][1]['PRICE_REDUCED'];
				}
François Jacquet's avatar
François Jacquet committed
50
				else
François Jacquet's avatar
François Jacquet committed
51
				{
François Jacquet's avatar
François Jacquet committed
52
					$discount = '';
François Jacquet's avatar
François Jacquet committed
53 54 55 56 57 58 59 60
				}
			}
			elseif ( $student['DISCOUNT'] == 'Free' )
			{
				if ( $items_RET[$item_sn][1]['PRICE_FREE'] != '' )
				{
					$price = $items_RET[$item_sn][1]['PRICE_FREE'];
				}
François Jacquet's avatar
François Jacquet committed
61
				else
François Jacquet's avatar
François Jacquet committed
62
				{
François Jacquet's avatar
François Jacquet committed
63
					$discount = '';
François Jacquet's avatar
François Jacquet committed
64 65
				}
			}
François Jacquet's avatar
François Jacquet committed
66 67

			$fields = 'ITEM_ID,TRANSACTION_ID,AMOUNT,DISCOUNT,SHORT_NAME,DESCRIPTION';
François Jacquet's avatar
François Jacquet committed
68 69 70
			$values = "'" . $item_id++ . "','" . $id . "','-" . $price . "','" . $discount . "','" . $items_RET[$item_sn][1]['SHORT_NAME'] . "','" . $items_RET[$item_sn][1]['DESCRIPTION'] . "'";
			$sql = "INSERT INTO FOOD_SERVICE_TRANSACTION_ITEMS (" . $fields . ") values (" . $values . ")";
			DBQuery( $sql );
François Jacquet's avatar
François Jacquet committed
71
		}
François Jacquet's avatar
François Jacquet committed
72

François Jacquet's avatar
François Jacquet committed
73 74 75 76 77 78
		$sql1 = "UPDATE FOOD_SERVICE_ACCOUNTS
			SET TRANSACTION_ID='" . $id . "',BALANCE=BALANCE+(SELECT sum(AMOUNT)
				FROM FOOD_SERVICE_TRANSACTION_ITEMS
				WHERE TRANSACTION_ID='" . $id . "')
			WHERE ACCOUNT_ID='" . $student['ACCOUNT_ID'] . "'";

François Jacquet's avatar
François Jacquet committed
79
		$fields = 'TRANSACTION_ID,ACCOUNT_ID,STUDENT_ID,SYEAR,SCHOOL_ID,DISCOUNT,BALANCE,TIMESTAMP,SHORT_NAME,DESCRIPTION,SELLER_ID';
François Jacquet's avatar
François Jacquet committed
80 81 82 83 84 85 86

		$values = "'" . $id . "','" . $student['ACCOUNT_ID'] . "','" . UserStudentID() . "','" .
			UserSyear() . "','" . UserSchool() . "','" . $discount .
			"',(SELECT BALANCE FROM FOOD_SERVICE_ACCOUNTS WHERE ACCOUNT_ID='" . $student['ACCOUNT_ID'] .
			"'),CURRENT_TIMESTAMP,'" . $menus_RET[$_REQUEST['menu_id']][1]['TITLE'] . "','" .
			$menus_RET[$_REQUEST['menu_id']][1]['TITLE'] . ' - ' . DBDate() . "','" . User( 'STAFF_ID' ) . "'";

François Jacquet's avatar
François Jacquet committed
87
		$sql2 = "INSERT INTO FOOD_SERVICE_TRANSACTIONS (" . $fields . ") values (" . $values . ")";
François Jacquet's avatar
François Jacquet committed
88

François Jacquet's avatar
François Jacquet committed
89
		DBQuery( 'BEGIN; ' . $sql1 . '; ' . $sql2 . '; COMMIT' );
François Jacquet's avatar
François Jacquet committed
90

François Jacquet's avatar
François Jacquet committed
91
		unset( $_SESSION['FSA_sale'] );
François Jacquet's avatar
François Jacquet committed
92 93 94

		// Unset modfunc & redirect URL.
		RedirectURL( 'modfunc' );
François Jacquet's avatar
François Jacquet committed
95 96
	}
	else
François Jacquet's avatar
François Jacquet committed
97 98 99 100 101 102 103
	{
		// Unset modfunc & redirect URL.
		RedirectURL( 'modfunc' );
	}

	// Unset submit & redirect URL.
	RedirectURL( 'submit' );
François Jacquet's avatar
François Jacquet committed
104 105
}

François Jacquet's avatar
François Jacquet committed
106
if ( UserStudentID() && ! $_REQUEST['modfunc'] )
François Jacquet's avatar
François Jacquet committed
107
{
108
	$student = DBGet( "SELECT s.STUDENT_ID," . DisplayNameSQL( 's' ) . " AS FULL_NAME,
109
	fsa.ACCOUNT_ID,fsa.STATUS,fsa.DISCOUNT,fsa.BARCODE,
110 111
	(SELECT BALANCE FROM FOOD_SERVICE_ACCOUNTS WHERE ACCOUNT_ID=fsa.ACCOUNT_ID) AS BALANCE
	FROM STUDENTS s,FOOD_SERVICE_STUDENT_ACCOUNTS fsa
112
	WHERE s.STUDENT_ID='" . UserStudentID() . "'
113
	AND fsa.STUDENT_ID=s.STUDENT_ID" );
114

François Jacquet's avatar
François Jacquet committed
115 116
	$student = $student[1];

117
	echo '<form action="' . URLEscape( 'Modules.php?modname=' . $_REQUEST['modname'] . '&modfunc=submit&menu_id=' . $_REQUEST['menu_id']  ) . '" method="POST">';
118 119 120

	DrawHeader(
		'',
121
		SubmitButton( _( 'Cancel Sale' ), 'submit[cancel]', '' ) . // No .primary button class.
122 123 124
		SubmitButton( _( 'Complete Sale' ), 'submit[save]' )
	);

François Jacquet's avatar
François Jacquet committed
125
	echo '</form>';
François Jacquet's avatar
François Jacquet committed
126

127 128 129 130
	DrawHeader(
		NoInput( $student['FULL_NAME'], $student['STUDENT_ID'] ),
		NoInput( red( $student['BALANCE'] ), _( 'Balance' ) )
	);
François Jacquet's avatar
François Jacquet committed
131

François Jacquet's avatar
François Jacquet committed
132
	if ( $student['BALANCE'] != '' )
François Jacquet's avatar
François Jacquet committed
133
	{
François Jacquet's avatar
François Jacquet committed
134 135
		echo '<table class="width-100p">';
		echo '<tr class="st"><td class="width-100p valign-top">';
François Jacquet's avatar
François Jacquet committed
136

François Jacquet's avatar
François Jacquet committed
137 138 139 140 141 142 143 144
		$RET = DBGet( 'SELECT fsti.DESCRIPTION,fsti.AMOUNT
			FROM FOOD_SERVICE_TRANSACTIONS fst,FOOD_SERVICE_TRANSACTION_ITEMS fsti
			WHERE fst.ACCOUNT_ID=\'' . $student['ACCOUNT_ID'] . '\'
			AND fst.STUDENT_ID=\'' . UserStudentID() . '\'
			AND fst.SYEAR=\'' . UserSyear() . '\'
			AND fst.SHORT_NAME=\'' . $menus_RET[$_REQUEST['menu_id']][1]['TITLE'] . '\'
			AND fst.TIMESTAMP BETWEEN CURRENT_DATE AND \'tomorrow\'
			AND fsti.TRANSACTION_ID=fst.TRANSACTION_ID' );
François Jacquet's avatar
François Jacquet committed
145

François Jacquet's avatar
François Jacquet committed
146
		$columns = array( 'DESCRIPTION' => _( 'Item' ), 'AMOUNT' => _( 'Amount' ) );
François Jacquet's avatar
François Jacquet committed
147

François Jacquet's avatar
François Jacquet committed
148
		$singular = sprintf( _( 'Earlier %s Sale' ), $menus_RET[$_REQUEST['menu_id']][1]['TITLE'] );
François Jacquet's avatar
François Jacquet committed
149

François Jacquet's avatar
François Jacquet committed
150
		$plural = sprintf( _( 'Earlier %s Sales' ), $menus_RET[$_REQUEST['menu_id']][1]['TITLE'] );
François Jacquet's avatar
François Jacquet committed
151 152

		ListOutput( $RET, $columns, $singular, $plural, array(), false, array( 'save' => false, 'search' => false ) );
François Jacquet's avatar
François Jacquet committed
153 154

		// IMAGE
François Jacquet's avatar
François Jacquet committed
155 156
		//FJ fix error Warning: fclose() expects parameter 1 to be resource, boolean given

François Jacquet's avatar
François Jacquet committed
157 158
		if ( file_exists( $picture = $StudentPicturesPath . UserSyear() . '/' . UserStudentID() . '.jpg' )
			|| file_exists( $picture = $StudentPicturesPath . ( UserSyear() - 1 ) . '/' . UserStudentID() . '.jpg' ) )
François Jacquet's avatar
François Jacquet committed
159 160 161
		{
			echo '</td><td rowspan="2"><img src="' . $picture . '" width="150" />';
		}
François Jacquet's avatar
François Jacquet committed
162

François Jacquet's avatar
François Jacquet committed
163 164
		echo '</td></tr>';
		echo '<tr><td class="width-100p valign-top">';
François Jacquet's avatar
François Jacquet committed
165

166
		$items_RET = DBGet( "SELECT fsi.SHORT_NAME,fsi.DESCRIPTION,fsi.PRICE,fsi.PRICE_REDUCED,fsi.PRICE_FREE,fsi.ICON
167
		FROM FOOD_SERVICE_ITEMS fsi,FOOD_SERVICE_MENU_ITEMS fsmi
François Jacquet's avatar
François Jacquet committed
168
		WHERE fsmi.MENU_ID='" . $_REQUEST['menu_id'] . "'
169 170
		AND fsi.ITEM_ID=fsmi.ITEM_ID
		AND fsmi.CATEGORY_ID IS NOT NULL
François Jacquet's avatar
François Jacquet committed
171 172
		AND fsi.SCHOOL_ID='" . UserSchool() . "'
		ORDER BY fsi.SORT_ORDER", array( 'ICON' => 'makeIcon' ), array( 'SHORT_NAME' ) );
François Jacquet's avatar
François Jacquet committed
173 174
		$items = array();

François Jacquet's avatar
François Jacquet committed
175 176 177 178 179 180
		foreach ( (array) $items_RET as $sn => $item )
		{
			$items += array( $sn => $item[1]['DESCRIPTION'] );
		}

		$LO_ret = array( array() );
François Jacquet's avatar
François Jacquet committed
181
//FJ fix error Warning: Invalid argument supplied for foreach()
François Jacquet's avatar
François Jacquet committed
182 183

		if ( isset( $_SESSION['FSA_sale'] ) && is_array( $_SESSION['FSA_sale'] ) )
François Jacquet's avatar
François Jacquet committed
184
		{
François Jacquet's avatar
François Jacquet committed
185
			foreach ( (array) $_SESSION['FSA_sale'] as $id => $item_sn )
François Jacquet's avatar
François Jacquet committed
186 187
			{
				// determine price based on discount
François Jacquet's avatar
François Jacquet committed
188 189 190 191 192 193 194 195 196 197
				$price = $items_RET[$item_sn][1]['PRICE'];

				if ( $student['DISCOUNT'] == 'Reduced' )
				{
					if ( $items_RET[$item_sn][1]['PRICE_REDUCED'] != '' )
					{
						$price = $items_RET[$item_sn][1]['PRICE_REDUCED'];
					}
				}
				elseif ( $student['DISCOUNT'] == 'Free' )
François Jacquet's avatar
François Jacquet committed
198
				{
François Jacquet's avatar
François Jacquet committed
199 200 201 202
					if ( $items_RET[$item_sn][1]['PRICE_FREE'] != '' )
					{
						$price = $items_RET[$item_sn][1]['PRICE_FREE'];
					}
François Jacquet's avatar
François Jacquet committed
203
				}
François Jacquet's avatar
François Jacquet committed
204 205

				$LO_ret[] = array( 'SALE_ID' => $id, 'PRICE' => $price, 'DESCRIPTION' => $items_RET[$item_sn][1]['DESCRIPTION'], 'ICON' => $items_RET[$item_sn][1]['ICON'] );
François Jacquet's avatar
François Jacquet committed
206 207 208
			}
		}

François Jacquet's avatar
François Jacquet committed
209 210 211 212
		unset( $LO_ret[0] );

		$link['remove'] = array( 'link' => 'Modules.php?modname=' . $_REQUEST['modname'] . '&modfunc=remove&menu_id=' . $_REQUEST['menu_id'],
			'variables' => array( 'id' => 'SALE_ID' ) );
François Jacquet's avatar
François Jacquet committed
213
//FJ css WPadmin
François Jacquet's avatar
François Jacquet committed
214
		//		$link['add']['html'] = array('DESCRIPTION' => '<table class="cellspacing-0"><tr><td>'.SelectInput('','item_sn','',$items).'</td></tr></table>','ICON' => '<table class="cellspacing-0"><tr><td><input type=submit value='._('Add').'></td></tr></table>','remove'=>button('add'));
François Jacquet's avatar
François Jacquet committed
215 216 217 218 219 220 221
		$link['add']['html'] = array(
			'DESCRIPTION' => SelectInput( '', 'item_sn', '', $items ),
			'ICON' => SubmitButton( _( 'Add' ) ),
			'PRICE' => '&nbsp;',
			'remove' => button( 'add' ),
		);

François Jacquet's avatar
François Jacquet committed
222
		$columns = array( 'DESCRIPTION' => _( 'Item' ), 'ICON' => _( 'Icon' ), 'PRICE' => _( 'Price' ) );
François Jacquet's avatar
François Jacquet committed
223 224 225

		$tabs = array();

François Jacquet's avatar
François Jacquet committed
226 227 228 229 230
		foreach ( (array) $menus_RET as $id => $menu )
		{
			$tabs[] = array( 'title' => $menu[1]['TITLE'], 'link' => 'Modules.php?modname=' . $_REQUEST['modname'] . '&menu_id=' . $id );
		}

François Jacquet's avatar
François Jacquet committed
231 232 233 234 235
		$extra = array(
			'save' => false,
			'search' => false,
			'header' => WrapTabs( $tabs, 'Modules.php?modname=' . $_REQUEST['modname'] . '&menu_id=' . $_REQUEST['menu_id'] ),
		);
François Jacquet's avatar
François Jacquet committed
236

François Jacquet's avatar
François Jacquet committed
237
		echo '<br />';
238
		echo '<form action="' . URLEscape( 'Modules.php?modname=' . $_REQUEST['modname'] . '&modfunc=add&menu_id=' . $_REQUEST['menu_id']  ) . '" method="POST">';
François Jacquet's avatar
François Jacquet committed
239

François Jacquet's avatar
François Jacquet committed
240
		ListOutput( $LO_ret, $columns, 'Item', 'Items', $link, array(), $extra );
François Jacquet's avatar
François Jacquet committed
241

François Jacquet's avatar
François Jacquet committed
242
		echo '</form>';
François Jacquet's avatar
François Jacquet committed
243

François Jacquet's avatar
François Jacquet committed
244
		echo '</td></tr></table>';
François Jacquet's avatar
François Jacquet committed
245 246
	}
	else
François Jacquet's avatar
François Jacquet committed
247 248 249
	{
		ErrorMessage( array( _( 'This student does not have a valid Meal Account.' ) ), 'fatal' );
	}
François Jacquet's avatar
François Jacquet committed
250
}