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
82 &UpdateBorrowerDebarred
84 # subs to move to Biblio.pm
93 C4::Circulation::Fines - Koha module dealing with fines
101 This module contains several functions for dealing with fines for
102 overdue items. It is primarily used by the 'misc/fines2.pl' script.
108 $overdues = Getoverdues( { minimumdays => 1, maximumdays => 30 } );
110 Returns the list of all overdue books, with their itemtype.
112 C<$overdues> is a reference-to-array. Each element is a
113 reference-to-hash whose keys are the fields of the issues table in the
121 my $dbh = C4::Context->dbh;
123 if ( C4::Context->preference('item-level_itypes') ) {
125 SELECT issues.*, items.itype as itemtype, items.homebranch, items.barcode
127 LEFT JOIN items USING (itemnumber)
128 WHERE date_due < CURDATE()
132 SELECT issues.*, biblioitems.itemtype, items.itype, items.homebranch, items.barcode
134 LEFT JOIN items USING (itemnumber)
135 LEFT JOIN biblioitems USING (biblioitemnumber)
136 WHERE date_due < CURDATE()
141 if ( exists $params->{'minimumdays'} and exists $params->{'maximumdays'} ) {
142 $statement .= ' AND TO_DAYS( NOW() )-TO_DAYS( date_due ) BETWEEN ? and ? ';
143 push @bind_parameters, $params->{'minimumdays'}, $params->{'maximumdays'};
144 } elsif ( exists $params->{'minimumdays'} ) {
145 $statement .= ' AND ( TO_DAYS( NOW() )-TO_DAYS( date_due ) ) > ? ';
146 push @bind_parameters, $params->{'minimumdays'};
147 } elsif ( exists $params->{'maximumdays'} ) {
148 $statement .= ' AND ( TO_DAYS( NOW() )-TO_DAYS( date_due ) ) < ? ';
149 push @bind_parameters, $params->{'maximumdays'};
151 $statement .= 'ORDER BY borrowernumber';
152 my $sth = $dbh->prepare( $statement );
153 $sth->execute( @bind_parameters );
154 return $sth->fetchall_arrayref({});
160 ($count, $overdueitems) = checkoverdues($borrowernumber);
162 Returns a count and a list of overdueitems for a given borrowernumber
167 my $borrowernumber = shift or return;
168 # don't select biblioitems.marc or biblioitems.marcxml... too slow on large systems
169 my $sth = C4::Context->dbh->prepare(
170 "SELECT biblio.*, items.*, issues.*,
173 biblioitems.itemtype,
176 biblioitems.publicationyear,
177 biblioitems.publishercode,
178 biblioitems.volumedate,
179 biblioitems.volumedesc,
180 biblioitems.collectiontitle,
181 biblioitems.collectionissn,
182 biblioitems.collectionvolume,
183 biblioitems.editionstatement,
184 biblioitems.editionresponsibility,
192 biblioitems.cn_source,
193 biblioitems.cn_class,
195 biblioitems.cn_suffix,
197 biblioitems.totalissues
199 LEFT JOIN items ON issues.itemnumber = items.itemnumber
200 LEFT JOIN biblio ON items.biblionumber = biblio.biblionumber
201 LEFT JOIN biblioitems ON items.biblioitemnumber = biblioitems.biblioitemnumber
202 WHERE issues.borrowernumber = ?
203 AND issues.date_due < CURDATE()"
205 # FIXME: SELECT * across 4 tables? do we really need the marc AND marcxml blobs??
206 $sth->execute($borrowernumber);
207 my $results = $sth->fetchall_arrayref({});
208 return ( scalar(@$results), $results); # returning the count and the results is silly
213 ($amount, $chargename, $daycount, $daycounttotal) = &CalcFine($item,
214 $categorycode, $branch, $days_overdue,
215 $description, $start_date, $end_date );
217 Calculates the fine for a book.
219 The issuingrules table in the Koha database is a fine matrix, listing
220 the penalties for each type of patron for each type of item and each branch (e.g., the
221 standard fine for books might be $0.50, but $1.50 for DVDs, or staff
222 members might get a longer grace period between the first and second
223 reminders that a book is overdue).
226 C<$item> is an item object (hashref).
228 C<$categorycode> is the category code (string) of the patron who currently has
231 C<$branchcode> is the library (string) whose issuingrules govern this transaction.
233 C<$days_overdue> is the number of days elapsed since the book's due date.
234 NOTE: supplying days_overdue is deprecated.
236 C<$start_date> & C<$end_date> are C4::Dates objects
237 defining the date range over which to determine the fine.
238 Note that if these are defined, we ignore C<$difference> and C<$dues> ,
239 but retain these for backwards-comptibility with extant fines scripts.
241 Fines scripts should just supply the date range over which to calculate the fine.
243 C<&CalcFine> returns four values:
245 C<$amount> is the fine owed by the patron (see above).
247 C<$chargename> is the chargename field from the applicable record in
248 the categoryitem table, whatever that is.
250 C<$daycount> is the number of days between start and end dates, Calendar adjusted (where needed),
251 minus any applicable grace period.
253 C<$daycounttotal> is C<$daycount> without consideration of grace period.
255 FIXME - What is chargename supposed to be ?
257 FIXME: previously attempted to return C<$message> as a text message, either "First Notice", "Second Notice",
258 or "Final Notice". But CalcFine never defined any value.
263 my ( $item, $bortype, $branchcode, $difference ,$dues , $start_date, $end_date ) = @_;
264 $debug and warn sprintf("CalcFine(%s, %s, %s, %s, %s, %s, %s)",
265 ($item ? '{item}' : 'UNDEF'),
266 ($bortype || 'UNDEF'),
267 ($branchcode || 'UNDEF'),
268 ($difference || 'UNDEF'),
270 ($start_date ? ($start_date->output('iso') || 'Not a C4::Dates object') : 'UNDEF'),
271 ( $end_date ? ( $end_date->output('iso') || 'Not a C4::Dates object') : 'UNDEF')
273 my $dbh = C4::Context->dbh;
276 # get issuingrules (fines part will be used)
277 $debug and warn sprintf("CalcFine calling GetIssuingRule(%s, %s, %s)", $bortype, $item->{'itemtype'}, $branchcode);
278 my $data = C4::Circulation::GetIssuingRule($bortype, $item->{'itemtype'}, $branchcode);
280 # if $difference is supplied, the difference has already been calculated, but we still need to adjust for the calendar.
281 # use copy-pasted functions from calendar module. (deprecated -- these functions will be removed from C4::Overdues ).
282 my $countspecialday = &GetSpecialHolidays($dues,$item->{itemnumber});
283 my $countrepeatableday = &GetRepeatableHolidays($dues,$item->{itemnumber},$difference);
284 my $countalldayclosed = $countspecialday + $countrepeatableday;
285 $daystocharge = $difference - $countalldayclosed;
287 # if $difference is not supplied, we have C4::Dates objects giving us the date range, and we use the calendar module.
288 if(C4::Context->preference('finesCalendar') eq 'noFinesWhenClosed') {
289 my $calendar = C4::Calendar->new( branchcode => $branchcode );
290 $daystocharge = $calendar->daysBetween( $start_date, $end_date );
292 $daystocharge = Date_to_Days(split('-',$end_date->output('iso'))) - Date_to_Days(split('-',$start_date->output('iso')));
295 # correct for grace period.
296 my $days_minus_grace = $daystocharge - $data->{'firstremind'};
297 if ($data->{'chargeperiod'} > 0 && $days_minus_grace > 0 ) {
298 $amount = int($daystocharge / $data->{'chargeperiod'}) * $data->{'fine'};
300 # a zero (or null) chargeperiod means no charge.
302 $amount = C4::Context->preference('maxFine') if(C4::Context->preference('maxFine') && ( $amount > C4::Context->preference('maxFine')));
303 $debug and warn sprintf("CalcFine returning (%s, %s, %s, %s)", $amount, $data->{'chargename'}, $days_minus_grace, $daystocharge);
304 return ($amount, $data->{'chargename'}, $days_minus_grace, $daystocharge);
305 # FIXME: chargename is NEVER populated anywhere.
309 =head2 GetSpecialHolidays
311 &GetSpecialHolidays($date_dues,$itemnumber);
313 return number of special days between date of the day and date due
315 C<$date_dues> is the envisaged date of book return.
317 C<$itemnumber> is the book's item number.
321 sub GetSpecialHolidays {
322 my ( $date_dues, $itemnumber ) = @_;
324 # calcul the today date
325 my $today = join "-", &Today();
327 # return the holdingbranch
328 my $iteminfo = GetIssuesIteminfo($itemnumber);
330 # use sql request to find all date between date_due and today
331 my $dbh = C4::Context->dbh;
333 qq|SELECT DATE_FORMAT(concat(year,'-',month,'-',day),'%Y-%m-%d') as date
334 FROM `special_holidays`
335 WHERE DATE_FORMAT(concat(year,'-',month,'-',day),'%Y-%m-%d') >= ?
336 AND DATE_FORMAT(concat(year,'-',month,'-',day),'%Y-%m-%d') <= ?
339 my @result = GetWdayFromItemnumber($itemnumber);
343 my $sth = $dbh->prepare($query);
344 $sth->execute( $date_dues, $today, $iteminfo->{'branchcode'} )
345 ; # FIXME: just use NOW() in SQL instead of passing in $today
347 while ( my $special_date = $sth->fetchrow_hashref ) {
348 push( @result_date, $special_date );
351 my $specialdaycount = scalar(@result_date);
353 for ( my $i = 0 ; $i < scalar(@result_date) ; $i++ ) {
354 $dateinsec = UnixDate( $result_date[$i]->{'date'}, "%o" );
355 ( undef, undef, undef, undef, undef, undef, $wday, undef, undef ) =
356 localtime($dateinsec);
357 for ( my $j = 0 ; $j < scalar(@result) ; $j++ ) {
358 if ( $wday == ( $result[$j]->{'weekday'} ) ) {
364 return $specialdaycount;
367 =head2 GetRepeatableHolidays
369 &GetRepeatableHolidays($date_dues, $itemnumber, $difference,);
371 return number of day closed between date of the day and date due
373 C<$date_dues> is the envisaged date of book return.
375 C<$itemnumber> is item number.
377 C<$difference> numbers of between day date of the day and date due
381 sub GetRepeatableHolidays {
382 my ( $date_dues, $itemnumber, $difference ) = @_;
383 my $dateinsec = UnixDate( $date_dues, "%o" );
384 my ( $sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst ) =
385 localtime($dateinsec);
386 my @result = GetWdayFromItemnumber($itemnumber);
390 for ( my $i = 0 ; $i < scalar(@result) ; $i++ ) {
393 for ( $j = 0 ; $j < $difference ; $j++ ) {
394 if ( $result[$i]->{'weekday'} == $k ) {
395 push( @dayclosedcount, $k );
398 ( $k = 0 ) if ( $k eq 7 );
401 return scalar(@dayclosedcount);
405 =head2 GetWayFromItemnumber
407 &Getwdayfromitemnumber($itemnumber);
409 return the different week day from repeatable_holidays table
411 C<$itemnumber> is item number.
415 sub GetWdayFromItemnumber {
416 my ($itemnumber) = @_;
417 my $iteminfo = GetIssuesIteminfo($itemnumber);
419 my $query = qq|SELECT weekday
420 FROM repeatable_holidays
423 my $sth = C4::Context->dbh->prepare($query);
425 $sth->execute( $iteminfo->{'branchcode'} );
426 while ( my $weekday = $sth->fetchrow_hashref ) {
427 push( @result, $weekday );
433 =head2 GetIssuesIteminfo
435 &GetIssuesIteminfo($itemnumber);
437 return all data from issues about item
439 C<$itemnumber> is item number.
443 sub GetIssuesIteminfo {
444 my ($itemnumber) = @_;
445 my $dbh = C4::Context->dbh;
446 my $query = qq|SELECT *
450 my $sth = $dbh->prepare($query);
451 $sth->execute($itemnumber);
452 my ($issuesinfo) = $sth->fetchrow_hashref;
459 &UpdateFine($itemnumber, $borrowernumber, $amount, $type, $description);
461 (Note: the following is mostly conjecture and guesswork.)
463 Updates the fine owed on an overdue book.
465 C<$itemnumber> is the book's item number.
467 C<$borrowernumber> is the borrower number of the patron who currently
468 has the book on loan.
470 C<$amount> is the current amount owed by the patron.
472 C<$type> will be used in the description of the fine.
474 C<$description> is a string that must be present in the description of
475 the fine. I think this is expected to be a date in DD/MM/YYYY format.
477 C<&UpdateFine> looks up the amount currently owed on the given item
478 and sets it to C<$amount>, creating, if necessary, a new entry in the
479 accountlines table of the Koha database.
484 # Question: Why should the caller have to
485 # specify both the item number and the borrower number? A book can't
486 # be on loan to two different people, so the item number should be
489 # Possible Answer: You might update a fine for a damaged item, *after* it is returned.
492 my ( $itemnum, $borrowernumber, $amount, $type, $due ) = @_;
493 $debug and warn "UpdateFine($itemnum, $borrowernumber, $amount, " . ($type||'""') . ", $due) called";
494 my $dbh = C4::Context->dbh;
495 # FIXME - What exactly is this query supposed to do? It looks up an
496 # entry in accountlines that matches the given item and borrower
497 # numbers, where the description contains $due, and where the
498 # account type has one of several values, but what does this _mean_?
499 # Does it look up existing fines for this item?
500 # FIXME - What are these various account types? ("FU", "O", "F", "M")
502 # "A" is Account Management Fee
507 # "FU" is Fine UPDATE??
509 # "REF" is Cash Refund
510 my $sth = $dbh->prepare(
511 "SELECT * FROM accountlines
514 AND accounttype IN ('FU','O','F','M')
515 AND description like ? "
517 $sth->execute( $itemnum, $borrowernumber, "%$due%" );
519 if ( my $data = $sth->fetchrow_hashref ) {
521 # we're updating an existing fine. Only modify if amount changed
522 # Note that in the current implementation, you cannot pay against an accruing fine
523 # (i.e. , of accounttype 'FU'). Doing so will break accrual.
524 if ( $data->{'amount'} != $amount ) {
525 my $diff = $amount - $data->{'amount'};
526 #3341: diff could be positive or negative!
527 my $out = $data->{'amountoutstanding'} + $diff;
530 SET date=now(), amount=?, amountoutstanding=?,
531 lastincrement=?, accounttype='FU'
532 WHERE borrowernumber=?
534 AND accounttype IN ('FU','O')
535 AND description LIKE ?
537 my $sth2 = $dbh->prepare($query);
538 # FIXME: BOGUS query cannot ensure uniqueness w/ LIKE %x% !!!
539 # LIMIT 1 added to prevent multiple affected lines
540 # FIXME: accountlines table needs unique key!! Possibly a combo of borrowernumber and accountline.
541 # But actually, we should just have a regular autoincrementing PK and forget accountline,
542 # including the bogus getnextaccountno function (doesn't prevent conflict on simultaneous ops).
543 # FIXME: Why only 2 account types here?
544 $debug and print STDERR "UpdateFine query: $query\n" .
545 "w/ args: $amount, $out, $diff, $data->{'borrowernumber'}, $data->{'itemnumber'}, \"\%$due\%\"\n";
546 $sth2->execute($amount, $out, $diff, $data->{'borrowernumber'}, $data->{'itemnumber'}, "%$due%");
548 # print "no update needed $data->{'amount'}"
551 my $sth4 = $dbh->prepare(
552 "SELECT title FROM biblio LEFT JOIN items ON biblio.biblionumber=items.biblionumber WHERE items.itemnumber=?"
554 $sth4->execute($itemnum);
555 my $title = $sth4->fetchrow;
557 # # print "not in account";
558 # my $sth3 = $dbh->prepare("Select max(accountno) from accountlines");
561 # # FIXME - Make $accountno a scalar.
562 # my @accountno = $sth3->fetchrow_array;
566 my $nextaccntno = C4::Accounts::getnextacctno($borrowernumber);
567 my $desc = ($type ? "$type " : '') . "$title $due"; # FIXEDME, avoid whitespace prefix on empty $type
568 my $query = "INSERT INTO accountlines
569 (borrowernumber,itemnumber,date,amount,description,accounttype,amountoutstanding,lastincrement,accountno)
570 VALUES (?,?,now(),?,?,'FU',?,?,?)";
571 my $sth2 = $dbh->prepare($query);
572 $debug and print STDERR "UpdateFine query: $query\nw/ args: $borrowernumber, $itemnum, $amount, $desc, $amount, $amount, $nextaccntno\n";
573 $sth2->execute($borrowernumber, $itemnum, $amount, $desc, $amount, $amount, $nextaccntno);
580 "due=".$due." amount=".$amount." itemnumber=".$itemnum
581 ) if C4::Context->preference("FinesLog");
586 $borrower = &BorType($borrowernumber);
588 Looks up a patron by borrower number.
590 C<$borrower> is a reference-to-hash whose keys are all of the fields
591 from the borrowers and categories tables of the Koha database. Thus,
592 C<$borrower> contains all information about both the borrower and
593 category he or she belongs to.
599 my ($borrowernumber) = @_;
600 my $dbh = C4::Context->dbh;
601 my $sth = $dbh->prepare(
602 "SELECT * from borrowers
603 LEFT JOIN categories ON borrowers.categorycode=categories.categorycode
604 WHERE borrowernumber=?"
606 $sth->execute($borrowernumber);
607 return $sth->fetchrow_hashref;
610 =head2 ReplacementCost
612 $cost = &ReplacementCost($itemnumber);
614 Returns the replacement cost of the item with the given item number.
619 sub ReplacementCost {
621 my $dbh = C4::Context->dbh;
623 $dbh->prepare("Select replacementprice from items where itemnumber=?");
624 $sth->execute($itemnum);
626 # FIXME - Use fetchrow_array or a slice.
627 my $data = $sth->fetchrow_hashref;
628 return ( $data->{'replacementprice'} );
633 $data->{'sum(amountoutstanding)'} = &GetFine($itemnum,$borrowernumber);
635 return the total of fine
637 C<$itemnum> is item number
639 C<$borrowernumber> is the borrowernumber
645 my ( $itemnum, $borrowernumber ) = @_;
646 my $dbh = C4::Context->dbh();
647 my $query = "SELECT sum(amountoutstanding) FROM accountlines
648 where accounttype like 'F%'
649 AND amountoutstanding > 0 AND itemnumber = ? AND borrowernumber=?";
650 my $sth = $dbh->prepare($query);
651 $sth->execute( $itemnum, $borrowernumber );
652 my $data = $sth->fetchrow_hashref();
653 return ( $data->{'sum(amountoutstanding)'} );
657 =head2 GetIssuingRules
659 FIXME - This sub should be deprecated and removed.
660 It ignores branch and defaults.
662 $data = &GetIssuingRules($itemtype,$categorycode);
664 Looks up for all issuingrules an item info
666 C<$itemnumber> is a reference-to-hash whose keys are all of the fields
667 from the borrowers and categories tables of the Koha database. Thus,
669 C<$categorycode> contains information about borrowers category
671 C<$data> contains all information about both the borrower and
672 category he or she belongs to.
675 sub GetIssuingRules {
676 warn "GetIssuingRules is deprecated: use GetIssuingRule from C4::Circulation instead.";
677 my ($itemtype,$categorycode)=@_;
678 my $dbh = C4::Context->dbh();
679 my $query=qq|SELECT *
681 WHERE issuingrules.itemtype=?
682 AND issuingrules.categorycode=?
684 my $sth = $dbh->prepare($query);
686 $sth->execute($itemtype,$categorycode);
687 return $sth->fetchrow_hashref;
691 sub ReplacementCost2 {
692 my ( $itemnum, $borrowernumber ) = @_;
693 my $dbh = C4::Context->dbh();
694 my $query = "SELECT amountoutstanding
696 WHERE accounttype like 'L'
697 AND amountoutstanding > 0
699 AND borrowernumber= ?";
700 my $sth = $dbh->prepare($query);
701 $sth->execute( $itemnum, $borrowernumber );
702 my $data = $sth->fetchrow_hashref();
703 return ( $data->{'amountoutstanding'} );
707 =head2 GetNextIdNotify
709 ($result) = &GetNextIdNotify($reference);
711 Returns the new file number
713 C<$result> contains the next file number
715 C<$reference> contains the beggining of file number
719 sub GetNextIdNotify {
720 my ($reference) = @_;
721 my $query = qq|SELECT max(notify_id)
723 WHERE notify_id like \"$reference%\"
726 # AND borrowernumber=?|;
727 my $dbh = C4::Context->dbh;
728 my $sth = $dbh->prepare($query);
730 my $result = $sth->fetchrow;
732 if ( $result eq '' ) {
733 ( $result = $reference . "01" );
736 $count = substr( $result, 6 ) + 1;
739 ( $count = "0" . $count );
741 $result = $reference . $count;
746 =head2 NumberNotifyId
748 (@notify) = &NumberNotifyId($borrowernumber);
750 Returns amount for all file per borrowers
751 C<@notify> array contains all file per borrowers
753 C<$notify_id> contains the file number for the borrower number nad item number
758 my ($borrowernumber)=@_;
759 my $dbh = C4::Context->dbh;
760 my $query=qq| SELECT distinct(notify_id)
762 WHERE borrowernumber=?|;
764 my $sth = $dbh->prepare($query);
765 $sth->execute($borrowernumber);
766 while ( my ($numberofnotify) = $sth->fetchrow ) {
767 push( @notify, $numberofnotify );
774 ($totalnotify) = &AmountNotify($notifyid);
776 Returns amount for all file per borrowers
777 C<$notifyid> is the file number
779 C<$totalnotify> contains amount of a file
781 C<$notify_id> contains the file number for the borrower number and item number
786 my ($notifyid,$borrowernumber)=@_;
787 my $dbh = C4::Context->dbh;
788 my $query=qq| SELECT sum(amountoutstanding)
790 WHERE notify_id=? AND borrowernumber = ?|;
791 my $sth=$dbh->prepare($query);
792 $sth->execute($notifyid,$borrowernumber);
793 my $totalnotify=$sth->fetchrow;
795 return ($totalnotify);
801 ($notify_id) = &GetNotifyId($borrowernumber,$itemnumber);
803 Returns the file number per borrower and itemnumber
805 C<$borrowernumber> is a reference-to-hash whose keys are all of the fields
806 from the items tables of the Koha database. Thus,
808 C<$itemnumber> contains the borrower categorycode
810 C<$notify_id> contains the file number for the borrower number nad item number
815 my ( $borrowernumber, $itemnumber ) = @_;
816 my $query = qq|SELECT notify_id
818 WHERE borrowernumber=?
820 AND (accounttype='FU' or accounttype='O')|;
821 my $dbh = C4::Context->dbh;
822 my $sth = $dbh->prepare($query);
823 $sth->execute( $borrowernumber, $itemnumber );
824 my ($notify_id) = $sth->fetchrow;
829 =head2 CreateItemAccountLine
831 () = &CreateItemAccountLine($borrowernumber, $itemnumber, $date, $amount,
832 $description, $accounttype, $amountoutstanding,
833 $timestamp, $notify_id, $level);
835 update the account lines with file number or with file level
837 C<$items> is a reference-to-hash whose keys are all of the fields
838 from the items tables of the Koha database. Thus,
840 C<$itemnumber> contains the item number
842 C<$borrowernumber> contains the borrower number
844 C<$date> contains the date of the day
846 C<$amount> contains item price
848 C<$description> contains the descritpion of accounttype
850 C<$accounttype> contains the account type
852 C<$amountoutstanding> contains the $amountoutstanding
854 C<$timestamp> contains the timestamp with time and the date of the day
856 C<$notify_id> contains the file number
858 C<$level> contains the file level
862 sub CreateItemAccountLine {
864 $borrowernumber, $itemnumber, $date, $amount,
865 $description, $accounttype, $amountoutstanding, $timestamp,
868 my $dbh = C4::Context->dbh;
869 my $nextaccntno = C4::Accounts::getnextacctno($borrowernumber);
870 my $query = "INSERT into accountlines
871 (borrowernumber,accountno,itemnumber,date,amount,description,accounttype,amountoutstanding,timestamp,notify_id,notify_level)
873 (?,?,?,?,?,?,?,?,?,?,?)";
875 my $sth = $dbh->prepare($query);
877 $borrowernumber, $nextaccntno, $itemnumber,
878 $date, $amount, $description,
879 $accounttype, $amountoutstanding, $timestamp,
884 =head2 UpdateAccountLines
886 () = &UpdateAccountLines($notify_id,$notify_level,$borrowernumber,$itemnumber);
888 update the account lines with file number or with file level
890 C<$items> is a reference-to-hash whose keys are all of the fields
891 from the items tables of the Koha database. Thus,
893 C<$itemnumber> contains the item number
895 C<$notify_id> contains the file number
897 C<$notify_level> contains the file level
899 C<$borrowernumber> contains the borrowernumber
903 sub UpdateAccountLines {
904 my ( $notify_id, $notify_level, $borrowernumber, $itemnumber ) = @_;
906 if ( $notify_id eq '' ) {
907 $query = qq|UPDATE accountlines
909 WHERE borrowernumber=? AND itemnumber=?
910 AND (accounttype='FU' or accounttype='O')|;
912 $query = qq|UPDATE accountlines
913 SET notify_id=?, notify_level=?
914 WHERE borrowernumber=?
916 AND (accounttype='FU' or accounttype='O')|;
919 my $sth = C4::Context->dbh->prepare($query);
920 if ( $notify_id eq '' ) {
921 $sth->execute( $notify_level, $borrowernumber, $itemnumber );
923 $sth->execute( $notify_id, $notify_level, $borrowernumber, $itemnumber );
929 ($items) = &GetItems($itemnumber);
931 Returns the list of all delays from overduerules.
933 C<$items> is a reference-to-hash whose keys are all of the fields
934 from the items tables of the Koha database. Thus,
936 C<$itemnumber> contains the borrower categorycode
940 # FIXME: This is a bad function to have here.
941 # Shouldn't it be in C4::Items?
942 # Shouldn't it be called GetItem since you only get 1 row?
943 # Shouldn't it be called GetItem since you give it only 1 itemnumber?
946 my $itemnumber = shift or return;
947 my $query = qq|SELECT *
950 my $sth = C4::Context->dbh->prepare($query);
951 $sth->execute($itemnumber);
952 my ($items) = $sth->fetchrow_hashref;
956 =head2 GetOverdueDelays
958 (@delays) = &GetOverdueDelays($categorycode);
960 Returns the list of all delays from overduerules.
962 C<@delays> it's an array contains the three delays from overduerules table
964 C<$categorycode> contains the borrower categorycode
968 sub GetOverdueDelays {
970 my $query = qq|SELECT delay1,delay2,delay3
972 WHERE categorycode=?|;
973 my $sth = C4::Context->dbh->prepare($query);
974 $sth->execute($category);
975 my (@delays) = $sth->fetchrow_array;
979 =head2 GetBranchcodesWithOverdueRules
981 my @branchcodes = C4::Overdues::GetBranchcodesWithOverdueRules()
983 returns a list of branch codes for branches with overdue rules defined.
987 sub GetBranchcodesWithOverdueRules {
988 my $dbh = C4::Context->dbh;
989 my $rqoverduebranches = $dbh->prepare("SELECT DISTINCT branchcode FROM overduerules WHERE delay1 IS NOT NULL AND branchcode <> '' ORDER BY branchcode");
990 $rqoverduebranches->execute;
991 my @branches = map { shift @$_ } @{ $rqoverduebranches->fetchall_arrayref };
993 my $availbranches = C4::Branch::GetBranches();
994 @branches = keys %$availbranches;
999 =head2 CheckAccountLineLevelInfo
1001 ($exist) = &CheckAccountLineLevelInfo($borrowernumber,$itemnumber,$accounttype,notify_level);
1003 Check and Returns the list of all overdue books.
1005 C<$exist> contains number of line in accounlines
1006 with the same .biblionumber,itemnumber,accounttype,and notify_level
1008 C<$borrowernumber> contains the borrower number
1010 C<$itemnumber> contains item number
1012 C<$accounttype> contains account type
1014 C<$notify_level> contains the accountline level
1019 sub CheckAccountLineLevelInfo {
1020 my ( $borrowernumber, $itemnumber, $level ) = @_;
1021 my $dbh = C4::Context->dbh;
1022 my $query = qq|SELECT count(*)
1024 WHERE borrowernumber =?
1026 AND notify_level=?|;
1027 my $sth = $dbh->prepare($query);
1028 $sth->execute( $borrowernumber, $itemnumber, $level );
1029 my ($exist) = $sth->fetchrow;
1033 =head2 GetOverduerules
1035 ($overduerules) = &GetOverduerules($categorycode);
1037 Returns the value of borrowers (debarred or not) with notify level
1039 C<$overduerules> return value of debbraed field in overduerules table
1041 C<$category> contains the borrower categorycode
1043 C<$notify_level> contains the notify level
1047 sub GetOverduerules {
1048 my ( $category, $notify_level ) = @_;
1049 my $dbh = C4::Context->dbh;
1050 my $query = qq|SELECT debarred$notify_level
1052 WHERE categorycode=?|;
1053 my $sth = $dbh->prepare($query);
1054 $sth->execute($category);
1055 my ($overduerules) = $sth->fetchrow;
1056 return ($overduerules);
1060 =head2 CheckBorrowerDebarred
1062 ($debarredstatus) = &CheckBorrowerDebarred($borrowernumber);
1064 Check if the borrowers is already debarred
1066 C<$debarredstatus> return 0 for not debarred and return 1 for debarred
1068 C<$borrowernumber> contains the borrower number
1072 # FIXME: Shouldn't this be in C4::Members?
1073 sub CheckBorrowerDebarred {
1074 my ($borrowernumber) = @_;
1075 my $dbh = C4::Context->dbh;
1079 WHERE borrowernumber=?
1081 my $sth = $dbh->prepare($query);
1082 $sth->execute($borrowernumber);
1083 my ($debarredstatus) = $sth->fetchrow;
1084 return ( $debarredstatus eq '1' ? 1 : 0 );
1087 =head2 UpdateBorrowerDebarred
1089 ($borrowerstatut) = &UpdateBorrowerDebarred($borrowernumber);
1091 update status of borrowers in borrowers table (field debarred)
1093 C<$borrowernumber> borrower number
1097 sub UpdateBorrowerDebarred{
1098 my($borrowernumber) = @_;
1099 my $dbh = C4::Context->dbh;
1100 my $query=qq|UPDATE borrowers
1102 WHERE borrowernumber=?
1104 my $sth=$dbh->prepare($query);
1105 $sth->execute($borrowernumber);
1110 =head2 CheckExistantNotifyid
1112 ($exist) = &CheckExistantNotifyid($borrowernumber,$itemnumber,$accounttype,$notify_id);
1114 Check and Returns the notify id if exist else return 0.
1116 C<$exist> contains a notify_id
1118 C<$borrowernumber> contains the borrower number
1120 C<$date_due> contains the date of item return
1125 sub CheckExistantNotifyid {
1126 my ( $borrowernumber, $date_due ) = @_;
1127 my $dbh = C4::Context->dbh;
1128 my $query = qq|SELECT notify_id FROM accountlines
1129 LEFT JOIN issues ON issues.itemnumber= accountlines.itemnumber
1130 WHERE accountlines.borrowernumber =?
1132 my $sth = $dbh->prepare($query);
1133 $sth->execute( $borrowernumber, $date_due );
1134 return $sth->fetchrow || 0;
1137 =head2 CheckAccountLineItemInfo
1139 ($exist) = &CheckAccountLineItemInfo($borrowernumber,$itemnumber,$accounttype,$notify_id);
1141 Check and Returns the list of all overdue items from the same file number(notify_id).
1143 C<$exist> contains number of line in accounlines
1144 with the same .biblionumber,itemnumber,accounttype,notify_id
1146 C<$borrowernumber> contains the borrower number
1148 C<$itemnumber> contains item number
1150 C<$accounttype> contains account type
1152 C<$notify_id> contains the file number
1156 sub CheckAccountLineItemInfo {
1157 my ( $borrowernumber, $itemnumber, $accounttype, $notify_id ) = @_;
1158 my $dbh = C4::Context->dbh;
1159 my $query = qq|SELECT count(*) FROM accountlines
1160 WHERE borrowernumber =?
1164 my $sth = $dbh->prepare($query);
1165 $sth->execute( $borrowernumber, $itemnumber, $accounttype, $notify_id );
1166 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("
1184 AND notify_level = ?
1185 AND itemnumber = ? ");
1186 $sth->execute($notify_id,$notify_level,$itemnumber);
1187 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
1197 FIXME: This function should be renamed.
1201 sub GetOverduesForBranch {
1202 my ( $branch, $location) = @_;
1203 my $itype_link = (C4::Context->preference('item-level_itypes')) ? " items.itype " : " biblioitems.itemtype ";
1204 my $dbh = C4::Context->dbh;
1207 borrowers.borrowernumber,
1209 borrowers.firstname,
1214 biblio.biblionumber,
1218 branches.branchname,
1221 items.itemcallnumber,
1224 itemtypes.description,
1225 accountlines.notify_id,
1226 accountlines.notify_level,
1227 accountlines.amountoutstanding
1229 LEFT JOIN issues ON issues.itemnumber = accountlines.itemnumber
1230 AND issues.borrowernumber = accountlines.borrowernumber
1231 LEFT JOIN borrowers ON borrowers.borrowernumber = accountlines.borrowernumber
1232 LEFT JOIN items ON items.itemnumber = issues.itemnumber
1233 LEFT JOIN biblio ON biblio.biblionumber = items.biblionumber
1234 LEFT JOIN biblioitems ON biblioitems.biblioitemnumber = items.biblioitemnumber
1235 LEFT JOIN itemtypes ON itemtypes.itemtype = $itype_link
1236 LEFT JOIN branches ON branches.branchcode = issues.branchcode
1237 WHERE (accountlines.amountoutstanding != '0.000000')
1238 AND (accountlines.accounttype = 'FU' )
1239 AND (issues.branchcode = ? )
1240 AND (issues.date_due < CURDATE())
1246 $sth = $dbh->prepare("$select AND items.location = ? ORDER BY borrowers.surname, borrowers.firstname");
1247 $sth->execute($branch, $location);
1249 $sth = $dbh->prepare("$select ORDER BY borrowers.surname, borrowers.firstname");
1250 $sth->execute($branch);
1252 while ( my $data = $sth->fetchrow_hashref ) {
1253 #check if the document has already been notified
1254 my $countnotify = CheckItemNotify($data->{'notify_id'}, $data->{'notify_level'}, $data->{'itemnumber'});
1255 if ($countnotify eq '0') {
1256 $getoverdues[$i] = $data;
1260 return (@getoverdues);
1264 =head2 AddNotifyLine
1266 &AddNotifyLine($borrowernumber, $itemnumber, $overduelevel, $method, $notifyId)
1268 Create a line into notify, if the method is phone, the notification_send_date is implemented to
1273 my ( $borrowernumber, $itemnumber, $overduelevel, $method, $notifyId ) = @_;
1274 my $dbh = C4::Context->dbh;
1275 if ( $method eq "phone" ) {
1276 my $sth = $dbh->prepare(
1277 "INSERT INTO notifys (borrowernumber,itemnumber,notify_date,notify_send_date,notify_level,method,notify_id)
1278 VALUES (?,?,now(),now(),?,?,?)"
1280 $sth->execute( $borrowernumber, $itemnumber, $overduelevel, $method,
1284 my $sth = $dbh->prepare(
1285 "INSERT INTO notifys (borrowernumber,itemnumber,notify_date,notify_level,method,notify_id)
1286 VALUES (?,?,now(),?,?,?)"
1288 $sth->execute( $borrowernumber, $itemnumber, $overduelevel, $method,
1294 =head2 RemoveNotifyLine
1296 &RemoveNotifyLine( $borrowernumber, $itemnumber, $notify_date );
1298 Cancel a notification
1302 sub RemoveNotifyLine {
1303 my ( $borrowernumber, $itemnumber, $notify_date ) = @_;
1304 my $dbh = C4::Context->dbh;
1305 my $sth = $dbh->prepare(
1306 "DELETE FROM notifys
1312 $sth->execute( $borrowernumber, $itemnumber, $notify_date );
1321 Koha Development Team <http://koha-community.org/>