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 four 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.
216 FIXME - What is chargename supposed to be ?
218 C<$message> is a text message, either "First Notice", "Second Notice",
225 my ( $item, $bortype, $branchcode, $difference ,$dues , $start_date, $end_date ) = @_;
226 my $dbh = C4::Context->dbh;
230 # get issuingrules (fines part will be used)
231 my $data = C4::Circulation::GetIssuingRule($bortype, $item->{'itemtype'},$branchcode);
233 # if $difference is supplied, the difference has already been calculated, but we still need to adjust for the calendar.
234 # use copy-pasted functions from calendar module. (deprecated -- these functions will be removed from C4::Overdues ).
235 my $countspecialday=&GetSpecialHolidays($dues,$item->{itemnumber});
236 my $countrepeatableday=&GetRepeatableHolidays($dues,$item->{itemnumber},$difference);
237 my $countalldayclosed = $countspecialday + $countrepeatableday;
238 $daystocharge = $difference - $countalldayclosed;
240 # if $difference is not supplied, we have C4::Dates objects giving us the date range, and we use the calendar module.
241 if(C4::Context->preference('finesCalendar') eq 'noFinesWhenClosed') {
242 my $calendar = C4::Calendar->new( branchcode => $branchcode );
243 $daystocharge = $calendar->daysBetween( $start_date, $end_date );
245 $daystocharge = Date_to_Days(split('-',$end_date->output('iso'))) - Date_to_Days(split('-',$start_date->output('iso')));
248 # correct for grace period.
249 $daystocharge -= $data->{'firstremind'};
250 if ($data->{'chargeperiod'} > 0 && $daystocharge > 0 ) {
251 $amount = int($daystocharge / $data->{'chargeperiod'}) * $data->{'fine'};
253 # a zero (or null) chargeperiod means no charge.
256 # warn "Calc Fine: " . join(", ", ($item->{'itemnumber'}, $bortype, $difference , $data->{'fine'} . " * " . $daycount . " days = \$ " . $amount , "desc: $dues")) ;
257 return ( $amount, $data->{'chargename'}, $printout ,$daystocharge , $daystocharge + $data->{'firstremind'} );
261 =item GetSpecialHolidays
263 &GetSpecialHolidays($date_dues,$itemnumber);
265 return number of special days between date of the day and date due
267 C<$date_dues> is the envisaged date of book return.
269 C<$itemnumber> is the book's item number.
273 sub GetSpecialHolidays {
274 my ($date_dues,$itemnumber) = @_;
275 # calcul the today date
276 my $today = join "-", &Today();
278 # return the holdingbranch
279 my $iteminfo=GetIssuesIteminfo($itemnumber);
280 # use sql request to find all date between date_due and today
281 my $dbh = C4::Context->dbh;
282 my $query=qq|SELECT DATE_FORMAT(concat(year,'-',month,'-',day),'%Y-%m-%d')as date
283 FROM `special_holidays`
284 WHERE DATE_FORMAT(concat(year,'-',month,'-',day),'%Y-%m-%d') >= ?
285 AND DATE_FORMAT(concat(year,'-',month,'-',day),'%Y-%m-%d') <= ?
288 my @result=GetWdayFromItemnumber($itemnumber);
292 my $sth = $dbh->prepare($query);
293 $sth->execute($date_dues,$today,$iteminfo->{'branchcode'});
295 while ( my $special_date=$sth->fetchrow_hashref){
296 push (@result_date,$special_date);
299 my $specialdaycount=scalar(@result_date);
301 for (my $i=0;$i<scalar(@result_date);$i++){
302 $dateinsec=UnixDate($result_date[$i]->{'date'},"%o");
303 (undef,undef,undef,undef,undef,undef,$wday,undef,undef) =localtime($dateinsec);
304 for (my $j=0;$j<scalar(@result);$j++){
305 if ($wday == ($result[$j]->{'weekday'})){
311 return $specialdaycount;
314 =item GetRepeatableHolidays
316 &GetRepeatableHolidays($date_dues, $itemnumber, $difference,);
318 return number of day closed between date of the day and date due
320 C<$date_dues> is the envisaged date of book return.
322 C<$itemnumber> is item number.
324 C<$difference> numbers of between day date of the day and date due
328 sub GetRepeatableHolidays{
329 my ($date_dues,$itemnumber,$difference) = @_;
330 my $dateinsec=UnixDate($date_dues,"%o");
331 my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) =localtime($dateinsec);
332 my @result=GetWdayFromItemnumber($itemnumber);
336 for (my $i=0;$i<scalar(@result);$i++){
339 for ( $j=0;$j<$difference;$j++){
340 if ($result[$i]->{'weekday'} == $k)
342 push ( @dayclosedcount ,$k);
348 return scalar(@dayclosedcount);
352 =item GetWayFromItemnumber
354 &Getwdayfromitemnumber($itemnumber);
356 return the different week day from repeatable_holidays table
358 C<$itemnumber> is item number.
362 sub GetWdayFromItemnumber{
364 my $iteminfo=GetIssuesIteminfo($itemnumber);
366 my $dbh = C4::Context->dbh;
367 my $query = qq|SELECT weekday
368 FROM repeatable_holidays
371 my $sth = $dbh->prepare($query);
374 $sth->execute($iteminfo->{'branchcode'});
375 while ( my $weekday=$sth->fetchrow_hashref){
376 push (@result,$weekday);
382 =item GetIssuesIteminfo
384 &GetIssuesIteminfo($itemnumber);
386 return all data from issues about item
388 C<$itemnumber> is item number.
392 sub GetIssuesIteminfo{
394 my $dbh = C4::Context->dbh;
395 my $query = qq|SELECT *
399 my $sth = $dbh->prepare($query);
400 $sth->execute($itemnumber);
401 my ($issuesinfo)=$sth->fetchrow_hashref;
408 &UpdateFine($itemnumber, $borrowernumber, $amount, $type, $description);
410 (Note: the following is mostly conjecture and guesswork.)
412 Updates the fine owed on an overdue book.
414 C<$itemnumber> is the book's item number.
416 C<$borrowernumber> is the borrower number of the patron who currently
417 has the book on loan.
419 C<$amount> is the current amount owed by the patron.
421 C<$type> will be used in the description of the fine.
423 C<$description> is a string that must be present in the description of
424 the fine. I think this is expected to be a date in DD/MM/YYYY format.
426 C<&UpdateFine> looks up the amount currently owed on the given item
427 and sets it to C<$amount>, creating, if necessary, a new entry in the
428 accountlines table of the Koha database.
433 # FIXME - This API doesn't look right: why should the caller have to
434 # specify both the item number and the borrower number? A book can't
435 # be on loan to two different people, so the item number should be
438 my ( $itemnum, $borrowernumber, $amount, $type, $due ) = @_;
439 my $dbh = C4::Context->dbh;
440 # FIXME - What exactly is this query supposed to do? It looks up an
441 # entry in accountlines that matches the given item and borrower
442 # numbers, where the description contains $due, and where the
443 # account type has one of several values, but what does this _mean_?
444 # Does it look up existing fines for this item?
445 # FIXME - What are these various account types? ("FU", "O", "F", "M")
446 my $sth = $dbh->prepare(
447 "Select * from accountlines where itemnumber=? and
448 borrowernumber=? and (accounttype='FU' or accounttype='O' or
449 accounttype='F' or accounttype='M') and description like ?"
451 $sth->execute( $itemnum, $borrowernumber, "%$due%" );
453 if ( my $data = $sth->fetchrow_hashref ) {
455 # we're updating an existing fine.
456 if ( $data->{'amount'} != $amount ) {
458 my $diff = $amount - $data->{'amount'};
459 my $out = $data->{'amountoutstanding'} + $diff;
460 my $sth2 = $dbh->prepare(
461 "UPDATE accountlines SET date=now(), amount=?,
462 amountoutstanding=?, lastincrement=? , accounttype='FU' WHERE
463 borrowernumber=? AND itemnumber=?
464 AND (accounttype='FU' OR accounttype='O') AND description LIKE ?"
466 $sth2->execute( $amount, $out, $diff , $data->{'borrowernumber'},
467 $data->{'itemnumber'}, "%$due%" );
472 # print "no update needed $data->{'amount'}"
477 # I think this else-clause deals with the case where we're adding
479 my $sth4 = $dbh->prepare(
480 "SELECT title FROM biblio LEFT JOIN items ON biblio.biblionumber=items.biblionumber WHERE items.itemnumber=?"
482 $sth4->execute($itemnum);
483 my $title = $sth4->fetchrow_hashref;
486 # # print "not in account";
487 # my $sth3 = $dbh->prepare("Select max(accountno) from accountlines");
490 # # FIXME - Make $accountno a scalar.
491 # my @accountno = $sth3->fetchrow_array;
495 my $nextaccntno = C4::Accounts::getnextacctno($borrowernumber);
496 my $sth2 = $dbh->prepare(
497 "INSERT INTO accountlines
498 (borrowernumber,itemnumber,date,amount,
499 description,accounttype,amountoutstanding,lastincrement,accountno) VALUES
500 (?,?,now(),?,?,'FU',?,?,?)"
502 $sth2->execute( $borrowernumber, $itemnum, $amount,
503 "$type $title->{'title'} $due",
504 $amount,$amount, $nextaccntno);
512 "due=".$due." amount=".$amount." itemnumber=".$itemnum
513 ) if C4::Context->preference("FinesLog");
520 $borrower = &BorType($borrowernumber);
522 Looks up a patron by borrower number.
524 C<$borrower> is a reference-to-hash whose keys are all of the fields
525 from the borrowers and categories tables of the Koha database. Thus,
526 C<$borrower> contains all information about both the borrower and
527 category he or she belongs to.
533 my ($borrowernumber) = @_;
534 my $dbh = C4::Context->dbh;
535 my $sth = $dbh->prepare(
536 "SELECT * from borrowers
537 LEFT JOIN categories ON borrowers.categorycode=categories.categorycode
538 WHERE borrowernumber=?"
540 $sth->execute($borrowernumber);
541 my $data = $sth->fetchrow_hashref;
546 =item ReplacementCost
548 $cost = &ReplacementCost($itemnumber);
550 Returns the replacement cost of the item with the given item number.
555 sub ReplacementCost {
557 my $dbh = C4::Context->dbh;
559 $dbh->prepare("Select replacementprice from items where itemnumber=?");
560 $sth->execute($itemnum);
562 # FIXME - Use fetchrow_array or something.
563 my $data = $sth->fetchrow_hashref;
565 return ( $data->{'replacementprice'} );
570 $data->{'sum(amountoutstanding)'} = &GetFine($itemnum,$borrowernumber);
572 return the total of fine
574 C<$itemnum> is item number
576 C<$borrowernumber> is the borrowernumber
582 my ( $itemnum, $borrowernumber ) = @_;
583 my $dbh = C4::Context->dbh();
584 my $query = "SELECT sum(amountoutstanding) FROM accountlines
585 where accounttype like 'F%'
586 AND amountoutstanding > 0 AND itemnumber = ? AND borrowernumber=?";
587 my $sth = $dbh->prepare($query);
588 $sth->execute( $itemnum, $borrowernumber );
589 my $data = $sth->fetchrow_hashref();
592 return ( $data->{'sum(amountoutstanding)'} );
598 =item GetIssuingRules
600 FIXME - This sub should be deprecated and removed.
601 It ignores branch and defaults.
603 $data = &GetIssuingRules($itemtype,$categorycode);
605 Looks up for all issuingrules an item info
607 C<$itemnumber> is a reference-to-hash whose keys are all of the fields
608 from the borrowers and categories tables of the Koha database. Thus,
610 C<$categorycode> contains information about borrowers category
612 C<$data> contains all information about both the borrower and
613 category he or she belongs to.
616 sub GetIssuingRules {
617 my ($itemtype,$categorycode)=@_;
618 my $dbh = C4::Context->dbh();
619 my $query=qq|SELECT *
621 WHERE issuingrules.itemtype=?
622 AND issuingrules.categorycode=?
624 my $sth = $dbh->prepare($query);
626 $sth->execute($itemtype,$categorycode);
627 my ($data) = $sth->fetchrow_hashref;
634 sub ReplacementCost2 {
635 my ( $itemnum, $borrowernumber ) = @_;
636 my $dbh = C4::Context->dbh();
637 my $query = "SELECT amountoutstanding
639 WHERE accounttype like 'L'
640 AND amountoutstanding > 0
642 AND borrowernumber= ?";
643 my $sth = $dbh->prepare($query);
644 $sth->execute( $itemnum, $borrowernumber );
645 my $data = $sth->fetchrow_hashref();
648 return ( $data->{'amountoutstanding'} );
652 =item GetNextIdNotify
654 ($result) = &GetNextIdNotify($reference);
656 Returns the new file number
658 C<$result> contains the next file number
660 C<$reference> contains the beggining of file number
666 sub GetNextIdNotify {
668 my $query=qq|SELECT max(notify_id)
670 WHERE notify_id like \"$reference%\"
672 # AND borrowernumber=?|;
673 my $dbh = C4::Context->dbh;
674 my $sth=$dbh->prepare($query);
676 my $result=$sth->fetchrow;
681 ($result=$reference."01") ;
684 $count=substr($result,6)+1;
687 ($count = "0".$count);
689 $result=$reference.$count;
697 (@notify) = &NumberNotifyId($borrowernumber);
699 Returns amount for all file per borrowers
700 C<@notify> array contains all file per borrowers
702 C<$notify_id> contains the file number for the borrower number nad item number
707 my ($borrowernumber)=@_;
708 my $dbh = C4::Context->dbh;
709 my $query=qq| SELECT distinct(notify_id)
711 WHERE borrowernumber=?|;
713 my $sth=$dbh->prepare($query);
714 $sth->execute($borrowernumber);
715 while ( my ($numberofnotify)=$sth->fetchrow){
716 push (@notify,$numberofnotify);
726 ($totalnotify) = &AmountNotify($notifyid);
728 Returns amount for all file per borrowers
729 C<$notifyid> is the file number
731 C<$totalnotify> contains amount of a file
733 C<$notify_id> contains the file number for the borrower number and item number
738 my ($notifyid,$borrowernumber)=@_;
739 my $dbh = C4::Context->dbh;
740 my $query=qq| SELECT sum(amountoutstanding)
742 WHERE notify_id=? AND borrowernumber = ?|;
743 my $sth=$dbh->prepare($query);
744 $sth->execute($notifyid,$borrowernumber);
745 my $totalnotify=$sth->fetchrow;
747 return ($totalnotify);
753 ($notify_id) = &GetNotifyId($borrowernumber,$itemnumber);
755 Returns the file number per borrower and itemnumber
757 C<$borrowernumber> is a reference-to-hash whose keys are all of the fields
758 from the items tables of the Koha database. Thus,
760 C<$itemnumber> contains the borrower categorycode
762 C<$notify_id> contains the file number for the borrower number nad item number
767 my ($borrowernumber,$itemnumber)=@_;
768 my $query=qq|SELECT notify_id
770 WHERE borrowernumber=?
772 AND (accounttype='FU' or accounttype='O')|;
773 my $dbh = C4::Context->dbh;
774 my $sth=$dbh->prepare($query);
775 $sth->execute($borrowernumber,$itemnumber);
776 my ($notify_id)=$sth->fetchrow;
782 =item CreateItemAccountLine
784 () = &CreateItemAccountLine($borrowernumber,$itemnumber,$date,$amount,$description,$accounttype,$amountoutstanding,$timestamp,$notify_id,$level);
786 update the account lines with file number or with file level
788 C<$items> is a reference-to-hash whose keys are all of the fields
789 from the items tables of the Koha database. Thus,
791 C<$itemnumber> contains the item number
793 C<$borrowernumber> contains the borrower number
795 C<$date> contains the date of the day
797 C<$amount> contains item price
799 C<$description> contains the descritpion of accounttype
801 C<$accounttype> contains the account type
803 C<$amountoutstanding> contains the $amountoutstanding
805 C<$timestamp> contains the timestamp with time and the date of the day
807 C<$notify_id> contains the file number
809 C<$level> contains the file level
814 sub CreateItemAccountLine {
815 my ($borrowernumber,$itemnumber,$date,$amount,$description,$accounttype,$amountoutstanding,$timestamp,$notify_id,$level)=@_;
816 my $dbh = C4::Context->dbh;
817 my $nextaccntno = C4::Accounts::getnextacctno($borrowernumber);
818 my $query= "INSERT into accountlines
819 (borrowernumber,accountno,itemnumber,date,amount,description,accounttype,amountoutstanding,timestamp,notify_id,notify_level)
821 (?,?,?,?,?,?,?,?,?,?,?)";
824 my $sth=$dbh->prepare($query);
825 $sth->execute($borrowernumber,$nextaccntno,$itemnumber,$date,$amount,$description,$accounttype,$amountoutstanding,$timestamp,$notify_id,$level);
829 =item UpdateAccountLines
831 () = &UpdateAccountLines($notify_id,$notify_level,$borrowernumber,$itemnumber);
833 update the account lines with file number or with file level
835 C<$items> is a reference-to-hash whose keys are all of the fields
836 from the items tables of the Koha database. Thus,
838 C<$itemnumber> contains the item number
840 C<$notify_id> contains the file number
842 C<$notify_level> contains the file level
844 C<$borrowernumber> contains the borrowernumber
848 sub UpdateAccountLines {
849 my ($notify_id,$notify_level,$borrowernumber,$itemnumber)=@_;
851 if ($notify_id eq '')
854 $query=qq|UPDATE accountlines
856 WHERE borrowernumber=? AND itemnumber=?
857 AND (accounttype='FU' or accounttype='O')|;
860 $query=qq|UPDATE accountlines
861 SET notify_id=?, notify_level=?
862 WHERE borrowernumber=?
864 AND (accounttype='FU' or accounttype='O')|;
866 my $dbh = C4::Context->dbh;
867 my $sth=$dbh->prepare($query);
869 if ($notify_id eq '')
871 $sth->execute($notify_level,$borrowernumber,$itemnumber);
874 $sth->execute($notify_id,$notify_level,$borrowernumber,$itemnumber);
883 ($items) = &GetItems($itemnumber);
885 Returns the list of all delays from overduerules.
887 C<$items> is a reference-to-hash whose keys are all of the fields
888 from the items tables of the Koha database. Thus,
890 C<$itemnumber> contains the borrower categorycode
895 my($itemnumber) = @_;
896 my $query=qq|SELECT *
899 my $dbh = C4::Context->dbh;
900 my $sth=$dbh->prepare($query);
901 $sth->execute($itemnumber);
902 my ($items)=$sth->fetchrow_hashref;
907 =item GetOverdueDelays
909 (@delays) = &GetOverdueDelays($categorycode);
911 Returns the list of all delays from overduerules.
913 C<@delays> it's an array contains the three delays from overduerules table
915 C<$categorycode> contains the borrower categorycode
919 sub GetOverdueDelays {
921 my $dbh = C4::Context->dbh;
922 my $query=qq|SELECT delay1,delay2,delay3
924 WHERE categorycode=?|;
925 my $sth=$dbh->prepare($query);
926 $sth->execute($category);
927 my (@delays)=$sth->fetchrow_array;
932 =item CheckAccountLineLevelInfo
934 ($exist) = &CheckAccountLineLevelInfo($borrowernumber,$itemnumber,$accounttype,notify_level);
936 Check and Returns the list of all overdue books.
938 C<$exist> contains number of line in accounlines
939 with the same .biblionumber,itemnumber,accounttype,and notify_level
941 C<$borrowernumber> contains the borrower number
943 C<$itemnumber> contains item number
945 C<$accounttype> contains account type
947 C<$notify_level> contains the accountline level
952 sub CheckAccountLineLevelInfo {
953 my($borrowernumber,$itemnumber,$level) = @_;
954 my $dbh = C4::Context->dbh;
955 my $query= qq|SELECT count(*)
957 WHERE borrowernumber =?
960 my $sth=$dbh->prepare($query);
961 $sth->execute($borrowernumber,$itemnumber,$level);
962 my ($exist)=$sth->fetchrow;
967 =item GetOverduerules
969 ($overduerules) = &GetOverduerules($categorycode);
971 Returns the value of borrowers (debarred or not) with notify level
973 C<$overduerules> return value of debbraed field in overduerules table
975 C<$category> contains the borrower categorycode
977 C<$notify_level> contains the notify level
982 my($category,$notify_level) = @_;
983 my $dbh = C4::Context->dbh;
984 my $query=qq|SELECT debarred$notify_level
986 WHERE categorycode=?|;
987 my $sth=$dbh->prepare($query);
988 $sth->execute($category);
989 my ($overduerules)=$sth->fetchrow;
991 return($overduerules);
995 =item CheckBorrowerDebarred
997 ($debarredstatus) = &CheckBorrowerDebarred($borrowernumber);
999 Check if the borrowers is already debarred
1001 C<$debarredstatus> return 0 for not debarred and return 1 for debarred
1003 C<$borrowernumber> contains the borrower number
1008 sub CheckBorrowerDebarred{
1009 my($borrowernumber) = @_;
1010 my $dbh = C4::Context->dbh;
1011 my $query=qq|SELECT debarred
1013 WHERE borrowernumber=?
1015 my $sth=$dbh->prepare($query);
1016 $sth->execute($borrowernumber);
1017 my ($debarredstatus)=$sth->fetchrow;
1019 if ($debarredstatus eq '1'){
1026 =item UpdateBorrowerDebarred
1028 ($borrowerstatut) = &UpdateBorrowerDebarred($borrowernumber);
1030 update status of borrowers in borrowers table (field debarred)
1032 C<$borrowernumber> borrower number
1036 sub UpdateBorrowerDebarred{
1037 my($borrowernumber) = @_;
1038 my $dbh = C4::Context->dbh;
1039 my $query=qq|UPDATE borrowers
1041 WHERE borrowernumber=?
1043 my $sth=$dbh->prepare($query);
1044 $sth->execute($borrowernumber);
1049 =item CheckExistantNotifyid
1051 ($exist) = &CheckExistantNotifyid($borrowernumber,$itemnumber,$accounttype,$notify_id);
1053 Check and Returns the notify id if exist else return 0.
1055 C<$exist> contains a notify_id
1057 C<$borrowernumber> contains the borrower number
1059 C<$date_due> contains the date of item return
1064 sub CheckExistantNotifyid {
1065 my($borrowernumber,$date_due) = @_;
1066 my $dbh = C4::Context->dbh;
1067 my $query = qq|SELECT notify_id FROM accountlines
1068 LEFT JOIN issues ON issues.itemnumber= accountlines.itemnumber
1069 WHERE accountlines.borrowernumber =?
1071 my $sth=$dbh->prepare($query);
1072 $sth->execute($borrowernumber,$date_due);
1073 my ($exist)=$sth->fetchrow;
1084 =item CheckAccountLineItemInfo
1086 ($exist) = &CheckAccountLineItemInfo($borrowernumber,$itemnumber,$accounttype,$notify_id);
1088 Check and Returns the list of all overdue items from the same file number(notify_id).
1090 C<$exist> contains number of line in accounlines
1091 with the same .biblionumber,itemnumber,accounttype,notify_id
1093 C<$borrowernumber> contains the borrower number
1095 C<$itemnumber> contains item number
1097 C<$accounttype> contains account type
1099 C<$notify_id> contains the file number
1103 sub CheckAccountLineItemInfo {
1104 my($borrowernumber,$itemnumber,$accounttype,$notify_id) = @_;
1105 my $dbh = C4::Context->dbh;
1106 my $query = qq|SELECT count(*) FROM accountlines
1107 WHERE borrowernumber =?
1111 my $sth=$dbh->prepare($query);
1112 $sth->execute($borrowernumber,$itemnumber,$accounttype,$notify_id);
1113 my ($exist)=$sth->fetchrow;
1118 =head2 CheckItemNotify
1120 Sql request to check if the document has alreday been notified
1121 this function is not exported, only used with GetOverduesForBranch
1125 sub CheckItemNotify {
1126 my ($notify_id,$notify_level,$itemnumber) = @_;
1127 my $dbh = C4::Context->dbh;
1128 my $sth = $dbh->prepare("
1129 SELECT COUNT(*) FROM notifys
1131 AND notify_level = ?
1132 AND itemnumber = ? ");
1133 $sth->execute($notify_id,$notify_level,$itemnumber);
1134 my $notified = $sth->fetchrow;
1139 =head2 GetOverduesForBranch
1141 Sql request for display all information for branchoverdues.pl
1142 2 possibilities : with or without location .
1143 display is filtered by branch
1147 sub GetOverduesForBranch {
1148 my ( $branch, $location) = @_;
1149 my $itype_link = (C4::Context->preference('item-level_itypes')) ? " items.itype " : " biblioitems.itemtype ";
1150 if ( not $location ) {
1151 my $dbh = C4::Context->dbh;
1152 my $sth = $dbh->prepare("
1155 borrowers.firstname,
1157 itemtypes.description,
1160 branches.branchname,
1164 items.itemcallnumber,
1165 borrowers.borrowernumber,
1167 biblio.biblionumber,
1169 accountlines.notify_id,
1170 accountlines.notify_level,
1172 accountlines.amountoutstanding
1174 LEFT JOIN issues ON issues.itemnumber = accountlines.itemnumber AND issues.borrowernumber = accountlines.borrowernumber
1175 LEFT JOIN borrowers ON borrowers.borrowernumber = accountlines.borrowernumber
1176 LEFT JOIN items ON items.itemnumber = issues.itemnumber
1177 LEFT JOIN biblio ON biblio.biblionumber = items.biblionumber
1178 LEFT JOIN biblioitems ON biblioitems.biblioitemnumber=items.biblioitemnumber
1179 LEFT JOIN itemtypes ON itemtypes.itemtype = $itype_link
1180 LEFT JOIN branches ON branches.branchcode = issues.branchcode
1181 WHERE ( accountlines.amountoutstanding != '0.000000')
1182 AND ( accountlines.accounttype = 'FU')
1183 AND (issues.branchcode = ?)
1184 AND (issues.date_due <= NOW())
1185 ORDER BY borrowers.surname
1187 $sth->execute($branch);
1190 while ( my $data = $sth->fetchrow_hashref ) {
1191 #check if the document has already been notified
1192 my $countnotify = CheckItemNotify($data->{'notify_id'},$data->{'notify_level'},$data->{'itemnumber'});
1193 if ($countnotify eq '0'){
1194 $getoverdues[$i] = $data;
1198 return (@getoverdues);
1202 my $dbh = C4::Context->dbh;
1203 my $sth = $dbh->prepare( "
1204 SELECT borrowers.surname,
1205 borrowers.firstname,
1207 itemtypes.description,
1210 branches.branchname,
1214 items.itemcallnumber,
1215 borrowers.borrowernumber,
1217 biblio.biblionumber,
1219 accountlines.notify_id,
1220 accountlines.notify_level,
1222 accountlines.amountoutstanding
1224 LEFT JOIN issues ON issues.itemnumber = accountlines.itemnumber AND issues.borrowernumber = accountlines.borrowernumber
1225 LEFT JOIN borrowers ON borrowers.borrowernumber = accountlines.borrowernumber
1226 LEFT JOIN items ON items.itemnumber = issues.itemnumber
1227 LEFT JOIN biblio ON biblio.biblionumber = items.biblionumber
1228 LEFT JOIN biblioitems ON biblioitems.biblioitemnumber=items.biblioitemnumber
1229 LEFT JOIN itemtypes ON itemtypes.itemtype = $itype_link
1230 LEFT JOIN branches ON branches.branchcode = issues.branchcode
1231 WHERE ( accountlines.amountoutstanding != '0.000000')
1232 AND ( accountlines.accounttype = 'FU')
1233 AND (issues.branchcode = ? AND items.location = ?)
1234 AND (issues.date_due <= NOW())
1235 ORDER BY borrowers.surname
1237 $sth->execute( $branch, $location);
1240 while ( my $data = $sth->fetchrow_hashref ) {
1241 #check if the document has already been notified
1242 my $countnotify = CheckItemNotify($data->{'notify_id'},$data->{'notify_level'},$data->{'itemnumber'});
1243 if ($countnotify eq '0'){
1244 $getoverdues[$i] = $data;
1249 return (@getoverdues);
1254 =head2 AddNotifyLine
1256 &AddNotifyLine($borrowernumber, $itemnumber, $overduelevel, $method, $notifyId)
1258 Creat a line into notify, if the method is phone, the notification_send_date is implemented to
1263 my ( $borrowernumber, $itemnumber, $overduelevel, $method, $notifyId ) = @_;
1264 if ( $method eq "phone" ) {
1265 my $dbh = C4::Context->dbh;
1266 my $sth = $dbh->prepare(
1267 "INSERT INTO notifys (borrowernumber,itemnumber,notify_date,notify_send_date,notify_level,method,notify_id)
1268 VALUES (?,?,now(),now(),?,?,?)"
1270 $sth->execute( $borrowernumber, $itemnumber, $overduelevel, $method,
1275 my $dbh = C4::Context->dbh;
1276 my $sth = $dbh->prepare(
1277 "INSERT INTO notifys (borrowernumber,itemnumber,notify_date,notify_level,method,notify_id)
1278 VALUES (?,?,now(),?,?,?)"
1280 $sth->execute( $borrowernumber, $itemnumber, $overduelevel, $method,
1287 =head2 RemoveNotifyLine
1289 &RemoveNotifyLine( $borrowernumber, $itemnumber, $notify_date );
1291 Cancel a notification
1295 sub RemoveNotifyLine {
1296 my ( $borrowernumber, $itemnumber, $notify_date ) = @_;
1297 my $dbh = C4::Context->dbh;
1298 my $sth = $dbh->prepare(
1299 "DELETE FROM notifys
1305 $sth->execute( $borrowernumber, $itemnumber, $notify_date );
1317 Koha Developement team <info@koha.org>