1 package C4::Circulation::Circ2;
3 #package to deal with Returns
4 #written 3/11/99 by olwen@katipo.co.nz
8 # Copyright 2000-2002 Katipo Communications
10 # This file is part of Koha.
12 # Koha is free software; you can redistribute it and/or modify it under the
13 # terms of the GNU General Public License as published by the Free Software
14 # Foundation; either version 2 of the License, or (at your option) any later
17 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
18 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
19 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
21 # You should have received a copy of the GNU General Public License along with
22 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
23 # Suite 330, Boston, MA 02111-1307 USA
31 #use C4::InterfaceCDK;
32 #use C4::Circulation::Main;
33 #use C4::Circulation::Renewals;
40 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
42 # set the version for version checking
47 C4::Circulation::Circ2 - Koha circulation module
51 use C4::Circulation::Circ2;
55 The functions in this module deal with circulation, issues, and
56 returns, as well as general information about the library.
65 @EXPORT = qw(&getbranches &getprinters &getpatroninformation ¤tissues &getissues &getiteminformation &findborrower &issuebook &returnbook &find_reserves &transferbook &decode
67 %EXPORT_TAGS = ( ); # eg: TAG => [ qw!name1 name2! ],
69 # your exported package globals go here,
70 # as well as any optionally exported functions
72 @EXPORT_OK = qw($Var1 %Hashit); # FIXME - Unused
75 # non-exported package globals go here
76 #use vars qw(@more $stuff);
78 # initalize package globals, first exported ones
83 # then the others (which are still accessible as $Some::Module::stuff)
88 # all file-scoped lexicals must be created before
89 # the functions below that use them.
91 # file-private lexicals go here
96 # here's a file-private function as a closure,
97 # callable as &$priv_func; it cannot be prototyped.
103 # make all your functions, whether exported or not;
107 $branches = &getbranches();
108 @branch_codes = keys %$branches;
109 %main_branch_info = %{$branches->{"MAIN"}};
111 Returns information about existing library branches.
113 C<$branches> is a reference-to-hash. Its keys are the branch codes for
114 all of the existing library branches, and its values are
115 references-to-hash describing that particular branch.
117 In each branch description (C<%main_branch_info>, above), there is a
118 key for each field in the branches table of the Koha database. In
119 addition, there is a key for each branch category code to which the
120 branch belongs (the category codes are taken from the branchrelations
125 # FIXME - This function doesn't feel as if it belongs here. It should
126 # go in some generic or administrative module, not in circulation.
128 # returns a reference to a hash of references to branches...
130 my $dbh = C4::Context->dbh;
131 my $sth=$dbh->prepare("select * from branches");
133 while (my $branch=$sth->fetchrow_hashref) {
134 my $tmp = $branch->{'branchcode'}; my $brc = $dbh->quote($tmp);
135 # FIXME - my $brc = $dbh->quote($branch->{"branchcode"});
136 my $query = "select categorycode from branchrelations where branchcode = $brc";
137 my $nsth = $dbh->prepare($query);
139 while (my ($cat) = $nsth->fetchrow_array) {
140 # FIXME - This seems wrong. It ought to be
141 # $branch->{categorycodes}{$cat} = 1;
142 # otherwise, there's a namespace collision if there's a
143 # category with the same name as a field in the 'branches'
144 # table (i.e., don't create a category called "issuing").
145 # In addition, the current structure doesn't really allow
146 # you to list the categories that a branch belongs to:
147 # you'd have to list keys %$branch, and remove those keys
148 # that aren't fields in the "branches" table.
152 $branches{$branch->{'branchcode'}}=$branch;
159 $printers = &getprinters($env);
160 @queues = keys %$printers;
162 Returns information about existing printer queues.
166 C<$printers> is a reference-to-hash whose keys are the print queues
167 defined in the printers table of the Koha database. The values are
168 references-to-hash, whose keys are the fields in the printers table.
172 # FIXME - Perhaps this really belongs in C4::Print?
176 my $dbh = C4::Context->dbh;
177 my $sth=$dbh->prepare("select * from printers");
179 while (my $printer=$sth->fetchrow_hashref) {
180 $printers{$printer->{'printqueue'}}=$printer;
185 =item getpatroninformation
187 ($borrower, $flags) = &getpatroninformation($env, $borrowernumber,
190 Looks up a patron and returns information about him or her. If
191 C<$borrowernumber> is true (nonzero), C<&getpatroninformation> looks
192 up the borrower by number; otherwise, it looks up the borrower by card
195 C<$env> is effectively ignored, but should be a reference-to-hash.
197 C<$borrower> is a reference-to-hash whose keys are the fields of the
198 borrowers table in the Koha database. In addition,
199 C<$borrower-E<gt>{flags}> is the same as C<$flags>.
201 C<$flags> is a reference-to-hash giving more detailed information
202 about the patron. Its keys act as flags: if they are set, then the key
203 is a reference-to-hash that gives further details:
205 if (exists($flags->{LOST}))
207 # Patron's card was reported lost
208 print $flags->{LOST}{message}, "\n";
211 Each flag has a C<message> key, giving a human-readable explanation of
212 the flag. If the state of a flag means that the patron should not be
213 allowed to borrow any more books, then it will have a C<noissues> key
216 The possible flags are:
222 Shows the patron's credit or debt, if any.
226 (Gone, no address.) Set if the patron has left without giving a
231 Set if the patron's card has been reported as lost.
235 Set if the patron has been debarred.
239 Any additional notes about the patron.
243 Set if the patron has overdue items. This flag has several keys:
245 C<$flags-E<gt>{ODUES}{itemlist}> is a reference-to-array listing the
246 overdue items. Its elements are references-to-hash, each describing an
247 overdue item. The keys are selected fields from the issues, biblio,
248 biblioitems, and items tables of the Koha database.
250 C<$flags-E<gt>{ODUES}{itemlist}> is a string giving a text listing of
251 the overdue items, one per line.
255 Set if any items that the patron has reserved are available.
257 C<$flags-E<gt>{WAITING}{itemlist}> is a reference-to-array listing the
258 available items. Each element is a reference-to-hash whose keys are
259 fields from the reserves table of the Koha database.
265 sub getpatroninformation {
267 my ($env, $borrowernumber,$cardnumber) = @_;
268 my $dbh = C4::Context->dbh;
271 if ($borrowernumber) {
272 $query = "select * from borrowers where borrowernumber=$borrowernumber";
273 } elsif ($cardnumber) {
274 $query = "select * from borrowers where cardnumber=$cardnumber";
276 $env->{'apierror'} = "invalid borrower information passed to getpatroninformation subroutine";
279 $env->{'mess'} = $query;
280 $sth = $dbh->prepare($query);
282 my $borrower = $sth->fetchrow_hashref;
283 my $flags = patronflags($env, $borrower, $dbh);
285 $borrower->{'flags'}=$flags;
286 return($borrower, $flags);
291 $str = &decode($chunk);
293 Decodes a segment of a string emitted by a CueCat barcode scanner and
298 # FIXME - At least, I'm pretty sure this is for decoding CueCat stuff.
301 my $seq = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789+-';
302 my @s = map { index($seq,$_); } split(//,$encoded);
317 my $n = (($s[0] << 6 | $s[1]) << 6 | $s[2]) << 6 | $s[3];
318 $r .=chr(($n >> 16) ^ 67) .
319 chr(($n >> 8 & 255) ^ 67) .
320 chr(($n & 255) ^ 67);
323 $r = substr($r,0,length($r)-$l);
327 =item getiteminformation
329 $item = &getiteminformation($env, $itemnumber, $barcode);
331 Looks up information about an item, given either its item number or
332 its barcode. If C<$itemnumber> is a nonzero value, it is used;
333 otherwise, C<$barcode> is used.
335 C<$env> is effectively ignored, but should be a reference-to-hash.
337 C<$item> is a reference-to-hash whose keys are fields from the biblio,
338 items, and biblioitems tables of the Koha database. It may also
339 contain the following keys:
345 The due date on this item, if it has been borrowed and not returned
346 yet. The date is in YYYY-MM-DD format.
350 The length of time for which the item can be borrowed, in days.
354 True if the item may not be borrowed.
360 sub getiteminformation {
361 # returns a hash of item information given either the itemnumber or the barcode
362 my ($env, $itemnumber, $barcode) = @_;
363 my $dbh = C4::Context->dbh;
366 $sth=$dbh->prepare("select * from biblio,items,biblioitems where items.itemnumber=$itemnumber and biblio.biblionumber=items.biblionumber and biblioitems.biblioitemnumber = items.biblioitemnumber");
368 my $q_barcode=$dbh->quote($barcode);
369 $sth=$dbh->prepare("select * from biblio,items,biblioitems where items.barcode=$q_barcode and biblio.biblionumber=items.biblionumber and biblioitems.biblioitemnumber = items.biblioitemnumber");
371 $env->{'apierror'}="getiteminformation() subroutine must be called with either an itemnumber or a barcode";
376 my $iteminformation=$sth->fetchrow_hashref;
378 # FIXME - Style: instead of putting the entire rest of the
379 # function in a block, just say
380 # return undef unless $iteminformation;
381 # That way, the rest of the function needn't be indented as much.
382 if ($iteminformation) {
383 $sth=$dbh->prepare("select date_due from issues where itemnumber=$iteminformation->{'itemnumber'} and isnull(returndate)");
385 my ($date_due) = $sth->fetchrow;
386 $iteminformation->{'date_due'}=$date_due;
388 # FIXME - The Dewey code is a string, not a number. Besides,
389 # "000" is a perfectly valid Dewey code.
390 #$iteminformation->{'dewey'}=~s/0*$//;
391 ($iteminformation->{'dewey'} == 0) && ($iteminformation->{'dewey'}='');
392 # FIXME - fetchrow_hashref is documented as being inefficient.
393 # Perhaps this should be rewritten as
394 # $sth = $dbh->prepare("select loanlength, notforloan ...");
396 # ($iteminformation->{loanlength},
397 # $iteminformation->{notforloan}) = fetchrow_array;
398 $sth=$dbh->prepare("select * from itemtypes where itemtype='$iteminformation->{'itemtype'}'");
400 my $itemtype=$sth->fetchrow_hashref;
401 $iteminformation->{'loanlength'}=$itemtype->{'loanlength'};
402 $iteminformation->{'notforloan'}=$itemtype->{'notforloan'};
405 return($iteminformation);
410 $borrowers = &findborrower($env, $key);
411 print $borrowers->[0]{surname};
413 Looks up patrons and returns information about them.
417 C<$key> is either a card number or a string. C<&findborrower> tries to
418 look it up as a card number first. If that fails, C<&findborrower>
419 looks up all patrons whose surname begins with C<$key>.
421 C<$borrowers> is a reference-to-array. Each element is a
422 reference-to-hash whose keys are the fields of the borrowers table in
427 # If you really want to throw a monkey wrench into the works, change
428 # your last name to "V10000008" :-)
430 # FIXME - This is different from &C4::Borrower::findborrower, but I
431 # think that one's obsolete.
433 # returns an array of borrower hash references, given a cardnumber or a partial
435 my ($env, $key) = @_;
436 my $dbh = C4::Context->dbh;
438 my $q_key=$dbh->quote($key);
439 my $sth=$dbh->prepare("select * from borrowers where cardnumber=$q_key");
442 my ($borrower)=$sth->fetchrow_hashref;
443 push (@borrowers, $borrower);
445 $q_key=$dbh->quote("$key%");
447 $sth=$dbh->prepare("select * from borrowers where surname like $q_key");
449 while (my $borrower = $sth->fetchrow_hashref) {
450 push (@borrowers, $borrower);
460 ($dotransfer, $messages, $iteminformation) =
461 &transferbook($newbranch, $barcode, $ignore_reserves);
463 Transfers an item to a new branch. If the item is currently on loan,
464 it is automatically returned before the actual transfer.
466 C<$newbranch> is the code for the branch to which the item should be
469 C<$barcode> is the barcode of the item to be transferred.
471 If C<$ignore_reserves> is true, C<&transferbook> ignores reserves.
472 Otherwise, if an item is reserved, the transfer fails.
474 Returns three values:
476 C<$dotransfer> is true iff the transfer was successful.
478 C<$messages> is a reference-to-hash which may have any of the
485 There is no item in the catalog with the given barcode. The value is
490 The item's home branch is permanent. This doesn't prevent the item
491 from being transferred, though. The value is the code of the item's
494 =item C<DestinationEqualsHolding>
496 The item is already at the branch to which it is being transferred.
497 The transfer is nonetheless considered to have failed. The value
502 The item was on loan, and C<&transferbook> automatically returned it
503 before transferring it. The value is the borrower number of the patron
508 The item was reserved. The value is a reference-to-hash whose keys are
509 fields from the reserves table of the Koha database, and
510 C<biblioitemnumber>. It also has the key C<ResFound>, whose value is
511 either C<Waiting> or C<Reserved>.
513 =item C<WasTransferred>
515 The item was eligible to be transferred. Barring problems
516 communicating with the database, the transfer should indeed have
517 succeeded. The value should be ignored.
523 # FIXME - This function tries to do too much, and its API is clumsy.
524 # If it didn't also return books, it could be used to change the home
525 # branch of a book while the book is on loan.
527 # Is there any point in returning the item information? The caller can
528 # look that up elsewhere if ve cares.
530 # This leaves the ($dotransfer, $messages) tuple. This seems clumsy.
531 # If the transfer succeeds, that's all the caller should need to know.
532 # Thus, this function could simply return 1 or 0 to indicate success
533 # or failure, and set $C4::Circulation::Circ2::errmsg in case of
534 # failure. Or this function could return undef if successful, and an
535 # error message in case of failure (this would feel more like C than
538 # transfer book code....
539 my ($tbr, $barcode, $ignoreRs) = @_;
543 my $branches = getbranches();
544 my $iteminformation = getiteminformation(\%env, 0, $barcode);
546 if (not $iteminformation) {
547 $messages->{'BadBarcode'} = $barcode;
550 # get branches of book...
551 my $hbr = $iteminformation->{'homebranch'};
552 my $fbr = $iteminformation->{'holdingbranch'};
554 if ($branches->{$hbr}->{'PE'}) {
555 $messages->{'IsPermanent'} = $hbr;
557 # can't transfer book if is already there....
558 # FIXME - Why not? Shouldn't it trivially succeed?
560 $messages->{'DestinationEqualsHolding'} = 1;
563 # check if it is still issued to someone, return it...
564 my ($currentborrower) = currentborrower($iteminformation->{'itemnumber'});
565 if ($currentborrower) {
566 returnbook($barcode, $fbr);
567 $messages->{'WasReturned'} = $currentborrower;
570 # FIXME - Don't call &CheckReserves unless $ignoreRs is true.
571 # That'll save a database query.
572 my ($resfound, $resrec) = CheckReserves($iteminformation->{'itemnumber'});
573 if ($resfound and not $ignoreRs) {
574 $resrec->{'ResFound'} = $resfound;
575 $messages->{'ResFound'} = $resrec;
578 #actually do the transfer....
580 dotransfer($iteminformation->{'itemnumber'}, $fbr, $tbr);
581 $messages->{'WasTransfered'} = 1;
583 return ($dotransfer, $messages, $iteminformation);
587 # FIXME - This is only used in &transferbook. Why bother making it a
590 my ($itm, $fbr, $tbr) = @_;
591 my $dbh = C4::Context->dbh;
592 $itm = $dbh->quote($itm);
593 $fbr = $dbh->quote($fbr);
594 $tbr = $dbh->quote($tbr);
595 #new entry in branchtransfers....
597 INSERT INTO branchtransfers
598 (itemnumber, frombranch, datearrived, tobranch)
599 VALUES ($itm, $fbr, now(), $tbr)
602 #update holdingbranch in items .....
605 SET datelastseen = now(),
607 WHERE items.itemnumber = $itm
614 ($iteminformation, $datedue, $rejected, $question, $questionnumber,
615 $defaultanswer, $message) =
616 &issuebook($env, $patroninformation, $barcode, $responses, $date);
618 Issue a book to a patron.
620 C<$env-E<gt>{usercode}> will be used in the usercode field of the
621 statistics table of the Koha database when this transaction is
624 C<$env-E<gt>{datedue}>, if given, specifies the date on which the book
625 is due back. This should be a string of the form "YYYY-MM-DD".
627 C<$env-E<gt>{branchcode}> is the code of the branch where this
628 transaction is taking place.
630 C<$patroninformation> is a reference-to-hash giving information about
631 the person borrowing the book. This is the first value returned by
632 C<&getpatroninformation>.
634 C<$barcode> is the bar code of the book being issued.
636 C<$responses> is a reference-to-hash. It represents the answers to the
637 questions asked by the C<$question>, C<$questionnumber>, and
638 C<$defaultanswer> return values (see below). The keys are numbers, and
639 the values can be "Y" or "N".
641 C<$date> is an optional date in the form "YYYY-MM-DD". If specified,
642 then only fines and charges up to that date will be considered when
643 checking to see whether the patron owes too much money to be lent a
646 C<&issuebook> returns an array of seven values:
648 C<$iteminformation> is a reference-to-hash describing the item just
649 issued. This in a form similar to that returned by
650 C<&getiteminformation>.
652 C<$datedue> is a string giving the date when the book is due, in the
655 C<$rejected> is either a string, or -1. If it is defined and is a
656 string, then the book may not be issued, and C<$rejected> gives the
657 reason for this. If C<$rejected> is -1, then the book may not be
658 issued, but no reason is given.
660 If there is a problem or question (e.g., the book is reserved for
661 another patron), then C<$question>, C<$questionnumber>, and
662 C<$defaultanswer> will be set. C<$questionnumber> indicates the
663 problem. C<$question> is a text string asking how to resolve the
664 problem, as a yes-or-no question, and C<$defaultanswer> is either "Y"
665 or "N", giving the default answer. The questions, their numbers, and
670 =item 1: "Issued to <name>. Mark as returned?" (Y)
672 =item 2: "Waiting for <patron> at <branch>. Allow issue?" (N)
674 =item 3: "Cancel reserve for <patron>?" (N)
676 =item 4: "Book is issued to this borrower. Renew?" (Y)
678 =item 5: "Reserved for <patron> at <branch> since <date>. Allow issue?" (N)
680 =item 6: "Set reserve for <patron> to waiting and transfer to <branch>?" (Y)
682 This is asked if the answer to question 5 was "N".
684 =item 7: "Cancel reserve for <patron>?" (N)
688 C<$message>, if defined, is an additional information message, e.g., a
693 # FIXME - The business with $responses is absurd. For one thing, these
694 # questions should have names, not numbers. For another, it'd be
695 # better to have the last argument be %extras. Then scripts can call
699 # -mark_returned => 0,
700 # -cancel_reserve => 1,
703 # and the script can use
704 # if (defined($extras{"-mark_returned"}) && $extras{"-mark_returned"})
705 # Heck, the $date argument should go in there as well.
707 # Also, there might be several reasons why a book can't be issued, but
708 # this API only supports asking one question at a time. Perhaps it'd
709 # be better to return a ref-to-list of problem IDs. Then the calling
710 # script can display a list of all of the problems at once.
712 # Is it this function's place to decide the default answer to the
713 # various questions? Why not document the various problems and allow
714 # the caller to decide?
716 my ($env, $patroninformation, $barcode, $responses, $date) = @_;
717 my $dbh = C4::Context->dbh;
718 my $iteminformation = getiteminformation($env, 0, $barcode);
720 my ($rejected,$question,$defaultanswer,$questionnumber, $noissue);
723 # See if there's any reason this book shouldn't be issued to this
725 SWITCH: { # FIXME - Yes, we know it's a switch. Tell us what it's for.
726 if ($patroninformation->{'gonenoaddress'}) {
727 $rejected="Patron is gone, with no known address.";
730 if ($patroninformation->{'lost'}) {
731 $rejected="Patron's card has been reported lost.";
734 if ($patroninformation->{'debarred'}) {
735 $rejected="Patron is Debarred";
738 my $amount = checkaccount($env,$patroninformation->{'borrowernumber'}, $dbh,$date);
739 # FIXME - "5" shouldn't be hardcoded. An Italian library might
740 # be generous enough to lend a book to a patron even if he
741 # does still owe them 5 lire.
742 if ($amount > 5 && $patroninformation->{'categorycode'} ne 'L' &&
743 $patroninformation->{'categorycode'} ne 'W' &&
744 $patroninformation->{'categorycode'} ne 'I' &&
745 $patroninformation->{'categorycode'} ne 'B' &&
746 $patroninformation->{'categorycode'} ne 'P') {
747 # FIXME - What do these category codes mean?
748 $rejected = sprintf "Patron owes \$%.02f.", $amount;
751 # FIXME - This sort of error-checking should be placed closer
752 # to the test; in this case, this error-checking should be
753 # done immediately after the call to &getiteminformation.
754 unless ($iteminformation) {
755 $rejected = "$barcode is not a valid barcode.";
758 if ($iteminformation->{'notforloan'} == 1) {
759 $rejected="Reference item: not for loan.";
762 if ($iteminformation->{'wthdrawn'} == 1) {
763 $rejected="Item withdrawn.";
766 if ($iteminformation->{'restricted'} == 1) {
767 $rejected="Restricted item.";
771 # See who, if anyone, currently has this book.
772 my ($currentborrower) = currentborrower($iteminformation->{'itemnumber'});
773 if ($currentborrower eq $patroninformation->{'borrowernumber'}) {
774 # Already issued to current borrower. Ask whether the loan should
776 my ($renewstatus) = renewstatus($env,$dbh,$patroninformation->{'borrowernumber'}, $iteminformation->{'itemnumber'});
777 if ($renewstatus == 0) {
778 $rejected="No more renewals allowed for this item.";
781 if ($responses->{4} eq '') {
783 $question = "Book is issued to this borrower.\nRenew?";
784 $defaultanswer = 'Y';
786 } elsif ($responses->{4} eq 'Y') {
787 my $charge = calc_charges($env, $dbh, $iteminformation->{'itemnumber'}, $patroninformation->{'borrowernumber'});
789 createcharge($env, $dbh, $iteminformation->{'itemnumber'}, $patroninformation->{'borrowernumber'}, $charge);
790 $iteminformation->{'charge'} = $charge;
792 &UpdateStats($env,$env->{'branchcode'},'renew',$charge,'',$iteminformation->{'itemnumber'},$iteminformation->{'itemtype'});
793 renewbook($env,$dbh, $patroninformation->{'borrowernumber'}, $iteminformation->{'itemnumber'});
800 } elsif ($currentborrower ne '') {
801 # This book is currently on loan, but not to the person
802 # who wants to borrow it now.
803 my ($currborrower, $cbflags) = getpatroninformation($env,$currentborrower,0);
804 if ($responses->{1} eq '') {
806 $question = "Issued to $currborrower->{'firstname'} $currborrower->{'surname'} ($currborrower->{'cardnumber'}).\nMark as returned?";
809 } elsif ($responses->{1} eq 'Y') {
810 returnbook($iteminformation->{'barcode'}, $env->{'branch'});
817 # See if the item is on reserve.
818 my ($restype, $res) = CheckReserves($iteminformation->{'itemnumber'});
820 my $resbor = $res->{'borrowernumber'};
821 if ($resbor eq $patroninformation->{'borrowernumber'}) {
822 # The item is on reserve to the current patron
824 } elsif ($restype eq "Waiting") {
825 # The item is on reserve and waiting, but has been
826 # reserved by some other patron.
827 my ($resborrower, $flags)=getpatroninformation($env, $resbor,0);
828 my $branches = getbranches();
829 my $branchname = $branches->{$res->{'branchcode'}}->{'branchname'};
830 if ($responses->{2} eq '') {
832 # FIXME - Assumes HTML
833 $question="<font color=red>Waiting</font> for $resborrower->{'firstname'} $resborrower->{'surname'} ($resborrower->{'cardnumber'}) at $branchname \nAllow issue?";
836 } elsif ($responses->{2} eq 'N') {
840 if ($responses->{3} eq '') {
842 $question="Cancel reserve for $resborrower->{'firstname'} $resborrower->{'surname'} ($resborrower->{'cardnumber'})?";
845 } elsif ($responses->{3} eq 'Y') {
846 CancelReserve(0, $res->{'itemnumber'}, $res->{'borrowernumber'});
849 } elsif ($restype eq "Reserved") {
850 # The item is on reserve for someone else.
851 my ($resborrower, $flags)=getpatroninformation($env, $resbor,0);
852 my $branches = getbranches();
853 my $branchname = $branches->{$res->{'branchcode'}}->{'branchname'};
854 if ($responses->{5} eq '') {
856 $question="Reserved for $resborrower->{'firstname'} $resborrower->{'surname'} ($resborrower->{'cardnumber'}) since $res->{'reservedate'} \nAllow issue?";
859 } elsif ($responses->{5} eq 'N') {
860 if ($responses->{6} eq '') {
862 $question="Set reserve for $resborrower->{'firstname'} $resborrower->{'surname'} ($resborrower->{'cardnumber'}) to waiting and transfer to $branchname?";
864 } elsif ($responses->{6} eq 'Y') {
865 my $tobrcd = ReserveWaiting($res->{'itemnumber'}, $res->{'borrowernumber'});
866 transferbook($tobrcd, $barcode, 1);
867 $message = "Item should now be waiting at $branchname";
872 if ($responses->{7} eq '') {
874 $question="Cancel reserve for $resborrower->{'firstname'} $resborrower->{'surname'} ($resborrower->{'cardnumber'})?";
877 } elsif ($responses->{7} eq 'Y') {
878 CancelReserve(0, $res->{'itemnumber'}, $res->{'borrowernumber'});
885 unless (($question) || ($rejected) || ($noissue)) {
886 # There's no reason why the item can't be issued.
887 # FIXME - my $loanlength = $iteminformation->{loanlength} || 21;
889 if ($iteminformation->{'loanlength'}) {
890 $loanlength=$iteminformation->{'loanlength'};
892 my $ti=time; # FIXME - Never used
893 my $datedue=time+($loanlength)*86400;
894 # FIXME - Could just use POSIX::strftime("%Y-%m-%d", localtime);
895 # That's what it's for. Or, in this case:
896 # $dateduef = $env->{datedue} ||
897 # strftime("%Y-%m-%d", localtime(time +
898 # $loanlength * 86400));
899 my @datearr = localtime($datedue);
900 $dateduef = (1900+$datearr[5])."-".($datearr[4]+1)."-".$datearr[3];
901 if ($env->{'datedue'}) {
902 $dateduef=$env->{'datedue'};
904 $dateduef=~ s/2001\-4\-25/2001\-4\-26/;
905 # FIXME - What's this for? Leftover from debugging?
907 # Record in the database the fact that the book was issued.
908 # FIXME - Use $dbh->do();
909 my $sth=$dbh->prepare("insert into issues (borrowernumber, itemnumber, date_due, branchcode) values ($patroninformation->{'borrowernumber'}, $iteminformation->{'itemnumber'}, '$dateduef', '$env->{'branchcode'}')");
912 $iteminformation->{'issues'}++;
913 # FIXME - Use $dbh->do();
914 $sth=$dbh->prepare("update items set issues=$iteminformation->{'issues'},datelastseen=now() where itemnumber=$iteminformation->{'itemnumber'}");
917 # If it costs to borrow this book, charge it to the patron's account.
918 my $charge=calc_charges($env, $dbh, $iteminformation->{'itemnumber'}, $patroninformation->{'borrowernumber'});
920 createcharge($env, $dbh, $iteminformation->{'itemnumber'}, $patroninformation->{'borrowernumber'}, $charge);
921 $iteminformation->{'charge'}=$charge;
923 # Record the fact that this book was issued.
924 &UpdateStats($env,$env->{'branchcode'},'issue',$charge,'',$iteminformation->{'itemnumber'},$iteminformation->{'itemtype'});
926 if ($iteminformation->{'charge'}) {
927 $message=sprintf "Rental charge of \$%.02f applies.", $iteminformation->{'charge'};
929 return ($iteminformation, $dateduef, $rejected, $question, $questionnumber, $defaultanswer, $message);
936 ($doreturn, $messages, $iteminformation, $borrower) =
937 &returnbook($barcode, $branch);
941 C<$barcode> is the bar code of the book being returned. C<$branch> is
942 the code of the branch where the book is being returned.
944 C<&returnbook> returns a list of four items:
946 C<$doreturn> is true iff the return succeeded.
948 C<$messages> is a reference-to-hash giving the reason for failure:
954 No item with this barcode exists. The value is C<$barcode>.
958 The book is not currently on loan. The value is C<$barcode>.
962 The book's home branch is a permanent collection. If you have borrowed
963 this book, you are not allowed to return it. The value is the code for
964 the book's home branch.
968 This book has been withdrawn/cancelled. The value should be ignored.
972 The item was reserved. The value is a reference-to-hash whose keys are
973 fields from the reserves table of the Koha database, and
974 C<biblioitemnumber>. It also has the key C<ResFound>, whose value is
975 either C<Waiting>, C<Reserved>, or 0.
979 C<$borrower> is a reference-to-hash, giving information about the
980 patron who last borrowed the book.
984 # FIXME - This API is bogus. There's no need to return $borrower and
985 # $iteminformation; the caller can ask about those separately, if it
986 # cares (it'd be inefficient to make two database calls instead of
987 # one, but &getpatroninformation and &getiteminformation can be
988 # memoized if this is an issue).
990 # The ($doreturn, $messages) tuple is redundant: if the return
991 # succeeded, that's all the caller needs to know. So &returnbook can
992 # return 1 and 0 on success and failure, and set
993 # $C4::Circulation::Circ2::errmsg to indicate the error. Or it can
994 # return undef for success, and an error message on error (though this
995 # is more C-ish than Perl-ish).
997 my ($barcode, $branch) = @_;
1001 # get information on item
1002 my ($iteminformation) = getiteminformation(\%env, 0, $barcode);
1003 if (not $iteminformation) {
1004 $messages->{'BadBarcode'} = $barcode;
1008 my ($currentborrower) = currentborrower($iteminformation->{'itemnumber'});
1009 if ((not $currentborrower) && $doreturn) {
1010 $messages->{'NotIssued'} = $barcode;
1013 # check if the book is in a permanent collection....
1014 my $hbr = $iteminformation->{'homebranch'};
1015 my $branches = getbranches();
1016 if ($branches->{$hbr}->{'PE'}) {
1017 $messages->{'IsPermanent'} = $hbr;
1019 # check that the book has been cancelled
1020 if ($iteminformation->{'wthdrawn'}) {
1021 $messages->{'wthdrawn'} = 1;
1024 # update issues, thereby returning book (should push this out into another subroutine
1025 my ($borrower) = getpatroninformation(\%env, $currentborrower, 0);
1027 doreturn($borrower->{'borrowernumber'}, $iteminformation->{'itemnumber'});
1028 $messages->{'WasReturned'}; # FIXME - This does nothing
1030 ($borrower) = getpatroninformation(\%env, $currentborrower, 0);
1031 # transfer book to the current branch
1032 my ($transfered, $mess, $item) = transferbook($branch, $barcode, 1);
1033 if ($transfered) { # FIXME - perl -wc complains about this line.
1034 $messages->{'WasTransfered'}; # FIXME - This does nothing
1036 # fix up the accounts.....
1037 if ($iteminformation->{'itemlost'}) {
1038 # Mark the item as not being lost.
1039 updateitemlost($iteminformation->{'itemnumber'});
1040 fixaccountforlostandreturned($iteminformation, $borrower);
1041 $messages->{'WasLost'}; # FIXME - This does nothing
1043 # fix up the overdues in accounts...
1044 fixoverduesonreturn($borrower->{'borrowernumber'}, $iteminformation->{'itemnumber'});
1045 # find reserves.....
1046 my ($resfound, $resrec) = CheckReserves($iteminformation->{'itemnumber'});
1048 $resrec->{'ResFound'} = $resfound;
1049 $messages->{'ResFound'} = $resrec;
1052 # Record the fact that this book was returned.
1053 UpdateStats(\%env, $branch ,'return','0','',$iteminformation->{'itemnumber'});
1054 return ($doreturn, $messages, $iteminformation, $borrower);
1058 # Takes a borrowernumber and an itemnuber.
1059 # Updates the 'issues' table to mark the item as returned (assuming
1060 # that it's currently on loan to the given borrower. Otherwise, the
1061 # item remains on loan.
1062 # Updates items.datelastseen for the item.
1064 # FIXME - This is only used in &returnbook. Why make it into a
1065 # separate function?
1067 my ($brn, $itm) = @_;
1068 my $dbh = C4::Context->dbh;
1069 $brn = $dbh->quote($brn);
1070 $itm = $dbh->quote($itm);
1071 my $query = "update issues set returndate = now() where (borrowernumber = $brn)
1072 and (itemnumber = $itm) and (returndate is null)";
1073 my $sth = $dbh->prepare($query);
1076 $query="update items set datelastseen=now() where itemnumber=$itm";
1077 $sth=$dbh->prepare($query);
1084 # Marks an item as not being lost.
1088 my $dbh = C4::Context->dbh;
1093 WHERE itemnumber = $itemno
1098 sub fixaccountforlostandreturned {
1099 my ($iteminfo, $borrower) = @_;
1101 my $dbh = C4::Context->dbh;
1102 my $itm = $dbh->quote($iteminfo->{'itemnumber'});
1103 # check for charge made for lost book
1104 my $query = "select * from accountlines where (itemnumber = $itm)
1105 and (accounttype='L' or accounttype='Rep') order by date desc";
1106 my $sth = $dbh->prepare($query);
1108 if (my $data = $sth->fetchrow_hashref) {
1109 # writeoff this amount
1111 my $amount = $data->{'amount'};
1112 my $acctno = $data->{'accountno'};
1114 if ($data->{'amountoutstanding'} == $amount) {
1115 $offset = $data->{'amount'};
1118 $offset = $amount - $data->{'amountoutstanding'};
1119 $amountleft = $data->{'amountoutstanding'} - $amount;
1121 my $uquery = "update accountlines set accounttype = 'LR',amountoutstanding='0'
1122 where (borrowernumber = '$data->{'borrowernumber'}')
1123 and (itemnumber = $itm) and (accountno = '$acctno') ";
1124 my $usth = $dbh->prepare($uquery);
1127 #check if any credit is left if so writeoff other accounts
1128 my $nextaccntno = getnextacctno(\%env,$data->{'borrowernumber'},$dbh);
1129 if ($amountleft < 0){
1132 if ($amountleft > 0){
1133 my $query = "select * from accountlines where (borrowernumber = '$data->{'borrowernumber'}')
1134 and (amountoutstanding >0) order by date";
1135 my $msth = $dbh->prepare($query);
1137 # offset transactions
1140 while (($accdata=$msth->fetchrow_hashref) and ($amountleft>0)){
1141 if ($accdata->{'amountoutstanding'} < $amountleft) {
1143 $amountleft = $amountleft - $accdata->{'amountoutstanding'};
1145 $newamtos = $accdata->{'amountoutstanding'} - $amountleft;
1148 my $thisacct = $accdata->{'accountno'};
1149 my $updquery = "update accountlines set amountoutstanding= '$newamtos'
1150 where (borrowernumber = '$data->{'borrowernumber'}')
1151 and (accountno='$thisacct')";
1152 my $usth = $dbh->prepare($updquery);
1155 $updquery = "insert into accountoffsets
1156 (borrowernumber, accountno, offsetaccount, offsetamount)
1158 ('$data->{'borrowernumber'}','$accdata->{'accountno'}','$nextaccntno','$newamtos')";
1159 $usth = $dbh->prepare($updquery);
1165 if ($amountleft > 0){
1168 my $desc="Book Returned ".$iteminfo->{'barcode'};
1169 $uquery = "insert into accountlines
1170 (borrowernumber,accountno,date,amount,description,accounttype,amountoutstanding)
1171 values ('$data->{'borrowernumber'}','$nextaccntno',now(),0-$amount,'$desc',
1173 $usth = $dbh->prepare($uquery);
1176 $uquery = "insert into accountoffsets
1177 (borrowernumber, accountno, offsetaccount, offsetamount)
1178 values ($borrower->{'borrowernumber'},$data->{'accountno'},$nextaccntno,$offset)";
1179 $usth = $dbh->prepare($uquery);
1182 $uquery = "update items set paidfor='' where itemnumber=$itm";
1183 $usth = $dbh->prepare($uquery);
1192 sub fixoverduesonreturn {
1193 my ($brn, $itm) = @_;
1194 my $dbh = C4::Context->dbh;
1195 $itm = $dbh->quote($itm);
1196 $brn = $dbh->quote($brn);
1197 # check for overdue fine
1198 my $query = "select * from accountlines where (borrowernumber=$brn)
1199 and (itemnumber = $itm) and (accounttype='FU' or accounttype='O')";
1200 my $sth = $dbh->prepare($query);
1202 # alter fine to show that the book has been returned
1203 if (my $data = $sth->fetchrow_hashref) {
1204 my $query = "update accountlines set accounttype='F' where (borrowernumber = $brn)
1205 and (itemnumber = $itm) and (acccountno='$data->{'accountno'}')";
1206 my $usth=$dbh->prepare($query);
1216 # NOTE!: If you change this function, be sure to update the POD for
1217 # &getpatroninformation.
1219 # $flags = &patronflags($env, $patron, $dbh);
1222 # {message} Message showing patron's credit or debt
1223 # {noissues} Set if patron owes >$5.00
1224 # {GNA} Set if patron gone w/o address
1225 # {message} "Borrower has no valid address"
1227 # {LOST} Set if patron's card reported lost
1228 # {message} Message to this effect
1230 # {DBARRED} Set is patron is debarred
1231 # {message} Message to this effect
1233 # {NOTES} Set if patron has notes
1234 # {message} Notes about patron
1235 # {ODUES} Set if patron has overdue books
1237 # {itemlist} ref-to-array: list of overdue books
1238 # {itemlisttext} Text list of overdue items
1239 # {WAITING} Set if there are items available that the
1241 # {message} Message to this effect
1242 # {itemlist} ref-to-array: list of available items
1244 # Original subroutine for Circ2.pm
1246 my ($env, $patroninformation, $dbh) = @_;
1247 my $amount = checkaccount($env, $patroninformation->{'borrowernumber'}, $dbh);
1250 $flaginfo{'message'}= sprintf "Patron owes \$%.02f", $amount;
1252 $flaginfo{'noissues'} = 1;
1254 $flags{'CHARGES'} = \%flaginfo;
1255 } elsif ($amount < 0){
1257 $flaginfo{'message'} = sprintf "Patron has credit of \$%.02f", -$amount;
1258 $flags{'CHARGES'} = \%flaginfo;
1260 if ($patroninformation->{'gonenoaddress'} == 1) {
1262 $flaginfo{'message'} = 'Borrower has no valid address.';
1263 $flaginfo{'noissues'} = 1;
1264 $flags{'GNA'} = \%flaginfo;
1266 if ($patroninformation->{'lost'} == 1) {
1268 $flaginfo{'message'} = 'Borrower\'s card reported lost.';
1269 $flaginfo{'noissues'} = 1;
1270 $flags{'LOST'} = \%flaginfo;
1272 if ($patroninformation->{'debarred'} == 1) {
1274 $flaginfo{'message'} = 'Borrower is Debarred.';
1275 $flaginfo{'noissues'} = 1;
1276 $flags{'DBARRED'} = \%flaginfo;
1278 if ($patroninformation->{'borrowernotes'}) {
1280 $flaginfo{'message'} = "$patroninformation->{'borrowernotes'}";
1281 $flags{'NOTES'} = \%flaginfo;
1283 my ($odues, $itemsoverdue)
1284 = checkoverdues($env, $patroninformation->{'borrowernumber'}, $dbh);
1287 $flaginfo{'message'} = "Yes";
1288 $flaginfo{'itemlist'} = $itemsoverdue;
1289 foreach (sort {$a->{'date_due'} cmp $b->{'date_due'}} @$itemsoverdue) {
1290 $flaginfo{'itemlisttext'}.="$_->{'date_due'} $_->{'barcode'} $_->{'title'} \n";
1292 $flags{'ODUES'} = \%flaginfo;
1294 my ($nowaiting, $itemswaiting)
1295 = CheckWaiting($patroninformation->{'borrowernumber'});
1296 if ($nowaiting > 0) {
1298 $flaginfo{'message'} = "Reserved items available";
1299 $flaginfo{'itemlist'} = $itemswaiting;
1300 $flags{'WAITING'} = \%flaginfo;
1308 # From Main.pm, modified to return a list of overdueitems, in addition to a count
1309 #checks whether a borrower has overdue items
1310 my ($env, $bornum, $dbh)=@_;
1311 my @datearr = localtime;
1312 my $today = ($datearr[5] + 1900)."-".($datearr[4]+1)."-".$datearr[3];
1315 my $query = "SELECT * FROM issues,biblio,biblioitems,items
1316 WHERE items.biblioitemnumber = biblioitems.biblioitemnumber
1317 AND items.biblionumber = biblio.biblionumber
1318 AND issues.itemnumber = items.itemnumber
1319 AND issues.borrowernumber = $bornum
1320 AND issues.returndate is NULL
1321 AND issues.date_due < '$today'";
1322 my $sth = $dbh->prepare($query);
1324 while (my $data = $sth->fetchrow_hashref) {
1325 push (@overdueitems, $data);
1329 return ($count, \@overdueitems);
1333 sub currentborrower {
1334 # Original subroutine for Circ2.pm
1335 my ($itemnumber) = @_;
1336 my $dbh = C4::Context->dbh;
1337 my $q_itemnumber = $dbh->quote($itemnumber);
1338 my $sth=$dbh->prepare("select borrowers.borrowernumber from
1339 issues,borrowers where issues.itemnumber=$q_itemnumber and
1340 issues.borrowernumber=borrowers.borrowernumber and issues.returndate is
1343 my ($borrower) = $sth->fetchrow;
1347 # FIXME - Not exported, but used in 'updateitem.pl' anyway.
1349 # Stolen from Main.pm
1350 # Check for reserves for biblio
1351 my ($env,$dbh,$itemnum)=@_;
1353 my $query = "select * from reserves,items
1354 where (items.itemnumber = '$itemnum')
1355 and (reserves.cancellationdate is NULL)
1356 and (items.biblionumber = reserves.biblionumber)
1357 and ((reserves.found = 'W')
1358 or (reserves.found is null))
1360 my $sth = $dbh->prepare($query);
1363 my $data=$sth->fetchrow_hashref;
1364 while ($data && $resbor eq '') {
1366 my $const = $data->{'constrainttype'};
1367 if ($const eq "a") {
1368 $resbor = $data->{'borrowernumber'};
1371 my $cquery = "select * from reserveconstraints,items
1372 where (borrowernumber='$data->{'borrowernumber'}')
1373 and reservedate='$data->{'reservedate'}'
1374 and reserveconstraints.biblionumber='$data->{'biblionumber'}'
1375 and (items.itemnumber=$itemnum and
1376 items.biblioitemnumber = reserveconstraints.biblioitemnumber)";
1377 my $csth = $dbh->prepare($cquery);
1379 if (my $cdata=$csth->fetchrow_hashref) {$found = 1;}
1380 if ($const eq 'o') {
1381 if ($found eq 1) {$resbor = $data->{'borrowernumber'};}
1383 if ($found eq 0) {$resbor = $data->{'borrowernumber'};}
1387 $data=$sth->fetchrow_hashref;
1390 return ($resbor,$resrec);
1395 $issues = ¤tissues($env, $borrower);
1397 Returns a list of books currently on loan to a patron.
1399 If C<$env-E<gt>{todaysissues}> is set and true, C<¤tissues> only
1400 returns information about books issued today. If
1401 C<$env-E<gt>{nottodaysissues}> is set and true, C<¤tissues> only
1402 returns information about books issued before today. If both are
1403 specified, C<$env-E<gt>{todaysissues}> is ignored. If neither is
1404 specified, C<¤tissues> returns all of the patron's issues.
1406 C<$borrower->{borrowernumber}> is the borrower number of the patron
1407 whose issues we want to list.
1409 C<¤tissues> returns a PHP-style array: C<$issues> is a
1410 reference-to-hash whose keys are integers in the range 1...I<n>, where
1411 I<n> is the number of items on issue (either today or before today).
1412 C<$issues-E<gt>{I<n>}> is a reference-to-hash whose keys are all of
1413 the fields of the biblio, biblioitems, items, and issues fields of the
1414 Koha database for that particular item.
1419 # New subroutine for Circ2.pm
1420 my ($env, $borrower) = @_;
1421 my $dbh = C4::Context->dbh;
1424 my $borrowernumber = $borrower->{'borrowernumber'};
1427 # Figure out whether to get the books issued today, or earlier.
1428 # FIXME - $env->{todaysissues} and $env->{nottodaysissues} can
1429 # both be specified, but are mutually-exclusive. This is bogus.
1430 # Make this a flag. Or better yet, return everything in (reverse)
1431 # chronological order and let the caller figure out which books
1432 # were issued today.
1433 if ($env->{'todaysissues'}) {
1435 # $today = POSIX::strftime("%Y%m%d", localtime);
1436 # FIXME - Since $today will be used in either case, move it
1437 # out of the two if-blocks.
1438 my @datearr = localtime(time());
1439 my $today = (1900+$datearr[5]).sprintf "%02d", ($datearr[4]+1).sprintf "%02d", $datearr[3];
1440 # FIXME - MySQL knows about dates. Just use
1441 # and issues.timestamp = curdate();
1442 $crit=" and issues.timestamp like '$today%' ";
1444 if ($env->{'nottodaysissues'}) {
1446 # $today = POSIX::strftime("%Y%m%d", localtime);
1447 # FIXME - Since $today will be used in either case, move it
1448 # out of the two if-blocks.
1449 my @datearr = localtime(time());
1450 my $today = (1900+$datearr[5]).sprintf "%02d", ($datearr[4]+1).sprintf "%02d", $datearr[3];
1451 # FIXME - MySQL knows about dates. Just use
1452 # and issues.timestamp < curdate();
1453 $crit=" and !(issues.timestamp like '$today%') ";
1456 # FIXME - Does the caller really need every single field from all
1458 my $select="select * from issues,items,biblioitems,biblio where
1459 borrowernumber='$borrowernumber' and issues.itemnumber=items.itemnumber and
1460 items.biblionumber=biblio.biblionumber and
1461 items.biblioitemnumber=biblioitems.biblioitemnumber and returndate is null
1462 $crit order by issues.date_due";
1464 my $sth=$dbh->prepare($select);
1466 while (my $data = $sth->fetchrow_hashref) {
1467 # FIXME - The Dewey code is a string, not a number.
1468 $data->{'dewey'}=~s/0*$//;
1469 ($data->{'dewey'} == 0) && ($data->{'dewey'}='');
1471 # $todaysdate = POSIX::strftime("%Y%m%d", localtime)
1472 # or better yet, just reuse $today which was calculated above.
1473 # This function isn't going to run until midnight, is it?
1475 # $todaysdate = POSIX::strftime("%Y-%m-%d", localtime)
1476 # if ($data->{'date_due'} lt $todaysdate)
1478 # Either way, the date should be be formatted outside of the
1480 my @datearr = localtime(time());
1481 my $todaysdate = (1900+$datearr[5]).sprintf ("%0.2d", ($datearr[4]
1482 +1)).sprintf ("%0.2d", $datearr[3]);
1483 my $datedue=$data->{'date_due'};
1485 if ($datedue < $todaysdate) {
1486 $data->{'overdue'}=1;
1488 my $itemnumber=$data->{'itemnumber'};
1489 # FIXME - Consecutive integers as hash keys? You have GOT to
1490 # be kidding me! Use an array, fercrissakes!
1491 $currentissues{$counter}=$data;
1495 return(\%currentissues);
1500 $issues = &getissues($borrowernumber);
1502 Returns the set of books currently on loan to a patron.
1504 C<$borrowernumber> is the patron's borrower number.
1506 C<&getissues> returns a PHP-style array: C<$issues> is a
1507 reference-to-hash whose keys are integers in the range 0..I<n>-1,
1508 where I<n> is the number of books the patron currently has on loan.
1510 The values of C<$issues> are references-to-hash whose keys are
1511 selected fields from the issues, items, biblio, and biblioitems tables
1512 of the Koha database.
1517 # New subroutine for Circ2.pm
1518 my ($borrower) = @_;
1519 my $dbh = C4::Context->dbh;
1520 my $borrowernumber = $borrower->{'borrowernumber'};
1521 my $brn =$dbh->quote($borrowernumber);
1523 my $select = "select issues.timestamp, issues.date_due, items.biblionumber,
1524 items.barcode, biblio.title, biblio.author, biblioitems.dewey,
1525 biblioitems.subclass
1526 from issues,items,biblioitems,biblio
1527 where issues.borrowernumber = $brn
1528 and issues.itemnumber = items.itemnumber
1529 and items.biblionumber = biblio.biblionumber
1530 and items.biblioitemnumber = biblioitems.biblioitemnumber
1531 and issues.returndate is null
1532 order by issues.date_due";
1534 my $sth=$dbh->prepare($select);
1537 while (my $data = $sth->fetchrow_hashref) {
1538 $data->{'dewey'} =~ s/0*$//;
1539 ($data->{'dewey'} == 0) && ($data->{'dewey'} = '');
1540 # FIXME - The Dewey code is a string, not a number.
1541 # FIXME - Use POSIX::strftime to get a text version of today's
1542 # date. That's what it's for.
1543 # FIXME - Move the date calculation outside of the loop.
1544 my @datearr = localtime(time());
1545 my $todaysdate = (1900+$datearr[5]).sprintf ("%0.2d", ($datearr[4]+1)).sprintf ("%0.2d", $datearr[3]);
1547 # FIXME - Instead of converting the due date to YYYYMMDD, just
1549 # $todaysdate = POSIX::strftime("%Y-%m-%d", localtime);
1551 # if ($date->{date_due} lt $todaysdate)
1552 my $datedue = $data->{'date_due'};
1554 if ($datedue < $todaysdate) {
1555 $data->{'overdue'} = 1;
1557 $currentissues{$counter} = $data;
1559 # FIXME - This is ludicrous. If you want to return an
1560 # array of values, just use an array. That's what
1561 # they're there for.
1564 return(\%currentissues);
1569 #Stolen from Main.pm
1570 # check for reserves waiting
1571 my ($env,$dbh,$bornum)=@_;
1573 my $query = "select * from reserves
1574 where (borrowernumber = '$bornum')
1575 and (reserves.found='W') and cancellationdate is NULL";
1576 my $sth = $dbh->prepare($query);
1579 if (my $data=$sth->fetchrow_hashref) {
1580 $itemswaiting[$cnt] =$data;
1584 return ($cnt,\@itemswaiting);
1588 # FIXME - This is nearly-identical to &C4::Accounts::checkaccount
1590 # Stolen from Accounts.pm
1591 #take borrower number
1592 #check accounts and list amounts owing
1593 my ($env,$bornumber,$dbh,$date)=@_;
1594 my $select="Select sum(amountoutstanding) from accountlines where
1595 borrowernumber=$bornumber and amountoutstanding<>0";
1597 $select.=" and date < '$date'";
1600 my $sth=$dbh->prepare($select);
1603 while (my $data=$sth->fetchrow_hashref){
1604 $total=$total+$data->{'sum(amountoutstanding)'};
1607 # output(1,2,"borrower owes $total");
1609 # # output(1,2,"borrower owes $total");
1611 # reconcileaccount($env,$dbh,$bornumber,$total);
1619 # Stolen from Renewals.pm
1620 # check renewal status
1621 my ($env,$dbh,$bornum,$itemno)=@_;
1624 my $q1 = "select * from issues
1625 where (borrowernumber = '$bornum')
1626 and (itemnumber = '$itemno')
1627 and returndate is null";
1628 my $sth1 = $dbh->prepare($q1);
1630 if (my $data1 = $sth1->fetchrow_hashref) {
1631 my $q2 = "select renewalsallowed from items,biblioitems,itemtypes
1632 where (items.itemnumber = '$itemno')
1633 and (items.biblioitemnumber = biblioitems.biblioitemnumber)
1634 and (biblioitems.itemtype = itemtypes.itemtype)";
1635 my $sth2 = $dbh->prepare($q2);
1637 if (my $data2=$sth2->fetchrow_hashref) {
1638 $renews = $data2->{'renewalsallowed'};
1640 if ($renews > $data1->{'renewals'}) {
1650 # Stolen from Renewals.pm
1651 # mark book as renewed
1652 my ($env,$dbh,$bornum,$itemno,$datedue)=@_;
1653 $datedue=$env->{'datedue'};
1654 if ($datedue eq "" ) {
1656 my $query= "Select * from biblioitems,items,itemtypes
1657 where (items.itemnumber = '$itemno')
1658 and (biblioitems.biblioitemnumber = items.biblioitemnumber)
1659 and (biblioitems.itemtype = itemtypes.itemtype)";
1660 my $sth=$dbh->prepare($query);
1662 if (my $data=$sth->fetchrow_hashref) {
1663 $loanlength = $data->{'loanlength'}
1667 my $datedu = time + ($loanlength * 86400);
1668 my @datearr = localtime($datedu);
1669 $datedue = (1900+$datearr[5])."-".($datearr[4]+1)."-".$datearr[3];
1671 my @date = split("-",$datedue);
1672 my $odatedue = ($date[2]+0)."-".($date[1]+0)."-".$date[0];
1673 my $issquery = "select * from issues where borrowernumber='$bornum' and
1674 itemnumber='$itemno' and returndate is null";
1675 my $sth=$dbh->prepare($issquery);
1677 my $issuedata=$sth->fetchrow_hashref;
1679 my $renews = $issuedata->{'renewals'} +1;
1680 my $updquery = "update issues
1681 set date_due = '$datedue', renewals = '$renews'
1682 where borrowernumber='$bornum' and
1683 itemnumber='$itemno' and returndate is null";
1684 $sth=$dbh->prepare($updquery);
1691 # FIXME - This is almost, but not quite, identical to
1692 # &C4::Circulation::Issues::calc_charges and
1693 # &C4::Circulation::Renewals2::calc_charges.
1694 # Pick one and stick with it.
1696 # Stolen from Issues.pm
1697 # calculate charges due
1698 my ($env, $dbh, $itemno, $bornum)=@_;
1703 # open (FILE,">>/tmp/charges");
1705 my $q1 = "select itemtypes.itemtype,rentalcharge from items,biblioitems,itemtypes
1706 where (items.itemnumber ='$itemno')
1707 and (biblioitems.biblioitemnumber = items.biblioitemnumber)
1708 and (biblioitems.itemtype = itemtypes.itemtype)";
1709 my $sth1= $dbh->prepare($q1);
1710 # print FILE "$q1\n";
1712 if (my $data1=$sth1->fetchrow_hashref) {
1713 $item_type = $data1->{'itemtype'};
1714 $charge = $data1->{'rentalcharge'};
1715 # print FILE "charge is $charge\n";
1716 my $q2 = "select rentaldiscount from borrowers,categoryitem
1717 where (borrowers.borrowernumber = '$bornum')
1718 and (borrowers.categorycode = categoryitem.categorycode)
1719 and (categoryitem.itemtype = '$item_type')";
1720 my $sth2=$dbh->prepare($q2);
1723 if (my $data2=$sth2->fetchrow_hashref) {
1724 my $discount = $data2->{'rentaldiscount'};
1725 # print FILE "discount is $discount";
1726 if ($discount eq 'NULL') {
1729 $charge = ($charge *(100 - $discount)) / 100;
1739 #Stolen from Issues.pm
1740 my ($env,$dbh,$itemno,$bornum,$charge) = @_;
1741 my $nextaccntno = getnextacctno($env,$bornum,$dbh);
1742 my $query = "insert into accountlines (borrowernumber,itemnumber,accountno,date,amount, description,accounttype,amountoutstanding) values ($bornum,$itemno,$nextaccntno,now(),$charge,'Rental','Rent',$charge)";
1743 my $sth = $dbh->prepare($query);
1750 # Stolen from Accounts.pm
1751 my ($env,$bornumber,$dbh)=@_;
1752 my $nextaccntno = 1;
1753 my $query = "select * from accountlines where (borrowernumber = '$bornumber') order by accountno desc";
1754 my $sth = $dbh->prepare($query);
1756 if (my $accdata=$sth->fetchrow_hashref){
1757 $nextaccntno = $accdata->{'accountno'} + 1;
1760 return($nextaccntno);
1765 ($status, $record) = &find_reserves($itemnumber);
1767 Looks up an item in the reserves.
1769 C<$itemnumber> is the itemnumber to look up.
1771 C<$status> is true iff the search was successful.
1773 C<$record> is a reference-to-hash describing the reserve. Its keys are
1774 the fields from the reserves table of the Koha database.
1778 # FIXME - This API is bogus: just return the record, or undef if none
1781 # Stolen from Returns.pm
1784 my $dbh = C4::Context->dbh;
1785 my ($itemdata) = getiteminformation(\%env, $itemno,0);
1786 my $bibno = $dbh->quote($itemdata->{'biblionumber'});
1787 my $bibitm = $dbh->quote($itemdata->{'biblioitemnumber'});
1788 my $query = "select * from reserves where ((found = 'W') or (found is null))
1789 and biblionumber = $bibno and cancellationdate is NULL
1790 order by priority, reservedate ";
1791 my $sth = $dbh->prepare($query);
1798 # FIXME - I'm not really sure what's going on here, but since we
1799 # only want one result, wouldn't it be possible (and far more
1800 # efficient) to do something clever in SQL that only returns one
1802 while (($resrec = $sth->fetchrow_hashref) && (not $resfound)) {
1803 # FIXME - Unlike Pascal, Perl allows you to exit loops
1804 # early. Take out the "&& (not $resfound)" and just
1805 # use "last" at the appropriate point in the loop.
1806 # (Oh, and just in passing: if you'd used "!" instead
1807 # of "not", you wouldn't have needed the parentheses.)
1809 my $brn = $dbh->quote($resrec->{'borrowernumber'});
1810 my $rdate = $dbh->quote($resrec->{'reservedate'});
1811 my $bibno = $dbh->quote($resrec->{'biblionumber'});
1812 if ($resrec->{'found'} eq "W") {
1813 if ($resrec->{'itemnumber'} eq $itemno) {
1817 # FIXME - Use 'elsif' to avoid unnecessary indentation.
1818 if ($resrec->{'constrainttype'} eq "a") {
1821 my $conquery = "select * from reserveconstraints where borrowernumber = $brn
1822 and reservedate = $rdate and biblionumber = $bibno and biblioitemnumber = $bibitm";
1823 my $consth = $dbh->prepare($conquery);
1825 if (my $conrec = $consth->fetchrow_hashref) {
1826 if ($resrec->{'constrainttype'} eq "o") {
1834 my $updquery = "update reserves set found = 'W', itemnumber = '$itemno'
1835 where borrowernumber = $brn and reservedate = $rdate and biblionumber = $bibno";
1836 my $updsth = $dbh->prepare($updquery);
1839 # FIXME - "last;" here to break out of the loop early.
1843 return ($resfound,$lastrec);
1846 END { } # module clean-up code here (global destructor)
1855 Koha Developement team <info@koha.org>