4 # Copyright 2000-2002 Katipo Communications
6 # This file is part of Koha.
8 # Koha is free software; you can redistribute it and/or modify it under the
9 # terms of the GNU General Public License as published by the Free Software
10 # Foundation; either version 2 of the License, or (at your option) any later
13 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
14 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License along with
18 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
19 # Suite 330, Boston, MA 02111-1307 USA
22 use Date::Calc qw/Today/;
23 use Date::Manip qw/UnixDate/;
27 use C4::Log; # logaction
29 use vars qw($VERSION @ISA @EXPORT);
32 # set the version for version checking
36 # subs to rename (and maybe merge some...)
41 &CheckAccountLineLevelInfo
42 &CheckAccountLineItemInfo
43 &CheckExistantNotifyid
53 &CreateItemAccountLine
66 # check that an equivalent don't exist already before moving
68 # subs to move to Circulation.pm
73 # &GetIssuingRules - delete.
74 # use C4::Circulation::GetIssuingRule instead.
76 # subs to move to Members.pm
78 &CheckBorrowerDebarred
79 &UpdateBorrowerDebarred
81 # subs to move to Biblio.pm
90 C4::Circulation::Fines - Koha module dealing with fines
98 This module contains several functions for dealing with fines for
99 overdue items. It is primarily used by the 'misc/fines2.pl' script.
107 ($overdues) = &Getoverdues();
109 Returns the list of all overdue books, with their itemtype.
111 C<$overdues> is a reference-to-array. Each element is a
112 reference-to-hash whose keys are the fields of the issues table in the
119 my $dbh = C4::Context->dbh;
120 my $sth = (C4::Context->preference('item-level_itypes')) ?
122 "SELECT issues.*,items.itype as itemtype, items.homebranch FROM issues
123 LEFT JOIN items USING (itemnumber)
124 WHERE date_due < now()
125 ORDER BY borrowernumber " )
128 "SELECT issues.*,biblioitems.itemtype,items.itype, items.homebranch FROM issues
129 LEFT JOIN items USING (itemnumber)
130 LEFT JOIN biblioitems USING (biblioitemnumber)
131 WHERE date_due < now()
132 ORDER BY borrowernumber " );
136 while ( my $data = $sth->fetchrow_hashref ) {
137 push @results, $data;
146 ( $count, $overdueitems )=checkoverdues( $borrowernumber, $dbh );
154 # From Main.pm, modified to return a list of overdueitems, in addition to a count
155 #checks whether a borrower has overdue items
156 my ( $borrowernumber, $dbh ) = @_;
157 my @datearr = localtime;
159 ( $datearr[5] + 1900 ) . "-" . ( $datearr[4] + 1 ) . "-" . $datearr[3];
162 my $sth = $dbh->prepare(
163 "SELECT * FROM issues
164 LEFT JOIN items ON issues.itemnumber = items.itemnumber
165 LEFT JOIN biblio ON items.biblionumber=biblio.biblionumber
166 LEFT JOIN biblioitems ON items.biblioitemnumber = biblioitems.biblioitemnumber
167 WHERE issues.borrowernumber = ?
168 AND issues.date_due < ?"
170 $sth->execute( $borrowernumber, $today );
171 while ( my $data = $sth->fetchrow_hashref ) {
172 push( @overdueitems, $data );
176 return ( $count, \@overdueitems );
181 ($amount, $chargename, $message, $daycounttotal, $daycount) =
182 &CalcFine($itemnumber, $categorycode, $branch, $days_overdue, $description, $start_date, $end_date );
184 Calculates the fine for a book.
186 The issuingrules table in the Koha database is a fine matrix, listing
187 the penalties for each type of patron for each type of item and each branch (e.g., the
188 standard fine for books might be $0.50, but $1.50 for DVDs, or staff
189 members might get a longer grace period between the first and second
190 reminders that a book is overdue).
193 C<$itemnumber> is the book's item number.
195 C<$categorycode> is the category code of the patron who currently has
198 C<$branchcode> is the library whose issuingrules govern this transaction.
200 C<$days_overdue> is the number of days elapsed since the book's due date.
201 NOTE: supplying days_overdue is deprecated.
203 C<$start_date> & C<$end_date> are C4::Dates objects
204 defining the date range over which to determine the fine.
205 Note that if these are defined, we ignore C<$difference> and C<$dues> ,
206 but retain these for backwards-comptibility with extant fines scripts.
208 Fines scripts should just supply the date range over which to calculate the fine.
210 C<&CalcFine> returns a list of three values:
212 C<$amount> is the fine owed by the patron (see above).
214 C<$chargename> is the chargename field from the applicable record in
215 the categoryitem table, whatever that is.
217 C<$message> is a text message, either "First Notice", "Second Notice",
224 my ( $item, $bortype, $branchcode, $difference ,$dues , $start_date, $end_date ) = @_;
225 my $dbh = C4::Context->dbh;
229 # get issuingrules (fines part will be used)
230 my $data = C4::Circulation::GetIssuingRule($bortype, $item->{'itemtype'},$branchcode);
232 # if $difference is supplied, the difference has already been calculated, but we still need to adjust for the calendar.
233 # use copy-pasted functions from calendar module. (deprecated -- these functions will be removed from C4::Overdues ).
234 my $countspecialday=&GetSpecialHolidays($dues,$item->{itemnumber});
235 my $countrepeatableday=&GetRepeatableHolidays($dues,$item->{itemnumber},$difference);
236 my $countalldayclosed = $countspecialday + $countrepeatableday;
237 $daystocharge = $difference - $countalldayclosed;
239 # if $difference is not supplied, we have C4::Dates objects giving us the date range, and we use the calendar module.
240 if(C4::Context->preference('finesCalendar') eq 'noFinesWhenClosed') {
241 my $calendar = C4::Calendar->new( branchcode => $branchcode );
242 $daystocharge = $calendar->daysBetween( $start_date, $end_date );
244 $daystocharge = Date_to_Days(split('-',$end_date->output('iso'))) - Date_to_Days(split('-',$start_date->output('iso')));
247 # correct for grace period.
248 $daystocharge -= $data->{'firstremind'};
249 if ($data->{'chargeperiod'} > 0 && $daystocharge > 0 ) {
250 $amount = int($daystocharge / $data->{'chargeperiod'}) * $data->{'fine'};
252 # a zero (or null) chargeperiod means no charge.
255 # warn "Calc Fine: " . join(", ", ($item->{'itemnumber'}, $bortype, $difference , $data->{'fine'} . " * " . $daycount . " days = \$ " . $amount , "desc: $dues")) ;
256 return ( $amount, $data->{'chargename'}, $printout ,$daystocharge , $daystocharge + $data->{'firstremind'} );
260 =item GetSpecialHolidays
262 &GetSpecialHolidays($date_dues,$itemnumber);
264 return number of special days between date of the day and date due
266 C<$date_dues> is the envisaged date of book return.
268 C<$itemnumber> is the book's item number.
272 sub GetSpecialHolidays {
273 my ($date_dues,$itemnumber) = @_;
274 # calcul the today date
275 my $today = join "-", &Today();
277 # return the holdingbranch
278 my $iteminfo=GetIssuesIteminfo($itemnumber);
279 # use sql request to find all date between date_due and today
280 my $dbh = C4::Context->dbh;
281 my $query=qq|SELECT DATE_FORMAT(concat(year,'-',month,'-',day),'%Y-%m-%d')as date
282 FROM `special_holidays`
283 WHERE DATE_FORMAT(concat(year,'-',month,'-',day),'%Y-%m-%d') >= ?
284 AND DATE_FORMAT(concat(year,'-',month,'-',day),'%Y-%m-%d') <= ?
287 my @result=GetWdayFromItemnumber($itemnumber);
291 my $sth = $dbh->prepare($query);
292 $sth->execute($date_dues,$today,$iteminfo->{'branchcode'});
294 while ( my $special_date=$sth->fetchrow_hashref){
295 push (@result_date,$special_date);
298 my $specialdaycount=scalar(@result_date);
300 for (my $i=0;$i<scalar(@result_date);$i++){
301 $dateinsec=UnixDate($result_date[$i]->{'date'},"%o");
302 (undef,undef,undef,undef,undef,undef,$wday,undef,undef) =localtime($dateinsec);
303 for (my $j=0;$j<scalar(@result);$j++){
304 if ($wday == ($result[$j]->{'weekday'})){
310 return $specialdaycount;
313 =item GetRepeatableHolidays
315 &GetRepeatableHolidays($date_dues, $itemnumber, $difference,);
317 return number of day closed between date of the day and date due
319 C<$date_dues> is the envisaged date of book return.
321 C<$itemnumber> is item number.
323 C<$difference> numbers of between day date of the day and date due
327 sub GetRepeatableHolidays{
328 my ($date_dues,$itemnumber,$difference) = @_;
329 my $dateinsec=UnixDate($date_dues,"%o");
330 my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) =localtime($dateinsec);
331 my @result=GetWdayFromItemnumber($itemnumber);
335 for (my $i=0;$i<scalar(@result);$i++){
338 for ( $j=0;$j<$difference;$j++){
339 if ($result[$i]->{'weekday'} == $k)
341 push ( @dayclosedcount ,$k);
347 return scalar(@dayclosedcount);
351 =item GetWayFromItemnumber
353 &Getwdayfromitemnumber($itemnumber);
355 return the different week day from repeatable_holidays table
357 C<$itemnumber> is item number.
361 sub GetWdayFromItemnumber{
363 my $iteminfo=GetIssuesIteminfo($itemnumber);
365 my $dbh = C4::Context->dbh;
366 my $query = qq|SELECT weekday
367 FROM repeatable_holidays
370 my $sth = $dbh->prepare($query);
373 $sth->execute($iteminfo->{'branchcode'});
374 while ( my $weekday=$sth->fetchrow_hashref){
375 push (@result,$weekday);
381 =item GetIssuesIteminfo
383 &GetIssuesIteminfo($itemnumber);
385 return all data from issues about item
387 C<$itemnumber> is item number.
391 sub GetIssuesIteminfo{
393 my $dbh = C4::Context->dbh;
394 my $query = qq|SELECT *
398 my $sth = $dbh->prepare($query);
399 $sth->execute($itemnumber);
400 my ($issuesinfo)=$sth->fetchrow_hashref;
407 &UpdateFine($itemnumber, $borrowernumber, $amount, $type, $description);
409 (Note: the following is mostly conjecture and guesswork.)
411 Updates the fine owed on an overdue book.
413 C<$itemnumber> is the book's item number.
415 C<$borrowernumber> is the borrower number of the patron who currently
416 has the book on loan.
418 C<$amount> is the current amount owed by the patron.
420 C<$type> will be used in the description of the fine.
422 C<$description> is a string that must be present in the description of
423 the fine. I think this is expected to be a date in DD/MM/YYYY format.
425 C<&UpdateFine> looks up the amount currently owed on the given item
426 and sets it to C<$amount>, creating, if necessary, a new entry in the
427 accountlines table of the Koha database.
432 # FIXME - This API doesn't look right: why should the caller have to
433 # specify both the item number and the borrower number? A book can't
434 # be on loan to two different people, so the item number should be
437 my ( $itemnum, $borrowernumber, $amount, $type, $due ) = @_;
438 my $dbh = C4::Context->dbh;
439 # FIXME - What exactly is this query supposed to do? It looks up an
440 # entry in accountlines that matches the given item and borrower
441 # numbers, where the description contains $due, and where the
442 # account type has one of several values, but what does this _mean_?
443 # Does it look up existing fines for this item?
444 # FIXME - What are these various account types? ("FU", "O", "F", "M")
445 my $sth = $dbh->prepare(
446 "Select * from accountlines where itemnumber=? and
447 borrowernumber=? and (accounttype='FU' or accounttype='O' or
448 accounttype='F' or accounttype='M') and description like ?"
450 $sth->execute( $itemnum, $borrowernumber, "%$due%" );
452 if ( my $data = $sth->fetchrow_hashref ) {
454 # I think this if-clause deals with the case where we're updating
456 # print "in accounts ...";
457 if ( $data->{'amount'} != $amount ) {
460 my $diff = $amount - $data->{'amount'};
461 my $out = $data->{'amountoutstanding'} + $diff;
462 my $sth2 = $dbh->prepare(
463 "UPDATE accountlines SET date=now(), amount=?,
464 amountoutstanding=?,accounttype='FU' WHERE
465 borrowernumber=? AND itemnumber=?
466 AND (accounttype='FU' OR accounttype='O') AND description LIKE ?"
468 $sth2->execute( $amount, $out, $data->{'borrowernumber'},
469 $data->{'itemnumber'}, "%$due%" );
474 # print "no update needed $data->{'amount'}"
479 # I think this else-clause deals with the case where we're adding
481 my $sth4 = $dbh->prepare(
482 "SELECT title FROM biblio LEFT JOIN items ON biblio.biblionumber=items.biblionumber WHERE items.itemnumber=?"
484 $sth4->execute($itemnum);
485 my $title = $sth4->fetchrow_hashref;
488 # # print "not in account";
489 # my $sth3 = $dbh->prepare("Select max(accountno) from accountlines");
492 # # FIXME - Make $accountno a scalar.
493 # my @accountno = $sth3->fetchrow_array;
497 my $nextaccntno = C4::Accounts::getnextacctno($borrowernumber);
498 my $sth2 = $dbh->prepare(
499 "INSERT INTO accountlines
500 (borrowernumber,itemnumber,date,amount,
501 description,accounttype,amountoutstanding,accountno) VALUES
502 (?,?,now(),?,?,'FU',?,?)"
504 $sth2->execute( $borrowernumber, $itemnum, $amount,
505 "$type $title->{'title'} $due",
506 $amount, $nextaccntno);
514 "due=".$due." amount=".$amount." itemnumber=".$itemnum
515 ) if C4::Context->preference("FinesLog");
522 $borrower = &BorType($borrowernumber);
524 Looks up a patron by borrower number.
526 C<$borrower> is a reference-to-hash whose keys are all of the fields
527 from the borrowers and categories tables of the Koha database. Thus,
528 C<$borrower> contains all information about both the borrower and
529 category he or she belongs to.
535 my ($borrowernumber) = @_;
536 my $dbh = C4::Context->dbh;
537 my $sth = $dbh->prepare(
538 "SELECT * from borrowers
539 LEFT JOIN categories ON borrowers.categorycode=categories.categorycode
540 WHERE borrowernumber=?"
542 $sth->execute($borrowernumber);
543 my $data = $sth->fetchrow_hashref;
548 =item ReplacementCost
550 $cost = &ReplacementCost($itemnumber);
552 Returns the replacement cost of the item with the given item number.
557 sub ReplacementCost {
559 my $dbh = C4::Context->dbh;
561 $dbh->prepare("Select replacementprice from items where itemnumber=?");
562 $sth->execute($itemnum);
564 # FIXME - Use fetchrow_array or something.
565 my $data = $sth->fetchrow_hashref;
567 return ( $data->{'replacementprice'} );
572 $data->{'sum(amountoutstanding)'} = &GetFine($itemnum,$borrowernumber);
574 return the total of fine
576 C<$itemnum> is item number
578 C<$borrowernumber> is the borrowernumber
584 my ( $itemnum, $borrowernumber ) = @_;
585 my $dbh = C4::Context->dbh();
586 my $query = "SELECT sum(amountoutstanding) FROM accountlines
587 where accounttype like 'F%'
588 AND amountoutstanding > 0 AND itemnumber = ? AND borrowernumber=?";
589 my $sth = $dbh->prepare($query);
590 $sth->execute( $itemnum, $borrowernumber );
591 my $data = $sth->fetchrow_hashref();
594 return ( $data->{'sum(amountoutstanding)'} );
600 =item GetIssuingRules
602 FIXME - This sub should be deprecated and removed.
603 It ignores branch and defaults.
605 $data = &GetIssuingRules($itemtype,$categorycode);
607 Looks up for all issuingrules an item info
609 C<$itemnumber> is a reference-to-hash whose keys are all of the fields
610 from the borrowers and categories tables of the Koha database. Thus,
612 C<$categorycode> contains information about borrowers category
614 C<$data> contains all information about both the borrower and
615 category he or she belongs to.
618 sub GetIssuingRules {
619 my ($itemtype,$categorycode)=@_;
620 my $dbh = C4::Context->dbh();
621 my $query=qq|SELECT *
623 WHERE issuingrules.itemtype=?
624 AND issuingrules.categorycode=?
626 my $sth = $dbh->prepare($query);
628 $sth->execute($itemtype,$categorycode);
629 my ($data) = $sth->fetchrow_hashref;
636 sub ReplacementCost2 {
637 my ( $itemnum, $borrowernumber ) = @_;
638 my $dbh = C4::Context->dbh();
639 my $query = "SELECT amountoutstanding
641 WHERE accounttype like 'L'
642 AND amountoutstanding > 0
644 AND borrowernumber= ?";
645 my $sth = $dbh->prepare($query);
646 $sth->execute( $itemnum, $borrowernumber );
647 my $data = $sth->fetchrow_hashref();
650 return ( $data->{'amountoutstanding'} );
654 =item GetNextIdNotify
656 ($result) = &GetNextIdNotify($reference);
658 Returns the new file number
660 C<$result> contains the next file number
662 C<$reference> contains the beggining of file number
668 sub GetNextIdNotify {
670 my $query=qq|SELECT max(notify_id)
672 WHERE notify_id like \"$reference%\"
674 # AND borrowernumber=?|;
675 my $dbh = C4::Context->dbh;
676 my $sth=$dbh->prepare($query);
678 my $result=$sth->fetchrow;
683 ($result=$reference."01") ;
686 $count=substr($result,6)+1;
689 ($count = "0".$count);
691 $result=$reference.$count;
699 (@notify) = &NumberNotifyId($borrowernumber);
701 Returns amount for all file per borrowers
702 C<@notify> array contains all file per borrowers
704 C<$notify_id> contains the file number for the borrower number nad item number
709 my ($borrowernumber)=@_;
710 my $dbh = C4::Context->dbh;
711 my $query=qq| SELECT distinct(notify_id)
713 WHERE borrowernumber=?|;
715 my $sth=$dbh->prepare($query);
716 $sth->execute($borrowernumber);
717 while ( my ($numberofnotify)=$sth->fetchrow){
718 push (@notify,$numberofnotify);
728 ($totalnotify) = &AmountNotify($notifyid);
730 Returns amount for all file per borrowers
731 C<$notifyid> is the file number
733 C<$totalnotify> contains amount of a file
735 C<$notify_id> contains the file number for the borrower number and item number
740 my ($notifyid,$borrowernumber)=@_;
741 my $dbh = C4::Context->dbh;
742 my $query=qq| SELECT sum(amountoutstanding)
744 WHERE notify_id=? AND borrowernumber = ?|;
745 my $sth=$dbh->prepare($query);
746 $sth->execute($notifyid,$borrowernumber);
747 my $totalnotify=$sth->fetchrow;
749 return ($totalnotify);
755 ($notify_id) = &GetNotifyId($borrowernumber,$itemnumber);
757 Returns the file number per borrower and itemnumber
759 C<$borrowernumber> is a reference-to-hash whose keys are all of the fields
760 from the items tables of the Koha database. Thus,
762 C<$itemnumber> contains the borrower categorycode
764 C<$notify_id> contains the file number for the borrower number nad item number
769 my ($borrowernumber,$itemnumber)=@_;
770 my $query=qq|SELECT notify_id
772 WHERE borrowernumber=?
774 AND (accounttype='FU' or accounttype='O')|;
775 my $dbh = C4::Context->dbh;
776 my $sth=$dbh->prepare($query);
777 $sth->execute($borrowernumber,$itemnumber);
778 my ($notify_id)=$sth->fetchrow;
784 =item CreateItemAccountLine
786 () = &CreateItemAccountLine($borrowernumber,$itemnumber,$date,$amount,$description,$accounttype,$amountoutstanding,$timestamp,$notify_id,$level);
788 update the account lines with file number or with file level
790 C<$items> is a reference-to-hash whose keys are all of the fields
791 from the items tables of the Koha database. Thus,
793 C<$itemnumber> contains the item number
795 C<$borrowernumber> contains the borrower number
797 C<$date> contains the date of the day
799 C<$amount> contains item price
801 C<$description> contains the descritpion of accounttype
803 C<$accounttype> contains the account type
805 C<$amountoutstanding> contains the $amountoutstanding
807 C<$timestamp> contains the timestamp with time and the date of the day
809 C<$notify_id> contains the file number
811 C<$level> contains the file level
816 sub CreateItemAccountLine {
817 my ($borrowernumber,$itemnumber,$date,$amount,$description,$accounttype,$amountoutstanding,$timestamp,$notify_id,$level)=@_;
818 my $dbh = C4::Context->dbh;
819 my $nextaccntno = C4::Accounts::getnextacctno($borrowernumber);
820 my $query= "INSERT into accountlines
821 (borrowernumber,accountno,itemnumber,date,amount,description,accounttype,amountoutstanding,timestamp,notify_id,notify_level)
823 (?,?,?,?,?,?,?,?,?,?,?)";
826 my $sth=$dbh->prepare($query);
827 $sth->execute($borrowernumber,$nextaccntno,$itemnumber,$date,$amount,$description,$accounttype,$amountoutstanding,$timestamp,$notify_id,$level);
831 =item UpdateAccountLines
833 () = &UpdateAccountLines($notify_id,$notify_level,$borrowernumber,$itemnumber);
835 update the account lines with file number or with file level
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,
840 C<$itemnumber> contains the item number
842 C<$notify_id> contains the file number
844 C<$notify_level> contains the file level
846 C<$borrowernumber> contains the borrowernumber
850 sub UpdateAccountLines {
851 my ($notify_id,$notify_level,$borrowernumber,$itemnumber)=@_;
853 if ($notify_id eq '')
856 $query=qq|UPDATE accountlines
858 WHERE borrowernumber=? AND itemnumber=?
859 AND (accounttype='FU' or accounttype='O')|;
862 $query=qq|UPDATE accountlines
863 SET notify_id=?, notify_level=?
864 WHERE borrowernumber=?
866 AND (accounttype='FU' or accounttype='O')|;
868 my $dbh = C4::Context->dbh;
869 my $sth=$dbh->prepare($query);
871 if ($notify_id eq '')
873 $sth->execute($notify_level,$borrowernumber,$itemnumber);
876 $sth->execute($notify_id,$notify_level,$borrowernumber,$itemnumber);
885 ($items) = &GetItems($itemnumber);
887 Returns the list of all delays from overduerules.
889 C<$items> is a reference-to-hash whose keys are all of the fields
890 from the items tables of the Koha database. Thus,
892 C<$itemnumber> contains the borrower categorycode
897 my($itemnumber) = @_;
898 my $query=qq|SELECT *
901 my $dbh = C4::Context->dbh;
902 my $sth=$dbh->prepare($query);
903 $sth->execute($itemnumber);
904 my ($items)=$sth->fetchrow_hashref;
909 =item GetOverdueDelays
911 (@delays) = &GetOverdueDelays($categorycode);
913 Returns the list of all delays from overduerules.
915 C<@delays> it's an array contains the three delays from overduerules table
917 C<$categorycode> contains the borrower categorycode
921 sub GetOverdueDelays {
923 my $dbh = C4::Context->dbh;
924 my $query=qq|SELECT delay1,delay2,delay3
926 WHERE categorycode=?|;
927 my $sth=$dbh->prepare($query);
928 $sth->execute($category);
929 my (@delays)=$sth->fetchrow_array;
934 =item CheckAccountLineLevelInfo
936 ($exist) = &CheckAccountLineLevelInfo($borrowernumber,$itemnumber,$accounttype,notify_level);
938 Check and Returns the list of all overdue books.
940 C<$exist> contains number of line in accounlines
941 with the same .biblionumber,itemnumber,accounttype,and notify_level
943 C<$borrowernumber> contains the borrower number
945 C<$itemnumber> contains item number
947 C<$accounttype> contains account type
949 C<$notify_level> contains the accountline level
954 sub CheckAccountLineLevelInfo {
955 my($borrowernumber,$itemnumber,$level) = @_;
956 my $dbh = C4::Context->dbh;
957 my $query= qq|SELECT count(*)
959 WHERE borrowernumber =?
962 my $sth=$dbh->prepare($query);
963 $sth->execute($borrowernumber,$itemnumber,$level);
964 my ($exist)=$sth->fetchrow;
969 =item GetOverduerules
971 ($overduerules) = &GetOverduerules($categorycode);
973 Returns the value of borrowers (debarred or not) with notify level
975 C<$overduerules> return value of debbraed field in overduerules table
977 C<$category> contains the borrower categorycode
979 C<$notify_level> contains the notify level
984 my($category,$notify_level) = @_;
985 my $dbh = C4::Context->dbh;
986 my $query=qq|SELECT debarred$notify_level
988 WHERE categorycode=?|;
989 my $sth=$dbh->prepare($query);
990 $sth->execute($category);
991 my ($overduerules)=$sth->fetchrow;
993 return($overduerules);
997 =item CheckBorrowerDebarred
999 ($debarredstatus) = &CheckBorrowerDebarred($borrowernumber);
1001 Check if the borrowers is already debarred
1003 C<$debarredstatus> return 0 for not debarred and return 1 for debarred
1005 C<$borrowernumber> contains the borrower number
1010 sub CheckBorrowerDebarred{
1011 my($borrowernumber) = @_;
1012 my $dbh = C4::Context->dbh;
1013 my $query=qq|SELECT debarred
1015 WHERE borrowernumber=?
1017 my $sth=$dbh->prepare($query);
1018 $sth->execute($borrowernumber);
1019 my ($debarredstatus)=$sth->fetchrow;
1021 if ($debarredstatus eq '1'){
1028 =item UpdateBorrowerDebarred
1030 ($borrowerstatut) = &UpdateBorrowerDebarred($borrowernumber);
1032 update status of borrowers in borrowers table (field debarred)
1034 C<$borrowernumber> borrower number
1038 sub UpdateBorrowerDebarred{
1039 my($borrowernumber) = @_;
1040 my $dbh = C4::Context->dbh;
1041 my $query=qq|UPDATE borrowers
1043 WHERE borrowernumber=?
1045 my $sth=$dbh->prepare($query);
1046 $sth->execute($borrowernumber);
1051 =item CheckExistantNotifyid
1053 ($exist) = &CheckExistantNotifyid($borrowernumber,$itemnumber,$accounttype,$notify_id);
1055 Check and Returns the notify id if exist else return 0.
1057 C<$exist> contains a notify_id
1059 C<$borrowernumber> contains the borrower number
1061 C<$date_due> contains the date of item return
1066 sub CheckExistantNotifyid {
1067 my($borrowernumber,$date_due) = @_;
1068 my $dbh = C4::Context->dbh;
1069 my $query = qq|SELECT notify_id FROM accountlines
1070 LEFT JOIN issues ON issues.itemnumber= accountlines.itemnumber
1071 WHERE accountlines.borrowernumber =?
1073 my $sth=$dbh->prepare($query);
1074 $sth->execute($borrowernumber,$date_due);
1075 my ($exist)=$sth->fetchrow;
1086 =item CheckAccountLineItemInfo
1088 ($exist) = &CheckAccountLineItemInfo($borrowernumber,$itemnumber,$accounttype,$notify_id);
1090 Check and Returns the list of all overdue items from the same file number(notify_id).
1092 C<$exist> contains number of line in accounlines
1093 with the same .biblionumber,itemnumber,accounttype,notify_id
1095 C<$borrowernumber> contains the borrower number
1097 C<$itemnumber> contains item number
1099 C<$accounttype> contains account type
1101 C<$notify_id> contains the file number
1105 sub CheckAccountLineItemInfo {
1106 my($borrowernumber,$itemnumber,$accounttype,$notify_id) = @_;
1107 my $dbh = C4::Context->dbh;
1108 my $query = qq|SELECT count(*) FROM accountlines
1109 WHERE borrowernumber =?
1113 my $sth=$dbh->prepare($query);
1114 $sth->execute($borrowernumber,$itemnumber,$accounttype,$notify_id);
1115 my ($exist)=$sth->fetchrow;
1120 =head2 CheckItemNotify
1122 Sql request to check if the document has alreday been notified
1123 this function is not exported, only used with GetOverduesForBranch
1127 sub CheckItemNotify {
1128 my ($notify_id,$notify_level,$itemnumber) = @_;
1129 my $dbh = C4::Context->dbh;
1130 my $sth = $dbh->prepare("
1131 SELECT COUNT(*) FROM notifys
1133 AND notify_level = ?
1134 AND itemnumber = ? ");
1135 $sth->execute($notify_id,$notify_level,$itemnumber);
1136 my $notified = $sth->fetchrow;
1141 =head2 GetOverduesForBranch
1143 Sql request for display all information for branchoverdues.pl
1144 2 possibilities : with or without location .
1145 display is filtered by branch
1149 sub GetOverduesForBranch {
1150 my ( $branch, $location) = @_;
1151 my $itype_link = (C4::Context->preference('item-level_itypes')) ? " items.itype " : " biblioitems.itemtype ";
1152 if ( not $location ) {
1153 my $dbh = C4::Context->dbh;
1154 my $sth = $dbh->prepare("
1157 borrowers.firstname,
1159 itemtypes.description,
1162 branches.branchname,
1166 items.itemcallnumber,
1167 borrowers.borrowernumber,
1169 biblio.biblionumber,
1171 accountlines.notify_id,
1172 accountlines.notify_level,
1174 accountlines.amountoutstanding
1176 LEFT JOIN issues ON issues.itemnumber = accountlines.itemnumber AND issues.borrowernumber = accountlines.borrowernumber
1177 LEFT JOIN borrowers ON borrowers.borrowernumber = accountlines.borrowernumber
1178 LEFT JOIN items ON items.itemnumber = issues.itemnumber
1179 LEFT JOIN biblio ON biblio.biblionumber = items.biblionumber
1180 LEFT JOIN biblioitems ON biblioitems.biblioitemnumber=items.biblioitemnumber
1181 LEFT JOIN itemtypes ON itemtypes.itemtype = $itype_link
1182 LEFT JOIN branches ON branches.branchcode = issues.branchcode
1183 WHERE ( accountlines.amountoutstanding != '0.000000')
1184 AND ( accountlines.accounttype = 'FU')
1185 AND (issues.branchcode = ?)
1186 AND (issues.date_due <= NOW())
1187 ORDER BY borrowers.surname
1189 $sth->execute($branch);
1192 while ( my $data = $sth->fetchrow_hashref ) {
1193 #check if the document has already been notified
1194 my $countnotify = CheckItemNotify($data->{'notify_id'},$data->{'notify_level'},$data->{'itemnumber'});
1195 if ($countnotify eq '0'){
1196 $getoverdues[$i] = $data;
1200 return (@getoverdues);
1204 my $dbh = C4::Context->dbh;
1205 my $sth = $dbh->prepare( "
1206 SELECT borrowers.surname,
1207 borrowers.firstname,
1209 itemtypes.description,
1212 branches.branchname,
1216 items.itemcallnumber,
1217 borrowers.borrowernumber,
1219 biblio.biblionumber,
1221 accountlines.notify_id,
1222 accountlines.notify_level,
1224 accountlines.amountoutstanding
1226 LEFT JOIN issues ON issues.itemnumber = accountlines.itemnumber AND issues.borrowernumber = accountlines.borrowernumber
1227 LEFT JOIN borrowers ON borrowers.borrowernumber = accountlines.borrowernumber
1228 LEFT JOIN items ON items.itemnumber = issues.itemnumber
1229 LEFT JOIN biblio ON biblio.biblionumber = items.biblionumber
1230 LEFT JOIN biblioitems ON biblioitems.biblioitemnumber=items.biblioitemnumber
1231 LEFT JOIN itemtypes ON itemtypes.itemtype = $itype_link
1232 LEFT JOIN branches ON branches.branchcode = issues.branchcode
1233 WHERE ( accountlines.amountoutstanding != '0.000000')
1234 AND ( accountlines.accounttype = 'FU')
1235 AND (issues.branchcode = ? AND items.location = ?)
1236 AND (issues.date_due <= NOW())
1237 ORDER BY borrowers.surname
1239 $sth->execute( $branch, $location);
1242 while ( my $data = $sth->fetchrow_hashref ) {
1243 #check if the document has already been notified
1244 my $countnotify = CheckItemNotify($data->{'notify_id'},$data->{'notify_level'},$data->{'itemnumber'});
1245 if ($countnotify eq '0'){
1246 $getoverdues[$i] = $data;
1251 return (@getoverdues);
1256 =head2 AddNotifyLine
1258 &AddNotifyLine($borrowernumber, $itemnumber, $overduelevel, $method, $notifyId)
1260 Creat a line into notify, if the method is phone, the notification_send_date is implemented to
1265 my ( $borrowernumber, $itemnumber, $overduelevel, $method, $notifyId ) = @_;
1266 if ( $method eq "phone" ) {
1267 my $dbh = C4::Context->dbh;
1268 my $sth = $dbh->prepare(
1269 "INSERT INTO notifys (borrowernumber,itemnumber,notify_date,notify_send_date,notify_level,method,notify_id)
1270 VALUES (?,?,now(),now(),?,?,?)"
1272 $sth->execute( $borrowernumber, $itemnumber, $overduelevel, $method,
1277 my $dbh = C4::Context->dbh;
1278 my $sth = $dbh->prepare(
1279 "INSERT INTO notifys (borrowernumber,itemnumber,notify_date,notify_level,method,notify_id)
1280 VALUES (?,?,now(),?,?,?)"
1282 $sth->execute( $borrowernumber, $itemnumber, $overduelevel, $method,
1289 =head2 RemoveNotifyLine
1291 &RemoveNotifyLine( $borrowernumber, $itemnumber, $notify_date );
1293 Cancel a notification
1297 sub RemoveNotifyLine {
1298 my ( $borrowernumber, $itemnumber, $notify_date ) = @_;
1299 my $dbh = C4::Context->dbh;
1300 my $sth = $dbh->prepare(
1301 "DELETE FROM notifys
1307 $sth->execute( $borrowernumber, $itemnumber, $notify_date );
1319 Koha Developement team <info@koha.org>