[r477]: finesScripts / trunk / createFine.php  Maximize  Restore  History

Download this file

446 lines (374 with data), 17.1 kB

  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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
#!/usr/bin/php
<?php
$debug = false; //In debug mode, everything runs as normal, but the fine is not committed to the database.
$maxFine = 10;
if ( ! $debug ) ob_start();
//Start Timer
$timeparts = explode(' ',microtime());
$thetime = $timeparts[1].substr($timeparts[0],1);
#ini_set( "memory_limit", "256M" );
## Limiting maximum script execuation time to 1 second
## If the script takes more than 1 second to execute,
## it is probably a runaway process that must be killed.
ini_set( "max_execution_time", "5" );
//Create our database handle
require_once("classes/MySQLConnectionFactory.class.php");
$dbh = MySQLConnectionFactory::create();
$log = fopen( '/var/log.fine', 'a' );
if ( $log ) {
fwrite ( $log, "\n################################\n" );
fwrite( $log, date(DATE_RFC822) . "\n" );
}
$isRenewal = false;
if ( isset( $argv[2] ) ) {
$barcode = getBarcodeByItemNumber( $argv[2], $dbh );
$isRenewal = true;
fwrite( $log, "This is a renewal, recieved an itemnumber instead of a barcode.\nRecieved itemnumber: " . $argv[2]
. "\nRetrieving barcode: " . $barcode . "\n" );
} else {
//Grab the incoming barcode
$barcode = $argv[1];
}
#if ( ! $barcode ) $barcode = $_REQUEST['barcode'];
#if ( ! $barcode ) {
# fwrite( $log, "NO BARCODE GIVEN\n" );
# die();
#}
fwrite( $log , "Working On Barcode: " . $barcode . "\n\n");
$fineData = getFineData( $barcode, $dbh, $log );
$issuingRule = getIssuingRule( $fineData, $issuingRule, $dbh, $log );
if ( $daysOverdue = isOverdue( $fineData, $issuingRule, $dbh, $log ) ) {
createFine( $fineData, $issuingRule, $daysOverdue, $maxFine, $dbh, $log, $isRenewal );
}
//End Timer
$timeparts = explode(' ',microtime());
$starttime = $timeparts[1].substr($timeparts[0],1);
$timeparts = explode(' ',microtime());
$endtime = $timeparts[1].substr($timeparts[0],1);
fwrite( $log, "Time To Run: " . bcsub($endtime,$starttime,6) . " Seconds\n" );
fclose( $log );
if ( ! $debug ) ob_end_clean();
//********************** Helper Functions ************************************//
// Function getFineData
// This function returns an array associated array of data about the borrower and the item
// Input:
// $barcode : The barcode of the relevent item
// $dbh : The database handle
// $log : The log file handle
// Output: array
function getFineData( $barcode, $dbh, $log ) {
fwrite( $log, "Entering getFineDat\n" );
$sql = 'SELECT items.itemnumber, items.itemcallnumber, issues.date_due, DATEDIFF( NOW(), issues.date_due ) as daysOverdue, borrowers.borrowernumber, borrowers.categorycode, issues.date_due, biblioitems.itemtype, items.price, items.barcode, items.holdingbranch, biblio.title'
. ' FROM items, issues, borrowers, biblio, biblioitems'
. ' WHERE'
. ' items.barcode = "' . mysql_real_escape_string( $barcode ) . '"'
. ' AND issues.itemnumber = items.itemnumber'
. ' AND borrowers.borrowernumber = issues.borrowernumber'
. ' AND items.biblionumber = biblioitems.biblionumber'
. ' AND items.biblionumber = biblio.biblionumber'
. ' AND issues.returndate IS NULL';
$results = mysql_query( $sql );
if ( ! $results ) {
if ( $log ) fwrite( $log, "\nA fatal MySQL error orccured\n>Query:\n" . $sql . "\nError: (" . mysql_errno() . ") " . mysql_error() . "\n");
die();
}
$row = mysql_fetch_assoc( $results );
if ( ! $row['barcode'] && $log ) {
if ( $log ) fwrite( $log, "No match found in database for SQL query:\n" . $sql . "\n" );
die();
}
if ( $log ) fwrite( $log, "getFineData Results:\n" . print_r( $row, true ) . "\n\n" );
return $row;
}
// Function getIssuingRule
// This function returns an associated array of data about the issuing rule for the item and borrower combination
// Input:
// $fineData : The output of getFineData()
// $dbh : The database handle
// $log : The log file handle
// Output: array
function getIssuingRule( $fineData, $dbh, $log ) {
//First, try to get the issuingrule that matches both the itemtype and categorycode
$sql = "SELECT * FROM issuingrules WHERE categorycode LIKE '" . $fineData['categorycode'] . "' AND itemtype LIKE '" . $fineData['itemtype'] . "' AND branchcode LIKE '" . $fineData['holdingbranch'] . "'";
$results = mysql_query( $sql );
if ( ! $results && $log ) {
fwrite( $log, "\nA fatal MySQL error orccured\n>Query:\n" . $sql . "\nError: (" . mysql_errno() . ") " . mysql_error() . "\n");
die();
} else {
fwrite( $log, "Found rule by categorycode and itemtype\n" );
}
//Nested if. Yes, it's ugly, but it's also faster.
if ( ! $results ) { //Try with itemtype as wildcard, we want categorycode to take precesdence
$sql = "SELECT * FROM issuingrules WHERE categorycode LIKE '" . $fineData['categorycode'] . "' AND itemtype LIKE '*' AND branchcode LIKE '" . $fineData['holdingbranch'] . "'";;
$results = mysql_query( $sql );
if ( ! $results ) {
fwrite( $log, "\nA fatal MySQL error orccured\n>Query:\n" . $sql . "\nError: (" . mysql_errno() . ") " . mysql_error() . "\n");
die();
} else {
fwrite( $log, "Found rule by categorycode\n" );
}
if ( ! $results ) { //Try with categorycode as wildcard
$sql = "SELECT * FROM issuingrules WHERE categorycode LIKE '*' AND itemtype LIKE '" . $fineData['itemtype'] . "' AND branchcode LIKE '" . $fineData['holdingbranch'] . "'";;
$results = mysql_query( $sql );
if ( ! $results ) {
fwrite( $log, "\nA fatal MySQL error orccured\n>Query:\n" . $sql . "\nError: (" . mysql_errno() . ") " . mysql_error() . "\n");
die();
} else {
fwrite( $log, "Found rule by itemtype\n" );
}
if ( ! $results ) { //Grab the default issuingrule
$sql = "SELECT * FROM issuingrules WHERE categorycode LIKE '*' AND itemtype LIKE '*' AND branchcode LIKE '" . $fineData['holdingbranch'] . "'";;
$results = mysql_query( $sql );
if ( ! $results ) {
fwrite( $log, "\nA fatal MySQL error orccured\n>Query:\n" . $sql . "\nError: (" . mysql_errno() . ") " . mysql_error() . "\n");
die();
} else {
fwrite( $log, "Found default rule\n" );
}
}
}
}
$row = mysql_fetch_assoc( $results );
if ( $log ) fwrite( $log, "getIssuingRule Results:\n" . print_r( $row, true ) . "\n" );
return $row;
}
// Function isOverdue
// If the item is past the overdue grace period,
// returns the number of days which have fines, otherwise, returns false.
// Input:
// $fineData : output of getFineData()
// $issuingrule : The issuing rule that applies to this item
// $dbh : The database handle
// $log : The log file handle
// Output: int Number of days where fines apply, or false
function isOverdue( $fineData, $issuingRule, $dbh, $log ) {
$numberOfHolidays = getNumberOfHolidays( $fineData, $issuingRule, $dbh, $log );
$daysPastGracePeriod = $fineData['daysOverdue'] - $issuingRule['firstremind'];
$daysOverdue = $daysPastGracePeriod - $numberOfHolidays;
if ( $log ) {
fwrite( $log, "Days Overdue: " . $fineData['daysOverdue'] . "\n" );
fwrite( $log, "Grace Period: " . $issuingRule['firstremind'] . "\n" );
fwrite( $log, "Days Past Grace: $daysPastGracePeriod \n" );
fwrite( $log, "Holidays: " . $numberOfHolidays . "\n" );
fwrite( $log, "Days Overdue: " . $daysOverdue . "\n" );
}
if ( $daysOverdue > 0 ) {
return $daysOverdue;
} else {
return false;
}
}
// Function createFine
// Takes the input and creates the correct fine in the database.
// Input:
// $fineData : Output of getFineData()
// $issuingrule : The issuing rule that applies to this item
// $daysOverdue : Number of days where fines are charged
// $maxFine : The maximum fine the library system allows
// $dbh : The database handle
// $log : The log file handle
// Output: None
function createFine( $fineData, $issuingRule, $daysOverdue, $maxFine, $dbh, $log, $isRenewal ) {
//Get the next accountlines.accountno to be used.
$sql = "SELECT MAX(accountno) AS lastAccountNo FROM accountlines";
$result = mysql_query( $sql );
if ( ! $result && $log ) {
fwrite( $log, "\nA fatal MySQL error orccured\n>Query:\n" . $sql . "\nError: (" . mysql_errno() . ") " . mysql_error() . "\n");
die();
}
$row = mysql_fetch_array( $result );
$nextAccountNo = $row['lastAccountNo'] + 1;
// Calculate the total amount of the fine.
$fineAmount = floor( $daysOverdue / $issuingRule['chargeperiod'] ) * $issuingRule['fine'];
// If the fine exceeds the maximum fine, lower it to the maximum fine
if ( $fineAmount > $maxFine ) {
fwrite( $log, "Fine Exceeds Maximum Fine, Lowering Fine From " . $fineAmount . " to " . $maxFine . "\n" );
$fineAmount = $maxFine;
}
// If the fine exceeds the price of the item, lower it to the price of the item
if ( $fineData['price'] && $fineAmount > $fineData['price'] ) {
fwrite( $log, "Fine Exceeds Item Price, Lowering Fine From " . $fineAmount . " to " . $fineData['price'] . "\n" );
$fineAmount = $fineData['price'];
}
$returnOrRenew = 'Returned';
if ( $isRenewal ) $returnOrRenew = 'Renewed';
$today = date('Y-m-d');
$description = $fineData['title'] . ' ( ' . $fineData['itemcallnumber'] . ' / '. $fineData['barcode'] . ' / ' . $fineData['holdingbranch'] . ' ) Due: ' . $fineData['date_due'] . ' ' . $returnOrRenew . ': ' . $today;
$sql = 'INSERT INTO accountlines ( '
. ' borrowernumber, '
. ' accountno, '
. ' itemnumber, '
. ' date, '
. ' amount, '
. ' description, '
. ' dispute, '
. ' accounttype, '
. ' amountoutstanding, '
. ' timestamp '
. ' ) VALUES ('
. ' \'' . mysql_real_escape_string( $fineData['borrowernumber'] ) . '\', '
. ' \'' . mysql_real_escape_string( $nextAccountNo ) . '\', '
. ' \'' . mysql_real_escape_string( $fineData['itemnumber'] ) . '\', '
. ' NOW(), '
. ' \'' . mysql_real_escape_string( $fineAmount ) . '\', '
. ' \'' . mysql_real_escape_string( $description ) . '\', '
. ' NULL,'
. ' \'F\','
. ' \'' . mysql_real_escape_string( $fineAmount ) . '\','
. ' NOW( )'
. ' );';
if ( ! $GLOBALS['debug'] ) {
$result = mysql_query( $sql );
if ( ! $result && $log ) {
fwrite( $log, "\nA fatal MySQL error orccured\n>Query:\n" . $sql . "\nError: (" . mysql_errno() . ") " . mysql_error() . "\n");
die();
} else {
if ( $log ) fwrite( $log, "Insert Successfull\n" . "Insert Accountline SQL:\n" . $sql . "\n" );
}
} else {
if ( $log ) {
fwrite( $log, "Running In Debug Mode, SQL Query Not Executed\n" );
fwrite( $log, "SQL For Accountline:\n" . $sql . "\n" );
}
}
}
// Function getNumberOfHolidays
// Finds all the holidays that fall between the date at which fines begin to accrue
// and the day when it was checked in.
// Input:
// $fineData : Output of getFineData()
// $issuingrule : The issuing rule that applies to this item
// $dbh : The database handle
// $log : The log file handle
// Output:
function getNumberOfHolidays( $fineData, $issuingRule, $dbh, $log ){
$numberOfHolidays = 0;
$todayTimestamp = strtotime("now");
//Get the date from which the overdue item began accruing fines.
$startChargingTimestamp = strtotime( $fineData['date_due'] . ' + ' . $issuingRule['firstremind'] . ' days' );
if ( $log ) {
fwrite( $log, "Calculating Holidays\n" );
fwrite( $log, "Date Due: " . $fineData['date_due'] . "\n" );
fwrite( $log, "Grace Days: " . $issuingRule['firstremind'] . "\n" );
fwrite( $log, "Date To Begin Charging: " . timestampToDate( $startChargingTimestamp ) . "\n" );
fwrite( $log, "Blorg: " . timestampToDate( dateToTimestamp( '1981-06-10' ) ) . "\n" );
}
//Add the relevant 'special' (once only) holidays to $numberOfHolidays
$holidays = getSpecialHolidays( $dbh, $branchcode = $issuingRule['branchcode'] );
foreach ( $holidays as $holiday ) {
$year = $holiday['year'];
$month = ( $holiday['month'] > 9 ) ? $holiday['month'] : '0' . $holiday['month'];
$day = ( $holiday['day'] > 9 ) ? $holiday['day'] : '0' . $holiday['day'];
$date = $year . '-' . $month . '-' . $day;
$holidayTimestamp = dateToTimestamp( $date );
fwrite( $log, "Found special hokdiay: " . $holiday['description'] . "\n" );
fwrite( $log, "holidayTimestamp: $holidayTimestamp\nstartChargingTimestamp: $startChargingTimestamp\ntodayTimestamp: $todayTimestamp\n" );
if ( $holidayTimestamp > $startChargingTimestamp && $holidayTimestamp < $todayTimestamp ) {
if ( $log ) fwrite( $log, "Found Relevent Special Holiday\nHoliday Date: " . timestampToDate( $holidayTimestamp ) . "\n" . print_r( $holiday, true ) . "\n" );
$numberOfHolidays++;
}
}
//Add the relevent repeating holidays to $numberOfHolidays
$holidays = getRepeatableHolidays( $dbh, $branchcode = $issuingRule['branchcode'] );
foreach ( $holidays as $holiday ) {
#fwrite( $log, "Value of holiday[weekday] is: " . $holiday['weekday'] . "\n" );
#if ( empty( $holiday['weekday'] ) ) fwrite( $log, "The value is empty\n" );
if ( $holiday['day'] ) { //This is a once a year holiday
$year = date('Y');
$month = ( $holiday['month'] > 9 ) ? $holiday['month'] : '0' . $holiday['month'];
$day = ( $holiday['day'] > 9 ) ? $holiday['day'] : '0' . $holiday['day'];
$date = $year . '-' . $month . '-' . $day;
$holidayTimestamp = dateToTimestamp( $date );
if ( $holidayTimestamp > $startChargingTimestamp && $holidayTimestamp < $todayTimestamp ) {
if ( $log ) fwrite( $log, "Found Relevent Annual Holiday\nHoliday Date: " . timestampToDate( $holidayTimestamp ) . "\n" . print_r( $holiday, true ) . "\n" );
$numberOfHolidays++;
}
} else { //This is a once a week holiday
switch ( $holiday['weekday'] ) {
case '0':
$dayOfWeek = 'Sunday';
break;
case '1':
$dayOfWeek = 'Monday';
break;
case '2':
$dayOfWeek = 'Tuesday';
break;
case '3':
$dayOfWeek = 'Wednesday';
break;
case '4':
$dayOfWeek = 'Thursday';
break;
case '5':
$dayOfWeek = 'Friday';
break;
case '6':
$dayOfWeek = 'Saturday';
}
if ( $log ) fwrite( $log, $dayOfWeek . " is a weekly repeatable holiday\n" );
//Here we are grabbing the last date of this weekday, ie last Sunday.
//We then proceed to count the number of Sundays between today and the time we begin charging fines.
$lastDayOfWeekTimestamp = strtotime( 'last ' . $dayOfWeek );
while ( $lastDayOfWeekTimestamp > $startChargingTimestamp ) {
if ( $log ) fwrite( $log, "Found Relevent Holiday\nHoliday Date: " . timestampToDate( $lastDayOfWeekTimestamp ) . "\n" . print_r( $holiday, true ) . "\n" );
$numberOfHolidays++;
$lastDayOfWeekTimestamp = strtotime( 'last ' . $dayOfWeek, $lastDayOfWeekTimestamp );
}
}
}
return $numberOfHolidays;
}
// Function getSpecialHolidays
// Returns an array of all rows in the table special_holidays
// Input:
// $dbh : The database handle
// Output: array
function getSpecialHolidays( $dbh, $branchcode ) {
$sql = "SELECT * FROM special_holidays WHERE branchcode = '$branchcode'";
$results = mysql_query( $sql );
$holidays = array();
while( $result = mysql_fetch_assoc( $results ) ) {
$holidays[] = $result;
}
return $holidays;
}
// Function getRepeatableHolidays
// Returns an array of all rows in the table repeatable_holidays
// Input:
// $dbh : The database handle
// Output: array
function getRepeatableHolidays( $dbh, $branchcode ) {
$sql = "SELECT * FROM repeatable_holidays WHERE branchcode = '$branchcode'";
$results = mysql_query( $sql );
$holidays = array();
while( $result = mysql_fetch_assoc( $results ) ) {
$holidays[] = $result;
}
return $holidays;
}
// Function getBarcodeByItemNumber
// Returns the barcode for the given itemnumber.
// Input: itemnumber, database handle
// returns $barcode; the barcode for the given itemnumber.
function getBarcodeByItemNumber( $itemnumber, $dbh ) {
$sql = 'SELECT barcode FROM items WHERE itemnumber = ' . $itemnumber;
$results = mysql_query( $sql );
$result = mysql_fetch_assoc( $results );
return $result['barcode'];
}
//dateToTimestamp accepts a date as parameter. In mySQLformat: yyyy-mm-dd (eg. 2001-03-16)
//edit the delimiter to your convenience.
//returns $timestamp; the timestamp associated with the input.
function dateToTimestamp( $date ){
$split_date = split ('-', $date);
$timestamp = gmmktime (0, 0, 0, $split_date[1], $split_date[2], $split_date[0]);
return $timestamp;
}
//timestampToDate accepts a timestamp as parameter.
//returns $date. In mySQLformat; yyyy-mm-dd (eg. 2001-03-16)
function timestampToDate( $timestamp ) {
$date = date ("Y-m-d" , $timestamp);
return $date;
}
?>

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:





No, thanks