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 < NOW()
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 < 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);
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 < NOW()"
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, $daycounttotal) = &CalcFine($item,
213 $categorycode, $branch,
214 $start_dt, $end_dt );
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<$start_date> & C<$end_date> are DateTime objects
233 defining the date range over which to determine the fine.
235 Fines scripts should just supply the date range over which to calculate the fine.
237 C<&CalcFine> returns four values:
239 C<$amount> is the fine owed by the patron (see above).
241 C<$chargename> is the chargename field from the applicable record in
242 the categoryitem table, whatever that is.
244 C<$daycount> is the number of days between start and end dates, Calendar adjusted (where needed),
245 minus any applicable grace period.
247 FIXME - What is chargename supposed to be ?
249 FIXME: previously attempted to return C<$message> as a text message, either "First Notice", "Second Notice",
250 or "Final Notice". But CalcFine never defined any value.
255 my ( $item, $bortype, $branchcode, $due_dt, $end_date ) = @_;
256 my $start_date = $due_dt->clone();
257 my $dbh = C4::Context->dbh;
260 # get issuingrules (fines part will be used)
261 my $data = C4::Circulation::GetIssuingRule($bortype, $item->{itemtype}, $branchcode);
262 if(C4::Context->preference('finesCalendar') eq 'noFinesWhenClosed') {
263 my $calendar = Koha::Calendar->new( branchcode => $branchcode );
264 $charge_duration = $calendar->days_between( $start_date, $end_date );
266 $charge_duration = $end_date - $start_date;
268 # correct for grace period.
269 my $fine_unit = $data->{lengthunit};
270 $fine_unit ||= 'days';
271 my $chargeable_units;
272 if ($fine_unit eq 'hours') {
273 $chargeable_units = $charge_duration->hours(); # TODO closed times???
276 $chargeable_units = $charge_duration->days;
278 my $days_minus_grace = $chargeable_units - $data->{firstremind};
279 if ($data->{'chargeperiod'} && $days_minus_grace ) {
280 $amount = int($chargeable_units / $data->{'chargeperiod'}) * $data->{'fine'};# TODO fine calc should be in cents
282 # a zero (or null) chargeperiod means no charge.
284 if(C4::Context->preference('maxFine') && ( $amount > C4::Context->preference('maxFine'))) {
285 $amount = C4::Context->preference('maxFine');
287 return ($amount, $data->{chargename}, $days_minus_grace);
288 # FIXME: chargename is NEVER populated anywhere.
292 =head2 GetSpecialHolidays
294 &GetSpecialHolidays($date_dues,$itemnumber);
296 return number of special days between date of the day and date due
298 C<$date_dues> is the envisaged date of book return.
300 C<$itemnumber> is the book's item number.
304 sub GetSpecialHolidays {
305 my ( $date_dues, $itemnumber ) = @_;
307 # calcul the today date
308 my $today = join "-", &Today();
310 # return the holdingbranch
311 my $iteminfo = GetIssuesIteminfo($itemnumber);
313 # use sql request to find all date between date_due and today
314 my $dbh = C4::Context->dbh;
316 qq|SELECT DATE_FORMAT(concat(year,'-',month,'-',day),'%Y-%m-%d') as date
317 FROM `special_holidays`
318 WHERE DATE_FORMAT(concat(year,'-',month,'-',day),'%Y-%m-%d') >= ?
319 AND DATE_FORMAT(concat(year,'-',month,'-',day),'%Y-%m-%d') <= ?
322 my @result = GetWdayFromItemnumber($itemnumber);
326 my $sth = $dbh->prepare($query);
327 $sth->execute( $date_dues, $today, $iteminfo->{'branchcode'} )
328 ; # FIXME: just use NOW() in SQL instead of passing in $today
330 while ( my $special_date = $sth->fetchrow_hashref ) {
331 push( @result_date, $special_date );
334 my $specialdaycount = scalar(@result_date);
336 for ( my $i = 0 ; $i < scalar(@result_date) ; $i++ ) {
337 $dateinsec = UnixDate( $result_date[$i]->{'date'}, "%o" );
338 ( undef, undef, undef, undef, undef, undef, $wday, undef, undef ) =
339 localtime($dateinsec);
340 for ( my $j = 0 ; $j < scalar(@result) ; $j++ ) {
341 if ( $wday == ( $result[$j]->{'weekday'} ) ) {
347 return $specialdaycount;
350 =head2 GetRepeatableHolidays
352 &GetRepeatableHolidays($date_dues, $itemnumber, $difference,);
354 return number of day closed between date of the day and date due
356 C<$date_dues> is the envisaged date of book return.
358 C<$itemnumber> is item number.
360 C<$difference> numbers of between day date of the day and date due
364 sub GetRepeatableHolidays {
365 my ( $date_dues, $itemnumber, $difference ) = @_;
366 my $dateinsec = UnixDate( $date_dues, "%o" );
367 my ( $sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst ) =
368 localtime($dateinsec);
369 my @result = GetWdayFromItemnumber($itemnumber);
373 for ( my $i = 0 ; $i < scalar(@result) ; $i++ ) {
376 for ( $j = 0 ; $j < $difference ; $j++ ) {
377 if ( $result[$i]->{'weekday'} == $k ) {
378 push( @dayclosedcount, $k );
381 ( $k = 0 ) if ( $k eq 7 );
384 return scalar(@dayclosedcount);
388 =head2 GetWayFromItemnumber
390 &Getwdayfromitemnumber($itemnumber);
392 return the different week day from repeatable_holidays table
394 C<$itemnumber> is item number.
398 sub GetWdayFromItemnumber {
399 my ($itemnumber) = @_;
400 my $iteminfo = GetIssuesIteminfo($itemnumber);
402 my $query = qq|SELECT weekday
403 FROM repeatable_holidays
406 my $sth = C4::Context->dbh->prepare($query);
408 $sth->execute( $iteminfo->{'branchcode'} );
409 while ( my $weekday = $sth->fetchrow_hashref ) {
410 push( @result, $weekday );
416 =head2 GetIssuesIteminfo
418 &GetIssuesIteminfo($itemnumber);
420 return all data from issues about item
422 C<$itemnumber> is item number.
426 sub GetIssuesIteminfo {
427 my ($itemnumber) = @_;
428 my $dbh = C4::Context->dbh;
429 my $query = qq|SELECT *
433 my $sth = $dbh->prepare($query);
434 $sth->execute($itemnumber);
435 my ($issuesinfo) = $sth->fetchrow_hashref;
442 &UpdateFine($itemnumber, $borrowernumber, $amount, $type, $description);
444 (Note: the following is mostly conjecture and guesswork.)
446 Updates the fine owed on an overdue book.
448 C<$itemnumber> is the book's item number.
450 C<$borrowernumber> is the borrower number of the patron who currently
451 has the book on loan.
453 C<$amount> is the current amount owed by the patron.
455 C<$type> will be used in the description of the fine.
457 C<$description> is a string that must be present in the description of
458 the fine. I think this is expected to be a date in DD/MM/YYYY format.
460 C<&UpdateFine> looks up the amount currently owed on the given item
461 and sets it to C<$amount>, creating, if necessary, a new entry in the
462 accountlines table of the Koha database.
467 # Question: Why should the caller have to
468 # specify both the item number and the borrower number? A book can't
469 # be on loan to two different people, so the item number should be
472 # Possible Answer: You might update a fine for a damaged item, *after* it is returned.
475 my ( $itemnum, $borrowernumber, $amount, $type, $due ) = @_;
476 $debug and warn "UpdateFine($itemnum, $borrowernumber, $amount, " . ($type||'""') . ", $due) called";
477 my $dbh = C4::Context->dbh;
478 # FIXME - What exactly is this query supposed to do? It looks up an
479 # entry in accountlines that matches the given item and borrower
480 # numbers, where the description contains $due, and where the
481 # account type has one of several values, but what does this _mean_?
482 # Does it look up existing fines for this item?
483 # FIXME - What are these various account types? ("FU", "O", "F", "M")
485 # "A" is Account Management Fee
490 # "FU" is Fine UPDATE??
492 # "REF" is Cash Refund
493 my $sth = $dbh->prepare(
494 "SELECT * FROM accountlines
497 AND accounttype IN ('FU','O','F','M')
498 AND description like ? "
500 $sth->execute( $itemnum, $borrowernumber, "%$due%" );
502 if ( my $data = $sth->fetchrow_hashref ) {
504 # we're updating an existing fine. Only modify if amount changed
505 # Note that in the current implementation, you cannot pay against an accruing fine
506 # (i.e. , of accounttype 'FU'). Doing so will break accrual.
507 if ( $data->{'amount'} != $amount ) {
508 my $diff = $amount - $data->{'amount'};
509 #3341: diff could be positive or negative!
510 my $out = $data->{'amountoutstanding'} + $diff;
513 SET date=now(), amount=?, amountoutstanding=?,
514 lastincrement=?, accounttype='FU'
515 WHERE borrowernumber=?
517 AND accounttype IN ('FU','O')
518 AND description LIKE ?
520 my $sth2 = $dbh->prepare($query);
521 # FIXME: BOGUS query cannot ensure uniqueness w/ LIKE %x% !!!
522 # LIMIT 1 added to prevent multiple affected lines
523 # FIXME: accountlines table needs unique key!! Possibly a combo of borrowernumber and accountline.
524 # But actually, we should just have a regular autoincrementing PK and forget accountline,
525 # including the bogus getnextaccountno function (doesn't prevent conflict on simultaneous ops).
526 # FIXME: Why only 2 account types here?
527 $debug and print STDERR "UpdateFine query: $query\n" .
528 "w/ args: $amount, $out, $diff, $data->{'borrowernumber'}, $data->{'itemnumber'}, \"\%$due\%\"\n";
529 $sth2->execute($amount, $out, $diff, $data->{'borrowernumber'}, $data->{'itemnumber'}, "%$due%");
531 # print "no update needed $data->{'amount'}"
534 my $sth4 = $dbh->prepare(
535 "SELECT title FROM biblio LEFT JOIN items ON biblio.biblionumber=items.biblionumber WHERE items.itemnumber=?"
537 $sth4->execute($itemnum);
538 my $title = $sth4->fetchrow;
540 # # print "not in account";
541 # my $sth3 = $dbh->prepare("Select max(accountno) from accountlines");
544 # # FIXME - Make $accountno a scalar.
545 # my @accountno = $sth3->fetchrow_array;
549 my $nextaccntno = C4::Accounts::getnextacctno($borrowernumber);
550 my $desc = ($type ? "$type " : '') . "$title $due"; # FIXEDME, avoid whitespace prefix on empty $type
551 my $query = "INSERT INTO accountlines
552 (borrowernumber,itemnumber,date,amount,description,accounttype,amountoutstanding,lastincrement,accountno)
553 VALUES (?,?,now(),?,?,'FU',?,?,?)";
554 my $sth2 = $dbh->prepare($query);
555 $debug and print STDERR "UpdateFine query: $query\nw/ args: $borrowernumber, $itemnum, $amount, $desc, $amount, $amount, $nextaccntno\n";
556 $sth2->execute($borrowernumber, $itemnum, $amount, $desc, $amount, $amount, $nextaccntno);
563 "due=".$due." amount=".$amount." itemnumber=".$itemnum
564 ) if C4::Context->preference("FinesLog");
569 $borrower = &BorType($borrowernumber);
571 Looks up a patron by borrower number.
573 C<$borrower> is a reference-to-hash whose keys are all of the fields
574 from the borrowers and categories tables of the Koha database. Thus,
575 C<$borrower> contains all information about both the borrower and
576 category he or she belongs to.
582 my ($borrowernumber) = @_;
583 my $dbh = C4::Context->dbh;
584 my $sth = $dbh->prepare(
585 "SELECT * from borrowers
586 LEFT JOIN categories ON borrowers.categorycode=categories.categorycode
587 WHERE borrowernumber=?"
589 $sth->execute($borrowernumber);
590 return $sth->fetchrow_hashref;
593 =head2 ReplacementCost
595 $cost = &ReplacementCost($itemnumber);
597 Returns the replacement cost of the item with the given item number.
602 sub ReplacementCost {
604 my $dbh = C4::Context->dbh;
606 $dbh->prepare("Select replacementprice from items where itemnumber=?");
607 $sth->execute($itemnum);
609 # FIXME - Use fetchrow_array or a slice.
610 my $data = $sth->fetchrow_hashref;
611 return ( $data->{'replacementprice'} );
616 $data->{'sum(amountoutstanding)'} = &GetFine($itemnum,$borrowernumber);
618 return the total of fine
620 C<$itemnum> is item number
622 C<$borrowernumber> is the borrowernumber
628 my ( $itemnum, $borrowernumber ) = @_;
629 my $dbh = C4::Context->dbh();
630 my $query = q|SELECT sum(amountoutstanding) as fineamount FROM accountlines
631 where accounttype like 'F%'
632 AND amountoutstanding > 0 AND itemnumber = ? AND borrowernumber=?|;
633 my $sth = $dbh->prepare($query);
634 $sth->execute( $itemnum, $borrowernumber );
635 my $fine = $sth->fetchrow_hashref();
636 if ($fine->{fineamount}) {
637 return $fine->{fineamount};
643 =head2 GetIssuingRules
645 FIXME - This sub should be deprecated and removed.
646 It ignores branch and defaults.
648 $data = &GetIssuingRules($itemtype,$categorycode);
650 Looks up for all issuingrules an item info
652 C<$itemnumber> is a reference-to-hash whose keys are all of the fields
653 from the borrowers and categories tables of the Koha database. Thus,
655 C<$categorycode> contains information about borrowers category
657 C<$data> contains all information about both the borrower and
658 category he or she belongs to.
661 sub GetIssuingRules {
662 warn "GetIssuingRules is deprecated: use GetIssuingRule from C4::Circulation instead.";
663 my ($itemtype,$categorycode)=@_;
664 my $dbh = C4::Context->dbh();
665 my $query=qq|SELECT *
667 WHERE issuingrules.itemtype=?
668 AND issuingrules.categorycode=?
670 my $sth = $dbh->prepare($query);
672 $sth->execute($itemtype,$categorycode);
673 return $sth->fetchrow_hashref;
677 sub ReplacementCost2 {
678 my ( $itemnum, $borrowernumber ) = @_;
679 my $dbh = C4::Context->dbh();
680 my $query = "SELECT amountoutstanding
682 WHERE accounttype like 'L'
683 AND amountoutstanding > 0
685 AND borrowernumber= ?";
686 my $sth = $dbh->prepare($query);
687 $sth->execute( $itemnum, $borrowernumber );
688 my $data = $sth->fetchrow_hashref();
689 return ( $data->{'amountoutstanding'} );
693 =head2 GetNextIdNotify
695 ($result) = &GetNextIdNotify($reference);
697 Returns the new file number
699 C<$result> contains the next file number
701 C<$reference> contains the beggining of file number
705 sub GetNextIdNotify {
706 my ($reference) = @_;
707 my $query = qq|SELECT max(notify_id)
709 WHERE notify_id like \"$reference%\"
712 # AND borrowernumber=?|;
713 my $dbh = C4::Context->dbh;
714 my $sth = $dbh->prepare($query);
716 my $result = $sth->fetchrow;
718 if ( $result eq '' ) {
719 ( $result = $reference . "01" );
722 $count = substr( $result, 6 ) + 1;
725 ( $count = "0" . $count );
727 $result = $reference . $count;
732 =head2 NumberNotifyId
734 (@notify) = &NumberNotifyId($borrowernumber);
736 Returns amount for all file per borrowers
737 C<@notify> array contains all file per borrowers
739 C<$notify_id> contains the file number for the borrower number nad item number
744 my ($borrowernumber)=@_;
745 my $dbh = C4::Context->dbh;
746 my $query=qq| SELECT distinct(notify_id)
748 WHERE borrowernumber=?|;
750 my $sth = $dbh->prepare($query);
751 $sth->execute($borrowernumber);
752 while ( my ($numberofnotify) = $sth->fetchrow ) {
753 push( @notify, $numberofnotify );
760 ($totalnotify) = &AmountNotify($notifyid);
762 Returns amount for all file per borrowers
763 C<$notifyid> is the file number
765 C<$totalnotify> contains amount of a file
767 C<$notify_id> contains the file number for the borrower number and item number
772 my ($notifyid,$borrowernumber)=@_;
773 my $dbh = C4::Context->dbh;
774 my $query=qq| SELECT sum(amountoutstanding)
776 WHERE notify_id=? AND borrowernumber = ?|;
777 my $sth=$dbh->prepare($query);
778 $sth->execute($notifyid,$borrowernumber);
779 my $totalnotify=$sth->fetchrow;
781 return ($totalnotify);
787 ($notify_id) = &GetNotifyId($borrowernumber,$itemnumber);
789 Returns the file number per borrower and itemnumber
791 C<$borrowernumber> is a reference-to-hash whose keys are all of the fields
792 from the items tables of the Koha database. Thus,
794 C<$itemnumber> contains the borrower categorycode
796 C<$notify_id> contains the file number for the borrower number nad item number
801 my ( $borrowernumber, $itemnumber ) = @_;
802 my $query = qq|SELECT notify_id
804 WHERE borrowernumber=?
806 AND (accounttype='FU' or accounttype='O')|;
807 my $dbh = C4::Context->dbh;
808 my $sth = $dbh->prepare($query);
809 $sth->execute( $borrowernumber, $itemnumber );
810 my ($notify_id) = $sth->fetchrow;
815 =head2 CreateItemAccountLine
817 () = &CreateItemAccountLine($borrowernumber, $itemnumber, $date, $amount,
818 $description, $accounttype, $amountoutstanding,
819 $timestamp, $notify_id, $level);
821 update the account lines with file number or with file level
823 C<$items> is a reference-to-hash whose keys are all of the fields
824 from the items tables of the Koha database. Thus,
826 C<$itemnumber> contains the item number
828 C<$borrowernumber> contains the borrower number
830 C<$date> contains the date of the day
832 C<$amount> contains item price
834 C<$description> contains the descritpion of accounttype
836 C<$accounttype> contains the account type
838 C<$amountoutstanding> contains the $amountoutstanding
840 C<$timestamp> contains the timestamp with time and the date of the day
842 C<$notify_id> contains the file number
844 C<$level> contains the file level
848 sub CreateItemAccountLine {
850 $borrowernumber, $itemnumber, $date, $amount,
851 $description, $accounttype, $amountoutstanding, $timestamp,
854 my $dbh = C4::Context->dbh;
855 my $nextaccntno = C4::Accounts::getnextacctno($borrowernumber);
856 my $query = "INSERT into accountlines
857 (borrowernumber,accountno,itemnumber,date,amount,description,accounttype,amountoutstanding,timestamp,notify_id,notify_level)
859 (?,?,?,?,?,?,?,?,?,?,?)";
861 my $sth = $dbh->prepare($query);
863 $borrowernumber, $nextaccntno, $itemnumber,
864 $date, $amount, $description,
865 $accounttype, $amountoutstanding, $timestamp,
870 =head2 UpdateAccountLines
872 () = &UpdateAccountLines($notify_id,$notify_level,$borrowernumber,$itemnumber);
874 update the account lines with file number or with file level
876 C<$items> is a reference-to-hash whose keys are all of the fields
877 from the items tables of the Koha database. Thus,
879 C<$itemnumber> contains the item number
881 C<$notify_id> contains the file number
883 C<$notify_level> contains the file level
885 C<$borrowernumber> contains the borrowernumber
889 sub UpdateAccountLines {
890 my ( $notify_id, $notify_level, $borrowernumber, $itemnumber ) = @_;
892 if ( $notify_id eq '' ) {
893 $query = qq|UPDATE accountlines
895 WHERE borrowernumber=? AND itemnumber=?
896 AND (accounttype='FU' or accounttype='O')|;
898 $query = qq|UPDATE accountlines
899 SET notify_id=?, notify_level=?
900 WHERE borrowernumber=?
902 AND (accounttype='FU' or accounttype='O')|;
905 my $sth = C4::Context->dbh->prepare($query);
906 if ( $notify_id eq '' ) {
907 $sth->execute( $notify_level, $borrowernumber, $itemnumber );
909 $sth->execute( $notify_id, $notify_level, $borrowernumber, $itemnumber );
915 ($items) = &GetItems($itemnumber);
917 Returns the list of all delays from overduerules.
919 C<$items> is a reference-to-hash whose keys are all of the fields
920 from the items tables of the Koha database. Thus,
922 C<$itemnumber> contains the borrower categorycode
926 # FIXME: This is a bad function to have here.
927 # Shouldn't it be in C4::Items?
928 # Shouldn't it be called GetItem since you only get 1 row?
929 # Shouldn't it be called GetItem since you give it only 1 itemnumber?
932 my $itemnumber = shift or return;
933 my $query = qq|SELECT *
936 my $sth = C4::Context->dbh->prepare($query);
937 $sth->execute($itemnumber);
938 my ($items) = $sth->fetchrow_hashref;
942 =head2 GetOverdueDelays
944 (@delays) = &GetOverdueDelays($categorycode);
946 Returns the list of all delays from overduerules.
948 C<@delays> it's an array contains the three delays from overduerules table
950 C<$categorycode> contains the borrower categorycode
954 sub GetOverdueDelays {
956 my $query = qq|SELECT delay1,delay2,delay3
958 WHERE categorycode=?|;
959 my $sth = C4::Context->dbh->prepare($query);
960 $sth->execute($category);
961 my (@delays) = $sth->fetchrow_array;
965 =head2 GetBranchcodesWithOverdueRules
967 my @branchcodes = C4::Overdues::GetBranchcodesWithOverdueRules()
969 returns a list of branch codes for branches with overdue rules defined.
973 sub GetBranchcodesWithOverdueRules {
974 my $dbh = C4::Context->dbh;
975 my $rqoverduebranches = $dbh->prepare("SELECT DISTINCT branchcode FROM overduerules WHERE delay1 IS NOT NULL AND branchcode <> '' ORDER BY branchcode");
976 $rqoverduebranches->execute;
977 my @branches = map { shift @$_ } @{ $rqoverduebranches->fetchall_arrayref };
979 my $availbranches = C4::Branch::GetBranches();
980 @branches = keys %$availbranches;
985 =head2 CheckAccountLineLevelInfo
987 ($exist) = &CheckAccountLineLevelInfo($borrowernumber,$itemnumber,$accounttype,notify_level);
989 Check and Returns the list of all overdue books.
991 C<$exist> contains number of line in accounlines
992 with the same .biblionumber,itemnumber,accounttype,and notify_level
994 C<$borrowernumber> contains the borrower number
996 C<$itemnumber> contains item number
998 C<$accounttype> contains account type
1000 C<$notify_level> contains the accountline level
1005 sub CheckAccountLineLevelInfo {
1006 my ( $borrowernumber, $itemnumber, $level ) = @_;
1007 my $dbh = C4::Context->dbh;
1008 my $query = qq|SELECT count(*)
1010 WHERE borrowernumber =?
1012 AND notify_level=?|;
1013 my $sth = $dbh->prepare($query);
1014 $sth->execute( $borrowernumber, $itemnumber, $level );
1015 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;
1042 return ($overduerules);
1046 =head2 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
1058 # FIXME: Shouldn't this be in C4::Members?
1059 sub CheckBorrowerDebarred {
1060 my ($borrowernumber) = @_;
1061 my $dbh = C4::Context->dbh;
1065 WHERE borrowernumber=?
1066 AND debarred > NOW()
1068 my $sth = $dbh->prepare($query);
1069 $sth->execute($borrowernumber);
1070 my $debarredstatus = $sth->fetchrow;
1071 return $debarredstatus;
1075 =head2 CheckExistantNotifyid
1077 ($exist) = &CheckExistantNotifyid($borrowernumber,$itemnumber,$accounttype,$notify_id);
1079 Check and Returns the notify id if exist else return 0.
1081 C<$exist> contains a notify_id
1083 C<$borrowernumber> contains the borrower number
1085 C<$date_due> contains the date of item return
1090 sub CheckExistantNotifyid {
1091 my ( $borrowernumber, $date_due ) = @_;
1092 my $dbh = C4::Context->dbh;
1093 my $query = qq|SELECT notify_id FROM accountlines
1094 LEFT JOIN issues ON issues.itemnumber= accountlines.itemnumber
1095 WHERE accountlines.borrowernumber =?
1097 my $sth = $dbh->prepare($query);
1098 $sth->execute( $borrowernumber, $date_due );
1099 return $sth->fetchrow || 0;
1102 =head2 CheckAccountLineItemInfo
1104 ($exist) = &CheckAccountLineItemInfo($borrowernumber,$itemnumber,$accounttype,$notify_id);
1106 Check and Returns the list of all overdue items from the same file number(notify_id).
1108 C<$exist> contains number of line in accounlines
1109 with the same .biblionumber,itemnumber,accounttype,notify_id
1111 C<$borrowernumber> contains the borrower number
1113 C<$itemnumber> contains item number
1115 C<$accounttype> contains account type
1117 C<$notify_id> contains the file number
1121 sub CheckAccountLineItemInfo {
1122 my ( $borrowernumber, $itemnumber, $accounttype, $notify_id ) = @_;
1123 my $dbh = C4::Context->dbh;
1124 my $query = qq|SELECT count(*) FROM accountlines
1125 WHERE borrowernumber =?
1129 my $sth = $dbh->prepare($query);
1130 $sth->execute( $borrowernumber, $itemnumber, $accounttype, $notify_id );
1131 my ($exist) = $sth->fetchrow;
1135 =head2 CheckItemNotify
1137 Sql request to check if the document has alreday been notified
1138 this function is not exported, only used with GetOverduesForBranch
1142 sub CheckItemNotify {
1143 my ($notify_id,$notify_level,$itemnumber) = @_;
1144 my $dbh = C4::Context->dbh;
1145 my $sth = $dbh->prepare("
1149 AND notify_level = ?
1150 AND itemnumber = ? ");
1151 $sth->execute($notify_id,$notify_level,$itemnumber);
1152 my $notified = $sth->fetchrow;
1156 =head2 GetOverduesForBranch
1158 Sql request for display all information for branchoverdues.pl
1159 2 possibilities : with or without location .
1160 display is filtered by branch
1162 FIXME: This function should be renamed.
1166 sub GetOverduesForBranch {
1167 my ( $branch, $location) = @_;
1168 my $itype_link = (C4::Context->preference('item-level_itypes')) ? " items.itype " : " biblioitems.itemtype ";
1169 my $dbh = C4::Context->dbh;
1172 borrowers.borrowernumber,
1174 borrowers.firstname,
1179 biblio.biblionumber,
1183 branches.branchname,
1186 items.itemcallnumber,
1189 itemtypes.description,
1190 accountlines.notify_id,
1191 accountlines.notify_level,
1192 accountlines.amountoutstanding
1194 LEFT JOIN issues ON issues.itemnumber = accountlines.itemnumber
1195 AND issues.borrowernumber = accountlines.borrowernumber
1196 LEFT JOIN borrowers ON borrowers.borrowernumber = accountlines.borrowernumber
1197 LEFT JOIN items ON items.itemnumber = issues.itemnumber
1198 LEFT JOIN biblio ON biblio.biblionumber = items.biblionumber
1199 LEFT JOIN biblioitems ON biblioitems.biblioitemnumber = items.biblioitemnumber
1200 LEFT JOIN itemtypes ON itemtypes.itemtype = $itype_link
1201 LEFT JOIN branches ON branches.branchcode = issues.branchcode
1202 WHERE (accountlines.amountoutstanding != '0.000000')
1203 AND (accountlines.accounttype = 'FU' )
1204 AND (issues.branchcode = ? )
1205 AND (issues.date_due < NOW())
1211 $sth = $dbh->prepare("$select AND items.location = ? ORDER BY borrowers.surname, borrowers.firstname");
1212 $sth->execute($branch, $location);
1214 $sth = $dbh->prepare("$select ORDER BY borrowers.surname, borrowers.firstname");
1215 $sth->execute($branch);
1217 while ( my $data = $sth->fetchrow_hashref ) {
1218 #check if the document has already been notified
1219 my $countnotify = CheckItemNotify($data->{'notify_id'}, $data->{'notify_level'}, $data->{'itemnumber'});
1220 if ($countnotify eq '0') {
1221 $getoverdues[$i] = $data;
1225 return (@getoverdues);
1229 =head2 AddNotifyLine
1231 &AddNotifyLine($borrowernumber, $itemnumber, $overduelevel, $method, $notifyId)
1233 Create a line into notify, if the method is phone, the notification_send_date is implemented to
1238 my ( $borrowernumber, $itemnumber, $overduelevel, $method, $notifyId ) = @_;
1239 my $dbh = C4::Context->dbh;
1240 if ( $method eq "phone" ) {
1241 my $sth = $dbh->prepare(
1242 "INSERT INTO notifys (borrowernumber,itemnumber,notify_date,notify_send_date,notify_level,method,notify_id)
1243 VALUES (?,?,now(),now(),?,?,?)"
1245 $sth->execute( $borrowernumber, $itemnumber, $overduelevel, $method,
1249 my $sth = $dbh->prepare(
1250 "INSERT INTO notifys (borrowernumber,itemnumber,notify_date,notify_level,method,notify_id)
1251 VALUES (?,?,now(),?,?,?)"
1253 $sth->execute( $borrowernumber, $itemnumber, $overduelevel, $method,
1259 =head2 RemoveNotifyLine
1261 &RemoveNotifyLine( $borrowernumber, $itemnumber, $notify_date );
1263 Cancel a notification
1267 sub RemoveNotifyLine {
1268 my ( $borrowernumber, $itemnumber, $notify_date ) = @_;
1269 my $dbh = C4::Context->dbh;
1270 my $sth = $dbh->prepare(
1271 "DELETE FROM notifys
1277 $sth->execute( $borrowernumber, $itemnumber, $notify_date );
1286 Koha Development Team <http://koha-community.org/>