[r88]: koha-simulate / tags / 0.1.1 / Simulate.pm  Maximize  Restore  History

Download this file

416 lines (323 with data), 13.2 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
## Copyright 2006 Kyle Hall
## Copyright 2006 Kyle Hall
## This file is part of koha-tools.
## koha-tools is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 2 of the License, or
## (at your option) any later version.
## koha-tools is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
## GNU General Public License for more details.
## You should have received a copy of the GNU General Public License
## along with koha-tools; if not, write to the Free Software
## Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
## The latest version of the file can always be found at
## http://koha-tools.sourceforge.net
package C4::Simulate;
use strict;
require Exporter;
use DBI;
use C4::Context;
use C4::Circulation::Circ2;
## Input: Number of patrons to check out items to, max number of items per patron
## Output: None
## Action: Randomly checks out a number of books to a random borrower
sub perform_random_issues {
my ($number_of_patrons, $max_items_per_patron) = @_;
my @borrower_numbers = get_random_borrower_numbers($number_of_patrons);
foreach my $borrower_number (@borrower_numbers) {
my $number_of_items = rand($max_items_per_patron) + 1;
my @barcodes = get_random_unissued_item_barcodes($number_of_items);
foreach my $barcode (@barcodes) {
issue_item($barcode, $borrower_number);
}
}
}
## Input: Number of checkins to perform
## Output: None
## Action: Checks in a given number of randomly selected checked out books
sub perform_random_returns {
my $number_of_returns = $_[0];
my @barcodes = get_random_issued_item_barcodes($number_of_returns);
foreach my $barcode (@barcodes) {
return_item($barcode);
}
}
## Input: Number of borrowers to get
## Output: array of borrowernumbers
sub get_random_borrower_numbers {
my ($number_of_borrowers_to_get) = @_;
my @borrowers;
my $dbh = C4::Context->dbh or die "Couldn't connect to database: " . DBD->errstr . "\n";
my $sth = $dbh->prepare("SELECT borrowernumber FROM borrowers
ORDER BY RAND() LIMIT $number_of_borrowers_to_get")
or die "Couldn't prepare statement: " . $dbh->errstr . "\n";
$sth->execute()
or die "Couldn't execute statement: " . $sth->errstr . "\n";
# Read the matching records and print them out
while (my @data = $sth->fetchrow_array()) {
push(@borrowers, $data[0]);
}
return @borrowers;
}
## Input: Number of items to get
## Output: array of barcodes
sub get_random_unissued_item_barcodes {
my ($number_of_items_to_get) = @_;
my @unissued_items;
my $dbh = C4::Context->dbh or die "Couldn't connect to database: " . DBD->errstr . "\n";
my $sth = $dbh->prepare("SELECT items.barcode
FROM items
LEFT JOIN issues ON items.itemnumber = issues.itemnumber
WHERE (
issues.returndate IS NULL
AND issues.borrowernumber IS NULL
)
ORDER BY RAND()
LIMIT $number_of_items_to_get")
or die "Couldn't prepare statement: " . $dbh->errstr . "\n";
$sth->execute()
or die "Couldn't execute statement: " . $sth->errstr . "\n";
## Push the barcodes onto the unissued_items array
while (my @data = $sth->fetchrow_array()) {
push(@unissued_items, $data[0]);
}
return @unissued_items;
}
## Input: Number of items to get
## Output: array of barcodes
sub get_random_issued_item_barcodes {
my ($number_of_items_to_get) = @_;
my @issued_items;
my $dbh = C4::Context->dbh or die "Couldn't connect to database: " . DBD->errstr . "\n";
my $sth = $dbh->prepare("SELECT items.barcode
FROM items, issues
WHERE (
issues.returndate IS NULL
AND items.itemnumber = issues.itemnumber
)
ORDER BY RAND()
LIMIT $number_of_items_to_get")
or die "Couldn't prepare statement: " . $dbh->errstr . "\n";
$sth->execute()
or die "Couldn't execute statement: " . $sth->errstr . "\n";
# Read the matching records and print them out
while (my @data = $sth->fetchrow_array()) {
push(@issued_items, $data[0]);
}
return @issued_items;
}
## Input: Item Barcode, Borrower Number
## Output: None
## Effects: Checks out given item to given borrower
sub issue_item {
my ($item_barcode, $borrower_number) = @_;
## I'd like to get this to work with C4::Circulation::Circ2::issuebook,
## But for some reason it just doesn't do anything when I call it.
# my ($card_number, $date, $cancelreserve) = '';
# my %env;
# $env{'branchcode'} = 'MPL';
# $env{'printer'}='devlp0';
# $env{'queue'}='devlp0';
# my ($borrower, $flags) = &getpatroninformation(%env, $borrower_number, $card_number);
# &issuebook(\%env,$borrower,$item_barcode,$date,$cancelreserve);
use Date::Manip;
my $date_due;
$date_due = DateCalc("today","+ 14 days");
$date_due = UnixDate($date_due,"%Y-%m-%d");
my %item = get_item($item_barcode);
write_issue_to_database($borrower_number,
$item{'itemnumber'},
$date_due,
$item{'homebranch'},
'',
'',
'',
'');
my $itemtype = get_itemtype($item{'biblioitemnumber'});
write_statistic_to_database(
$item{'holdingbranch'},
my $proccode = '',
my $value = 0,
my $type = 'issue',
my $other = '',
my $usercode = '',
$item{'itemnumber'},
$itemtype,
$borrower_number);
}
## Input: Item barcode
## Output: None
## Effects: Returns the item to its home branch
sub return_item {
my ($item_barcode) = @_;
## Get the home branch for this item
my $dbh = C4::Context->dbh or die "Couldn't connect to database: " . DBD->errstr . "\n";
my $sth = $dbh->prepare("SELECT items.holdingbranch
FROM items
WHERE items.barcode = $item_barcode
")
or die "Couldn't prepare statement: " . $dbh->errstr . "\n";
$sth->execute()
or die "Couldn't execute statement: " . $sth->errstr . "\n";
my @data = $sth->fetchrow_array();
my $item_branchcode = $data[0];
## Return the item to its home branch
my ($doreturn, $messages, $iteminformation, $borrower) =
&returnbook($item_barcode, $item_branchcode);
if ($doreturn) {
return 1;
} else {
die("Item Return Failed (barcode: $item_barcode, branchcode: $item_branchcode ): $messages");
}
}
## Input: Item barcode
## Output: Hash of item's information
sub get_item {
my $item_barcode = $_[0];
my $dbh = C4::Context->dbh or die "Couldn't connect to database: " . DBD->errstr . "\n";
my $sth = $dbh->prepare("SELECT *
FROM items
WHERE items.barcode = $item_barcode
")
or die "Couldn't prepare statement: " . $dbh->errstr . "\n";
$sth->execute()
or die "Couldn't execute statement: " . $sth->errstr . "\n";
my @data = $sth->fetchrow_array();
my %item;
$item{'itemnumber'} = $data[0];
$item{'biblionumber'} = $data[1];
$item{'multivolumepart'} = $data[2];
$item{'biblioitemnumber'} = $data[3];
$item{'barcode'} = $data[4];
$item{'dateaccessioned'} = $data[5];
$item{'booksellerid'} = $data[6];
$item{'homebranch'} = $data[7];
$item{'price'} = $data[8];
$item{'replacementprice'} = $data[9];
$item{'replacementpricedate'} = $data[10];
$item{'datelastborrowed'} = $data[11];
$item{'datelastseen'} = $data[12];
$item{'multivolume'} = $data[13];
$item{'stack'} = $data[14];
$item{'notforloan'} = $data[15];
$item{'itemlost'} = $data[16];
$item{'wthdrawn'} = $data[17];
$item{'itemcallnumber'} = $data[18];
$item{'issues'} = $data[19];
$item{'renewals'} = $data[20];
$item{'reserves'} = $data[21];
$item{'restricted'} = $data[22];
$item{'binding'} = $data[23];
$item{'itemnotes'} = $data[24];
$item{'holdingbranch'} = $data[25];
$item{'paidfor'} = $data[26];
$item{'timestamp'} = $data[27];
$item{'location '} = $data[28];
return %item;
}
## Input: borrowernumber, itemnumber, date_due, branchcode
## Output: None
## Action: Checks out issue to borrower from the books home branch
sub write_issue_to_database {
my ($borrowernumber,
$itemnumber,
$date_due,
$branchcode,
$issueingbranch,
$returndate,
$lastrenewdate,
$return,
$renewals) = @_;
my $dbh = C4::Context->dbh or die "Couldn't connect to database: " . DBD->errstr . "\n";
my $sth = $dbh->prepare("INSERT INTO issues (
borrowernumber,
itemnumber,
date_due,
branchcode,
issuingbranch,
returndate,
lastreneweddate,
return,
renewals,
timestamp )
VALUES (
?, ?, ?, ?, NULL, NULL, NULL, NULL, NULL, NOW()
);
")
or die "Couldn't prepare statement: " . $dbh->errstr . "\n";
$sth->execute($borrowernumber,
$itemnumber,
$date_due,
$branchcode,
)
or die "Couldn't execute statement: " . $sth->errstr . "\n";
}
## Input: datetime, branch, proccode, value, type, other, usercode, itemnumber, itemtype, borrowernumber
## Output: None
## Action: Creates a line in the statistics table
sub write_statistic_to_database {
my ($branch,
$proccode,
$value,
$type,
$other,
$usercode,
$itemnumber,
$itemtype,
$borrowernumber) = @_;
my $datetime = get_last_issued_timestamp();
my $dbh = C4::Context->dbh or die "Couldn't connect to database: " . DBD->errstr . "\n";
my $sth = $dbh->prepare("INSERT INTO statistics (
datetime,
branch,
proccode,
value,
type,
other,
usercode,
itemnumber,
itemtype,
borrowernumber)
VALUES (
?, ?, ?, ?, ?, ?, ?, ?, ?, ?);
")
or die "Couldn't prepare statement: " . $dbh->errstr . "\n";
$sth->execute($datetime,
$branch,
$proccode,
$value,
$type,
$other,
$usercode,
$itemnumber,
$itemtype,
$borrowernumber,
)
or die "Couldn't execute statement: " . $sth->errstr . "\n";
}
## Input: None
## Output: The timestamp of the last issued item
sub get_last_issued_timestamp {
my $dbh = C4::Context->dbh or die "Couldn't connect to database: " . DBD->errstr . "\n";
my $sth = $dbh->prepare("SELECT MAX( TIMESTAMP ) AS newest_timestamp
FROM issues")
or die "Couldn't prepare statement: " . $dbh->errstr . "\n";
$sth->execute()
or die "Couldn't execute statement: " . $sth->errstr . "\n";
my @data = $sth->fetchrow_array();
my $datetime = $data[0];
return $datetime;
}
sub get_itemtype {
my $biblioitemnumber = @_;
my $dbh = C4::Context->dbh or die "Couldn't connect to database: " . DBD->errstr . "\n";
my $sth = $dbh->prepare("SELECT itemtype FROM biblioitems WHERE biblioitemnumber = ?")
or die "Couldn't prepare statement: " . $dbh->errstr . "\n";
$sth->execute($biblioitemnumber)
or die "Couldn't execute statement: " . $sth->errstr . "\n";
my @data = $sth->fetchrow_array();
my $itemtype = $data[0];
return $itemtype;
}

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

Sign up for the SourceForge newsletter:





No, thanks