Code Cleaning Members.
[koha.git] / C4 / Circulation.pm
1 package C4::Circulation;
2
3 # Copyright 2000-2002 Katipo Communications
4 #
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
10 # version.
11 #
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License along with
17 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
18 # Suite 330, Boston, MA  02111-1307 USA
19
20 # $Id$
21
22 use strict;
23 require Exporter;
24 use C4::Context;
25 use C4::Stats;
26 use C4::Reserves2;
27 use C4::Koha;
28 use C4::Biblio;
29 use C4::Reserves2;
30 use C4::Members;
31 use C4::Date;
32 use Date::Calc qw(
33   Today
34   Today_and_Now
35   Add_Delta_YM
36   Add_Delta_DHMS
37   Date_to_Days
38 );
39 use POSIX qw(strftime);
40 use C4::Branch; # GetBranches
41 use C4::Log; # logaction
42
43 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
44
45 # set the version for version checking
46 $VERSION = do { my @v = '$Revision$' =~ /\d+/g; shift(@v).".".join( "_", map { sprintf "%03d", $_ } @v ); };
47
48 =head1 NAME
49
50 C4::Circulation::Circ2 - Koha circulation module
51
52 =head1 SYNOPSIS
53
54 use C4::Circulation;
55
56 =head1 DESCRIPTION
57
58 The functions in this module deal with circulation, issues, and
59 returns, as well as general information about the library.
60 Also deals with stocktaking.
61
62 =head1 FUNCTIONS
63
64 =cut
65
66 @ISA    = qw(Exporter);
67
68 # FIXME subs that should probably be elsewhere
69 push @EXPORT, qw(
70   &fixoverduesonreturn
71 );
72
73 # subs to deal with issuing a book
74 push @EXPORT, qw(
75   &CanBookBeIssued
76   &CanBookBeRenewed
77   &AddIssue
78   &AddRenewal
79   &GetItemIssue
80   &GetItemIssues
81   &GetBorrowerIssues
82   &GetIssuingCharges
83   &GetBiblioIssues
84   &AnonymiseIssueHistory
85 );
86 # subs to deal with returns
87 push @EXPORT, qw(
88   &AddReturn
89 );
90
91 # subs to deal with transfers
92 push @EXPORT, qw(
93   &transferbook
94   &GetTransfers
95   &GetTransfersFromTo
96   &updateWrongTransfer
97   &DeleteTransfer
98 );
99
100 # subs to remove
101 push @EXPORT, qw(
102   &decode
103   &dotransfer
104 );
105
106 =head2 decode
107
108 =head3 $str = &decode($chunk);
109
110 =over 4
111
112 =item Decodes a segment of a string emitted by a CueCat barcode scanner and
113 returns it.
114
115 =back
116
117 =cut
118
119 # FIXME - At least, I'm pretty sure this is for decoding CueCat stuff.
120
121 # FIXME From Paul : i don't understand what this sub does & why it has to be called on every circ. Speak of this with chris maybe ?
122 sub decode {
123     my ($encoded) = @_;
124     my $seq =
125       'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789+-';
126     my @s = map { index( $seq, $_ ); } split( //, $encoded );
127     my $l = ( $#s + 1 ) % 4;
128     if ($l) {
129         if ( $l == 1 ) {
130             warn "Error!";
131             return;
132         }
133         $l = 4 - $l;
134         $#s += $l;
135     }
136     my $r = '';
137     while ( $#s >= 0 ) {
138         my $n = ( ( $s[0] << 6 | $s[1] ) << 6 | $s[2] ) << 6 | $s[3];
139         $r .=
140             chr( ( $n >> 16 ) ^ 67 )
141          .chr( ( $n >> 8 & 255 ) ^ 67 )
142          .chr( ( $n & 255 ) ^ 67 );
143         @s = @s[ 4 .. $#s ];
144     }
145     $r = substr( $r, 0, length($r) - $l );
146     return $r;
147 }
148
149 =head2 transferbook
150
151 ($dotransfer, $messages, $iteminformation) = &transferbook($newbranch, $barcode, $ignore_reserves);
152
153 Transfers an item to a new branch. If the item is currently on loan, it is automatically returned before the actual transfer.
154
155 C<$newbranch> is the code for the branch to which the item should be transferred.
156
157 C<$barcode> is the barcode of the item to be transferred.
158
159 If C<$ignore_reserves> is true, C<&transferbook> ignores reserves.
160 Otherwise, if an item is reserved, the transfer fails.
161
162 Returns three values:
163
164 =head3 $dotransfer 
165
166 is true if the transfer was successful.
167
168 =head3 $messages
169
170 is a reference-to-hash which may have any of the following keys:
171
172 =over 4
173
174 =item C<BadBarcode>
175
176 There is no item in the catalog with the given barcode. The value is C<$barcode>.
177
178 =item C<IsPermanent>
179
180 The item's home branch is permanent. This doesn't prevent the item from being transferred, though. The value is the code of the item's home branch.
181
182 =item C<DestinationEqualsHolding>
183
184 The item is already at the branch to which it is being transferred. The transfer is nonetheless considered to have failed. The value should be ignored.
185
186 =item C<WasReturned>
187
188 The item was on loan, and C<&transferbook> automatically returned it before transferring it. The value is the borrower number of the patron who had the item.
189
190 =item C<ResFound>
191
192 The item was reserved. The value is a reference-to-hash whose keys are fields from the reserves table of the Koha database, and C<biblioitemnumber>. It also has the key C<ResFound>, whose value is either C<Waiting> or C<Reserved>.
193
194 =item C<WasTransferred>
195
196 The item was eligible to be transferred. Barring problems communicating with the database, the transfer should indeed have succeeded. The value should be ignored.
197
198 =back
199
200 =cut
201
202 sub transferbook {
203     my ( $tbr, $barcode, $ignoreRs ) = @_;
204     my $messages;
205     my $dotransfer      = 1;
206     my $branches        = GetBranches();
207     my $itemnumber = GetItemnumberFromBarcode( $barcode );
208     my $issue      = GetItemIssue($itemnumber);
209     my $biblio = GetBiblioFromItemNumber($itemnumber);
210
211     # bad barcode..
212     if ( not $itemnumber ) {
213         $messages->{'BadBarcode'} = $barcode;
214         $dotransfer = 0;
215     }
216
217     # get branches of book...
218     my $hbr = $biblio->{'homebranch'};
219     my $fbr = $biblio->{'holdingbranch'};
220
221     # if is permanent...
222     if ( $hbr && $branches->{$hbr}->{'PE'} ) {
223         $messages->{'IsPermanent'} = $hbr;
224     }
225
226     # can't transfer book if is already there....
227     if ( $fbr eq $tbr ) {
228         $messages->{'DestinationEqualsHolding'} = 1;
229         $dotransfer = 0;
230     }
231
232     # check if it is still issued to someone, return it...
233     if ($issue->{borrowernumber}) {
234         AddReturn( $barcode, $fbr );
235         $messages->{'WasReturned'} = $issue->{borrowernumber};
236     }
237
238     # find reserves.....
239     # That'll save a database query.
240     my ( $resfound, $resrec ) =
241       CheckReserves( $itemnumber );
242     if ( $resfound and not $ignoreRs ) {
243         $resrec->{'ResFound'} = $resfound;
244
245         #         $messages->{'ResFound'} = $resrec;
246         $dotransfer = 1;
247     }
248
249     #actually do the transfer....
250     if ($dotransfer) {
251         dotransfer( $itemnumber, $fbr, $tbr );
252
253         # don't need to update MARC anymore, we do it in batch now
254         $messages->{'WasTransfered'} = 1;
255     }
256     return ( $dotransfer, $messages, $biblio );
257 }
258
259 # Not exported
260 # FIXME - This is only used in &transferbook. Why bother making it a
261 # separate function?
262 sub dotransfer {
263     my ( $itm, $fbr, $tbr ) = @_;
264     
265     my $dbh = C4::Context->dbh;
266     $itm = $dbh->quote($itm);
267     $fbr = $dbh->quote($fbr);
268     $tbr = $dbh->quote($tbr);
269     
270     #new entry in branchtransfers....
271     $dbh->do(
272 "INSERT INTO branchtransfers (itemnumber, frombranch, datesent, tobranch)
273                     VALUES ($itm, $fbr, now(), $tbr)"
274     );
275
276     #update holdingbranch in items .....
277       $dbh->do(
278           "UPDATE items set holdingbranch = $tbr WHERE items.itemnumber = $itm");
279     &ModDateLastSeen($itm);
280     &domarctransfer( $dbh, $itm );
281     return;
282 }
283
284 ##New sub to dotransfer in marc tables as well. Not exported -TG 10/04/2006
285 sub domarctransfer {
286     my ( $dbh, $itemnumber ) = @_;
287     $itemnumber =~ s /\'//g;    ##itemnumber seems to come with quotes-TG
288     my $sth =
289       $dbh->prepare(
290         "select biblionumber,holdingbranch from items where itemnumber=$itemnumber"
291       );
292     $sth->execute();
293     while ( my ( $biblionumber, $holdingbranch ) = $sth->fetchrow ) {
294         &ModItemInMarconefield( $biblionumber, $itemnumber,
295             'items.holdingbranch', $holdingbranch );
296     }
297     return;
298 }
299
300 =head2 CanBookBeIssued
301
302 Check if a book can be issued.
303
304 my ($issuingimpossible,$needsconfirmation) = CanBookBeIssued($borrower,$barcode,$year,$month,$day);
305
306 =over 4
307
308 =item C<$borrower> hash with borrower informations (from GetMemberDetails)
309
310 =item C<$barcode> is the bar code of the book being issued.
311
312 =item C<$year> C<$month> C<$day> contains the date of the return (in case it's forced by "stickyduedate".
313
314 =back
315
316 Returns :
317
318 =over 4
319
320 =item C<$issuingimpossible> a reference to a hash. It contains reasons why issuing is impossible.
321 Possible values are :
322
323 =back
324
325 =head3 INVALID_DATE 
326
327 sticky due date is invalid
328
329 =head3 GNA
330
331 borrower gone with no address
332
333 =head3 CARD_LOST
334
335 borrower declared it's card lost
336
337 =head3 DEBARRED
338
339 borrower debarred
340
341 =head3 UNKNOWN_BARCODE
342
343 barcode unknown
344
345 =head3 NOT_FOR_LOAN
346
347 item is not for loan
348
349 =head3 WTHDRAWN
350
351 item withdrawn.
352
353 =head3 RESTRICTED
354
355 item is restricted (set by ??)
356
357 C<$issuingimpossible> a reference to a hash. It contains reasons why issuing is impossible.
358 Possible values are :
359
360 =head3 DEBT
361
362 borrower has debts.
363
364 =head3 RENEW_ISSUE
365
366 renewing, not issuing
367
368 =head3 ISSUED_TO_ANOTHER
369
370 issued to someone else.
371
372 =head3 RESERVED
373
374 reserved for someone else.
375
376 =head3 INVALID_DATE
377
378 sticky due date is invalid
379
380 =head3 TOO_MANY
381
382 if the borrower borrows to much things
383
384 =cut
385
386 # check if a book can be issued.
387 # returns an array with errors if any
388
389 sub TooMany ($$) {
390     my $borrower        = shift;
391     my $biblionumber = shift;
392     my $cat_borrower    = $borrower->{'categorycode'};
393     my $branch_borrower = $borrower->{'branchcode'};
394     my $dbh             = C4::Context->dbh;
395
396     my $sth =
397       $dbh->prepare('select itemtype from biblioitems where biblionumber = ?');
398     $sth->execute($biblionumber);
399     my $type = $sth->fetchrow;
400     $sth =
401       $dbh->prepare(
402 'select * from issuingrules where categorycode = ? and itemtype = ? and branchcode = ?'
403       );
404
405 #     my $sth2 = $dbh->prepare("select COUNT(*) from issues i, biblioitems s where i.borrowernumber = ? and i.returndate is null and i.itemnumber = s.biblioitemnumber and s.itemtype like ?");
406     my $sth2 =
407       $dbh->prepare(
408 "select COUNT(*) from issues i, biblioitems s1, items s2 where i.borrowernumber = ? and i.returndate is null and i.itemnumber = s2.itemnumber and s1.itemtype like ? and s1.biblioitemnumber = s2.biblioitemnumber"
409       );
410     my $sth3 =
411       $dbh->prepare(
412 'select COUNT(*) from issues where borrowernumber = ? and returndate is null'
413       );
414     my $alreadyissued;
415
416     # check the 3 parameters
417     $sth->execute( $cat_borrower, $type, $branch_borrower );
418     my $result = $sth->fetchrow_hashref;
419
420     #    warn "==>".$result->{maxissueqty};
421
422 # Currently, using defined($result) ie on an entire hash reports whether memory
423 # for that aggregate has ever been allocated. As $result is used all over the place
424 # it would rarely return as undefined.
425     if ( defined( $result->{maxissueqty} ) ) {
426         $sth2->execute( $borrower->{'borrowernumber'}, "%$type%" );
427         my $alreadyissued = $sth2->fetchrow;
428         if ( $result->{'maxissueqty'} <= $alreadyissued ) {
429             return ( "a $alreadyissued / ".( $result->{maxissueqty} + 0 ) );
430         }
431         else {
432             return;
433         }
434     }
435
436     # check for branch=*
437     $sth->execute( $cat_borrower, $type, "" );
438     $result = $sth->fetchrow_hashref;
439     if ( defined( $result->{maxissueqty} ) ) {
440         $sth2->execute( $borrower->{'borrowernumber'}, "%$type%" );
441         my $alreadyissued = $sth2->fetchrow;
442         if ( $result->{'maxissueqty'} <= $alreadyissued ) {
443             return ( "b $alreadyissued / ".( $result->{maxissueqty} + 0 ) );
444         }
445         else {
446             return;
447         }
448     }
449
450     # check for itemtype=*
451     $sth->execute( $cat_borrower, "*", $branch_borrower );
452     $result = $sth->fetchrow_hashref;
453     if ( defined( $result->{maxissueqty} ) ) {
454         $sth3->execute( $borrower->{'borrowernumber'} );
455         my ($alreadyissued) = $sth3->fetchrow;
456         if ( $result->{'maxissueqty'} <= $alreadyissued ) {
457
458 #        warn "HERE : $alreadyissued / ($result->{maxissueqty} for $borrower->{'borrowernumber'}";
459             return ( "c $alreadyissued / " . ( $result->{maxissueqty} + 0 ) );
460         }
461         else {
462             return;
463         }
464     }
465
466     # check for borrowertype=*
467     $sth->execute( "*", $type, $branch_borrower );
468     $result = $sth->fetchrow_hashref;
469     if ( defined( $result->{maxissueqty} ) ) {
470         $sth2->execute( $borrower->{'borrowernumber'}, "%$type%" );
471         my $alreadyissued = $sth2->fetchrow;
472         if ( $result->{'maxissueqty'} <= $alreadyissued ) {
473             return ( "d $alreadyissued / " . ( $result->{maxissueqty} + 0 ) );
474         }
475         else {
476             return;
477         }
478     }
479
480     $sth->execute( "*", "*", $branch_borrower );
481     $result = $sth->fetchrow_hashref;
482     if ( defined( $result->{maxissueqty} ) ) {
483         $sth3->execute( $borrower->{'borrowernumber'} );
484         my $alreadyissued = $sth3->fetchrow;
485         if ( $result->{'maxissueqty'} <= $alreadyissued ) {
486             return ( "e $alreadyissued / " . ( $result->{maxissueqty} + 0 ) );
487         }
488         else {
489             return;
490         }
491     }
492
493     $sth->execute( "*", $type, "" );
494     $result = $sth->fetchrow_hashref;
495     if ( defined( $result->{maxissueqty} ) && $result->{maxissueqty} >= 0 ) {
496         $sth2->execute( $borrower->{'borrowernumber'}, "%$type%" );
497         my $alreadyissued = $sth2->fetchrow;
498         if ( $result->{'maxissueqty'} <= $alreadyissued ) {
499             return ( "f $alreadyissued / " . ( $result->{maxissueqty} + 0 ) );
500         }
501         else {
502             return;
503         }
504     }
505
506     $sth->execute( $cat_borrower, "*", "" );
507     $result = $sth->fetchrow_hashref;
508     if ( defined( $result->{maxissueqty} ) ) {
509         $sth2->execute( $borrower->{'borrowernumber'}, "%$type%" );
510         my $alreadyissued = $sth2->fetchrow;
511         if ( $result->{'maxissueqty'} <= $alreadyissued ) {
512             return ( "g $alreadyissued / " . ( $result->{maxissueqty} + 0 ) );
513         }
514         else {
515             return;
516         }
517     }
518
519     $sth->execute( "*", "*", "" );
520     $result = $sth->fetchrow_hashref;
521     if ( defined( $result->{maxissueqty} ) ) {
522         $sth3->execute( $borrower->{'borrowernumber'} );
523         my $alreadyissued = $sth3->fetchrow;
524         if ( $result->{'maxissueqty'} <= $alreadyissued ) {
525             return ( "h $alreadyissued / " . ( $result->{maxissueqty} + 0 ) );
526         }
527         else {
528             return;
529         }
530     }
531     return;
532 }
533
534 =head2 itemissues
535
536   @issues = &itemissues($biblioitemnumber, $biblio);
537
538 Looks up information about who has borrowed the bookZ<>(s) with the
539 given biblioitemnumber.
540
541 C<$biblio> is ignored.
542
543 C<&itemissues> returns an array of references-to-hash. The keys
544 include the fields from the C<items> table in the Koha database.
545 Additional keys include:
546
547 =over 4
548
549 =item C<date_due>
550
551 If the item is currently on loan, this gives the due date.
552
553 If the item is not on loan, then this is either "Available" or
554 "Cancelled", if the item has been withdrawn.
555
556 =item C<card>
557
558 If the item is currently on loan, this gives the card number of the
559 patron who currently has the item.
560
561 =item C<timestamp0>, C<timestamp1>, C<timestamp2>
562
563 These give the timestamp for the last three times the item was
564 borrowed.
565
566 =item C<card0>, C<card1>, C<card2>
567
568 The card number of the last three patrons who borrowed this item.
569
570 =item C<borrower0>, C<borrower1>, C<borrower2>
571
572 The borrower number of the last three patrons who borrowed this item.
573
574 =back
575
576 =cut
577
578 #'
579 sub itemissues {
580     my ( $bibitem, $biblio ) = @_;
581     my $dbh = C4::Context->dbh;
582
583     # FIXME - If this function die()s, the script will abort, and the
584     # user won't get anything; depending on how far the script has
585     # gotten, the user might get a blank page. It would be much better
586     # to at least print an error message. The easiest way to do this
587     # is to set $SIG{__DIE__}.
588     my $sth =
589       $dbh->prepare("Select * from items where items.biblioitemnumber = ?")
590       || die $dbh->errstr;
591     my $i = 0;
592     my @results;
593
594     $sth->execute($bibitem) || die $sth->errstr;
595
596     while ( my $data = $sth->fetchrow_hashref ) {
597
598         # Find out who currently has this item.
599         # FIXME - Wouldn't it be better to do this as a left join of
600         # some sort? Currently, this code assumes that if
601         # fetchrow_hashref() fails, then the book is on the shelf.
602         # fetchrow_hashref() can fail for any number of reasons (e.g.,
603         # database server crash), not just because no items match the
604         # search criteria.
605         my $sth2 = $dbh->prepare(
606             "SELECT * FROM issues
607                 LEFT JOIN borrowers ON issues.borrowernumber = borrowers.borrowernumber
608                 WHERE itemnumber = ?
609                     AND returndate IS NULL
610             "
611         );
612
613         $sth2->execute( $data->{'itemnumber'} );
614         if ( my $data2 = $sth2->fetchrow_hashref ) {
615             $data->{'date_due'} = $data2->{'date_due'};
616             $data->{'card'}     = $data2->{'cardnumber'};
617             $data->{'borrower'} = $data2->{'borrowernumber'};
618         }
619         else {
620             if ( $data->{'wthdrawn'} eq '1' ) {
621                 $data->{'date_due'} = 'Cancelled';
622             }
623             else {
624                 $data->{'date_due'} = 'Available';
625             }    # else
626         }    # else
627
628         $sth2->finish;
629
630         # Find the last 3 people who borrowed this item.
631         $sth2 = $dbh->prepare(
632             "SELECT * FROM issues, borrowers
633                 LEFT JOIN borrowers ON  issues.borrowernumber = borrowers.borrowernumber
634                 WHERE itemnumber = ?
635                 AND returndate IS NOT NULL
636                 ORDER BY returndate DESC,timestamp DESC"
637         );
638
639 #        $sth2 = $dbh->prepare("
640 #            SELECT *
641 #            FROM issues
642 #                LEFT JOIN borrowers ON issues.borrowernumber = borrowers.borrowernumber
643 #            WHERE   itemnumber = ?
644 #                AND returndate is not NULL
645 #            ORDER BY returndate DESC,timestamp DESC
646 #        ");
647
648         $sth2->execute( $data->{'itemnumber'} );
649         for ( my $i2 = 0 ; $i2 < 2 ; $i2++ )
650         {    # FIXME : error if there is less than 3 pple borrowing this item
651             if ( my $data2 = $sth2->fetchrow_hashref ) {
652                 $data->{"timestamp$i2"} = $data2->{'timestamp'};
653                 $data->{"card$i2"}      = $data2->{'cardnumber'};
654                 $data->{"borrower$i2"}  = $data2->{'borrowernumber'};
655             }    # if
656         }    # for
657
658         $sth2->finish;
659         $results[$i] = $data;
660         $i++;
661     }
662
663     $sth->finish;
664     return (@results);
665 }
666
667 =head2 CanBookBeIssued
668
669 $issuingimpossible, $needsconfirmation = 
670         CanBookBeIssued( $borrower, $barcode, $year, $month, $day, $inprocess );
671
672 C<$issuingimpossible> and C<$needsconfirmation> are some hashref.
673
674 =cut
675
676 sub CanBookBeIssued {
677     my ( $borrower, $barcode, $year, $month, $day, $inprocess ) = @_;
678     my %needsconfirmation;    # filled with problems that needs confirmations
679     my %issuingimpossible;    # filled with problems that causes the issue to be IMPOSSIBLE
680     my $item = GetItem(GetItemnumberFromBarcode( $barcode ));
681     my $issue = GetItemIssue($item->{itemnumber});
682     my $dbh             = C4::Context->dbh;
683
684     #
685     # DUE DATE is OK ?
686     #
687     my ( $duedate, $invalidduedate ) = fixdate( $year, $month, $day );
688     $issuingimpossible{INVALID_DATE} = 1 if ($invalidduedate);
689
690     #
691     # BORROWER STATUS
692     #
693     if ( $borrower->{flags}->{GNA} ) {
694         $issuingimpossible{GNA} = 1;
695     }
696     if ( $borrower->{flags}->{'LOST'} ) {
697         $issuingimpossible{CARD_LOST} = 1;
698     }
699     if ( $borrower->{flags}->{'DBARRED'} ) {
700         $issuingimpossible{DEBARRED} = 1;
701     }
702     if ( Date_to_Days(Today) > 
703         Date_to_Days( split "-", $borrower->{'dateexpiry'} ) )
704     {
705
706         #
707         #if (&Date_Cmp(&ParseDate($borrower->{expiry}),&ParseDate("today"))<0) {
708         $issuingimpossible{EXPIRED} = 1;
709     }
710
711     #
712     # BORROWER STATUS
713     #
714
715     # DEBTS
716     my ($amount) =
717       GetBorrowerAcctRecord( $borrower->{'borrowernumber'}, $duedate );
718     if ( C4::Context->preference("IssuingInProcess") ) {
719         my $amountlimit = C4::Context->preference("noissuescharge");
720         if ( $amount > $amountlimit && !$inprocess ) {
721             $issuingimpossible{DEBT} = sprintf( "%.2f", $amount );
722         }
723         elsif ( $amount <= $amountlimit && !$inprocess ) {
724             $needsconfirmation{DEBT} = sprintf( "%.2f", $amount );
725         }
726     }
727     else {
728         if ( $amount > 0 ) {
729             $needsconfirmation{DEBT} = $amount;
730         }
731     }
732
733     #
734     # JB34 CHECKS IF BORROWERS DONT HAVE ISSUE TOO MANY BOOKS
735     #
736     my $toomany = TooMany( $borrower, $item->{biblionumber} );
737     $needsconfirmation{TOO_MANY} = $toomany if $toomany;
738
739     #
740     # ITEM CHECKING
741     #
742     unless ( $item->{barcode} ) {
743         $issuingimpossible{UNKNOWN_BARCODE} = 1;
744     }
745     if (   $item->{'notforloan'}
746         && $item->{'notforloan'} > 0 )
747     {
748         $issuingimpossible{NOT_FOR_LOAN} = 1;
749     }
750     if ( $item->{'wthdrawn'} && $item->{'wthdrawn'} == 1 )
751     {
752         $issuingimpossible{WTHDRAWN} = 1;
753     }
754     if (   $item->{'restricted'}
755         && $item->{'restricted'} == 1 )
756     {
757         $issuingimpossible{RESTRICTED} = 1;
758     }
759     if ( C4::Context->preference("IndependantBranches") ) {
760         my $userenv = C4::Context->userenv;
761         if ( ($userenv) && ( $userenv->{flags} != 1 ) ) {
762             $issuingimpossible{NOTSAMEBRANCH} = 1
763               if ( $item->{'holdingbranch'} ne $userenv->{branch} );
764         }
765     }
766
767     #
768     # CHECK IF BOOK ALREADY ISSUED TO THIS BORROWER
769     #
770     if ( $issue->{borrowernumber} && $issue->{borrowernumber} eq $borrower->{'borrowernumber'} )
771     {
772
773         # Already issued to current borrower. Ask whether the loan should
774         # be renewed.
775         my ($CanBookBeRenewed) = CanBookBeRenewed(
776             $borrower->{'borrowernumber'},
777             $item->{'itemnumber'}
778         );
779         if ( $CanBookBeRenewed == 0 ) {    # no more renewals allowed
780             $issuingimpossible{NO_MORE_RENEWALS} = 1;
781         }
782         else {
783
784             #        $needsconfirmation{RENEW_ISSUE} = 1;
785         }
786     }
787     elsif ($issue->{borrowernumber}) {
788
789         # issued to someone else
790         my $currborinfo = GetMemberDetails( $issue->{borrowernumber} );
791
792 #        warn "=>.$currborinfo->{'firstname'} $currborinfo->{'surname'} ($currborinfo->{'cardnumber'})";
793         $needsconfirmation{ISSUED_TO_ANOTHER} =
794 "$currborinfo->{'reservedate'} : $currborinfo->{'firstname'} $currborinfo->{'surname'} ($currborinfo->{'cardnumber'})";
795     }
796
797     # See if the item is on reserve.
798     my ( $restype, $res ) = CheckReserves( $item->{'itemnumber'} );
799     if ($restype) {
800         my $resbor = $res->{'borrowernumber'};
801         if ( $resbor ne $borrower->{'borrowernumber'} && $restype eq "Waiting" )
802         {
803
804             # The item is on reserve and waiting, but has been
805             # reserved by some other patron.
806             my ( $resborrower, $flags ) =
807               GetMemberDetails( $resbor, 0 );
808             my $branches   = GetBranches();
809             my $branchname =
810               $branches->{ $res->{'branchcode'} }->{'branchname'};
811             $needsconfirmation{RESERVE_WAITING} =
812 "$resborrower->{'firstname'} $resborrower->{'surname'} ($resborrower->{'cardnumber'}, $branchname)";
813
814 # CancelReserve(0, $res->{'itemnumber'}, $res->{'borrowernumber'}); Doesn't belong in a checking subroutine.
815         }
816         elsif ( $restype eq "Reserved" ) {
817
818             # The item is on reserve for someone else.
819             my ( $resborrower, $flags ) =
820               GetMemberDetails( $resbor, 0 );
821             my $branches   = GetBranches();
822             my $branchname =
823               $branches->{ $res->{'branchcode'} }->{'branchname'};
824             $needsconfirmation{RESERVED} =
825 "$res->{'reservedate'} : $resborrower->{'firstname'} $resborrower->{'surname'} ($resborrower->{'cardnumber'})";
826         }
827     }
828     if ( C4::Context->preference("LibraryName") eq "Horowhenua Library Trust" )
829     {
830         if ( $borrower->{'categorycode'} eq 'W' ) {
831             my %issuingimpossible;
832             return ( \%issuingimpossible, \%needsconfirmation );
833         }
834         else {
835             return ( \%issuingimpossible, \%needsconfirmation );
836         }
837     }
838     else {
839         return ( \%issuingimpossible, \%needsconfirmation );
840     }
841 }
842
843 =head2 AddIssue
844
845 Issue a book. Does no check, they are done in CanBookBeIssued. If we reach this sub, it means the user confirmed if needed.
846
847 &AddIssue($borrower,$barcode,$date)
848
849 =over 4
850
851 =item C<$borrower> hash with borrower informations (from GetMemberDetails)
852
853 =item C<$barcode> is the bar code of the book being issued.
854
855 =item C<$date> contains the max date of return. calculated if empty.
856
857 AddIssue does the following things :
858 - step 0�: check that there is a borrowernumber & a barcode provided
859 - check for RENEWAL (book issued & being issued to the same patron)
860     - renewal YES = Calculate Charge & renew
861     - renewal NO  = 
862         * BOOK ACTUALLY ISSUED ? do a return if book is actually issued (but to someone else)
863         * RESERVE PLACED ?
864             - fill reserve if reserve to this patron
865             - cancel reserve or not, otherwise
866         * TRANSFERT PENDING ?
867             - complete the transfert
868         * ISSUE THE BOOK
869
870 =back
871
872 =cut
873
874 sub AddIssue {
875     my ( $borrower, $barcode, $date, $cancelreserve ) = @_;
876     
877     my $dbh = C4::Context->dbh;
878 if ($borrower and $barcode){
879 #   my ($borrower, $flags) = &GetMemberDetails($borrowernumber, 0);
880     # find which item we issue
881     my $item = GetItem('', $barcode);
882     
883     # get actual issuing if there is one
884     my $actualissue = GetItemIssue( $item->{itemnumber});
885     
886     # get biblioinformation for this item
887     my $biblio = GetBiblioFromItemNumber($item->{itemnumber});
888
889 #
890 # check if we just renew the issue.
891 #
892     if ( $actualissue->{borrowernumber} eq $borrower->{'borrowernumber'} ) {
893         # we renew, do we need to add some charge ?
894         my ( $charge, $itemtype ) = GetIssuingCharges(
895             $item->{'itemnumber'},
896             $borrower->{'borrowernumber'}
897         );
898         if ( $charge > 0 ) {
899             AddIssuingCharge(
900                 $item->{'itemnumber'},
901                 $borrower->{'borrowernumber'}, $charge
902             );
903             $item->{'charge'} = $charge;
904         }
905         &UpdateStats(
906             C4::Context->userenv->{'branch'},
907             'renew',                        $charge,
908             '',                             $item->{'itemnumber'},
909             $biblio->{'itemtype'}, $borrower->{'borrowernumber'}
910         );
911         AddRenewal(
912             $borrower->{'borrowernumber'},
913             $item->{'itemnumber'}
914         );
915     }
916     else {# it's NOT a renewal
917         if ( $actualissue->{borrowernumber}) {
918             # This book is currently on loan, but not to the person
919             # who wants to borrow it now. mark it returned before issuing to the new borrower
920             AddReturn(
921                 $item->{'barcode'},
922                 C4::Context->userenv->{'branch'}
923             );
924         }
925
926         # See if the item is on reserve.
927         my ( $restype, $res ) =
928           CheckReserves( $item->{'itemnumber'} );
929         if ($restype) {
930             my $resbor = $res->{'borrowernumber'};
931             if ( $resbor eq $borrower->{'borrowernumber'} ) {
932
933                 # The item is reserved by the current patron
934                 FillReserve($res);
935             }
936             elsif ( $restype eq "Waiting" ) {
937
938                 # warn "Waiting";
939                 # The item is on reserve and waiting, but has been
940                 # reserved by some other patron.
941                 my ( $resborrower, $flags ) = GetMemberDetails( $resbor, 0 );
942                 my $branches   = GetBranches();
943                 my $branchname =
944                   $branches->{ $res->{'branchcode'} }->{'branchname'};
945                 if ($cancelreserve) {
946                     CancelReserve( 0, $res->{'itemnumber'},
947                         $res->{'borrowernumber'} );
948                 }
949                 else {
950
951        # set waiting reserve to first in reserve queue as book isn't waiting now
952                     UpdateReserve(
953                         1,
954                         $res->{'biblionumber'},
955                         $res->{'borrowernumber'},
956                         $res->{'branchcode'}
957                     );
958                 }
959             }
960             elsif ( $restype eq "Reserved" ) {
961
962                 # warn "Reserved";
963                 # The item is reserved by someone else.
964                 my ( $resborrower, $flags ) =
965                   GetMemberDetails( $resbor, 0 );
966                 my $branches   = GetBranches();
967                 my $branchname =
968                   $branches->{ $res->{'branchcode'} }->{'branchname'};
969                 if ($cancelreserve) { # cancel reserves on this item
970                     CancelReserve( 0, $res->{'itemnumber'},
971                         $res->{'borrowernumber'} );
972                 }
973             }
974         }
975
976         # Starting process for transfer job (checking transfert and validate it if we have one)
977             my ($datesent) = GetTransfers($item->{'itemnumber'});
978             if ($datesent) {
979         #       updating line of branchtranfert to finish it, and changing the to branch value, implement a comment for lisibility of this case (maybe for stats ....)
980             my $sth =
981                     $dbh->prepare(
982                     "UPDATE branchtransfers 
983                         SET datearrived = now(),
984                         tobranch = ?,
985                         comments = 'Forced branchtransfert'
986                     WHERE itemnumber= ? AND datearrived IS NULL"
987                     );
988                     $sth->execute(C4::Context->userenv->{'branch'},$item->{'itemnumber'});
989                     $sth->finish;
990             }
991
992         # Record in the database the fact that the book was issued.
993         my $sth =
994           $dbh->prepare(
995                 "INSERT INTO issues 
996                     (borrowernumber, itemnumber,issuedate, date_due, branchcode)
997                 VALUES (?,?,?,?,?)"
998           );
999         my $loanlength = GetLoanLength(
1000             $borrower->{'categorycode'},
1001             $biblio->{'itemtype'},
1002             $borrower->{'branchcode'}
1003         );
1004         my $datedue  = time + ($loanlength) * 86400;
1005         my @datearr  = localtime($datedue);
1006         my $dateduef =
1007             ( 1900 + $datearr[5] ) . "-"
1008           . ( $datearr[4] + 1 ) . "-"
1009           . $datearr[3];
1010         if ($date) {
1011             $dateduef = $date;
1012         }
1013
1014        # if ReturnBeforeExpiry ON the datedue can't be after borrower expirydate
1015         if ( C4::Context->preference('ReturnBeforeExpiry')
1016             && $dateduef gt $borrower->{dateexpiry} )
1017         {
1018             $dateduef = $borrower->{dateexpiry};
1019         }
1020         $sth->execute(
1021             $borrower->{'borrowernumber'},
1022             $item->{'itemnumber'},
1023             strftime( "%Y-%m-%d", localtime ),$dateduef, C4::Context->userenv->{'branch'}
1024         );
1025         $sth->finish;
1026         $item->{'issues'}++;
1027         $sth =
1028           $dbh->prepare(
1029             "UPDATE items SET issues=?, holdingbranch=?, itemlost=0, datelastborrowed  = now() WHERE itemnumber=?");
1030         $sth->execute(
1031             $item->{'issues'},
1032             C4::Context->userenv->{'branch'},
1033             $item->{'itemnumber'}
1034         );
1035         $sth->finish;
1036         &ModDateLastSeen( $item->{'itemnumber'} );
1037         # If it costs to borrow this book, charge it to the patron's account.
1038         my ( $charge, $itemtype ) = GetIssuingCharges(
1039             $item->{'itemnumber'},
1040             $borrower->{'borrowernumber'}
1041         );
1042         if ( $charge > 0 ) {
1043             AddIssuingCharge(
1044                 $item->{'itemnumber'},
1045                 $borrower->{'borrowernumber'}, $charge
1046             );
1047             $item->{'charge'} = $charge;
1048         }
1049
1050         # Record the fact that this book was issued.
1051         &UpdateStats(
1052             C4::Context->userenv->{'branch'},
1053             'issue',                        $charge,
1054             '',                             $item->{'itemnumber'},
1055             $item->{'itemtype'}, $borrower->{'borrowernumber'}
1056         );
1057     }
1058     
1059     &logaction(C4::Context->userenv->{'number'},"CIRCULATION","ISSUE",$borrower->{'borrowernumber'},$biblio->{'biblionumber'}) 
1060         if C4::Context->preference("IssueLog");
1061   }  
1062 }
1063
1064 =head2 GetLoanLength
1065
1066 Get loan length for an itemtype, a borrower type and a branch
1067
1068 my $loanlength = &GetLoanLength($borrowertype,$itemtype,branchcode)
1069
1070 =cut
1071
1072 sub GetLoanLength {
1073     my ( $borrowertype, $itemtype, $branchcode ) = @_;
1074     my $dbh = C4::Context->dbh;
1075     my $sth =
1076       $dbh->prepare(
1077 "select issuelength from issuingrules where categorycode=? and itemtype=? and branchcode=?"
1078       );
1079
1080 # try to find issuelength & return the 1st available.
1081 # check with borrowertype, itemtype and branchcode, then without one of those parameters
1082     $sth->execute( $borrowertype, $itemtype, $branchcode );
1083     my $loanlength = $sth->fetchrow_hashref;
1084     return $loanlength->{issuelength}
1085       if defined($loanlength) && $loanlength->{issuelength} ne 'NULL';
1086
1087     $sth->execute( $borrowertype, $itemtype, "" );
1088     $loanlength = $sth->fetchrow_hashref;
1089     return $loanlength->{issuelength}
1090       if defined($loanlength) && $loanlength->{issuelength} ne 'NULL';
1091
1092     $sth->execute( $borrowertype, "*", $branchcode );
1093     $loanlength = $sth->fetchrow_hashref;
1094     return $loanlength->{issuelength}
1095       if defined($loanlength) && $loanlength->{issuelength} ne 'NULL';
1096
1097     $sth->execute( "*", $itemtype, $branchcode );
1098     $loanlength = $sth->fetchrow_hashref;
1099     return $loanlength->{issuelength}
1100       if defined($loanlength) && $loanlength->{issuelength} ne 'NULL';
1101
1102     $sth->execute( $borrowertype, "*", "" );
1103     $loanlength = $sth->fetchrow_hashref;
1104     return $loanlength->{issuelength}
1105       if defined($loanlength) && $loanlength->{issuelength} ne 'NULL';
1106
1107     $sth->execute( "*", "*", $branchcode );
1108     $loanlength = $sth->fetchrow_hashref;
1109     return $loanlength->{issuelength}
1110       if defined($loanlength) && $loanlength->{issuelength} ne 'NULL';
1111
1112     $sth->execute( "*", $itemtype, "" );
1113     $loanlength = $sth->fetchrow_hashref;
1114     return $loanlength->{issuelength}
1115       if defined($loanlength) && $loanlength->{issuelength} ne 'NULL';
1116
1117     $sth->execute( "*", "*", "" );
1118     $loanlength = $sth->fetchrow_hashref;
1119     return $loanlength->{issuelength}
1120       if defined($loanlength) && $loanlength->{issuelength} ne 'NULL';
1121
1122     # if no rule is set => 21 days (hardcoded)
1123     return 21;
1124 }
1125
1126 =head2 AddReturn
1127
1128 ($doreturn, $messages, $iteminformation, $borrower) =
1129     &AddReturn($barcode, $branch);
1130
1131 Returns a book.
1132
1133 C<$barcode> is the bar code of the book being returned. C<$branch> is
1134 the code of the branch where the book is being returned.
1135
1136 C<&AddReturn> returns a list of four items:
1137
1138 C<$doreturn> is true iff the return succeeded.
1139
1140 C<$messages> is a reference-to-hash giving the reason for failure:
1141
1142 =over 4
1143
1144 =item C<BadBarcode>
1145
1146 No item with this barcode exists. The value is C<$barcode>.
1147
1148 =item C<NotIssued>
1149
1150 The book is not currently on loan. The value is C<$barcode>.
1151
1152 =item C<IsPermanent>
1153
1154 The book's home branch is a permanent collection. If you have borrowed
1155 this book, you are not allowed to return it. The value is the code for
1156 the book's home branch.
1157
1158 =item C<wthdrawn>
1159
1160 This book has been withdrawn/cancelled. The value should be ignored.
1161
1162 =item C<ResFound>
1163
1164 The item was reserved. The value is a reference-to-hash whose keys are
1165 fields from the reserves table of the Koha database, and
1166 C<biblioitemnumber>. It also has the key C<ResFound>, whose value is
1167 either C<Waiting>, C<Reserved>, or 0.
1168
1169 =back
1170
1171 C<$borrower> is a reference-to-hash, giving information about the
1172 patron who last borrowed the book.
1173
1174 =cut
1175
1176 # FIXME - This API is bogus. There's no need to return $borrower and
1177 # $iteminformation; the caller can ask about those separately, if it
1178 # cares (it'd be inefficient to make two database calls instead of
1179 # one, but &GetMemberDetails and &getiteminformation can be
1180 # memoized if this is an issue).
1181 #
1182 # The ($doreturn, $messages) tuple is redundant: if the return
1183 # succeeded, that's all the caller needs to know. So &AddReturn can
1184 # return 1 and 0 on success and failure, and set
1185 # $C4::Circulation::Circ2::errmsg to indicate the error. Or it can
1186 # return undef for success, and an error message on error (though this
1187 # is more C-ish than Perl-ish).
1188
1189 sub AddReturn {
1190     my ( $barcode, $branch ) = @_;
1191     my $dbh      = C4::Context->dbh;
1192     my $messages;
1193     my $doreturn = 1;
1194     my $borrower;
1195     my $validTransfert = 0;
1196     my $reserveDone = 0;
1197     
1198     # get information on item
1199     my $iteminformation = GetItemIssue( GetItemnumberFromBarcode($barcode));
1200     unless ($iteminformation->{'itemnumber'} ) {
1201         $messages->{'BadBarcode'} = $barcode;
1202         $doreturn = 0;
1203     } else {
1204         # find the borrower
1205         if ( ( not $iteminformation->{borrowernumber} ) && $doreturn ) {
1206             $messages->{'NotIssued'} = $barcode;
1207             $doreturn = 0;
1208         }
1209     
1210         # check if the book is in a permanent collection....
1211         my $hbr      = $iteminformation->{'homebranch'};
1212         my $branches = GetBranches();
1213         if ( $hbr && $branches->{$hbr}->{'PE'} ) {
1214             $messages->{'IsPermanent'} = $hbr;
1215         }
1216     
1217         # check that the book has been cancelled
1218         if ( $iteminformation->{'wthdrawn'} ) {
1219             $messages->{'wthdrawn'} = 1;
1220             $doreturn = 0;
1221         }
1222     
1223     #     new op dev : if the book returned in an other branch update the holding branch
1224     
1225     # update issues, thereby returning book (should push this out into another subroutine
1226         $borrower = GetMemberDetails( $iteminformation->{borrowernumber}, 0 );
1227     
1228     # case of a return of document (deal with issues and holdingbranch)
1229     
1230         if ($doreturn) {
1231             my $sth =
1232             $dbh->prepare(
1233     "update issues set returndate = now() where (borrowernumber = ?) and (itemnumber = ?) and (returndate is null)"
1234             );
1235             $sth->execute( $borrower->{'borrowernumber'},
1236                 $iteminformation->{'itemnumber'} );
1237             $messages->{'WasReturned'} = 1;    # FIXME is the "= 1" right?
1238         }
1239     
1240     # continue to deal with returns cases, but not only if we have an issue
1241     
1242     # the holdingbranch is updated if the document is returned in an other location .
1243     if ( $iteminformation->{'holdingbranch'} ne C4::Context->userenv->{'branch'} )
1244             {
1245                     UpdateHoldingbranch(C4::Context->userenv->{'branch'},$iteminformation->{'itemnumber'});     
1246     #           reload iteminformation holdingbranch with the userenv value
1247                     $iteminformation->{'holdingbranch'} = C4::Context->userenv->{'branch'};
1248             }
1249         ModDateLastSeen( $iteminformation->{'itemnumber'} );
1250         ($borrower) = GetMemberDetails( $iteminformation->{borrowernumber}, 0 );
1251         
1252         # fix up the accounts.....
1253         if ( $iteminformation->{'itemlost'} ) {
1254             $messages->{'WasLost'} = 1;
1255         }
1256     
1257     # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
1258     #     check if we have a transfer for this document
1259         my ($datesent,$frombranch,$tobranch) = GetTransfers( $iteminformation->{'itemnumber'} );
1260     
1261     #     if we have a transfer to do, we update the line of transfers with the datearrived
1262         if ($datesent) {
1263             if ( $tobranch eq C4::Context->userenv->{'branch'} ) {
1264                     my $sth =
1265                     $dbh->prepare(
1266                             "update branchtransfers set datearrived = now() where itemnumber= ? AND datearrived IS NULL"
1267                     );
1268                     $sth->execute( $iteminformation->{'itemnumber'} );
1269                     $sth->finish;
1270     #         now we check if there is a reservation with the validate of transfer if we have one, we can         set it with the status 'W'
1271             SetWaitingStatus( $iteminformation->{'itemnumber'} );
1272             }
1273         else {
1274             $messages->{'WrongTransfer'} = $tobranch;
1275             $messages->{'WrongTransferItem'} = $iteminformation->{'itemnumber'};
1276         }
1277         $validTransfert = 1;
1278         }
1279     
1280     # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # 
1281     # fix up the overdues in accounts...
1282         fixoverduesonreturn( $borrower->{'borrowernumber'},
1283             $iteminformation->{'itemnumber'} );
1284     
1285     # find reserves.....
1286     #     if we don't have a reserve with the status W, we launch the Checkreserves routine
1287         my ( $resfound, $resrec ) =
1288         CheckReserves( $iteminformation->{'itemnumber'} );
1289         if ($resfound) {
1290             $resrec->{'ResFound'}   = $resfound;
1291             $messages->{'ResFound'} = $resrec;
1292             $reserveDone = 1;
1293         }
1294     
1295         # update stats?
1296         # Record the fact that this book was returned.
1297         UpdateStats(
1298             $branch, 'return', '0', '',
1299             $iteminformation->{'itemnumber'},
1300             $iteminformation->{'itemtype'},
1301             $borrower->{'borrowernumber'}
1302         );
1303         
1304         &logaction(C4::Context->userenv->{'number'},"CIRCULATION","RETURN",$iteminformation->{borrowernumber},$iteminformation->{'biblionumber'}) 
1305             if C4::Context->preference("ReturnLog");
1306         
1307         #adding message if holdingbranch is non equal a userenv branch to return the document to homebranch
1308         #we check, if we don't have reserv or transfert for this document, if not, return it to homebranch .
1309         
1310         if ( ($iteminformation->{'holdingbranch'} ne $iteminformation->{'homebranch'}) and not $messages->{'WrongTransfer'} and ($validTransfert ne 1) and ($reserveDone ne 1) ){
1311                     if (C4::Context->preference("AutomaticItemReturn") == 1) {
1312                     dotransfer($iteminformation->{'itemnumber'}, C4::Context->userenv->{'branch'}, $iteminformation->{'homebranch'});
1313                     $messages->{'WasTransfered'} = 1;
1314                     warn "was transfered";
1315                     }
1316         }
1317     }
1318     return ( $doreturn, $messages, $iteminformation, $borrower );
1319 }
1320
1321 =head2 fixoverdueonreturn
1322
1323     &fixoverdueonreturn($brn,$itm);
1324
1325 C<$brn> borrowernumber
1326
1327 C<$itm> itemnumber
1328
1329 =cut
1330
1331 sub fixoverduesonreturn {
1332     my ( $borrowernumber, $item ) = @_;
1333     my $dbh = C4::Context->dbh;
1334
1335     # check for overdue fine
1336     my $sth =
1337       $dbh->prepare(
1338 "SELECT * FROM accountlines WHERE (borrowernumber = ?) AND (itemnumber = ?) AND (accounttype='FU' OR accounttype='O')"
1339       );
1340     $sth->execute( $borrowernumber, $item );
1341
1342     # alter fine to show that the book has been returned
1343     if ( my $data = $sth->fetchrow_hashref ) {
1344         my $usth =
1345           $dbh->prepare(
1346 "UPDATE accountlines SET accounttype='F' WHERE (borrowernumber = ?) AND (itemnumber = ?) AND (accountno = ?)"
1347           );
1348         $usth->execute( $borrowernumber, $item, $data->{'accountno'} );
1349         $usth->finish();
1350     }
1351     $sth->finish();
1352     return;
1353 }
1354
1355 =head2 GetItemIssue
1356
1357 $issues = &GetBorrowerIssue($itemnumber);
1358
1359 Returns patrons currently having a book. nothing if item is not issued atm
1360
1361 C<$itemnumber> is the itemnumber
1362
1363 Returns an array of hashes
1364 =cut
1365
1366 sub GetItemIssue {
1367     my ( $itemnumber) = @_;
1368     return unless $itemnumber;
1369     my $dbh = C4::Context->dbh;
1370     my @GetItemIssues;
1371     
1372     # get today date
1373     my $today = POSIX::strftime("%Y%m%d", localtime);
1374
1375     my $sth = $dbh->prepare(
1376         "SELECT * FROM issues 
1377         LEFT JOIN items ON issues.itemnumber=items.itemnumber
1378     WHERE
1379     issues.itemnumber=?  AND returndate IS NULL ");
1380     $sth->execute($itemnumber);
1381     my $data = $sth->fetchrow_hashref;
1382     my $datedue = $data->{'date_due'};
1383     $datedue =~ s/-//g;
1384     if ( $datedue < $today ) {
1385         $data->{'overdue'} = 1;
1386     }
1387     $data->{'itemnumber'} = $itemnumber; # fill itemnumber, in case item is not on issue
1388     $sth->finish;
1389     return ($data);
1390 }
1391
1392 =head2 GetItemIssues
1393
1394 $issues = &GetBorrowerIssues($itemnumber, $history);
1395
1396 Returns patrons that have issued a book
1397
1398 C<$itemnumber> is the itemnumber
1399 C<$history> is 0 if you want actuel "issuer" (if it exist) and 1 if you want issues history
1400
1401 Returns an array of hashes
1402 =cut
1403
1404 sub GetItemIssues {
1405     my ( $itemnumber,$history ) = @_;
1406     my $dbh = C4::Context->dbh;
1407     my @GetItemIssues;
1408     
1409     # get today date
1410     my $today = POSIX::strftime("%Y%m%d", localtime);
1411
1412     my $sth = $dbh->prepare(
1413         "SELECT * FROM issues 
1414     WHERE
1415     itemnumber=?".($history?"":" AND returndate IS NULL ").
1416     "ORDER BY issues.date_due DESC"
1417     );
1418     $sth->execute($itemnumber);
1419     while ( my $data = $sth->fetchrow_hashref ) {
1420         my $datedue = $data->{'date_due'};
1421         $datedue =~ s/-//g;
1422         if ( $datedue < $today ) {
1423             $data->{'overdue'} = 1;
1424         }
1425         my $itemnumber = $data->{'itemnumber'};
1426
1427         push @GetItemIssues, $data;
1428     }
1429     $sth->finish;
1430     return ( \@GetItemIssues );
1431 }
1432
1433 =head2 GetBorrowerIssues
1434
1435 $issues = &GetBorrowerIssues($borrower);
1436
1437 Returns a list of books currently on loan to a patron.
1438
1439 C<$borrower->{borrowernumber}> is the borrower number of the patron
1440 whose issues we want to list.
1441
1442 C<&GetBorrowerIssues> returns a PHP-style array: C<$issues> is a
1443 reference-to-hash whose keys are integers in the range 1...I<n>, where
1444 I<n> is the number of items on issue (either today or before today).
1445 C<$issues-E<gt>{I<n>}> is a reference-to-hash whose keys are all of
1446 the fields of the biblio, biblioitems, items, and issues fields of the
1447 Koha database for that particular item.
1448
1449 =cut
1450
1451 sub GetBorrowerIssues {
1452     my ( $borrower ) = @_;
1453     my $dbh = C4::Context->dbh;
1454     my @GetBorrowerIssues;
1455     # get today date
1456     my $today = POSIX::strftime("%Y%m%d", localtime);
1457
1458     my $sth = $dbh->prepare(
1459         "SELECT * FROM issues 
1460     LEFT JOIN items ON issues.itemnumber=items.itemnumber
1461     LEFT JOIN biblio ON     items.biblionumber=biblio.biblionumber 
1462     LEFT JOIN biblioitems ON items.biblioitemnumber=biblioitems.biblioitemnumber
1463     WHERE
1464     borrowernumber=? AND returndate IS NULL
1465     ORDER BY issues.date_due"
1466     );
1467     $sth->execute($borrower->{'borrowernumber'});
1468     while ( my $data = $sth->fetchrow_hashref ) {
1469         my $datedue = $data->{'date_due'};
1470         $datedue =~ s/-//g;
1471         if ( $datedue < $today ) {
1472             $data->{'overdue'} = 1;
1473         }
1474         my $itemnumber = $data->{'itemnumber'};
1475
1476         push @GetBorrowerIssues, $data;
1477     }
1478     $sth->finish;
1479     return ( \@GetBorrowerIssues );
1480 }
1481
1482 =head2 GetBiblioIssues
1483
1484 $issues = GetBiblioIssues($biblionumber);
1485
1486 this function get all issues from a biblionumber.
1487
1488 Return:
1489 C<$issues> is a reference to array which each value is ref-to-hash. This ref-to-hash containts all column from
1490 tables issues and the firstname,surname & cardnumber from borrowers.
1491
1492 =cut
1493
1494 sub GetBiblioIssues {
1495     my $biblionumber = shift;
1496     return undef unless $biblionumber;
1497     my $dbh   = C4::Context->dbh;
1498     my $query = "
1499         SELECT issues.*,biblio.biblionumber,biblio.title, biblio.author,borrowers.cardnumber,borrowers.surname,borrowers.firstname
1500         FROM issues
1501             LEFT JOIN borrowers ON borrowers.borrowernumber = issues.borrowernumber
1502             LEFT JOIN items ON issues.itemnumber = items.itemnumber
1503             LEFT JOIN biblioitems ON items.itemnumber = biblioitems.biblioitemnumber
1504             LEFT JOIN biblio ON biblio.biblionumber = items.biblioitemnumber
1505         WHERE biblio.biblionumber = ?
1506         ORDER BY issues.timestamp
1507     ";
1508     my $sth = $dbh->prepare($query);
1509     $sth->execute($biblionumber);
1510
1511     my @issues;
1512     while ( my $data = $sth->fetchrow_hashref ) {
1513         push @issues, $data;
1514     }
1515     return \@issues;
1516 }
1517
1518 =head2 CanBookBeRenewed
1519
1520 $ok = &CanBookBeRenewed($borrowernumber, $itemnumber);
1521
1522 Find out whether a borrowed item may be renewed.
1523
1524 C<$dbh> is a DBI handle to the Koha database.
1525
1526 C<$borrowernumber> is the borrower number of the patron who currently
1527 has the item on loan.
1528
1529 C<$itemnumber> is the number of the item to renew.
1530
1531 C<$CanBookBeRenewed> returns a true value iff the item may be renewed. The
1532 item must currently be on loan to the specified borrower; renewals
1533 must be allowed for the item's type; and the borrower must not have
1534 already renewed the loan.
1535
1536 =cut
1537
1538 sub CanBookBeRenewed {
1539
1540     # check renewal status
1541     my ( $borrowernumber, $itemnumber ) = @_;
1542     my $dbh       = C4::Context->dbh;
1543     my $renews    = 1;
1544     my $renewokay = 0;
1545
1546     # Look in the issues table for this item, lent to this borrower,
1547     # and not yet returned.
1548
1549     # FIXME - I think this function could be redone to use only one SQL call.
1550     my $sth1 = $dbh->prepare(
1551         "SELECT * FROM issues
1552             WHERE borrowernumber = ?
1553             AND itemnumber = ?
1554             AND returndate IS NULL"
1555     );
1556     $sth1->execute( $borrowernumber, $itemnumber );
1557     if ( my $data1 = $sth1->fetchrow_hashref ) {
1558
1559         # Found a matching item
1560
1561         # See if this item may be renewed. This query is convoluted
1562         # because it's a bit messy: given the item number, we need to find
1563         # the biblioitem, which gives us the itemtype, which tells us
1564         # whether it may be renewed.
1565         my $sth2 = $dbh->prepare(
1566             "SELECT renewalsallowed FROM items
1567                 LEFT JOIN biblioitems on items.biblioitemnumber = biblioitems.biblioitemnumber
1568                 LEFT JOIN itemtypes ON biblioitems.itemtype = itemtypes.itemtype
1569                 WHERE items.itemnumber = ?
1570                 "
1571         );
1572         $sth2->execute($itemnumber);
1573         if ( my $data2 = $sth2->fetchrow_hashref ) {
1574             $renews = $data2->{'renewalsallowed'};
1575         }
1576         if ( $renews && $renews > $data1->{'renewals'} ) {
1577             $renewokay = 1;
1578         }
1579         $sth2->finish;
1580         my ( $resfound, $resrec ) = C4::Reserves2::CheckReserves($itemnumber);
1581         if ($resfound) {
1582             $renewokay = 0;
1583         }
1584         ( $resfound, $resrec ) = C4::Reserves2::CheckReserves($itemnumber);
1585         if ($resfound) {
1586             $renewokay = 0;
1587         }
1588
1589     }
1590     $sth1->finish;
1591     return ($renewokay);
1592 }
1593
1594 =head2 AddRenewal
1595
1596 &AddRenewal($borrowernumber, $itemnumber, $datedue);
1597
1598 Renews a loan.
1599
1600 C<$borrowernumber> is the borrower number of the patron who currently
1601 has the item.
1602
1603 C<$itemnumber> is the number of the item to renew.
1604
1605 C<$datedue> can be used to set the due date. If C<$datedue> is the
1606 empty string, C<&AddRenewal> will calculate the due date automatically
1607 from the book's item type. If you wish to set the due date manually,
1608 C<$datedue> should be in the form YYYY-MM-DD.
1609
1610 =cut
1611
1612 sub AddRenewal {
1613
1614     my ( $borrowernumber, $itemnumber, $datedue ) = @_;
1615     my $dbh = C4::Context->dbh;
1616
1617     # If the due date wasn't specified, calculate it by adding the
1618     # book's loan length to today's date.
1619     if ( $datedue eq "" ) {
1620
1621         my $biblio = GetBiblioFromItemNumber($itemnumber);
1622         my $borrower = GetMemberDetails( $borrowernumber, 0 );
1623         my $loanlength = GetLoanLength(
1624             $borrower->{'categorycode'},
1625             $biblio->{'itemtype'},
1626             $borrower->{'branchcode'}
1627         );
1628         my ( $due_year, $due_month, $due_day ) =
1629           Add_Delta_DHMS( Today_and_Now(), $loanlength, 0, 0, 0 );
1630         $datedue = "$due_year-$due_month-$due_day";
1631
1632     }
1633
1634     # Find the issues record for this book
1635     my $sth =
1636       $dbh->prepare("SELECT * FROM issues
1637                         WHERE borrowernumber=? 
1638                         AND itemnumber=? 
1639                         AND returndate IS NULL"
1640       );
1641     $sth->execute( $borrowernumber, $itemnumber );
1642     my $issuedata = $sth->fetchrow_hashref;
1643     $sth->finish;
1644
1645     # Update the issues record to have the new due date, and a new count
1646     # of how many times it has been renewed.
1647     my $renews = $issuedata->{'renewals'} + 1;
1648     $sth = $dbh->prepare("UPDATE issues SET date_due = ?, renewals = ?
1649                             WHERE borrowernumber=? 
1650                             AND itemnumber=? 
1651                             AND returndate IS NULL"
1652     );
1653     $sth->execute( $datedue, $renews, $borrowernumber, $itemnumber );
1654     $sth->finish;
1655
1656     # Log the renewal
1657     UpdateStats( C4::Context->userenv->{'branchcode'}, 'renew', '', '', $itemnumber );
1658
1659     # Charge a new rental fee, if applicable?
1660     my ( $charge, $type ) = GetIssuingCharges( $itemnumber, $borrowernumber );
1661     if ( $charge > 0 ) {
1662         my $accountno = getnextacctno( $borrowernumber );
1663         my $item = GetBiblioFromItemNumber($itemnumber);
1664         $sth = $dbh->prepare(
1665                 "INSERT INTO accountlines
1666                     (borrowernumber,accountno,date,amount,
1667                         description,accounttype,amountoutstanding,
1668                     itemnumber)
1669                     VALUES (?,?,now(),?,?,?,?,?)"
1670         );
1671         $sth->execute( $borrowernumber, $accountno, $charge,
1672             "Renewal of Rental Item $item->{'title'} $item->{'barcode'}",
1673             'Rent', $charge, $itemnumber );
1674         $sth->finish;
1675     }
1676 }
1677
1678 =head2 GetIssuingCharges
1679
1680 ($charge, $item_type) = &GetIssuingCharges($itemnumber, $borrowernumber);
1681
1682 Calculate how much it would cost for a given patron to borrow a given
1683 item, including any applicable discounts.
1684
1685 C<$itemnumber> is the item number of item the patron wishes to borrow.
1686
1687 C<$borrowernumber> is the patron's borrower number.
1688
1689 C<&GetIssuingCharges> returns two values: C<$charge> is the rental charge,
1690 and C<$item_type> is the code for the item's item type (e.g., C<VID>
1691 if it's a video).
1692
1693 =cut
1694
1695 sub GetIssuingCharges {
1696
1697     # calculate charges due
1698     my ( $itemnumber, $borrowernumber ) = @_;
1699     my $charge = 0;
1700     my $dbh    = C4::Context->dbh;
1701     my $item_type;
1702
1703     # Get the book's item type and rental charge (via its biblioitem).
1704     my $sth1 = $dbh->prepare(
1705         "SELECT itemtypes.itemtype,rentalcharge FROM items
1706             LEFT JOIN biblioitems ON biblioitems.biblioitemnumber = items.biblioitemnumber
1707             LEFT JOIN itemtypes ON biblioitems.itemtype = itemtypes.itemtype
1708             WHERE items.itemnumber =?
1709         "
1710     );
1711     $sth1->execute($itemnumber);
1712     if ( my $data1 = $sth1->fetchrow_hashref ) {
1713         $item_type = $data1->{'itemtype'};
1714         $charge    = $data1->{'rentalcharge'};
1715         my $q2 = "SELECT rentaldiscount FROM borrowers
1716             LEFT JOIN issuingrules ON borrowers.categorycode = issuingrules.categorycode
1717             WHERE borrowers.borrowernumber = ?
1718             AND issuingrules.itemtype = ?";
1719         my $sth2 = $dbh->prepare($q2);
1720         $sth2->execute( $borrowernumber, $item_type );
1721         if ( my $data2 = $sth2->fetchrow_hashref ) {
1722             my $discount = $data2->{'rentaldiscount'};
1723             if ( $discount eq 'NULL' ) {
1724                 $discount = 0;
1725             }
1726             $charge = ( $charge * ( 100 - $discount ) ) / 100;
1727         }
1728         $sth2->finish;
1729     }
1730
1731     $sth1->finish;
1732     return ( $charge, $item_type );
1733 }
1734
1735 =head2 AddIssuingCharge
1736
1737 &AddIssuingCharge( $itemno, $borrowernumber, $charge )
1738
1739 =cut
1740
1741 sub AddIssuingCharge {
1742     my ( $itemnumber, $borrowernumber, $charge ) = @_;
1743     my $dbh = C4::Context->dbh;
1744     my $nextaccntno = getnextacctno( $borrowernumber );
1745     my $query ="
1746         INSERT INTO accountlines
1747             (borrowernumber, itemnumber, accountno,
1748             date, amount, description, accounttype,
1749             amountoutstanding)
1750         VALUES (?, ?, ?,now(), ?, 'Rental', 'Rent',?)
1751     ";
1752     my $sth = $dbh->prepare($query);
1753     $sth->execute( $borrowernumber, $itemnumber, $nextaccntno, $charge, $charge );
1754     $sth->finish;
1755 }
1756
1757 =head2 GetTransfers
1758
1759 GetTransfers($itemnumber);
1760
1761 =cut
1762
1763 sub GetTransfers {
1764     my ($itemnumber) = @_;
1765
1766     my $dbh = C4::Context->dbh;
1767
1768     my $query = '
1769         SELECT datesent,
1770                frombranch,
1771                tobranch
1772         FROM branchtransfers
1773         WHERE itemnumber = ?
1774           AND datearrived IS NULL
1775         ';
1776     my $sth = $dbh->prepare($query);
1777     $sth->execute($itemnumber);
1778     my @row = $sth->fetchrow_array();
1779     $sth->finish;
1780     return @row;
1781 }
1782
1783
1784 =head2 GetTransfersFromTo
1785
1786 @results = GetTransfersFromTo($frombranch,$tobranch);
1787
1788 Returns the list of pending transfers between $from and $to branch
1789
1790 =cut
1791
1792 sub GetTransfersFromTo {
1793     my ( $frombranch, $tobranch ) = @_;
1794     return unless ( $frombranch && $tobranch );
1795     my $dbh   = C4::Context->dbh;
1796     my $query = "
1797         SELECT itemnumber,datesent,frombranch
1798         FROM   branchtransfers
1799         WHERE  frombranch=?
1800           AND  tobranch=?
1801           AND datearrived IS NULL
1802     ";
1803     my $sth = $dbh->prepare($query);
1804     $sth->execute( $frombranch, $tobranch );
1805     my @gettransfers;
1806
1807     while ( my $data = $sth->fetchrow_hashref ) {
1808         push @gettransfers, $data;
1809     }
1810     $sth->finish;
1811     return (@gettransfers);
1812 }
1813
1814 =head2 DeleteTransfer
1815
1816 &DeleteTransfer($itemnumber);
1817
1818 =cut
1819
1820 sub DeleteTransfer {
1821     my ($itemnumber) = @_;
1822     my $dbh          = C4::Context->dbh;
1823     my $sth          = $dbh->prepare(
1824         "DELETE FROM branchtransfers
1825          WHERE itemnumber=?
1826          AND datearrived IS NULL "
1827     );
1828     $sth->execute($itemnumber);
1829     $sth->finish;
1830 }
1831
1832 =head2 AnonymiseIssueHistory
1833
1834 $rows = AnonymiseIssueHistory($borrowernumber,$date)
1835
1836 This function write NULL instead of C<$borrowernumber> given on input arg into the table issues.
1837 if C<$borrowernumber> is not set, it will delete the issue history for all borrower older than C<$date>.
1838
1839 return the number of affected rows.
1840
1841 =cut
1842
1843 sub AnonymiseIssueHistory {
1844     my $date           = shift;
1845     my $borrowernumber = shift;
1846     my $dbh            = C4::Context->dbh;
1847     my $query          = "
1848         UPDATE issues
1849         SET    borrowernumber = NULL
1850         WHERE  returndate < '".$date."'
1851           AND borrowernumber IS NOT NULL
1852     ";
1853     $query .= " AND borrowernumber = '".$borrowernumber."'" if defined $borrowernumber;
1854     my $rows_affected = $dbh->do($query);
1855     return $rows_affected;
1856 }
1857
1858 =head2 updateWrongTransfer
1859
1860 $items = updateWrongTransfer($itemNumber,$borrowernumber,$waitingAtLibrary,$FromLibrary);
1861
1862 This function validate the line of brachtransfer but with the wrong destination (mistake from a librarian ...), and create a new line in branchtransfer from the actual library to the original library of reservation 
1863
1864 =cut
1865
1866 sub updateWrongTransfer {
1867         my ( $itemNumber,$waitingAtLibrary,$FromLibrary ) = @_;
1868         my $dbh = C4::Context->dbh;     
1869 # first step validate the actual line of transfert .
1870         my $sth =
1871                 $dbh->prepare(
1872                         "update branchtransfers set datearrived = now(),tobranch=?,comments='wrongtransfer' where itemnumber= ? AND datearrived IS NULL"
1873                 );
1874                 $sth->execute($FromLibrary,$itemNumber);
1875                 $sth->finish;
1876
1877 # second step create a new line of branchtransfer to the right location .
1878         dotransfer($itemNumber, $FromLibrary, $waitingAtLibrary);
1879
1880 #third step changing holdingbranch of item
1881         UpdateHoldingbranch($FromLibrary,$itemNumber);
1882 }
1883
1884 =head2 UpdateHoldingbranch
1885
1886 $items = UpdateHoldingbranch($branch,$itmenumber);
1887 Simple methode for updating hodlingbranch in items BDD line
1888 =cut
1889
1890 sub UpdateHoldingbranch {
1891         my ( $branch,$itmenumber ) = @_;
1892         my $dbh = C4::Context->dbh;     
1893 # first step validate the actual line of transfert .
1894         my $sth =
1895                 $dbh->prepare(
1896                         "update items set holdingbranch = ? where itemnumber= ?"
1897                 );
1898                 $sth->execute($branch,$itmenumber);
1899                 $sth->finish;
1900         
1901         
1902 }
1903
1904 1;
1905
1906 __END__
1907
1908 =head1 AUTHOR
1909
1910 Koha Developement team <info@koha.org>
1911
1912 =cut
1913