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 $amount = C4::Context->preference('maxFine') if(C4::Context->preference('maxFine') && ( $amount > C4::Context->preference('maxFine')));
283 return ( $amount, $data->{'chargename'}, $days_minus_grace, $daystocharge);
287 =item GetSpecialHolidays
289 &GetSpecialHolidays($date_dues,$itemnumber);
291 return number of special days between date of the day and date due
293 C<$date_dues> is the envisaged date of book return.
295 C<$itemnumber> is the book's item number.
299 sub GetSpecialHolidays {
300 my ($date_dues,$itemnumber) = @_;
301 # calcul the today date
302 my $today = join "-", &Today();
304 # return the holdingbranch
305 my $iteminfo=GetIssuesIteminfo($itemnumber);
306 # use sql request to find all date between date_due and today
307 my $dbh = C4::Context->dbh;
308 my $query=qq|SELECT DATE_FORMAT(concat(year,'-',month,'-',day),'%Y-%m-%d')as date
309 FROM `special_holidays`
310 WHERE DATE_FORMAT(concat(year,'-',month,'-',day),'%Y-%m-%d') >= ?
311 AND DATE_FORMAT(concat(year,'-',month,'-',day),'%Y-%m-%d') <= ?
314 my @result=GetWdayFromItemnumber($itemnumber);
318 my $sth = $dbh->prepare($query);
319 $sth->execute($date_dues,$today,$iteminfo->{'branchcode'});
321 while ( my $special_date=$sth->fetchrow_hashref){
322 push (@result_date,$special_date);
325 my $specialdaycount=scalar(@result_date);
327 for (my $i=0;$i<scalar(@result_date);$i++){
328 $dateinsec=UnixDate($result_date[$i]->{'date'},"%o");
329 (undef,undef,undef,undef,undef,undef,$wday,undef,undef) =localtime($dateinsec);
330 for (my $j=0;$j<scalar(@result);$j++){
331 if ($wday == ($result[$j]->{'weekday'})){
337 return $specialdaycount;
340 =item GetRepeatableHolidays
342 &GetRepeatableHolidays($date_dues, $itemnumber, $difference,);
344 return number of day closed between date of the day and date due
346 C<$date_dues> is the envisaged date of book return.
348 C<$itemnumber> is item number.
350 C<$difference> numbers of between day date of the day and date due
354 sub GetRepeatableHolidays{
355 my ($date_dues,$itemnumber,$difference) = @_;
356 my $dateinsec=UnixDate($date_dues,"%o");
357 my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) =localtime($dateinsec);
358 my @result=GetWdayFromItemnumber($itemnumber);
362 for (my $i=0;$i<scalar(@result);$i++){
365 for ( $j=0;$j<$difference;$j++){
366 if ($result[$i]->{'weekday'} == $k)
368 push ( @dayclosedcount ,$k);
374 return scalar(@dayclosedcount);
378 =item GetWayFromItemnumber
380 &Getwdayfromitemnumber($itemnumber);
382 return the different week day from repeatable_holidays table
384 C<$itemnumber> is item number.
388 sub GetWdayFromItemnumber{
390 my $iteminfo=GetIssuesIteminfo($itemnumber);
392 my $dbh = C4::Context->dbh;
393 my $query = qq|SELECT weekday
394 FROM repeatable_holidays
397 my $sth = $dbh->prepare($query);
400 $sth->execute($iteminfo->{'branchcode'});
401 while ( my $weekday=$sth->fetchrow_hashref){
402 push (@result,$weekday);
408 =item GetIssuesIteminfo
410 &GetIssuesIteminfo($itemnumber);
412 return all data from issues about item
414 C<$itemnumber> is item number.
418 sub GetIssuesIteminfo{
420 my $dbh = C4::Context->dbh;
421 my $query = qq|SELECT *
425 my $sth = $dbh->prepare($query);
426 $sth->execute($itemnumber);
427 my ($issuesinfo)=$sth->fetchrow_hashref;
434 &UpdateFine($itemnumber, $borrowernumber, $amount, $type, $description);
436 (Note: the following is mostly conjecture and guesswork.)
438 Updates the fine owed on an overdue book.
440 C<$itemnumber> is the book's item number.
442 C<$borrowernumber> is the borrower number of the patron who currently
443 has the book on loan.
445 C<$amount> is the current amount owed by the patron.
447 C<$type> will be used in the description of the fine.
449 C<$description> is a string that must be present in the description of
450 the fine. I think this is expected to be a date in DD/MM/YYYY format.
452 C<&UpdateFine> looks up the amount currently owed on the given item
453 and sets it to C<$amount>, creating, if necessary, a new entry in the
454 accountlines table of the Koha database.
459 # Question: Why should the caller have to
460 # specify both the item number and the borrower number? A book can't
461 # be on loan to two different people, so the item number should be
464 # Possible Answer: You might update a fine for a damaged item, *after* it is returned.
467 my ( $itemnum, $borrowernumber, $amount, $type, $due ) = @_;
468 $debug and warn "UpdateFine($itemnum, $borrowernumber, $amount, " . ($type||'""') . ", $due) called";
469 my $dbh = C4::Context->dbh;
470 # FIXME - What exactly is this query supposed to do? It looks up an
471 # entry in accountlines that matches the given item and borrower
472 # numbers, where the description contains $due, and where the
473 # account type has one of several values, but what does this _mean_?
474 # Does it look up existing fines for this item?
475 # FIXME - What are these various account types? ("FU", "O", "F", "M")
477 # "A" is Account Management Fee
482 # "FU" is Fine UPDATE??
484 # "REF" is Cash Refund
485 my $sth = $dbh->prepare(
486 "SELECT * FROM accountlines
489 AND accounttype IN ('FU','O','F','M')
490 AND description like ? "
492 $sth->execute( $itemnum, $borrowernumber, "%$due%" );
494 if ( my $data = $sth->fetchrow_hashref ) {
496 # we're updating an existing fine.
497 if ( $data->{'amount'} != $amount ) {
498 my $diff = $amount - $data->{'amount'};
499 my $out = $data->{'amountoutstanding'} + $diff;
502 SET date=now(), amount=?, amountoutstanding=?,
503 lastincrement=?, accounttype='FU'
504 WHERE borrowernumber=?
506 AND accounttype IN ('FU','O')
507 AND description LIKE ?
509 my $sth2 = $dbh->prepare($query);
510 # FIXME: BOGUS query cannot ensure uniqueness w/ LIKE %x% !!!
511 # LIMIT 1 added to prevent multiple affected lines
512 # FIXME: accountlines table needs unique key!! Possibly a combo of borrowernumber and accountline.
513 # But actually, we should just have a regular autoincrementing PK and forget accountline,
514 # including the bogus getnextaccountno function (doesn't prevent conflict on simultaneous ops).
515 # FIXME: Why only 2 account types here?
516 $debug and print STDERR "UpdateFine query: $query\n" .
517 "w/ args: $amount, $out, $diff, $data->{'borrowernumber'}, $data->{'itemnumber'}, \"\%$due\%\"\n";
518 $sth2->execute($amount, $out, $diff, $data->{'borrowernumber'}, $data->{'itemnumber'}, "%$due%");
520 # print "no update needed $data->{'amount'}"
523 my $sth4 = $dbh->prepare(
524 "SELECT title FROM biblio LEFT JOIN items ON biblio.biblionumber=items.biblionumber WHERE items.itemnumber=?"
526 $sth4->execute($itemnum);
527 my $title = $sth4->fetchrow;
529 # # print "not in account";
530 # my $sth3 = $dbh->prepare("Select max(accountno) from accountlines");
533 # # FIXME - Make $accountno a scalar.
534 # my @accountno = $sth3->fetchrow_array;
538 my $nextaccntno = C4::Accounts::getnextacctno($borrowernumber);
539 my $desc = ($type ? "$type " : '') . "$title $due"; # FIXEDME, avoid whitespace prefix on empty $type
540 my $query = "INSERT INTO accountlines
541 (borrowernumber,itemnumber,date,amount,description,accounttype,amountoutstanding,lastincrement,accountno)
542 VALUES (?,?,now(),?,?,'FU',?,?,?)";
543 my $sth2 = $dbh->prepare($query);
544 $debug and print STDERR "UpdateFine query: $query\nw/ args: $borrowernumber, $itemnum, $amount, $desc, $amount, $amount, $nextaccntno\n";
545 $sth2->execute($borrowernumber, $itemnum, $amount, $desc, $amount, $amount, $nextaccntno);
552 "due=".$due." amount=".$amount." itemnumber=".$itemnum
553 ) if C4::Context->preference("FinesLog");
558 $borrower = &BorType($borrowernumber);
560 Looks up a patron by borrower number.
562 C<$borrower> is a reference-to-hash whose keys are all of the fields
563 from the borrowers and categories tables of the Koha database. Thus,
564 C<$borrower> contains all information about both the borrower and
565 category he or she belongs to.
571 my ($borrowernumber) = @_;
572 my $dbh = C4::Context->dbh;
573 my $sth = $dbh->prepare(
574 "SELECT * from borrowers
575 LEFT JOIN categories ON borrowers.categorycode=categories.categorycode
576 WHERE borrowernumber=?"
578 $sth->execute($borrowernumber);
579 my $data = $sth->fetchrow_hashref;
584 =item ReplacementCost
586 $cost = &ReplacementCost($itemnumber);
588 Returns the replacement cost of the item with the given item number.
593 sub ReplacementCost {
595 my $dbh = C4::Context->dbh;
597 $dbh->prepare("Select replacementprice from items where itemnumber=?");
598 $sth->execute($itemnum);
600 # FIXME - Use fetchrow_array or something.
601 my $data = $sth->fetchrow_hashref;
603 return ( $data->{'replacementprice'} );
608 $data->{'sum(amountoutstanding)'} = &GetFine($itemnum,$borrowernumber);
610 return the total of fine
612 C<$itemnum> is item number
614 C<$borrowernumber> is the borrowernumber
620 my ( $itemnum, $borrowernumber ) = @_;
621 my $dbh = C4::Context->dbh();
622 my $query = "SELECT sum(amountoutstanding) FROM accountlines
623 where accounttype like 'F%'
624 AND amountoutstanding > 0 AND itemnumber = ? AND borrowernumber=?";
625 my $sth = $dbh->prepare($query);
626 $sth->execute( $itemnum, $borrowernumber );
627 my $data = $sth->fetchrow_hashref();
628 return ( $data->{'sum(amountoutstanding)'} );
632 =item GetIssuingRules
634 FIXME - This sub should be deprecated and removed.
635 It ignores branch and defaults.
637 $data = &GetIssuingRules($itemtype,$categorycode);
639 Looks up for all issuingrules an item info
641 C<$itemnumber> is a reference-to-hash whose keys are all of the fields
642 from the borrowers and categories tables of the Koha database. Thus,
644 C<$categorycode> contains information about borrowers category
646 C<$data> contains all information about both the borrower and
647 category he or she belongs to.
650 sub GetIssuingRules {
651 warn "GetIssuingRules is deprecated: use GetIssuingRule from C4::Circulation instead.";
652 my ($itemtype,$categorycode)=@_;
653 my $dbh = C4::Context->dbh();
654 my $query=qq|SELECT *
656 WHERE issuingrules.itemtype=?
657 AND issuingrules.categorycode=?
659 my $sth = $dbh->prepare($query);
661 $sth->execute($itemtype,$categorycode);
662 return $sth->fetchrow_hashref;
666 sub ReplacementCost2 {
667 my ( $itemnum, $borrowernumber ) = @_;
668 my $dbh = C4::Context->dbh();
669 my $query = "SELECT amountoutstanding
671 WHERE accounttype like 'L'
672 AND amountoutstanding > 0
674 AND borrowernumber= ?";
675 my $sth = $dbh->prepare($query);
676 $sth->execute( $itemnum, $borrowernumber );
677 my $data = $sth->fetchrow_hashref();
678 return ( $data->{'amountoutstanding'} );
682 =item GetNextIdNotify
684 ($result) = &GetNextIdNotify($reference);
686 Returns the new file number
688 C<$result> contains the next file number
690 C<$reference> contains the beggining of file number
696 sub GetNextIdNotify {
698 my $query=qq|SELECT max(notify_id)
700 WHERE notify_id like \"$reference%\"
702 # AND borrowernumber=?|;
703 my $dbh = C4::Context->dbh;
704 my $sth=$dbh->prepare($query);
706 my $result=$sth->fetchrow;
711 ($result=$reference."01") ;
714 $count=substr($result,6)+1;
717 ($count = "0".$count);
719 $result=$reference.$count;
727 (@notify) = &NumberNotifyId($borrowernumber);
729 Returns amount for all file per borrowers
730 C<@notify> array contains all file per borrowers
732 C<$notify_id> contains the file number for the borrower number nad item number
737 my ($borrowernumber)=@_;
738 my $dbh = C4::Context->dbh;
739 my $query=qq| SELECT distinct(notify_id)
741 WHERE borrowernumber=?|;
743 my $sth=$dbh->prepare($query);
744 $sth->execute($borrowernumber);
745 while ( my ($numberofnotify)=$sth->fetchrow){
746 push (@notify,$numberofnotify);
756 ($totalnotify) = &AmountNotify($notifyid);
758 Returns amount for all file per borrowers
759 C<$notifyid> is the file number
761 C<$totalnotify> contains amount of a file
763 C<$notify_id> contains the file number for the borrower number and item number
768 my ($notifyid,$borrowernumber)=@_;
769 my $dbh = C4::Context->dbh;
770 my $query=qq| SELECT sum(amountoutstanding)
772 WHERE notify_id=? AND borrowernumber = ?|;
773 my $sth=$dbh->prepare($query);
774 $sth->execute($notifyid,$borrowernumber);
775 my $totalnotify=$sth->fetchrow;
777 return ($totalnotify);
783 ($notify_id) = &GetNotifyId($borrowernumber,$itemnumber);
785 Returns the file number per borrower and itemnumber
787 C<$borrowernumber> is a reference-to-hash whose keys are all of the fields
788 from the items tables of the Koha database. Thus,
790 C<$itemnumber> contains the borrower categorycode
792 C<$notify_id> contains the file number for the borrower number nad item number
797 my ($borrowernumber,$itemnumber)=@_;
798 my $query=qq|SELECT notify_id
800 WHERE borrowernumber=?
802 AND (accounttype='FU' or accounttype='O')|;
803 my $dbh = C4::Context->dbh;
804 my $sth=$dbh->prepare($query);
805 $sth->execute($borrowernumber,$itemnumber);
806 my ($notify_id)=$sth->fetchrow;
812 =item CreateItemAccountLine
814 () = &CreateItemAccountLine($borrowernumber,$itemnumber,$date,$amount,$description,$accounttype,$amountoutstanding,$timestamp,$notify_id,$level);
816 update the account lines with file number or with file level
818 C<$items> is a reference-to-hash whose keys are all of the fields
819 from the items tables of the Koha database. Thus,
821 C<$itemnumber> contains the item number
823 C<$borrowernumber> contains the borrower number
825 C<$date> contains the date of the day
827 C<$amount> contains item price
829 C<$description> contains the descritpion of accounttype
831 C<$accounttype> contains the account type
833 C<$amountoutstanding> contains the $amountoutstanding
835 C<$timestamp> contains the timestamp with time and the date of the day
837 C<$notify_id> contains the file number
839 C<$level> contains the file level
844 sub CreateItemAccountLine {
845 my ($borrowernumber,$itemnumber,$date,$amount,$description,$accounttype,$amountoutstanding,$timestamp,$notify_id,$level)=@_;
846 my $dbh = C4::Context->dbh;
847 my $nextaccntno = C4::Accounts::getnextacctno($borrowernumber);
848 my $query= "INSERT into accountlines
849 (borrowernumber,accountno,itemnumber,date,amount,description,accounttype,amountoutstanding,timestamp,notify_id,notify_level)
851 (?,?,?,?,?,?,?,?,?,?,?)";
854 my $sth=$dbh->prepare($query);
855 $sth->execute($borrowernumber,$nextaccntno,$itemnumber,$date,$amount,$description,$accounttype,$amountoutstanding,$timestamp,$notify_id,$level);
859 =item UpdateAccountLines
861 () = &UpdateAccountLines($notify_id,$notify_level,$borrowernumber,$itemnumber);
863 update the account lines with file number or with file level
865 C<$items> is a reference-to-hash whose keys are all of the fields
866 from the items tables of the Koha database. Thus,
868 C<$itemnumber> contains the item number
870 C<$notify_id> contains the file number
872 C<$notify_level> contains the file level
874 C<$borrowernumber> contains the borrowernumber
878 sub UpdateAccountLines {
879 my ($notify_id,$notify_level,$borrowernumber,$itemnumber)=@_;
881 if ($notify_id eq '')
884 $query=qq|UPDATE accountlines
886 WHERE borrowernumber=? AND itemnumber=?
887 AND (accounttype='FU' or accounttype='O')|;
890 $query=qq|UPDATE accountlines
891 SET notify_id=?, notify_level=?
892 WHERE borrowernumber=?
894 AND (accounttype='FU' or accounttype='O')|;
896 my $dbh = C4::Context->dbh;
897 my $sth=$dbh->prepare($query);
899 if ($notify_id eq '')
901 $sth->execute($notify_level,$borrowernumber,$itemnumber);
904 $sth->execute($notify_id,$notify_level,$borrowernumber,$itemnumber);
913 ($items) = &GetItems($itemnumber);
915 Returns the list of all delays from overduerules.
917 C<$items> is a reference-to-hash whose keys are all of the fields
918 from the items tables of the Koha database. Thus,
920 C<$itemnumber> contains the borrower categorycode
925 my($itemnumber) = @_;
926 my $query=qq|SELECT *
929 my $dbh = C4::Context->dbh;
930 my $sth=$dbh->prepare($query);
931 $sth->execute($itemnumber);
932 my ($items)=$sth->fetchrow_hashref;
937 =item GetOverdueDelays
939 (@delays) = &GetOverdueDelays($categorycode);
941 Returns the list of all delays from overduerules.
943 C<@delays> it's an array contains the three delays from overduerules table
945 C<$categorycode> contains the borrower categorycode
949 sub GetOverdueDelays {
951 my $dbh = C4::Context->dbh;
952 my $query=qq|SELECT delay1,delay2,delay3
954 WHERE categorycode=?|;
955 my $sth=$dbh->prepare($query);
956 $sth->execute($category);
957 my (@delays)=$sth->fetchrow_array;
962 =head2 GetBranchcodesWithOverdueRules
966 my @branchcodes = C4::Overdues::GetBranchcodesWithOverdueRules()
968 returns a list of branch codes for branches with overdue rules defined.
974 sub GetBranchcodesWithOverdueRules {
975 my $dbh = C4::Context->dbh;
976 my $rqoverduebranches = $dbh->prepare("SELECT DISTINCT branchcode FROM overduerules WHERE delay1 IS NOT NULL");
977 $rqoverduebranches->execute;
978 my @branches = map { shift @$_ } @{ $rqoverduebranches->fetchall_arrayref };
979 $rqoverduebranches->finish;
983 =item CheckAccountLineLevelInfo
985 ($exist) = &CheckAccountLineLevelInfo($borrowernumber,$itemnumber,$accounttype,notify_level);
987 Check and Returns the list of all overdue books.
989 C<$exist> contains number of line in accounlines
990 with the same .biblionumber,itemnumber,accounttype,and notify_level
992 C<$borrowernumber> contains the borrower number
994 C<$itemnumber> contains item number
996 C<$accounttype> contains account type
998 C<$notify_level> contains the accountline level
1003 sub CheckAccountLineLevelInfo {
1004 my($borrowernumber,$itemnumber,$level) = @_;
1005 my $dbh = C4::Context->dbh;
1006 my $query= qq|SELECT count(*)
1008 WHERE borrowernumber =?
1010 AND notify_level=?|;
1011 my $sth=$dbh->prepare($query);
1012 $sth->execute($borrowernumber,$itemnumber,$level);
1013 my ($exist)=$sth->fetchrow;
1018 =item GetOverduerules
1020 ($overduerules) = &GetOverduerules($categorycode);
1022 Returns the value of borrowers (debarred or not) with notify level
1024 C<$overduerules> return value of debbraed field in overduerules table
1026 C<$category> contains the borrower categorycode
1028 C<$notify_level> contains the notify level
1032 sub GetOverduerules{
1033 my($category,$notify_level) = @_;
1034 my $dbh = C4::Context->dbh;
1035 my $query=qq|SELECT debarred$notify_level
1037 WHERE categorycode=?|;
1038 my $sth=$dbh->prepare($query);
1039 $sth->execute($category);
1040 my ($overduerules)=$sth->fetchrow;
1042 return($overduerules);
1046 =item CheckBorrowerDebarred
1048 ($debarredstatus) = &CheckBorrowerDebarred($borrowernumber);
1050 Check if the borrowers is already debarred
1052 C<$debarredstatus> return 0 for not debarred and return 1 for debarred
1054 C<$borrowernumber> contains the borrower number
1059 sub CheckBorrowerDebarred{
1060 my($borrowernumber) = @_;
1061 my $dbh = C4::Context->dbh;
1062 my $query=qq|SELECT debarred
1064 WHERE borrowernumber=?
1066 my $sth=$dbh->prepare($query);
1067 $sth->execute($borrowernumber);
1068 my ($debarredstatus)=$sth->fetchrow;
1070 if ($debarredstatus eq '1'){
1077 =item UpdateBorrowerDebarred
1079 ($borrowerstatut) = &UpdateBorrowerDebarred($borrowernumber);
1081 update status of borrowers in borrowers table (field debarred)
1083 C<$borrowernumber> borrower number
1087 sub UpdateBorrowerDebarred{
1088 my($borrowernumber) = @_;
1089 my $dbh = C4::Context->dbh;
1090 my $query=qq|UPDATE borrowers
1092 WHERE borrowernumber=?
1094 my $sth=$dbh->prepare($query);
1095 $sth->execute($borrowernumber);
1100 =item CheckExistantNotifyid
1102 ($exist) = &CheckExistantNotifyid($borrowernumber,$itemnumber,$accounttype,$notify_id);
1104 Check and Returns the notify id if exist else return 0.
1106 C<$exist> contains a notify_id
1108 C<$borrowernumber> contains the borrower number
1110 C<$date_due> contains the date of item return
1115 sub CheckExistantNotifyid {
1116 my($borrowernumber,$date_due) = @_;
1117 my $dbh = C4::Context->dbh;
1118 my $query = qq|SELECT notify_id FROM accountlines
1119 LEFT JOIN issues ON issues.itemnumber= accountlines.itemnumber
1120 WHERE accountlines.borrowernumber =?
1122 my $sth=$dbh->prepare($query);
1123 $sth->execute($borrowernumber,$date_due);
1124 my ($exist)=$sth->fetchrow;
1135 =item CheckAccountLineItemInfo
1137 ($exist) = &CheckAccountLineItemInfo($borrowernumber,$itemnumber,$accounttype,$notify_id);
1139 Check and Returns the list of all overdue items from the same file number(notify_id).
1141 C<$exist> contains number of line in accounlines
1142 with the same .biblionumber,itemnumber,accounttype,notify_id
1144 C<$borrowernumber> contains the borrower number
1146 C<$itemnumber> contains item number
1148 C<$accounttype> contains account type
1150 C<$notify_id> contains the file number
1154 sub CheckAccountLineItemInfo {
1155 my($borrowernumber,$itemnumber,$accounttype,$notify_id) = @_;
1156 my $dbh = C4::Context->dbh;
1157 my $query = qq|SELECT count(*) FROM accountlines
1158 WHERE borrowernumber =?
1162 my $sth=$dbh->prepare($query);
1163 $sth->execute($borrowernumber,$itemnumber,$accounttype,$notify_id);
1164 my ($exist)=$sth->fetchrow;
1169 =head2 CheckItemNotify
1171 Sql request to check if the document has alreday been notified
1172 this function is not exported, only used with GetOverduesForBranch
1176 sub CheckItemNotify {
1177 my ($notify_id,$notify_level,$itemnumber) = @_;
1178 my $dbh = C4::Context->dbh;
1179 my $sth = $dbh->prepare("
1180 SELECT COUNT(*) FROM notifys
1182 AND notify_level = ?
1183 AND itemnumber = ? ");
1184 $sth->execute($notify_id,$notify_level,$itemnumber);
1185 my $notified = $sth->fetchrow;
1190 =head2 GetOverduesForBranch
1192 Sql request for display all information for branchoverdues.pl
1193 2 possibilities : with or without location .
1194 display is filtered by branch
1198 sub GetOverduesForBranch {
1199 my ( $branch, $location) = @_;
1200 my $itype_link = (C4::Context->preference('item-level_itypes')) ? " items.itype " : " biblioitems.itemtype ";
1201 if ( not $location ) {
1202 my $dbh = C4::Context->dbh;
1203 my $sth = $dbh->prepare("
1206 borrowers.firstname,
1208 itemtypes.description,
1211 branches.branchname,
1215 items.itemcallnumber,
1216 borrowers.borrowernumber,
1218 biblio.biblionumber,
1220 accountlines.notify_id,
1221 accountlines.notify_level,
1223 accountlines.amountoutstanding
1225 LEFT JOIN issues ON issues.itemnumber = accountlines.itemnumber AND issues.borrowernumber = accountlines.borrowernumber
1226 LEFT JOIN borrowers ON borrowers.borrowernumber = accountlines.borrowernumber
1227 LEFT JOIN items ON items.itemnumber = issues.itemnumber
1228 LEFT JOIN biblio ON biblio.biblionumber = items.biblionumber
1229 LEFT JOIN biblioitems ON biblioitems.biblioitemnumber=items.biblioitemnumber
1230 LEFT JOIN itemtypes ON itemtypes.itemtype = $itype_link
1231 LEFT JOIN branches ON branches.branchcode = issues.branchcode
1232 WHERE ( accountlines.amountoutstanding != '0.000000')
1233 AND ( accountlines.accounttype = 'FU')
1234 AND (issues.branchcode = ?)
1235 AND (issues.date_due <= NOW())
1236 ORDER BY borrowers.surname
1238 $sth->execute($branch);
1241 while ( my $data = $sth->fetchrow_hashref ) {
1242 #check if the document has already been notified
1243 my $countnotify = CheckItemNotify($data->{'notify_id'},$data->{'notify_level'},$data->{'itemnumber'});
1244 if ($countnotify eq '0'){
1245 $getoverdues[$i] = $data;
1249 return (@getoverdues);
1253 my $dbh = C4::Context->dbh;
1254 my $sth = $dbh->prepare( "
1255 SELECT borrowers.surname,
1256 borrowers.firstname,
1258 itemtypes.description,
1261 branches.branchname,
1265 items.itemcallnumber,
1266 borrowers.borrowernumber,
1268 biblio.biblionumber,
1270 accountlines.notify_id,
1271 accountlines.notify_level,
1273 accountlines.amountoutstanding
1275 LEFT JOIN issues ON issues.itemnumber = accountlines.itemnumber AND issues.borrowernumber = accountlines.borrowernumber
1276 LEFT JOIN borrowers ON borrowers.borrowernumber = accountlines.borrowernumber
1277 LEFT JOIN items ON items.itemnumber = issues.itemnumber
1278 LEFT JOIN biblio ON biblio.biblionumber = items.biblionumber
1279 LEFT JOIN biblioitems ON biblioitems.biblioitemnumber=items.biblioitemnumber
1280 LEFT JOIN itemtypes ON itemtypes.itemtype = $itype_link
1281 LEFT JOIN branches ON branches.branchcode = issues.branchcode
1282 WHERE ( accountlines.amountoutstanding != '0.000000')
1283 AND ( accountlines.accounttype = 'FU')
1284 AND (issues.branchcode = ? AND items.location = ?)
1285 AND (issues.date_due <= NOW())
1286 ORDER BY borrowers.surname
1288 $sth->execute( $branch, $location);
1291 while ( my $data = $sth->fetchrow_hashref ) {
1292 #check if the document has already been notified
1293 my $countnotify = CheckItemNotify($data->{'notify_id'},$data->{'notify_level'},$data->{'itemnumber'});
1294 if ($countnotify eq '0'){
1295 $getoverdues[$i] = $data;
1300 return (@getoverdues);
1305 =head2 AddNotifyLine
1307 &AddNotifyLine($borrowernumber, $itemnumber, $overduelevel, $method, $notifyId)
1309 Creat a line into notify, if the method is phone, the notification_send_date is implemented to
1314 my ( $borrowernumber, $itemnumber, $overduelevel, $method, $notifyId ) = @_;
1315 if ( $method eq "phone" ) {
1316 my $dbh = C4::Context->dbh;
1317 my $sth = $dbh->prepare(
1318 "INSERT INTO notifys (borrowernumber,itemnumber,notify_date,notify_send_date,notify_level,method,notify_id)
1319 VALUES (?,?,now(),now(),?,?,?)"
1321 $sth->execute( $borrowernumber, $itemnumber, $overduelevel, $method,
1326 my $dbh = C4::Context->dbh;
1327 my $sth = $dbh->prepare(
1328 "INSERT INTO notifys (borrowernumber,itemnumber,notify_date,notify_level,method,notify_id)
1329 VALUES (?,?,now(),?,?,?)"
1331 $sth->execute( $borrowernumber, $itemnumber, $overduelevel, $method,
1338 =head2 RemoveNotifyLine
1340 &RemoveNotifyLine( $borrowernumber, $itemnumber, $notify_date );
1342 Cancel a notification
1346 sub RemoveNotifyLine {
1347 my ( $borrowernumber, $itemnumber, $notify_date ) = @_;
1348 my $dbh = C4::Context->dbh;
1349 my $sth = $dbh->prepare(
1350 "DELETE FROM notifys
1356 $sth->execute( $borrowernumber, $itemnumber, $notify_date );
1368 Koha Developement team <info@koha.org>