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.
106 $overdues = Getoverdues( { minimumdays => 1, maximumdays => 30 } );
108 Returns the list of all overdue books, with their itemtype.
110 C<$overdues> is a reference-to-array. Each element is a
111 reference-to-hash whose keys are the fields of the issues table in the
120 my $dbh = C4::Context->dbh;
122 if ( C4::Context->preference('item-level_itypes') ) {
124 SELECT issues.*,items.itype as itemtype, items.homebranch FROM issues
125 LEFT JOIN items USING (itemnumber)
126 WHERE date_due < now()
130 SELECT issues.*,biblioitems.itemtype,items.itype, items.homebranch FROM issues
131 LEFT JOIN items USING (itemnumber)
132 LEFT JOIN biblioitems USING (biblioitemnumber)
133 WHERE date_due < now()
138 if ( exists $params->{'minimumdays'} and exists $params->{'maximumdays'} ) {
139 $statement .= ' AND TO_DAYS( NOW() )-TO_DAYS( date_due ) BETWEEN ? and ? ';
140 push @bind_parameters, $params->{'minimumdays'}, $params->{'maximumdays'};
141 } elsif ( exists $params->{'minimumdays'} ) {
142 $statement .= ' AND ( TO_DAYS( NOW() )-TO_DAYS( date_due ) ) > ? ';
143 push @bind_parameters, $params->{'minimumdays'};
144 } elsif ( exists $params->{'maximumdays'} ) {
145 $statement .= ' AND ( TO_DAYS( NOW() )-TO_DAYS( date_due ) ) < ? ';
146 push @bind_parameters, $params->{'maximumdays'};
148 $statement .= 'ORDER BY borrowernumber';
149 my $sth = $dbh->prepare( $statement );
150 $sth->execute( @bind_parameters );
151 return $sth->fetchall_arrayref({});
157 ( $count, $overdueitems )=checkoverdues( $borrowernumber, $dbh );
165 # From Main.pm, modified to return a list of overdueitems, in addition to a count
166 #checks whether a borrower has overdue items
167 my ( $borrowernumber, $dbh ) = @_;
168 my @datearr = localtime;
170 ( $datearr[5] + 1900 ) . "-" . ( $datearr[4] + 1 ) . "-" . $datearr[3];
173 my $sth = $dbh->prepare(
174 "SELECT * FROM issues
175 LEFT JOIN items ON issues.itemnumber = items.itemnumber
176 LEFT JOIN biblio ON items.biblionumber=biblio.biblionumber
177 LEFT JOIN biblioitems ON items.biblioitemnumber = biblioitems.biblioitemnumber
178 WHERE issues.borrowernumber = ?
179 AND issues.date_due < ?"
181 $sth->execute( $borrowernumber, $today );
182 while ( my $data = $sth->fetchrow_hashref ) {
183 push( @overdueitems, $data );
187 return ( $count, \@overdueitems );
192 ($amount, $chargename, $daycount, $daycounttotal) =
193 &CalcFine($item, $categorycode, $branch, $days_overdue, $description, $start_date, $end_date );
195 Calculates the fine for a book.
197 The issuingrules table in the Koha database is a fine matrix, listing
198 the penalties for each type of patron for each type of item and each branch (e.g., the
199 standard fine for books might be $0.50, but $1.50 for DVDs, or staff
200 members might get a longer grace period between the first and second
201 reminders that a book is overdue).
204 C<$item> is an item object (hashref).
206 C<$categorycode> is the category code (string) of the patron who currently has
209 C<$branchcode> is the library (string) whose issuingrules govern this transaction.
211 C<$days_overdue> is the number of days elapsed since the book's due date.
212 NOTE: supplying days_overdue is deprecated.
214 C<$start_date> & C<$end_date> are C4::Dates objects
215 defining the date range over which to determine the fine.
216 Note that if these are defined, we ignore C<$difference> and C<$dues> ,
217 but retain these for backwards-comptibility with extant fines scripts.
219 Fines scripts should just supply the date range over which to calculate the fine.
221 C<&CalcFine> returns four values:
223 C<$amount> is the fine owed by the patron (see above).
225 C<$chargename> is the chargename field from the applicable record in
226 the categoryitem table, whatever that is.
228 C<$daycount> is the number of days between start and end dates, Calendar adjusted (where needed),
229 minus any applicable grace period.
231 C<$daycounttotal> is C<$daycount> without consideration of grace period.
233 FIXME - What is chargename supposed to be ?
235 FIXME: previously attempted to return C<$message> as a text message, either "First Notice", "Second Notice",
236 or "Final Notice". But CalcFine never defined any value.
242 my ( $item, $bortype, $branchcode, $difference ,$dues , $start_date, $end_date ) = @_;
243 $debug and warn sprintf("CalcFine(%s, %s, %s, %s, %s, %s, %s)",
244 ($item ? '{item}' : 'UNDEF'),
245 ($bortype || 'UNDEF'),
246 ($branchcode || 'UNDEF'),
247 ($difference || 'UNDEF'),
249 ($start_date ? ($start_date->output('iso') || 'Not a C4::Dates object') : 'UNDEF'),
250 ( $end_date ? ( $end_date->output('iso') || 'Not a C4::Dates object') : 'UNDEF')
252 my $dbh = C4::Context->dbh;
255 # get issuingrules (fines part will be used)
256 my $data = C4::Circulation::GetIssuingRule($bortype, $item->{'itemtype'},$branchcode);
258 # if $difference is supplied, the difference has already been calculated, but we still need to adjust for the calendar.
259 # use copy-pasted functions from calendar module. (deprecated -- these functions will be removed from C4::Overdues ).
260 my $countspecialday = &GetSpecialHolidays($dues,$item->{itemnumber});
261 my $countrepeatableday = &GetRepeatableHolidays($dues,$item->{itemnumber},$difference);
262 my $countalldayclosed = $countspecialday + $countrepeatableday;
263 $daystocharge = $difference - $countalldayclosed;
265 # if $difference is not supplied, we have C4::Dates objects giving us the date range, and we use the calendar module.
266 if(C4::Context->preference('finesCalendar') eq 'noFinesWhenClosed') {
267 my $calendar = C4::Calendar->new( branchcode => $branchcode );
268 $daystocharge = $calendar->daysBetween( $start_date, $end_date );
270 $daystocharge = Date_to_Days(split('-',$end_date->output('iso'))) - Date_to_Days(split('-',$start_date->output('iso')));
273 # correct for grace period.
274 my $days_minus_grace = $daystocharge - $data->{'firstremind'};
275 if ($data->{'chargeperiod'} > 0 && $days_minus_grace > 0 ) {
276 $amount = int($days_minus_grace / $data->{'chargeperiod'}) * $data->{'fine'};
278 # a zero (or null) chargeperiod means no charge.
280 $amount = C4::Context->preference('maxFine') if(C4::Context->preference('maxFine') && ( $amount > C4::Context->preference('maxFine')));
281 return ( $amount, $data->{'chargename'}, $days_minus_grace, $daystocharge);
285 =head2 GetSpecialHolidays
287 &GetSpecialHolidays($date_dues,$itemnumber);
289 return number of special days between date of the day and date due
291 C<$date_dues> is the envisaged date of book return.
293 C<$itemnumber> is the book's item number.
297 sub GetSpecialHolidays {
298 my ($date_dues,$itemnumber) = @_;
299 # calcul the today date
300 my $today = join "-", &Today();
302 # return the holdingbranch
303 my $iteminfo=GetIssuesIteminfo($itemnumber);
304 # use sql request to find all date between date_due and today
305 my $dbh = C4::Context->dbh;
306 my $query=qq|SELECT DATE_FORMAT(concat(year,'-',month,'-',day),'%Y-%m-%d')as date
307 FROM `special_holidays`
308 WHERE DATE_FORMAT(concat(year,'-',month,'-',day),'%Y-%m-%d') >= ?
309 AND DATE_FORMAT(concat(year,'-',month,'-',day),'%Y-%m-%d') <= ?
312 my @result=GetWdayFromItemnumber($itemnumber);
316 my $sth = $dbh->prepare($query);
317 $sth->execute($date_dues,$today,$iteminfo->{'branchcode'});
319 while ( my $special_date=$sth->fetchrow_hashref){
320 push (@result_date,$special_date);
323 my $specialdaycount=scalar(@result_date);
325 for (my $i=0;$i<scalar(@result_date);$i++){
326 $dateinsec=UnixDate($result_date[$i]->{'date'},"%o");
327 (undef,undef,undef,undef,undef,undef,$wday,undef,undef) =localtime($dateinsec);
328 for (my $j=0;$j<scalar(@result);$j++){
329 if ($wday == ($result[$j]->{'weekday'})){
335 return $specialdaycount;
338 =head2 GetRepeatableHolidays
340 &GetRepeatableHolidays($date_dues, $itemnumber, $difference,);
342 return number of day closed between date of the day and date due
344 C<$date_dues> is the envisaged date of book return.
346 C<$itemnumber> is item number.
348 C<$difference> numbers of between day date of the day and date due
352 sub GetRepeatableHolidays{
353 my ($date_dues,$itemnumber,$difference) = @_;
354 my $dateinsec=UnixDate($date_dues,"%o");
355 my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) =localtime($dateinsec);
356 my @result=GetWdayFromItemnumber($itemnumber);
360 for (my $i=0;$i<scalar(@result);$i++){
363 for ( $j=0;$j<$difference;$j++){
364 if ($result[$i]->{'weekday'} == $k)
366 push ( @dayclosedcount ,$k);
372 return scalar(@dayclosedcount);
376 =head2 GetWayFromItemnumber
378 &Getwdayfromitemnumber($itemnumber);
380 return the different week day from repeatable_holidays table
382 C<$itemnumber> is item number.
386 sub GetWdayFromItemnumber{
388 my $iteminfo=GetIssuesIteminfo($itemnumber);
390 my $dbh = C4::Context->dbh;
391 my $query = qq|SELECT weekday
392 FROM repeatable_holidays
395 my $sth = $dbh->prepare($query);
398 $sth->execute($iteminfo->{'branchcode'});
399 while ( my $weekday=$sth->fetchrow_hashref){
400 push (@result,$weekday);
406 =head2 GetIssuesIteminfo
408 &GetIssuesIteminfo($itemnumber);
410 return all data from issues about item
412 C<$itemnumber> is item number.
416 sub GetIssuesIteminfo{
418 my $dbh = C4::Context->dbh;
419 my $query = qq|SELECT *
423 my $sth = $dbh->prepare($query);
424 $sth->execute($itemnumber);
425 my ($issuesinfo)=$sth->fetchrow_hashref;
432 &UpdateFine($itemnumber, $borrowernumber, $amount, $type, $description);
434 (Note: the following is mostly conjecture and guesswork.)
436 Updates the fine owed on an overdue book.
438 C<$itemnumber> is the book's item number.
440 C<$borrowernumber> is the borrower number of the patron who currently
441 has the book on loan.
443 C<$amount> is the current amount owed by the patron.
445 C<$type> will be used in the description of the fine.
447 C<$description> is a string that must be present in the description of
448 the fine. I think this is expected to be a date in DD/MM/YYYY format.
450 C<&UpdateFine> looks up the amount currently owed on the given item
451 and sets it to C<$amount>, creating, if necessary, a new entry in the
452 accountlines table of the Koha database.
457 # Question: Why should the caller have to
458 # specify both the item number and the borrower number? A book can't
459 # be on loan to two different people, so the item number should be
462 # Possible Answer: You might update a fine for a damaged item, *after* it is returned.
465 my ( $itemnum, $borrowernumber, $amount, $type, $due ) = @_;
466 $debug and warn "UpdateFine($itemnum, $borrowernumber, $amount, " . ($type||'""') . ", $due) called";
467 my $dbh = C4::Context->dbh;
468 # FIXME - What exactly is this query supposed to do? It looks up an
469 # entry in accountlines that matches the given item and borrower
470 # numbers, where the description contains $due, and where the
471 # account type has one of several values, but what does this _mean_?
472 # Does it look up existing fines for this item?
473 # FIXME - What are these various account types? ("FU", "O", "F", "M")
475 # "A" is Account Management Fee
480 # "FU" is Fine UPDATE??
482 # "REF" is Cash Refund
483 my $sth = $dbh->prepare(
484 "SELECT * FROM accountlines
487 AND accounttype IN ('FU','O','F','M')
488 AND description like ? "
490 $sth->execute( $itemnum, $borrowernumber, "%$due%" );
492 if ( my $data = $sth->fetchrow_hashref ) {
494 # we're updating an existing fine. Only modify if we're adding to the charge.
495 # Note that in the current implementation, you cannot pay against an accruing fine
496 # (i.e. , of accounttype 'FU'). Doing so will break accrual.
497 if ( $data->{'amount'} != $amount ) {
498 my $diff = $amount - $data->{'amount'};
499 $diff = 0 if ( $data->{amount} > $amount);
500 my $out = $data->{'amountoutstanding'} + $diff;
503 SET date=now(), amount=?, amountoutstanding=?,
504 lastincrement=?, accounttype='FU'
505 WHERE borrowernumber=?
507 AND accounttype IN ('FU','O')
508 AND description LIKE ?
510 my $sth2 = $dbh->prepare($query);
511 # FIXME: BOGUS query cannot ensure uniqueness w/ LIKE %x% !!!
512 # LIMIT 1 added to prevent multiple affected lines
513 # FIXME: accountlines table needs unique key!! Possibly a combo of borrowernumber and accountline.
514 # But actually, we should just have a regular autoincrementing PK and forget accountline,
515 # including the bogus getnextaccountno function (doesn't prevent conflict on simultaneous ops).
516 # FIXME: Why only 2 account types here?
517 $debug and print STDERR "UpdateFine query: $query\n" .
518 "w/ args: $amount, $out, $diff, $data->{'borrowernumber'}, $data->{'itemnumber'}, \"\%$due\%\"\n";
519 $sth2->execute($amount, $out, $diff, $data->{'borrowernumber'}, $data->{'itemnumber'}, "%$due%");
521 # print "no update needed $data->{'amount'}"
524 my $sth4 = $dbh->prepare(
525 "SELECT title FROM biblio LEFT JOIN items ON biblio.biblionumber=items.biblionumber WHERE items.itemnumber=?"
527 $sth4->execute($itemnum);
528 my $title = $sth4->fetchrow;
530 # # print "not in account";
531 # my $sth3 = $dbh->prepare("Select max(accountno) from accountlines");
534 # # FIXME - Make $accountno a scalar.
535 # my @accountno = $sth3->fetchrow_array;
539 my $nextaccntno = C4::Accounts::getnextacctno($borrowernumber);
540 my $desc = ($type ? "$type " : '') . "$title $due"; # FIXEDME, avoid whitespace prefix on empty $type
541 my $query = "INSERT INTO accountlines
542 (borrowernumber,itemnumber,date,amount,description,accounttype,amountoutstanding,lastincrement,accountno)
543 VALUES (?,?,now(),?,?,'FU',?,?,?)";
544 my $sth2 = $dbh->prepare($query);
545 $debug and print STDERR "UpdateFine query: $query\nw/ args: $borrowernumber, $itemnum, $amount, $desc, $amount, $amount, $nextaccntno\n";
546 $sth2->execute($borrowernumber, $itemnum, $amount, $desc, $amount, $amount, $nextaccntno);
553 "due=".$due." amount=".$amount." itemnumber=".$itemnum
554 ) if C4::Context->preference("FinesLog");
559 $borrower = &BorType($borrowernumber);
561 Looks up a patron by borrower number.
563 C<$borrower> is a reference-to-hash whose keys are all of the fields
564 from the borrowers and categories tables of the Koha database. Thus,
565 C<$borrower> contains all information about both the borrower and
566 category he or she belongs to.
572 my ($borrowernumber) = @_;
573 my $dbh = C4::Context->dbh;
574 my $sth = $dbh->prepare(
575 "SELECT * from borrowers
576 LEFT JOIN categories ON borrowers.categorycode=categories.categorycode
577 WHERE borrowernumber=?"
579 $sth->execute($borrowernumber);
580 my $data = $sth->fetchrow_hashref;
585 =head2 ReplacementCost
587 $cost = &ReplacementCost($itemnumber);
589 Returns the replacement cost of the item with the given item number.
594 sub ReplacementCost {
596 my $dbh = C4::Context->dbh;
598 $dbh->prepare("Select replacementprice from items where itemnumber=?");
599 $sth->execute($itemnum);
601 # FIXME - Use fetchrow_array or something.
602 my $data = $sth->fetchrow_hashref;
604 return ( $data->{'replacementprice'} );
609 $data->{'sum(amountoutstanding)'} = &GetFine($itemnum,$borrowernumber);
611 return the total of fine
613 C<$itemnum> is item number
615 C<$borrowernumber> is the borrowernumber
621 my ( $itemnum, $borrowernumber ) = @_;
622 my $dbh = C4::Context->dbh();
623 my $query = "SELECT sum(amountoutstanding) FROM accountlines
624 where accounttype like 'F%'
625 AND amountoutstanding > 0 AND itemnumber = ? AND borrowernumber=?";
626 my $sth = $dbh->prepare($query);
627 $sth->execute( $itemnum, $borrowernumber );
628 my $data = $sth->fetchrow_hashref();
629 return ( $data->{'sum(amountoutstanding)'} );
633 =head2 GetIssuingRules
635 FIXME - This sub should be deprecated and removed.
636 It ignores branch and defaults.
638 $data = &GetIssuingRules($itemtype,$categorycode);
640 Looks up for all issuingrules an item info
642 C<$itemnumber> is a reference-to-hash whose keys are all of the fields
643 from the borrowers and categories tables of the Koha database. Thus,
645 C<$categorycode> contains information about borrowers category
647 C<$data> contains all information about both the borrower and
648 category he or she belongs to.
651 sub GetIssuingRules {
652 warn "GetIssuingRules is deprecated: use GetIssuingRule from C4::Circulation instead.";
653 my ($itemtype,$categorycode)=@_;
654 my $dbh = C4::Context->dbh();
655 my $query=qq|SELECT *
657 WHERE issuingrules.itemtype=?
658 AND issuingrules.categorycode=?
660 my $sth = $dbh->prepare($query);
662 $sth->execute($itemtype,$categorycode);
663 return $sth->fetchrow_hashref;
667 sub ReplacementCost2 {
668 my ( $itemnum, $borrowernumber ) = @_;
669 my $dbh = C4::Context->dbh();
670 my $query = "SELECT amountoutstanding
672 WHERE accounttype like 'L'
673 AND amountoutstanding > 0
675 AND borrowernumber= ?";
676 my $sth = $dbh->prepare($query);
677 $sth->execute( $itemnum, $borrowernumber );
678 my $data = $sth->fetchrow_hashref();
679 return ( $data->{'amountoutstanding'} );
683 =head2 GetNextIdNotify
685 ($result) = &GetNextIdNotify($reference);
687 Returns the new file number
689 C<$result> contains the next file number
691 C<$reference> contains the beggining of file number
697 sub GetNextIdNotify {
699 my $query=qq|SELECT max(notify_id)
701 WHERE notify_id like \"$reference%\"
703 # AND borrowernumber=?|;
704 my $dbh = C4::Context->dbh;
705 my $sth=$dbh->prepare($query);
707 my $result=$sth->fetchrow;
712 ($result=$reference."01") ;
715 $count=substr($result,6)+1;
718 ($count = "0".$count);
720 $result=$reference.$count;
726 =head2 NumberNotifyId
728 (@notify) = &NumberNotifyId($borrowernumber);
730 Returns amount for all file per borrowers
731 C<@notify> array contains all file per borrowers
733 C<$notify_id> contains the file number for the borrower number nad item number
738 my ($borrowernumber)=@_;
739 my $dbh = C4::Context->dbh;
740 my $query=qq| SELECT distinct(notify_id)
742 WHERE borrowernumber=?|;
744 my $sth=$dbh->prepare($query);
745 $sth->execute($borrowernumber);
746 while ( my ($numberofnotify)=$sth->fetchrow){
747 push (@notify,$numberofnotify);
757 ($totalnotify) = &AmountNotify($notifyid);
759 Returns amount for all file per borrowers
760 C<$notifyid> is the file number
762 C<$totalnotify> contains amount of a file
764 C<$notify_id> contains the file number for the borrower number and item number
769 my ($notifyid,$borrowernumber)=@_;
770 my $dbh = C4::Context->dbh;
771 my $query=qq| SELECT sum(amountoutstanding)
773 WHERE notify_id=? AND borrowernumber = ?|;
774 my $sth=$dbh->prepare($query);
775 $sth->execute($notifyid,$borrowernumber);
776 my $totalnotify=$sth->fetchrow;
778 return ($totalnotify);
784 ($notify_id) = &GetNotifyId($borrowernumber,$itemnumber);
786 Returns the file number per borrower and itemnumber
788 C<$borrowernumber> 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 borrower categorycode
793 C<$notify_id> contains the file number for the borrower number nad item number
798 my ($borrowernumber,$itemnumber)=@_;
799 my $query=qq|SELECT notify_id
801 WHERE borrowernumber=?
803 AND (accounttype='FU' or accounttype='O')|;
804 my $dbh = C4::Context->dbh;
805 my $sth=$dbh->prepare($query);
806 $sth->execute($borrowernumber,$itemnumber);
807 my ($notify_id)=$sth->fetchrow;
813 =head2 CreateItemAccountLine
815 () = &CreateItemAccountLine($borrowernumber,$itemnumber,$date,$amount,$description,$accounttype,$amountoutstanding,$timestamp,$notify_id,$level);
817 update the account lines with file number or with file level
819 C<$items> is a reference-to-hash whose keys are all of the fields
820 from the items tables of the Koha database. Thus,
822 C<$itemnumber> contains the item number
824 C<$borrowernumber> contains the borrower number
826 C<$date> contains the date of the day
828 C<$amount> contains item price
830 C<$description> contains the descritpion of accounttype
832 C<$accounttype> contains the account type
834 C<$amountoutstanding> contains the $amountoutstanding
836 C<$timestamp> contains the timestamp with time and the date of the day
838 C<$notify_id> contains the file number
840 C<$level> contains the file level
845 sub CreateItemAccountLine {
846 my ($borrowernumber,$itemnumber,$date,$amount,$description,$accounttype,$amountoutstanding,$timestamp,$notify_id,$level)=@_;
847 my $dbh = C4::Context->dbh;
848 my $nextaccntno = C4::Accounts::getnextacctno($borrowernumber);
849 my $query= "INSERT into accountlines
850 (borrowernumber,accountno,itemnumber,date,amount,description,accounttype,amountoutstanding,timestamp,notify_id,notify_level)
852 (?,?,?,?,?,?,?,?,?,?,?)";
855 my $sth=$dbh->prepare($query);
856 $sth->execute($borrowernumber,$nextaccntno,$itemnumber,$date,$amount,$description,$accounttype,$amountoutstanding,$timestamp,$notify_id,$level);
860 =head2 UpdateAccountLines
862 () = &UpdateAccountLines($notify_id,$notify_level,$borrowernumber,$itemnumber);
864 update the account lines with file number or with file level
866 C<$items> is a reference-to-hash whose keys are all of the fields
867 from the items tables of the Koha database. Thus,
869 C<$itemnumber> contains the item number
871 C<$notify_id> contains the file number
873 C<$notify_level> contains the file level
875 C<$borrowernumber> contains the borrowernumber
879 sub UpdateAccountLines {
880 my ($notify_id,$notify_level,$borrowernumber,$itemnumber)=@_;
882 if ($notify_id eq '')
885 $query=qq|UPDATE accountlines
887 WHERE borrowernumber=? AND itemnumber=?
888 AND (accounttype='FU' or accounttype='O')|;
891 $query=qq|UPDATE accountlines
892 SET notify_id=?, notify_level=?
893 WHERE borrowernumber=?
895 AND (accounttype='FU' or accounttype='O')|;
897 my $dbh = C4::Context->dbh;
898 my $sth=$dbh->prepare($query);
900 if ($notify_id eq '')
902 $sth->execute($notify_level,$borrowernumber,$itemnumber);
905 $sth->execute($notify_id,$notify_level,$borrowernumber,$itemnumber);
914 ($items) = &GetItems($itemnumber);
916 Returns the list of all delays from overduerules.
918 C<$items> is a reference-to-hash whose keys are all of the fields
919 from the items tables of the Koha database. Thus,
921 C<$itemnumber> contains the borrower categorycode
926 my($itemnumber) = @_;
927 my $query=qq|SELECT *
930 my $dbh = C4::Context->dbh;
931 my $sth=$dbh->prepare($query);
932 $sth->execute($itemnumber);
933 my ($items)=$sth->fetchrow_hashref;
938 =head2 GetOverdueDelays
940 (@delays) = &GetOverdueDelays($categorycode);
942 Returns the list of all delays from overduerules.
944 C<@delays> it's an array contains the three delays from overduerules table
946 C<$categorycode> contains the borrower categorycode
950 sub GetOverdueDelays {
952 my $dbh = C4::Context->dbh;
953 my $query=qq|SELECT delay1,delay2,delay3
955 WHERE categorycode=?|;
956 my $sth=$dbh->prepare($query);
957 $sth->execute($category);
958 my (@delays)=$sth->fetchrow_array;
963 =head2 GetBranchcodesWithOverdueRules
967 my @branchcodes = C4::Overdues::GetBranchcodesWithOverdueRules()
969 returns a list of branch codes for branches with overdue rules defined.
975 sub GetBranchcodesWithOverdueRules {
976 my $dbh = C4::Context->dbh;
977 my $rqoverduebranches = $dbh->prepare("SELECT DISTINCT branchcode FROM overduerules WHERE delay1 IS NOT NULL AND branchcode <> ''");
978 $rqoverduebranches->execute;
979 my @branches = map { shift @$_ } @{ $rqoverduebranches->fetchall_arrayref };
980 $rqoverduebranches->finish;
984 =head2 CheckAccountLineLevelInfo
986 ($exist) = &CheckAccountLineLevelInfo($borrowernumber,$itemnumber,$accounttype,notify_level);
988 Check and Returns the list of all overdue books.
990 C<$exist> contains number of line in accounlines
991 with the same .biblionumber,itemnumber,accounttype,and notify_level
993 C<$borrowernumber> contains the borrower number
995 C<$itemnumber> contains item number
997 C<$accounttype> contains account type
999 C<$notify_level> contains the accountline level
1004 sub CheckAccountLineLevelInfo {
1005 my($borrowernumber,$itemnumber,$level) = @_;
1006 my $dbh = C4::Context->dbh;
1007 my $query= qq|SELECT count(*)
1009 WHERE borrowernumber =?
1011 AND notify_level=?|;
1012 my $sth=$dbh->prepare($query);
1013 $sth->execute($borrowernumber,$itemnumber,$level);
1014 my ($exist)=$sth->fetchrow;
1019 =head2 GetOverduerules
1021 ($overduerules) = &GetOverduerules($categorycode);
1023 Returns the value of borrowers (debarred or not) with notify level
1025 C<$overduerules> return value of debbraed field in overduerules table
1027 C<$category> contains the borrower categorycode
1029 C<$notify_level> contains the notify level
1033 sub GetOverduerules{
1034 my($category,$notify_level) = @_;
1035 my $dbh = C4::Context->dbh;
1036 my $query=qq|SELECT debarred$notify_level
1038 WHERE categorycode=?|;
1039 my $sth=$dbh->prepare($query);
1040 $sth->execute($category);
1041 my ($overduerules)=$sth->fetchrow;
1043 return($overduerules);
1047 =head2 CheckBorrowerDebarred
1049 ($debarredstatus) = &CheckBorrowerDebarred($borrowernumber);
1051 Check if the borrowers is already debarred
1053 C<$debarredstatus> return 0 for not debarred and return 1 for debarred
1055 C<$borrowernumber> contains the borrower number
1060 sub CheckBorrowerDebarred{
1061 my($borrowernumber) = @_;
1062 my $dbh = C4::Context->dbh;
1063 my $query=qq|SELECT debarred
1065 WHERE borrowernumber=?
1067 my $sth=$dbh->prepare($query);
1068 $sth->execute($borrowernumber);
1069 my ($debarredstatus)=$sth->fetchrow;
1071 if ($debarredstatus eq '1'){
1078 =head2 UpdateBorrowerDebarred
1080 ($borrowerstatut) = &UpdateBorrowerDebarred($borrowernumber);
1082 update status of borrowers in borrowers table (field debarred)
1084 C<$borrowernumber> borrower number
1088 sub UpdateBorrowerDebarred{
1089 my($borrowernumber) = @_;
1090 my $dbh = C4::Context->dbh;
1091 my $query=qq|UPDATE borrowers
1093 WHERE borrowernumber=?
1095 my $sth=$dbh->prepare($query);
1096 $sth->execute($borrowernumber);
1101 =head2 CheckExistantNotifyid
1103 ($exist) = &CheckExistantNotifyid($borrowernumber,$itemnumber,$accounttype,$notify_id);
1105 Check and Returns the notify id if exist else return 0.
1107 C<$exist> contains a notify_id
1109 C<$borrowernumber> contains the borrower number
1111 C<$date_due> contains the date of item return
1116 sub CheckExistantNotifyid {
1117 my($borrowernumber,$date_due) = @_;
1118 my $dbh = C4::Context->dbh;
1119 my $query = qq|SELECT notify_id FROM accountlines
1120 LEFT JOIN issues ON issues.itemnumber= accountlines.itemnumber
1121 WHERE accountlines.borrowernumber =?
1123 my $sth=$dbh->prepare($query);
1124 $sth->execute($borrowernumber,$date_due);
1125 my ($exist)=$sth->fetchrow;
1136 =head2 CheckAccountLineItemInfo
1138 ($exist) = &CheckAccountLineItemInfo($borrowernumber,$itemnumber,$accounttype,$notify_id);
1140 Check and Returns the list of all overdue items from the same file number(notify_id).
1142 C<$exist> contains number of line in accounlines
1143 with the same .biblionumber,itemnumber,accounttype,notify_id
1145 C<$borrowernumber> contains the borrower number
1147 C<$itemnumber> contains item number
1149 C<$accounttype> contains account type
1151 C<$notify_id> contains the file number
1155 sub CheckAccountLineItemInfo {
1156 my($borrowernumber,$itemnumber,$accounttype,$notify_id) = @_;
1157 my $dbh = C4::Context->dbh;
1158 my $query = qq|SELECT count(*) FROM accountlines
1159 WHERE borrowernumber =?
1163 my $sth=$dbh->prepare($query);
1164 $sth->execute($borrowernumber,$itemnumber,$accounttype,$notify_id);
1165 my ($exist)=$sth->fetchrow;
1170 =head2 CheckItemNotify
1172 Sql request to check if the document has alreday been notified
1173 this function is not exported, only used with GetOverduesForBranch
1177 sub CheckItemNotify {
1178 my ($notify_id,$notify_level,$itemnumber) = @_;
1179 my $dbh = C4::Context->dbh;
1180 my $sth = $dbh->prepare("
1181 SELECT COUNT(*) FROM notifys
1183 AND notify_level = ?
1184 AND itemnumber = ? ");
1185 $sth->execute($notify_id,$notify_level,$itemnumber);
1186 my $notified = $sth->fetchrow;
1191 =head2 GetOverduesForBranch
1193 Sql request for display all information for branchoverdues.pl
1194 2 possibilities : with or without location .
1195 display is filtered by branch
1199 sub GetOverduesForBranch {
1200 my ( $branch, $location) = @_;
1201 my $itype_link = (C4::Context->preference('item-level_itypes')) ? " items.itype " : " biblioitems.itemtype ";
1202 if ( not $location ) {
1203 my $dbh = C4::Context->dbh;
1204 my $sth = $dbh->prepare("
1207 borrowers.firstname,
1209 itemtypes.description,
1212 branches.branchname,
1216 items.itemcallnumber,
1217 borrowers.borrowernumber,
1219 biblio.biblionumber,
1221 accountlines.notify_id,
1222 accountlines.notify_level,
1224 accountlines.amountoutstanding
1226 LEFT JOIN issues ON issues.itemnumber = accountlines.itemnumber AND issues.borrowernumber = accountlines.borrowernumber
1227 LEFT JOIN borrowers ON borrowers.borrowernumber = accountlines.borrowernumber
1228 LEFT JOIN items ON items.itemnumber = issues.itemnumber
1229 LEFT JOIN biblio ON biblio.biblionumber = items.biblionumber
1230 LEFT JOIN biblioitems ON biblioitems.biblioitemnumber=items.biblioitemnumber
1231 LEFT JOIN itemtypes ON itemtypes.itemtype = $itype_link
1232 LEFT JOIN branches ON branches.branchcode = issues.branchcode
1233 WHERE ( accountlines.amountoutstanding != '0.000000')
1234 AND ( accountlines.accounttype = 'FU')
1235 AND (issues.branchcode = ?)
1236 AND (issues.date_due <= NOW())
1237 ORDER BY borrowers.surname
1239 $sth->execute($branch);
1242 while ( my $data = $sth->fetchrow_hashref ) {
1243 #check if the document has already been notified
1244 my $countnotify = CheckItemNotify($data->{'notify_id'},$data->{'notify_level'},$data->{'itemnumber'});
1245 if ($countnotify eq '0'){
1246 $getoverdues[$i] = $data;
1250 return (@getoverdues);
1254 my $dbh = C4::Context->dbh;
1255 my $sth = $dbh->prepare( "
1256 SELECT borrowers.surname,
1257 borrowers.firstname,
1259 itemtypes.description,
1262 branches.branchname,
1266 items.itemcallnumber,
1267 borrowers.borrowernumber,
1269 biblio.biblionumber,
1271 accountlines.notify_id,
1272 accountlines.notify_level,
1274 accountlines.amountoutstanding
1276 LEFT JOIN issues ON issues.itemnumber = accountlines.itemnumber AND issues.borrowernumber = accountlines.borrowernumber
1277 LEFT JOIN borrowers ON borrowers.borrowernumber = accountlines.borrowernumber
1278 LEFT JOIN items ON items.itemnumber = issues.itemnumber
1279 LEFT JOIN biblio ON biblio.biblionumber = items.biblionumber
1280 LEFT JOIN biblioitems ON biblioitems.biblioitemnumber=items.biblioitemnumber
1281 LEFT JOIN itemtypes ON itemtypes.itemtype = $itype_link
1282 LEFT JOIN branches ON branches.branchcode = issues.branchcode
1283 WHERE ( accountlines.amountoutstanding != '0.000000')
1284 AND ( accountlines.accounttype = 'FU')
1285 AND (issues.branchcode = ? AND items.location = ?)
1286 AND (issues.date_due <= NOW())
1287 ORDER BY borrowers.surname
1289 $sth->execute( $branch, $location);
1292 while ( my $data = $sth->fetchrow_hashref ) {
1293 #check if the document has already been notified
1294 my $countnotify = CheckItemNotify($data->{'notify_id'},$data->{'notify_level'},$data->{'itemnumber'});
1295 if ($countnotify eq '0'){
1296 $getoverdues[$i] = $data;
1301 return (@getoverdues);
1306 =head2 AddNotifyLine
1308 &AddNotifyLine($borrowernumber, $itemnumber, $overduelevel, $method, $notifyId)
1310 Creat a line into notify, if the method is phone, the notification_send_date is implemented to
1315 my ( $borrowernumber, $itemnumber, $overduelevel, $method, $notifyId ) = @_;
1316 if ( $method eq "phone" ) {
1317 my $dbh = C4::Context->dbh;
1318 my $sth = $dbh->prepare(
1319 "INSERT INTO notifys (borrowernumber,itemnumber,notify_date,notify_send_date,notify_level,method,notify_id)
1320 VALUES (?,?,now(),now(),?,?,?)"
1322 $sth->execute( $borrowernumber, $itemnumber, $overduelevel, $method,
1327 my $dbh = C4::Context->dbh;
1328 my $sth = $dbh->prepare(
1329 "INSERT INTO notifys (borrowernumber,itemnumber,notify_date,notify_level,method,notify_id)
1330 VALUES (?,?,now(),?,?,?)"
1332 $sth->execute( $borrowernumber, $itemnumber, $overduelevel, $method,
1339 =head2 RemoveNotifyLine
1341 &RemoveNotifyLine( $borrowernumber, $itemnumber, $notify_date );
1343 Cancel a notification
1347 sub RemoveNotifyLine {
1348 my ( $borrowernumber, $itemnumber, $notify_date ) = @_;
1349 my $dbh = C4::Context->dbh;
1350 my $sth = $dbh->prepare(
1351 "DELETE FROM notifys
1357 $sth->execute( $borrowernumber, $itemnumber, $notify_date );
1367 Koha Developement team <info@koha.org>