oups, fixing bug in EXPORT...
[koha.git] / C4 / Circulation / Fines.pm
1 package C4::Circulation::Fines;
2
3 # $Id$
4
5 # Copyright 2000-2002 Katipo Communications
6 #
7 # This file is part of Koha.
8 #
9 # Koha is free software; you can redistribute it and/or modify it under the
10 # terms of the GNU General Public License as published by the Free Software
11 # Foundation; either version 2 of the License, or (at your option) any later
12 # version.
13 #
14 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
15 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
16 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License along with
19 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
20 # Suite 330, Boston, MA  02111-1307 USA
21
22 use strict;
23 require Exporter;
24 use C4::Context;
25 use Date::Calc qw/Today/;
26 use vars qw($VERSION @ISA @EXPORT);
27 use C4::Accounts;
28 use Date::Manip qw/UnixDate/;
29 use C4::Log; # logaction
30
31 # set the version for version checking
32 $VERSION = do { my @v = '$Revision$' =~ /\d+/g; 
33 shift(@v) . "." . join("_", map {sprintf "%03d", $_ } @v); };
34
35 =head1 NAME
36
37 C4::Circulation::Fines - Koha module dealing with fines
38
39 =head1 SYNOPSIS
40
41   use C4::Circulation::Fines;
42
43 =head1 DESCRIPTION
44
45 This module contains several functions for dealing with fines for
46 overdue items. It is primarily used by the 'misc/fines2.pl' script.
47
48 =head1 FUNCTIONS
49
50 =over 2
51
52 =cut
53
54 @ISA    = qw(Exporter);
55 # subs to rename (and maybe merge some...)
56 push @EXPORT, qw(
57         &CalcFine
58         &Getoverdues
59         &CheckAccountLineLevelInfo
60         &CheckAccountLineItemInfo
61         &CheckExistantNotifyid
62         &GetNextIdNotify
63         &GetNotifyId
64         &NumberNotifyId
65         &AmountNotify
66         &UpdateAccountLines
67         &UpdateFine
68         &GetOverdueDelays
69         &GetOverduerules
70         &GetFine
71         &CreateItemAccountLine
72         &ReplacementCost2
73 );
74 # subs to remove
75 push @EXPORT, qw(
76         &BorType
77 );
78
79 #
80 # All subs to move : check that an equivalent don't exist already before moving
81 #
82
83 # subs to move to Circulation.pm
84 push @EXPORT, qw(
85         &GetIssuingRules
86         &GetIssuesIteminfo
87 );
88 # subs to move to Members.pm
89 push @EXPORT, qw(
90         &CheckBorrowerDebarred
91         &UpdateBorrowerDebarred
92 );
93 # subs to move to Biblio.pm
94 push @EXPORT, qw(
95         &GetItems
96         &ReplacementCost
97 );
98
99 =item Getoverdues
100
101   ($count, $overdues) = &Getoverdues();
102
103 Returns the list of all overdue books.
104
105 C<$count> is the number of elements in C<@{$overdues}>.
106
107 C<$overdues> is a reference-to-array. Each element is a
108 reference-to-hash whose keys are the fields of the issues table in the
109 Koha database.
110
111 =cut
112
113 #'
114 sub Getoverdues {
115     my $dbh = C4::Context->dbh;
116     my $sth = $dbh->prepare(
117         "Select * from issues where date_due < now() and returndate is
118   NULL order by borrowernumber "
119     );
120     $sth->execute;
121
122     # FIXME - Use push @results
123     my $i = 0;
124     my @results;
125     while ( my $data = $sth->fetchrow_hashref ) {
126         $results[$i] = $data;
127         $i++;
128     }
129     $sth->finish;
130
131     #  print @results;
132     # FIXME - Bogus API.
133     return ( $i, \@results );
134 }
135
136 =item CalcFine
137
138   ($amount, $chargename, $message) =
139     &CalcFine($itemnumber, $borrowercode, $days_overdue);
140
141 Calculates the fine for a book.
142
143 The issuingrules table in the Koha database is a fine matrix, listing
144 the penalties for each type of patron for each type of item and each branch (e.g., the
145 standard fine for books might be $0.50, but $1.50 for DVDs, or staff
146 members might get a longer grace period between the first and second
147 reminders that a book is overdue).
148
149 The fine is calculated as follows: if it is time for the first
150 reminder, the fine is the value listed for the given (branch, item type,
151 borrower code) combination. If it is time for the second reminder, the
152 fine is doubled. Finally, if it is time to send the account to a
153 collection agency, the fine is set to 5 local monetary units (a really
154 good deal for the patron if the library is in Italy). Otherwise, the
155 fine is 0.
156
157 Note that the way this function is currently implemented, it only
158 returns a nonzero value on the notable days listed above. That is, if
159 the categoryitems entry says to send a first reminder 7 days after the
160 book is due, then if you call C<&CalcFine> 7 days after the book is
161 due, it will give a nonzero fine. If you call C<&CalcFine> the next
162 day, however, it will say that the fine is 0.
163
164 C<$itemnumber> is the book's item number.
165
166 C<$borrowercode> is the borrower code of the patron who currently has
167 the book.
168
169 C<$days_overdue> is the number of days elapsed since the book's due
170 date.
171
172 C<&CalcFine> returns a list of three values:
173
174 C<$amount> is the fine owed by the patron (see above).
175
176 C<$chargename> is the chargename field from the applicable record in
177 the categoryitem table, whatever that is.
178
179 C<$message> is a text message, either "First Notice", "Second Notice",
180 or "Final Notice".
181
182 =cut
183
184 #'
185 sub CalcFine {
186     my ( $itemnumber, $bortype, $difference , $dues  ) = @_;
187     my $dbh = C4::Context->dbh;
188     my $data = GetIssuingRules($itemnumber,$bortype);
189     my $amount = 0;
190     my $printout;
191     my $countspecialday=&GetSpecialHolidays($dues,$itemnumber);
192     my $countrepeatableday=&GetRepeatableHolidays($dues,$itemnumber,$difference);    
193     my $countalldayclosed = $countspecialday + $countrepeatableday;
194     my $daycount = $difference - $countalldayclosed;    
195     my $daycounttotal = $daycount - $data->{'firstremind'};
196         if ($data->{'firstremind'} < $daycount)
197     {
198     $amount   = $daycounttotal*$data->{'fine'};
199     }
200  return ( $amount, $data->{'chargename'}, $printout ,$daycounttotal ,$daycount );
201 }
202
203
204 =item GetSpecialHolidays
205
206 &GetSpecialHolidays($date_dues,$itemnumber);
207
208 return number of special days  between date of the day and date due
209
210 C<$date_dues> is the envisaged date of book return.
211
212 C<$itemnumber> is the book's item number.
213
214 =cut
215
216 sub GetSpecialHolidays {
217 my ($date_dues,$itemnumber) = @_;
218 # calcul the today date
219 my $today = join "-", &Today();
220
221 # return the holdingbranch
222 my $iteminfo=GetIssuesIteminfo($itemnumber);
223 # use sql request to find all date between date_due and today
224 my $dbh = C4::Context->dbh;
225 my $query=qq|SELECT DATE_FORMAT(concat(year,'-',month,'-',day),'%Y-%m-%d')as date 
226 FROM `special_holidays`
227 WHERE DATE_FORMAT(concat(year,'-',month,'-',day),'%Y-%m-%d') >= ?
228 AND   DATE_FORMAT(concat(year,'-',month,'-',day),'%Y-%m-%d') <= ?
229 AND branchcode=?
230 |;
231 my @result=GetWdayFromItemnumber($itemnumber);
232 my @result_date;
233 my $wday;
234 my $dateinsec;
235 my $sth = $dbh->prepare($query);
236 $sth->execute($date_dues,$today,$iteminfo->{'branchcode'});
237
238 while ( my $special_date=$sth->fetchrow_hashref){
239     push (@result_date,$special_date);
240 }
241
242 my $specialdaycount=scalar(@result_date);
243
244     for (my $i=0;$i<scalar(@result_date);$i++){
245         $dateinsec=UnixDate($result_date[$i]->{'date'},"%o");
246         (undef,undef,undef,undef,undef,undef,$wday,undef,undef) =localtime($dateinsec);
247         for (my $j=0;$j<scalar(@result);$j++){
248             if ($wday == ($result[$j]->{'weekday'})){
249             $specialdaycount --;
250             }
251         }
252     }
253
254 return $specialdaycount;
255 }
256
257 =item GetRepeatableHolidays
258
259 &GetRepeatableHolidays($date_dues, $itemnumber, $difference,);
260
261 return number of day closed between date of the day and date due
262
263 C<$date_dues> is the envisaged date of book return.
264
265 C<$itemnumber> is item number.
266
267 C<$difference> numbers of between day date of the day and date due
268
269 =cut
270
271 sub GetRepeatableHolidays{
272 my ($date_dues,$itemnumber,$difference) = @_;
273 my $dateinsec=UnixDate($date_dues,"%o");
274 my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) =localtime($dateinsec);
275 my @result=GetWdayFromItemnumber($itemnumber);
276 my @dayclosedcount;
277 my $j;
278
279 for (my $i=0;$i<scalar(@result);$i++){
280     my $k=$wday;
281
282         for ( $j=0;$j<$difference;$j++){
283             if ($result[$i]->{'weekday'} == $k)
284                     {
285                     push ( @dayclosedcount ,$k);
286             }
287         $k++;
288         ($k=0) if($k eq 7);
289         }
290     }
291 return scalar(@dayclosedcount);
292 }
293
294
295 =item GetWayFromItemnumber
296
297 &Getwdayfromitemnumber($itemnumber);
298
299 return the different week day from repeatable_holidays table
300
301 C<$itemnumber> is  item number.
302
303 =cut
304
305 sub GetWdayFromItemnumber{
306 my($itemnumber)=@_;
307 my $iteminfo=GetIssuesIteminfo($itemnumber);
308 my @result;
309 my $dbh = C4::Context->dbh;
310 my $query = qq|SELECT weekday  
311     FROM repeatable_holidays
312     WHERE branchcode=?
313 |;
314 my $sth = $dbh->prepare($query);
315     #  print $query;
316
317 $sth->execute($iteminfo->{'branchcode'});
318 while ( my $weekday=$sth->fetchrow_hashref){
319     push (@result,$weekday);
320     }
321 return @result;
322 }
323
324
325 =item GetIssuesIteminfo
326
327 &GetIssuesIteminfo($itemnumber);
328
329 return all data from issues about item
330
331 C<$itemnumber> is  item number.
332
333 =cut
334
335 sub GetIssuesIteminfo{
336 my($itemnumber)=@_;
337 my $dbh = C4::Context->dbh;
338 my $query = qq|SELECT *  
339     FROM issues
340     WHERE itemnumber=?
341 |;
342 my $sth = $dbh->prepare($query);
343 $sth->execute($itemnumber);
344 my ($issuesinfo)=$sth->fetchrow_hashref;
345 return $issuesinfo;
346 }
347
348
349 =item UpdateFine
350
351   &UpdateFine($itemnumber, $borrowernumber, $amount, $type, $description);
352
353 (Note: the following is mostly conjecture and guesswork.)
354
355 Updates the fine owed on an overdue book.
356
357 C<$itemnumber> is the book's item number.
358
359 C<$borrowernumber> is the borrower number of the patron who currently
360 has the book on loan.
361
362 C<$amount> is the current amount owed by the patron.
363
364 C<$type> will be used in the description of the fine.
365
366 C<$description> is a string that must be present in the description of
367 the fine. I think this is expected to be a date in DD/MM/YYYY format.
368
369 C<&UpdateFine> looks up the amount currently owed on the given item
370 and sets it to C<$amount>, creating, if necessary, a new entry in the
371 accountlines table of the Koha database.
372
373 =cut
374
375 #'
376 # FIXME - This API doesn't look right: why should the caller have to
377 # specify both the item number and the borrower number? A book can't
378 # be on loan to two different people, so the item number should be
379 # sufficient.
380 sub UpdateFine {
381     my ( $itemnum, $borrowernumber, $amount, $type, $due ) = @_;
382     my $dbh = C4::Context->dbh;
383     # FIXME - What exactly is this query supposed to do? It looks up an
384     # entry in accountlines that matches the given item and borrower
385     # numbers, where the description contains $due, and where the
386     # account type has one of several values, but what does this _mean_?
387     # Does it look up existing fines for this item?
388     # FIXME - What are these various account types? ("FU", "O", "F", "M")
389     my $sth = $dbh->prepare(
390         "Select * from accountlines where itemnumber=? and
391   borrowernumber=? and (accounttype='FU' or accounttype='O' or
392   accounttype='F' or accounttype='M') and description like ?"
393     );
394     $sth->execute( $itemnum, $borrowernumber, "%$due%" );
395
396     if ( my $data = $sth->fetchrow_hashref ) {
397
398         # I think this if-clause deals with the case where we're updating
399         # an existing fine.
400         #    print "in accounts ...";
401     if ( $data->{'amount'} != $amount ) {
402            
403         #      print "updating";
404             my $diff = $amount - $data->{'amount'};
405             my $out  = $data->{'amountoutstanding'} + $diff;
406             my $sth2 = $dbh->prepare(
407                 "update accountlines set date=now(), amount=?,
408       amountoutstanding=?,accounttype='FU' where
409       borrowernumber=? and itemnumber=?
410       and (accounttype='FU' or accounttype='O') and description like ?"
411             );
412             $sth2->execute( $amount, $out, $data->{'borrowernumber'},
413                 $data->{'itemnumber'}, "%$due%" );
414             $sth2->finish;
415         }
416         else {
417
418             #      print "no update needed $data->{'amount'}"
419         }
420     }
421     else {
422
423         # I think this else-clause deals with the case where we're adding
424         # a new fine.
425         my $sth4 = $dbh->prepare(
426             "select title from biblio,items where items.itemnumber=?
427     and biblio.biblionumber=items.biblionumber"
428         );
429         $sth4->execute($itemnum);
430         my $title = $sth4->fetchrow_hashref;
431         $sth4->finish;
432
433 #         #   print "not in account";
434 #         my $sth3 = $dbh->prepare("Select max(accountno) from accountlines");
435 #         $sth3->execute;
436
437 #         # FIXME - Make $accountno a scalar.
438 #         my @accountno = $sth3->fetchrow_array;
439 #         $sth3->finish;
440 #         $accountno[0]++;
441 # begin transaction
442   my $nextaccntno = getnextacctno(undef,$borrowernumber,$dbh);    
443     my $sth2 = $dbh->prepare(
444             "Insert into accountlines
445     (borrowernumber,itemnumber,date,amount,
446     description,accounttype,amountoutstanding,accountno) values
447     (?,?,now(),?,?,'FU',?,?)"
448         );
449         $sth2->execute( $borrowernumber, $itemnum, $amount,
450             "$type $title->{'title'} $due",
451             $amount, $nextaccntno);
452         $sth2->finish;
453     }
454     # logging action
455     &logaction(
456         C4::Context->userenv->{'number'},
457         "FINES",
458         $type,
459         $borrowernumber,
460         "due=".$due."  amount=".$amount." itemnumber=".$itemnum
461         ) if C4::Context->preference("FinesLog");
462
463     $sth->finish;
464 }
465
466 =item BorType
467
468   $borrower = &BorType($borrowernumber);
469
470 Looks up a patron by borrower number.
471
472 C<$borrower> is a reference-to-hash whose keys are all of the fields
473 from the borrowers and categories tables of the Koha database. Thus,
474 C<$borrower> contains all information about both the borrower and
475 category he or she belongs to.
476
477 =cut
478
479 #'
480 sub BorType {
481     my ($borrowernumber) = @_;
482     my $dbh              = C4::Context->dbh;
483     my $sth              = $dbh->prepare(
484         "Select * from borrowers,categories where
485   borrowernumber=? and
486 borrowers.categorycode=categories.categorycode"
487     );
488     $sth->execute($borrowernumber);
489     my $data = $sth->fetchrow_hashref;
490     $sth->finish;
491     return ($data);
492 }
493
494 =item ReplacementCost
495
496   $cost = &ReplacementCost($itemnumber);
497
498 Returns the replacement cost of the item with the given item number.
499
500 =cut
501
502 #'
503 sub ReplacementCost {
504     my ($itemnum) = @_;
505     my $dbh       = C4::Context->dbh;
506     my $sth       =
507       $dbh->prepare("Select replacementprice from items where itemnumber=?");
508     $sth->execute($itemnum);
509
510     # FIXME - Use fetchrow_array or something.
511     my $data = $sth->fetchrow_hashref;
512     $sth->finish;
513     return ( $data->{'replacementprice'} );
514 }
515
516 =item GetFine
517
518 $data->{'sum(amountoutstanding)'} = &GetFine($itemnum,$borrowernumber);
519
520 return the total of fine
521
522 C<$itemnum> is item number
523
524 C<$borrowernumber> is the borrowernumber
525
526 =cut 
527
528
529 sub GetFine {
530     my ( $itemnum, $borrowernumber ) = @_;
531     my $dbh   = C4::Context->dbh();
532     my $query = "SELECT sum(amountoutstanding) FROM accountlines 
533     where accounttype like 'F%'  
534   AND amountoutstanding > 0 AND itemnumber = ? AND borrowernumber=?";
535     my $sth = $dbh->prepare($query);
536     $sth->execute( $itemnum, $borrowernumber );
537     my $data = $sth->fetchrow_hashref();
538     $sth->finish();
539     $dbh->disconnect();
540     return ( $data->{'sum(amountoutstanding)'} );
541 }
542
543
544
545
546 =item GetIssuingRules
547
548 $data = &GetIssuingRules($itemnumber,$categorycode);
549
550 Looks up for all issuingrules an item info 
551
552 C<$itemnumber> is a reference-to-hash whose keys are all of the fields
553 from the borrowers and categories tables of the Koha database. Thus,
554
555 C<$categorycode> contains  information about borrowers category 
556
557 C<$data> contains all information about both the borrower and
558 category he or she belongs to.
559 =cut 
560
561 sub GetIssuingRules {
562    my ($itemnumber,$categorycode)=@_;
563    my $dbh   = C4::Context->dbh();    
564    my $query=qq|SELECT * 
565         FROM items,biblioitems,itemtypes,issuingrules
566         WHERE items.itemnumber=?
567         AND items.biblioitemnumber=biblioitems.biblioitemnumber
568         AND biblioitems.itemtype=itemtypes.itemtype
569         AND issuingrules.itemtype=itemtypes.itemtype
570         AND issuingrules.categorycode=?
571         AND  (items.itemlost <> 1
572         OR items.itemlost is NULL)|;
573     my $sth = $dbh->prepare($query);
574     #  print $query;
575     $sth->execute($itemnumber,$categorycode);
576     my ($data) = $sth->fetchrow_hashref;
577    $sth->finish;
578 return ($data);
579
580 }
581
582
583 sub ReplacementCost2 {
584     my ( $itemnum, $borrowernumber ) = @_;
585     my $dbh   = C4::Context->dbh();
586     my $query = "SELECT amountoutstanding 
587          FROM accountlines
588              WHERE accounttype like 'L'
589          AND amountoutstanding > 0
590          AND itemnumber = ?
591          AND borrowernumber= ?";
592     my $sth = $dbh->prepare($query);
593     $sth->execute( $itemnum, $borrowernumber );
594     my $data = $sth->fetchrow_hashref();
595     $sth->finish();
596     $dbh->disconnect();
597     return ( $data->{'amountoutstanding'} );
598 }
599
600
601 =item GetNextIdNotify
602
603 ($result) = &GetNextIdNotify($reference);
604
605 Returns the new file number
606
607 C<$result> contains the next file number
608
609 C<$reference> contains the beggining of file number
610
611 =cut
612
613
614
615 sub GetNextIdNotify {
616 my ($reference)=@_;
617 my $query=qq|SELECT max(notify_id) 
618          FROM accountlines
619          WHERE notify_id  like \"$reference%\"
620          |;
621 # AND borrowernumber=?|;   
622 my $dbh = C4::Context->dbh;
623 my $sth=$dbh->prepare($query);
624 $sth->execute();
625 my $result=$sth->fetchrow;
626 $sth->finish;
627 my $count;
628     if ($result eq '')
629     {
630     ($result=$reference."01")  ;
631     }else
632     {
633     $count=substr($result,6)+1;
634      
635     if($count<10){
636      ($count = "0".$count);
637      }
638      $result=$reference.$count;
639      }
640 return $result;
641 }
642
643
644 =item AmountNotify
645
646 (@notify) = &AmountNotify($borrowernumber);
647
648 Returns amount for all file per borrowers
649 C<@notify> array contains all file per borrowers
650
651 C<$notify_id> contains the file number for the borrower number nad item number
652
653 =cut
654
655 sub NumberNotifyId{
656     my ($borrowernumber)=@_;
657     my $dbh = C4::Context->dbh;
658     my $env;
659     my $query=qq|    SELECT distinct(notify_id)
660             FROM accountlines
661             WHERE borrowernumber=?|;
662     my @notify;
663     my $sth=$dbh->prepare($query);
664         $sth->execute($borrowernumber);
665           while ( my $numberofotify=$sth->fetchrow_array){
666     push (@notify,$numberofotify);
667     }
668     $sth->finish;
669
670     return (@notify);
671
672 }
673
674 =item AmountNotify
675
676 ($totalnotify) = &AmountNotify($notifyid);
677
678 Returns amount for all file per borrowers
679 C<$notifyid> is the file number
680
681 C<$totalnotify> contains amount of a file
682
683 C<$notify_id> contains the file number for the borrower number nad item number
684
685 =cut
686
687 sub AmountNotify{
688     my ($notifyid)=@_;
689     my $dbh = C4::Context->dbh;
690     my $query=qq|    SELECT sum(amountoutstanding)
691             FROM accountlines
692             WHERE notify_id=?|;
693     my $sth=$dbh->prepare($query);
694         $sth->execute($notifyid);
695           my $totalnotify=$sth->fetchrow;
696     $sth->finish;
697     return ($totalnotify);
698 }
699
700
701 =item GetNotifyId
702
703 ($notify_id) = &GetNotifyId($borrowernumber,$itemnumber);
704
705 Returns the file number per borrower and itemnumber
706
707 C<$borrowernumber> is a reference-to-hash whose keys are all of the fields
708 from the items tables of the Koha database. Thus,
709
710 C<$itemnumber> contains the borrower categorycode
711
712 C<$notify_id> contains the file number for the borrower number nad item number
713
714 =cut
715
716  sub GetNotifyId {
717  my ($borrowernumber,$itemnumber)=@_;
718  my $query=qq|SELECT notify_id 
719            FROM accountlines
720            WHERE borrowernumber=?
721           AND itemnumber=?
722            AND (accounttype='FU' or accounttype='O')|;
723  my $dbh = C4::Context->dbh;
724  my $sth=$dbh->prepare($query);
725  $sth->execute($borrowernumber,$itemnumber);
726  my ($notify_id)=$sth->fetchrow;
727  $sth->finish;
728  return ($notify_id);
729
730  }
731
732 =item CreateItemAccountLine
733
734 () = &CreateItemAccountLine($borrowernumber,$itemnumber,$date,$amount,$description,$accounttype,$amountoutstanding,$timestamp,$notify_id,$level);
735
736 update the account lines with file number or with file level
737
738 C<$items> is a reference-to-hash whose keys are all of the fields
739 from the items tables of the Koha database. Thus,
740
741 C<$itemnumber> contains the item number
742
743 C<$borrowernumber> contains the borrower number
744
745 C<$date> contains the date of the day
746
747 C<$amount> contains item price
748
749 C<$description> contains the descritpion of accounttype 
750
751 C<$accounttype> contains the account type
752
753 C<$amountoutstanding> contains the $amountoutstanding 
754
755 C<$timestamp> contains the timestamp with time and the date of the day
756
757 C<$notify_id> contains the file number
758
759 C<$level> contains the file level
760
761
762 =cut
763
764  sub CreateItemAccountLine {
765   my ($borrowernumber,$itemnumber,$date,$amount,$description,$accounttype,$amountoutstanding,$timestamp,$notify_id,$level)=@_;
766   my $dbh = C4::Context->dbh;
767   my $nextaccntno = getnextacctno(undef,$borrowernumber,$dbh);
768    my $query= qq|INSERT into accountlines  
769          (borrowernumber,accountno,itemnumber,date,amount,description,accounttype,amountoutstanding,timestamp,notify_id,notify_level)
770           VALUES
771              (?,?,?,?,?,?,?,?,?,?,?)|;
772   
773   
774   my $sth=$dbh->prepare($query);
775   $sth->execute($borrowernumber,$nextaccntno,$itemnumber,$date,$amount,$description,$accounttype,$amountoutstanding,$timestamp,$notify_id,$level);
776   $sth->finish;
777  }
778
779 =item UpdateAccountLines
780
781 () = &UpdateAccountLines($notify_id,$notify_level,$borrowernumber,$itemnumber);
782
783 update the account lines with file number or with file level
784
785 C<$items> is a reference-to-hash whose keys are all of the fields
786 from the items tables of the Koha database. Thus,
787
788 C<$itemnumber> contains the item number
789
790 C<$notify_id> contains the file number
791
792 C<$notify_level> contains the file level
793
794 C<$borrowernumber> contains the borrowernumber
795
796 =cut
797
798 sub UpdateAccountLines {
799 my ($notify_id,$notify_level,$borrowernumber,$itemnumber)=@_;
800 my $query;
801 if ($notify_id eq '')
802 {
803
804     $query=qq|UPDATE accountlines
805     SET  notify_level=?
806     WHERE borrowernumber=? AND itemnumber=?
807     AND (accounttype='FU' or accounttype='O')|;
808 }else
809 {
810     $query=qq|UPDATE accountlines
811      SET notify_id=?, notify_level=?
812            WHERE borrowernumber=?
813     AND itemnumber=?
814         AND (accounttype='FU' or accounttype='O')|;
815 }
816  my $dbh = C4::Context->dbh;
817  my $sth=$dbh->prepare($query);
818
819 if ($notify_id eq '')
820 {
821     $sth->execute($notify_level,$borrowernumber,$itemnumber);
822 }else
823 {
824     $sth->execute($notify_id,$notify_level,$borrowernumber,$itemnumber);
825 }
826  $sth->finish;
827
828 }
829
830
831 =item GetItems
832
833 ($items) = &GetItems($itemnumber);
834
835 Returns the list of all delays from overduerules.
836
837 C<$items> is a reference-to-hash whose keys are all of the fields
838 from the items tables of the Koha database. Thus,
839
840 C<$itemnumber> contains the borrower categorycode
841
842 =cut
843
844 sub GetItems {
845     my($itemnumber) = @_;
846     my $query=qq|SELECT *
847              FROM items
848               WHERE itemnumber=?|;
849         my $dbh = C4::Context->dbh;
850         my $sth=$dbh->prepare($query);
851         $sth->execute($itemnumber);
852         my ($items)=$sth->fetchrow_hashref;
853         $sth->finish;
854     return($items);
855 }
856
857 =item GetOverdueDelays
858
859 (@delays) = &GetOverdueDelays($categorycode);
860
861 Returns the list of all delays from overduerules.
862
863 C<@delays> it's an array contains the three delays from overduerules table
864
865 C<$categorycode> contains the borrower categorycode
866
867 =cut
868
869 sub GetOverdueDelays {
870     my($category) = @_;
871     my $dbh = C4::Context->dbh;
872         my $query=qq|SELECT delay1,delay2,delay3
873                 FROM overduerules
874                 WHERE categorycode=?|;
875     my $sth=$dbh->prepare($query);
876         $sth->execute($category);
877         my (@delays)=$sth->fetchrow_array;
878         $sth->finish;
879         return(@delays);
880 }
881
882 =item CheckAccountLineLevelInfo
883
884 ($exist) = &CheckAccountLineLevelInfo($borrowernumber,$itemnumber,$accounttype,notify_level);
885
886 Check and Returns the list of all overdue books.
887
888 C<$exist> contains number of line in accounlines
889 with the same .biblionumber,itemnumber,accounttype,and notify_level
890
891 C<$borrowernumber> contains the borrower number
892
893 C<$itemnumber> contains item number
894
895 C<$accounttype> contains account type
896
897 C<$notify_level> contains the accountline level 
898
899
900 =cut
901
902 sub CheckAccountLineLevelInfo {
903     my($borrowernumber,$itemnumber,$level) = @_;
904     my $dbh = C4::Context->dbh;
905         my $query=    qq|SELECT count(*)
906             FROM accountlines
907             WHERE borrowernumber =?
908             AND itemnumber = ?
909             AND notify_level=?|;
910     my $sth=$dbh->prepare($query);
911         $sth->execute($borrowernumber,$itemnumber,$level);
912         my ($exist)=$sth->fetchrow;
913         $sth->finish;
914         return($exist);
915 }
916
917 =item GetOverduerules
918
919 ($overduerules) = &GetOverduerules($categorycode);
920
921 Returns the value of borrowers (debarred or not) with notify level
922
923 C<$overduerules> return value of debbraed field in overduerules table
924
925 C<$category> contains the borrower categorycode
926
927 C<$notify_level> contains the notify level
928 =cut
929
930
931 sub GetOverduerules{
932     my($category,$notify_level) = @_;
933     my $dbh = C4::Context->dbh;
934         my $query=qq|SELECT debarred$notify_level
935              FROM overduerules
936              WHERE categorycode=?|;
937     my $sth=$dbh->prepare($query);
938         $sth->execute($category);
939         my ($overduerules)=$sth->fetchrow;
940         $sth->finish;
941         return($overduerules);
942 }
943
944
945 =item CheckBorrowerDebarred
946
947 ($debarredstatus) = &CheckBorrowerDebarred($borrowernumber);
948
949 Check if the borrowers is already debarred
950
951 C<$debarredstatus> return 0 for not debarred and return 1 for debarred
952
953 C<$borrowernumber> contains the borrower number
954
955 =cut
956
957
958 sub CheckBorrowerDebarred{
959     my($borrowernumber) = @_;
960     my $dbh = C4::Context->dbh;
961         my $query=qq|SELECT debarred
962               FROM borrowers
963              WHERE borrowernumber=?
964             |;
965     my $sth=$dbh->prepare($query);
966         $sth->execute($borrowernumber);
967         my ($debarredstatus)=$sth->fetchrow;
968         $sth->finish;
969         if ($debarredstatus eq '1'){
970     return(1);}
971     else{
972     return(0);
973     }
974 }
975
976 =item UpdateBorrowerDebarred
977
978 ($borrowerstatut) = &UpdateBorrowerDebarred($borrowernumber);
979
980 update status of borrowers in borrowers table (field debarred)
981
982 C<$borrowernumber> borrower number
983
984 =cut
985
986 sub UpdateBorrowerDebarred{
987     my($borrowernumber) = @_;
988     my $dbh = C4::Context->dbh;
989         my $query=qq|UPDATE borrowers
990              SET debarred='1'
991                      WHERE borrowernumber=?
992             |;
993     my $sth=$dbh->prepare($query);
994         $sth->execute($borrowernumber);
995         $sth->finish;
996         return 1;
997 }
998
999 =item CheckExistantNotifyid
1000
1001   ($exist) = &CheckExistantNotifyid($borrowernumber,$itemnumber,$accounttype,$notify_id);
1002
1003 Check and Returns the notify id if exist else return 0.
1004
1005 C<$exist> contains a notify_id 
1006
1007 C<$borrowernumber> contains the borrower number
1008
1009 C<$date_due> contains the date of item return 
1010
1011
1012 =cut
1013
1014 sub CheckExistantNotifyid {
1015      my($borrowernumber,$date_due) = @_;
1016      my $dbh = C4::Context->dbh;
1017          my $query =  qq|SELECT notify_id FROM issues,accountlines
1018              WHERE accountlines.borrowernumber =?
1019              AND issues.itemnumber= accountlines.itemnumber
1020               AND date_due = ?|;
1021     my $sth=$dbh->prepare($query);
1022          $sth->execute($borrowernumber,$date_due);
1023          my ($exist)=$sth->fetchrow;
1024          $sth->finish;
1025          if ($exist eq '')
1026     {
1027     return(0);
1028     }else
1029         {
1030     return($exist);
1031     }
1032 }
1033
1034 =item CheckAccountLineItemInfo
1035
1036   ($exist) = &CheckAccountLineItemInfo($borrowernumber,$itemnumber,$accounttype,$notify_id);
1037
1038 Check and Returns the list of all overdue items from the same file number(notify_id).
1039
1040 C<$exist> contains number of line in accounlines
1041 with the same .biblionumber,itemnumber,accounttype,notify_id
1042
1043 C<$borrowernumber> contains the borrower number
1044
1045 C<$itemnumber> contains item number
1046
1047 C<$accounttype> contains account type
1048
1049 C<$notify_id> contains the file number 
1050
1051 =cut
1052
1053 sub CheckAccountLineItemInfo {
1054      my($borrowernumber,$itemnumber,$accounttype,$notify_id) = @_;
1055      my $dbh = C4::Context->dbh;
1056          my $query =  qq|SELECT count(*) FROM accountlines
1057              WHERE borrowernumber =?
1058              AND itemnumber = ?
1059               AND accounttype= ?
1060             AND notify_id = ?|;
1061     my $sth=$dbh->prepare($query);
1062          $sth->execute($borrowernumber,$itemnumber,$accounttype,$notify_id);
1063          my ($exist)=$sth->fetchrow;
1064          $sth->finish;
1065          return($exist);
1066  }
1067
1068
1069 1;
1070 __END__
1071
1072 =back
1073
1074 =head1 AUTHOR
1075
1076 Koha Developement team <info@koha.org>
1077
1078 =cut