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