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
30 use vars qw($VERSION @ISA @EXPORT);
33 # set the version for version checking
37 # subs to rename (and maybe merge some...)
42 &CheckAccountLineLevelInfo
43 &CheckAccountLineItemInfo
44 &CheckExistantNotifyid
54 &CreateItemAccountLine
67 # check that an equivalent don't exist already before moving
69 # subs to move to Circulation.pm
74 # &GetIssuingRules - delete.
75 # use C4::Circulation::GetIssuingRule instead.
77 # subs to move to Members.pm
79 &CheckBorrowerDebarred
80 &UpdateBorrowerDebarred
82 # subs to move to Biblio.pm
91 C4::Circulation::Fines - Koha module dealing with fines
99 This module contains several functions for dealing with fines for
100 overdue items. It is primarily used by the 'misc/fines2.pl' script.
108 $overdues = Getoverdues( { minimumdays => 1, maximumdays => 30 } );
110 Returns the list of all overdue books, with their itemtype.
112 C<$overdues> is a reference-to-array. Each element is a
113 reference-to-hash whose keys are the fields of the issues table in the
122 my $dbh = C4::Context->dbh;
124 if ( C4::Context->preference('item-level_itypes') ) {
126 SELECT issues.*,items.itype as itemtype, items.homebranch FROM issues
127 LEFT JOIN items USING (itemnumber)
128 WHERE date_due < now()
132 SELECT issues.*,biblioitems.itemtype,items.itype, items.homebranch FROM issues
133 LEFT JOIN items USING (itemnumber)
134 LEFT JOIN biblioitems USING (biblioitemnumber)
135 WHERE date_due < now()
140 if ( exists $params->{'minimumdays'} and exists $params->{'maximumdays'} ) {
141 $statement .= ' AND TO_DAYS( NOW() )-TO_DAYS( date_due ) BETWEEN ? and ? ';
142 push @bind_parameters, $params->{'minimumdays'}, $params->{'maximumdays'};
143 } elsif ( exists $params->{'minimumdays'} ) {
144 $statement .= ' AND ( TO_DAYS( NOW() )-TO_DAYS( date_due ) ) > ? ';
145 push @bind_parameters, $params->{'minimumdays'};
146 } elsif ( exists $params->{'maximumdays'} ) {
147 $statement .= ' AND ( TO_DAYS( NOW() )-TO_DAYS( date_due ) ) < ? ';
148 push @bind_parameters, $params->{'maximumdays'};
150 $statement .= 'ORDER BY borrowernumber';
151 my $sth = $dbh->prepare( $statement );
152 $sth->execute( @bind_parameters );
153 return $sth->fetchall_arrayref({});
159 ( $count, $overdueitems )=checkoverdues( $borrowernumber, $dbh );
167 # From Main.pm, modified to return a list of overdueitems, in addition to a count
168 #checks whether a borrower has overdue items
169 my ( $borrowernumber, $dbh ) = @_;
170 my @datearr = localtime;
172 ( $datearr[5] + 1900 ) . "-" . ( $datearr[4] + 1 ) . "-" . $datearr[3];
175 my $sth = $dbh->prepare(
176 "SELECT * FROM issues
177 LEFT JOIN items ON issues.itemnumber = items.itemnumber
178 LEFT JOIN biblio ON items.biblionumber=biblio.biblionumber
179 LEFT JOIN biblioitems ON items.biblioitemnumber = biblioitems.biblioitemnumber
180 WHERE issues.borrowernumber = ?
181 AND issues.date_due < ?"
183 $sth->execute( $borrowernumber, $today );
184 while ( my $data = $sth->fetchrow_hashref ) {
185 push( @overdueitems, $data );
189 return ( $count, \@overdueitems );
194 ($amount, $chargename, $daycount, $daycounttotal) =
195 &CalcFine($item, $categorycode, $branch, $days_overdue, $description, $start_date, $end_date );
197 Calculates the fine for a book.
199 The issuingrules table in the Koha database is a fine matrix, listing
200 the penalties for each type of patron for each type of item and each branch (e.g., the
201 standard fine for books might be $0.50, but $1.50 for DVDs, or staff
202 members might get a longer grace period between the first and second
203 reminders that a book is overdue).
206 C<$item> is an item object (hashref).
208 C<$categorycode> is the category code (string) of the patron who currently has
211 C<$branchcode> is the library (string) whose issuingrules govern this transaction.
213 C<$days_overdue> is the number of days elapsed since the book's due date.
214 NOTE: supplying days_overdue is deprecated.
216 C<$start_date> & C<$end_date> are C4::Dates objects
217 defining the date range over which to determine the fine.
218 Note that if these are defined, we ignore C<$difference> and C<$dues> ,
219 but retain these for backwards-comptibility with extant fines scripts.
221 Fines scripts should just supply the date range over which to calculate the fine.
223 C<&CalcFine> returns four values:
225 C<$amount> is the fine owed by the patron (see above).
227 C<$chargename> is the chargename field from the applicable record in
228 the categoryitem table, whatever that is.
230 C<$daycount> is the number of days between start and end dates, Calendar adjusted (where needed),
231 minus any applicable grace period.
233 C<$daycounttotal> is C<$daycount> without consideration of grace period.
235 FIXME - What is chargename supposed to be ?
237 FIXME: previously attempted to return C<$message> as a text message, either "First Notice", "Second Notice",
238 or "Final Notice". But CalcFine never defined any value.
244 my ( $item, $bortype, $branchcode, $difference ,$dues , $start_date, $end_date ) = @_;
245 $debug and warn sprintf("CalcFine(%s, %s, %s, %s, %s, %s, %s)",
246 ($item ? '{item}' : 'UNDEF'),
247 ($bortype || 'UNDEF'),
248 ($branchcode || 'UNDEF'),
249 ($difference || 'UNDEF'),
251 ($start_date ? ($start_date->output('iso') || 'Not a C4::Dates object') : 'UNDEF'),
252 ( $end_date ? ( $end_date->output('iso') || 'Not a C4::Dates object') : 'UNDEF')
254 my $dbh = C4::Context->dbh;
257 # get issuingrules (fines part will be used)
258 my $data = C4::Circulation::GetIssuingRule($bortype, $item->{'itemtype'},$branchcode);
260 # if $difference is supplied, the difference has already been calculated, but we still need to adjust for the calendar.
261 # use copy-pasted functions from calendar module. (deprecated -- these functions will be removed from C4::Overdues ).
262 my $countspecialday = &GetSpecialHolidays($dues,$item->{itemnumber});
263 my $countrepeatableday = &GetRepeatableHolidays($dues,$item->{itemnumber},$difference);
264 my $countalldayclosed = $countspecialday + $countrepeatableday;
265 $daystocharge = $difference - $countalldayclosed;
267 # if $difference is not supplied, we have C4::Dates objects giving us the date range, and we use the calendar module.
268 if(C4::Context->preference('finesCalendar') eq 'noFinesWhenClosed') {
269 my $calendar = C4::Calendar->new( branchcode => $branchcode );
270 $daystocharge = $calendar->daysBetween( $start_date, $end_date );
272 $daystocharge = Date_to_Days(split('-',$end_date->output('iso'))) - Date_to_Days(split('-',$start_date->output('iso')));
275 # correct for grace period.
276 my $days_minus_grace = $daystocharge - $data->{'firstremind'};
277 if ($data->{'chargeperiod'} > 0 && $days_minus_grace > 0 ) {
278 $amount = int($days_minus_grace / $data->{'chargeperiod'}) * $data->{'fine'};
280 # a zero (or null) chargeperiod means no charge.
282 return ( $amount, $data->{'chargename'}, $days_minus_grace, $daystocharge);
286 =item GetSpecialHolidays
288 &GetSpecialHolidays($date_dues,$itemnumber);
290 return number of special days between date of the day and date due
292 C<$date_dues> is the envisaged date of book return.
294 C<$itemnumber> is the book's item number.
298 sub GetSpecialHolidays {
299 my ($date_dues,$itemnumber) = @_;
300 # calcul the today date
301 my $today = join "-", &Today();
303 # return the holdingbranch
304 my $iteminfo=GetIssuesIteminfo($itemnumber);
305 # use sql request to find all date between date_due and today
306 my $dbh = C4::Context->dbh;
307 my $query=qq|SELECT DATE_FORMAT(concat(year,'-',month,'-',day),'%Y-%m-%d')as date
308 FROM `special_holidays`
309 WHERE DATE_FORMAT(concat(year,'-',month,'-',day),'%Y-%m-%d') >= ?
310 AND DATE_FORMAT(concat(year,'-',month,'-',day),'%Y-%m-%d') <= ?
313 my @result=GetWdayFromItemnumber($itemnumber);
317 my $sth = $dbh->prepare($query);
318 $sth->execute($date_dues,$today,$iteminfo->{'branchcode'});
320 while ( my $special_date=$sth->fetchrow_hashref){
321 push (@result_date,$special_date);
324 my $specialdaycount=scalar(@result_date);
326 for (my $i=0;$i<scalar(@result_date);$i++){
327 $dateinsec=UnixDate($result_date[$i]->{'date'},"%o");
328 (undef,undef,undef,undef,undef,undef,$wday,undef,undef) =localtime($dateinsec);
329 for (my $j=0;$j<scalar(@result);$j++){
330 if ($wday == ($result[$j]->{'weekday'})){
336 return $specialdaycount;
339 =item GetRepeatableHolidays
341 &GetRepeatableHolidays($date_dues, $itemnumber, $difference,);
343 return number of day closed between date of the day and date due
345 C<$date_dues> is the envisaged date of book return.
347 C<$itemnumber> is item number.
349 C<$difference> numbers of between day date of the day and date due
353 sub GetRepeatableHolidays{
354 my ($date_dues,$itemnumber,$difference) = @_;
355 my $dateinsec=UnixDate($date_dues,"%o");
356 my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) =localtime($dateinsec);
357 my @result=GetWdayFromItemnumber($itemnumber);
361 for (my $i=0;$i<scalar(@result);$i++){
364 for ( $j=0;$j<$difference;$j++){
365 if ($result[$i]->{'weekday'} == $k)
367 push ( @dayclosedcount ,$k);
373 return scalar(@dayclosedcount);
377 =item GetWayFromItemnumber
379 &Getwdayfromitemnumber($itemnumber);
381 return the different week day from repeatable_holidays table
383 C<$itemnumber> is item number.
387 sub GetWdayFromItemnumber{
389 my $iteminfo=GetIssuesIteminfo($itemnumber);
391 my $dbh = C4::Context->dbh;
392 my $query = qq|SELECT weekday
393 FROM repeatable_holidays
396 my $sth = $dbh->prepare($query);
399 $sth->execute($iteminfo->{'branchcode'});
400 while ( my $weekday=$sth->fetchrow_hashref){
401 push (@result,$weekday);
407 =item GetIssuesIteminfo
409 &GetIssuesIteminfo($itemnumber);
411 return all data from issues about item
413 C<$itemnumber> is item number.
417 sub GetIssuesIteminfo{
419 my $dbh = C4::Context->dbh;
420 my $query = qq|SELECT *
424 my $sth = $dbh->prepare($query);
425 $sth->execute($itemnumber);
426 my ($issuesinfo)=$sth->fetchrow_hashref;
433 &UpdateFine($itemnumber, $borrowernumber, $amount, $type, $description);
435 (Note: the following is mostly conjecture and guesswork.)
437 Updates the fine owed on an overdue book.
439 C<$itemnumber> is the book's item number.
441 C<$borrowernumber> is the borrower number of the patron who currently
442 has the book on loan.
444 C<$amount> is the current amount owed by the patron.
446 C<$type> will be used in the description of the fine.
448 C<$description> is a string that must be present in the description of
449 the fine. I think this is expected to be a date in DD/MM/YYYY format.
451 C<&UpdateFine> looks up the amount currently owed on the given item
452 and sets it to C<$amount>, creating, if necessary, a new entry in the
453 accountlines table of the Koha database.
458 # Question: Why should the caller have to
459 # specify both the item number and the borrower number? A book can't
460 # be on loan to two different people, so the item number should be
463 # Possible Answer: You might update a fine for a damaged item, *after* it is returned.
466 my ( $itemnum, $borrowernumber, $amount, $type, $due ) = @_;
467 $debug and warn "UpdateFine($itemnum, $borrowernumber, $amount, " . ($type||'""') . ", $due) called";
468 my $dbh = C4::Context->dbh;
469 # FIXME - What exactly is this query supposed to do? It looks up an
470 # entry in accountlines that matches the given item and borrower
471 # numbers, where the description contains $due, and where the
472 # account type has one of several values, but what does this _mean_?
473 # Does it look up existing fines for this item?
474 # FIXME - What are these various account types? ("FU", "O", "F", "M")
476 # "A" is Account Management Fee
481 # "FU" is Fine UPDATE??
483 # "REF" is Cash Refund
484 my $sth = $dbh->prepare(
485 "SELECT * FROM accountlines
488 AND accounttype IN ('FU','O','F','M')
489 AND description like ? "
491 $sth->execute( $itemnum, $borrowernumber, "%$due%" );
493 if ( my $data = $sth->fetchrow_hashref ) {
495 # we're updating an existing fine.
496 if ( $data->{'amount'} != $amount ) {
497 my $diff = $amount - $data->{'amount'};
498 my $out = $data->{'amountoutstanding'} + $diff;
501 SET date=now(), amount=?, amountoutstanding=?,
502 lastincrement=?, accounttype='FU'
503 WHERE borrowernumber=?
505 AND accounttype IN ('FU','O')
506 AND description LIKE ?
508 my $sth2 = $dbh->prepare($query);
509 # FIXME: BOGUS query cannot ensure uniqueness w/ LIKE %x% !!!
510 # LIMIT 1 added to prevent multiple affected lines
511 # FIXME: accountlines table needs unique key!! Possibly a combo of borrowernumber and accountline.
512 # But actually, we should just have a regular autoincrementing PK and forget accountline,
513 # including the bogus getnextaccountno function (doesn't prevent conflict on simultaneous ops).
514 # FIXME: Why only 2 account types here?
515 $debug and print STDERR "UpdateFine query: $query\n" .
516 "w/ args: $amount, $out, $diff, $data->{'borrowernumber'}, $data->{'itemnumber'}, \"\%$due\%\"\n";
517 $sth2->execute($amount, $out, $diff, $data->{'borrowernumber'}, $data->{'itemnumber'}, "%$due%");
519 # print "no update needed $data->{'amount'}"
522 my $sth4 = $dbh->prepare(
523 "SELECT title FROM biblio LEFT JOIN items ON biblio.biblionumber=items.biblionumber WHERE items.itemnumber=?"
525 $sth4->execute($itemnum);
526 my $title = $sth4->fetchrow;
528 # # print "not in account";
529 # my $sth3 = $dbh->prepare("Select max(accountno) from accountlines");
532 # # FIXME - Make $accountno a scalar.
533 # my @accountno = $sth3->fetchrow_array;
537 my $nextaccntno = C4::Accounts::getnextacctno($borrowernumber);
538 my $desc = ($type ? "$type " : '') . "$title $due"; # FIXEDME, avoid whitespace prefix on empty $type
539 my $query = "INSERT INTO accountlines
540 (borrowernumber,itemnumber,date,amount,description,accounttype,amountoutstanding,lastincrement,accountno)
541 VALUES (?,?,now(),?,?,'FU',?,?,?)";
542 my $sth2 = $dbh->prepare($query);
543 $debug and print STDERR "UpdateFine query: $query\nw/ args: $borrowernumber, $itemnum, $amount, $desc, $amount, $amount, $nextaccntno\n";
544 $sth2->execute($borrowernumber, $itemnum, $amount, $desc, $amount, $amount, $nextaccntno);
551 "due=".$due." amount=".$amount." itemnumber=".$itemnum
552 ) if C4::Context->preference("FinesLog");
557 $borrower = &BorType($borrowernumber);
559 Looks up a patron by borrower number.
561 C<$borrower> is a reference-to-hash whose keys are all of the fields
562 from the borrowers and categories tables of the Koha database. Thus,
563 C<$borrower> contains all information about both the borrower and
564 category he or she belongs to.
570 my ($borrowernumber) = @_;
571 my $dbh = C4::Context->dbh;
572 my $sth = $dbh->prepare(
573 "SELECT * from borrowers
574 LEFT JOIN categories ON borrowers.categorycode=categories.categorycode
575 WHERE borrowernumber=?"
577 $sth->execute($borrowernumber);
578 my $data = $sth->fetchrow_hashref;
583 =item ReplacementCost
585 $cost = &ReplacementCost($itemnumber);
587 Returns the replacement cost of the item with the given item number.
592 sub ReplacementCost {
594 my $dbh = C4::Context->dbh;
596 $dbh->prepare("Select replacementprice from items where itemnumber=?");
597 $sth->execute($itemnum);
599 # FIXME - Use fetchrow_array or something.
600 my $data = $sth->fetchrow_hashref;
602 return ( $data->{'replacementprice'} );
607 $data->{'sum(amountoutstanding)'} = &GetFine($itemnum,$borrowernumber);
609 return the total of fine
611 C<$itemnum> is item number
613 C<$borrowernumber> is the borrowernumber
619 my ( $itemnum, $borrowernumber ) = @_;
620 my $dbh = C4::Context->dbh();
621 my $query = "SELECT sum(amountoutstanding) FROM accountlines
622 where accounttype like 'F%'
623 AND amountoutstanding > 0 AND itemnumber = ? AND borrowernumber=?";
624 my $sth = $dbh->prepare($query);
625 $sth->execute( $itemnum, $borrowernumber );
626 my $data = $sth->fetchrow_hashref();
627 return ( $data->{'sum(amountoutstanding)'} );
631 =item GetIssuingRules
633 FIXME - This sub should be deprecated and removed.
634 It ignores branch and defaults.
636 $data = &GetIssuingRules($itemtype,$categorycode);
638 Looks up for all issuingrules an item info
640 C<$itemnumber> is a reference-to-hash whose keys are all of the fields
641 from the borrowers and categories tables of the Koha database. Thus,
643 C<$categorycode> contains information about borrowers category
645 C<$data> contains all information about both the borrower and
646 category he or she belongs to.
649 sub GetIssuingRules {
650 warn "GetIssuingRules is deprecated: use GetIssuingRule from C4::Circulation instead.";
651 my ($itemtype,$categorycode)=@_;
652 my $dbh = C4::Context->dbh();
653 my $query=qq|SELECT *
655 WHERE issuingrules.itemtype=?
656 AND issuingrules.categorycode=?
658 my $sth = $dbh->prepare($query);
660 $sth->execute($itemtype,$categorycode);
661 return $sth->fetchrow_hashref;
665 sub ReplacementCost2 {
666 my ( $itemnum, $borrowernumber ) = @_;
667 my $dbh = C4::Context->dbh();
668 my $query = "SELECT amountoutstanding
670 WHERE accounttype like 'L'
671 AND amountoutstanding > 0
673 AND borrowernumber= ?";
674 my $sth = $dbh->prepare($query);
675 $sth->execute( $itemnum, $borrowernumber );
676 my $data = $sth->fetchrow_hashref();
677 return ( $data->{'amountoutstanding'} );
681 =item GetNextIdNotify
683 ($result) = &GetNextIdNotify($reference);
685 Returns the new file number
687 C<$result> contains the next file number
689 C<$reference> contains the beggining of file number
695 sub GetNextIdNotify {
697 my $query=qq|SELECT max(notify_id)
699 WHERE notify_id like \"$reference%\"
701 # AND borrowernumber=?|;
702 my $dbh = C4::Context->dbh;
703 my $sth=$dbh->prepare($query);
705 my $result=$sth->fetchrow;
710 ($result=$reference."01") ;
713 $count=substr($result,6)+1;
716 ($count = "0".$count);
718 $result=$reference.$count;
726 (@notify) = &NumberNotifyId($borrowernumber);
728 Returns amount for all file per borrowers
729 C<@notify> array contains all file per borrowers
731 C<$notify_id> contains the file number for the borrower number nad item number
736 my ($borrowernumber)=@_;
737 my $dbh = C4::Context->dbh;
738 my $query=qq| SELECT distinct(notify_id)
740 WHERE borrowernumber=?|;
742 my $sth=$dbh->prepare($query);
743 $sth->execute($borrowernumber);
744 while ( my ($numberofnotify)=$sth->fetchrow){
745 push (@notify,$numberofnotify);
755 ($totalnotify) = &AmountNotify($notifyid);
757 Returns amount for all file per borrowers
758 C<$notifyid> is the file number
760 C<$totalnotify> contains amount of a file
762 C<$notify_id> contains the file number for the borrower number and item number
767 my ($notifyid,$borrowernumber)=@_;
768 my $dbh = C4::Context->dbh;
769 my $query=qq| SELECT sum(amountoutstanding)
771 WHERE notify_id=? AND borrowernumber = ?|;
772 my $sth=$dbh->prepare($query);
773 $sth->execute($notifyid,$borrowernumber);
774 my $totalnotify=$sth->fetchrow;
776 return ($totalnotify);
782 ($notify_id) = &GetNotifyId($borrowernumber,$itemnumber);
784 Returns the file number per borrower and itemnumber
786 C<$borrowernumber> is a reference-to-hash whose keys are all of the fields
787 from the items tables of the Koha database. Thus,
789 C<$itemnumber> contains the borrower categorycode
791 C<$notify_id> contains the file number for the borrower number nad item number
796 my ($borrowernumber,$itemnumber)=@_;
797 my $query=qq|SELECT notify_id
799 WHERE borrowernumber=?
801 AND (accounttype='FU' or accounttype='O')|;
802 my $dbh = C4::Context->dbh;
803 my $sth=$dbh->prepare($query);
804 $sth->execute($borrowernumber,$itemnumber);
805 my ($notify_id)=$sth->fetchrow;
811 =item CreateItemAccountLine
813 () = &CreateItemAccountLine($borrowernumber,$itemnumber,$date,$amount,$description,$accounttype,$amountoutstanding,$timestamp,$notify_id,$level);
815 update the account lines with file number or with file level
817 C<$items> is a reference-to-hash whose keys are all of the fields
818 from the items tables of the Koha database. Thus,
820 C<$itemnumber> contains the item number
822 C<$borrowernumber> contains the borrower number
824 C<$date> contains the date of the day
826 C<$amount> contains item price
828 C<$description> contains the descritpion of accounttype
830 C<$accounttype> contains the account type
832 C<$amountoutstanding> contains the $amountoutstanding
834 C<$timestamp> contains the timestamp with time and the date of the day
836 C<$notify_id> contains the file number
838 C<$level> contains the file level
843 sub CreateItemAccountLine {
844 my ($borrowernumber,$itemnumber,$date,$amount,$description,$accounttype,$amountoutstanding,$timestamp,$notify_id,$level)=@_;
845 my $dbh = C4::Context->dbh;
846 my $nextaccntno = C4::Accounts::getnextacctno($borrowernumber);
847 my $query= "INSERT into accountlines
848 (borrowernumber,accountno,itemnumber,date,amount,description,accounttype,amountoutstanding,timestamp,notify_id,notify_level)
850 (?,?,?,?,?,?,?,?,?,?,?)";
853 my $sth=$dbh->prepare($query);
854 $sth->execute($borrowernumber,$nextaccntno,$itemnumber,$date,$amount,$description,$accounttype,$amountoutstanding,$timestamp,$notify_id,$level);
858 =item UpdateAccountLines
860 () = &UpdateAccountLines($notify_id,$notify_level,$borrowernumber,$itemnumber);
862 update the account lines with file number or with file level
864 C<$items> is a reference-to-hash whose keys are all of the fields
865 from the items tables of the Koha database. Thus,
867 C<$itemnumber> contains the item number
869 C<$notify_id> contains the file number
871 C<$notify_level> contains the file level
873 C<$borrowernumber> contains the borrowernumber
877 sub UpdateAccountLines {
878 my ($notify_id,$notify_level,$borrowernumber,$itemnumber)=@_;
880 if ($notify_id eq '')
883 $query=qq|UPDATE accountlines
885 WHERE borrowernumber=? AND itemnumber=?
886 AND (accounttype='FU' or accounttype='O')|;
889 $query=qq|UPDATE accountlines
890 SET notify_id=?, notify_level=?
891 WHERE borrowernumber=?
893 AND (accounttype='FU' or accounttype='O')|;
895 my $dbh = C4::Context->dbh;
896 my $sth=$dbh->prepare($query);
898 if ($notify_id eq '')
900 $sth->execute($notify_level,$borrowernumber,$itemnumber);
903 $sth->execute($notify_id,$notify_level,$borrowernumber,$itemnumber);
912 ($items) = &GetItems($itemnumber);
914 Returns the list of all delays from overduerules.
916 C<$items> is a reference-to-hash whose keys are all of the fields
917 from the items tables of the Koha database. Thus,
919 C<$itemnumber> contains the borrower categorycode
924 my($itemnumber) = @_;
925 my $query=qq|SELECT *
928 my $dbh = C4::Context->dbh;
929 my $sth=$dbh->prepare($query);
930 $sth->execute($itemnumber);
931 my ($items)=$sth->fetchrow_hashref;
936 =item GetOverdueDelays
938 (@delays) = &GetOverdueDelays($categorycode);
940 Returns the list of all delays from overduerules.
942 C<@delays> it's an array contains the three delays from overduerules table
944 C<$categorycode> contains the borrower categorycode
948 sub GetOverdueDelays {
950 my $dbh = C4::Context->dbh;
951 my $query=qq|SELECT delay1,delay2,delay3
953 WHERE categorycode=?|;
954 my $sth=$dbh->prepare($query);
955 $sth->execute($category);
956 my (@delays)=$sth->fetchrow_array;
961 =head2 GetBranchcodesWithOverdueRules
965 my @branchcodes = C4::Overdues::GetBranchcodesWithOverdueRules()
967 returns a list of branch codes for branches with overdue rules defined.
973 sub GetBranchcodesWithOverdueRules {
974 my $dbh = C4::Context->dbh;
975 my $rqoverduebranches = $dbh->prepare("SELECT DISTINCT branchcode FROM overduerules WHERE delay1 IS NOT NULL");
976 $rqoverduebranches->execute;
977 my @branches = map { shift @$_ } @{ $rqoverduebranches->fetchall_arrayref };
978 $rqoverduebranches->finish;
982 =item CheckAccountLineLevelInfo
984 ($exist) = &CheckAccountLineLevelInfo($borrowernumber,$itemnumber,$accounttype,notify_level);
986 Check and Returns the list of all overdue books.
988 C<$exist> contains number of line in accounlines
989 with the same .biblionumber,itemnumber,accounttype,and notify_level
991 C<$borrowernumber> contains the borrower number
993 C<$itemnumber> contains item number
995 C<$accounttype> contains account type
997 C<$notify_level> contains the accountline level
1002 sub CheckAccountLineLevelInfo {
1003 my($borrowernumber,$itemnumber,$level) = @_;
1004 my $dbh = C4::Context->dbh;
1005 my $query= qq|SELECT count(*)
1007 WHERE borrowernumber =?
1009 AND notify_level=?|;
1010 my $sth=$dbh->prepare($query);
1011 $sth->execute($borrowernumber,$itemnumber,$level);
1012 my ($exist)=$sth->fetchrow;
1017 =item GetOverduerules
1019 ($overduerules) = &GetOverduerules($categorycode);
1021 Returns the value of borrowers (debarred or not) with notify level
1023 C<$overduerules> return value of debbraed field in overduerules table
1025 C<$category> contains the borrower categorycode
1027 C<$notify_level> contains the notify level
1031 sub GetOverduerules{
1032 my($category,$notify_level) = @_;
1033 my $dbh = C4::Context->dbh;
1034 my $query=qq|SELECT debarred$notify_level
1036 WHERE categorycode=?|;
1037 my $sth=$dbh->prepare($query);
1038 $sth->execute($category);
1039 my ($overduerules)=$sth->fetchrow;
1041 return($overduerules);
1045 =item CheckBorrowerDebarred
1047 ($debarredstatus) = &CheckBorrowerDebarred($borrowernumber);
1049 Check if the borrowers is already debarred
1051 C<$debarredstatus> return 0 for not debarred and return 1 for debarred
1053 C<$borrowernumber> contains the borrower number
1058 sub CheckBorrowerDebarred{
1059 my($borrowernumber) = @_;
1060 my $dbh = C4::Context->dbh;
1061 my $query=qq|SELECT debarred
1063 WHERE borrowernumber=?
1065 my $sth=$dbh->prepare($query);
1066 $sth->execute($borrowernumber);
1067 my ($debarredstatus)=$sth->fetchrow;
1069 if ($debarredstatus eq '1'){
1076 =item UpdateBorrowerDebarred
1078 ($borrowerstatut) = &UpdateBorrowerDebarred($borrowernumber);
1080 update status of borrowers in borrowers table (field debarred)
1082 C<$borrowernumber> borrower number
1086 sub UpdateBorrowerDebarred{
1087 my($borrowernumber) = @_;
1088 my $dbh = C4::Context->dbh;
1089 my $query=qq|UPDATE borrowers
1091 WHERE borrowernumber=?
1093 my $sth=$dbh->prepare($query);
1094 $sth->execute($borrowernumber);
1099 =item CheckExistantNotifyid
1101 ($exist) = &CheckExistantNotifyid($borrowernumber,$itemnumber,$accounttype,$notify_id);
1103 Check and Returns the notify id if exist else return 0.
1105 C<$exist> contains a notify_id
1107 C<$borrowernumber> contains the borrower number
1109 C<$date_due> contains the date of item return
1114 sub CheckExistantNotifyid {
1115 my($borrowernumber,$date_due) = @_;
1116 my $dbh = C4::Context->dbh;
1117 my $query = qq|SELECT notify_id FROM accountlines
1118 LEFT JOIN issues ON issues.itemnumber= accountlines.itemnumber
1119 WHERE accountlines.borrowernumber =?
1121 my $sth=$dbh->prepare($query);
1122 $sth->execute($borrowernumber,$date_due);
1123 my ($exist)=$sth->fetchrow;
1134 =item CheckAccountLineItemInfo
1136 ($exist) = &CheckAccountLineItemInfo($borrowernumber,$itemnumber,$accounttype,$notify_id);
1138 Check and Returns the list of all overdue items from the same file number(notify_id).
1140 C<$exist> contains number of line in accounlines
1141 with the same .biblionumber,itemnumber,accounttype,notify_id
1143 C<$borrowernumber> contains the borrower number
1145 C<$itemnumber> contains item number
1147 C<$accounttype> contains account type
1149 C<$notify_id> contains the file number
1153 sub CheckAccountLineItemInfo {
1154 my($borrowernumber,$itemnumber,$accounttype,$notify_id) = @_;
1155 my $dbh = C4::Context->dbh;
1156 my $query = qq|SELECT count(*) FROM accountlines
1157 WHERE borrowernumber =?
1161 my $sth=$dbh->prepare($query);
1162 $sth->execute($borrowernumber,$itemnumber,$accounttype,$notify_id);
1163 my ($exist)=$sth->fetchrow;
1168 =head2 CheckItemNotify
1170 Sql request to check if the document has alreday been notified
1171 this function is not exported, only used with GetOverduesForBranch
1175 sub CheckItemNotify {
1176 my ($notify_id,$notify_level,$itemnumber) = @_;
1177 my $dbh = C4::Context->dbh;
1178 my $sth = $dbh->prepare("
1179 SELECT COUNT(*) FROM notifys
1181 AND notify_level = ?
1182 AND itemnumber = ? ");
1183 $sth->execute($notify_id,$notify_level,$itemnumber);
1184 my $notified = $sth->fetchrow;
1189 =head2 GetOverduesForBranch
1191 Sql request for display all information for branchoverdues.pl
1192 2 possibilities : with or without location .
1193 display is filtered by branch
1197 sub GetOverduesForBranch {
1198 my ( $branch, $location) = @_;
1199 my $itype_link = (C4::Context->preference('item-level_itypes')) ? " items.itype " : " biblioitems.itemtype ";
1200 if ( not $location ) {
1201 my $dbh = C4::Context->dbh;
1202 my $sth = $dbh->prepare("
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 = ?)
1234 AND (issues.date_due <= NOW())
1235 ORDER BY borrowers.surname
1237 $sth->execute($branch);
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;
1248 return (@getoverdues);
1252 my $dbh = C4::Context->dbh;
1253 my $sth = $dbh->prepare( "
1254 SELECT borrowers.surname,
1255 borrowers.firstname,
1257 itemtypes.description,
1260 branches.branchname,
1264 items.itemcallnumber,
1265 borrowers.borrowernumber,
1267 biblio.biblionumber,
1269 accountlines.notify_id,
1270 accountlines.notify_level,
1272 accountlines.amountoutstanding
1274 LEFT JOIN issues ON issues.itemnumber = accountlines.itemnumber AND issues.borrowernumber = accountlines.borrowernumber
1275 LEFT JOIN borrowers ON borrowers.borrowernumber = accountlines.borrowernumber
1276 LEFT JOIN items ON items.itemnumber = issues.itemnumber
1277 LEFT JOIN biblio ON biblio.biblionumber = items.biblionumber
1278 LEFT JOIN biblioitems ON biblioitems.biblioitemnumber=items.biblioitemnumber
1279 LEFT JOIN itemtypes ON itemtypes.itemtype = $itype_link
1280 LEFT JOIN branches ON branches.branchcode = issues.branchcode
1281 WHERE ( accountlines.amountoutstanding != '0.000000')
1282 AND ( accountlines.accounttype = 'FU')
1283 AND (issues.branchcode = ? AND items.location = ?)
1284 AND (issues.date_due <= NOW())
1285 ORDER BY borrowers.surname
1287 $sth->execute( $branch, $location);
1290 while ( my $data = $sth->fetchrow_hashref ) {
1291 #check if the document has already been notified
1292 my $countnotify = CheckItemNotify($data->{'notify_id'},$data->{'notify_level'},$data->{'itemnumber'});
1293 if ($countnotify eq '0'){
1294 $getoverdues[$i] = $data;
1299 return (@getoverdues);
1304 =head2 AddNotifyLine
1306 &AddNotifyLine($borrowernumber, $itemnumber, $overduelevel, $method, $notifyId)
1308 Creat a line into notify, if the method is phone, the notification_send_date is implemented to
1313 my ( $borrowernumber, $itemnumber, $overduelevel, $method, $notifyId ) = @_;
1314 if ( $method eq "phone" ) {
1315 my $dbh = C4::Context->dbh;
1316 my $sth = $dbh->prepare(
1317 "INSERT INTO notifys (borrowernumber,itemnumber,notify_date,notify_send_date,notify_level,method,notify_id)
1318 VALUES (?,?,now(),now(),?,?,?)"
1320 $sth->execute( $borrowernumber, $itemnumber, $overduelevel, $method,
1325 my $dbh = C4::Context->dbh;
1326 my $sth = $dbh->prepare(
1327 "INSERT INTO notifys (borrowernumber,itemnumber,notify_date,notify_level,method,notify_id)
1328 VALUES (?,?,now(),?,?,?)"
1330 $sth->execute( $borrowernumber, $itemnumber, $overduelevel, $method,
1337 =head2 RemoveNotifyLine
1339 &RemoveNotifyLine( $borrowernumber, $itemnumber, $notify_date );
1341 Cancel a notification
1345 sub RemoveNotifyLine {
1346 my ( $borrowernumber, $itemnumber, $notify_date ) = @_;
1347 my $dbh = C4::Context->dbh;
1348 my $sth = $dbh->prepare(
1349 "DELETE FROM notifys
1355 $sth->execute( $borrowernumber, $itemnumber, $notify_date );
1367 Koha Developement team <info@koha.org>