4 # Copyright 2000-2002 Katipo Communications
5 # copyright 2010 BibLibre
7 # This file is part of Koha.
9 # Koha is free software; you can redistribute it and/or modify it under the
10 # terms of the GNU General Public License as published by the Free Software
11 # Foundation; either version 2 of the License, or (at your option) any later
14 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
15 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
16 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License along
19 # with Koha; if not, write to the Free Software Foundation, Inc.,
20 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 #use warnings; FIXME - Bug 2505
24 use Date::Calc qw/Today Date_to_Days/;
25 use Date::Manip qw/UnixDate/;
29 use C4::Log; # logaction
32 use vars qw($VERSION @ISA @EXPORT);
35 # set the version for version checking
39 # subs to rename (and maybe merge some...)
44 &CheckAccountLineLevelInfo
45 &CheckAccountLineItemInfo
46 &CheckExistantNotifyid
56 &CreateItemAccountLine
69 # check that an equivalent don't exist already before moving
71 # subs to move to Circulation.pm
76 # &GetIssuingRules - delete.
77 # use C4::Circulation::GetIssuingRule instead.
79 # subs to move to Members.pm
81 &CheckBorrowerDebarred
83 # subs to move to Biblio.pm
92 C4::Circulation::Fines - Koha module dealing with fines
100 This module contains several functions for dealing with fines for
101 overdue items. It is primarily used by the 'misc/fines2.pl' script.
107 $overdues = Getoverdues( { minimumdays => 1, maximumdays => 30 } );
109 Returns the list of all overdue books, with their itemtype.
111 C<$overdues> is a reference-to-array. Each element is a
112 reference-to-hash whose keys are the fields of the issues table in the
120 my $dbh = C4::Context->dbh;
122 if ( C4::Context->preference('item-level_itypes') ) {
124 SELECT issues.*, items.itype as itemtype, items.homebranch, items.barcode
126 LEFT JOIN items USING (itemnumber)
127 WHERE date_due < CURDATE()
131 SELECT issues.*, biblioitems.itemtype, items.itype, items.homebranch, items.barcode
133 LEFT JOIN items USING (itemnumber)
134 LEFT JOIN biblioitems USING (biblioitemnumber)
135 WHERE date_due < CURDATE()
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);
161 Returns a count and a list of overdueitems for a given borrowernumber
166 my $borrowernumber = shift or return;
167 # don't select biblioitems.marc or biblioitems.marcxml... too slow on large systems
168 my $sth = C4::Context->dbh->prepare(
169 "SELECT biblio.*, items.*, issues.*,
172 biblioitems.itemtype,
175 biblioitems.publicationyear,
176 biblioitems.publishercode,
177 biblioitems.volumedate,
178 biblioitems.volumedesc,
179 biblioitems.collectiontitle,
180 biblioitems.collectionissn,
181 biblioitems.collectionvolume,
182 biblioitems.editionstatement,
183 biblioitems.editionresponsibility,
191 biblioitems.cn_source,
192 biblioitems.cn_class,
194 biblioitems.cn_suffix,
196 biblioitems.totalissues
198 LEFT JOIN items ON issues.itemnumber = items.itemnumber
199 LEFT JOIN biblio ON items.biblionumber = biblio.biblionumber
200 LEFT JOIN biblioitems ON items.biblioitemnumber = biblioitems.biblioitemnumber
201 WHERE issues.borrowernumber = ?
202 AND issues.date_due < CURDATE()"
204 # FIXME: SELECT * across 4 tables? do we really need the marc AND marcxml blobs??
205 $sth->execute($borrowernumber);
206 my $results = $sth->fetchall_arrayref({});
207 return ( scalar(@$results), $results); # returning the count and the results is silly
212 ($amount, $chargename, $daycount, $daycounttotal) = &CalcFine($item,
213 $categorycode, $branch, $days_overdue,
214 $description, $start_date, $end_date );
216 Calculates the fine for a book.
218 The issuingrules table in the Koha database is a fine matrix, listing
219 the penalties for each type of patron for each type of item and each branch (e.g., the
220 standard fine for books might be $0.50, but $1.50 for DVDs, or staff
221 members might get a longer grace period between the first and second
222 reminders that a book is overdue).
225 C<$item> is an item object (hashref).
227 C<$categorycode> is the category code (string) of the patron who currently has
230 C<$branchcode> is the library (string) whose issuingrules govern this transaction.
232 C<$days_overdue> is the number of days elapsed since the book's due date.
233 NOTE: supplying days_overdue is deprecated.
235 C<$start_date> & C<$end_date> are C4::Dates objects
236 defining the date range over which to determine the fine.
237 Note that if these are defined, we ignore C<$difference> and C<$dues> ,
238 but retain these for backwards-comptibility with extant fines scripts.
240 Fines scripts should just supply the date range over which to calculate the fine.
242 C<&CalcFine> returns four values:
244 C<$amount> is the fine owed by the patron (see above).
246 C<$chargename> is the chargename field from the applicable record in
247 the categoryitem table, whatever that is.
249 C<$daycount> is the number of days between start and end dates, Calendar adjusted (where needed),
250 minus any applicable grace period.
252 C<$daycounttotal> is C<$daycount> without consideration of grace period.
254 FIXME - What is chargename supposed to be ?
256 FIXME: previously attempted to return C<$message> as a text message, either "First Notice", "Second Notice",
257 or "Final Notice". But CalcFine never defined any value.
262 my ( $item, $bortype, $branchcode, $difference ,$dues , $start_date, $end_date ) = @_;
263 $debug and warn sprintf("CalcFine(%s, %s, %s, %s, %s, %s, %s)",
264 ($item ? '{item}' : 'UNDEF'),
265 ($bortype || 'UNDEF'),
266 ($branchcode || 'UNDEF'),
267 ($difference || 'UNDEF'),
269 ($start_date ? ($start_date->output('iso') || 'Not a C4::Dates object') : 'UNDEF'),
270 ( $end_date ? ( $end_date->output('iso') || 'Not a C4::Dates object') : 'UNDEF')
272 my $dbh = C4::Context->dbh;
275 # get issuingrules (fines part will be used)
276 $debug and warn sprintf("CalcFine calling GetIssuingRule(%s, %s, %s)", $bortype, $item->{'itemtype'}, $branchcode);
277 my $data = C4::Circulation::GetIssuingRule($bortype, $item->{'itemtype'}, $branchcode);
279 # if $difference is supplied, the difference has already been calculated, but we still need to adjust for the calendar.
280 # use copy-pasted functions from calendar module. (deprecated -- these functions will be removed from C4::Overdues ).
281 my $countspecialday = &GetSpecialHolidays($dues,$item->{itemnumber});
282 my $countrepeatableday = &GetRepeatableHolidays($dues,$item->{itemnumber},$difference);
283 my $countalldayclosed = $countspecialday + $countrepeatableday;
284 $daystocharge = $difference - $countalldayclosed;
286 # if $difference is not supplied, we have C4::Dates objects giving us the date range, and we use the calendar module.
287 if(C4::Context->preference('finesCalendar') eq 'noFinesWhenClosed') {
288 my $calendar = C4::Calendar->new( branchcode => $branchcode );
289 $daystocharge = $calendar->daysBetween( $start_date, $end_date );
291 $daystocharge = Date_to_Days(split('-',$end_date->output('iso'))) - Date_to_Days(split('-',$start_date->output('iso')));
294 # correct for grace period.
295 my $days_minus_grace = $daystocharge - $data->{'firstremind'};
296 if ($data->{'chargeperiod'} > 0 && $days_minus_grace > 0 ) {
297 $amount = int($daystocharge / $data->{'chargeperiod'}) * $data->{'fine'};
299 # a zero (or null) chargeperiod means no charge.
301 $amount = C4::Context->preference('maxFine') if(C4::Context->preference('maxFine') && ( $amount > C4::Context->preference('maxFine')));
302 $debug and warn sprintf("CalcFine returning (%s, %s, %s, %s)", $amount, $data->{'chargename'}, $days_minus_grace, $daystocharge);
303 return ($amount, $data->{'chargename'}, $days_minus_grace, $daystocharge);
304 # FIXME: chargename is NEVER populated anywhere.
308 =head2 GetSpecialHolidays
310 &GetSpecialHolidays($date_dues,$itemnumber);
312 return number of special days between date of the day and date due
314 C<$date_dues> is the envisaged date of book return.
316 C<$itemnumber> is the book's item number.
320 sub GetSpecialHolidays {
321 my ( $date_dues, $itemnumber ) = @_;
323 # calcul the today date
324 my $today = join "-", &Today();
326 # return the holdingbranch
327 my $iteminfo = GetIssuesIteminfo($itemnumber);
329 # use sql request to find all date between date_due and today
330 my $dbh = C4::Context->dbh;
332 qq|SELECT DATE_FORMAT(concat(year,'-',month,'-',day),'%Y-%m-%d') as date
333 FROM `special_holidays`
334 WHERE DATE_FORMAT(concat(year,'-',month,'-',day),'%Y-%m-%d') >= ?
335 AND DATE_FORMAT(concat(year,'-',month,'-',day),'%Y-%m-%d') <= ?
338 my @result = GetWdayFromItemnumber($itemnumber);
342 my $sth = $dbh->prepare($query);
343 $sth->execute( $date_dues, $today, $iteminfo->{'branchcode'} )
344 ; # FIXME: just use NOW() in SQL instead of passing in $today
346 while ( my $special_date = $sth->fetchrow_hashref ) {
347 push( @result_date, $special_date );
350 my $specialdaycount = scalar(@result_date);
352 for ( my $i = 0 ; $i < scalar(@result_date) ; $i++ ) {
353 $dateinsec = UnixDate( $result_date[$i]->{'date'}, "%o" );
354 ( undef, undef, undef, undef, undef, undef, $wday, undef, undef ) =
355 localtime($dateinsec);
356 for ( my $j = 0 ; $j < scalar(@result) ; $j++ ) {
357 if ( $wday == ( $result[$j]->{'weekday'} ) ) {
363 return $specialdaycount;
366 =head2 GetRepeatableHolidays
368 &GetRepeatableHolidays($date_dues, $itemnumber, $difference,);
370 return number of day closed between date of the day and date due
372 C<$date_dues> is the envisaged date of book return.
374 C<$itemnumber> is item number.
376 C<$difference> numbers of between day date of the day and date due
380 sub GetRepeatableHolidays {
381 my ( $date_dues, $itemnumber, $difference ) = @_;
382 my $dateinsec = UnixDate( $date_dues, "%o" );
383 my ( $sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst ) =
384 localtime($dateinsec);
385 my @result = GetWdayFromItemnumber($itemnumber);
389 for ( my $i = 0 ; $i < scalar(@result) ; $i++ ) {
392 for ( $j = 0 ; $j < $difference ; $j++ ) {
393 if ( $result[$i]->{'weekday'} == $k ) {
394 push( @dayclosedcount, $k );
397 ( $k = 0 ) if ( $k eq 7 );
400 return scalar(@dayclosedcount);
404 =head2 GetWayFromItemnumber
406 &Getwdayfromitemnumber($itemnumber);
408 return the different week day from repeatable_holidays table
410 C<$itemnumber> is item number.
414 sub GetWdayFromItemnumber {
415 my ($itemnumber) = @_;
416 my $iteminfo = GetIssuesIteminfo($itemnumber);
418 my $query = qq|SELECT weekday
419 FROM repeatable_holidays
422 my $sth = C4::Context->dbh->prepare($query);
424 $sth->execute( $iteminfo->{'branchcode'} );
425 while ( my $weekday = $sth->fetchrow_hashref ) {
426 push( @result, $weekday );
432 =head2 GetIssuesIteminfo
434 &GetIssuesIteminfo($itemnumber);
436 return all data from issues about item
438 C<$itemnumber> is item number.
442 sub GetIssuesIteminfo {
443 my ($itemnumber) = @_;
444 my $dbh = C4::Context->dbh;
445 my $query = qq|SELECT *
449 my $sth = $dbh->prepare($query);
450 $sth->execute($itemnumber);
451 my ($issuesinfo) = $sth->fetchrow_hashref;
458 &UpdateFine($itemnumber, $borrowernumber, $amount, $type, $description);
460 (Note: the following is mostly conjecture and guesswork.)
462 Updates the fine owed on an overdue book.
464 C<$itemnumber> is the book's item number.
466 C<$borrowernumber> is the borrower number of the patron who currently
467 has the book on loan.
469 C<$amount> is the current amount owed by the patron.
471 C<$type> will be used in the description of the fine.
473 C<$description> is a string that must be present in the description of
474 the fine. I think this is expected to be a date in DD/MM/YYYY format.
476 C<&UpdateFine> looks up the amount currently owed on the given item
477 and sets it to C<$amount>, creating, if necessary, a new entry in the
478 accountlines table of the Koha database.
483 # Question: Why should the caller have to
484 # specify both the item number and the borrower number? A book can't
485 # be on loan to two different people, so the item number should be
488 # Possible Answer: You might update a fine for a damaged item, *after* it is returned.
491 my ( $itemnum, $borrowernumber, $amount, $type, $due ) = @_;
492 $debug and warn "UpdateFine($itemnum, $borrowernumber, $amount, " . ($type||'""') . ", $due) called";
493 my $dbh = C4::Context->dbh;
494 # FIXME - What exactly is this query supposed to do? It looks up an
495 # entry in accountlines that matches the given item and borrower
496 # numbers, where the description contains $due, and where the
497 # account type has one of several values, but what does this _mean_?
498 # Does it look up existing fines for this item?
499 # FIXME - What are these various account types? ("FU", "O", "F", "M")
501 # "A" is Account Management Fee
506 # "FU" is Fine UPDATE??
508 # "REF" is Cash Refund
509 my $sth = $dbh->prepare(
510 "SELECT * FROM accountlines
513 AND accounttype IN ('FU','O','F','M')
514 AND description like ? "
516 $sth->execute( $itemnum, $borrowernumber, "%$due%" );
518 if ( my $data = $sth->fetchrow_hashref ) {
520 # we're updating an existing fine. Only modify if amount changed
521 # Note that in the current implementation, you cannot pay against an accruing fine
522 # (i.e. , of accounttype 'FU'). Doing so will break accrual.
523 if ( $data->{'amount'} != $amount ) {
524 my $diff = $amount - $data->{'amount'};
525 #3341: diff could be positive or negative!
526 my $out = $data->{'amountoutstanding'} + $diff;
529 SET date=now(), amount=?, amountoutstanding=?,
530 lastincrement=?, accounttype='FU'
531 WHERE borrowernumber=?
533 AND accounttype IN ('FU','O')
534 AND description LIKE ?
536 my $sth2 = $dbh->prepare($query);
537 # FIXME: BOGUS query cannot ensure uniqueness w/ LIKE %x% !!!
538 # LIMIT 1 added to prevent multiple affected lines
539 # FIXME: accountlines table needs unique key!! Possibly a combo of borrowernumber and accountline.
540 # But actually, we should just have a regular autoincrementing PK and forget accountline,
541 # including the bogus getnextaccountno function (doesn't prevent conflict on simultaneous ops).
542 # FIXME: Why only 2 account types here?
543 $debug and print STDERR "UpdateFine query: $query\n" .
544 "w/ args: $amount, $out, $diff, $data->{'borrowernumber'}, $data->{'itemnumber'}, \"\%$due\%\"\n";
545 $sth2->execute($amount, $out, $diff, $data->{'borrowernumber'}, $data->{'itemnumber'}, "%$due%");
547 # print "no update needed $data->{'amount'}"
550 my $sth4 = $dbh->prepare(
551 "SELECT title FROM biblio LEFT JOIN items ON biblio.biblionumber=items.biblionumber WHERE items.itemnumber=?"
553 $sth4->execute($itemnum);
554 my $title = $sth4->fetchrow;
556 # # print "not in account";
557 # my $sth3 = $dbh->prepare("Select max(accountno) from accountlines");
560 # # FIXME - Make $accountno a scalar.
561 # my @accountno = $sth3->fetchrow_array;
565 my $nextaccntno = C4::Accounts::getnextacctno($borrowernumber);
566 my $desc = ($type ? "$type " : '') . "$title $due"; # FIXEDME, avoid whitespace prefix on empty $type
567 my $query = "INSERT INTO accountlines
568 (borrowernumber,itemnumber,date,amount,description,accounttype,amountoutstanding,lastincrement,accountno)
569 VALUES (?,?,now(),?,?,'FU',?,?,?)";
570 my $sth2 = $dbh->prepare($query);
571 $debug and print STDERR "UpdateFine query: $query\nw/ args: $borrowernumber, $itemnum, $amount, $desc, $amount, $amount, $nextaccntno\n";
572 $sth2->execute($borrowernumber, $itemnum, $amount, $desc, $amount, $amount, $nextaccntno);
579 "due=".$due." amount=".$amount." itemnumber=".$itemnum
580 ) if C4::Context->preference("FinesLog");
585 $borrower = &BorType($borrowernumber);
587 Looks up a patron by borrower number.
589 C<$borrower> is a reference-to-hash whose keys are all of the fields
590 from the borrowers and categories tables of the Koha database. Thus,
591 C<$borrower> contains all information about both the borrower and
592 category he or she belongs to.
598 my ($borrowernumber) = @_;
599 my $dbh = C4::Context->dbh;
600 my $sth = $dbh->prepare(
601 "SELECT * from borrowers
602 LEFT JOIN categories ON borrowers.categorycode=categories.categorycode
603 WHERE borrowernumber=?"
605 $sth->execute($borrowernumber);
606 return $sth->fetchrow_hashref;
609 =head2 ReplacementCost
611 $cost = &ReplacementCost($itemnumber);
613 Returns the replacement cost of the item with the given item number.
618 sub ReplacementCost {
620 my $dbh = C4::Context->dbh;
622 $dbh->prepare("Select replacementprice from items where itemnumber=?");
623 $sth->execute($itemnum);
625 # FIXME - Use fetchrow_array or a slice.
626 my $data = $sth->fetchrow_hashref;
627 return ( $data->{'replacementprice'} );
632 $data->{'sum(amountoutstanding)'} = &GetFine($itemnum,$borrowernumber);
634 return the total of fine
636 C<$itemnum> is item number
638 C<$borrowernumber> is the borrowernumber
644 my ( $itemnum, $borrowernumber ) = @_;
645 my $dbh = C4::Context->dbh();
646 my $query = "SELECT sum(amountoutstanding) FROM accountlines
647 where accounttype like 'F%'
648 AND amountoutstanding > 0 AND itemnumber = ? AND borrowernumber=?";
649 my $sth = $dbh->prepare($query);
650 $sth->execute( $itemnum, $borrowernumber );
651 my $data = $sth->fetchrow_hashref();
652 return ( $data->{'sum(amountoutstanding)'} );
656 =head2 GetIssuingRules
658 FIXME - This sub should be deprecated and removed.
659 It ignores branch and defaults.
661 $data = &GetIssuingRules($itemtype,$categorycode);
663 Looks up for all issuingrules an item info
665 C<$itemnumber> is a reference-to-hash whose keys are all of the fields
666 from the borrowers and categories tables of the Koha database. Thus,
668 C<$categorycode> contains information about borrowers category
670 C<$data> contains all information about both the borrower and
671 category he or she belongs to.
674 sub GetIssuingRules {
675 warn "GetIssuingRules is deprecated: use GetIssuingRule from C4::Circulation instead.";
676 my ($itemtype,$categorycode)=@_;
677 my $dbh = C4::Context->dbh();
678 my $query=qq|SELECT *
680 WHERE issuingrules.itemtype=?
681 AND issuingrules.categorycode=?
683 my $sth = $dbh->prepare($query);
685 $sth->execute($itemtype,$categorycode);
686 return $sth->fetchrow_hashref;
690 sub ReplacementCost2 {
691 my ( $itemnum, $borrowernumber ) = @_;
692 my $dbh = C4::Context->dbh();
693 my $query = "SELECT amountoutstanding
695 WHERE accounttype like 'L'
696 AND amountoutstanding > 0
698 AND borrowernumber= ?";
699 my $sth = $dbh->prepare($query);
700 $sth->execute( $itemnum, $borrowernumber );
701 my $data = $sth->fetchrow_hashref();
702 return ( $data->{'amountoutstanding'} );
706 =head2 GetNextIdNotify
708 ($result) = &GetNextIdNotify($reference);
710 Returns the new file number
712 C<$result> contains the next file number
714 C<$reference> contains the beggining of file number
718 sub GetNextIdNotify {
719 my ($reference) = @_;
720 my $query = qq|SELECT max(notify_id)
722 WHERE notify_id like \"$reference%\"
725 # AND borrowernumber=?|;
726 my $dbh = C4::Context->dbh;
727 my $sth = $dbh->prepare($query);
729 my $result = $sth->fetchrow;
731 if ( $result eq '' ) {
732 ( $result = $reference . "01" );
735 $count = substr( $result, 6 ) + 1;
738 ( $count = "0" . $count );
740 $result = $reference . $count;
745 =head2 NumberNotifyId
747 (@notify) = &NumberNotifyId($borrowernumber);
749 Returns amount for all file per borrowers
750 C<@notify> array contains all file per borrowers
752 C<$notify_id> contains the file number for the borrower number nad item number
757 my ($borrowernumber)=@_;
758 my $dbh = C4::Context->dbh;
759 my $query=qq| SELECT distinct(notify_id)
761 WHERE borrowernumber=?|;
763 my $sth = $dbh->prepare($query);
764 $sth->execute($borrowernumber);
765 while ( my ($numberofnotify) = $sth->fetchrow ) {
766 push( @notify, $numberofnotify );
773 ($totalnotify) = &AmountNotify($notifyid);
775 Returns amount for all file per borrowers
776 C<$notifyid> is the file number
778 C<$totalnotify> contains amount of a file
780 C<$notify_id> contains the file number for the borrower number and item number
785 my ($notifyid,$borrowernumber)=@_;
786 my $dbh = C4::Context->dbh;
787 my $query=qq| SELECT sum(amountoutstanding)
789 WHERE notify_id=? AND borrowernumber = ?|;
790 my $sth=$dbh->prepare($query);
791 $sth->execute($notifyid,$borrowernumber);
792 my $totalnotify=$sth->fetchrow;
794 return ($totalnotify);
800 ($notify_id) = &GetNotifyId($borrowernumber,$itemnumber);
802 Returns the file number per borrower and itemnumber
804 C<$borrowernumber> is a reference-to-hash whose keys are all of the fields
805 from the items tables of the Koha database. Thus,
807 C<$itemnumber> contains the borrower categorycode
809 C<$notify_id> contains the file number for the borrower number nad item number
814 my ( $borrowernumber, $itemnumber ) = @_;
815 my $query = qq|SELECT notify_id
817 WHERE borrowernumber=?
819 AND (accounttype='FU' or accounttype='O')|;
820 my $dbh = C4::Context->dbh;
821 my $sth = $dbh->prepare($query);
822 $sth->execute( $borrowernumber, $itemnumber );
823 my ($notify_id) = $sth->fetchrow;
828 =head2 CreateItemAccountLine
830 () = &CreateItemAccountLine($borrowernumber, $itemnumber, $date, $amount,
831 $description, $accounttype, $amountoutstanding,
832 $timestamp, $notify_id, $level);
834 update the account lines with file number or with file level
836 C<$items> is a reference-to-hash whose keys are all of the fields
837 from the items tables of the Koha database. Thus,
839 C<$itemnumber> contains the item number
841 C<$borrowernumber> contains the borrower number
843 C<$date> contains the date of the day
845 C<$amount> contains item price
847 C<$description> contains the descritpion of accounttype
849 C<$accounttype> contains the account type
851 C<$amountoutstanding> contains the $amountoutstanding
853 C<$timestamp> contains the timestamp with time and the date of the day
855 C<$notify_id> contains the file number
857 C<$level> contains the file level
861 sub CreateItemAccountLine {
863 $borrowernumber, $itemnumber, $date, $amount,
864 $description, $accounttype, $amountoutstanding, $timestamp,
867 my $dbh = C4::Context->dbh;
868 my $nextaccntno = C4::Accounts::getnextacctno($borrowernumber);
869 my $query = "INSERT into accountlines
870 (borrowernumber,accountno,itemnumber,date,amount,description,accounttype,amountoutstanding,timestamp,notify_id,notify_level)
872 (?,?,?,?,?,?,?,?,?,?,?)";
874 my $sth = $dbh->prepare($query);
876 $borrowernumber, $nextaccntno, $itemnumber,
877 $date, $amount, $description,
878 $accounttype, $amountoutstanding, $timestamp,
883 =head2 UpdateAccountLines
885 () = &UpdateAccountLines($notify_id,$notify_level,$borrowernumber,$itemnumber);
887 update the account lines with file number or with file level
889 C<$items> is a reference-to-hash whose keys are all of the fields
890 from the items tables of the Koha database. Thus,
892 C<$itemnumber> contains the item number
894 C<$notify_id> contains the file number
896 C<$notify_level> contains the file level
898 C<$borrowernumber> contains the borrowernumber
902 sub UpdateAccountLines {
903 my ( $notify_id, $notify_level, $borrowernumber, $itemnumber ) = @_;
905 if ( $notify_id eq '' ) {
906 $query = qq|UPDATE accountlines
908 WHERE borrowernumber=? AND itemnumber=?
909 AND (accounttype='FU' or accounttype='O')|;
911 $query = qq|UPDATE accountlines
912 SET notify_id=?, notify_level=?
913 WHERE borrowernumber=?
915 AND (accounttype='FU' or accounttype='O')|;
918 my $sth = C4::Context->dbh->prepare($query);
919 if ( $notify_id eq '' ) {
920 $sth->execute( $notify_level, $borrowernumber, $itemnumber );
922 $sth->execute( $notify_id, $notify_level, $borrowernumber, $itemnumber );
928 ($items) = &GetItems($itemnumber);
930 Returns the list of all delays from overduerules.
932 C<$items> is a reference-to-hash whose keys are all of the fields
933 from the items tables of the Koha database. Thus,
935 C<$itemnumber> contains the borrower categorycode
939 # FIXME: This is a bad function to have here.
940 # Shouldn't it be in C4::Items?
941 # Shouldn't it be called GetItem since you only get 1 row?
942 # Shouldn't it be called GetItem since you give it only 1 itemnumber?
945 my $itemnumber = shift or return;
946 my $query = qq|SELECT *
949 my $sth = C4::Context->dbh->prepare($query);
950 $sth->execute($itemnumber);
951 my ($items) = $sth->fetchrow_hashref;
955 =head2 GetOverdueDelays
957 (@delays) = &GetOverdueDelays($categorycode);
959 Returns the list of all delays from overduerules.
961 C<@delays> it's an array contains the three delays from overduerules table
963 C<$categorycode> contains the borrower categorycode
967 sub GetOverdueDelays {
969 my $query = qq|SELECT delay1,delay2,delay3
971 WHERE categorycode=?|;
972 my $sth = C4::Context->dbh->prepare($query);
973 $sth->execute($category);
974 my (@delays) = $sth->fetchrow_array;
978 =head2 GetBranchcodesWithOverdueRules
980 my @branchcodes = C4::Overdues::GetBranchcodesWithOverdueRules()
982 returns a list of branch codes for branches with overdue rules defined.
986 sub GetBranchcodesWithOverdueRules {
987 my $dbh = C4::Context->dbh;
988 my $rqoverduebranches = $dbh->prepare("SELECT DISTINCT branchcode FROM overduerules WHERE delay1 IS NOT NULL AND branchcode <> '' ORDER BY branchcode");
989 $rqoverduebranches->execute;
990 my @branches = map { shift @$_ } @{ $rqoverduebranches->fetchall_arrayref };
992 my $availbranches = C4::Branch::GetBranches();
993 @branches = keys %$availbranches;
998 =head2 CheckAccountLineLevelInfo
1000 ($exist) = &CheckAccountLineLevelInfo($borrowernumber,$itemnumber,$accounttype,notify_level);
1002 Check and Returns the list of all overdue books.
1004 C<$exist> contains number of line in accounlines
1005 with the same .biblionumber,itemnumber,accounttype,and notify_level
1007 C<$borrowernumber> contains the borrower number
1009 C<$itemnumber> contains item number
1011 C<$accounttype> contains account type
1013 C<$notify_level> contains the accountline level
1018 sub CheckAccountLineLevelInfo {
1019 my ( $borrowernumber, $itemnumber, $level ) = @_;
1020 my $dbh = C4::Context->dbh;
1021 my $query = qq|SELECT count(*)
1023 WHERE borrowernumber =?
1025 AND notify_level=?|;
1026 my $sth = $dbh->prepare($query);
1027 $sth->execute( $borrowernumber, $itemnumber, $level );
1028 my ($exist) = $sth->fetchrow;
1032 =head2 GetOverduerules
1034 ($overduerules) = &GetOverduerules($categorycode);
1036 Returns the value of borrowers (debarred or not) with notify level
1038 C<$overduerules> return value of debbraed field in overduerules table
1040 C<$category> contains the borrower categorycode
1042 C<$notify_level> contains the notify level
1046 sub GetOverduerules {
1047 my ( $category, $notify_level ) = @_;
1048 my $dbh = C4::Context->dbh;
1049 my $query = qq|SELECT debarred$notify_level
1051 WHERE categorycode=?|;
1052 my $sth = $dbh->prepare($query);
1053 $sth->execute($category);
1054 my ($overduerules) = $sth->fetchrow;
1055 return ($overduerules);
1059 =head2 CheckBorrowerDebarred
1061 ($debarredstatus) = &CheckBorrowerDebarred($borrowernumber);
1063 Check if the borrowers is already debarred
1065 C<$debarredstatus> return 0 for not debarred and return 1 for debarred
1067 C<$borrowernumber> contains the borrower number
1071 # FIXME: Shouldn't this be in C4::Members?
1072 sub CheckBorrowerDebarred {
1073 my ($borrowernumber) = @_;
1074 my $dbh = C4::Context->dbh;
1078 WHERE borrowernumber=?
1079 AND debarred > NOW()
1081 my $sth = $dbh->prepare($query);
1082 $sth->execute($borrowernumber);
1083 my $debarredstatus = $sth->fetchrow;
1084 return $debarredstatus;
1088 =head2 CheckExistantNotifyid
1090 ($exist) = &CheckExistantNotifyid($borrowernumber,$itemnumber,$accounttype,$notify_id);
1092 Check and Returns the notify id if exist else return 0.
1094 C<$exist> contains a notify_id
1096 C<$borrowernumber> contains the borrower number
1098 C<$date_due> contains the date of item return
1103 sub CheckExistantNotifyid {
1104 my ( $borrowernumber, $date_due ) = @_;
1105 my $dbh = C4::Context->dbh;
1106 my $query = qq|SELECT notify_id FROM accountlines
1107 LEFT JOIN issues ON issues.itemnumber= accountlines.itemnumber
1108 WHERE accountlines.borrowernumber =?
1110 my $sth = $dbh->prepare($query);
1111 $sth->execute( $borrowernumber, $date_due );
1112 return $sth->fetchrow || 0;
1115 =head2 CheckAccountLineItemInfo
1117 ($exist) = &CheckAccountLineItemInfo($borrowernumber,$itemnumber,$accounttype,$notify_id);
1119 Check and Returns the list of all overdue items from the same file number(notify_id).
1121 C<$exist> contains number of line in accounlines
1122 with the same .biblionumber,itemnumber,accounttype,notify_id
1124 C<$borrowernumber> contains the borrower number
1126 C<$itemnumber> contains item number
1128 C<$accounttype> contains account type
1130 C<$notify_id> contains the file number
1134 sub CheckAccountLineItemInfo {
1135 my ( $borrowernumber, $itemnumber, $accounttype, $notify_id ) = @_;
1136 my $dbh = C4::Context->dbh;
1137 my $query = qq|SELECT count(*) FROM accountlines
1138 WHERE borrowernumber =?
1142 my $sth = $dbh->prepare($query);
1143 $sth->execute( $borrowernumber, $itemnumber, $accounttype, $notify_id );
1144 my ($exist) = $sth->fetchrow;
1148 =head2 CheckItemNotify
1150 Sql request to check if the document has alreday been notified
1151 this function is not exported, only used with GetOverduesForBranch
1155 sub CheckItemNotify {
1156 my ($notify_id,$notify_level,$itemnumber) = @_;
1157 my $dbh = C4::Context->dbh;
1158 my $sth = $dbh->prepare("
1162 AND notify_level = ?
1163 AND itemnumber = ? ");
1164 $sth->execute($notify_id,$notify_level,$itemnumber);
1165 my $notified = $sth->fetchrow;
1169 =head2 GetOverduesForBranch
1171 Sql request for display all information for branchoverdues.pl
1172 2 possibilities : with or without location .
1173 display is filtered by branch
1175 FIXME: This function should be renamed.
1179 sub GetOverduesForBranch {
1180 my ( $branch, $location) = @_;
1181 my $itype_link = (C4::Context->preference('item-level_itypes')) ? " items.itype " : " biblioitems.itemtype ";
1182 my $dbh = C4::Context->dbh;
1185 borrowers.borrowernumber,
1187 borrowers.firstname,
1192 biblio.biblionumber,
1196 branches.branchname,
1199 items.itemcallnumber,
1202 itemtypes.description,
1203 accountlines.notify_id,
1204 accountlines.notify_level,
1205 accountlines.amountoutstanding
1207 LEFT JOIN issues ON issues.itemnumber = accountlines.itemnumber
1208 AND issues.borrowernumber = accountlines.borrowernumber
1209 LEFT JOIN borrowers ON borrowers.borrowernumber = accountlines.borrowernumber
1210 LEFT JOIN items ON items.itemnumber = issues.itemnumber
1211 LEFT JOIN biblio ON biblio.biblionumber = items.biblionumber
1212 LEFT JOIN biblioitems ON biblioitems.biblioitemnumber = items.biblioitemnumber
1213 LEFT JOIN itemtypes ON itemtypes.itemtype = $itype_link
1214 LEFT JOIN branches ON branches.branchcode = issues.branchcode
1215 WHERE (accountlines.amountoutstanding != '0.000000')
1216 AND (accountlines.accounttype = 'FU' )
1217 AND (issues.branchcode = ? )
1218 AND (issues.date_due < CURDATE())
1224 $sth = $dbh->prepare("$select AND items.location = ? ORDER BY borrowers.surname, borrowers.firstname");
1225 $sth->execute($branch, $location);
1227 $sth = $dbh->prepare("$select ORDER BY borrowers.surname, borrowers.firstname");
1228 $sth->execute($branch);
1230 while ( my $data = $sth->fetchrow_hashref ) {
1231 #check if the document has already been notified
1232 my $countnotify = CheckItemNotify($data->{'notify_id'}, $data->{'notify_level'}, $data->{'itemnumber'});
1233 if ($countnotify eq '0') {
1234 $getoverdues[$i] = $data;
1238 return (@getoverdues);
1242 =head2 AddNotifyLine
1244 &AddNotifyLine($borrowernumber, $itemnumber, $overduelevel, $method, $notifyId)
1246 Create a line into notify, if the method is phone, the notification_send_date is implemented to
1251 my ( $borrowernumber, $itemnumber, $overduelevel, $method, $notifyId ) = @_;
1252 my $dbh = C4::Context->dbh;
1253 if ( $method eq "phone" ) {
1254 my $sth = $dbh->prepare(
1255 "INSERT INTO notifys (borrowernumber,itemnumber,notify_date,notify_send_date,notify_level,method,notify_id)
1256 VALUES (?,?,now(),now(),?,?,?)"
1258 $sth->execute( $borrowernumber, $itemnumber, $overduelevel, $method,
1262 my $sth = $dbh->prepare(
1263 "INSERT INTO notifys (borrowernumber,itemnumber,notify_date,notify_level,method,notify_id)
1264 VALUES (?,?,now(),?,?,?)"
1266 $sth->execute( $borrowernumber, $itemnumber, $overduelevel, $method,
1272 =head2 RemoveNotifyLine
1274 &RemoveNotifyLine( $borrowernumber, $itemnumber, $notify_date );
1276 Cancel a notification
1280 sub RemoveNotifyLine {
1281 my ( $borrowernumber, $itemnumber, $notify_date ) = @_;
1282 my $dbh = C4::Context->dbh;
1283 my $sth = $dbh->prepare(
1284 "DELETE FROM notifys
1290 $sth->execute( $borrowernumber, $itemnumber, $notify_date );
1299 Koha Development Team <http://koha-community.org/>