File: booking.php

package info (click to toggle)
fibusql 0.4.1-3
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 376 kB
  • ctags: 391
  • sloc: php: 1,830; sh: 45; makefile: 35
file content (337 lines) | stat: -rw-r--r-- 12,633 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
<?php
/*
    FibuSQL 0.4.1  -  (c) 2003 Martin Pitt <martin@piware.de>

    This software is protected by the GNU General Public License (see
    file COPYING).
*/

include_once 'backend/base.inc';
include_once 'backend/invalbal.inc';
include get_lang_inc( '-booking' );

########################################
# validate and copy generic arguments
########################################
$id = get_http_int( 'id', 1 );
$repid = get_http_int( 'repid', 1 );
$repDays = get_http_int( 'repDays', 1 );
$repMonths = get_http_int( 'repMonths', 1 );
$repeat = $HTTP_GET_VARS['repeat'];

########################################
# action 'delete from journal'
########################################
if( $HTTP_GET_VARS['delete'] && !is_null( $id ) ) {
    # invalidate debit and credit acc 
    $accs = $db->getRow( 'select debit_acc, credit_acc from journal where id='.$id );
    if( !check_res_delay( $accs, $LANG_ERR_readj ) ) {
	invalidate_balance( $accs[0] );
	invalidate_balance( $accs[1] );

	# forward to journal
	if( !check_res_delay( $db->query( 'delete from journal where id=' . $id), 
			      $LANG_err_delj ) ) {
	    header( 'Location: journal.php' );
	    exit;
	}
    }
}

########################################
# action 'delete from repeated'
########################################
if( $HTTP_GET_VARS['delete'] && !is_null( $repid ) ) {
    # forward to repeated
    if( !check_res_delay( $db->query( 'delete from repeated where id=' . $repid), 
			      $LANG_err_delrep ) ) {
	header( 'Location: repeated.php' );
	exit;
    }
}

########################################
# if actions are given, extract variables and check them for validity
########################################
if( $HTTP_GET_VARS['add'] || $HTTP_GET_VARS['change'] ) {
    $debit_acc = get_http_int( 'debit_acc', 1, $LANG_err_debacc );
    $credit_acc = get_http_int( 'credit_acc', 1, $LANG_err_credacc );
    $value = get_http_formatfloat( 'value', 1, $LANG_err_value );

    if( !$HTTP_GET_VARS['receipt'] )
	$receipt = null;
    else 
	$receipt = get_http_int( 'receipt', 1, $LANG_err_receipt );

    if( $HTTP_GET_VARS['description'] )
	# unslash and escape quotes
	$description = str_replace( "'", "''", stripslashes( $HTTP_GET_VARS['description'] ) );	
    else
	$fail = $LANG_err_emptydesc;

    $time = $HTTP_GET_VARS['time' ];
    if( !preg_match( '/^\s*\d\d\d\d-\d\d-\d\d\s*$/', $time ) &&
	!preg_match( '/^\s*\d\d\d\d-\d\d-\d\d\s+\d\d?:\d\d\s*$/', $time ) )
	$fail = $LANG_err_date;
}

########################################
# action 'insert new entry into journal'
########################################
if( $HTTP_GET_VARS['add'] && !$fail && is_null( $id ) && is_null( $repid ) ) {
    if( $HTTP_GET_VARS['repeat'] == 'once' ) {
	$newid = $db->getOne( 'select max(id) from journal' ) + 1;
	if( !check_res_delay( $newid, $LANG_err_maxidj ) ) 
	    $res = $db->query( "insert into journal values( $newid, " .
			       $debit_acc . ", " . $credit_acc . ", " . $value . ", "
			       . ( $receipt ? $receipt : 'null' ). ", '" . $description
			       . "', '" . $time . "')" );
    }
    else if( $HTTP_GET_VARS['repeat'] == 'daily' ) {
	if( is_numeric( $HTTP_GET_VARS['repDays'] ) && $HTTP_GET_VARS['repDays'] > 0 ) {
	    $repDays = intval( $HTTP_GET_VARS['repDays'] );
	    $newid = $db->getOne( 'select max(id) from repeated' ) + 1;
	    if( !check_res_delay( $newid, $LANG_err_maxidr ) ) 
		$res = $db->query( "insert into repeated values( $newid, " .
				     $debit_acc . ", " . $credit_acc . ", " .
				     $value . ", " . ( $receipt ? $receipt : 'null' ) . ", '" .
				     $description . "', '" . $time . "', " . 
				     $repDays . ", null)" );
	} else
	    $fail = $LANG_err_dayrep;
    }
    else if( $HTTP_GET_VARS['repeat'] == 'monthly' ) {
	if( is_numeric( $HTTP_GET_VARS['repMonths'] ) && $HTTP_GET_VARS['repMonths'] > 0 ) {
	    $repMonths = intval( $HTTP_GET_VARS['repMonths'] );
	    $newid = $db->getOne( 'select max(id) from repeated' ) + 1;
	    if( !check_res_delay( $newid, $LANG_err_maxidr ) ) 
		$res = $db->query( "insert into repeated values( $newid, " .
				     $debit_acc . ', ' . $credit_acc . ', ' .
				     $value . ', ' . ( $receipt ? $receipt : 'null' ) . ", '" .
				     $description . "', '" . $time . 
				     "', null, " . $repMonths . ")" );
	} else
	    $fail = $LANG_err_monthrep;
    }
    else
	$fail = $LANG_err_repmode;
    
    if( !$fail && !check_res_delay( $res, $LANG_err_insertj ) ) {
	invalidate_balance( $debit_acc );
	invalidate_balance( $credit_acc );
	header( 'Location: journal.php' );
	exit;
    }
}

########################################
# action 'change journal entry'
########################################
if( $HTTP_GET_VARS['change'] && !$fail && !is_null( $id ) && is_null( $repid ) ) {
    # invalidate debit and credit acc
    $accs = $db->getRow( 'select debit_acc, credit_acc from journal where id='.$id );
    if( !check_res_delay( $accs, $LANG_ERR_readj ) ) {
	invalidate_balance( $accs[0] );
	invalidate_balance( $accs[1] );
	invalidate_balance( $debit_acc );
	invalidate_balance( $credit_acc );

	$q = 'update journal set debit_acc=' . $debit_acc . ', credit_acc='
	    . $credit_acc . ', value=' . $value . ', receipt=' . ( $receipt ? $receipt : 'null' )
	    . ", description='" . $description . "', time='" . $time
	    . "'" . ' where id=' . $id;

	if( !check_res_delay( $db->query( $q ), $LANG_err_updatej ) ) {
	    header( 'Location: journal.php' );
	    exit;
	}
    }
}

########################################
# action 'change repeated entry'
########################################
if( $HTTP_GET_VARS['change'] && !$fail && is_null( $id ) && !is_null( $repid ) ) {
    if( $repeat == 'daily' ) {
	$repMonths = null;
	if( $repDays <= 0 ) {
	    $fail = $LANG_err_dayrep;
	    $repDays = null;
	}
    } else if( $repeat == 'monthly' ) {
	$repDays = null;
	if( $repMonths <= 0 ) {
	    $fail = $LANG_err_monthrep;
	    $repMonths = null;
	}
    } else
	$fail = $LANG_err_repmode;

    if( !$fail ) {
	$q = 'update repeated set debit_acc=' . $debit_acc . ', credit_acc='
	    . $credit_acc . ', value=' . $value . ', receipt=' . ( $receipt ? $receipt : 'null' )
	    . ", description='" . $description . "', nextdue='" .  $time . "', repdays="
	    . ( $repDays ? $repDays : 'null' ) . ', repmonths=' . ( $repMonths ? $repMonths : 'null' )
	    . ' where id=' . $repid;

	if( !check_res_delay( $db->query( $q ), $LANG_err_updaterep ) ) {
	    header( 'Location: repeated.php' );
	    exit;
	}
    }
}

########################################
# page header and $fail display
########################################
include 'pagehead-html.inc';

if( $fail )
    echo "<p class=\"error\">$LANG_error: $fail</p>\n";

########################################
# set field default contents (from GET vars or current db values)
########################################
if( $id ) { # came from journal 
    $r = $db->getRow( 'select debit_acc, credit_acc, value, receipt, description, '
		    . 'extract(YEAR from time), extract(MONTH from time), extract(DAY from time),'
		    . 'extract(HOUR from time), extract(MINUTE from time) from journal where id='.$id );
    check_res( $r, $LANG_err_readj );
    if( !$debit_acc ) $debit_acc = $r[0];
    if( !$credit_acc ) $credit_acc = $r[1];
    if( !$value ) $value = $r[2];
    if( !$receipt ) $receipt = $r[3];
    if( !$description ) $description = $r[4];
    if( !$time ) $time = sprintf( '%d-%02d-%02d %02d:%02d', $r[5], $r[6], $r[7], $r[8], $r[9] );
} 
else if( $repid ) { # came from repeated
    $r = $db->getRow( 'select debit_acc, credit_acc, value, receipt, description, '
		    . 'extract(YEAR from nextDue), extract(MONTH from nextDue), extract(DAY from nextDue),'
		    . 'extract(HOUR from nextDue), extract(MINUTE from nextDue),'
		    . 'repdays, repmonths from repeated where id='.$repid );
    check_res( $r, $LANG_err_readrep );

    if( !$debit_acc ) $debit_acc = $r[0];
    if( !$credit_acc ) $credit_acc = $r[1];
    if( !$value ) $value = $r[2];
    if( !$receipt ) $receipt = $r[3];
    if( !$description ) $description = $r[4];
    if( !$time ) $time = sprintf( '%d-%02d-%02d %02d:%02d', $r[5], $r[6], $r[7], $r[8], $r[9] );
    if( !$repDays ) $repDays = $r[10];
    if( !$repMonths ) $repMonths = $r[11];
    if( !$repeat ) $repeat = $repDays ? 'daily' : 'monthly';
}
else if( !$time ) { # new entry, set current time
    $d = $db->getRow( 'select extract(YEAR from CURRENT_TIMESTAMP), extract(MONTH from CURRENT_TIMESTAMP),'
	. 'extract(DAY from CURRENT_TIMESTAMP), extract(HOUR from CURRENT_TIMESTAMP), '
	. 'extract(MINUTE from CURRENT_TIMESTAMP)' );
    check_res( $d, $LANG_err_extracttime );
    $time = sprintf( '%d-%02d-%02d %02d:%02d', $d[0], $d[1], $d[2], $d[3], $d[4] );
}

########################################
# build form
########################################
$accounts = $db->getAll( 'select account, name from accounts where account <> 0 order by account' );
check_res( $accounts, $LANG_err_readacc );
?>

  <h2><?php echo $id ? $LANG_changebooking : ( $repid ? $LANG_changerep : $LANG_menu_new ); ?></h2>

  <form method="get" action="<?php echo $HTTP_SERVER_VARS['PHP_SELF'] ?>">

     <table>
        <tr><td><?php echo $LANG_debitacc ?>:</td>
          <td>
            <select name="debit_acc">
<?php
for( $i = 0; $i < count( $accounts ); ++$i ) {
    echo '<option value="' . $accounts[$i][0] . '"';
    if( $debit_acc == $accounts[$i][0] )
        echo ' selected="selected"';
    echo '>' . $accounts[$i][0] . " - " . htmlq( $accounts[$i][1] ) . "</option>\n";
}
?>
            </select>
          </td>
        </tr>
        <tr><td><?php echo $LANG_creditacc ?>:</td>
          <td>
            <select name="credit_acc">
<?php
for( $i = 0; $i < count( $accounts ); ++$i ) {
    echo '<option value="' . $accounts[$i][0] . '"';
    if( $credit_acc == $accounts[$i][0] )
        echo ' selected="selected"';
    echo '>' . $accounts[$i][0] . " - " . htmlq( $accounts[$i][1] ) . "</option>\n";
}
?>
            </select>
          </td>
        </tr>
        <tr><td><?php echo $LANG_value ?>:</td>
          <td><input type="text" size="15" maxlength="15" name="value"
		value="<?php echo $value ? formatval( $value ) : '0'; ?>" /></td>
        </tr>
        <tr><td colspan="2">&nbsp;</td></tr>
        <tr>
          <td><?php echo $repid ? $LANG_nextdue : $LANG_time ?>:</td>
          <td><input type="text" size="23" maxlength="23" name="time"
		value="<?php echo $time; ?>" /></td>
        </tr>
        <tr>
          <td><?php echo $LANG_receipt ?>:</td>
          <td><input type="text" size="7" maxlength="7" name="receipt"
		value="<?php if( $receipt ) echo $receipt; ?>" /></td>
        </tr>
        <tr>
          <td><?php echo $LANG_desc ?>:</td>
          <td><input type="text" size="50" maxlength="255" name="description"
		value="<?php if( $description ) echo $description; ?>" /></td>
        </tr>
       
        <tr><td colspan="2">&nbsp;</td></tr>

	<?php if( is_null( $id ) ) { ?>
        <tr>
	  <td valign="top"><?php echo $LANG_repetition ?>:</td>
          <td>
            <input type="radio" name="repeat" value="once" 
	      <?php if( !$repeat || $repeat == 'once' )
	        echo 'checked="checked"'; ?> /><?php echo $LANG_unique ?><br />

            <input type="radio" name="repeat" value="daily" 
	      <?php if( $repeat == 'daily' ) echo 'checked="checked"' ?> />
	      <?php echo $LANG_every ?>
	    <input type="text" name="repDays" value="<?php 
	      if( $repDays ) echo $repDays; else echo '1' ?>" size="3"
	      maxlength="2" /> <?php echo $LANG_days ?><br />

            <input type="radio" name="repeat" value="monthly" 
	       <?php if( $repeat == 'monthly' ) echo 'checked="checked"' ?>/>
	       <?php echo $LANG_every ?>
	    <input type="text" name="repMonths" value="<?php
	      if( $repMonths ) echo $repMonths; else echo '1' ?>"
	       size="3" maxlength="2" /> <?php echo $LANG_months ?>
          </td>
        </tr>
	<?php } ?>
     </table>

     <p><?php 
     if( $id ) echo "<input type=\"hidden\" name=\"id\" value=\"$id\" />\n";
     if( $repid ) echo "<input type=\"hidden\" name=\"repid\" value=\"$repid\" />\n"; 
     ?> </p>

     <p>
     <?php if( is_null( $id ) && is_null( $repid ) ) { ?>
     <input type="submit" name="add" value="<?php echo $LANG_book ?>" />
     <?php } else { ?>
     <input type="submit" name="change" value="<?php echo $LANG_change ?>" />
     <input type="submit" name="delete" value="<?php echo $LANG_delete ?>" />
     <?php } ?>
     <input type="reset" value="<?php echo $LANG_reset ?>" />
     </p>
  </form>
</body>
</html>